Customize AutoCAD through Programming

28 Mar, 2013 By: Andrew G. Roe

Learn how to search for text and modify layers with Visual Studio.NET.

We still have some more code to add before we’re ready to run this program.

13. In the Code window, just above the End Class statement, add the following:

      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As 
System.EventArgs) _
   Handles MyBase.Load
            txtSearch = Nothing
            intMatch = 0
            intTextEntities = 0
            AcadApp = GetObject(, "AutoCAD.Application")
            acadDoc = AcadApp.ActiveDocument
            For Each objLayer In acadDoc.Layers
      End Sub
      Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles TextBox1.TextChanged
            txtSearch = TextBox1.Text
      End Sub
      Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, 
ByVal e _
   As System.EventArgs) Handles ListBox1.SelectedIndexChanged
            objNewLayer = ListBox1.SelectedItem
      End Sub
   End Class

The first piece of code contains a subroutine that defines what happens when the form loads. It assigns initial values for the text and integer variables, then gets the AutoCAD application object and document objects, respectively. A For Each…Next loop populates the list box with a list of active layers in the drawing.

The next code snippet contains a subroutine that defines what happens when the user types something in the text box and assigns this to the txtSearch variable. The final segment identifies which layer the user selects and assigns it to the objNewLayer variable. Let’s test the program.

1. With AutoCAD running and a drawing with some text entities open, click the Play button (or press F5) to build and run the application.

2. Type some text that matches existing text somewhere in the drawing.

3. Select a layer from the list box, as shown in the figure below.

4. Click the button to search for the text and ask the user whether to move it to the selected layer.

5. Click the X in the upper-right corner of the form to stop execution of the program and close the form.

You can click File > Save All to save the code, the form, and the other elements of your .NET application. Visual Studio creates an EXE file that can be run as a standalone application outside the .NET environment.

You can also create a plugin — a module that can be run from within the AutoCAD environment. Autodesk describes this process in an online training document, "My First Plug-in Training."


This example illustrates some key concepts of accessing the AutoCAD object model and modifying entities programmatically. It 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. You’d also want to provide error handling for those inevitable situations where users try to make the program do something unintended. And you can probably think of various ways to further customize this example, but this shows you how to get started.

In future installments of this series, we'll continue to explore additional facets of AutoCAD programming, such as building plugins and working with Autodesk’s vertical products. If you would like to suggest a topic, feel free to send me an e-mail.

1 2 3 

Add comment

Note: Comments are moderated and will appear live after approval by the site moderator.

AutoCAD Tips!

Lynn Allen

In her easy-to-follow, friendly style, long-time Cadalyst contributing editor 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!

Follow Lynn on TwitterFollow Lynn on Twitter

Do you use any augmented or virtual reality (AR or VR) technologies in your workflow?
Not yet, but we are planning to implement it.
No, but we think these technologies could hold value for us.
No; these technologies do not hold value for us.
Submit Vote

Download Cadalyst Magazine Special Edition