cad二次开发吧 关注:662贴子:1,409
  • 0回复贴,共1

vb.NET:定义一个可以被LISP调用的函数

只看楼主收藏回复

.NET:定义一个可以被LISP调用的函数

在AutoCAD 2007的托管API中,你能够定义可以被Lisp调用的封装对象。在一般的.net程序中你都通过使用诸如<CommandMethod("MyCommand")>的属性来声明一个可以被AutoCAD调用的命令。在AutoCAD2007中,你只要把一个函数的属性声明为<LispFunction("MyLispFunction")> ,就可以在LISP程序中调用它了。 

这里要说明的是,你需要对传入的参数进行拆箱,而对输出的结果要进行装箱。  下面演示了如何处理参数以及对结果进行装箱: 

<LispFunction("LISPfunction")> _
Public Function VBfunction(ByVal rbfArgs As ResultBuffer) As ResultBuffer

    '获取输入的参数...
    Dim arInputArgs As Array
    Dim realArg1 As Double
    Dim intArg2 As Integer
    Dim strArg3 As String
    arInputArgs = rbfArgs.AsArray
    realArg1 = CType(arInputArgs.GetValue(0), TypedValue).Value
    intArg2 = CType(arInputArgs.GetValue(1), TypedValue).Value
    strArg3 = CType(arInputArgs.GetValue(2), TypedValue).Value

    '在这里做你想做的事情...
    '...
    '...

    '对结果装箱...
    '对double型使用RTREAL (5001)
    '对string型使用RTSTR (5003)
    '对整型使用RTSHORT (5005)
    Dim rbfResult As ResultBuffer
    rbfResult = New ResultBuffer( _
        New TypedValue(CInt(5001), 3.14159), _
        New TypedValue(CInt(5003), 42), _
        New TypedValue(CInt(5005), "Goodbye!"))
    Return rbfResult

End Function 

上面的代码假定传入的第一个参数为一实数,然后是一个整数,最后为一个字符串。下面是你在Lisp中调用这段代码后的结果: 

Command: (LISPfunction 1.234 9876 "Hello!")
(3.14159 42 "Goodbye!") 



IP属地:四川1楼2008-03-15 20:41回复