AutoLISP: The Complete Guide — Chapter 5 / 16

Variables and Data Types

So far, we have computed results that disappeared immediately after being displayed. To write useful programs, you need to be able to store values and reuse them. That is the purpose of variables.

Creating a variable with setq

In AutoLISP, you create (or modify) a variable using the setq function (set quote):

(setq radius 5)

This expression creates a variable named radius and assigns it the value 5. You can then use this variable in other expressions:

(setq radius 5)
(* pi (expt radius 2))  ; → 78.5398

Creating the radius variable and using it in a calculation

Assigning multiple variables at once

setq lets you assign multiple variables in a single call:

(setq
  width 10
  height 5
  depth 3
)

This is equivalent to three separate calls, but more concise:

(setq width 10)
(setq height 5)
(setq depth 3)

The exclamation mark !

In the AutoCAD command line, you can check the value of a variable by prefixing it with an exclamation mark:

!radius

AutoCAD displays: 5

This is very handy for debugging. Note that this ! syntax is specific to the AutoCAD command line and does not work inside a .lsp file.

Value of the radius variable displayed by !rayon

Variable names

Variable names in AutoLISP can contain:

  • Letters (a-z, A-Z)
  • Digits (0-9)
  • Special characters such as -, _, ?, !, *

Some common conventions:

(setq number-of-sides 12)     ; Hyphens to separate words
(setq start_point '(0 0 0))   ; Underscores also accepted
(setq isValid T)               ; camelCase sometimes used

Variable names are not case-sensitive: Radius, radius, and RADIUS all refer to the same variable.

Data types in detail

AutoLISP is a dynamically typed language: you do not need to declare a variable's type. The type is automatically determined by the assigned value.

Integers (INT)

Whole numbers, positive or negative, with no decimal point:

(setq counter 0)
(setq age 42)
(setq temperature -5)

AutoLISP integers are limited to the range -2,147,483,648 to 2,147,483,647 (signed 32-bit integers).

You can check a value's type using the type function:

(type 42)    ; → INT

Reals (REAL)

Floating-point numbers:

(setq pi-approx 3.14159)
(setq tolerance 0.001)
(setq ratio -2.5)

Important: always use a period as the decimal separator, never a comma.

(type 3.14)  ; → REAL

Strings (STR)

Text enclosed in double quotes:

(setq name "AutoLISP")
(setq message "Hello world!")

To include a quote inside a string, escape it with a backslash:

(setq quote "He said \"hello\"")

Special characters in strings:

Sequence Meaning
\n Newline
\\ Literal backslash
\" Literal quote
\t Tab
(type "text")  ; → STR

String operations

(strlen "Hello")                ; → 5 (length)
(strcat "Hel" "lo")            ; → "Hello" (concatenation)
(strcase "hello")              ; → "HELLO" (uppercase)
(strcase "HELLO" T)            ; → "hello" (lowercase)
(substr "Hello" 1 3)           ; → "Hel" (substring)

Lists (LIST)

Lists are the fundamental data structure in LISP (it's in the name!). We will study them in detail in a dedicated chapter, but here is a preview:

(setq point '(10.0 20.0 0.0))   ; A 3D point
(setq colors '("red" "green" "blue"))

The apostrophe ' before the parenthesis prevents AutoLISP from trying to evaluate the list as an expression. We will revisit this essential mechanism later.

(type '(1 2 3))  ; → LIST

nil and T

nil represents the absence of a value, false, or an empty list. T represents true.

(setq result nil)
(setq isOpen T)

Checking a value's type

The type function returns the type of a value:

(type 42)        ; → INT
(type 3.14)      ; → REAL
(type "text")    ; → STR
(type '(1 2))    ; → LIST
(type nil)       ; → nil
(type T)         ; → SYM
(type +)         ; → SUBR (built-in function)

Calls to (type ...) in the command line

Type conversions

AutoLISP provides functions to convert between types:

Conversion Function Example
Integer → Real float (float 5)5.0
Real → Integer fix (fix 3.7)3
Integer → String itoa (itoa 42)"42"
String → Integer atoi (atoi "42")42
Real → String rtos (rtos 3.14 2 2)"3.14"
String → Real atof (atof "3.14")3.14

These function names come from the C language:

  • itoa = integer to ASCII
  • atoi = ASCII to integer
  • atof = ASCII to float
  • rtos = real to string

The rtos function in detail

rtos is particularly useful because it lets you control the output format:

(rtos 3.14159 2 2)   ; → "3.14" (decimal mode, 2 decimal places)
(rtos 3.14159 2 4)   ; → "3.1416" (decimal mode, 4 decimal places)

The second argument is the mode (2 = decimal) and the third is the precision (number of decimal places).

AutoCAD system variables

AutoLISP can read and modify AutoCAD system variables using getvar and setvar:

(getvar "OSMODE")     ; Reads the object snap mode
(setvar "OSMODE" 0)   ; Disables object snaps
(getvar "DWGNAME")    ; Name of the current drawing

A practical example: volume calculation

Let's calculate and display the volume of a rectangular box:

(setq
  length 10.0
  width 5.0
  height 3.0
)

(setq volume (* length width height))

(alert
  (strcat
    "Dimensions: "
    (rtos length 2 1) " x "
    (rtos width 2 1) " x "
    (rtos height 2 1)
    "\nVolume: "
    (rtos volume 2 2)
    " units³"
  )
)

Dialog box displaying the dimensions and volume

Summary

Concept Syntax Example
Create a variable setq (setq x 10)
Read a variable Use its name (+ x 5)
Inspect (command line) ! !x
Check the type type (type x)INT
Integer → Real float (float 5)5.0
Integer → String itoa (itoa 42)"42"
Real → String rtos (rtos 3.14 2 2)"3.14"

In the next chapter, we will learn how to create our own functions with defun — this is where AutoLISP programming truly becomes powerful.


Helping hand Need an AutoCAD (AutoLISP, ObjectARX, .NET, VBA) development? Contact me for a free quote.