Cadalyst CAD Management

CAD Programming: Introduction

Written by Andrew G. Roe | Aug 24, 2021 11:51:00 AM

Regardless of how many features are packed into CAD software, end users inevitably want to add more features or adjust existing features. Today’s CAD software products offer plenty of options for doing this, ranging from basic interface customization to integration of multi-featured add-on software. This article looks at programming options for two of the more commonly used CAD products in the AEC industry — Autodesk’s AutoCAD and Bentley Systems’ MicroStation. This is not intended to be an evaluation or comparison of the two products’ programmability, but an overview of the options available for different situations.

 

Simple Customization

Starting with customization options for non-programmers, both AutoCAD and MicroStation, along with other Autodesk and Bentley products, allow you to customize menus and commands to fit your needs. We won’t dive into the details here, but using customization tools, along with some behind-the-scenes work, you can tweak the user interface, define external commands that invoke other programs from the CAD environment, and create scripts to batch-process groups of commands.

AutoCAD and MicroStation each include macro recording features that allow you to automate commonly repeated tasks without typing any code. In AutoCAD, 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 macro recorder allows easy recording and playback of automated scripts.

 

In MicroStation, the process is similar, with the macro recorder found in the Utilities tab. MicroStation also allows you to promote a macro script to a Visual Basic for Applications (VBA) project for further editing and customization.

MicroStation scripts can be promoted to VBA projects.

 

VBA 

Microsoft introduced VBA in the early 1990s for automating tasks in Microsoft Office, and later offered licenses for other software vendors to incorporate the programming environment into their products. Autodesk incorporated VBA into AutoCAD in the late 1990s, enabling users to develop event-driven programs, including graphical user interfaces (GUIs), logical branching, and access to AutoCAD’s object model — all within the AutoCAD environment. 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 later restored it, so VBA is still readily accessible from within AutoCAD.

Bentley introduced VBA with the MicroStation V8i generation and has continued to include it in its Bentley CONNECT Edition products. Similar to other VBA environments, MicroStation VBA provides access to Microsoft VBA functionality and MicroStation VBA's Object Model library. 

Learn more about VBA and macros basics here.  

 

.NET

As VBA has endured an uncertain future, Visual Studio.NET has gained popularity among serious CAD developers due to its versatility and more robust feature set. As a true object-oriented programming environment that enables development across multiple languages such as VB and C#, .NET offers additional power but it also comes with a steeper learning curve. Instead of working inside the CAD environment, applications are developed externally in the .NET environment.

Additional programming skills are required to manage .NET system settings, references, and other resources, but once you grasp the basics, the potential for developing professional applications is greater. The full Visual Studio.NET product is a separate purchase, but users can download Microsoft’s Visual Studio Community edition for free, and it provides much of the same functionality needed for CAD programmers. The .NET environment remains a viable option for both AutoCAD and MicroStation programmers.

Learn more about .NET here.

 

Vendor-Specific Options

In addition to the platform-neutral options, both Autodesk and Bentley also offer proprietary options. AutoLISP, based on the LISP language, remains a favorite of longtime AutoCAD programmers. With a somewhat cryptic syntax relying heavily on parentheses, AutoLISP can initially confuse novice programmers, but it provides handy interaction with AutoCAD. To make AutoLISP easier to use, Autodesk introduced Visual LISP (VLISP), which provides an IDE similar to that of VBA, along with a compiler, debugger, and other tools.

For more advanced AutoCAD 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. ObjectARX is typically used by third-party developers building commercial AutoCAD add-ons and requires knowledge of C/C++ programming languages. ObjectARX files are one of several file types available from the Load/Unload Applications window in AutoCAD. 

Get reacquainted with AutoLISP here and dig deeper here

You can load a variety of application file types into AutoCAD.

 

Bentley’s MicroStation Development Libraries (MDL) provide intermediate and advanced programmers additional power. Application programming interfaces (APIs) are available in both native (C++ and C) and managed (.NET) environments, providing access to the MicroStation object model and DGN design data. Many parts of standard MicroStation have been developed using MDL.

