马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
Autolisp Defun 函数需要定义为 (defun c:** ...) 方可被 Invoke 调用
The external function being invoked must be an external function in an ARX application currently loaded. From the external function's point of view, the acedInvoke() call is invisible. It retrieves its arguments by calling acedGetArgs() and returns a value by calling one of the value-return functions (acedRetInt(), acedRetReal(), and so on), as it does when invoked by an AutoLISP function or by the AutoCAD user.
以上是 help 中一段说明,其中提到 参数 要可以实现 acedGetArgs(),这个参数是 ObjectARX 定义 autolisp 函数都要实现的方式,在这个条件下 ARX 和 C# 的通讯就有了另外一个通道,看下面一个简单例子
ObjectARX 代码
- static int ads_test(void)
- {
- resbuf *args=acedGetArgs();
- if (args==NULL)
- {
- return RTNORM;
- }
- if (args->restype==RTREAL)
- {
- double num=args->resval.rreal;
- double xx = num*num;
- return acedRetReal(xx);
- }
- return RTNORM;
- }
复制代码 相应的写一个C#命令如下
- [SuppressUnmanagedCodeSecurity]
- [DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl)]
- extern static private int acedInvoke(IntPtr args, out IntPtr result);
-
- public static ResultBuffer CallLispFunction(ResultBuffer args)
- {
- var ip = IntPtr.Zero;
- var st = acedInvoke(args.UnmanagedObject, out ip);
- if (ip == IntPtr.Zero) return null;
- var rbRes = ResultBuffer.Create(ip, true);
- return rbRes;
- }
- [CommandMethod("MyGroup", "MyCommand", "MyCommandLocal", CommandFlags.Modal)]
- public void MyCommand() // This method can have any name
- {
- // Put your command code here
- var doc = Application.DocumentManager.MdiActiveDocument;
- var ed = doc.Editor;
- var prs = ed.GetDouble("\nInput number:");
- if (prs.Status==PromptStatus.OK)
- {
- var num = prs.Value;
- var rb = new ResultBuffer
- {
- new TypedValue((int) LispDataType.Text, "test"),
- new TypedValue((int) LispDataType.Double, num)
- };
- var ret=CallLispFunction(rb);
- if (ret!=null)
- {
- var arr=ret.AsArray();
- ed.WriteMessage("\nResult="+arr[0].Value.ToString());
- }
- }
- }
在 Acad 下加载运行,
命令: mycommand
Input number: 12.0
Result=144
|