AutoLISP Function Definition (.NET)

When defining an AutoLISP function, you use the LispFunction attribute. The LispFunction attribute expects a string value to use as the global name of the AutoLISP function that is being defined. Along with a global function name, the LispFunction structure can accept the following values:

Syntax to Define an AutoLISP Function

The following demonstrates the creation of a LispFunction attribute that defines an AutoLISP function named InsertDynamicBlock.

VB.NET

<LispFunction("InsertDynamicBlock")> _
Public Sub InsertDynamicBlock(ByVal rbArgs As ResultBuffer)
 . . .
End Sub

C#

[LispFunction("DisplayFullName")]
public static void DisplayFullName(ResultBuffer rbArgs)
{
 . . .
}

Retrieve Values Passed into an AutoLISP Function

Use a Foreach loop to step through the values returned in the ResultBuffer by the AutoLISP function. A ResultBuffer is a collection of TypedValue objects. The TypeCode property of a TypedValue object can be used to determine the value type for each value passed into the AutoLISP function. The Value property is used to return the value of the TypedValue object.

To define an AutoLISP Function

This example code defines an AutoLISP function named DisplayFullName. While the method defined in the .NET project accepts a single value, the AutoLISP function expects two string values to produce the correct output.

Load the .NET project into AutoCAD and enter the following at the Command prompt:

(displayfullname "First" "Last")

The following is the output displayed after the AutoLISP function is executed:

Name: First Last

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
 
<LispFunction("DisplayFullName")> _
Public Sub DisplayFullName(ByVal rbArgs As ResultBuffer)
  If Not rbArgs = Nothing Then
      Dim strVal1 As String = "", strVal2 As String = ""
 
      Dim nCnt As Integer = 0
      For Each rb As TypedValue In rbArgs
          If (rb.TypeCode = Autodesk.AutoCAD.Runtime.LispDataType.Text) Then
              Select Case nCnt
                  Case 0
                      strVal1 = rb.Value.ToString()
                  Case 1
                      strVal2 = rb.Value.ToString()
              End Select
 
              nCnt = nCnt + 1
          End If
      Next
 
      Application.DocumentManager.MdiActiveDocument.Editor. _
         WriteMessage(vbLf & "Name: " & strVal1 & " " & strVal2)
  End If
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
 
[LispFunction("DisplayFullName")]
public static void DisplayFullName(ResultBuffer rbArgs)
{
  if (rbArgs != null)
  {
      string strVal1 = "";
      string strVal2 = "";
 
      int nCnt = 0;
      foreach (TypedValue rb in rbArgs)
      {
          if (rb.TypeCode == (int)Autodesk.AutoCAD.Runtime.LispDataType.Text)
          {
              switch(nCnt)
              {
                  case 0:
                      strVal1 = rb.Value.ToString();
                      break;
                  case 1:
                      strVal2 = rb.Value.ToString();
                      break;
              }
 
              nCnt = nCnt + 1;
          }
      }
 
      Application.DocumentManager.MdiActiveDocument.Editor.
         WriteMessage("\nName: " + strVal1 + " " + strVal2);
  }
}