3. Advanced Toolbar Macro Programming

by Ralph Grabowski

In the last lesson, we saw how quick'n easy it is to change the meaning of a toolbar button in AutoCAD for Windows, Release 11, 12, and LT. A single right-click on the button brings up a dialogue box that lets you change the icon (the miniature picture that labels the button) and macro, the AutoCAD action carried out when you left-click on the button. In this lesson, we look at some advanced macro programming tricks.

This tutorial is based on the toolbars found in Release 11, Release 12, and the first two releases of LT.
Release 13's toolbar is significantly different.

The INI File

AutoCAD stores these toolbar macros in the AcLt.Ini initialization file. You'll find the file in the same subdirectory as the AutoCAD executable (Acad.Exe or AcLt.Exe). The INI files are commonly used by Windows applications to store program settings between sessions; even Windows itself uses several INI files for its own purposes. Since INI files are in ASCII format, you can use the Notepad text editor to look at it.

(Right-click image to download.) The figure shows the first part of AcLt.Ini from AutoCAD LT. Every INI file looks slightly different, depending on the settings in effect. INI files are often divided into sections. Section titles are in square brackets, such as [AutoCAD LT General]. It is here that we find the toolbar definitions, for example:

ToolBar1=\3\3_TOOLBOX ^88^

Let's pick this mishmash apart, bit by bit, to see what it means. When AutoCAD loads, it reads the INI file. Whatever is behind the equals sign in Toolbar1= is stuck onto the first toolbar button. Toolbar1= means this is the first toolbar button. The second toolbar button is defined by Toolbar2= and so on.

From last month, we know that \3\3 are two Cancels in row like pressing [Ctrl]+C twice. That ensures any command in progress is cancelled before executing the macro. The _TOOLBOX is the command that toggles the display of the toolbox. Recall that the underscore ( _ ) internationalizes any English-language AutoCAD command. The command can be typed in UPPERCASE or lowercase -- it doesn't matter.

There is a space after the command, which AutoCAD automatically appends to the macro: this acts like pressing the [Enter] key at the end of a command.

At first glance, the suffix ^88^ seems fairly mysterious. It turns out to be a rather awkward method of specifying the icon displayed by the toolbar button. Number 88 happens to be the 88th bitmap stored by AutoCAD LT . The carets (^ ^) tell AutoCAD that the 88 refers to the icon and is not part of the macro.

How do you know which icon number correspond with which icon number? Not very easily, I'm afraid. Recall that the Toolbar Button Customization dialogue box presented a list of 91 icon images. Those images are listed in alphabetical order and, by no coincidence, in numerical order. The icon of the Toolbox is the 88th icon in the list. Thus, if you want to a specific icon image, you'll need to count along to find out its number.

Let's look at a couple more toolbar definitions:

ToolBar9='_DDLMODES ^56^

Here, button #9 starts with an apostrophe ( ' ), which makes the DdLModes command transparent. By clicking on this button during another command, you display the Layer Control dialogue box. The icon image is the 56th one.

ToolBar13=

This button is undefined since there is nothing behind the equal ( = ) sign. When you click on the 13th button, nothing happens. As well, the button itself is blank.

Dialogue Box or Text Editor?

We have now looked at toolbar macros from two approaches. Last month, we saw how to write macros with the Toolbar Customization dialogue box within AutoCAD. This month, we looked at changing macros via the Acad.Ini (or AcLt.Ini) file with the Notepad text editor.

Which approach is better? The dialogue box is best when creating one or two macros on-the-fly. Accessing the INI file directly is best when customizing all 26 toolbar buttons at once or with a program external to AutoCAD.

Next, we look at a combination of the two approaches: we modify the INI file directly from within AutoCAD LT.

Environment Variables

LT has one advantage over full-strength AutoCAD with its unique SetEnv command. Even Release 13 lacks the command. SetEnv lets you directly manipulate the values in the INI file. The LT Release 1 documentation only mentions SetEnv in passing; the command remains underdocumented in LT Release 2.

Let's jump right in and try it out. When you type SetEnv, LT prompts you for the variable name:

        Command: setenv
        Variable name: toolbarsize

ToolBarSize is the setting in AcLt.Ini that determines the size of the toolbar icons. It's not well known, but you can change the size of the icons displayed by AutoCAD R11, R12, and LT. After you enter the name of the variable, such as toolbarsize, LT prompts you for the new value:

        Value <16>: 24

