How to Control AutoCAD® From a Standalone Executable

by Will on May 16, 2011

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!

Will

Enter Your Mail Address

Be Sociable, Share!

{ 15 comments… read them below or add one }

Ranjan May 16, 2011 at 10:35 am

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

Reply

Will May 18, 2011 at 12:08 pm

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.

post created

Reply

john coon May 16, 2011 at 4:27 pm

Will,

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?

John

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

Reply

Will May 17, 2011 at 7:43 am

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.

Reply

Jose July 13, 2011 at 1:27 am

Hi Will,
I spend one day to access to ThisDrawing object without success in vb.net (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

Jose

(sorry for my english)

Reply

Will July 18, 2011 at 12:46 pm

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.

Reply

Jose July 20, 2011 at 11:15 pm

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

Reply

Suresh September 9, 2011 at 6:42 am

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.

Regards

Suresh

Reply

Will September 20, 2011 at 7:39 pm

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.

Reply

Avinash Patil November 5, 2011 at 6:00 am

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

Thanks,

Avinash

Reply

Will November 8, 2011 at 2:01 pm

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.

Will

Reply

Jalindar May 8, 2012 at 10:21 am

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.

Reply

Will October 2, 2012 at 11:46 am

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.

Reply

Paul July 17, 2012 at 4:51 am

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

Reply

Will August 8, 2012 at 6:38 pm

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

Reply

Leave a Comment

Previous post:

Next post: