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