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.