How to pass autolisp List to .Net LispFunction in c#

How to pass autolisp List to .Net LispFunction in c#

jadhav_vilas84
Contributor Contributor
2,001 Views
4 Replies
Message 1 of 5

How to pass autolisp List to .Net LispFunction in c#

jadhav_vilas84
Contributor
Contributor

Hi, 

 

from Autolisp :

(setq xmlfilepath  "C:\myxml.xml")

(setq mylst (list (list "fsi_perm" 1200) (list "fsi_Basic" 1300) (list "fsi_temp" 1400) )

(updatexml xmlfilepath "ProjectDetails" mylst )

 

In .net C#

[LispFunction("updatexml")]

public _AcDb.ResultBuffer updatexml(_AcDb.ResultBuffer rbArgs)

{

   _AcDb.ResultBuffer resBufOut = new _AcDb.ResultBuffer();
   Array argsArray = null;
argsArray = rbArgs.AsArray();

string xmlfilename = argsArray.GetValue(0).ToString();

  /// how to receive mylst  value and split  it as single value 


}

 

0 Likes
Accepted solutions (1)
2,002 Views
4 Replies
Replies (4)
Message 2 of 5

_gile
Consultant
Consultant
Accepted solution

Hi

 

Assuming the arguments always are two strings and a list of string short pairs, you can do something like this:

 

        [LispFunction("updatexml")]
        public static ResultBuffer UpdateXml(ResultBuffer resbuf)
        {
            var args = resbuf.AsArray();

            string xmlfilename = (string)args[0].Value;

            string someString = (string)args[1].Value;

            var list = new List<KeyValuePair<string, short>>();
            for (int i = 3; i < args.Length - 1; i += 4)
            {
                list.Add(new KeyValuePair<string, short>(
                    (string)args[i + 1].Value,
                    (short)args[i + 2].Value));
            }

            // ...
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 5

Anonymous
Not applicable

namespace wcq1
{
public class Class1
{
  [CommandMethod("WCQ")] //向AutoCAD注册新的命令
  [LispFunction("Cus")] //注册一个LISP函数,Check user Status  

  public void WCQ()
  {
      Document acDoc = Application.DocumentManager.MdiActiveDocument; //获取当前活动文档
      Editor acEd = acDoc.Editor; //当前编辑器对象

      acEd.WriteMessage("\nthis can work");
  }
  public bool Cus()
  {
        return true;
  }
  }
}

 

 

when I use (cus) in AutoCAD, wrong

命令: (cus)
System.ArgumentException: 无法绑定到目标方法,因其签名或安全透明度与委托类型的签名或安全透明度不兼容。
在 System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method, Boolean throwOnBindFailure)
在 System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method)
在 Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorker(MethodInfo mi, Object commandObject, Boolean bLispFunction)
在 Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorkerWithExceptionFilter(MethodInfo mi, Object commandObject, Boolean bLispFunction)
在 Autodesk.AutoCAD.Runtime.PerDocumentCommandClass.Invoke(MethodInfo mi, Boolean bLispFunction)
在 Autodesk.AutoCAD.Runtime.CommandClass.CommandThunk.InvokeLisp(); 错误: ADS 请求错误

0 Likes
Message 4 of 5

_gile
Consultant
Consultant

Hi,

The attribute must be just before the method it decores.

the method decored with LispFunction attribute must have a single argument of type ResultBuffer (even if null).

Read this topic:

https://help.autodesk.com/view/OARX/2019/ENU/?guid=GUID-3B2760FE-A0DC-4229-AEBE-5CC83290BA95

 

  [CommandMethod("WCQ")] //向AutoCAD注册新的命令 
  public void WCQ()
  {
      Document acDoc = Application.DocumentManager.MdiActiveDocument; //获取当前活动文档
      Editor acEd = acDoc.Editor; //当前编辑器对象

      acEd.WriteMessage("\nthis can work");
  }

  [LispFunction("Cus")] //注册一个LISP函数,Check user Status 
  public bool Cus( ResultBuffer resbuf)
  {
        return true;
  }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 5

Anonymous
Not applicable

Great! it works fine.

thx very much.

 

0 Likes