AutoCAD

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
            ListBox1.Items.Add(objLayer.Name)
            Next
      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."

Review

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

Autodesk Technical Evangelist Lynn Allen guides you through a different AutoCAD feature in every edition of her popular "Circles and Lines" tutorial series. For even more AutoCAD how-to, check out Lynn's quick tips in the Cadalyst Video Gallery. Subscribe to Cadalyst's free Tips & Tools Weekly e-newsletter and we'll notify you every time a new video tip is published. All exclusively from Cadalyst!
Follow Lynn on Twitter Follow Lynn on Twitter


Poll
Which file format do you use most often for CAD drawing/model exchange?
Native format
PDF
3D PDF
DWF
STEP or IGES
JT
IFC
Other
Submit Vote