Customize AutoCAD through Programming28 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.
About the Author: Andrew G. Roe
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!