Embedding AutoCAD 2009 in a standalone dialog
This post takes a look at another topic outlined in this overview of the new API features in AutoCAD 2009.
AutoCAD 2009 introduces the ability to embed the application in a standalone dialog or form via an ActiveX control. This capability has been around for a number of releases of AutoCAD OEM, but this feature has now been made available in the main AutoCAD product.
The way the control works is to launch an instance of AutoCAD in the background (it should go without saying that AutoCAD needs to be installed on the system, but I've said it, anyway :-) and it then pipes the graphics generated by AutoCAD into the area specified by the bounds of the control. It also then pipes back any mouse movements or keystrokes, to allow the embedded AutoCAD to be controlled. It's pretty neat: you'll see the standard cursor, be able to enter commands via dynamic input, and more-or-less do whatever can be done inside the full product.
The control is especially handy if you want to present a reduced user-interface to the people using the product (which is really what AutoCAD OEM is for, in a nutshell, although the development effort involved in creating a full AutoCAD OEM application makes it inappropriate for quick & easy UI streamlining).
Let's start our look at this control by creating a new C# Windows Application project in Visual Studio 2005 (you can use whatever ActiveX container you like, though - it should even work from a web-page or an Office document):
Once Visual Studio has created the new project, we need to add our control to the toolbox. If you right-click on the toolbox, you'll be able to select "Choose Items...".
From here, there should be an item "AcCtrl" in the list of COM Components. Otherwise you can browse to it in c:\Program Files\Common Files\Autodesk Shared\AcCtrl.dll.
Then you simply need to place the control on your form.
Once we've done that, we're going to add a few more controls - for the drawing path, and a text string for commands we want to try "posting" to the embedded AutoCAD application.
Here's the C# code we'll use to drive the embedded control from the form. You should be able to work out what the various controls have been called in the project by looking at the code.
using System;
using System.Windows.Forms;
namespace EmbedAutoCAD
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void browseButton_Click(
object sender, EventArgs e)
{
OpenFileDialog dlg =
new OpenFileDialog();
dlg.InitialDirectory =
System.Environment.CurrentDirectory;
dlg.Filter =
"DWG files (*.dwg)|*.dwg|All files (*.*)|*.*";
Cursor oc = Cursor;
String fn = "";
if (dlg.ShowDialog() ==
DialogResult.OK)
{
Cursor = Cursors.WaitCursor;
fn = dlg.FileName;
Refresh();
}
if (fn != "")
this.drawingPath.Text = fn;
Cursor = oc;
}
private void loadButton_Click(
object sender, EventArgs e)
{
if (System.IO.File.Exists(drawingPath.Text))
axAcCtrl1.Src = drawingPath.Text;
else
MessageBox.Show("File does not exist");
}
private void postButton_Click(
object sender, EventArgs e)
{
axAcCtrl1.PostCommand(cmdString.Text);
}
}
}
Finally, when we run the application and load a drawing via the browse/load buttons, the real fun starts. :-)
Try entering commands via dynamic input, or via the "Post a command" textbox. You might feel a little disorientated due to the lack of a command-line (I do love my command-line ;-), but dynamic input allows you to at least see what you're typing.
Here's the C# project for you to download.

Subscribe via RSS
Please tell me how to get xrefs to show up using the AcCtrl.dll control from 2009 TruEView via VB.
Posted by: tb | May 28, 2008 at 06:04 PM
Do your xrefs get loaded when launching the DWG TrueView executable directly?
I haven't yet looked into xref support when TrueView is hosted by the control, but I thought I'd start by asking this question.
Kean
Posted by: Kean | May 29, 2008 at 01:32 PM
Hi Kean,
nice tool. But on some occacions, the whole ACAD frame comes up, with all menus, icons, ribbons, commandline and so on.
This happens e.g. when a message on the commandline is printed or a dialgbox opens. And when you work with additional DBX/ARX loaded, this happen nearly every time. How to avoid this?
Greetings
Markus
Posted by: Markus Hannweber | August 07, 2008 at 05:05 PM
Hi Markus,
Please submit a reproducible case via the ADN site, and we'll look into it.
Regards,
Kean
Posted by: Kean | August 07, 2008 at 05:08 PM
Hi Kean,
While in a AutoCAD session, I wrote a command that displays the above as a (modal) form. When exiting i get an error from AcVBA.arx (access violation reading ...).
My questions:
- is there another way to display entities in a separate window/control, or is AcCtrl the way to go? (I need zoom/pan/selection functionality from the window)
- is there a way to display the control without starting another instance of AutoCAD?
Thanks,
Harrie
Posted by: harrie | October 09, 2008 at 05:33 PM
Hi Harrie,
I would recommend against using the AcCtrl control in-process to AutoCAD: this effectively fires up and drives a separate instance of AutoCAD behind the scenes.
You will hopefully find the BlockView control more interesting, as mentioned in this previous post.
Regards,
Kean
Posted by: Kean | October 10, 2008 at 09:03 AM
Hi Kean:
A DWG file with XREF displays well on the stand alone DWG TrueView 2009 but not when using DWG TrueView 2009 as an ActiveX control. Does this feature work when used as an ActiveX control?
(The layer manager dialog invoked from the activeX control still lists all the layers just like the stand alone does.)
Thanks,
Bala
Posted by: Bala Padmanabhan | February 25, 2009 at 08:34 PM
Hi Bala,
This issue has now been mentioned a couple of times in comments on this post...
I've just tried it myself, and can see the behaviour you're describing. Running SysInternals' ProcessMonitor shows that the xrefed DWG files are being accessed, but it's true that they aren't being displayed.
I recommend submitting the issue via the ADN site, if you're a member (this blog isn't a forum to get support... I unfortunately don't have time to investigate issues in depth unless they relate specifically to code I've posted).
Regards,
Kean
Posted by: Kean Walmsley | February 26, 2009 at 11:11 AM
Hi Kean
I've been trying this with AutoCAD 2010 and get the following error ...
"Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))"
at the line ...
axAcCtrl1.Src = drawingPath.Text;
I've tried REGSVR32 AcCtrl.dll, with no effect
Have you any ideas/pointers ?
Thanks in advance,
Nick
Posted by: Nick Hall | July 01, 2009 at 09:29 AM
Hi Nick,
I'm not going to be able to look into this anytime soon - would you mind submitting it via ADN?
Thanks & regards,
Kean
Posted by: Kean Walmsley | July 01, 2009 at 05:15 PM