VBA, Visual LISP Answers to the Same Question (Harry's Code Class Newsletter)27 Oct, 2008
User's request for global text clean-up routine leads to dual solution that serves as a great instructional example, as well.
A simple question can sometimes present an excellent opportunity to teach new concepts. In this case, the question was from an AutoCAD operator: How do you write a utility to globally change the case of text in a drawing? The opportunity for me is to show how to solve this problem using two different AutoCAD programming languages, Visual LISP and VBA (Visual Basic for Applications).
Over the years I’ve learned that studying examples is a great way to learn a programming language (and you can tweak existing code to make it your own). In this newsletter I’ll describe the command function to address the challenge in question, then show how to program it using Visual LISP and VBA. You’ll be able to gain some insights about both programming languages by comparing and contrasting the unique approaches of each. Download the source code for each variation at Cadalyst’sCAD Tips site. (Search by tip title: Global Text Clean-Up.) The download includes two versions of the code, one in VBA (CASEFIXER.DVB), one in Visual LISP (CASEFIX.LSP).
The user who asked how to program this routine adheres to an in-house drawing standard that requires all uppercase text, and he wanted a utility to clean up drawings. He also wanted the ability to change either a complete drawing or only selected text and to select between upper- and lowercase for the change.
I call these types of command functions “global editors,” and they always have the same basic structure. There will be an input portion to select the edit parameters, followed by a loop for object selection and subsequent processing of the objects selected. In this situation the processing will convert text values to all uppercase or all lowercase.
To begin your programming efforts, think about the entity objects to be adjusted by the routine. There are several entities to consider: Text, multiple line text, dimension overrides, and attributes are the four primary text objects in an AutoCAD drawing. Each of these objects has properties wherein we can locate the text data that is to be adjusted.
Learning about AutoCAD entity objects requires that you get into the AutoCAD Help system. From AutoCAD, type Help on the Command line. Select the Contents panel and locate an entry for “Active X Automation and VBA.” From the options that display next, select “Active X and VBA Reference.” When the developer help appears, select Object Model. You should now see a flow chart depicting the AutoCAD object hierarchy. Go ahead and select a couple of objects to see what is documented. Specifically, check out Text, Mtext, AttributeReference, and some of the Dim options.
Under the Attribute references, text and Mtext objects, you find that the property TextString contains the text value. Dimension override text is stashed in the TextOverride property. These, understandably, are the properties you’ll want to adjust.
The key is to drill down to the bottom level. To handle all the options, we need to write a routine that checks for the TextString or TextOverride properties. That way we don’t have to code something to handle all the dimension options and text options. By checking the validity of the property name we can determine if an entity object has something of interest for our routine.
We can now write a function that handles the objects. If the object has one of the properties of interest, the function is to apply upper- or lowercase, then update the object in place. In the associated source code, Visual LISP function CaseCheck and VBA subroutine CaseFlip (in UserForm1) are two variations of how to accomplish this desired task.
Now you’re left simply with addressing the matter of having an input system followed by a selection of objects to process. In the Visual LISP example, the input is done at the Command line, which is quite simple to code. The VBA example uses a dialog box for the parameter input because that is the simplest approach to code in that language. Use the version that suits your preferences and background.
Compare this general outline of the process with the code download, and I bet you’ll find that learning by example works for you too.
I will be lurking in Cadalyst’sHot Tip Harry Discussion Forums, looking for more fodder for the newsletter. I hope this set of example routines can help guide some AutoLISP holdouts into Visual LISP, provide a bridge between the two languages for study, or just solve a simple problem of flipping text cases around in your drawings. Until next month, keep on programmin’!
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!