Circles and Lines: The CUI and Macro Madness12 Jan, 2006 By: Lynn Allen Cadalyst
The real power of CUI involves writing your own macros.
For the past three months I've explored how the new CUI dialog box makes it easy for AutoCAD 2006 users to customize menus, shortcut keys, mouse buttons, etc. This month I'll finish off the CUI series with a booster shot for you to venture into the more powerful side of customizing--let's delve into writing your own macros.
Pull up the CUI dialog box and find the command list in the lower left corner (figure 1). Everything you can drop into a menu or assign to a shortcut key must exist in this list first.
Figure 1. The CUI command list displays all the commands you can use in your menus.
AutoCAD built quite a few macros for you already. For example, you'll see Zoom Object or Break at a Point. You can add your own macros too. Perhaps you'd like to create a text macro that sets the current layer to Text first and then executes Text or Mtext. Maybe you have a frequently used fillet radius you'd like to quickly grab from a menu, or anything you find yourself doing over and over. Macros can save you valuable time by automating tedious processes.
You just need to understand the macro lingo. It looks complicated and scary, but it's really quite simple. You just need to know the secret code.
To execute an Enter in a macro, you will use a semicolon (;). For example:
Zoom;a will execute a Zoom (Enter) a for all (Enter)
AutoCAD automatically executes an Enter at the end of the macro for you (as long as the last character is not a backslash or a semicolon). This means you don't need to include the final semicolon (but it doesn't hurt if you do).
Zoom;a; this would also work fine.
Let's try another macro that sets our limits to 36 X 24. Sometimes it helps to go through the command manually first inside of AutoCAD to ensure you know all the prompts you'll need to answer.
Command: Limits (Enter)
Reset Model space limits:
Specify lower left corner or [ON/OFF] <0.0000,0.0000>: (Enter)
Specify upper right corner <12.0000,9.0000>: 36,24 (Enter)
Now let's try it in macro form.
Limits;0,0;36,24 will execute the Limits command (Enter) 0,0 (Enter) 36,24 (Enter)
Let's look at the AutoCAD prompts to erase the last object on the screen.
Select objects: L (enter)
Select objects: (enter)
And now the macro form:
This executes the Erase command, (Enter) L (Enter) (Enter).
Why do you need an extra Enter? You want to get completely out of the Erase command and the Erase command takes an extra Enter to get to the Command prompt.
Often you want the menu items to pause for user input. In order to get that to occur, you will use a backslash (\). Let's say you want to create a circle that lets the user select the center and then specifies a radius of 2.
Circle;\2 This executes the Circle command (Enter), pauses for the user to select the center of the circle, and then uses 2 for the radius (Enter)
Let's try one more -- we'll set the current layer to Text and then execute the Mtext command.
Let's look at the manual process inside of AutoCAD first. I'll use the system variable Clayer to quickly reset the current layer.
Command: Clayer (Enter)
Enter new value for CLAYER <"0">: Text (Enter)
Command: Mtext (Enter)
And now the macro:
Macros can't really talk to dialog boxes. Consequently, while creating macros we often want to execute the Command line interface of a command rather than the corresponding dialog box. To do so, simply insert a dash in front of the command. For example, I want to insert a block of a desk. AutoCAD executes the Insert dialog box by default; I need to override that by forcing a Command line interface with a dash. I want the user to select the insertion point, leave the x- and y-scale factors at 1, and then pause again for user input on the rotation angle.
Let's look at the AutoCAD command prompts first:
Command: -Insert (Enter)
Enter block name or [?]: desk (Enter)
Specify insertion point or
[Basepoint/Scale/X/Y/Z/Rotate/PScale/PX/PY/PZ/PRotate]: (pause for user input)
Enter X scale factor, specify opposite corner, or [Corner/XYZ] <1>: (Enter)
Enter Y scale factor <use X scale factor> (Enter)
Specify rotation angle <0>: (pause for user input)
Now let's convert this to code:
You'll notice that nearly all of the macros in the CUI start with ^C^C. This executes two Cancel commands (or Escapes). Issuing ^C^C before a macro ensures you are at the Command prompt before the command is executed. We all know that you can't execute one command while you are in another (unless it's a transparent command). For example, you can't execute the Circle command while you are in the Line command. So correcting our macros above, you'll add ^C^C to the beginning of each line of code.
I realize that the commands that begin with a C make this code look all the more confusing, but that's the way it is! Why two ^C's? Just in case you are in an embedded command that requires two Cancel commands to get all the way out to the Command prompt (such as DIM or Layer).
Here's a handy macro -- see if you can figure out what it does.
This macro makes sure you are at the Command prompt, executes the Trim command, uses an extra Enter to select everything as a cutting edge and then pauses for the user to select the objects to trim.
Do you always need to start with ^C^C? Not if you are using a transparent command such as:
This transparently executes the Zoom command (Enter) P for previous (Enter).
Automatic Repeating of a Macro
Perhaps you'd like to create a macro that repeats until you hit Escape to terminate. Simply add an asterisk (*) to the beginning of the code.
I want to create a macro that creates multiple squares. Let's look at the AutoCAD prompts first.
Command: Polygon (Enter)
Enter number of sides <4>: 4 (Enter)
Specify center of polygon or [Edge]: e (Enter)
Specify first endpoint of edge: (pause) Specify second endpoint of edge: (pause)
So now you know how to write these macros, what do you do with them? You can add your own commands to the AutoCAD command list in the CUI dialog box. Simply select the New button, name your macro and enter the macro code. Figure 2 shows the addition of a command called Square using the macro code above.
Figure 2. Select the New button to add your own command and macro code.
After you've created your own commands, you can drag and drop them into menus as discussed in my previous CUI articles.
Incidentally, this macro syntax is consistent for all releases of AutoCAD. The process of adding these macros into your menus, however, is different. You can also use these macros in your palettes.
This column was a quickie lesson on macros -- hopefully you'll be adventurous and give them a try! Happy New Year to you, and Happy AutoCAD-ing!
Autodesk Technical Evangelist Lynn Allen guides you through a different AutoCAD feature in every edition of her popular "Circles and Lines" tutorial series. For even more AutoCAD how-to, check out Lynn's quick tips in the Cadalyst Video Gallery. Subscribe to Cadalyst's Tips & Tricks Tuesdays free e-newsletter and we'll notify you every time a new video tip is available. All exclusively from Cadalyst!
Place your DWG File on a Diet 18 Jun, 2013
Tech Toys 360: Pro-Form Le Tour de France Bike 18 Jun, 2013
Update 3 now available for Revit 2013 Products - Revit Clinic 18 Jun, 2013
Nice Design—What Does It Feel Like? 17 Jun, 2013
LiveMap: The Augmented Reality Motorcycle Helmet of the Future? 18 Jun, 2013