Harry's Code Class: Tips for Programmers (January 2007)23 Jan, 2007
Customizing AutoCAD: The Nuts and Bolts
A basic overview of LISP, VBA and Object ARX launches us
into the craft of programming.
Is programming an art or a skill? Arguments on both sides of that question make good sense. Programming can be an art, appreciated by other programmers for elegance and succinctness. And programming is a skill, too, because the more you do it, the more you improve -- and probably more importantly, the more tricks you learn. I consider programming to be a craft, meaning practitioners must possess both art and skill to be proficient. In this Harry's Code Class: Tips for Programmers, I'd like to keep the on-going discussion centered on how you can learn to be a better code craftsperson.
I'm a professional programmer, trained in computer science at the university level, with more than 30 years' experience. As such, I tend to take for granted a lot of the aspects of this craft. Fortunately, I spend a lot time teaching others about the craft and am constantly reminded that the basics need to be repeated.
Crafters use tools, and programmers are no exception. Our tools are software, such as text editors, linkers and compilers. AutoCAD customization brings additional options to the toolbox, such as Object ARX and Visual LISP developer environments and the custom user interface editor. The good news is that you don't have to know every tool to begin programming custom AutoCAD solutions. As you progress, you'll become aware of the other tools and might even be tempted to venture deeper into using them -- but for now you can settle on mastering just a few.
With all this in mind, I'd like to use Harry's first Code Class to present a basic introduction to the tools of the AutoCAD programming craft. Most of the tools you need for programming are already embedded in AutoCAD, as I'll discuss in detail.
Getting Started: The Basics
The basic tools you need to craft AutoCAD code are simply AutoCAD and a computer. Note: AutoCAD LT supports a minimal subset of the programming capacity available in the full version; as a result, I won't be delving into AutoCAD LT customization techniques. My focus will be on the full implementation of AutoCAD, which extends to AutoCAD-based solutions such as the Desktop editions.
LISP and VBA. LISP (a name that derives from "list processing") and VBA (Visual Basic for Applications) are two very powerful programming languages for customizing AutoCAD, and are also the most common. Visual LISP is the current version of AutoLISP, which has been available since AutoCAD v2.18. A great deal of legacy code exists for LISP in AutoCAD -- you'll find hundreds of AutoLISP routines in Harry's Get the Code! archives alone -- plus you can find many books and online resources for learning LISP. To the experienced programmer, LISP code seems a bit odd-looking at first, but using it becomes quite natural over time. In fact, during my earlier years using LISP, I noticed that many of my FORTRAN programs began to look like LISP code.
VBA is a version of Microsoft Visual Basic. If you know the BASIC programming language, you'll probably find VBA to be the best starting point for adding functionality to AutoCAD.
The full version of AutoCAD comes standard with Visual LISP and VBA developer environments you can use to create and edit programs. A development environment is a tool that includes a text editor for typing and changing program text, a project management tool for linking various code modules together into a single program, and plenty of help to make programming easier. These tools are programs written for programmers and are not immediately intuitive to nonprogrammers or those unsure about what they aim to accomplish. However, if you have been using visual programming tools in other environments, then you'll find these tools to be a familiar setting for your work.
To access the Visual LISP developer environment in AutoCAD, type Vlide on the Command line. To initiate VBA, use Vbaide.
Visual LISP and VBA programs run differently in AutoCAD. Visual LISP statements can be typed and launched at the AutoCAD command line. When the first character typed at the Command line is an open parenthesis, the LISP evaluator takes over.
Running a VBA program, on the other hand, requires that the VBA environment be running, and you launch macros from the VBA manager. You cannot type VBA command statements directly at the AutoCAD Command prompt. This subtle distinction can be very important when you are writing programs for custom menu options. Menu coding is essentially the same as text typed at the Command line, so Visual LISP programs integrate smoothly into custom menu programming, while VBA programs tend to be a bit clunky in that regard.
ObjectARX. ObjectARX is a programming library used by Microsoft Visual C++ programmers to create ARX files. An ARX file is a variation of a DLL (dynamic link library), meaning that although it is an executable, it needs a host. In this case, the host is AutoCAD, where ARX files are loaded and run in a manner similar to Visual LISP program files.
Two advantages of ARX programming are speed and security. C++ based programs can be faster than those written in Visual LISP and VBA, by a factor of five or more. Speed can be important when processing large sets of data through iterative processes. The secondary benefit, security, results from the fact that compiled and linked ARX programs are the most difficult to reverse engineer. They also run the smoothest. For these reasons, ObjectARX is the choice of many developers who want to sell their software to others. More important to others may be that ARX gives you deeper control over "reactors" -- the event system inside AutoCAD that reacts as commands and actions take place -- allowing you to take complete control of the system. To program in ObjectARX, you need Microsoft Visual Studio (the version that corresponds to your AutoCAD version) and the associated ObjectARX link libraries from Autodesk.
How to Choose?
So, which programming option is best? That depends on who you are and what you are trying to accomplish. For the majority of AutoCAD users who program, the best solution is Visual LISP. LISP code is so common that, chances are, the routine you need for a particular AutoCAD task -- or something close to it -- already exists somewhere, ready for download.
If you have a background in Visual BASIC then you might give the AutoCAD VBA environment a try. AutoCAD's object set is extensive and will take some time to learn, but the rewards are well worth the effort. For further reading, downloads and discussion groups related to AutoCAD custom programming, check out the Autodesk Developer Center.
If you intend to get serious about AutoCAD customization but you're not an AutoCAD user, you'll find it invaluable to first gain a basic understanding of AutoCAD operations and drawing structure. Professional programmers tasked with creating an AutoCAD application can gain insight about how to structure an AutoCAD routine by talking with AutoCAD users about how they would solve the problem manually.
What topics will Harry cover in future newsletters? That depends on what you need. You can e-mail Harry directly with your requests -- or better yet, join in the Hot Tip Harry Discussion Forums on Cadalyst.com and say what's on your mind. I'll compile the feedback and present solutions here -- all aimed at sharing information about the programmer's craft.
Until next time, 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 Tips & Tricks Tuesdays free e-newsletter and we'll notify you every time a new video tip is available. All exclusively from Cadalyst!