8. Writing a Simple AutoLISP Program, Part 2

by Ralph Grabowski

In the last lesson, I presented several tips for helping out when you write your own AutoLISP functions. In this lesson, we learn the first steps in writing an AutoLISP routine of our own.

Why Write a Program?

If you are like many CAD users, you are busy creating drawings, you have no time to learn how to program. No doubt, you may be wondering, "Why bother learning a programming language?" In some ways, it's like being back in school again. Sitting in the classroom sometimes seems like a waste of time. But the things you learn now make life easier later. Learning some AutoLISP programming now means you'll feel really good whipping off a few lines of code and letting AutoLISP perform a tedious task for you. The nice thing about AutoLISP is that you can program it on the fly. And you can use it for really simple but tedious tasks. Here's the example we'll use for this tutorial:

The Id Command

AutoCAD has the Id command. When you pick a point on the screen, it tells you the 3D x,y,z- coordinates of the point. Problem is, Id reports the value in the command prompt area, like this:

Wouldn't it be great if you could change Id to place the coordinates on your drawing, next to the pick point? That would let you label x,y-coordinates and z-elevations over a site plan. With AutoLISP, you can.

The Plan of Attack

Before we write any AutoLISP code, we need to figure out how we're going to get those x,y,z- coordinates off the command prompt area and into the drawing. We've just recognized the two parts to solving the problem:

Part 1. Obtain the coordinates from the drawing, probably by picking a point.

Part 2. Place the coordinates as text in the drawing.

Obtaining the Coordinates

AutoLISP provides several ways to get the coordinates of a picked point. Browsing through the Customization Guide, we find we could:

It would be a useful lesson to use each of the three and see what happens. By experimenting, you make mistakes, then learn from the mistakes. Start AutoCAD, load a drawing, and switch to the Text window with F2. At the Command prompt, type:

You are executing an AutoCAD command from within an AutoLISP routine. The Command function lets you use any AutoCAD command in AutoLISP. The AutoCAD command is in quotation marks "ID" because the command is a string (programmer talk for "text"). Just like before, AutoCAD prompts you for the point. This time use an object snap to snap to a geometric feature, like the end of a line:

That's great! We've typed in a tiny AutoLISP routine. Now, AutoCAD stores the x,y,z- coordinates of the last-picked point in system variable LastPoint. Before we can place the coordinates as text in the drawing, we have to store the coordinates in a variable. Recall from earlier tutorials that the SetQ function lets us store a value in a variable. Let's make use of this now. Type at the Command prompt:

Xyz is the name of the variable we are using to store the x,y,z-coordinates. GetVar is the name of the AutoLISP that retrieves the value stored in a system variable. And "LastPoint" is the name of the system variable; once again, it is surrounded by quotation marks because it is a string.

After typing in the AutoLISP function, AutoCAD returns the value it has stored in variable Xyz: (8.9227 6.5907 0.0000). Note how the coordinates are surrounded by parenthesis. This is called a "list" and is the list format that LISP is famous for. The spaces separate the numbers, which are the x, y, and z-coordinates, respectively:

AutoCAD always stores these values in the order of x, y, and z. You will never find the z- coordinate first or the x-coordinate last. So, we've now solved the first problem in one manner. We obtained the coordinates off the drawing and stored them in a variable. We did mention a third AutoLISP function we could use, GetPoint. Programmers prefer GetPoint because it is more efficient than the Id-LastPoint combo we used above. Type the following to see that it works exactly the same:

As before, we use the SetQ function to store the value of the coordinates in variable Xyz. The GetPoint function waits for you to pick a point on the screen. The "Point: " is called a prompt and tells the user what the program is expecting the user to do. In this case, we mimicked the prompt of the ID command. But we could just as easily have written anything, like:

Or, we could have no prompt at all, as follows:

That's right. No prompt. Just a silent AutoCAD waiting patiently for the right thing to happen ... and the user puzzled at why nothing is happening. A lack of communication, you might say. That's why prompts are important.

We've now seen approaches that solve the same problem in two different ways. With the x,y,z-coordinates safely stored in a variable, let's tackle the second problem

Placing the Text

To place text in the drawing, there is just the Command function in conjunction with the Text command. I suppose the DText or MText commands might work, but we are placing one line of text and the Text command is excellent for that. The Text command is trickier than the Id command. It has four prompts that our AutoLISP routine must answer:

This is where I find a quick reference book handy that lists all the options of every AutoCAD command. Let's see if we can now construct the AutoLISP function for placing the coordinates as text:
(command The Command function.
"text" Text is the AutoCAD command being executed. 
xyz We can use the Xyz variable as the starting point for the text.
200 The height of the text. Change this number to something convenient for your drawings.
0 The rotation angle of the text.
xyz  We're lucky: the Text command accepts numbers as text.
) And remember: one closing parenthesis for every opening parenthesis.

Let's try this out at the Command: prompt:

AutoCAD runs through the Text command, inserting the responses for its prompts, then placing the coordinates as text. We've solved the second part of the problem.

Putting It Together

Let's put together the two solutions to our problem:

There we have it: a full-fledged AutoLISP program. Well, not quite. It's a pain to retype those two lines each time you want to label a point. In the next leson, we find out how to save the code to a LSP file on disk. We'll also dress up the code, including suppressing that "nil" that gets output at the end of the routine.

An Alternative to Programming

Just because this tutorial is about AutoLISP doesn't mean that it is always the best approach. Here's a wacky work-around to labeling points that uses no programming at all. However, it only works with the Windows version of AutoCAD Release 13:

1. Use the Id command and pick a point:

2. Switch to the text screen by pressing function key F2.

3. Highlight the text: Press the mouse button and drag the cursor over the "X = 8.9227 Y = 6.5907 Z = 0.0000" coordinates.

4. Click the right mouse button. AutoCAD displays the cursor menu.

5. Select Copy from the cursor menu. AutoCAD copies the coordinate text to the Windows clipboard.

6. Press F2 to switch back to AutoCAD drawing window.

7. Press Ctrl+V. AutoCAD pastes the text into the drawing using the current text style.

AutoCAD always pastes stuff in the upper-left corner of the drawing screen. You can use the Move command to shift the text into position.

Tailoring AutoCAD Part 7 | Part 8 | Part 9 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.