How to Control AutoCAD® From a Standalone Executable

William Forty
William Forty

I've had a few requests recently to do a post on creating executables that control AutoCAD®. Though generally it is better to have tools well integrated into AutoCAD® via the use of a NETLOADed dll for example, sometimes an exe can be an elegant solution.

The general concepts are fairly straight forward.

  1. Check for a running instance of AutoCAD®, and create a reference to it. If it is not running, launch AutoCAD®, and create a reference to it.
  2. Use COM to control AutoCAD®

When I started that list, I expected it to be longer - but essentially that's it. If you're more of a VBA user, you'll be very pleased to hear that as we're using COM, you can pretty much re-use all the VBA code you ever learned.

So, lets draw a basic shape. Something like below:

random shape

Ok - this can easily be handled by a polyline, so this is what our exe will do.

First things first though. Create a new standalone exe project in VB. You can use VB.NET, or VB6 for this. You'll not need to worry about targeting any specific .NET frameworks or anything like that. As we are using COM we are completely sidestepping those issues.

So I'm going to go ahead with VB6; a bit old-school, but it illustrates the point well. New Project > Standard EXE.

Firstly, we need to create a reference to AutoCAD® in our code. To do this, we need to load the relevant AutoCAD® types and libraries into our exe project. So, go to Project > References. From here you'll want to select the AutoCAD® type libraries you want to use, depending on which version of AutoCAD® you're using.

Once loaded, we can create variables that are typed for AutoCAD®, and we are now able to write our code. For the purposes of this example we're not really interested in using the Form properly, we're only really using it as a container for our code in the Load event:

Private Sub Form_Load()
    Dim ACAD As AcadApplication 'Create ACAD variable of type AcadApplication
    'On Error Resume Next 'This tells VBA to ignore errors
    Set ACAD = GetObject(, "AutoCAD®.Application") 'Get a running instance of the class AutoCAD®.Application
    'On Error GoTo 0 'This tells VBA to go back to NOT ignoring errors
    If ACAD Is Nothing Then 'Check to see if the above worked
        Set ACAD = New AcadApplication 'Set the ACAD variable to equal a new instance of AutoCAD®
        ACAD.Visible = True 'Once loaded, set AutoCAD® to be visible
    End If

    Dim llCorner As Variant
    llCorner = ACAD.ActiveDocument.Utility.GetPoint(, "Pick the lower left corner for the shape")

    'Draw shape in terms of the lower left corner
    Dim coords(11) As Double
    'lower left corner of shape
    coords(0) = llCorner(0)
    coords(1) = llCorner(1)
    'lower right corner of shape
    coords(2) = llCorner(0) + 100
    coords(3) = llCorner(1)
    'upper right corner of shape
    coords(4) = llCorner(0) + 100
    coords(5) = llCorner(1) + 30
    'right hand corner of semicircle
    coords(6) = llCorner(0) + 70
    coords(7) = llCorner(1) + 30
    'Draw the semicircle as a straight line for now. We will set the bulge on this segment later.
    'left hand corner of semicircle
    coords(8) = llCorner(0) + 30
    coords(9) = llCorner(1) + 30
    'upper left corner of shape
    coords(10) = llCorner(0)
    coords(11) = llCorner(1) + 30

    'With the coordinates defined, use these to create a new polyline
    Dim poly As AcadLWPolyline
    Set poly = ACAD.ActiveDocument.ModelSpace.AddLightWeightPolyline(coords)
    'Set the bulge of segment 4 of the polyline (ie, index 3) to a value of -1
    poly.SetBulge 3, -1
    'Close the polyline
    poly.Closed = True

    'End application
    Unload Me

End Sub

So that's how to draw this shape using a VB6 standalone. The method would be very similar for VB.NET, but you'll need to take a slightly different approach when accessing the ThisDrawing object. This is discussed in my Introduction to VB.NET in AutoCAD®.

Hope this helps someone, and I encourage you to join the hordes of people that have subscribed by filling out an email address below!



2011-05-16 10:35:08

Hi Will, you are too good and kind........Thank you very much for helping me out..... But i have come up with another strange situation where i'm forced to use "MEASURE" command in VBA.. I want to place a Block(rectangle object) along an arc ; so i'm trying hard to do it using VBA. Can you give it a try to solve......PLz

2011-05-18 12:08:00

I'm in the process of writing a post for you on this - watch this space... and I'll post a link below when I've created the post. created

