You can write some simple AutoLISP programs of your own that create what are called keyboard macros, Macros-like script files-are strings of predefined keyboard entries. They are invaluable for shortcuts to commands and options you use frequently.For example, you might find that, while editing a particular drawing, you often use the Break command to break an object at a single point. Here’s a way you should turn this operation into a macro.
- Open the Unit file and, at the command prompt, enter the following text. Be sure you enter the line exactly as shown here. If you make a mistake while entering this.line, you can use the I-beam cursor or arrow keys to go to the location of your error to it.
- Next, enter breakat at the command prompt. The Break command starts and you are prompted to select an object.
- Click the wall on the right side of the unit.
- At the Enter First Point: prompt, click a point on the wall where you want to create a break.
- To see the result of the break, click the wall again. You will see that it has been split into two lines, as shown in Figure 19.15.
You’ve just written and your first AutoLISP macro! Let’s take a closer look at this very simple program (see Figure 19.16), It starts out with an opening parenthesis, as do all AutoLISP programs, followed by the word defun. Defun is an AutoLISP function that lets you create commands; it is followed by the name you want to give the command (Breakat, in this case). The command name is preceded by C:, telling defun to make this command accessible from the command prompt. If the C: we] omitted, you would have to start Breakat using parentheses, as in (Breakat).
After the command name is a set of open and closing parentheses. This encloses what is called the argument list. The details aren’t important; just be aware that these parentheses must follow the command name.
Finally, a list of words follows, enclosed by another set of parentheses. This list starts with the word conmand. Conmand is an AutoLISP function that tells AutoLISP that whatever follows should be entered just like regular keyboard input. Only one item in the Breakat macro-the word pause-is not part of the keyboard input series. Pause is an AutoLISP function that tells AutoLISP to pause for input. In this particular macro, AutoLISP pauses to let you pick an object to break.
Notice that most of the items in the macro are enclosed in quotation marks. Literal keyboard input must be enclosed in quotation marks in this way. The pause function, on the other hand, does not require quotation marks because it is a proper function, one that AutoUSP can recognize.
Finally, the program closes with two closing parentheses. All parentheses in an AutoLISP program must be in balanced pairs, so these two parentheses close the opening parenthesis at the start of the command function as well as the opening parenthesis back at-the beginning of the defun function.
Storing AutoLISP Macros as Files
When you create a program at the command prompt, such as you did-with the Breakat macro, AutoCAD remembers it only until you exit the current file. Unless you want to recreate this macro the next time you use AutoCAD, you can save it by copying it into an ASCII text file with a .lsp extension, as shown in the following . example, where the Breakat macro is saved, along with some other macros I use often.
Figure 19.17 shows the contents of a file named Keycad.1sp. This file contains the macro you used previously, along with several others. The other macros are commands that include optional responses. For example, the third item, defun c: corner, would cause AutoCAD to start the Fillet command, enter an R to issue the Radius option, and finally enter a 0 for the fillet radius. Table 19.1 shows the command abbreviations and what they do.
Use the Windows Notepad application and copy the listing in Figure 19.17.Give this file the name Keycad. 1sp, and be sure you save it as an ASCll file. Then, whenever you want to these macros, you don’t have to load each one individually. Instead, you load the Keycad. 1sp file the first time you want to use one of the macros, and they’re all available for the rest of the session.
Once it is loaded, you can use any of the macros contained within it lust by entering the macro name. For example, entering pts.J will set the point style to the shape of an X.
Macros loaded in this manner will be available to you until you exit AutoCAD. Of course, you can have these macros loaded automatically every time you start Auto- CAD by including the Keycad. 1sp file in the Startup Suite of the Load/Unload Applications dialog box. That way, you don’t have to remember to load it in order to use the macros. .
Now that you have some firsthand experience with AutoLISP, I hope these examples will encourage you to try learning more about this powerful tool. If you would like to learn more about AutoLISP, the book TheABCs of AutoLISP is included on the companion CD-ROM. This 400-page book, converted into an electronic document, is a completed resource for AutoLISP, including tutorials and example programs.