Chapter 35. Exploring AutoLISP Further

AutoLISP offers many features that enable you to create sophisticated programs. These include variables, functions, and conditional structures. You can use these features to easily work with drawing coordinates. You can retrieve information about drawing objects and then modify them. As a result, getting input from the user makes your programs more interactive.

Note

AutoCAD LT does not support AutoLISP. This entire chapter is for AutoCAD only.

Creating Variables

You can't do anything very useful in programming without using variables. A variable is a symbolic name that a program operates on. An important part of the usefulness of variables is that you can assign values to them. You use the setq function to name and assign a value to a variable.

The following example sets the value of 3 to a variable named radius.

(setq radius 3)
3

You can try this example in Visual LISP in the Console window. If you want to use this variable on the AutoCAD command line, precede it with an exclamation point (!). For example:

Command: !radius
3

The exclamation point before a variable evaluates the value that is stored in the variable and returns it to the command line. When you use a variable in the Console window that you've already set, you don't need the exclamation point. The Console treats everything that you type there as an AutoLISP expression.

Assigning strings to a variable is as easy as assigning numerals to a variable:

(setq name "Robin")
"Robin"

You can also ...

Get AutoCAD® 2009 & AutoCAD LT® 2009 Bible now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.