Create Lisp function with arguments in .net

Create Lisp function with arguments in .net

Anonymous
Not applicable
2,719 Views
3 Replies
Message 1 of 4

Create Lisp function with arguments in .net

Anonymous
Not applicable

I found a sample of code on these forums that takes an object passed to the function via lisp and modifies it. I stripped it down some in order to just test that the function loads properly, and it does not seem to.

Imports Autodesk
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Colors
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Runtime
Imports System.Windows.Forms
Imports Autodesk.AutoCAD.EditorInput

Namespace LispFunctionTest



    Public Class Class1

        <LispFunction("msgtext")> _
        Public Sub msgtest(ByVal resbuf As ResultBuffer)
            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Dim ed As Editor = doc.Editor
            Dim db As Database = doc.Database
            If resbuf Is Nothing Then
                ed.WriteMessage(vbLf & "Error: too few arguments" & vbLf)
                Return
            End If
            Dim args As TypedValue() = resbuf.AsArray()
            If args.Length = 0 Then
                ed.WriteMessage(vbLf & "Error: too few arguments" & vbLf)
                Return
            End If
            If args.Length > 1 Then
                ed.WriteMessage(vbLf & "Error: too much arguments" & vbLf)
                Return
            End If
            If args(0).TypeCode <> LispDataType.Text Then
                ed.WriteMessage(vbLf & "Error: incorrect argument" & vbLf)
                Return
            End If
        End Sub

    End Class

End Namespace

 

 

It gives me the following error:

 

System.ArgumentException: Error binding to target method.
   at System.Delegate.CreateDelegate(Type type, Object firstArgument,
MethodInfo method, Boolean throwOnBindFailure)
   at System.Delegate.CreateDelegate(Type type, Object firstArgument,
MethodInfo method)
   at AcMgCommandClass.InvokeWorker(AcMgCommandClass* , MethodInfo mi, Object
commandObject, Boolean bLispFunction)
   at AcMgCommandClass.InvokeWorkerWithExceptionFilter(AcMgCommandClass* ,
MethodInfo mi, Object commandObject, Boolean bLispFunction)
   at AcMgPerDocumentCommandClass.Invoke(AcMgPerDocumentCommandClass* ,
gcroot<System::Reflection::MethodInfo ^>* mi, Boolean bLispFunction)
   at AcMgCommandClass.CommandThunk.InvokeLisp(CommandThunk* )*Cancel*

 

Any help would be much appreciated.

0 Likes
Accepted solutions (2)
2,720 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable
Accepted solution

Works here with AutoCAD 2011 SP1.

 

I could be remembering incorrectly, but I think other versions may have required a function returning an object, i.e.

 

Public Function msgtest(ByVal resbuf As ResultBuffer) As Object

 

Does it change if you do that (and return Nothing?)

 

-drg

Message 3 of 4

_gile
Consultant
Consultant
Accepted solution

Hi,

 

A LISP function may always return something even nil: new TypedValue(LispDataType.Nil).

It seems that A2008 requires the return to be a ResultBuffer, other versions (2007, 2009, 2010, 2011) accept any type of object.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 4 of 4

Anonymous
Not applicable

Worked like a charm returning nothing. Thanks!

0 Likes