Down and Dirty Customizing-Part II

28 Feb, 1999 By: Lynn Allen

Last month, we discussed the basic structure behind the AutoCAD menu. You probably didn't find it to be the most exciting column, but nevertheless an important precursor to the entire customization process. This foundation provided the first step along the road to customizing your AutoCAD environment to suit your specific needs. This month, we'll continue to cover actual menu syntax and create some simple macros.

It's probably easiest to start with programming your toolbars. A few months ago, I wrote a column that stopped short of programming the macros that exist behind the tools on the toolbars (Circles and Lines, "Creating Your Own Toolbars," October, 1998, pp. 99-104). Let's right click once on any icon on a toolbar to display the Toolbars dialog box. Right click again on a tool to display the Button Properties dialog. Here's a quick review of this dialog box:

Name-the tooltip name that appears when the cursor hovers over the button.

Help-the user help string that appears along the status line.

Edit-the property that sends you to another dialog box for editing the picture on the button.

Macro-the programming that tells AutoCAD what you want a button to do when it's selected.

Let's look at some basic macro syntax. As you know, you can't run a standard AutoCAD command unless you're at the command prompt (we'll address transparent commands later). For example, if you tried to run the LINE command while still in the COPY command, you'd get an error message. To ensure that AutoCAD is at the command prompt before executing your toolbar command, we'll need to run a couple of cancels. This is done with the following syntax:


This syntax actually executes two control-C's (or cancels). Why two? There are still some commands that could require two cancels to get you all the way back to the command prompt. Selecting an option of the LAYER command, the old DIM: commands or the CHANGE command could all put you in this particular situation. Just to make sure, we always include two cancels (better safe then sorry!). I do know some overly paranoid programmers who actually include three ^C's in their macros, though it's really not necessary.

To write a macro that executes the LINE command, use the following syntax:


To write a macro that executes the CIRCLE command, use the following syntax:


The two C's next to each other look a bit strange, but it is correct. (Note: It doesn't matter whether or not you use upper or lowercase characters. I mixed it up in my two examples just to illustrate this.)

The AutoCAD macros found in your existing toolbars include an underscore in front of the commands as displayed in the following example:


Since AutoCAD is translated into so many different languages (19, I believe), it's important that AutoCAD search for the English source command. Hence, the underscore forced AutoCAD to do exactly that. If you have no intention of using your macros in any language other than English, then you don't need to include the underscore. For our purposes, we'll go ahead and leave it out.

Adding Enters to Your Macros
Should you need to include an Enter in your macro, do so by using a semicolon (;). The following syntax erases all of the objects in your drawing:


This is the same as entering the following syntax from the command prompt in AutoCAD:

<Esc>, <Esc>, ERASE, <Enter>, ALL, <Enter>, <Enter>

and it yields the following results:

Command: erase

Select objects: all

34 found

Select objects: <Enter>

It takes two enters to get all the way out of the ERASE command, hence the additional Enter at the end. Without it, this macro leaves you in the ERASE command, which would be bad programming manners!

Pausing for User Input
You might also want to pause your macro and let the user answer a question. Perhaps you're writing a macro that inserts a block called PULLEY, so you probably want to pause when the INSERT command prompts for an insertion point and let the user answer that question. You can do this by placing a backslash in the macro when you want to turn control over to the user. You might also want to let the user select a rotation angle for the block, as shown here:

^C^Cinsert; pulley;\;;\

This is the same as entering the following syntax from the command prompt in AutoCAD:

<Esc>, <Esc>, INSERT, PULLEY, (user-input), <Enter>, <Enter>, (user-input)

which yields the following results:

Command: insert

Block name (or ?): pulley

Insertion point: pause for user input

X scale factor <1> / Corner / XYZ: <Enter>

Y scale factor (default=X): <Enter>

Rotation angle <0>: pause foruser input

You'll find that you need to know the proper command sequence before coding your macro. Those of you who've been using AutoCAD for years will probably already know the various AutoCAD command sequences off the top of your heads. Those of you who are new to the program may have to actually key in the sequence within AutoCAD first, before coding your macro.

You'll find that, even though you can execute a dialog box within a macro, you can't do anything else past that point; you can't select any of the options within the dialog box. For example, you can execute the layer dialog box (DDLMODES), but you can't create any new layers, set a new current layer and so on. There's no way to use a macro to select a button within a dialog, fill in a text box and so forth. If you replaced the INSERT command that we used in the function above with the DDINSERT command, it would no longer work. Hence, you'll have to resort to the old command-line driven commands. For example, instead of the DDLMODES command, you'll use LAYER; instead of MTEXT, you'll use TEXT; instead of BHATCH, HATCH.

In Release 14, a few of the command-line driven commands such as TEXT, XREF and STYLE, now automatically execute the associated dialog box. To force the command-line interface of the command, you'll have to place a dash (-) in front of it as in this syntax:



If I wanted to ensure that my users were on the DIMENSION layer before they executed the DIMLIN command, the syntax could look like this:


This would be the same as entering the following syntax from the command prompt in AutoCAD:

<Esc>, <Esc>, LAYER, <Enter>, S(for SET), <Enter>, dimension, <Enter>, <Enter>, DIMLIN

which yields the following results:

Command: -layer

?/Make/Set/New/ON/OFF/Color/ Ltype/Freeze/Thaw/LOck/Unlock: S

New current layer <0>: dimension

Freeze/Thaw/LOck/Unlock: <Enter>

Command: DIMLIN

If you're in Release 13 or prior, leave the dash preceding the LAYER command out of your macro as illustrated here:


Transparent Commands
There are many transparent commands within AutoCAD. ZOOM, PAN, VIEW, and DDLMODES are just a few that come to mind. A transparent command is a command that you can use within an existing command. To use a command transparently, you place a single quote in front of it. When executing commands that can behave transparently, you probably won't want to include cancels in front of the macro as in the following example:


This cancels the user out of the existing command before executing the ZOOM command. This might upset users if they want to perform the ZOOM without leaving the command they were currently in.


is a more versatile macro.

How to Torture Your Coworkers
Programming macros is best used to increase your productivity, but can occasionally be used to have a little fun with your coworkers. As I travel around the world speaking on various AutoCAD topics, I am amazed to find the excitement generated when I share some clever ways to torture coworkers. You'll find that the more you learn about customizing, the more dangerous you become. With that disclaimer, I'll show you a clever trick you can perform using toolbars and your new found knowledge.

A simple method of torture would be just to switch the macros associated with the toolbars around. For example, what's to keep you from switching the LINE and CIRCLE commands.

Though this is a mean trick, and one you probably shouldn't employ when your co-worker is on poduction deadlines, if you followed along, it tells me you understand the information you've just read. Along with this knowledge comes the responsibility to use it wisely-don't abuse it! Until next month...

More News and Resources from Cadalyst Partners

For Mold Designers! Cadalyst has an area of our site focused on technologies and resources specific to the mold design professional. Sponsored by Siemens NX.  Visit the Equipped Mold Designer here!

For Architects! Cadalyst has an area of our site focused on technologies and resources specific to the building design professional. Sponsored by HP.  Visit the Equipped Architect here!