9. Writing a Simple AutoLISP Program, Part 3

by Ralph Grabowski

In the last lesson, we learned the first steps in writing an AutoLISP routine of our own. We created a two-line AutoLISP program that looks like this:

(setq xyz (getpoint "Pick point: "))
(command "text" xyz 200 0 xyz)

When you pick a point on the screen, the program labels the point with its x,y,z -coordinates. A quick reminder on how to use it:

1. Type the AutoLISP code in at the Command: prompt exactly as shown. All those spaces, parentheses, and quote marks are important; upper and lower case characters are not important.

2. When AutoCAD prompts, "Pick point: ", pick a point on the drawing:

Pick point: [pick]

3. Notice that AutoCAD writes the x,y,z-coordinates at the point you picked. You may need to zoom window or extents to see the text.

Sprucing Up the Code

There we have it: a full-fledged AutoLISP program. Well, not quite. What we have is the algorithm -- the core of every computer program that performs the actual work. What is lacking is most of the user interface -- the part that makes it easier for the user to employ the program. All we have for a user interface is part of the first line that prompts, "Pick point: ". There's a lot of user interface problems with our little program. How many can you think of? Here's a list of problems I've come up with:

  1. It's a pain to retype those two lines each time you want to label a point -- we need to give the program a name ...
  2. ... and we need to save it on disk so that we don't need to retype the code with each new AutoCAD session...
  3. ... and if we use this AutoLISP program a lot, then we should have a way of having it load automatically.
  4. The x,y,z -coordinates are printed to eight decimal places, which, for most users, that's way too many.
  5. You may want to control the layer that the text is placed on.
  6. You may want a specific text style.
  7. Certainly, you would like some control over the size and orientation of the text.
  8. Here's an orthogonal idea: store the x,y,z coordinates to a file on disk -- just in case you ever want to reuse the data.

Conquering Feature Bloat

"Okay," you may be thinking, "I can agree that these are all desirable improvements. Go right ahead, Mr. Grabowski, and show how to add them in." But, wait a minute! When you're not familiar with AutoLISP, you may not realize how a user interface adds a tremendous amount of code, which mean more bugs and more debugging. If you are familiar with programming, then you know how quickly a simple program fills up with feature-bloat.

While all those added features sound desirable, they may make the program less desirable. Can you imagine how irritated you'd get if you had to answer the questions about decimal places, text font, text size, text orientation, layer name, filename -- each time you wanted to label a single point?

Take a second look at the wishlist to check off what is important and cross off what you could live without. I would argue the first three items are important and we'll see how to implement them in this lesson. (In the next lesson, we'll deal with the other five items.)

Wishlist Item #1: Naming the Program

To give the program a name, I surround the code with the (defun)function, as follows:

(defun c:label ( / xyz)
  (setq xyz (getpoint "Pick point: "))
  (command "text" xyz 200 0 xyz))
)

Let's take a look at what I've added, piece by piece:

To run the Label program, all I need do is type "label" at the Command: prompt, as follows:

Command: label *tt
Pick point: [pick]

Command: label *mono
Pick point: [pick]

Command: label *courier
Pick point: [pick]

When the c: prefix is missing, then I run the program like an AutoLISP function with the parentheses, as follows:

Command: (label) *mono overwritten by tt inline
Pick point: [pick]

The ( ) empty parentheses declare the names of inputand local variables; the slash separates the two:

The benefit to declaring variables as local is that AutoCAD automatically frees up the memory used by the variable when the AutoLISP program ends; the drawback is that the value is lost, making debugging harder. For this reason, otherwise-local variables are kept global until the program is debugged.

The  ) closing parenthesis balances the opening parenthesis at the beginning of the program. I indented the code in the middle to make it stand out from the defun line and the closing parenthesis. This is standard among programmers; the indents make it easier to read code. You can use a pair of spaces or the tab key since AutoLISP doesn't care.

Wishlist Item #2: Saving the Program

By saving the program to a file on disk, I avoid retyping the code with each new AutoCAD session. I do this, as follows:

1. Start a text editor (the NotePad supplied with Windows is good).

2. Type the code shown:

(defun c:label ( / xyz)
    (setq xyz (getpoint "Pick point: "))
    (command "text" xyz 200 0 xyz)
 )

3. Save the file with the name Label.Lsp in one of AutoCAD's subdirectories named Support.

Wishlist Item #3: Automatically Loading the Program

To load the program into AutoCAD, I type the following:

Command: (load "label")

If AutoCAD cannot find the AutoLISP program, then I have to specify the subdirectory path. Assuming I saved Label.Lsp in the subdirectory \acad13\win\support I would type:

Command: (load "\\acad13\\win\\support\\label")

I now use the point labelling routine, as follows:

Command: label
Pick point: [pick]

AutoCAD provides a way to automatically load AutoLISP programs. When AutoCAD starts up, it looks for a file called acad.lsp. AutoCAD automatically loads the names of AutoLISP programs listed in the file. To add Label.Lsp is easy. Open the Acad.Lsp file with a text editor (if the file does not exist, then start a new file called Acad.Lsp and store it in a Support subdirectory). Add the name of the program:

(load "label.lsp")

Save the Acad.Lsp file. Start AutoCAD and it should automatically load Label.


Tailoring AutoCAD Part 8 | Part 9 | Part 10 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 XYZ Publishing, PO Box 3053, Sumas WA, 98295-3053, unless otherwise noted.