The World of AutoCAD Programming Platforms, Part 210 May, 2012 By: Andrew G. Roe
VBA, Visual Studio.NET, and AutoLISP each offer unique benefits and drawbacks for customizing AutoCAD.
In the previous installment of this series about AutoCAD programming, we briefly reviewed several environments currently available to programmers. Now, let's take a closer look by developing an example in three of those environments: Visual Basic for Applications (VBA), Visual Studio.NET, and AutoLISP. The first two approaches access the AutoCAD Object Model to obtain drawing information, while the third approach uses AutoLISP functions to obtain this information.
Even though Autodesk removed VBA from the standard AutoCAD interface, you can still develop and run VBA programs in AutoCAD 2010–2013 using a free, downloadable VBA enabler, the Microsoft Visual Basic for Applications module. Versions are available for both 32-bit and 64-bit AutoCAD, but be aware that VBA is a 32-bit application and can run erratically on 64-bit systems.
To become familiar with VBA routines and compare this approach to other options, let's develop a simple program to obtain some information about the active drawing using a graphical user interface (GUI) for user interaction. After you've downloaded the VBA enabler, start AutoCAD, then perform the following steps:
1. Select the Manage tab of the AutoCAD ribbon, and click Visual Basic Editor.
This opens the Visual Basic Interactive Development Environment (IDE).
2. From the Insert menu, click UserForm to insert a blank UserForm.
3. From the Toolbox, click and drag a command button onto the UserForm.
4. With the new command button selected, click the Name line in the Properties window, and change the Name property to GetDwgInfo.
5. In a similar manner, change the Caption property to Get Drawing Info.
6. Double-click the command button to display the Code window. A subroutine is already set up for the command button you just double-clicked.
7. Modify the code to read as follows:
Private Sub GetDwgInfo_Click()
Dim DrawingName As String
Dim NumEntities As Integer
DrawingName = ThisDrawing.Name
NumEntities = ThisDrawing.ModelSpace.Count
MsgBox "The name of the drawing is: " & DrawingName & vbCrLf & _
"The number of entities is: " & NumEntities
8. Click the Play button to run the routine just created.
9. Click the Get Drawing Info button to display the name of your drawing and the number of entities in the drawing.
10. Click OK to close the information window, then click the X in the upper right of the UserForm to stop execution of the program, close the UserForm, and return to the VBA IDE.
Let's briefly review the code to understand how this works. The two Dim statements declare two variables — DrawingName and NumEntities — to store the name of the drawing and the number of entities, respectively. These two values are then set equal to two properties accessible from the AutoCAD Object Model. A message box is displayed showing the values of the two variables. (For simplicity, this and other samples in this article contain no error handling, but in a real-world application, you should add code to handle situations where the user enters invalid data or other circumstances prevent the program from executing properly.)
You can save your program by clicking File > Save in the VBA IDE. You can then access it later in several different ways, such as by manually loading it in future AutoCAD sessions or embedding it in the current drawing. See the Manage tab to further explore these options.
Next, let's look at how to accomplish a similar task from the .NET environment. This example uses Visual Basic.NET, although you can also use Visual C#.NET. To complete this exercise, you'll need either Visual Studio.NET or the free Visual Studio Express. (If you're using AutoCAD 2013, you'll also need to download the ObjectARX SDK to run this example. Even though it's not an ObjectARX example, the necessary references are located in the SDK for 2013.)
1. Start Visual Studio and create a new project by clicking File > New > Project.
2. In the New Project dialog box, select Windows Forms Application, then type a name and file location.
3. Click OK to close the New Project dialog box.
4. Click View > Solution Explorer to display the Solution Explorer.
5. Double-click on My Project in the Solution Explorer. The Project Properties screen appears.
6. Click the References tab on the left.
7. Click the Add… button to display the Add Reference dialog box.
8. In the Add Reference dialog box, click the Browse tab, and navigate to the ObjectARX 2013\inc-win32 or ObjectARX 2013\inc-win64 folder, depending on your version of AutoCAD. Select the Autodesk.AutoCAD.Interop.Common.dll and Autodesk.AutoCAD.Interop.dll files by clicking on one and Ctrl-clicking on the other.
(Note: This example is for AutoCAD 2013. If you are using AutoCAD 2012 or older versions, you can click the COM tab and select the appropriate type library and ObjectDBX common type library: axdb18enu.tlb and acax18enu.tlb for AutoCAD 2012. Alternatively, you can download the ObjectARX SDK for older versions and reference the two dll files listed earlier.)
9. Click OK to close the Add Reference dialog box.
10. Click on the Form1.vb[Design] tab to display the form. From the Toolbox, click and drag a button onto the form.
11. Click View > Properties Window. In the Properties window, change the Name property of the button to GetDwgInfo and the Text property to Get Drawing Info.
12. Double-click the button to display the Code window. A subroutine is already set up for the command button you just double-clicked.
13. Modify the code to read as follows:
Public Class Form1
' AutoCAD must be open before running this example.
Dim AcadApp As Autodesk.AutoCAD.Interop.AcadApplication
Dim acadDoc As Autodesk.AutoCAD.Interop.AcadDocument
Dim DrawingName As String
Dim NumEntities As Integer
Public Sub GetDwgInfo_Click(sender As System.Object, _
e As System.EventArgs) Handles GetDwgInfo.Click
AcadApp = GetObject(, "AutoCAD.Application")
acadDoc = AcadApp.ActiveDocument
DrawingName = acadDoc.Name
NumEntities = acadDoc.ModelSpace.Count
MsgBox("The name of the drawing is: " & DrawingName & vbCrLf & _
"The number of entities is: " & NumEntities)
14. With AutoCAD running, click the Play button (or press F5) to build and run the application.
15. Click the Get Drawing Info button to display the name of your drawing and the number of entities in the drawing.
16. Click OK to close the information window, then click the X in the upper right of the form to stop execution of the program and close the form.
As you can see, a few more steps are required for a .NET program than for a VBA program. References must be added so the .NET program can access the AutoCAD Object Model. The AutoCAD Application and Document objects are then used to obtain properties about those objects. This example requires AutoCAD to be running and contains no error handling. In a real application, you'd want to check to see if AutoCAD is running, and if not, start it. Otherwise, the code is similar to the VBA example.
You can save your .NET application by clicking File > Save All to save the code, the form, and the other elements of the project. Visual Studio creates an EXE file that can be run as a standalone application outside of the .NET environment. You can also create a plug-in, or a module that can be run from within the AutoCAD environment. Autodesk describes this process in an online training document, "My First Plug-in."
You can also accomplish our example task in AutoLISP, albeit with a slightly different approach:
1. From the Manage tab of the AutoCAD ribbon, click Visual LISP Editor to open the Visual LISP IDE.
2. In the Visual LISP IDE, click File > New File to open a code window.
3. Type (or copy and paste) the following code into the Visual LISP code window:
(defun c:GetDwgInfo ()
(princ "Drawing name: ")
(princ (getvar "DWGNAME"))
(princ "\nNumber of entities: ")
(setq eset(ssget "x"))
(setq cntr 1)
(while (< cntr (sslength eset))
(setq cntr(+ cntr 1))
4. In the Visual LISP IDE, click File > Save and save the file in a location of your choice. The file is saved with a LSP extension, but is a simple text file editable with any text-editing software.
5. From the Manage tab of the AutoCAD ribbon, click Load Application.
6. Navigate to the previously saved LSP file and click Load. Click Close to close the Load/Unload Applications dialog box.
7. At the AutoCAD Command line, type GetDWGInfo and press Enter. The drawing name and number of entities are displayed at the AutoCAD Command line.
In a nutshell, the drawing name is accessed using an AutoLISP function called getvar, which accesses an AutoCAD system variable called DWGNAME. The number of entities is obtained by creating a selection set called eset, then selecting all drawing elements with the "x" filter parameter and iterating through them using a variable called cntr. The princ function is used to print data to the AutoCAD Command line.
You can also use various Visual LISP features to create a GUI similar to that used with the VBA example, but the approach shown here demonstrates how to use bare-bones AutoLISP to save code in a simple text file and execute the code at the AutoCAD Command line.
Each of these programming approaches employs slightly different techniques to accomplish a similar task. VBA offers perhaps the simplest approach to building a GUI-based application, but it has a limited future. Visual Studio.NET provides additional capabilities, but requires more up-front work and has a steeper learning curve. AutoLISP provides smooth, direct interaction with AutoCAD, but entails a somewhat cryptic programming syntax and requires additional steps to create GUI-based applications. In the next installment of this series, we'll look at more programming techniques focused on future technologies.
About the Author: Andrew G. Roe
In her easy-to-follow, friendly style, long-time Cadalyst contributing editor and Autodesk Technical Evangelist Lynn Allen guides you through a new feature or time-saving trick in every episode of her popular AutoCAD video tips. Subscribe to the free Cadalyst Video Picks newsletter and we'll notify you every time a new video tip is published. All exclusively from Cadalyst!