Now updated for AutoCAD 2000!

6. Introduction to AutoLISP Programming, Part 3

by Ralph Grabowski

In the last lesson, we got a taste of many of the different kinds of functions AutoLISP offers you for manipulating numbers and text. We learned about five groups of AutoLISP functions: math, geometry, conditional, string, and conversion.

 In this lesson, we continue our tour of by looking at the more esoteric functions that AutoLISP uses to manipulate the drawing file. Autodesk made AutoLISP so powerful that it can manipulate almost any aspect of the AutoCAD drawing.

External Command Functions

"Powerful" often equates to "complicated" yet one of AutoLISP's most powerful functions is its simplest to understand: the Command function. As its name suggests, Command executes AutoCAD commands from within AutoLISP.

Think about it: this means that it is trivial to get AutoLISP to draw a circle, place text, zoom a viewport, whatever. Anything you type at the 'Command:' prompt is available with the Command function. Let's see how the Command function works by drawing a circle. First, though, let's recall how the Circle command works:

Command: circle
3P/2P/TTR/<Center point>: 2,2
Diameter: D
Diameter: 1.5

In the Command function, you mimic what you type at the 'Command:' prompt like this:

Command: (command "circle" "2,2" "D" "1.5")

Notice how all the typed text is in quotation marks. After you type that in, AutoCAD responds by drawing the circle:

circle 3P/2P/TTR/<Center point>: 2,2 Diameter: D Diameter: 1.5
Command: nil

(Don't worry about that "nil" at the end; that's just AutoLISP's way to letting you know it is finished with it's work. In the next tutorial, you find out how to prevent nil from appearing.)

Let's look at one of the more complex commands to use with the Command function, Text. When we use the Text command, AutoCAD presents these prompts:

Command: text
Justify/Style: 5,10
Height <0.2000>: 1.5
Rotation angle <0>: [Enter]
Text: Tailoring AutoCAD

Converted to AutoLISP-ese, this becomes:

Command: (command "text" "5,10" "1.5" "" "Tailoring AutoCAD")

And AutoCAD responds with:

text Justify/Style: 5,10
Height <0.2000>: 1.5
Rotation angle <0>:
Text: Tailoring AutoCAD
Command: nil

and draws the text.

  (AJ) -I don't know how important it is but in A2002 the response is:

text
Current text style: 'Standard' Text height: 1.5
Specify start point of text or [Justify/Style]: 5,10
Specify height <0.2000>: 1.5
Specify rotation angle of text <0>:
Enter text: Tailoring AutoCAD
Command: nil

For the 'Rotation angle:' prompt, we had simply pressed the Enter key. Notice how that was dealt with in the AutoLISP function: "" a pair of empty quotation marks. You use the same thing for commands that automatically repeat themselves, such as the Line command:

Command: (command "line" "1,2" "3,4" "")

When you don't include that final "", then you leave AutoCAD hanging with a To point: prompt and your AutoLISP routine fails.

By now it should be clear to you that you have to really know the prompt sequence of AutoCAD's more than 300 commands to work effectively with the Command function. The easiest way to get a handle on those is to purchase one of the "quick reference" books on the market, which list commands in alphabetical order, along with the complete prompt sequence. And, as we see in a minute, check that the quick reference book has a listingof all system variables, their default value, and the range of permissible values.

But the Command function has a failing. Earlier, I said, "Anything you type at the 'Command:' prompt is available with Command function." I place the emphasis on the word "type." The Command function breaks down completely when it comes to dialog boxes. That's right: any command that uses a dialog box won't work with the Commandvfunction -- nor, for that matter, with the toolbar macros we looked at a couple of tutorials ago. It is for this reason that AutoCAD includes command line versions of almost every (butnot all) commands.

While you could use the Command function to access system variables, AutoLISP has a pair of more direct functions: GetVar and SetVar.
GetVar gets the value of a system variable, while SetVar changes (sets) the value. For example, system variable SplFrame determines whether the frame of a spline polyline is displayed; by default, the value of SplFrame is 0: the frame is not displayed, as confirmed by GetVar:

Command: (getvar "splframe")
0

To display the frame, change the value of SplFrame to 1 with SetVar as follows:

