Practical Tips from Harry Himself (Hot Tip Harry AutoCAD Tutorial)1 Dec, 2007 By: Bill Kramer
Responding to user requests, the author delivers routines that demonstrate the functionality of VBA and Visual LISP programming.
I conducted an informal survey on the Discussion Forums and found that approximately one-half of you are interested in seeing more tips related to VBA, but the other half would prefer to focus on Visual LISP. Only a small number of people expressed any desire to see other programming exploits, such as ObjectARX. You can add your response to this survey by posting to the forum thread.
Due to its long life and continued use by a majority of AutoCAD programmers, Visual LISP is one of the better choices when it comes to adding new commands to AutoCAD. However, VBA can do a couple things that Visual LISP can't. One of the most apparent differences between the two is how they manipulate multiple documents. Visual LISP is closely integrated into drawings and reloads every time a new drawing is loaded. Any variables and expressions must be reloaded whenever another drawing is opened. Think of each drawing as possessing its own Visual LISP environment or space. In other words, data from one drawing isn't automatically available in a new drawing that is loaded and active.
VBA (Visual Basic for Applications) runs at the application level. As a result, VBA macros remain available without any reloading as you move from one AutoCAD drawing to another or when you load a file from a disk. If you want to run a procedure of some kind against a group of drawings, VBA is the best choice as the driving programming language.
A note about the BAS files in this month's batch of code: A BAS file is ASCII text, and you can view the file using Notepad. More importantly, it can be imported into an open VBA project. To import a BAS file, simply start the VBA editor (type Vbaide on AutoCAD's Command line) and select the File / Import menu option. A new module is created in the current project containing the code. Some of the BAS file modules in this month's tips are ready to run macros (type VBARun at the AutoCAD command line to run them), and others are subroutines or utilities that are used by the other macros. The tips have been provided as BAS files so that you can view them with Notepad and import them into multiple projects.
And that brings me to the programming tips direct from Harry.
Tip 2259: Process All Open Drawings
This tip consists of a very basic VBA routine presented as a BAS file, RUNALLOPEN.BAS. It is a single public macro that will loop through all open drawings. In this case, the macro zooms the display of each drawing to the current extents. But you could do much more by adding acDWG.SendCommand lines that contain any AutoCAD commands you want. For this macro to work best, I suggest using generic AutoCAD commands -- nothing too complex -- such as a LISP routine, until you get a handle on the synchronizing of VBA, AutoCAD, and Visual LISP. This function works by iterating through the open documents in a current session of AutoCAD. VBA makes this task simple.
Tip 2260: Process All Drawings in a Folder
Just as you can apply commands to every open drawing using a routine like the one above, you also can apply commands to every drawing in a folder, except in this latter case the routine actually opens drawings rather than just accessing them. This tip, supplied for import as the module file RUNFOLDER.BAS, requires the presence of the next tip (Pick a Folder Dialog Box) in the same project. The concept is to use the DIR function of VBA to loop through a selected folder of drawings. Each drawing is opened, and a series of commands is applied (in this case, just a simple zoom) before the drawing is closed with a save back to the disk. If you have folders full of drawings and need to run a set of AutoCAD commands against each, then this code may be just the answer in terms of a basic template. Once again, the SendCommand method of the drawing object is used to manipulate the drawing. With the drawing open, VBA routines can accomplish much more, such as directly manipulating objects, cleaning up tables, and revising references.
Tip 2261: Pick a Folder Dialog Box
VBA is a programming language you can use to customize a variety of software solutions in addition to AutoCAD -- primarily Microsoft Office products such as Word, Excel, Outlook, and Access. Not surprisingly, VBAcomes with a host of interesting features that have nothing to do with AutoCAD. Communicating with the operating system and using features shared by other applications is something that occasionally yields some valuable treasure. This tip presents a dialog box that prompts the user to select a folder. Although the common file dialog box could be used for this purpose, another option is available inside the basic tools of Windows. The module file BROWSEFOLDER.BAS contains the code needed to display the folder dialog box inside VBA. Inside your VBA code, you will now have a function named BrowseFolder that will return a string of the folder name selected. As noted, this function is used in the previous tip, Process All Drawings in a Folder, to select a folder to process (and as an example). Other systems functions inside of Windows also may be of interest. You can learn more about these tools on the Microsoft Developer Network (MSDN) Web site, or you can often find exactly what you need through a general Internet search or by asking fellow users at the Cadalyst Hot Tip Harry Discussion Forums.
Tip 2262: Pass Strings between VBA and Visual LISP Using a Dictionary
When VBA first appeared on the scene, I thought it would be great if we could use VBA's superior user interface capabilities in dialog boxes along with the more AutoCAD-intimate Visual LISP. Like other users, I quickly discovered that there was a communications gap between the two. This tip represents one way to bridge the communications gap between the two environments: using a dictionary with an Xrecord. Xrecords are nongraphic objects in a drawing that can contain data arranged in group codes. Unlike extended data in which all the group codes are in the thousands, the Xrecord group codes are just like normal entities. It's a convenient place to save and pass data between modules such as strings, numbers, points, and entity links. The function example in this tip works with just a single string, but you could expand it to meet your own needs. A dictionary named LISP_VBA (source file LISPVBA.LSP) is created and used by both Visual LISP and VBA to share data in a single Xrecord containing a string data element. To make it easier in Visual LISP, the functions (LISP->VBA) and (VBA->LISP) are provided. (LISP->VBA) has one parameter, which is a string. (VBA->LISP) has no parameters, but it returns a string. An example module (BAS file) for VBA can catch and receive data provided as part of the next tip, Use VBA for Input from Visual LISP.
Tip 2263: Use VBA for Input from Visual LISP
Incorporating the code files LISPBROWSEFOLDER.BAS and BROWSEFOLDER.LSP in addition to code from two tips above, this tip allows Visual LISP to call up the VBA Browse Folder dialog box (Pick a Folder Dialog Box) by passing a title to VBA (Use VBA for Input from Visual LISP) and receiving the folder name as a string back from VBA (also Use VBA for Input from Visual LISP). Between sending the string to VBA and getting the result, the Visual LISP code invokes the VBA macro, then waits for a sign that the VBA macro has finished. The sign is a changed value in the UserI1 system variable. When that value is changed (by the VBA macro), Visual LISP knows that an answer is ready for pickup. Calling the function (BrowseFolder prompt) will bring up the VBA folder browser then return the selection made. If nothing is selected, a blank string is returned.
Harry extends his best wishes to all for a grand New Year! Keep sending in those tips and tricks! You can win cool stuff -- including Cadalyst T-shirts in cool, new designs -- if your tip passes muster with Harry. Don't forget: Harry looks for your ideas and comments regularly at the Cadalyst discussion forums. Stop by for a visit if you haven't already found this great resource. Use requires registration, but it's free.
Keep on programmin'.
Join the Hot Tip Harry Challenge
Harry needs your tips! Do you have an original routine written in VBA, Visual LISP, or even ObjectARX for AutoCAD customization? Send it in, with all the source code, to firstname.lastname@example.org. Your tip may be needed by others now, so don't delay!
Subscribe to Harry's Code Class
Cadalyst's Hot Tip Harry helps programmers and programmer wannabes learn the ins and outs of AutoCAD customization in his monthly e-newsletter, Harry's Code Class. Subscribe to Harry's Code Class today and start building your programming expertise.
Hot Tip Harry Discussion Forums
Harry is standing by online to aid AutoCAD users and programmers. Cadalyst's Hot Tip Harry -- Help! Discussion Forum offers assistance with AutoCAD programming and customization problems. Hot Tip Harry -- Requests Discussion Forum can help you locate a specific LISP or VBA routine for AutoCAD. Participation in Cadalyst Discussion Forums requires registration, but it's free.
Get Your Weekly Fix of AutoCAD Tips
Cadalyst's Tips & Tools Weekly e-newsletter mails each Monday, delivering AutoCAD tips by readers for readers -- all reviewed by Cadalyst's volunteer Tip Patrol. Plus, you'll receive a rundown of new online CAD resources, deals and freebies, new CAD-related hardware and software products, and more. View the Cadalyst Tips & Tools Weekly archives and subscribe today!
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!