AutoCAD .NET API can also define lisp functions. Just like creating commands through specifying the CommandMethod attribute for a method, we can specify the LispFunction attribute for a special kind of methods to define LISP functions.
The method needs to accept a ResultBuffer argument and generally returns a common Object. By the way, the method should be local. It cannot be global or static. It must be public, certainly.
[LispFunction("DoubleMe")]
public Object DoubleMe_LispFunction(ResultBuffer rb)
{
Object ret = null;
if (rb != null)
{
TypedValue[] tvArr = rb.AsArray();
if (tvArr.Count() > 0)
{
if (tvArr.Count() == 1) // a single value
{
ret = DoubleValue(tvArr[0]);
}
else // a list
{
ret = rb;
}
}
}
return ret;
}
Object DoubleValue(TypedValue tv)
{
Object ret = null;
if (tv.TypeCode == (int)LispDataType.T_atom)
{
ret = true;
}
else if (tv.TypeCode == (int)LispDataType.Int16)
{
ret = ((Int16)tv.Value) * 2;
}
else if (tv.TypeCode == (int)LispDataType.Int32)
{
ret = ((Int32)tv.Value) * 2;
}
else if (tv.TypeCode == (int)LispDataType.Double
|| tv.TypeCode == (int)LispDataType.Angle)
{
ret = ((double)tv.Value) * 2.0;
}
else if (tv.TypeCode == (int)LispDataType.None
|| tv.TypeCode == (int)LispDataType.Nil
|| tv.TypeCode == (int)LispDataType.Void)
{
ret = null;
}
else if (tv.TypeCode == (int)LispDataType.Text)
{
ret = (tv.Value as String) + (tv.Value as String);
}
else if (tv.TypeCode == (int)LispDataType.Point2d)
{
Point2d p2d = (Point2d)tv.Value * 2.0;
ret = p2d;
}
else if (tv.TypeCode == (int)LispDataType.Point3d)
{
Point3d p3d = (Point3d)tv.Value * 2.0;
ret = p3d;
}
else if (tv.TypeCode == (int)LispDataType.ObjectId)
{
ret = tv.Value;
}
else if (tv.TypeCode == (int)LispDataType.SelectionSet)
{
ret = tv.Value;
}
else if (tv.TypeCode == (int)LispDataType.ListBegin)
{
ret = tv.Value;
Debug.WriteLine(string.Format("TypeCode: {0}/ListBegin\t\tValue: {1}", tv.TypeCode, tv.Value));
}
else if (tv.TypeCode == (int)LispDataType.ListEnd)
{
ret = tv.Value;
Debug.WriteLine(string.Format("TypeCode: {0}/ListEnd\t\tValue: {1}", tv.TypeCode, tv.Value));
}
else if (tv.TypeCode == (int)LispDataType.Orientation)
{
ret = tv.Value;
Debug.WriteLine(string.Format("TypeCode: {0}/Orientation\t\tValue: {1}", tv.TypeCode, tv.Value));
}
else if (tv.TypeCode == (int)LispDataType.DottedPair)
{
ret = tv.Value;
Debug.WriteLine(string.Format("TypeCode: {0}/DottedPair\t\tValue: {1}", tv.TypeCode, tv.Value));
}
else
{
ret = tv;
}
return ret;
}
There are totally only around a couple of dozens of lines of code above, but various input types and return types can be exercised by the single same LISP function definition. In fact, the return/output types of the LISP function depend on its input. The function tries to double its input value and return it if possible and convenient.
The following are some example outputs in the AutoCAD command line window when the LISP function is exercised:
Command: (doubleme 1.1 )
2.2
Command: (doubleme 0 )
0
Command: (doubleme 1.5)
3.0
Command: (doubleme 3)
6
Command: (doubleme pi)
6.28319
Command: (doubleme (sin (/ pi 6)))
1.0
Command: (doubleme '( 1 2 3) )
(2.0 4.0 6.0)
Command: (doubleme '( 1 ) )
(1)
Command: (doubleme T )
T
Command: (doubleme nil)
nil
Command: (doubleme )
nil
Command: (doubleme (> 0 1))
nil
Command: (doubleme (> 2 1))
T
Command: (doubleme "ab" "cd" )
("ab" "cd")
Command: (doubleme '(8 . "aabb"))
(8 . "aabb")
Command: (doubleme 1.1 2.2 3.3)
(1.1 2.2 3.3)
Command: (doubleme 1.1 2.2 )
(1.1 2.2)
Command: (setq id (car (entsel)))
Select object: <Entity name: 7ffffb09f10>
Command: (doubleme id)
<Entity name: 7ffffb09f10>
Command: (setq entlst (entget id))
((-1 . <Entity name: 7ffffb09f10>) (0 . "CIRCLE") (330 . <Entity name:
7ffffb069f0>) (5 . "231") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 .
"0") (100 . "AcDbCircle") (10 -16.1102 8.8777 0.0) (40 . 9.78317) (210 0.0 0.0
1.0))
Command: (doubleme entlst)
((-1 . <Entity name: 7ffffb09f10>) (0 . "CIRCLE") (330 . <Entity name:
7ffffb069f0>) (5 . "231") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 .
"0") (100 . "AcDbCircle") (10 -16.1102 8.8777 0.0) (40 . 9.78317) (210 0.0 0.0
1.0))
Command: (setq pnt (getpoint))
(65.4005 11.0181 0.0)
Command: (doubleme pnt)
(130.801 22.0363 0.0)
Command: (setq angle (getangle))
Specify second point: 0.701371
Command: (doubleme angle)
1.40274
Command: (setq ss (ssget))
11 found
<Selection set: 1e>
Command: (doubleme ss)
<Selection set: 1e>
Command: (doubleme "abcd")
"abcdabcd"
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides a LispFunction Definer to help us define LISP functions from .NET addins.
Posted by: |