Customize AutoCAD through Programming

28 Mar, 2013 By: Andrew G. Roe

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

Consider the following scenario: In an AutoCAD drawing with a large amount of text, you want to find all the text containing a certain word or phrase, then move that text to a specific layer. Perhaps the drawing has some notes containing the word “valve,” for example, and you want to consolidate all this text in a layer called Valves.

AutoCAD provides a nice search-and-replace feature — the Find command — and with a few extra clicks we could move the identified text to another layer. But for a more automated approach to modifying text properties, you will need to put on your customization hat.

In my previous articles about AutoCAD programming, we learned how to use the .NET programming environment to extract drawing information, create AutoCAD entities, and modify them programmatically. In this article, we will combine some of these tools and explore more deeply how we can use .NET programming tools to accomplish the task at hand.

As before, this example uses the Visual Basic.NET programming environment, although you can use Visual C#.NET instead. To complete this exercise, you'll need either Visual Studio.NET or the free Visual Studio Express. (If you're using AutoCAD 2013, you should also download the ObjectARX software development kit [SDK] to run this example. Even though it's not an ObjectARX example, the necessary references are located in the SDK for 2013.)

Create a Text Search Program

1. Start Visual Studio and create a new VB.NET project called TextSeeker. (If you need a refresher on creating a new project, see "The World of AutoCAD Programming Platforms, Part 3.")

2. Double-click on My Project in the Solution Explorer; the Project Properties screen will appear.

3. Click the References tab on the left.

4. Click the Add button to display the Add Reference dialog box.

5. In the Add Reference dialog box, click the Browse tab, and navigate to the folder containing AutoCAD’s DLL files. Typically this is the ObjectARX 2013\inc-win32 or ObjectARX 2013\inc-win64 folder, depending on which version of AutoCAD you are using. 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 above.)

6. Click OK to close the Add Reference dialog box.

7. Double-click on the Form1.vb[Design] tab to display the form. From the Toolbox, click and drag a text box, a list box, a button, and two labels onto the form, arranging them as shown below.

8. Click View > Properties Window. In the Properties window, change the Text property for the button to Search and Modify.

9. Change the Text property for the form to Text Seeker.

10. Change the Text property for the two labels to Search String and Target Layer, respectively. The form should now look like the image below.

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

Follow Lynn on TwitterFollow Lynn on Twitter

Which device do you typically use to read content?
A desktop computer / tower workstation
A tablet
A smartphone
A laptop or mobile workstation
I regularly use both a desktop computer and a smartphone for this purpose
I regularly use another combination of devices for this purpose
I prefer to print out articles from the website and read them on paper
Submit Vote

Download Cadalyst, Fall 2015

Download Cadalyst Magazine Special Edition