Problem Calling a LISP routine from C#

Problem Calling a LISP routine from C#

ganderson
Advocate Advocate
2,353 Views
2 Replies
Message 1 of 3

Problem Calling a LISP routine from C#

ganderson
Advocate
Advocate

Hi,

   I'm trying to setup layers using previously written LISP code in my C# program. My LISP code is:

(defun c:SetUpLayers ()
  (command "-layer" "m" "const1" "c" "1")
  (command "")
  (command "")
  (command "-layer" "m" "const2" "c" "5")
  (command "")
  (command "")
  (command "-layer" "m" "const3" "c" "3")
  (command "")
  (command "")
) ;ends and sets all layers for sloper building   

 

I am calling this function from C# with this code:

 

 [CommandMethod("SetUpLayers", CommandFlags.Modal)]
        public void SetUpLayers() //Setup AutoCad drawing Layers
        {
            // build the arguments list
            ResultBuffer args = new ResultBuffer(
                new TypedValue((int)LispDataType.Text, "c:SetUpLayers"));

            // call the LISP fuction anf get the return value
            ResultBuffer result = Autodesk.AutoCAD.ApplicationServices.Application.Invoke(args);
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            ed.WriteMessage(result.ToString());
        }

Although this code ran perfectly when I invoked it in a LISP program, I am getting this error when I try and run it from C#

; error: AutoCAD command rejected: "-layer"

 

Can someone help me out?

        Thanks,

                      ~Vonnie

0 Likes
Accepted solutions (1)
2,354 Views
2 Replies
Replies (2)
Message 2 of 3

_gile
Consultant
Consultant
Accepted solution

Hi,

 

It seems to me you cannot call commands from a LISP which is invoked from .NET (just writing this sentence shows how this process seems complex for such a simple stuff).

 

Why not directly call the commands from .NET (using Document.SendStringtoExecute() or Editor.Command() methods) ?

 

Or, better, add these layers (LayerTableRecord objects) to the LayerTable using the .NET API. This may be a good learning exercise.

 

Most of the time, calling LISP from .NET is definitely not a simple task and there are many issues you'll be facing due to the differences between the two environments (document context vs application context, interpreted language vs compiled language, dynamic types vs static types, ...)



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 3

ganderson
Advocate
Advocate

Thanks for your help. I have a program that I wrote years ago that I'd like to interact with the C# program that I am working on now. Up til now, all the LISP calls that I've made from C# have worked flawlessly, but I will take your advice and use the .NET API for working with Layers.

         ~Giovonnae

0 Likes