Autolisp Cap 27
Autolisp Cap 27
Introduction to
AutoLISP
Learning Objectives
After completing this chapter, you will be able to:
✓ Use basic AutoLISP functions on the command line.
✓ Locate, load, and run existing AutoLISP programs.
✓ Define new AutoCAD commands.
✓ Write AutoLISP programs using the Visual LISP Editor.
What Is AutoLISP?
AutoLISP is a derivative, or dialect, of the LISP programming language. LISP,
which is an acronym for list processing, is a high-level computer programming
language used in artificial intelligence (AI) systems. In this reference, the term
high-level does not mean complex, rather it means powerful. As a matter of fact, many
AutoCAD users refer to AutoLISP as the “nonprogrammer’s language” because it is
easy to understand.
AutoLISP is specially designed by Autodesk to work with AutoCAD. It is a flexible
language that allows the programmer to create custom commands and functions that
can greatly increase productivity and drawing efficiency.
Knowing the basics of AutoLISP gives you a better understanding of how AutoCAD
works. By learning just a few simple functions, you can create new commands that
make a significant difference in your daily productivity. Read through this chapter
slowly while you are at a computer. Type all of the examples and exercises as you read
them. This is the best way to get a feel for AutoLISP.
AutoLISP Basics
As stated earlier, LISP stands for list processing, which indicates that AutoLISP
processes lists. In the LISP language, a list can be defined as any number of data
enclosed in parentheses. Each item in a list must be separated from other items by a
space.
When any entry is made on the command line, it is checked to see if the first
character is a parenthesis. The opening parenthesis tells AutoCAD that an AutoLISP
expression is being entered. AutoCAD then sends the expression to the AutoLISP
interpreter for evaluation. The initial input can be supplied as direct keyboard entry
or even a menu macro. The format for an AutoLISP expression, called syntax, is:
(FunctionName AnyRequiredData…)
The first item in the AutoLISP expression is a function name. A function in
AutoLISP is similar to a command in AutoCAD. Some functions require additional
information. For example, the addition function requires numeric data:
Command: (+ 2 4)↵
6
Command:
Any required or optional data for a function are called the arguments. Some func-
tions use no arguments; others may require one or more. When entering an AutoLISP
expression, it is important to close it using a closing parenthesis prior to pressing [Enter].
When you press [Enter] , the AutoLISP interpreter checks to see that the number of
opening and closing parentheses match. If they do not, you are prompted:
Command: (+ 2 4↵
(_>
Symbol Function
+ Addition; returns the sum of all the supplied number arguments.
– Subtraction; subtracts the sum of the second through the last
number arguments from the first number argument and returns
the result.
* Multiplication; returns the product of all the supplied number
arguments.
/ Division; divides the first number argument by the product of
the second through the last number arguments.
Exercise 27-1
Complete the exercise on the student website.
www.g-wlearning.com/CAD
Significant Digits
AutoLISP performs all mathematical calculations to 15 decimal places, but only
displays six significant digits. For example, take a close look at this expression:
Command: (+ 15 (* 3.75 2.125))↵
22.9688
The actual result is 22.96875, but AutoLISP displays only six significant digits on the
command line and rounds the number for display only. This is true for large and small
numbers alike. The next example shows how AutoLISP uses exponential notation to
display larger numbers using only six digits:
Command: (* 1000 1575.25)↵
1.57525e+006
This final example uses a numeric printing function (real to string) set to show eight
decimal places in order to indicate the number is not actually rounded and that no
precision is lost:
Command: (rtos (+ 15 (* 3.75 2.125)) 2 8)↵
"22.96875000"
Exercise 27-2
Complete the exercise on the student website.
www.g-wlearning.com/CAD
Variables
All programming languages make use of variables to temporarily store informa-
tion. The variable name can be used in an expression anywhere in the program. When
AutoLISP encounters a variable in an expression, it uses the value of the variable to
evaluate the expression. An AutoLISP variable name cannot be made up of numeric
characters only, nor can it contain any of the following characters.
• Open parenthesis (()
• Close parenthesis ())
Figure 27-1.
Each AutoLISP (setq D (* (+ A B ) 2))
expression must
be enclosed within
parentheses. In this 3
evaluation of variable 2
D, expression 3 is 1
evaluated first, then
expression 2, and
finally expression 1.
Exercise 27-3
Complete the exercise on the student website.
www.g-wlearning.com/CAD
VLIDE
Ribbon
Manage
Editor is first displayed, it appears as shown in Figure 27-2. The windows within the > Applications
editor can be minimized or maximized. The editor itself can be temporarily closed to
return to AutoCAD as necessary. Visual LISP Editor
To create a new AutoLISP program using the Visual LISP Editor, pick the New Type
file button, select New File from the editor’s File pull-down menu, or press [Ctrl]+[N]. VLIDE
VLISP
This opens a window for an untitled document on the Visual LISP Editor desktop. See
Figure 27-3. The windows and features in the Visual LISP Editor are:
• Desktop. This is the main area of the editor window. It is similar to the main
program window in AutoCAD and can be used to relocate toolbars or windowed
components, such as the Visual LISP Console or a text editor window.
• Text editor window. Text editor windows are used to write and edit AutoLISP
programs. Different windows can be used to create new files or view existing
programs. The Visual LISP Editor provides interactive feedback as you enter
material to help you avoid errors.
• Visual LISP Console window. This window provides several functions. You
can use it to enter any AutoLISP expression to immediately see the results.
You can also enter any AutoLISP variable to determine its value. Visual LISP
commands can also be entered from this window. The text can then be copied
from the window to a text editor window.
• Trace window. This window is minimized when you first display the Visual
LISP Editor. It records a history of the functions within your program and can
be used to trace values when developing or debugging a program.
Figure 27-2.
The primary features of the Visual LISP Editor.
Pull-down New file Activate AutoCAD
menu bar button button
Desktop
Text editor
window
• Status bar. This area at the bottom of the Visual LISP Editor is similar to the
status bar in AutoCAD’s main program window. It provides feedback regarding
the status of the current window or application being used.
Several visual aids are provided to help identify functions as you enter text. For
example, as you construct the expressions that make up your program, a color-coding
system provides immediate feedback as you type. Text for any unrecognized items, such
as a user variable or a portion of a function, is shown in black. For example, if you enter
the (setq) function, the text is shown in black until you have entered the letters set.
Because AutoLISP recognizes the text entry as the valid function (set), a function not
covered in this book, the color of the text is changed to blue. When you have entered the
full (setq) function name, the text remains blue because AutoLISP also recognizes this
function name. The color coding can be very useful. If you enter a function name and
the text does not turn blue, you know that you have made an incorrect entry. The default
color coding system used in the Visual LISP Editor appears in the following chart.
Another valuable visual aid provided by the Visual LISP Editor is instant paren-
thesis matching. When you enter a closing parenthesis in an expression, the cursor
jumps to the opening parenthesis and then returns to the current position. If the closing
parenthesis does not have a match, the cursor does not jump. This helps indicate that
a matching parenthesis is needed.
Copyright by Goodheart-Willcox Co., Inc.
Chapter 27 Introduction to AutoLISP AutoCAD and Its Applications—Advanced 651
Once you have entered one or more expressions in the Visual LISP Editor, you can
save the file and then test the results in the Visual LISP Console window. Or, you can
return to AutoCAD to test your results.
PROFESSIONAL TIP
If you frequently work with AutoLISP program files, you may wish
to learn more about the advanced features of the Visual LISP Editor.
Some of these advanced features include powerful formatting and
debugging tools that make your programming time much more
productive. For a complete discussion of these topics, refer to Visual
LISP Programming available from Goodheart-Willcox Publisher.
PROFESSIONAL TIP
When defining new command names, keep in mind that most
AutoCAD users are one-handed typists because the other hand is
used for the pointing device. For example, when deciding on the
name for a function that performs a ZOOM Previous, it may be easier
for the user to type the [Z]+[X] key combination rather than [Z]+[P].
The [Z] and [P] keys are in opposite corners of the keyboard.
(defun C:FC ()
(command "FILLET" "RADIUS" 0 "FILLET" "MULTIPLE")
(princ)
)
When using the Visual LISP Editor, the final closing parenthesis is not automati-
cally “flush left.” You will need to delete the spaces added. It is a good habit to place the
final closing parenthesis flush left to help keep your program organized.
APPLOAD
Ribbon
files, into AutoCAD. Once the command is selected in AutoCAD, the Load/Unload Manage
> Applications
Applications dialog box appears, Figure 27-4. A list of currently loaded applications
appears in the Loaded Applications tab. To load an application file, select it in the file- Load Application
selection window near the top of the dialog box. You can highlight any number of files Type
APPLOAD
Figure 27-4.
The Load/Unload Applications dialog box is used to load AutoLISP program files into AutoCAD.
Select file
Pick to load
the selected
file into
AutoCAD
Currently
loaded
application
files
Check to add
the selected
file to the
History list
tab
Exercise 27-4
Complete the exercise on the student website.
www.g-wlearning.com/CAD
PROFESSIONAL TIP
The sample AutoLISP expressions in this section are entered at the
command line. However, AutoLISP expressions are more effective
as part of a saved program file.
Exercise 27-5
Complete the exercise on the student website.
www.g-wlearning.com/CAD
CAUTION
The symbol T is a built-in AutoLISP constant defined as a protected
symbol. However, it is possible to change its value using the (setq)
function. Do not change its value. Be certain not to use the variable
name T for any of your own variables or other functions referencing
this symbol may not properly function. If it is accidentally changed,
you can reset the value of T using the following expression.
Command: (setq T 'T)↵
Exercise 27-6
Complete the exercise on the student website.
www.g-wlearning.com/CAD
PROFESSIONAL TIP
Design your AutoLISP programs to closely resemble the AutoCAD
interface. For example, it is easier for the user to read “back-to-back”
prompts when the prompts appear on separate lines. Use the \n
string to specify a new line for a prompt. For example:
(setq PT2 (getpoint "\nTo point:"))
Exercise 27-7
Complete the exercise on the student website.
www.g-wlearning.com/CAD
4. Use the program in problem 3 to create a new command that draws a square with
thick lines.
A. The line thickness should be a percentage of the fillet radius (between 5%
and 10%).
B. Reset the line thickness so that the next rectangle drawn with the RECTANG
command does not automatically have thick lines. Note: Use ^C to cancel a
command that you do not want to complete. For example:
(command "LINE" PT1 ^C)
5. Write an AutoLISP program that allows the user to draw parallel rectangles.
A. Provide a prompt that asks the user to enter an offset distance for a second