Create Macros

AutoCAD

 
Create Macros
 
 
 

A macro defines the action that results when an interface element is selected. A macro accomplishes a drawing task that would otherwise take a series of actions by a user. A macro can contain commands, special characters, DIESEL (Direct Interpretively Evaluated String Expression Language) or AutoLISP programming code.

NoteAs AutoCAD is revised and enhanced, the sequence of prompts for various commands (and sometimes command names) might change. Therefore, your custom macros might require minor changes when you upgrade to a new release of AutoCAD.

You add macros to interface elements by using the Customize User Interface (CUI) Editor. Select an existing command or create a new command in the Command List pane. Enter macros in the Macros section of the Properties pane. There are no length limitations for macros. However, you do need to know how specific characters are used in macros and be aware of other considerations or limitations.

Macro Basics

A macro in a user interface element can be as simple as a command (such as circle) and some special characters (such as ^C^C).

For example, the macro ^C^C_circle \1, draws a circle with a radius of 1 unit. The components that define this macro are explained in the table below.

Components in CIRCLE macro

Component

Component type

Result

^C^C

Special control character

Cancels any running commands

_

Special control character

Automatically translates the command that follows into other languages

CIRCLE

Command

Starts the CIRCLE command

\

Special control character

Creates a pause for the user to specify the center point

1

Special control character

Responds to the prompt for the circle's radius (1)

For a list of special control characters that you can use in macros, see Use Special Control Characters in Macros.

Cancel Running Commands

Make sure that you have no AutoCAD commands in progress before you execute a macro. To automatically cancel a command before executing a macro, enter ^C^C at the beginning of the macro (which is the same as pressing ESC twice). Although a single ^C cancels most commands, ^C^C is required to return to the command prompt from a dimensioning command and ^C^C^C is required based on the current option of the Layer command. ^C^C handles canceling out of most command sequences and therefore is the recommended sequence used to ensure no command is active before the macro is started.

Verify Macro Characters

Every character in a macro is significant, even a blank space.

When you place a space at the end of the macro, AutoCAD processes the macro as though you had entered a command (circle, for example) and then pressed the SPACEBAR to complete the command.

Terminate Macros

Some macros require special terminators. Some commands (TEXT, for example) require you to press ENTER rather than SPACEBAR to terminate the command. Some commands require more than one space (or ENTER) to complete, but some text editors cannot create a line with trailing blanks.

Two special conventions resolve these problems.

  • A semicolon (;) in a macro automatically issues ENTER at the command prompt.
  • If a line ends with a control character, a backslash (\), a plus sign (+), or a semicolon (;), AutoCAD does not add a blank space after it.

An item that ends with a backslash (\) pauses a macro for user input.

Compare the following macros:

ucs 
ucs ; 

The first example enters ucs at the command prompt and presses SPACEBAR. The following prompt is displayed.

Specify origin of UCS or [Face/NAmed/OBject/Previous/View/World/X/Y/Z/ZAxis] <World>:

The second example enters ucs, presses SPACEBAR, and presses ENTER, which accepts the default value (World).

Suppress Echoes and Prompts in Macros

Characters in a macro appear in the command window as though you had typed the characters on the keyboard. They are also displayed in the user interface element. This display duplication is called “echoing”. You can suppress the “echoed” displays with the MENUECHO system variable. If echoes and prompts from item input are turned off, a ^P in the item turns them off.

Create Long Macros

You can create a macro of any length, without requiring any special characters at the end of a line. The Properties pane in the Customize User Interface (CUI) Editor accepts a macro of any length.

  • Use Special Control Characters in Macros

    You can use special characters, including control characters, in macros. In a macro, the caret (^) is equivalent to pressing the CTRL key on the keyboard. You can combine the caret with another character to construct macros that do such things as turn the grid on and off (^G) or cancel a command (^C).

  • Pause for User Input in Macros

    To accept input from the keyboard or pointing device in the middle of a command, place a backslash (\) in the macro at the point where you want input.

  • Provide International Support in Macros

    To develop menus that can be used with a non-English-language version of AutoCAD, precede each command or option with the underscore character (_). The underscore character allows the standard commands and options to be translated automatically.

  • Use Built-in Commands in Macros

    To develop macros that use built-in commands that are part of AutoCAD, precede each command with the period character (.). The period character allows the built-in command to be used even if it has been undefined with the UNDEFINE command making the macro predicable when it is used on other systems that share the same customization file.

  • Repeat Commands in Macros

    You can use a leading asterisk (*) to repeat a command in a macro until you choose another command.

  • Use Single Object Selection Mode in Macros

    Single Object Selection mode cancels the normal repetition of the Select Objects prompt in editing commands. After you select one object and respond to any other prompts, the command ends.

  • Use Macros to Swap User Interface Elements

    You can replace the contents of active menus, mouse buttons, tablet buttons, tablet menus, or screen menus. The swapped content can be that of another user interface element of the same type in the main CUI file, or it can come from a partial CUI file.

  • Use Conditional Expressions in Macros

    You can add conditional expressions to a macro by using a command that introduces macro expressions written in DIESEL (Direct Interpretively Evaluated String Expression Language).

  • Use AutoLISP in Macros

    Creating commands that use AutoLISP is a more advanced way to use the AutoCAD customization feature.