Going for the Record! (Learning Curve AutoCAD Tutorial)
31 Jul, 2009 By: Bill FaneAutoCAD's Action Recorder lets you bring your macros to life.
It was a cold, clear, dark winter night. Once per second, the person beside Captain LearnCurve droned out "two seconds early, one early, on time, one late, on time …" They were on special-assignment duty, traveling at ridiculously high speeds down narrow, twisty, icy mountain roads …
… in the interior of British Columbia, Canada. The difference this time is that he's not being driven, he is the driver, and the voice beside him belongs to his navigator, Tom Burgess.
Many years ago, way back in the last millennium, the Captain and Tom were attending university together. Tom had an MGA sports car, and we ended up doing a lot of rallying together. The premiere event at the time was the Thunderbird. No relation to Ford; the thunderbird is a totem of West Coast native Americans and is the mascot of the University of British Columbia. Anyway, the Thunderbird was a winter weekender over snowy mountain backcountry roads. We won it in 1963. We have kept in touch over the years, and in 2007, Tom discovered that the Thunderbird was still running and that 2007 was its 50th anniversary. We entered and had so much fun that we have entered each year since then. We run as the Foolish Antiques Rallye Team (FART). Our slogan alternates each year between "We Drive Like Stink" and "Our Driving Stinks." We're aiming for a win in 2013, which will be the 50th anniversary of our original win. This should be some kind of record.
Captain LearnCurve and Tom Burgess en route to victory in the 1963 Thunderbird rally.
Record! That's it! This month's topic! The Action Recorder!
Do you often find yourself repeating the same sequence of commands over and over again? If so, wouldn't it be nice to be able to set things up so that a simple mouse click would repeat the sequence?
Well, technically this ability always has been possible. AutoCAD has supported menu macro customizing from the beginning, and AutoLISP programming was introduced in version 2.8 in 1986 or so. Subsequent programming support has included C++, ObjectARX, and VBA. The good news is that these customizing techniques can be very powerful. The bad news is that all of them require some level of programming skill and an intimate knowledge of the command-line version and responses of AutoCAD commands.
The really good news is that starting with AutoCAD 2009, it now (some would say finally) includes a macro recorder. Simply click on the Record button, perform the desired sequence of operations, and click on Stop. Next click on Play, and your sequence runs again.
Okay, so that explanation was a little simplistic. Let's take a look at it in greater detail. AutoCAD 2010 adds a bit more and changes a few things, which I'll come back to later. For now, I'll use the 2009 version.
Turn Me On!
As usual with AutoCAD these days, the command can be started several ways. The fundamental method is to enter ActRecord at the Command prompt, or ActRecord to start it transparently in the middle of another command. Secondary methods include:
- Ribbon: Tools tab | Action Recorder panel | Record
- Menu: Tools | Action Recorder | Record
- Right-click: Action Recorder | Record
You can find the Action Recorder on the ribbon.
Nothing much seems to happen except that the Action Tree window has appeared at the bottom of the Action Recorder ribbon panel, and a small red dot has appeared just above and to the right of the cursor. They indicate that the action recorder is remembering everything that you do. Begin with something simple, such as drawing a circle with a line through it.
Recording a macro.
While you are doing this, watch the Action Tree window. It records all of your command entries and responses as you work.
To stop recording, invoke the ActStop command:
- Ribbon: Tools tab | Action Recorder panel | Stop
- Menu: Tools | Action Recorder | Stop
- Right-click: Action Recorder | Stop
AutoCAD next presents the Action Macro dialog box:
Give your new macro a name.
It's a pretty simple dialog box to use, because you have direct access to only the first and fourth fields. Easier yet, the first field has a default value and the use of the fourth field is optional.
For your first attempt, simply enter a suitable action macro command name such as My_First_Macro in the first field. Note that the name cannot include spaces, and the only special or punctuation characters allowed are the hyphen (-) and the underscore (_).
Optionally, you can enter a description such as "Line plus circle" in the last field.
Click OK when you are ready. The Action Recorder closes, and the name of your macro appears in the first line of the Action Tree and in the drop-down menu.
Lights, Camera, Action!
Now it's time to run your new macro:
- Ribbon: Tools tab | Action Recorder panel | Play
- Menu: Tools | Action Recorder | Play | My_First_Macro
- Right-click: Action Recorder | Play | My_First_Macro
- Command line: My_First_Macro
Did you notice something interesting here? There is no Play Macro command, because the name of your macro has become a new AutoCAD command.
Anyway, it doesn't look very impressive when you play your new macro. All that seems to happen is that an alert box might pop up advising you that the macro has finished running. Whoopee.
Guess what this dialog box indicates and does?
Okay, run the List command (Ribbon: Home | Properties | List, or Menu: Tools | Inquiry | List) and use a window to select all the objects that you created while you were recording the macro. Surprise! There are two of everything!
Erase everything and run your macro again. It looks like everything has come back as though you had used Oops or Undo, but what has really happened is that AutoCAD has created them all over again, following the instructions that were recorded in the macro. Better yet, your macro is now available to be used in other drawings.
That's great if you want to do the exact same thing over and over again, but what if you want to draw your circle and line in a different, user-specified location each time? No problem. Simply edit the macro so that it asks the user to specify the coordinates of the center of the circle instead of using the specific values that you used when you created the macro. This task is very easy to do, but the Action Tree for your macro must be visible. If it isn't, make sure the Tools ribbon tab is active, and that the name of your macro shows in the drop-down menu of the Action Recorder ribbon panel.
Next click on the Action Recorder panel name to display the Action Tree for your macro. Without strolling outside the confines of the Action Tree, move the cursor down until it sits on the coordinates at the center of the circle. Right-click and then select Request User Input from the context menu that appears. The coordinate numbers change to an italic typeface.
Run the macro again, and one of two things will happen. You might get a dialog box that looks like this:
Input Request in a macro might produce a dialog box like this one.
If you do, click on the Provide Input option. It will lead you to the other possible situation, wherein the Command prompt is asking you for a location for the center of the circle. Interestingly, all Action Macro communications appear at the Command prompt, even if you have dynamic input turned on.
Let's examine the results. As expected, the macro has drawn the circle in the newly specified location and with the radius that you originally used when recording the macro.
The detail that might surprise you, but is logical when you think about it, is that the line was not drawn in its original location. Instead, it was drawn relative to the new location of the circle, even though you did not specify relative coordinates when you recorded the macro originally.
You'll notice that during playback of the macro I made reference to two dialog boxes that might appear. If they do and you don't want them to, then simply click on the check box in the lower-left corner of the appropriate box. Getting them to turn back on is a little convoluted, but I'll cover that step next time.
Thinking inside the Box
The Action Recorder can record most AutoCAD commands, but I have a few interesting points to note.
For starters, the Action Recorder specifically records keyboard input, and it only records mouse input when the mouse is being used to indicate points or distances. If you launch a command that uses a dialog box, then the Action Recorder records only the fact that you launched and then closed the command. Any dialog box selections or inputs are not recorded.
For example, if you launch the Hatch command, select a hatch pattern, set a hatch scale, and pick a point, then the command will complete as specified. However, when you run the macro it will simply launch the Hatch command and then sit there waiting for you to run the dialog box manually. When you click OK, the macro will resume running with the new Hatch dialog box values.
The workaround in this case is to use the command-line version of the command while recording the macro. In most cases, this version consists of the command name prefixed by a hyphen, such as -Hatch.
Another handy solution is to use certain direct-entry equivalent commands. For example, the Layer command brings up the Layer Properties Manager palette, but the command sequence Clayer Hatch will directly push the current layer to change to the Hatch layer, if it exists. More on this development next time.
This column has been a good start at introducing the Action Recorder. Be sure to come back next time, when I will dig a little deeper into its mysteries.
By the way, the Foolish Antiques Rallye Team was running 10th out of 47 in the rally when a snow bank jumped out in front of us. Even so, we managed to end up 33rd.
The Foolish Antiques Rallye Team competes in the 2009 Thunderbird rally.
And Now for Something Completely Different …
When you visit Hawaii Volcanoes National Park, be sure to bring a flashlight. A section of the Thurston lava tube is lighted for tourists, but a much larger section is open to the public but unlit.
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!