Now updated for AutoCAD 2000!

4. Introduction to AutoLISP Programming

by Ralph Grabowski

While a toolbar macro is very easy to create, it severely limits your ability to control AutoCAD. In this tutorial, we look at the most powerful way for a non-programmer to customize AutoCAD -- at the cost of being more difficult to create than a toolbar macro.

The AutoLISP Programming Language

For 15 years now, AutoCAD has had a full-ledged programming language embedded in itself: AutoLISP. At no extra cost to you, AutoLISP lets you create programs that manipulate nearly every aspect of AutoCAD and the drawing. AutoLISP lets you do anything from adding two numbers together -- in the middle of a command -- to parametrically drawing a staircase in 3D, to generating a new user interface for AutoCAD. AutoLISP has the ability to manipulate data in the drawing database. In fact, most of the new commands (such as ddModify, 3dArray, and XRefClip) added to AutoCAD in Release 12 were actually AutoLISP programs. If you still work with that version, take a look in the \Support subdirectory of Release 12 or 13: those 26 command names ending with LSP are commands that use AutoLISP, rather than code within AutoCAD itself.

(AutoCAD 2000 relies more on another programming interface, called ObjectARX, to provide new commands to AutoCAD. Still, a search of the AutoCAD 2000 folders finds 121 LISP files!)

The most important aspect of AutoLISP, in my opinion, is that you can toss off a few lines of code to help automate your work. In the next couple of tutorials, I'll show you how to write simple AutoLISP code that makes your AutoCAD drafting day easier. Later, I show you how to integrate AutoLISP code into Toolbar macros and script files.

In contrast, AutoCAD's most powerful programming interface -- known as ObjectARX (AutoCAD Runtime eXtension) -- are merely interfaces: you have to buy additional the programming tools (read: $$$) and have an in-depth knowledge of advanced programming methodology.

The primary advantage to using ObjectARx is speed: these programs run compute-intensive code as much as 100 times faster than AutoLISP. For this reason, Autodesk wrote most of the new commands in ObjectARX. Look for filenames ending in ARX as examples of ObjectARX program and commands.

(AutoCAD LT users are out of luck: AutoLISP is not available to you. Even though Autodesk had AutoLISP in the beta version of LT Release 1, the code was yanked in the last days before the software began shipping. When you try to use AutoLISP, LT responds: "AutoLISP command not available.")

Simple AutoLISP: Adding Two Numbers

With that bit of background, let's dive right into using AutoLISP. We'll start with something easy, something everyone knows about: add together two numbers, like 9 plus 7.

1. Start AutoCAD, any version from v2.18 through today's most recent version. (LT users can read along, just in case AutoLISP ever shows up again!) There is no need to open a drawing.

2. When the Command: prompt appears, type the following on the keyboard:

Command: (+ 9 7) [press Enter]
16
Command:

AutoCAD replies with the answer, 16. However, getting to that answer via (+ 9 7) may seem convoluted to you. That's because AutoLISP uses "prefix notation": the operator, +, appears before the operands, 9 and 7. Think of it in terms of "add 9 and 7." This is similar to how AutoCAD itself works: type in the command name first (such as Circle), then enter the coordinates of the circle.

3. Parentheses surround every AutoLISP statement. Every opening parenthesis, (, requires a closing parenthesis, ). I'll tell you right now that balancing parentheses is the most frustrating aspect to using AutoLISP. Here's what happens when you leave out the closing parentheses:

Command: (+  9  7 [press Enter]
1>

(Note AutoCad 2002 gives the response:)

(_>

AutoCAD displays the '1>' or '(_>)' prompt to tell you that one closing parenthesis is missing.

4. Type the missing ) and AutoCAD is satisfied:

1> )
16
Command:


*AJ
at this point try putting in:

Command: (+  9  7 )) [press Enter]
1>

AutoCAD 2002 displays   ; error: extra right paren on input   to tell you that there is an extra closing parenthesis (bracket).


5. The parentheses serve a second purpose by alerting AutoCAD that you are using AutoLISP. When you attempt to enter the same AutoLISP function + 7 9 without an opening parentheses, AutoCAD reacts unfavorably to each space character you type. For example:

Command: + [press Spacebar]
Unknown command "+". Type ? for list of commands.
Command:

(Note AutoCad 2002 responds with:)

Command: + [press Spacebar]
+ Unknown command "+". Press F1 for help.
Command:

6. As you probably suspect, AutoLISP provides all the basic arithmetic functions: addition, subtraction, multiplication, and division. Let's try each of the other three, first subtraction:

