当定义一个 AutoLISP 函数时,应该使用 LispFunction 属性。LispFunction 属性期待一个字符串值 ,它将当作 AutoLISP 函数的全局名称。和全局函数名称一起,LispFunction 结构还能接受如下值:
下面演示了使用LispFunction属性定义了一个名为 InsertDynamicBlock 的 AutoLISP 函数。
通过 AutoLISP 函数,使用 Foreach 遍历 ResultBuffer 返回的值。ResultBuffer 是 TypedValue 对象的集合。TypedValue 对象的 TypeCode 属性可以用来确定每一个传递到 AutoLISP 函数的值的值类型。Value 属性用于返回 TypedValue 对象的值。
这个救命代码定义一个命名为 DisplayFullName 的 AutoLISP 函数。 虽然在 .NET 工程中定义的这个方法接收一个值,但是这个 AutoLISP 函数期待两个字符串值以给出合适的输出。
加载 .NET 工程到 AutoCAD 中并在命令提示中输入下列的命令:
(displayfullname "First" "Last")
Name: First Last
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
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);
}
}