MicroStation can simultaneously manage multiple MDL applications. Some MDL applications insert their own submenus in the Applications menu in MicroStation's main menu bar and have their own key-ins.

Developers can add new MDL applications to those already provided with MicroStation.

 

Python and Other Tales

Python. A small, but growing number of CAD programmers have dipped their toes into open-source programming languages, which are generally free to download and use in application development. Python, an open-source platform reportedly named after the BBC show “Monty Python’s Flying Circus,” is an interpreted language, requiring no compilation or linking. This allows more interactive experimenting and testing of programs. Python provides high-level data structures and an object-oriented programming approach, and can be used as an extension language for customizable applications such as AutoCAD and MicroStation, though perhaps less directly than other options.

Documentation is a bit lean on connecting to AutoCAD and MicroStation with Python. Due to its open-source nature, some of the best sources are public domain web sites and user groups. The Python Package Index (PyPI) is a repository of software for the Python programming language, and includes an AutoCAD-specific pyautocad site, which provides specific information on writing ActiveX Automation scripts with Python.

On the Bentley side, distinct Python developer shells are available for certain vertical applications. For example, Python-based software development kits (SDKs) are available for LumenRT, Bentley’s visualization and reality modeling software, and PLAXIS, a geotechnical analysis product. Check out the Bentley visualization and geotechnical analysis user communities for more information.

Get started with Python here.

Learn how to use Python with .NET here.

Dig deeper into Python and pyautocad.

Dynamo. Another open-source alternative for CAD programmers, Dynamo offers a visual programming environment with minimal coding. Running on the side as a standalone product, it has its own execution engine and can access other products via connectors. In 2015, Autodesk introduced its companion product, Dynamo Studio, to aid developers, but recently announced plans to discontinue it, as Dynamo capabilities have been extended into several products, including Revit and Civil 3D. The core Dynamo technology is available as a free download via the Dynamo Sandbox.

Dynamo is an open source visual programming environment option for CAD users.

Learn Dynamo basics here.

Dig in deeper on out how to use Dynamo here.

TypeScriptTypeScript is Microsoft’s superset of JavaScript, has garnered some attention as a tool to develop portable code using high-level language features. It adds error-checking capabilities to those of JavaScript, which originated as a simple scripting language for web browsers. Bentley’s iTwin.js, a comprehensive set of APIs for working with digital twin models (discussed further below), is written in TypeScript.

React. This open-source JavaScript-based tool provides a library for building user interfaces. Maintained by Facebook and a community of developers, it can be used in the development of single-page or mobile applications, but requires the use of additional libraries for client-side functionality and connections to CAD software.

 

What Next?

The growth of mobile and cloud computing has added new twists to CAD programming in recent years. With mobile products allowing users to access drawings and other data with mobile devices, programmers are constantly seeking new and enhanced tools to develop custom applications for these environments.

Forge. Autodesk’s cloud platform, allows developers to run AutoCAD, Civil 3D, Revit, Inventor, and 3ds Max engines on the cloud with design automation, adding efficiency to capturing and re-using data. Autodesk Construction Cloud is also part of Autodesk’s cloud strategy and connects workflows, teams and data at various stages of construction.

Bentley’s iTwin platform services. These solutions have resulted in new solutions for web-based design review and construction management, as well as the ability to reference models into design products from the cloud. The iTwin platform enables data aggregation across various supported vendor file formats with the ability to access other project-related and previously unavailable data. Bentley’s Platform as a Service (PaaS) enables developers to create their own digital twin solutions or enhance the Bentley ecosystem with new solutions.

If you’re a novice programmer, the numerous options may appear daunting, but don’t despair. With so many options available, you can choose the tools that best fit your needs and skillset, and build on them over time.

In future articles, we’ll look at some specific examples to help you become more familiar with the different programming options.