Learning to LISP1 Feb, 1997 By: Bill Fane
Captain LearnCurve finished rounding the last marker buoy and dug in for the final cut through the exit gate. Ho hum, another clean pass at 36 mph with 15 off. He signaled to the boat driver to cut the power.
Just then his acute hearing picked up a plaintive cry:
The boss showed me the perfect AutoLISP routine that he found in CADALYST's Hot Tip Harry column. It will save me hours of work every week. There are only two problems: I don't have a clue what AutoLISP is, and even less of a clue what to do with the routine. Oh, woe is me! Whatever shall I do?
AutoLISP is a programming language built in AutoCAD.
Oh, great! Now you expect me to be a programmer geek!
What's wrong with that? It seems to have worked out all right for Mr. Gates. The good news is, you don't have to be a programmer to take advantage of AutoLISP. As you already noted, a lot of programs are already available if you only knew what to do with them.
Let's start with a small demonstration of what AutoLISP is and what you can do with it. At the command prompt, type
(+ 4 6) (including the parentheses) and press <Enter>. AutoCAD displays the number 10 on the command line.
Big deal! I knew that 4 + 6 = 10, or at least my pocket calculator knew it.
I'll tell you later why this is such a big deal, but in the meantime we can make two primary observations about AutoLISP:
(Everything is enclosed in parentheses.)
In a weird order things written are.
AutoLISP allows nested functions such as
(setq A (+ 2.5 (* (3.0 (/ pi)))), giving rise to the vernacular translation of the acronym LISP as Lost In Stupid Parentheses. In fact, it stands for list processing, because it is particularly good at processing lists of information. By a strange coincidence, AutoCAD drawing information consists of lists of data about each object.
The second observation isn't really true if you think of the operation as "add four and six to get ten" rather than "four plus six equals ten." You should be used to working in this type of sequence because AutoCAD works the same way--first you tell it you want a line and then you tell it where to start and end the line.
Now, back to the pocket calculator. AutoLISP contains all the standard arithmetic operations such as add, subtract, multiply, and divide.
Big deal! My calculator does logs, trig, exponents, and more.
So does AutoLISP, but does your calculator talk directly to AutoCAD?
What do you mean?
Try this. Suppose you want to draw a circle with a circumference of six units. You can figure it out on your calculator and key in the answer when the Circle command asks for the radius, but you can also use AutoLISP.
Start the Circle command. Pick a center point, and when AutoCAD asks for a radius, type in
(/ 6 (* 2 pi))
and press <Enter>. Presto! AutoCAD draws a circle with radius of 0.95493 units. Hence the next lesson: AutoCAD lets you respond with an AutoLISP function or expression any time it asks for input.
Cute, but my pocket calculator has four memories.
Now it's my turn to say, "Big deal." AutoLISP has hundreds of memories. Type in
(setq RAD (/ 6 (* 2 pi)))
and press <Enter>. AutoCAD displays 0.95493 as it did before. Now type in
!RAD and press <Enter>. Once again AutoCAD displays 0.95493. With this simple operation you created a memory location named RAD and stored the value of 0.95493 in it for later recall.
Vocabulary. In programmer terminology you have created a variable. It is called a variable because you can vary its contents. Whenever you need the value again you simply recall the variable by name, and its current contents are returned to you.
Wow, that's like totally rad, man!
AutoLISP variables can have descriptive names such as POINT3, SIZE, LAST_POINT, etc. AutoLISP is not case-sensitive, so RAD, Rad, and rad are the same.
You can have as many memories as you need, simply by giving them suitable names. Variable names can use letters, numbers, and most punctuation marks, with the following limitations.
They cannot consist of just numbers. The variable name 34 is not legal, but 3A is.
They cannot include
left and right parentheses
Believe it or not, this is a legal variable name:
Remember when I said AutoCAD lets you respond with an AutoLISP expression at any time? Okay, invoke the Circle command again and pick a center. When AutoCAD asks for a radius type in
!RAD and press <Enter>. Presto, another circle with a radius of 0.95493 units!
To recall the value assigned to a variable at the Command prompt or within an AutoCAD command, put an exclamation mark in front of its name.
Along with arithmetic operations and internal functions to calculate logs, trigs, exponents, etc., AutoLISP makes logical comparisons such as less than, equal to, greater than, and other equality and conditional evaluations. It performs logical branching by using commands such as if-then based on logical operations. You can also set it up to stop and ask for your input as needed.
It lets you create your own AutoLISP functions and AutoCAD commands, and even lets you redefine existing AutoCAD commands. The bad news is that if you type all this in at the Command prompt and then exit AutoCAD, or even simply open another drawing or start a new one, you lose all your variable names and data and all your new functions.
Oh, bummer! You mean I have to type everything in again when I work on a different drawing?
Don't be silly. There are two ways of saving AutoLISP functions. The first way involves customizing AutoCAD's menus. You don't have to live with AutoCAD's standard menus. AutoCAD's menu structure is held in an external text file and can contain anything that you type in.
Try this example. In DOS, use AutoCAD's Shell command to get the DOS prompt and start Edit. If using Windows, start Notepad. Create a simple text file called test.mnu and type these five lines:
[CIRCUMF.]^C^C^Pcircle \(/ (getdist "circumference?") (* 2 pi))
Return to AutoCAD (in DOS, enter the Exit command) and invoke the Menu command. You may need to use the Browse button in the dialog box to find your
test.mnu file. When you find it, you'll end up with a single menu bar item called Circle. When you pick it, the pull-down menu that appears has only a single entry that displays the prompt Circumf. When you pick it, it:
issues a pair of <Ctrl>-<C>s that cancels anything in progress;
issues a <Ctrl>-<P> to suppress screen echoing of the macro as it runs;
the backslash makes the menu item pause and waits for you to pick a center point;
invokes your AutoLISP routine that asks for a circumference, calculates the radius, and returns it to the Circle command, which draws the circle.
Standard AutoCAD menus contain a lot of AutoLISP, some of which is built in and some of which is held in a separate file.
You can save AutoLISP routines in a separate ASCII text file created with your text editor and then load these programs into AutoCAD as needed.
Commands. Another powerful AutoLISP use is creating new AutoCAD commands.
Use your text editor again to create a simple text file like this:
(setq PT (getpoint "Center point? "))
(/ (getdist "Circumference? ")(* 2 pi))
(command "circle" PT CIRC)
Save it as
TEST.LSP in AutoCAD's current directory. When you return to the Command prompt, type in
(load "test") and press <Enter>.
This loads your AutoLISP program files into AutoCAD. Note that (load "...") is an AutoLISP function itself and must be enclosed in parentheses.
When you type
Circ and press <Enter> at the Command prompt, you are asked for a center point and a circumference, and a suitable circle is drawn. That's magic! You created a new AutoCAD command called Circ. Try making a few circles, then use the List command to verify that the circles have the correct circumferences.
You have the power
You are now ready to tap the power of AutoLISP programming without even being a programmer. Let's go back to the beginning . . .
Where Captain LearnCurve was waterskiing?
No, right after that, where you were wailing because your boss just handed you a Hot Tip from CADALYST.
By now you should be able to use AutoLISP routines that you find--simply type them (or import computer files) into a text file, load them, and run them.
So how do you run an AutoLISP routine? Easy. Look through the program listing for anything that says (
defun c:<name>(). The <name> following the c: is a new AutoCAD command name.
Some programs don't contain the (defun c: code. Look for
(defun <name> without the c:. Run this from the Command prompt by typing in
Well-written code has a remarks section at the beginning of the program that tells you what it does and how to run it.
So where should you store all your AutoLISP routines? That depends. If you put them in the right place, then AutoCAD automatically finds them. Assuming you run a standard AutoCAD installation, the right directory for recent releases is:
Release 12 DOS in C:\ ACAD12\SUPPORT;
Release 12 Windows in C:\ACADWIN\SUPPORT;
Release 13 in R13\COM\ SUPPORT;
AutoCAD LT: Don't even try. It doesn't support AutoLISP.
If you have more than a few routines, you might lose them among the standard routines that come with AutoCAD. If so, create a suitable directory, such as C:\ R13\COM\CUSTOM, and then tell AutoCAD where to find it.
DOS versions: Edit the batch file that starts AutoCAD. Find the line that says
SET ACAD=<path> and add your directory name to the existing path.
Windows versions: From within AutoCAD run the Preferences command. From the dialog box, pick Environment, then Support, and add your directory to the path. In Release 12 remember to save your changes.
AutoCAD also reduces typing. When you start or edit a drawing, AutoCAD looks for the
acad.lsp file and loads it. You can thus create your own commands and always have them available, just like all AutoCAD commands.
So, what do you have to say?
Wanna buy a calculator?
And now for something completely different
Styrofoam is hydroscopic. When the insulating lid on your hot tub gets so heavy that it takes three people to open it, you don't have to replace the entire lid--just replace the styrofoam.
The lids are made of a vinyl cover over a polyethylene-wrapped piece of generic house-insulation foam. Be sure to seal the polyethylene with tape to help keep the water out.
Who has the code?
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!
Life of Pi shows why Robots will one day have emotions 20 May, 2013
Webcast Replay: Flexible Design with PTC Creo 20 May, 2013
Discover and Fix Your Vibration Vulnerability with SolidWorks Simulation 17 May, 2013