My question probably reveals my inexperience with API- I've been tearing through tutorials this week.
I'm really curious, is it possible to call or modify/inherit any autocad command? I'm looking to create a command that hatches the area between an offset, but I can't find any material on how to even utilize existing commands.
Thanks in advance for any help
Solved! Go to Solution.
You can call an AutoCAD command from your own command using one of the following techniques:
The .NET SendStringToExecute method
The ActiveX SendCommand method
P/Invoking the C++ acedCommand method - this technique has a slight risk to it, because of AutoCAD's use of Fiber technology (which the .NET Framework doesn't support), but it usually works without problem.
You can't modify an existing command, but you can redefine it or react to what it has done (using event callbacks).
You'll find some sample code if you search for all those method names on http://adndevblog.typepad.com/autocad/.
I've found that the best way to script AutoCAD commands from managed code is synchronously, using the undocumented RunCommand() method of the Editor class. The .NET framework's lack of support for the cooperative/non-preemptive threading model AutoCAD uses (e.g. 'fibers') is not a problem. Managed code runs in AutoCAD's cooperaative fiber mode threading model just fine.
What you cannot do with RunCommand() or acedCmd() is execute commnds that trigger a change in the active document (e.g., "OPEN", "NEW", etc), because acedCmd() (and its RunCommand() managed wrapper) can only be called from the document execution context, which is active only when its associated document is active (every open document has its own distinct execution context, only one of which can be active at any given time). So, if your command-scripting code causes the active document to change, the execution of the calling code will be suspended until the associated document that was active when the scripting was started becomes active again. That usually isn't a problem for simple cases where you are only operating on the active document. It would be a problem in cases where your scripting operates on many open documents (like ScriptPro does), so that's something to bear in mind.
The RunCommand() managed wrapper for acedCmd() addresses a few problems associated with the direct use of the latter via P/Invoke, which Kean's writing doesn't mention, namely that acedCmd() does not support managed selection sets. The RunCommand() method addresses that internally and provides a way to automate commands that require a user to select objects, and to provide them with a managed selection set.
The post at the link below contains a wrapper for RunCommand() that makes it easier to use, along with an example:
Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register
Start with some of our most frequented solutions to get help installing your software.