Command: (-  9  7)
2
Command:

7. Multiplication is done using the familiar * (asterisk) symbol, as follows:

Command: (*  9  7)
63
Command:

8. Finally, division is performed with the / (slash) symbol:

Command: (/  9  7)
1
Command:

Oops, that's not correct! Dividing 9 by 7 is 1.28571, not 1. What happened? Up until now, you have been working with "integer" numbers (also known as "whole" numbers). For that reason, AutoLISP has been returning the result as an integer number, although this was not apparent until you performed a division.

9. To work with real numbers, add a decimal suffix. This converts integers to real numbers and forces AutoLISP to perform real-number division, as follows:

Command: (/  9.0  7.0)
1.28571
Command:

And AutoLISP returns the answer correct to five decimal places.

10. Finally, let's see how AutoLISP lets you nest calculations:

Command: (+ (- (* (/ 9.0 7.0) 4) 3) 2)
4.14286
Command:

Note how the parentheses aid in separating the nesting of the terms.



Practical Example: AutoLISP in Commands

Okay, so we've learned how AutoCAD works as a $3,000, four-function calculator. This overpriced calculator pays its way when you employ AutoLISP to perform calculations within a command. For example, you need to draw a linear array of seven circles to fit in a 9" space.

 1. Start the Circle command, as follows:

Command: circle
3P/2P/TTR/<Center point>: [pick a point]
Diameter/<Radius>: d

2. Instead of typing the value for the diameter, enter the AutoLISP equation, as follows:

Diameter: (/  9.0  7)
Command:

3. AutoCAD draws a circle with a diameter of 1.28571 inches. You can use an appropriate AutoLISP function anytime AutoCAD expects user input.

4. Now go on to the Array command and draw the other six circles, as follows:

Command: array
Select objects: L
1 found Select objects: [Enter]
Rectangular or Polar array (R/P): [Enter]
Number of rows (---) <1>: [Enter]
Number of columns (|||) <1>: 7
Distance between columns (|||): (/  9.0  7.0)

Once again, you use AutoLISP to specify the array spacing, which happens to equal the circle diameter.



Remembering the Result: SetQ

In the above example, you used the (/ 9.0 7) equation twice: once in the Circle command and again in the Array command. Just like the M-key on a calculator lets it remember the result of your calculation, AutoLISP can be made to remember the results of all your calculations.

1. To do this, you employ the most common AutoLISP function known as SetQ. This curiously named function is short for "SET eQual to." To save the result of a calculation, you use the SetQ function together with a variable, as follows:

Command: (setq x (/ 9.0 7))
1.28571
Command:

From algebra class, you probably recall equations like 'x = 7 + 9' and 'x = 7 / 9'. The 'x' is known as a "variable" because it can have any value. In this case, x remembers the result of the (/ 9.0 7.0) calculation. Notice the extra set of parentheses.

 2. To prove to yourself that x contains the value of 1.28571, use AutoCAD's ! (exclamation) prefix, as follows:

Command: !x

1.28571
Command:

The ! prefix is useful for reminding yourself of the value contained by a variable, in case you've forgotten or are wondering what happened during the calculation.

3. AutoLISP isn't limited to just one variable. You can make up any combination of characters to create variable names, such as Pt1, Diameter, and Yvalue. The only limitation is that you cannot use AutoLISP function names, such as SetQ, T, and GetInt. In fact, it is good to create variable names that reflect the contents, such as the circle diameter we calculated above. But you also want to balance a descriptive name, such as 'Diameter' with minimized typing, such as 'x'. A good compromise for our example is 'Dia'. You make one variable equal another, as follows:

Command: (setq  dia  x)
1.28571
Command: !dia
1.28571
Command:

4. Let's redo the Circle and Array commands, this time using variable Dia, as follows:

Command: circle
3P/2P/TTR/<Center point>: [pick point]
Diameter: d
Diameter: !dia
Command:  array
Select objects: L
1 found Select objects: [Enter]
Rectangular or Polar array (R/P): [Enter]
Number of rows (---) <1>: [Enter]
Number of columns (|||) <1>: 7
Distance between columns (|||): !dia

AutoCAD precisely draws the same seven circles.

Summing Up

In this lesson, we began to learn how to employ AutoLISP to make our AutoCAD drafting work easier. In the next lesson, we get a taste of the many different kinds of functions AutoLISP offers you. 


Tailoring AutoCAD Part 5 is the next tutorial.

Comments on this tutorial series? Tell me about it.

Return to home page.

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 XYZPublishing, PO Box 3053, Sumas WA, 98295-3053, unless otherwise noted.