Skip to content

AutoLISP (1 of 5)

quote

(quote expression) or 'expression

Returns the expression without evaluating it.

'(1 2 3)          ; (1 2 3)
(quote a)         ; A

On the command line only what starts with ( is evaluated: to see a quoted list, type (quote (1 2 3)). Symbols are returned in uppercase.

Example

A fixed list of layers, without evaluating its elements:

(setq layers '("WALLS" "DOORS"))
(length layers)   ; 2

See also

list · eval · function

function

(function symbol-or-lambda)

Like quote, but tells that the argument is a function. Used with mapcar and the like.

For lambda expressions passed as arguments to mapcar, vl-some and similar functions, function documents the intent and is the form AutoCAD recommends, although quote gives the same result in most cases.

(mapcar (function (lambda (x) (* x x))) '(1 2 3))   ; (1 4 9)

lambda

(lambda (arguments [/ locals]) expression...)

Creates an unnamed function. Returns the function.

((lambda (a b) (+ a b)) 2 3)   ; 5

It is mostly used as an argument of mapcar, vl-remove-if or apply, when the function is only needed in that place. It can also be stored in a variable and called later.

Example

(mapcar '(lambda (x) (* x 10)) '(1 2 3))   ; (10 20 30)
(setq square (lambda (x) (* x x)))
(square 4)                                 ; 16

See also

defun · mapcar · function

setq

(setq symbol expression [symbol expression]...)

Evaluates each expression and assigns it to the symbol before it, which is not evaluated. Returns the last value assigned.

Variables are global unless declared as local in the defun argument list after the slash. Assigning nil removes the value. Together with cons it builds lists step by step.

(setq a 10 b (* a 2))   ; 20

defun

(defun name (arguments [/ locals]) expression...)

Defines a function. The arguments and the local variables (after /) get their previous value back on exit; other variables are global. Returns the name. If the name starts with C:, the function becomes a command.

(defun double (x) (* 2 x))
(double 4)   ; 8

Local variables

What comes after / is local: on leaving the function it gets its previous value back. That way one routine does not overwrite the variables of another:

(setq a 1)
(defun f (/ a) (setq a 5))
(f)   ; 5
a     ; 1

New commands

With the C: prefix the function can be typed on the command line like any other command. It usually ends with (princ) so that the return value is not printed:

(defun c:hello () (princ "Hello") (princ))   ; C:HELLO

After that, just type HELLO. The .lsp file that contains it is loaded with APPLOAD or load.

Common mistakes

See also

lambda · load · setq · APPLOAD

defun-q

(defun-q name (arguments [/ locals]) expression...)

Same as defun.

Differences with AutoCAD

In AutoCAD the function is kept as a list and can be modified with defun-q-list-set; in LinceCAD it is a normal function.

if

(if test then [else])

Evaluates the test; if it is not nil, evaluates and returns then; if it is nil, else (or nil if missing).

Only one expression is allowed for then and for else: use progn to run several. Any value other than nil counts as true, including 0 and the empty string. There is no elseif; nest another if or use cond.

(if (> a 0) "positive" "zero or negative")

cond

(cond (test expression...) ...)

Evaluates the tests in order and, for the first one that is not nil, its expressions. Returns the last one evaluated (or the test, if the clause has no expressions). T as the last test gives the default case.

(cond ((< x 0) "negative") ((= x 0) "zero") (T "positive"))

while

(while test expression...)

Repeats the expressions while the test is not nil. Returns the value of the last expression evaluated.

Make sure the test can become nil, or the routine will not end; Esc stops a loop that never finishes. For a fixed number of iterations repeat is simpler, and foreach walks the items of a list.

(setq i 0)
(while (< i 3) (setq i (1+ i)))   ; 3

repeat

(repeat integer expression...)

Evaluates the expressions the given number of times. Returns the value of the last one.

The count is evaluated once, at the start. For loops that depend on a condition use while; to walk a list use foreach. A count of zero or less runs nothing.

(setq n 1)
(repeat 4 (setq n (* n 2)))   ; 16

foreach

(foreach symbol list expression...)

Gives the symbol each element of the list and evaluates the expressions. Returns the value of the last one; the symbol gets its value back at the end.

(foreach c '("A" "B") (princ c))   ; prints AB

What it is for

It goes through a list without keeping track of an index: the layer names in a list, the points of a polyline, the DXF pairs from entget… It does not work directly with a selection set: that is gone through with ssname.

More examples

(setq s 0)
(foreach n '(1 2 3) (setq s (+ s n)))   ; 6
(foreach n nil (princ n))               ; nil: with the empty list it does nothing

The symbol gets its value back at the end:

(setq x 5)
(foreach x '(1 2) (* x 10))   ; 20
x                             ; 5

Example

Print the distance from the origin to each point in a list:

(foreach p '((0 0) (3 4))
  (princ (distance '(0 0) p))
  (terpri))
; prints 0.0 and 5.0, one per line

Common mistakes

See also

mapcar · repeat · while

progn

(progn expression...)

Evaluates the expressions in order and returns the last one. Used to put several where one is expected, as in if.

It is also the way to give several steps to the branches of if and cond, or to make a block that returns a value. In a defun the body already behaves like an implicit progn.

(if ok (progn (princ "Done") (setq n (1+ n))))

and

(and expression...)

Evaluates the expressions until one is nil. Returns T if none is nil (also with no arguments) and nil otherwise.

Evaluation stops at the first nil, so later expressions are not run: this makes it handy as a guard, for example (and obj (cdr (assoc 8 (entget obj)))) only reads the layer when obj exists.

(and (> 3 1) "text")   ; T

or

(or expression...)

Evaluates the expressions until one is not nil. Returns T if it finds one and nil if all are nil.

Evaluation stops at the first non-nil expression, so it is a short-circuit test: (or (getenv "X") "default") supplies a default value. With no arguments it returns nil.

(or nil 5)   ; T

t

T

Symbol whose value is T: the "true" value of tests. Predicates that hold return it.

(= 1 1)   ; T

On the command line, a lone T is taken as a command alias; use T inside expressions. Any value that is not nil counts as true, but T is the one used by convention.

Example

Default case of cond:

(setq x 5)
(cond ((< x 0) "negative") (T "zero or positive"))   ; "zero or positive"

See also

cond · not · and

pi

pi

Variable holding the number π (3.14159…). It can be changed with setq, as in AutoCAD, though it is not a good idea.

(* 2 pi)   ; 6.28319

On the command line the bare symbol is not evaluated: type (princ pi) or use it inside an expression. Use it to convert degrees to radians, which is how AutoLISP angles are given.

Example

Convert 30° to radians:

(* 30 (/ pi 180))   ; 0.523599

See also

sin · cos · angtos

pause

pause

Variable whose value is "\\". In command it makes the command wait for the user to give that input.

(command "_circle" pause 10)

It can be used as many times as there are values the user has to give. While the command waits, the user can pick with the mouse, with object snaps, or type the value.

Example

A circle of radius 10 at the point the user gives:

(command "_circle" pause 10)

The command asks for the center; answering 30,30 places the circle at (30.0 30.0 0.0).

See also

command · getpoint

error

(defun *error* (message) ...)

Function the program calls when a routine fails, with the error message, before unwinding the stack: the local variables of the running functions still have their values. Used to restore variables and close files. Without *error*, the program writes ; error: message.

(defun *error* (msg)
  (if (/= msg "Function cancelled") (princ (strcat "\nError: " msg)))
  (princ))

+

(+ [number number]...)

Adds the numbers. Integer if all are integers (32-bit, overflowing as in AutoCAD); real if any is real. With no arguments, 0.

(+ 1 2 3)     ; 6
(+ 1 2.5)     ; 3.5

-

(- [number number]...)

Subtracts the others from the first. With a single number, changes its sign. With no arguments, 0.

(- 10 3 2)   ; 5
(- 4)        ; -4

With integers the result is an integer; if there is any real, a real ((- 10 2.5) gives 7.5). Use it to subtract coordinates, compute clearances or change the sign of a distance.

Example

Height difference between two points:

(setq p1 '(0 0 12.5) p2 '(5 5 10))
(- (caddr p1) (caddr p2))   ; 2.5

See also

+ · 1- · abs

*

(* [number number]...)

Multiplies the numbers. Integer if all are integers. With no arguments, 0.

(* 2 3 4)   ; 24

Use it to scale measurements, compute areas or convert units. With a real among the arguments the result is real ((* 2 1.5) gives 3.0). Integers are 32-bit, as in AutoCAD, and overflow silently: (* 100000 100000) gives 1410065408; if you expect large numbers, write one of them as a real (100000.0).

Example

Area of a 12.5 by 4 rectangle and twice that area:

(setq area (* 12.5 4))   ; 50.0
(* area 2)               ; 100.0

See also

+ · / · expt · float

/

(/ [number number]...)

Divides the first by the others. Between integers, the result is an integer and is truncated; with any real, real. Dividing by zero gives the error divide by zero.

(/ 7 2)     ; 3
(/ 7 2.0)   ; 3.5

1+

(1+ number)

Adds 1 to the number.

(1+ 5)   ; 6

Returns a number of the same type as the argument ((1+ 2.5) gives 3.5). It is the usual way to advance the counter of a loop with repeat or while.

Example

Count from 0 to 3:

(setq i 0)
(while (< i 3) (setq i (1+ i)))   ; 3

See also

1- · + · repeat

1-

(1- number)

Subtracts 1 from the number.

(1- 5)   ; 4

Returns a number of the same type as the argument ((1- 0.5) gives -0.5). Use it to walk a list or a selection set backwards, from the last index down to 0.

Example

Index of the last element of a list:

(setq l '(a b c))
(nth (1- (length l)) l)   ; C

See also

1+ · - · nth

abs

(abs number)

Absolute value, of the same type as the number.

(abs -2.5)   ; 2.5

An integer gives an integer and a real gives a real ((abs -7) gives 7). Use it to compare differences regardless of sign, for example to know whether two coordinates are within a tolerance.

Example

Are two X values less than 0.001 apart?

(setq x1 10.0004 x2 10.0)
(< (abs (- x1 x2)) 0.001)   ; T

More examples

(abs 0)      ; 0
(abs -0.5)   ; 0.5

Common mistakes

See also

- · equal · minusp

fix

(fix number)

Integer part of the number, truncated toward zero, as an integer. If it does not fit in 32 bits, returns a truncated real.

Unlike rounding, it never goes away from zero: (fix -3.7) is -3. To round to the nearest integer add 0.5 for positive values, for example (fix (+ x 0.5)). A string or nil is an error.

(fix 3.7)    ; 3
(fix -3.7)   ; -3

float

(float number)

The number as a real.

(float 3)   ; 3.0

Use it to avoid integer division: (/ 7 2) gives 3, but (/ (float 7) 2) gives 3.5. A real is returned unchanged. For the opposite, dropping decimals, use fix.

Example

Average of a list of integers:

(setq l '(3 4 4))
(/ (float (apply '+ l)) (length l))   ; 3.66667

See also

fix · / · atof

rem

(rem number number...)

Remainder of dividing the first by the others, from left to right. Integer if all are integers.

The sign of the result follows the first number: (rem -7 2) is -1. For a modulo that is always positive add the divisor and apply rem again. Dividing by zero is an error.

(rem 17 5)     ; 2
(rem 7.5 2)    ; 1.5

gcd

(gcd integer integer)

Greatest common divisor of two integers.

(gcd 12 18)   ; 6

The sign does not matter ((gcd -12 18) gives 6) and (gcd 0 5) gives 5. Use it to simplify ratios, for example a 50:100 scale to 1:2.

Example

Simplify a scale:

(setq a 50 b 100 m (gcd a b))
(list (/ a m) (/ b m))   ; (1 2)

See also

rem · /

min

(min number...)

The smallest of the numbers. If any is real, the result is real.

(min 3 1 2)     ; 1
(min 3 1.0)     ; 1.0

It needs at least one number; with no arguments it gives too few arguments. With apply it works on lists, for example for the lower left corner of a point cloud.

Example

Smallest Y coordinate of some points:

(setq pts '((0 4) (12 3) (7 9)))
(apply 'min (mapcar 'cadr pts))   ; 3

See also

max · apply · <

max

(max number...)

The largest of the numbers. If any is real, the result is real.

(max 3 1 2)   ; 3

It needs at least one number; with no arguments it gives too few arguments. With apply it works on lists: (apply 'max '(3 7 2)) gives 7.

Example

Largest X coordinate of some points:

(setq pts '((0 0) (12 3) (7 9)))
(apply 'max (mapcar 'car pts))   ; 12

See also

min · apply · >

sqrt

(sqrt number)

Square root, as a real. A negative number is an error.

(sqrt 16)   ; 4.0

It always returns a real, also with integers ((sqrt 16) gives 4.0). A negative number gives the error floating-point exception.

Example

Diagonal of a 3 by 4 rectangle:

(sqrt (+ (* 3 3) (* 4 4)))   ; 5.0

See also

expt · distance · abs

expt

(expt base exponent)

Power. If both are integers, the result is an integer; with a negative integer exponent it gives 0, like integer division ((expt 2 -1) gives 0). If either is a real, the result is a real: (expt 2.0 -1) gives 0.5. Integers overflow beyond 2147483647.

(expt 2 10)    ; 1024
(expt 2 0.5)   ; 1.41421
(expt 2.0 3)   ; 8.0

Example

Area of a circle of radius 3:

(* pi (expt 3 2))   ; 28.2743

More examples

(expt 10 3)    ; 1000
(expt 4 0.5)   ; 2.0: with exponent 0.5 it is the square root
(expt -8 2)    ; 64
(expt 1.5 2)   ; 2.25
(expt 2 30)    ; 1073741824

Common mistakes

See also

sqrt · exp · *

exp

(exp number)

e raised to the number, as a real.

(exp 1)   ; 2.71828

It is the inverse of log: (log (exp 2)) gives 2.0. (exp 0) gives 1.0. Use expt for any other base.

Example

Continuous growth factor of 5 % over 10 periods:

(exp (* 0.05 10))   ; 1.64872

See also

log · expt · sqrt

log

(log number)

Natural logarithm of a positive number, as a real.

(log 10)   ; 2.30259

With 0 or a negative number it gives the error floating-point exception. For the base-10 logarithm divide by (log 10).

Example

Base-10 logarithm of 1000:

(/ (log 1000) (log 10))   ; 3.0

See also

exp · expt · sqrt

sin

(sin angle)

Sine of an angle in radians.

(sin (/ pi 2))   ; 1.0

The result is always a real between -1 and 1 ((sin 0) gives 0.0). Angles are in radians: 30° is (/ pi 6).

Example

Rise of a 10 m ramp at 30°:

(* 10 (sin (/ pi 6)))   ; 5.0

See also

cos · atan · polar

cos

(cos angle)

Cosine of an angle in radians.

(cos 0)   ; 1.0

The result is always a real between -1 and 1 ((cos pi) gives -1.0). AutoLISP angles are in radians: convert degrees with (* degrees (/ pi 180)).

Example

Horizontal projection of a 10-unit segment at 60°:

(* 10 (cos (/ pi 3)))   ; 5.0

See also

sin · atan · polar

atan

(atan y [x])

Arctangent in radians. With one number, between -π/2 and π/2; with two, the angle of y/x in the right quadrant, between -π and π.

(atan 1)      ; 0.785398
(atan 1 -1)   ; 2.35619

~

(~ integer)

Bitwise NOT of the integer.

(~ 5)   ; -6

It is the same as changing the sign and subtracting 1 ((~ 7) gives -8). Combined with logand it turns bits off.

Example

Remove bit 32 (Intersection) from OSMODE:

(setvar "OSMODE" (logand (getvar "OSMODE") (~ 32)))

See also

logand · logior · lsh

logand

(logand [integer]...)

Bitwise AND of the integers. Useful to test a bit of a value such as OSMODE.

(logand 7 12)   ; 4

With no arguments it gives 0. With a single-bit mask it tells whether that bit is on: the result is the bit or 0. This is how variables made of bit sums, such as OSMODE or AUTOSNAP, are read.

Example

Is the Intersection object snap (32) on?

(logand (getvar "OSMODE") 32)   ; 32 if it is on, 0 if not

See also

logior · ~ · getvar

logior

(logior [integer]...)

Bitwise inclusive OR of the integers.

(logior 1 4)   ; 5

With no arguments it gives 0. Use it to turn bits on without touching the others, for example to add an object snap mode to the ones OSMODE already has.

Example

Turn on Endpoint (1) and Intersection (32) on top of the current ones:

(setvar "OSMODE" (logior (getvar "OSMODE") 1 32))

See also

logand · ~ · setvar

lsh

(lsh integer bits)

Shifts the bits of the integer to the left (or to the right, if bits is negative).

(lsh 1 4)    ; 16
(lsh 16 -2)  ; 4

It works with 32-bit integers: (lsh 1 31) gives -2147483648 and, to the right, zeros come in ((lsh -1 -1) gives 2147483647). Shifting one bit is the same as multiplying or dividing by 2.

Example

Value of bit number 5:

(lsh 1 5)   ; 32

See also

logand · logior · ~

=

(= value value...)

T if all the values are equal. Compares numbers (an integer and a real are equal if their values are) or strings.

(= 2 2.0)       ; T
(= "a" "A")     ; nil

/=

(/= value value...)

T if each value is different from the next one. Numbers or strings.

(/= 1 2)   ; T

With more than two values it only compares each one with the next one: (/= 1 2 1) gives T even though the first and last are equal. Strings are case-sensitive ((/= "a" "A") gives T). To compare reals with a tolerance use equal.

Example

Warn if the current layer is not layer 0:

(if (/= (getvar "CLAYER") "0") (princ "\nNot on layer 0."))

See also

= · equal · not

<

(< value value...)

T if each value is less than the next one. Numbers or strings (by character order).

(< 1 2 3)       ; T
(< "abc" "abd") ; T

With more than two values it checks that they are in increasing order: (< 1 3 2) gives nil. Strings are compared by the code of each character, so uppercase letters come before lowercase ones: (< "B" "a") gives T.

Example

Check that a radius is between 1 and 100:

(setq r 25)
(< 1 r 100)   ; T

See also

<= · > · min

>

(> value value...)

T if each value is greater than the next one.

(> 3 2 1)   ; T

Accepts numbers and strings, like <: strings are compared by character code ((> "b" "a") gives T). With more than two values it checks that they are in decreasing order.

Example

Pick the larger of two radii without max:

(setq r1 5 r2 8)
(if (> r1 r2) r1 r2)   ; 8

See also

>= · < · max

Download LinceCAD Manual contents

Download the free 30-day trial · Open and edit DWG files · AutoCAD LT alternative

Coming from AutoCAD? Check what is compatible · CAD with a perpetual license