The preceding post on RealDWG and object enablers discussed one potential method for making use of AcDb functionality to read and write AutoCAD DWG files inside of Revit. Assuming that you have a license for a full version of AutoCAD in addition to your Revit one, there is another easy method to achieve this which has been implemented and tested.
It is possible to run AutoCAD as a full-blown process in the background inside Revit. This functionality is new in AutoCAD 2009, and the code we present is based on a demo by Kean Walmsley on embedding AutoCAD 2009 in a standalone dialog. Obviously this requires a full version of AutoCAD to be installed on the machine. Among other things, Kean's code defines a MainForm class, which can be used to load and run AutoCAD and access its API functionality.
Markus Hannweber of SOFiSTiK AG has made use of this within a Revit plug-in. Here are some examples of what is possible:
MainForm mainform = new MainForm(); AxACCTRLLib.AxAcCtrl c = mainform.axAcCtrl1; c.Src = "d:\\demo\\embed.dwg"; c.PostCommand("(setvar \"cmddia\" 0) "); c.PostCommand("(arxload\"sofibldarx\") "); c.PostCommand("(load\"blist\") "); c.PostCommand("SOF_BLIST "); c.PostCommand("(S:ENDACAD) "); c.Src = null;
The individual lines do the following:
- Instantiate Kean's MainForm class and open AutoCAD in the background.
- Initialise a variable 'c' to address the AutoCAD control. It also helps keep the source code line length under control.
- Load a drawing, object enablers are automatically loaded with it, if set up to do so.
- Suppress AutoCAD command line output.
- Load an ARX application into AutoCAD.
- Load an AutoLISP application into AutoCAD.
- Start any application command, whether defined in Lisp or ARX.
- Run a command to reset the 'drawing dirty' flag, so the next command can quit AutoCAD without user interaction.
- Close AutoCAD.
Obviously, the actions you trigger in your background AutoCAD process must not require any user interaction, or, if they do, you must feed them the appropriate command line user input through something like mainform.axAcCtrl1.PostCommand(...).