Command: (setvar "splframe" 1)
1

However, we have made a crass assumption here: that the initial value of SplFrame is 0. Zero is the default value but not necessarily the value at the time that you run the AutoLISP routine. How do we know what the value of SplFrame is before we change it? We'll answer that question in another lesson. Stay tuned.

GetXXX and Selection Set Functions

It's one thing to execute a command to draw a new object, such as the new circle and text we drew above with the Command function. It is trickier working with objects that already exist, such as moving that circle and editing the text. That's where the a group of 14 functions known collectively as Getxxx come into play. These get data from the screen. Some of the more useful ones include:

Here's how to use some of these with the Text command from above. Let's redo the code so that AutoLISP prompts us for verything first, then executes the Text command:

Command: (setq TxtStr (getstring T "What do you want to write? "))
What do you want to write? Tailoring AutoCAD "Tailoring AutoCAD"

Notice that extra "T"; that's a workaround that lets GetString accept a string of text with spaces. When you leave out the T, then GetString only accepts text up to the first space: you would end up with just "Tailoring" and no "AutoCAD." The SetQ function stores the phrase "Tailoring AutoCAD" in the variable TxtStr.

Command: (setq TxtHt (getreal "How big do you want the letters? "))
How big do you want the letters? 2
2.0

Notice how GetReal converts the 2 (an integer) to a real number, 2.0.

Command: (setq TxtAng (getangle "Tilt the text by how much? ")) Tilt the text by how much? 30
0.523599

Notice how GetAngle converts the 30 (a decimal degree) into radians, 0.523599.

Command: (setq TxtIns (getpoint "Where do you want the text to start? "))
Where do you want the text to start? (27.8068 4.9825 0.0)

Notice how GetPoint returns the x, y, and z values of the coordinate, even though z is zero. Finally, we execute the Text command with the four variables:

Command: (command "text" TxtIns TxtHt TxtAng TxtStr)
text Justify/Style:
Height <1.5000>: 2.000000000000000
Rotation angle <0>: 0.523598775598299
Text: Tailoring AutoCAD
Command: nil

There! We've just customized the Text command to our liking. Not only did we change the prompts that the user sees, but we change the order of the prompts.

To work with more than one object at a time, AutoLISP has a group of six functions for creating a selection set. These all begin with "SS", as in:

AutoCAD's Select command can only deal with one selection set at a time; the AutoLISP SS commands can work with up to 128 different selection sets.

Object Manipulation Functions

The really powerful AutoLISP functions are the ones that go right in and manipulate the drawing database. Unlike the Commandfunction, which is powerful but simple, the object manipulation functions are powerful and complicated. Here's a summary of what some of these are:

The "Ent" prefix is short for entity, the old name for AutoCAD objects. The "symbol table" refers to the part of the drawing database that stores the names of layers, text styles, and other named objects in the drawing.

To create and manipulate objects, these AutoLISP functions work with a variant on the DXF format, known as "dotted pairs." For example, to work with a layer named RightOfWay, you employ the following format:

2 . RightOfWay"

Once again, the quotation marks indicate the start and end of the data, while the dot in the middle separates the two values: 2 is the DXF code for layer names and RightOfWay is the name of the layer. You can see that to work with these object manipulating functions, you need a good grasp of the DXF format.

Advanced AutoLISP Functions

There is a whole host of AutoLISP functions that you may never use in your AutoCAD programming career. For example, Autodesk has written 41 AutoLISP functions for controlling the ASE (AutoCAD Structured query language Extension) link between objects in the AutoCAD drawing and records in an external database file. These 41 functions are not hard to spot: they all start with "Ase_", as in Ase_LsUnite and Ase_DoCmp.

 Another set of AutoLISP functions -- 21 of 'em -- are strictly for loading and displaying dialogue boxes, such as Load_Dialogue and New_Dialogue. Another five functions are for memory management, whose use is so rarified that Autodesk recommends never using them.

Summing Up

In this lesson, we concluded our tour of AutoLISP by looking at the more esoteric functions available in AutoCAD for manipulating the drawing file. In the next tutorial, I'll present tips for helping you create your own AutoLISP functions.


TailoringAutoCAD Part 5 | Part 6 | Part 7 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.