If the variable exists, LT returns the value in angle brackets, such as <16>. By default, the value of ToolBarSize is 16, measured in pixels. You can change the number down to 6 to display more buttons but very tiny icons. Or you can increase the value to 32, which display very large but fewer buttons.

Type the new value and press [Enter]. AutoCAD records the new value in its INI file. Note that the new icon size do not come into effect until AutoCAD starts again.

Another trick is to directly customize buttons on the toolbar. The process works like this:

        Command: setenv
        Variable name: toolbar13
        Value: ^C^C_bmake^9^

Here we have assigned the BMake command (short for block make; displays the Block Definition dialogue box) to toolbar button #13 and uses icon #9, the Block icon. In the AcLt.Ini file, this appears as:

        ToolBar13=^C^C_bmake^9^

When you want to change the macro, SetEnv helpfully displays the current setting, as follows:

        Command: setenv
        Variable name: toolbar13
        Value <^C^C_bmake^9^>: ^C^C_block^9^

Despite the SetEnv command's unique availability in LT, I have found it has numerous drawbacks, which Autodesk failed to address in LT Release 2:

Multiple Command Toolbar Macros

We have seen three approaches to customizing toolbar macros. Admittedly, this month's two approaches are rather esoteric but useful in certain situations. Let's move on now to writing a complex macro. Up until now, we have worked with single-command macros.

A toolbar macro can have any number of commands, within two limitations:

  1. The macro can consist of a maximum of 79 characters in AutoCAD R11 and R12,
    or 255 characters in AutoCAD LT Release 1 and 2; and
  2. The macro cannot pause for user input, neither a screen pick nor for value input from the keyboard.

With these two limitations in mind, here is a three-command macro that I wrote when I needed to plot a large number of drawings. A client had provided me with several hundred small drawings that she needed plotted on a laser printer. I found that loading each drawing via the File | Open command quickly became tedious.

Then I remembered that AutoCAD for Windows supports drag'n drop. That means I could click on the drawing filename in the Windows File Manager, then drag the filename into the AutoCAD window.

The drag'n drop approach doesn't actually open the file; instead, AutoCAD inserts the file as a block. Since I only needed AutoCAD for plotting, I didn't need to explode the block.

However, the Insert command itself imposes an overhead, since it wants to know the insertion point, x- and y-scales, and rotation angle. That too became tedious. At that point, I wrote the following toolbar macro:

        0,0 1 1 0 zoom e plot

This macro automates the following steps:

  1. 0,0 inserts the block at the origin (0,0). Notice that the macro can start in the middle of a command.
  2. 1 1 0 specifies the x- and y-scales of 1.0 and a rotation angle of 0 degrees. Since these are the default values, I could have written the macro with spaces, but "0,0 " makes macro harder to read and debug.
  3. zoom e performs a zoom extents so that I could see the entire drawing.
  4. plot starts the Plot command.

A single mouse click executes all three commands (representing 22 keystrokes or six menu picks) faster than you could ever hope to type or select them from the menus.

I would have liked to have continued the macro to automate the rest of the plot but AutoCAD cannot do that in dialogue boxes. I could have reverted to the command-line version of the Plot command, but I needed to do a windowed plot by picking two points on the screen, which it can't do.

Here is another macro that places a D-size (24"x36") drawing border 0.1 units wide using the PLine command:

\3\3_pline 0,0 width 0.1 0.1 36,0 36,24 0,24 close _zoom e

If you run up against the 79- (or 255) character limits, you can conserve characters with aliases. By employing aliases, the above macro is shortened by 15 characters to:

\3\3 _L 0,0 w 0.1 0.1 36,0 36,24 0,24 c _z e

This ends our look at AutoCAD's easiest (and weakest) customization feature. While toolbar macros are very easy to create, they severely limit your ability to control AutoCAD. In the next lesson, we look at more powerful ways to customize AutoCAD at the cost of being more difficult to create -- the AutoLISP programming language. 


Tailoring AutoCAD Part 2 | Part 3 | Part 4 is the next tutorial.

Comments on this tutorial series? Tell me about it.

Return to Contents.

All contents copyright 1995-9 XYZ Publishing, Ltd. . All rights are reserved. No material may be reproduced electronically or in print without written permission from XYZ Publishing, PO Box 3053, Sumas WA, 98295-3053, unless otherwise noted.