Creating Command Macros from AutoCAD Scripts

September 15, 2021 Matt Miyamoto

If you’ve been working with AutoCAD scripts and are looking for ways to access them a little quicker, creating custom command macros out of your scripts is something you should consider.  AutoCAD command macros can be created quickly through the CUI dialog and allow you to define your own custom commands.  An additional benefit of command macros is that they also understand and process command strings from AutoCAD scripts.

If you want to create a custom command macro out of an AutoCAD script, you’ll need to come up with a keyboard shortcut for the command, find an image for the icon, and do a quick copy and paste.  Because this is accessing the CUI file for AutoCAD, you’ll also need Administrative access for the location where your files are saved.

For this example, I have a script that runs the -PURGE command and purges four items automatically (zero-length geometry, empty text objects, RegApps and orphaned data).  The script saves me time by running through each option consecutively in one process rather than having to repeat the command and specify each item one at a time.  As a script, dragging and dropping the file into an open AutoCAD session will run it, however if I have multiple files that I want to use it on, having access to a command within the application would be faster.

The script string is fairly simple, and shown below.  It repeats the -PURGE command four times, each with a different option for the relevant items to be purged.

-PURGE Z              -PURGE E              -PURGE R             *             N             -PURGE O

To convert this into a command macro, my first step is to use the CUI command and the option to Create a New Command.

To define my new custom command, I’ll use PZERO as the command name and have an image that I’ve created for the icon.

After entering the info in the right panel of the CUI dialog, the last thing I need to do is copy and paste the script code into the Long String Editor for the macro.

A single click in the Macro cell displays the ellipsis icon (...) and a single click on the ellipsis opens the Long String Editor dialog where I can paste my script content.

By default, the Long String Editor starts with ^C^C which is shortcut code for the ESC key (twice) or “Cancel” at the command line.  It’s good practice to start macros with this to automatically cancel out of any existing commands before triggering your custom command.

Pasting my Purge ZERO code into the Long String Editor looks like this:

I added another ^C^C string at the end as an extra “Cancel” just in case.  Now I have a custom command macro defined.  Clicking OK, then Apply and OK again to exit out of the CUI dialog box confirms the changes.

The easiest way to access my new command is to add it to a ToolPalette.  With a  custom ToolPalette tab set up, all I need to do is right-click and choose Customize Commands.  In the CUI dialog, I’ll search for my command, then drag and drop it into the ToolPalette.

Now I’m all set, and anytime I want to run my custom PURGE Z-E-R-O command, all I need to do is click on it from the ToolPalette and I’ll be able to see the results appear in the command line history.

If you’ve been working with AutoCAD Scripts and want to take it one step further by converting it to an AutoCAD command macro, now you know how to do it. 

One quick tip; if your new command macro isn’t working after you exit the CUI dialog box, you may need to reload the CUI file or do a quick close and re-launch of AutoCAD to get your macro updated and working in your AutoCAD session.

About the Author

Matt Miyamoto

Project Manager - Civil Solutions<br><br>With over 15 years of experience in the civil engineering industry, Matt provides training, consulting, technical support, and implementation strategies for organizations transitioning to Civil 3D. Matt is a licensed civil engineer, an Autodesk Certified Instructor (ACI) as well as an Autodesk Certified BIM Specialist: Roads and Highway Solutions. Additionally, Matt is an Autodesk Certified Professional for AutoCAD and AutoCAD Civil 3D.

Follow on Linkedin More Content by Matt Miyamoto
Previous Article
Customizing Your Infraworks Model with Watermarks
Customizing Your Infraworks Model with Watermarks

Adding a custom watermark to Infraworks models

Next Article
Civil 3D - Issue: Pipe Length (Center vs Walls)
Civil 3D - Issue: Pipe Length (Center vs Walls)

When labeling Civil 3D Pipe lengths have options for Center to Center or Inside Walls.


Sign up for email updates

First Name
Last Name
Thank you!
Error - something went wrong!