The World of AutoCAD Programming Platforms, Part 112 Apr, 2012 By: Andrew G. Roe
VBA remains an option, but its days appear numbered. Check out the other choices available for AutoCAD customization.
AutoCAD's open architecture has long been one of its most appealing qualities. The freedom to customize menus, add custom commands, and develop add-on programs that run within AutoCAD has motivated numerous users to get under the hood and do some fine-tuning.
With AutoCAD 2013 now on the street, the options for programming AutoCAD remain robust, although the landscape continues to evolve. Autodesk has indicated for several years that Visual Basic for Applications (VBA), a programming environment introduced in the late 1990s, will eventually be phased out. VBA still has a pulse for the time being, but it appears to be on life support, as Autodesk is promoting Visual Studio.NET as the preferred development platform instead.
The .NET environment — which is more versatile and complex than VBA — has gained popularity in recent years, but has left some less-experienced programmers out in the cold. Meanwhile, the growth of mobile devices and cloud computing has introduced new considerations for CAD users and programmers. To better understand this shifting landscape, let's review various customization options, starting with the simpler methods and progressing to more complex alternatives.
First off, AutoCAD offers plenty of customization options for non-programmers. Custom menus and commands have been achievable since the early days, before AutoCAD releases were tagged by year. Using customization tools from the AutoCAD ribbon, along with some behind-the-scenes work, you can customize the user interface, define external commands that invoke other programs from the AutoCAD Command prompt, create aliases for AutoCAD command shortcuts, and craft scripts to batch-process groups of commands.
AutoCAD 2009 introduced a macro recorder that allows you to automate frequently repeated tasks without typing any code. From the Manage tab of the ribbon, simply click the Record button to capture AutoCAD actions, the Stop button to end the macro recording, and the Play button to execute the macro.
AutoCAD's Action Recorder enables users to automate repetitive tasks.
Unlike the macro recorder in Microsoft Office, AutoCAD's Action Recorder does not produce editable Visual Basic code, but it allows certain adjustments to recorded steps and prompts for user input.
Introduced in AutoCAD Release 14, VBA enabled users to develop event-driven programs within AutoCAD sessions using a handy integrated development environment (IDE).
Microsoft and Autodesk are phasing out VBA, shown here, in favor of .NET.
VBA quickly gained popularity, as even novice programmers found they could write programs with graphical user interfaces (GUIs) and logical branching. Access to AutoCAD's object model provided easy ways to create and modify entities, extract data from drawings, and adjust drawing settings and properties. Using ActiveX automation, programmers could also write standalone VB applications to drive AutoCAD and exchange information with other VBA-enabled products such as Microsoft Word, Excel, and Access.
VBA remained a popular development platform for several years, but Microsoft stopped distributing VBA licenses in 2007, favoring its .NET framework for more flexibility in development, deployment, and Web services.
Autodesk removed VBA from the standard AutoCAD interface in AutoCAD 2010, but has continued to unofficially support VBA with a downloadable VBA enabler for versions 2010–2013. The plug-in has allowed many developers to continue using VBA routines, albeit with some difficulty: Because VBA is a 32-bit application, it runs as a separate 32-bit process on 64-bit AutoCAD, resulting in sluggish behavior and even crashing of VBA routines. VBA’s days are clearly numbered for AutoCAD users.
Visual Studio .NET and VSTA
Much to the chagrin of some VBA fans, the future of AutoCAD programming lies in Visual Studio.NET, a true object-oriented programming environment that enables development across multiple languages including VB and C#. “.NET is more of a programmer's programming environment,” said Stephen Preston, Autodesk's senior Americas manager of Developer Technical Services.
Along with the additional power comes a steeper learning curve. Instead of working inside the AutoCAD VBA environment, applications are developed externally in the .NET environment. Additional programming savvy is required to manage system settings, references, and other resources. Once the basics are grasped, however, the potential for developing professional applications is greater. “We're encouraging people to take the plunge,” said Kean Walmsley, software architect for Autodesk's AutoCAD product line. The full Visual Studio.NET product is a separate purchase costing several hundred dollars, but Microsoft's Visual Basic Express can be downloaded for free and provides virtually the same functionality AutoCAD programmers need.
Several years ago, Microsoft also introduced an embedded version of Visual Studio called Visual Studio for Applications (VSTA), but it seems to have a limited future. Initially intended to replace VBA, VSTA never really gained momentum, and Microsoft is fairly close-lipped about its future. Autodesk embedded VSTA in recent versions of Revit, but removed it from the 2013 version.
Based on the LISP language developed in the 1950s, AutoLISP has been a favorite of AutoCAD programmers since the mid-1980s. With a somewhat cryptic syntax relying heavily on parentheses, AutoLISP can initially confuse novice programmers, but supporters claim it provides easier interaction with AutoCAD than VBA or .NET. You can type an AutoLISP statement in the AutoCAD Command line and see the results directly. To make AutoLISP easier to use, Autodesk introduced Visual LISP (VLISP) in R14, with an IDE similar to that of VBA, along with a compiler, debugger, and other tools.
Visual LISP is easier to use than AutoLISP, and has an integrated development environment similar to VBA.
For advanced programmers, ObjectARX (AutoCAD Runtime Extension) provides a compiled-language environment that allows you to load and run projects in the same address space as AutoCAD. This enables programs to run more efficiently. ObjectARX is typically used by third-party developers building commercial AutoCAD add-ons and requires knowledge of C/C++ programming languages. In other words, it is not for beginners.
Mobile and Cloud Options
The growth of mobile and cloud computing is adding a new twist to AutoCAD programming. With products like AutoCAD WS allowing users to access drawings with mobile devices, programmers are eager to develop custom applications that enhance that process. Autodesk has introduced APIs (application programming interfaces) that allow users to connect external storage services supporting WebDAV (Web Distributed Authoring and Versioning), an extension of the Hypertext Transfer Protocol for data exchange with Web servers.
More APIs are under development, so stay tuned. Preston advises potential mobile programmers to learn C#, as it more closely resembles languages used for mobile platforms, such as Java for Android devices and Objective-C for Apple's iOS.
Which Way to Go?
With all these options available, a common question is: Which programming approach should I adopt? It seems inevitable that Autodesk will eventually pull the plug on VBA, so only follow that route if you're maintaining older applications. Even then, you might want to start migrating VBA routines to .NET. AutoLISP, once considered a dying art, appears to have a place in the AutoCAD arena for the near future, thanks to the convenience of the VLISP IDE and a dedicated user base. If you know AutoLISP, you can maintain current routines, but think twice before starting any significant new development.
In most cases, the .NET environment appears to offer the most potential for new applications. Check out the AutoCAD .NET Developer's Guide or AutoCAD's online help resources to get started. In the next part of this series, we'll explore some programming examples to help you become more familiar with the different options.