Retrieve Result from Asynchronous Command

Retrieve Result from Asynchronous Command

mtrd_001
Enthusiast Enthusiast
2,296 Views
3 Replies
Message 1 of 4

Retrieve Result from Asynchronous Command

mtrd_001
Enthusiast
Enthusiast

I am writing a netloaded dll for AutoCad which needs to invoke several Lisp commands (inbuilt Autocad Electrical commands such as wd_insym2), retrieve the returned result and stores it in a local .Net variable (a property of a custom class), then continue with the next command.

 

As it is one continuous execution, and each command often relies on the result of the previous command, ideally the commands would run synchronously. I have been using Application.Invoke() which is fairly effective for most commands, however some particular commands such as wd_insym2 seem to run additional commands such as UNDO, and it gives me errors like:

AutoCAD command rejected: "_.UNDO"

 which someone explained to me was due to reentrancy issues whereby my command calls the Electrical command, which in turn calls other commands such as UNDO, which is giving me the error. Can you confirm this is correct?

 

However, I can't see how I can do this with asynchronous commands. I tried using the doc.CommandEnded handler, and putting the .NET command into a loop until the doc.CommandEnded handler was activated, but that didn't work because the asynchronous commands were never executed.

 

I need a way of either satisfactorily executing commands synchronously, or alternatively pausing execution until the asynchronous command is executed.

 

Be gald of anyone's assistance/advice.

0 Likes
2,297 Views
3 Replies
Replies (3)
Message 2 of 4

SENL1362
Advisor
Advisor
Gile wrote some excellent examples how to use the new (A)sync commands
http://www.theswamp.org/index.php?topic=49124.0

Message 3 of 4

mtrd_001
Enthusiast
Enthusiast

The posts you refer to refer to the Editor.CommandAsync() command, which is new to AutoCad 2015. Unfortunately I only have AutoCad 2013.

 

Someone on another forum suggested using Await Editor.CommandAsync(), however I can't find any command within the ACad 2013 API that is a task that can be treated as asynchronous by .net (i.e. as a Task).

 

Is there ANY way, using only AutoCad 2013 API, to call a command asynchronously, then suspend execution until the command has been executed?

0 Likes
Message 4 of 4

SENL1362
Advisor
Advisor
You could use something like this [DllImport("accore.dll", EntryPoint = "acedCmd", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] extern static private int acedCmd(IntPtr resbuf); ... public static void Command(params object[] args) { ResultBuffer resbuf = new ResultBuffer(); foreach (object obj in args) { switch (obj.GetType().Name) { case "String": resbuf.Add(new TypedValue((int)LispDataType.Text, obj)); break; case "Int16": resbuf.Add(new TypedValue((int)LispDataType.Int16, obj)); break; ... acedCmd(resbuf.UnmanagedObject); } Command("YourAcadCommand"); var cmdNames = (string)Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("CMDNAMES"); while (cmdNames =="YOURACADCOMMAND") { System.Threading.Thread.Sleep(1000); var cmdNames = (string)Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("CMDNAMES"); }
0 Likes