john coon
2011-05-16 16:27:48


When you create a exe routine for Autocad do you need to add the imports like below and references. I've never done a exe for Autocad. Can you create one with VS expess?


Imports Autodesk.AutoCAD®
Imports Autodesk.AutoCAD®.ApplicationServices.Application
Imports Autodesk.AutoCAD®.DatabaseServices
Imports Autodesk.AutoCAD®.Geometry
Imports Autodesk.AutoCAD®.Runtime
Imports Autodesk.AutoCAD®.ApplicationServices

2011-05-17 07:43:39

For VB6, no you don't. For VB.NET, yes, most probably. Although it should be noted that these are not the same types we would normally use for creating a .NET project - it's the same type libraries you'd use from VBA.

2011-07-13 01:27:43

Hi Will, I spend one day to access to ThisDrawing object without success in (i'm a newbie in programming). You suggest see the "Introduction to VB.NET in AutoCAD®.", but these metodd is for a class library (.dll) and don't work for executables :(

I copy your code totally and import the next (from the references):

Imports Autodesk.AutoCAD®.Interop
Imports Autodesk.AutoCAD®.Interop.Common

Then i run the code with autocad open and the "crosshair" says :"Pick the lower left corner for the shape" (ok there), then i pick in the modelspace but nothing happens (because i can't access to the model space) :(

Can you help me? :P


(sorry for my english)

2011-07-18 12:46:03

Under .NET there is no native "ThisDrawing" object, and you cannot use the same methods you used to use with VBA. Try having a look at a few of my more recent .NET posts - most of them manipulate the active drawing in some way and should give you some pointers.

2011-07-20 23:15:41

Will, I don't know what i do but now the code is working :D Thanks anyways!!!

2011-09-09 06:42:26

Hi will,

I created a dll. If i execute that dll it will create a text file with some details from the diagram. My question is, 1. how to create a reference from autocad to that dll ? 2. after creating an image how to execute that dll from autocad ?

Can u help me,

Thanks in advance.



2011-09-20 19:39:24

The normal way is to use the NETLOAD command in AutoCAD® to load the dll into AutoCAD®. Then you use whatever command name you gave the subroutine in the dll.

Avinash Patil
2011-11-05 06:00:24

I am new to can you create a small exe rutine for me in am using VS 2005.



2011-11-08 14:01:45

Hi Avinash,

If you're learning, you'll probably not want to create exes. You'll be better off trying to create proper managed code - that's the better, more correct way of building AutoCAD® applications. Have a look in my VB.NET tutorials for an introduction to VB.NET. This will get you going in the right direction.


2012-05-08 10:21:16

I am trying to create the stand alone application ,I am able to get AcadApplication object. Can you tell me how to get only Application or database object from AcadApplication. Any suggestion is most welcome.

2012-10-02 11:46:20

Sorry for the delay in responding... If you have an instance of the AcadApplication in say, the ACAD object, you should be able to use ACAD.Application.

2012-07-17 04:51:40

Hi Will, Here´s a question. I´ve created a custom user interface command (with this macro ^C^C-plot;n;;previous plot;;n;y;y;) it works fine. Now i´m trying to do a VB application that runs this command of several files. My idea Was: Open file,.... excecute this command ...close file...and lopp. I Cant make VB to run the command, Do you have any ideas Thanks In advance!! Pauli

2012-08-08 18:38:57

I'm afraid these macros are limited to toolbars etc - you cannot use them in VB. You'll have to write the code to do what you need using VB :-(

2013-05-07 03:42:10

Hi Will, Your site is very usfull and full of good knowledge for starting in I have a VB6 exe that is a tab form of which two tabs are for general use calculations and the other tabs are for AutoCAD® users who can take results and modify drawing values (block attributes ect). This has been good as it can be run without autoCAD for some users. Now going into I not sure if to place all of this in a dll or try and create a standalone exe that will also run without AutoCAD®. So i am looking around to see what other people are doing.

2015-01-05 11:22:52

how we can run src script file from form application without manual selection.i am using the following line in my program:- ScriptFilePath="D:\data.src" acadDoc.SendCommand "_script" & vbCr & ScriptFilePath & vbCr it is running without error in my program but always display dialogue box to select the file manually from the system drive. will can you help and suggest me the correct way to perform the exercise.....

2016-02-02 09:23:42

hello i m learn vba autocad so plez help me