7. Tips in Using AutoLISP

by Ralph Grabowski

In the last tutorial, we concluded our tour of AutoLISP by looking at the more esoteric functions available in AutoCAD for manipulating the drawing file. In this tutorial, I present tips for helping out when you write your own AutoLISP functions. You may want to save clip this column for reference.

Tip #1. Use an ASCII Text Editor.
AutoLISP code must be written in plain ASCII text -- no special characters of the sort that all word processors add in. When you write AutoLISP code with, say, Word, then save as a DOC file (the default), AutoCAD will simply refuse to load the AutoLISP file, even when the file extension is LSP.

In an increasingly WYSIWYG world, it is becoming harder to find a true ASCII text editor. There is one pure ASCII text editor supplied free by Microsoft with Windows called Notepad, which you'll find in the \Windows folder. Do not use Write or WordPad supplied with Windows. While both of these have an option to save in ASCII, you're bound to forget sometimes and end up frustrated.

Almost any other word processor has an option to save text in plain ASCII, but not by default. Word processors have a number of different terms for what I mean by "pure ASCII format". Word calls it "Text Only"; WordPerfect calls it "DOS Text"; WordPad calls it "Text Document."

Tip #2: Loading LSP Code into AutoCAD
To load the AutoLISP code into AutoCAD, you use the (load "mylisp") command line format, which is not to be confused with the Load command which is for loading a SHX compiled shape files). Here's an example where "Points.Lsp" is the name of the AutoLISP routine:

	Command: (load "points")

You don't need to type the ".LSP" extension. When AutoCAD cannot find the Points.Lsp, you'll need to specify the subdirectory name by using either a forward slash or double backslashes -- your choice:

	Command: (load "\\autocad\\points")

After you've typed this a few times, you'll find this gets tedious. To solve the problem, write a one-line AutoLISP routine that reduces the keystrokes, like this:

	Command: (defun c:x () (load "points"))

Now anytime you need to load the Points.Lsp routine, you just type X and press Enter, as follows:

	Command: x

Under Windows, you use the above shortcut method or drag the LSP file from the File Manager into AutoCAD. Note that the codemoves one way: from the text editor to AutoCAD; the code never moves from AutoCAD back to the text editor.

Tip #3: Toggling System Variables
One of the problems in programming is: How to change a value whenyou don't know what the value is? In AutoCAD, you come across this problem with system variables, many of which are toggles. A "toggle" system variable has a value of 0 or 1, indicating that the value is either off (0) or on (1). For example, system variable SplFrame is by default 0: when turned off, splined polylines do not display their frame.

No computer programmer ever assumes that the value of SplFrameis going to be zero just because that's its default value. In the case of toggle system variables, there two solutions:

(1) use the (if) function to see if the value is 0 or 1; or

(2) subtract 1 and take the absolute value.

Tip #4: Be Neat and Tidy.
Remember, your mother always told you to pick up your things. This problem of the settings of system variables applies universally. When your AutoLISP routine changes values of system variables, it must always set them back to the way they were before the routine began running.

Many programmers write a set of generic functions that save the current settings at the beginning of the routine. Carrying out changes. Restore the saved values at the end of the routine. Here's a code fragment that shows this:

	(setq splvar (getvar "splframe"))
	...
	(setvar "splframe" splvar)

Tip #5: Suppress That nil
Any time you run an AutoLISP routine, there is that pesky "nil" appearing as the very last thing displayed. There is an actual reason for nil appearing, but the reason isn't good enough for leaving it there. Here's how to prevent that nil from appearing: end your routine with a (princ)all by itself.

Tip #6: Read-Write-Append File
In (almost) all cases, AutoLISP doesn't care if you use UPPERCASE or lowercase for writing the code. For legibility, there are some conventions, such as AutoLISP function names in all lowercase, your function names in Mixed Case, and AutoCAD variables and command names in all UPPERCASE.

As I said, AutoLISP doesn't care, and converts everything intouppercase in any case. It also strips out all comments, excess white space, tabs, and return characters. The exception is text in quote marks, such as prompts, is left as is.

There are two exception where AutoLISP does care: when you are working with file functions and escape codes. The (open) function uses the arguments "r", "w", and "a" to read to, write from, and append to a file, respectively. Those three characters must be lowercase.

(Speaking of files, AutoLISP can only access ASCII files in sequential order. It cannot read binary files and is incapable random access.)

Escape codes used in text strings must also remain lowercase. For example, "\e" is the escape character (equivalent to ASCII 27) and "\t" is the tab characters. Note that they use backslashes; it is for this reason that you cannot use the backslash for separating folders names back in Tip #2. AutoLISP would think you were typing an escape code.

Tip # 7: Quotation Marks as Quotation Marks
As we have seen, AutoLISP uses quotation marks " and " for strings. Thus, you cannot use a quotation mark as for displaying quotation marks and inches, such as displaying 25 inches as 25".

The workaround is to use the escape codes mentioned above in Tip #6, specifically the octal code equivalent for the ASCII character for the quotation mark. Sound complicated? It is. But all you need to know is 042. Here's how it works.

First, assign the strings to variables, as follows:

	(setq disttxt "The length is ")
	(setq distval 25)
	(setq qumark "\042")

Notice how I assigned octal 042 to variable qumark. The backslash tells AutoLISP the numbers following are in octal. Octal, by the way, is half of hexadecimal: 0 1 2 3 4 5 6 7 10 11 12 ... 16 17 20 ... Then concatenate the three strings together with the (strcat) function:

	(strcat distxt distval qumark)

To produce the prompt:

	The length is 25"

Tip # 8: Keep Variables to Six Characters
To write the most efficient code, keep the variable names to a length of six characters or less. You may have notice that the variables I used above were all six characters or fewer. Go over six-characters and AutoLISP uses twice the space to hold the variable name.

Summing Up
In this tutorial, I presented some tips for helping you create your own AutoLISP functions. In the next tutorial, we'll go step by step throughcreating an AutoLISP routine that labels points in the screen.


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