Learning to LISP
Learning what to do with Hot Tip Harry code. by Bill Fane
AutoLISP--the beginning
AutoLISP is a programming language built in AutoCAD.
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 the spaces) and press <Enter>. AutoCAD displays the number 10 on the command line.
We can make two primary observations about AutoLISP:
Rad variables
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.
AutoLISP contains all the standard arithmetic operations such as add, subtract, multiply, and divide as well as logs, trig, exponents, and more.
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.
AutoLISP can store hundreds of variables in memory. 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.
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
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.
AutoLISP power
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.
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:
***SCREEN
[ ]
***POP1
[CIRCLE]
[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:
Standard AutoCAD menus contain a lot of AutoLISP, some of which is built in and some of which is held in a separate file.
Now what happened to me in A2002
1. I saved test.mnu by the expedient of copy pasting straight from the Browser screen into NotePad and saving into my 2002/Support_MyCustom folder. I now put all my customisations in a separately created folder rather than losing them in the clutter of the Support folder to simplify walking away from the computer or upgrading software and taking my customisations with me.
2. Entering Menu at the A2002 commad line bought up the Select Menu Dialog box which displayed the folders and files with .mns or .mnc extensions in the Support folder.
Use the drop down in the File Type: box and select Menu Templates (.mnu). Now only the .mnu files show.
3. When I browsed for my Support_Custom folder and selected the test.mnu file a message box warned that the menu source file (.mns file) would be overwritten resulting in the loss of any toolbar customisations.
4. I loaded the test.mnu and all the existing toolbars and menus disappeared leaving the single new Circle menu with it's Circumfernce drop-down (or 'pop-up' is the correct term) as described by the author, Jim Patrick. Also appearing in the Support_MyCustom folder alongside test.mnu was a test.mns and a test.mnc.
5. Selecting the Circle->Circumference sadly just turned my OTRACK button on and off. I kid you not. With every attempt, the OTRACK button toggled between it's on and off position. You can get the effect yourself on-screen by toggling the F11 function key!
6. I close Autocad with this display and restarted it. No buttons groups or meunus came up except File and Edit etc associated with Windows. Not even the single Circle-Circumference menu, nothing, nada. Scarey.
7. Entering Menu again and loading test.mnu brought the single Circle-Circumference menu back and a repeat of the OTRACK button being toggled.
8. Entering Menu again and loading acad.mnu (should I have loaded it or acad.mns??) in the actual support folder caused my full display to come back on with all the various 3D toolbar drawing and editing toolgroups in their various postions as placed by myself except, sadly some little button I made in the days before I started keeping all my customisations in a separate folder was gone!
9. I close Autocad with this full display and restarted it and up comes the full display again.
10. Entering Menu again and loading test.mnu took me back to the single Circle-Circumference menu except then when using it the actual Select Menu Dialog box kept coming up although at times this stopped happening and doing Circle-Circumference appeared to do nothing (OTRACK did not toggle).
11.Put a space between the first backslash (pause for circle center to be picked and the following opening bracket. Finally caused prompt for circle center to appear but instead of getting a Circumference? prompt it just asked for the routine radius.
11. Put a space after the ? before the " at the end of "Circumference? " because the folowing lisp example had one and it worked fine. Still did not get a Circumference? prompt.
12. Gave up.
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:
(defun c:CIRC()
(setq PT (getpoint "Center point? "))
(setq CIRC
(/ (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.
Lisp worked fine! (unlike the Circle menu!).
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 . . .
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 <name>)
.
Well-written code has a remarks section at the beginning of the program that tells you what it does and how to run it.
Storage
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:
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.