About Variables (AutoLISP)

Variables are used to store a value or list of values in memory.

The data type of a variable is determined when a value is assigned. Variables retain their value until a new value is assigned or the variable goes out of scope. The scope of a variable can either be global or local. Global variables are accessible by any AutoLISP program that is loaded into a drawing, while local variables are only available within a specific function or command. You use the AutoLISP setq function to assign values to variables.

The syntax of the setq function is as follows:

(setq variable_name1 value1 [variable_name2 value2 ...])

The setq function assigns the specified value to the variable name given, and returns the last assigned value as its function result. The following example creates two variables: val and abc. val is assigned the value of 3, while abc is assigned the value of 3.875.

(setq val 3 abc 3.875)
3.875

The following example creates a variable named layr and assigns it the value of “EXTERIOR-WALLS”.

(setq layr "EXTERIOR-WALLS")
"EXTERIOR-WALLS"

Using a Variable with a Function

Once a value is assigned to a variable, it can be used in an expression as the value for an argument of a function. The following uses two of the previously created variables in a few AutoLISP expressions to create a layer and draw a line with a specific length at 0 degrees.

(command "_.-layer" "_make" layr "")
(command "_.line" PAUSE (strcat "@" (itoa val) "<0") "")

Checking the Value of a Variable

You can use the following methods to determine the current value of a variable: