Streamlining Design: Custom Scripting and Programming for CAD Automation

Custom Scripting in CAD

According to the Springer Handbook of Automation, automation has existed since prehistory and has been evolving for centuries. Over the years since, process autonomy, decision-making autonomy, and automatic control have evolved across three generations to what it is today. The first generation, spanning from prehistory to the 15th century, lacked automatic control but featured some process and decision-making autonomy. The second generation, characterized by automatic control but without the input of a computer, then followed. This generation spanned from the 16th to the early 20th century. 

Perhaps the most consequential propellant of automation came with the invention of the digital computer and the transistor, starting in the 1940s. This development birthed the current third generation. The subsequent evolution of the digital computer, computing power, and other technologies has driven the growth in automation and modern inventions. This includes the smartphone, internet, robotics, and advanced software like CAD and CAM.

Today, CAD and CAM tools are integral to automated manufacturing; they simplify otherwise time-consuming manual processes. For instance, designers use CAD software to create geometric data that the CAM software uses to generate tool paths automatically. However, despite their integral role in enabling automation, designers often perform repetitive tasks that could be automated. Which is where CAD automation comes in.

CAD automation streamlines design workflows, enhances productivity, improves accuracy, and frees up the designer to handle other more pressing tasks instead of cyclically handling the same tasks over and over. Several approaches are available to automate CAD software. We have discussed these approaches extensively in this article, albeit focusing on custom scripting in CAD as well as programming, which give you the freedom to decide how and what to automate. Let’s get started.

Basics of CAD Automation

There are five approaches you can use to achieve CAD automation, namely:

  1. Macros
  2. Scripts
  3. Custom CAD automation applications
  4. Automation add-ins and modules
  5. Built-in CAD automation functionalities

Macros

A macro is a prerecorded sequence of commands that the software executes. These commands are static, meaning the macro is inflexible and cannot be changed once recorded. As such, running a macro over and over performs the same task. You must record another macro if you need to automate a different task.

Scripts

You can think of scripts as text files that list software commands line by line. The software then interprets and executes the commands in the order in which they are listed. That said, it is essential to note that a script is not a program. Unlike programs, scripts do not need to be compiled. Instead, it is interpreted directly by the software. This means you cannot use a script to create applications, nor can you use them to create new classes of objects. Custom scripting in CAD is more flexible and useful than recording and using macros. 

Custom CAD Automation Applications

The third approach, creating custom and standalone automation applications, requires a programming background. These applications let you automate complex and repetitive applications. But first, you need to learn about and use the software’s application programming interfaces (APIs). 

Unlike scripts, custom automation applications are programs that have been compiled into machine language. Their lines of code have been compiled into machine language. As such, they exist as separate entities from the CAD software and can run independently. Custom applications make API calls to interact with the CAD software. An API call is a request to access the software’s data or specific functions. We discuss custom design automation applications in greater detail later in this article. 

Automation Add-ins and Modules

You can use an automation add-in if you are not well-versed in programming and cannot create a custom automation application. Such an add-in is created by a third-party developer and is intended to serve a specific function. 

For instance, AutomateWorks for SolidWorks helps you automate the process of configuring/customizing models with Excel. It eliminates manual processes, as the software automatically updates all 3D models, 2D drawings, and bills of materials based on the configurations you make. Similarly, DriveWorksXpress, which comes already installed in SolidWorks, is an entry-level design automation tool for automating everyday design tasks.

Built-in CAD Automation Functionalities

CAD software ships with some features that support design automation. Examples include:

  • CAD automation tools in NX include template-based design, generative design and topology optimization, and equation-driven modeling
  • iLogic for Inventor Automation allows users to create rules (logic) to accomplish various tasks, thus simplifying time-consuming processes
  • Design Tables in SolidWorks enable the creation of numerous configurations for parts or assemblies by defining dimensions in an Excel spreadsheet, whether embedded or externally linked
  • Parametric modeling is a design paradigm that uses parameters such as dimensions to create relationships between features. Thus, when you change one or more dimensions, the software adjusts the part accordingly.

This article will, however, focus on custom scripting in CAD and programming for CAD automation (creating custom automation applications).

Introduction to Custom Scripting in CAD and Programming in CAD

There is a need to distinguish between custom scripting in CAD and programming for CAD automation. 

Custom Scripting in CAD

Your typical design workflow may include repeating the same sequence of commands several times during the day. Of course, you can execute these commands from your CAD software interface. But the repetitive nature makes this approach quite inefficient, especially considering there are better ways of completing these tasks. One of them is custom scripting in CAD to automate this process. Custom scripting essentially means creating a script by listing these commands in a text file and having the CAD software run the script. 

Thus, a script helps you automate repetitive tasks. It does away with the need to manually input the commands by typing them or using your mouse pointer. And what is even more remarkable is that custom scripting in CAD is not limited to one or two processes. You can accomplish a lot with custom scripts. 

Popular Use Cases for Custom Scripting in CAD

You can create custom scripts that automate a number of processes, including:

  • Prompting the CAD software to communicate with you, the user, by having it require you to input a command or figure
  • Performing mathematical operations
  • Creating or modifying objects directly
  • Evaluating variable conditions
  • Accessing the software’s built-in commands and system variables and changing the settings in a drawing
  • Automating repetitive tasks
  • Creating, modifying, and exporting software data
  • Perform drawing cleanup operations across multiple drawing files

Tools for Creating Custom Scripts in CAD

There are three main tools you can use to create a custom script:

  1. External text editor: You can use a text editor like Notepad for Windows or TextEdit for macOS to write down your sequence of commands. This approach is recommended for fairly complex and lengthy scripts. By default, the editor will save the file using the .txt extension, which is not a script file extension. As such, you should change the file’s extension to the script file extension that your CAD software supports. For instance, AutoCAD and DraftSight require that you use the .scr file extension. With Scan2CAD, which lets you create scripts to batch convert multiple PDFs to DXF or DWG, save the script as a .s2c file. Creo Illustrate supports the .bat, .cmd, and .exe file formats.
  2. Built-in script editors: The Rhino 3D built-in script editor and 3ds Max built-in script editor allow you to create custom scripts and run them directly from the editors.

Creating Custom Scripts in CAD

Every CAD software has its own unique commands. This is a crucial point to consider when custom scripting in CAD: given that commands are not universal, you should not expect your script to run on every software. This is why we discuss creating custom scripts in CAD. If you are familiar with a particular software, you have its commands at your fingertips. 

But if you are a beginner or are unsure what commands to use, you can execute the commands as you would normally (using your keyboard or mouse). Then, use the command history as your reference or copy all its contents and paste it into the script file. However, disregard or delete any messages displayed in the command window/history; only copy the commands. Armed with the list of commands, follow these steps:

  1. Open an ASCII-based text editor (Notepad on Windows or TextEdit for macOS)
  2. Type the commands in the command history and include values that define the dimension or toggle some settings on or off. For this step, let’s consider the image below, which shows a simple script to be executed in AutoCAD. This CAD automation script is intended to direct AutoCAD to create a layer named title_block, assign color number 6 (c 6) to the layer, draw a rectangle that measures 828 by 552 units, enable ortho mode, turn off grid mode, enable zoom extent, and sets the running object snaps to Endpoint, Midpoint, and Intersection.

    A Custom Script for Creating a Rectangle in AutoCAD

    A Custom Script for Creating a Rectangle in AutoCAD (source)

    As the image shows, you must specify the various values for the dimensions and color number, for example. You must also add 0 or 1 to toggle a mode off or on, respectively.
  3. Save the text file using the appropriate script file format (as detailed above).

Best Practices for Custom Scripting in CAD

Creating a script for CAD automation follows what can be regarded as a set of rules, which we call best practices. Failure to adhere to these rules makes the file just another text file rather than a script. Here are a few best practices you should consider when creating a custom script for design automation:

  1. Use an ASCII text editor to create a script text file
  2. Each line of the script should contain a command
  3. Save the script file using a file extension supported by your CAD software; for instance, use the .scr extension for AutoCAD and DraftSight, .s2c for Scan2CAD, .bat/.exe/.cmd for Creo Illustrate.
  4. Use double quotation marks for commands such as the style or layer name that allow a string with spaces. Without the quotation marks, the blank space is interpreted as a press of the Enter button.
  5. Avoid commands that display dialog boxes, as the script cannot execute this command.
  6. End the script file with a blank line, as this ensures the software evaluates the last line
  7. To add comments in a script, start the comment with a semi-colon character (in AutoCAD) or # for other software
  8. Order the commands as you want them executed as they determine the sequence of responses
  9. Frequently revise the prompts and command names used in your script when you upgrade to the latest release of a program because the prompts and command names can change.
  10. Save the script files in a folder that is part of the software’s support file search path.
  11. If your script is intended to automate batch processing, it is recommended that you create backup copies of your files to prevent data loss.

Running Scripts

Creating and saving a script is just one part of the equation. The other entails running the script, which prompts the CAD software to load the script file. Once the software loads the script file, it executes the commands therein from the command window similarly to how it would execute normal CAD commands. And just like there are a number of approaches you can use to create a custom script, there are similarly several ways to run scripts, depending on the software. These include:

  1. Terminal: Every modern operating system ships with a text-based interface called a terminal that lets you enter commands that quite simplistically instruct the computer what to do. For instance, you can use this interface to compel the computer to open CAD software and run your script file. To do this, simply specify the paths for both. This is the approach supported by Scan2CAD.
  2. Command Window: This option enables you to run the custom script directly within the software using the software’s own script-execution command. For instance, to run the script within the AutoCAD interface, simply type SCRIPT on the command window and hit Enter. DraftSight uses a slightly different command, LOADSCRIPT, which prompts you to select your script file.
  3. Embedded Scripts: Some software applications, like Rhino 3D and AutoCAD, let you embed scripts in the toolbar button. However, this option limits script editing. To describe how you can go about using this option, let’s take the example of AutoCAD. With AutoCAD, you can customize the user interface, adding a button to the toolbar that, once pressed, runs a script command using a macro. This is highlighted in red in the image below. The script command then executes the script file, activating the action for which the button is intended, as shown here.

    Screenshot of the Customise User Interface Window in AutoCAD

    Customise User Interface Window in AutoCAD (source)


  4. Application Programming Interface (API): An API allows you to access most of the functionalities of the CAD software. As such, you can run the script using the API and execute various commands.
  5. Batch Processing Tools: Autodesk offers the ScriptPro tool that runs/executes the same script across multiple drawing files. The tool also allows you to call a script file from another script. As a result, you can run multiple scripts in selected drawings within the same session.

Do note that a script can be reversed by a single undo command, as the commands therein are treated as one. You can also pause the execution of a script file in DraftSight using the PAUSESCRIPT command.

Programming for CAD Automation

If you are more technically inclined, you will appreciate that there is a lot you can accomplish by creating a program for design automation. And with multiple CAD software publishers allowing programmers to create custom programs that integrate with and automate their software via an API, your work is cut out considerably. You can create custom design automation programs for SketchUp, NX, AutoCAD, Inventor, Revit, 3ds Max, Creo, and SolidWorks, just to mention a few.

SketchUp and NX

For instance, you can write a program for SketchUp that automates the process of using the pushing and pulling tool to create 3D shapes from a face. City planners commonly use this tool to create models of towns and cities. Such a program will make API calls via the SketchUp Ruby API.

Siemens also offers programmers NX Open, a collection of APIs that allows programmers to create custom applications for NX. NX Open does not restrict programmers to only creating tools for CAD automation. With it, you can create applications that customize the NX interface. You can also use it to integrate third-party applications with NX through an open architecture. It also lets programmers share data even across the internet. NX Open supports all well-known programming languages, including Python, Java, C, C#, C++, and Visual Basic, just to mention a few.

Autodesk Software

With the help of the API, you can also build apps and services that access all the functionalities and features of software running in the cloud. This means you do not have to install the desktop version of the software to use it. This is how Autodesk has implemented its APIs, collectively known as Autodesk Design Automation APIs, for software like Revit, Inventor, 3ds Max, and AutoCAD. This way, Autodesk lets you run custom add-ins and scripts that automate processes such as creating, modifying, and exporting software data.

Creo and SolidWorks

For its part, PTC allows programmers to expand the capabilities of Creo Parametric by writing programs and seamlessly integrating them into the CAD software via an API. The company provides a Creo customization toolkit to support the process. Similarly, SolidWorks’ API contains hundreds of actions that you can call from applications created using C#, C++, Visual Basic (VB), VB.NET, or Visual Basic for Applications (VBA). 

Benefits of CAD Automation with Custom Scripting and Programs 

Many benefits abound from automating CAD processes using custom scripts and programs. These include:

  1. Custom scripting in CAD simplifies otherwise demanding and time-consuming tasks and maintenance processes. For instance, you can write a simple script that accesses thousands of CAD files stored locally or in the cloud and converts them into another format.
  2. Creating scripts does not require a special IDE and can, therefore, be created by individuals with little to no programming knowledge. All you need is an ASCII-based text editor like NotePad or TextEdit.
  3. Scripts and programs can be integrated easily into any automation workflow via APIs. Software publishers include documentation that provides information on how to perform the integration.
  4. Custom scripting in CAD and the resulting CAD automation reduces mistakes and ensures accuracy
  5. Design automation via scripts and programs improves the workflow and enhances productivity
  6. Custom scripts and programs allow customization to standardize the model design and avoid repetitive modeling
  7. Besides automating CAD software, custom programs can customize the software and its capabilities

Overcoming Challenges in CAD Scripting and Programming

While custom scripting in CAD and programming for CAD automation do indeed offer numerous benefits, they are not without a set of unique challenges:

  1. Commands and prompts can change in future releases, so you may need to regularly revise your script whenever you or your company upgrades to the latest release of the software
  2. Language differences: some commands written in one language may not universally apply to software configured to use another. For instance, you cannot use an English command in the French version of the software. You can solve this problem by customizing the script accordingly or using an underscore before each command.

Conclusion

You can use several approaches to automate processes and tasks in CAD software. For instance, you could use built-in automation features like parametric modeling, design tables, rule-based (logic) tools, or automation add-ins. But these options may not be customized to your specific needs, meaning you have to make do with the functionalities the creator packaged into the product, which may not cut it for you. Fortunately, you could create a custom program that integrates with the software via an API or write a custom script. 

Generally, CAD automation lets you enjoy a bevy of benefits, including better productivity, improved accuracy, easy integration into existing workflows, and better customization. But perhaps the most universal benefit of CAD automation is that it automates repetitive tasks. This article has captured how you can automate such tasks by creating a custom script or custom program for a variety of CAD software, including AutoCAD, Revit, 3ds Max, SolidWorks, Inventor, DraftSight, SketchUp, and Creo.

scan2cad advert for free trial