Satish Lele
satish.lele@gmail.com


Programming in AutoLISP

LISP means list processing. AutoLISP is segment of LISP designed for AutoCAD. AutoLISP is accessed through the AutoLISP interpreter.
AutoLISP uses symbols to refer to data. A symbol name can not consist only of numeric characters. Symbol names are not case sensitive and may consist of any sequence of alphanumeric and notation characters, except the following:
( Open Parenthesis
) Close Parenthesis
. Period
' Apostrophe
" Quote Symbol
; Semicolon
An AutoLISP variable assumes the data type of the value assigned to it. Until they are assigned new values, variables retain their original values. An AutoLISP variable that has not been assigned a value is said to be nil.
Predefined Variables:
PAUSE: Defined as a string consisting of a double backslash (\\) character. This variable is used with the command function to pause for user input.
PI: Defined as the constant pi. It evaluates to approximately 3.14159.
T: Defined as the constant T. This is used as a non-nil value.
Values are assigned by operator setq. You can store values using variables. A variable is like a container that holds value. That value can change in the course of a program’s operation. The Setq function tells AutoLISP to assign a value to a variable. The format is (setq variable value). Note that the format is between opening parentheses ( and closing parentheses ). First entity is operator and others are its arguments. Different operators have different number of arguments. Some arguments are even optional. You can see the value assigned to a variable by adding ! before the variable at command prompt. ! is another way of saying “Display the contents of”. Variables evaluate to the last value assigned to them. Variables are messengers to the program. They store and convey all forms of data. They are actually pointers to locations in computer’s memory where a value is stored. In order to conserve memory, variable names should be short, but descriptive. Integer variables should be i1, i2 etc, real numbers r1, r2 etc. strings s1, s2 etc.
Setq is a special function that is a combination of two other functions, Set and Quote. Set assigns the values of the second argument to the value of the first argument. The Quote function provides a means of preventing the evaluation of an argument. AutoLISP always tries to evaluate lists as if they are expressions. In case of setvar, it will apply the value of second argument to first one, if the first one a system variable.
The structure (operator arguments) is called an expression and it is the basic structure for all AutoLISP programs. Everything intended for the AutoLISP interpreter, from the simplest expression to the most complex programs, must be written with this structure. The result returned from evaluating an expression is called the value of the expression. An operator is an instruction to take some specific option. Operator is often referred to as function and the items to be operated on as the arguments to the function or simply arguments. All AutoLISP expressions, no matter what size, follow the structure and are enclosed in parentheses. All parentheses must be balanced. Double quotation marks enclosing text must also be balanced. Spaces are used to separate the function and arguments of the expression. Since AutoLISP evaluates all arguments, expressions can also be used as arguments to a function.
Operator setq can have number of pairs of Variables and Values, as it’s arguments. Its format is
(setq variable1 value1
   variable2 value2
   variable3 value3
   variable4 value4
)
Defun (): Defines a function with name and binds the routines together. The arguments to Defun are first the function name, and then the argument list. Argument list is used for two purposes. It is used when one program is called from another function and variable values must be passed between them. If c: is left out of function name, you create a user-defined function that can be used in other programs or during an AutoCAD command. Second use of argument list is to determine Global and Local variables. A global variable maintains its value even after a program has finished executing. A local variable, on the other hand, holds its value only within the program or user defined function it is found in. Local variables do not take up system memory permanently. / symbol is used to separate arguments and local variables. You must include a space before and after / to avoid creating an error. Variables left out of the list become global.
(defun function_name ()
  ( )
  ( )
  ( )
  ( )
)
It runs inside other functions and can have arguments and local variables.
(defun c:function_name ()
  ( )
  ( )
  ( )
  ( )
)
It runs as AutoCAD command and can not have arguments, but can have local variables.
Arguments / Local Variables:
(defun function_name ( arguments / local variables)
)
/ separates arguments and local variables
Arguments: values passed to function
Local Variables: variables used in function and these lose values at end of function.
(defun function_name ( / p0 p1 p2 p3)
)
No arguments, 4 local variables.
Arguments / Local Variables in function_name Program:
(defun function_name ( x y / p0 p1 p2 p3)
)
2 arguments, 4 local variables.
The text enclosed by Defun is saved as a text file with .lsp extension. (function_name.lsp) To run the function it should be loaded in the drawing. At command prompt it is loaded by typing (load “function_name.lsp”) or (load “function_name”). If it is found in defined path, it is loaded. Otherwise it gives a message ”File not found”. When loaded, function_name program can be run by typing (function_name).
GetReal and GetInt:
(getreal “optional string prompt”)
(getreal “Enter Distance in X Direction”)
(setq x (getreal “Enter Distance in X Direction”))
AND
(getint “optional string prompt”)
(getint “Enter Distance in X Direction”)
(setq x (getint “Enter Distance in X Direction”))
(defun function_name ( / p0 p1 p2 p3)
(setq x (getreal “Enter Distance in X Direction”)
y (getreal “Enter Distance in Y Direction”)
p0 (getpoint “select first point”)
) (some setq and command function lines)
)
The following points cover most common transcription errors:
Pay special attention to the spelling of variable names and the number and placement of parentheses.
Be sure to space the elements of your code correctly.
Problem can occur when lower case l and 1 are confused. O and 0 are also problematic.
When a program requires other functions to work properly, be sure that all the functions needed are loaded at the same time the main program is loaded.
Data Types:
Integers are whole numbers from -32768 to 32767.
Real numbers include decimal values. Computations performed on integers, are faster than those performed on real numbers. Real numbers with value between 0.1 and -0.1 must begin with zero.
String refers to text. You can combine or concatenate two strings into one string with strcat operator. Strings and numeric values cannot be evaluated together.
Lists are data elements enclosed in parentheses. They are the basic data structure in AutoLISP. A list can be made up of any number of integers, real numbers, strings and even other lists. When a list contains a function as its first element, we can generally assume that it is an expression intended for evaluation. Such a list is often referred to as a form. There are two types of lists, those intended for evaluation and those used to store data. AutoLISP blindly evaluates everything, Quote is needed to tell AutoLISP not to evaluate a list, ‘().
File Descriptors are used in a program to access files that have been opened for processing.
Entity Names: Every object, or entity, in an AutoCAD drawing has a name. The name is alphanumeric code unique to that object. The name can be accessed by AutoLISP and used as a means of selecting individual objects for processing. Entity names are provided by AutoCAD and are not user definable. Also entity names can change from one drawing session to another.
Symbols / Variables: These are usually text, but they can also contain numbers.
Subrs: Subroutines are the built-in functions offered by AutoLISP.
Atoms: An atom is an element that cannot be taken apart into elements. In a co-ordinate list, the x, y and z values are atoms.
alert : Displays an alert function_name with error or warning message passed as a string.
(alert string)
An alert box is a dialog box with a single OK button.
(alert "That function is not available.")
You can display multiple lines by using the newline character in string.
(alert "That function\nis not available.")
Note Line length and the number of lines in an alert box are platform, device, and window dependent. AutoCAD truncates any string that is too long to fit inside an alert box.
command: Executes an AutoCAD command. (command [arguments] ...)
(command "_line" pt1 pt2 "")
Foreign Language Support: If you develop AutoLISP programs that can be used with a foreign language version of AutoCAD, the standard AutoCAD commands and keywords are automatically translated if you precede each command or keyword with an underscore (_).
If you are using the dot prefix (to avoid using redefined commands), you can place the dot and underscore in either order. Both "._line" and "_.line" are valid.
getvar: Retrieves the value of an AutoCAD system variable. (getvar varname)
The varname argument is a string that names the system variable. If varname is not a valid system variable, getvar returns nil. For example, assuming that the fillet radius is set to 0.25 units,
(getvar "FILLETRAD") returns 0.25
setvar: Sets an AutoCAD system variable to a specified value. (setvar varname value)
If successful, setvar returns the value of the system variable. You must enclose the variable name in quotation marks.
(setvar "FILLETRAD" 0.50) returns 0.5 and sets the AutoCAD fillet radius to 0.5 units. For system variables with integer values, the supplied value must be between -32,768 and +32,767.
backBack top