Kean Walmsley

July 2009

Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  

Twitter Updates

    follow me on Twitter



    « Initialization code in your F# AutoCAD application | Main | Using a jig to rotate an AutoCAD entity via .NET »

    March 27, 2008

    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):

    New application project

    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...".

    Add to toolbox

    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.

    Add control to toolbox

    Then you simply need to place the control on your form.

    Add control to 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.

    Design our form

    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. :-)

    Embedded AutoCAD 2009

    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.

    TrackBack

    TrackBack URL for this entry:
    http://www.typepad.com/services/trackback/6a00d83452464869e200e5518f4d078834

    Listed below are links to weblogs that reference Embedding AutoCAD 2009 in a standalone dialog:

    Comments

    Please tell me how to get xrefs to show up using the AcCtrl.dll control from 2009 TruEView via VB.

    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

    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

    Hi Markus,

    Please submit a reproducible case via the ADN site, and we'll look into it.

    Regards,

    Kean

    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

    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

    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

    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

    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

    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

    Verify your Comment

    Previewing your Comment

    This is only a preview. Your comment has not yet been posted.

    Working...
    Your comment could not be posted. Error type:
    Your comment has been posted. Post another comment

    The letters and numbers you entered did not match the image. Please try again.

    As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

    Having trouble reading this image? View an alternate.

    Working...

    Post a comment

    Feed & Share

    Search