Skip to content

AutoLISP (3 of 5)

vl-sort

(vl-sort list function)

The list sorted with the comparison function, which receives two elements and returns non-nil if the first one goes first.

(vl-sort '(3 1 2) '<)   ; (1 2 3)

Differences with AutoCAD

The sort is stable and keeps repeated elements (AutoCAD may remove them).

More examples

(vl-sort '(3 1 2) '>)                ; (3 2 1)
(vl-sort '("b" "A" "a") '<)          ; ("A" "a" "b"): capitals come first
(vl-sort '(2 1 2) '<)                ; (1 2 2)
(vl-sort nil '<)                     ; nil

Example

Sort points by their X with your own function:

(vl-sort '((5 1) (2 7) (9 0))
         '(lambda (p q) (< (car p) (car q))))
; ((2 7) (5 1) (9 0))

In the same way, dotted pairs are sorted by their key, (car a), or texts regardless of case by comparing (strcase a) and (strcase b).

Common mistakes

See also

lambda · mapcar · strcase

vl-remove-if

(vl-remove-if function list)

The list without the elements for which the function returns non-nil.

(vl-remove-if 'minusp '(1 -2 3))   ; (1 3)

The function can be a quoted symbol or a lambda. It is the opposite of vl-remove-if-not.

Example

Remove the values greater than 2:

(vl-remove-if '(lambda (x) (> x 2)) '(1 2 3 4))   ; (1 2)

See also

vl-remove-if-not · vl-remove · lambda

vl-remove-if-not

(vl-remove-if-not function list)

Only the elements for which the function returns non-nil.

(vl-remove-if-not 'numberp '(1 "a" 2))   ; (1 2)

It works as a filter: it keeps what meets the condition. The function can be a quoted symbol or a lambda.

Example

The strings of a list:

(vl-remove-if-not '(lambda (x) (eq (type x) 'STR)) '(1 "a" 2 "b"))   ; ("a" "b")

See also

vl-remove-if · vl-member-if · lambda

vl-some

(vl-some function list [list]...)

Applies the function to the elements of the lists, in parallel, and returns the first result that is not nil (or nil).

It stops at the first non-nil result, so it is the efficient way to ask whether any element satisfies a test. With several lists, the shortest one decides how many pairs are examined.

(vl-some '> '(1 5) '(2 3))   ; T

vl-every

(vl-every function list [list]...)

T if the function returns non-nil for all the elements of the lists.

(vl-every 'numberp '(1 2.5))   ; T

With an empty list it gives T. With several lists it calls the function with one element of each: (vl-every '< '(1 2) '(3 4)) checks 1 < 3 and 2 < 4. It stops at the first element that fails.

Example

Are all the coordinates of a point numbers?

(vl-every 'numberp '(1 2.5))     ; T
(vl-every 'numberp '(1 "a"))     ; nil

See also

vl-some · mapcar · vl-member-if

vl-member-if

(vl-member-if function list)

The rest of the list from the first element for which the function returns non-nil, or nil.

It is the predicate version of member: instead of an item it takes a test function, which may be a lambda. Use vl-position to know the index and vl-some to know only whether any item passes.

(vl-member-if 'minusp '(1 -2 3))   ; (-2 3)

acad_strlsort

(acad_strlsort list-of-strings)

The list of strings sorted alphabetically, ignoring case.

(acad_strlsort '("b" "A" "c"))   ; ("A" "b" "c")

The order is by character, not natural: "Floor 10" comes before "Floor 2". All elements must be strings; any other type gives bad argument type: stringp. To sort other values use vl-sort.

Example

Names sorted to show them on the command line:

(acad_strlsort '("Floor 10" "Floor 2" "elevation"))
; ("elevation" "Floor 10" "Floor 2")

See also

vl-sort · strcase · tblnext

mapcar

(mapcar function list [list]...)

List with the result of applying the function to each element (with several lists, to the elements at the same position, up to the shortest one).

(mapcar '+ '(1 2) '(10 20))   ; (11 22)

apply

(apply function list)

Calls the function with the elements of the list as arguments.

(apply 'max '(3 7 2))   ; 7

The function can be a quoted symbol ('max), a user function or a lambda. It only takes two arguments: (apply '+ 1 '(2 3)) gives too many arguments. It is the way to apply a function to a list whose size is not known in advance.

Example

Add up and join the elements of lists:

(apply '+ '(1 2 3))              ; 6
(apply 'strcat '("a" "b" "c"))   ; "abc"

See also

mapcar · eval · lambda

eval

(eval expression)

Evaluates the value of the expression: used to run a list built by the routine.

(eval '(+ 1 2))   ; 3

Together with read it runs an expression written in a string: (eval (read "(+ 2 3)")) gives 5. A quoted symbol evaluates to its value ((eval 'pi) gives 3.14159).

Example

Build a call and run it:

(setq expr (list '* 3 4))   ; (* 3 4)
(eval expr)                 ; 12

See also

read · apply · quote

set

(set symbol expression)

Like setq, but the symbol is evaluated too: the variable can be chosen at run time.

(set (read "radius") 5)   ; radius is 5

It returns the value assigned. With a quoted symbol ((set 'b 7)) it does the same as setq. Use it to give values to variables whose name the routine builds.

Example

Create the variables p1, p2 and p3:

(foreach i '(1 2 3) (set (read (strcat "p" (itoa i))) (* i 10)))
(list p1 p2 p3)   ; (10 20 30)

See also

setq · read · vl-symbol-value

read

(read [string])

Reads the first expression of the string and returns it without evaluating it. With no argument, nil.

(read "(1 2)")   ; (1 2)
(read "abc")     ; ABC

If the string has several expressions, it only reads the first one ((read "1.5 2") gives 1.5). Strings in quotes are read as strings. With eval it runs an expression written in a text.

Example

(eval (read "(+ 2 3)"))   ; 5

See also

eval · atoi · read-line

vl-symbol-name

(vl-symbol-name symbol)

Name of the symbol as a string, in uppercase.

(vl-symbol-name 'radius)   ; "RADIUS"

Symbols are stored in uppercase, so the name comes out in uppercase even if it was typed in lowercase. It is the inverse of using read on a string.

Example

(strcat "Variable " (vl-symbol-name 'radius))   ; "Variable RADIUS"

See also

vl-symbol-value · read · vl-symbolp

vl-symbol-value

(vl-symbol-value symbol)

Value of the symbol.

(vl-symbol-value 'pi)   ; 3.14159

A symbol with no value gives nil. It is the same as eval of a quoted symbol.

Example

Read variables whose name the routine builds:

(setq p1 10)
(vl-symbol-value (read (strcat "p" "1")))   ; 10

See also

set · vl-symbol-name · boundp

strcat

(strcat [string]...)

Joins the strings. With no arguments, "".

(strcat "Layer " "0")   ; "Layer 0"

All arguments must be strings: (strcat "a" 1) gives bad argument type: stringp. Convert numbers first with itoa or rtos.

Example

A message with a number:

(strcat "There are " (itoa 12) " columns")   ; "There are 12 columns"

More examples

(strcat)                                  ; ""
(strcat "L = " (rtos 15.5 2 2) " m")      ; "L = 15.50 m"
(strcat "Line" "\n" "two")                ; two lines when printed with princ

Common mistakes

See also

itoa · rtos · substr

strlen

(strlen [string]...)

Number of characters of the string (with several, the sum).

(strlen "Lince")   ; 5

It counts characters, not bytes: (strlen "ñ") gives 1. With no arguments it gives 0.

Example

Pad with zeros up to three digits:

(setq s "7")
(while (< (strlen s) 3) (setq s (strcat "0" s)))   ; "007"

See also

substr · strcat · vl-string-elt

substr

(substr string start [length])

Part of the string from position start (the first one is 1), with the given length or up to the end.

(substr "LinceCAD" 6)     ; "CAD"
(substr "LinceCAD" 1 5)   ; "Lince"

More examples

(substr "abc" 2 10)   ; "bc": if the length goes too far, it stops at the end
(substr "abc" 10)     ; "": if the start goes too far, an empty string
(substr "abc" 1 0)    ; ""

Example

Split a file name into name and extension:

(setq a "plan.dwg")
(substr a (- (strlen a) 2))       ; "dwg"
(substr a 1 (- (strlen a) 4))     ; "plan"

To cut at the last backslash or the last dot, find its position with vl-string-position.

Common mistakes

See also

strlen · strcat · vl-string-position · vl-string-search

strcase

(strcase string [lowercase])

The string in uppercase; if the second argument is not nil, in lowercase.

(strcase "Wall")     ; "WALL"
(strcase "Wall" T)   ; "wall"

It also changes accented letters: (strcase "ñandú") gives "ÑANDÚ". It only accepts strings. Use it to compare answers regardless of case.

Example

Accept «y», «Y» or «Yes»:

(setq r "yes")
(= (substr (strcase r) 1 1) "Y")   ; T

See also

wcmatch · substr · acad_strlsort

itoa

(itoa integer)

The integer as a string.

(itoa 25)   ; "25"

It only accepts integers: (itoa 2.5) gives bad argument type: fixnump. To write reals use rtos. It is mostly used to build messages with strcat.

Example

Number parts on the command line:

(defun c:number (/ i)
  (setq i 1)
  (repeat 3 (princ (strcat "\nPart " (itoa i))) (setq i (1+ i)))
  (princ))

NUMBER writes Part 1, Part 2 and Part 3.

See also

atoi · rtos · strcat

atoi

(atoi string)

Integer written at the start of the string (0 if there is none).

(atoi "12abc")   ; 12

It reads a sign and the digits at the start and stops at the first character that is not one: (atoi "-3.7") gives -3 and (atoi "1e3") gives 1. Leading spaces are allowed. For numbers with decimals use atof.

Example

Number in a sheet name:

(atoi "12abc")   ; 12
(atoi "abc")     ; 0

See also

atof · itoa · distof

atof

(atof string)

Real written at the start of the string (0.0 if there is none). The decimal separator is the point.

A decimal comma is not understood: replace it first with vl-string-translate. To read integers use atoi.

(atof "2.5 m")   ; 2.5

chr

(chr code)

Character with that (Unicode) code, as a string.

(chr 65)   ; "A"

It accepts any Unicode code: (chr 241) gives "ñ" and (chr 8364) gives "€". It is the inverse of ascii. Use it to write characters that are hard to type, such as double quotes inside a string ((chr 34)).

Example

Text with the degree sign:

(strcat "45" (chr 176))   ; "45°"

See also

ascii · vl-list->string · strcat

ascii

(ascii string)

Code of the first character of the string (0 if it is empty).

(ascii "A")   ; 65

It returns the Unicode code of the character, so accented letters have their own code too: (ascii "ñ") gives 241 and (ascii "Árbol") gives 193. It is the inverse of chr.

Example

Does an answer start with a digit?

(setq c (ascii "7 pieces"))
(and (>= c 48) (<= c 57))   ; T

See also

chr · vl-string->list · vl-string-elt

wcmatch

(wcmatch string pattern)

T if the string matches the pattern. Wildcards: * (any text), ? (one character), # (one digit), @ (one letter), . (one non-alphanumeric character), [abc] and [a-z] (one of them), [~abc] (none of them), ~ at the start negates the pattern, , separates several patterns and the reverse quote makes the next character literal. It is case-sensitive.

(wcmatch "DIM-12" "DIM-##")   ; T
(wcmatch "Wall" "W*,P*")      ; T

What it is for

It is the way to compare names with wildcards: layers starting with WALL, numbered blocks, texts with a digit… The same wildcards work in the strings of ssget filters.

More examples

(wcmatch "wall" "W*")             ; nil (case-sensitive)
(wcmatch (strcase "wall") "W*")   ; T
(wcmatch "A-01" "~A*")            ; nil (~ negates the pattern)
(wcmatch "AXES" "[A-E]*")         ; T
(wcmatch "A*B" "*`**")            ; T (`* is a literal asterisk)
(wcmatch "A-1" "A.#")             ; T (. is a non-alphanumeric character)

Example

Keep the wall layers from a list of names:

(vl-remove-if-not '(lambda (c) (wcmatch c "WALL*"))
                  '("WALLS" "WALL-EXT" "DIMS" "AXES"))
; ("WALLS" "WALL-EXT")

Common mistakes

See also

strcase · ssget · vl-string-search · vl-remove-if-not

vl-string-trim

(vl-string-trim characters string)

The string without the given characters at the start and at the end.

(vl-string-trim " " "  hello  ")   ; "hello"

The first argument is a set of characters; the ones in the middle are kept ((vl-string-trim "*" "**A*B**") gives "A*B"). It is handy to clean up what the user types or what is read from a file.

Example

(vl-string-trim " \t" "\t  Floor 1  ")   ; "Floor 1"

See also

vl-string-left-trim · vl-string-right-trim · read-line

vl-string-left-trim

(vl-string-left-trim characters string)

The string without the given characters at the start.

(vl-string-left-trim "0" "0042")   ; "42"

The first argument is a set of characters, not a word: all the characters in it are removed, in any order ((vl-string-left-trim " \t" " x ") gives "x ").

Example

(vl-string-left-trim "0" "0042")   ; "42"

See also

vl-string-right-trim · vl-string-trim · substr

vl-string-right-trim

(vl-string-right-trim characters string)

The string without the given characters at the end.

(vl-string-right-trim ".0" "2.500")   ; "2.5"

The first argument is a set of characters: with ".0" the trailing zeros and dot of a number are removed ("2.000" gives "2").

Example

A number without trailing zeros:

(vl-string-right-trim ".0" "2.500")   ; "2.5"

See also

vl-string-left-trim · vl-string-trim · rtos

vl-string-subst

(vl-string-subst new pattern string [start])

The string with the first occurrence of the pattern (from start) replaced by new.

Only the first occurrence is replaced. To replace all of them, call it in a loop starting after each replacement. The original string is not modified: the result is a new string.

(vl-string-subst "B" "A" "AXA")   ; "BXA"

vl-string-translate

(vl-string-translate from to string)

Replaces each character of from found in the string by the one at the same position in to.

The two character sets must be given in the same order. Typical use is changing a decimal comma into a point before atof, as in the example.

(vl-string-translate "," "." "2,5")   ; "2.5"

vl-string-position

(vl-string-position code string [start [from-end]])

Position (from 0) of the character with that code, or nil. With a non-nil fourth argument, finds the last occurrence.

Codes are character codes, obtained with ascii. It is useful to split a path at the last backslash or slash, for example to separate the file name from its folder, together with substr.

(vl-string-position (ascii "\\") "C:\\dwg\\a.dwg" nil T)   ; 6

More examples

(vl-string-position (ascii "a") "banana")     ; 1
(vl-string-position 97 "banana" 2)            ; 3, searching from position 2
(vl-string-position 122 "banana")             ; nil

Example

Split a path into folder and name (92 is the code of the backslash):

(setq r "C:\\dwg\\a.dwg" i (vl-string-position 92 r nil T))   ; 6
(substr r 1 i)       ; "C:\\dwg"
(substr r (+ i 2))   ; "a.dwg"

Positions count from 0 and substr from 1: that is why the name starts at i + 2.

Common mistakes

See also

vl-string-search · substr · ascii

vl-string->list

(vl-string->list string)

List with the codes of the characters of the string.

(vl-string->list "AB")   ; (65 66)

It gives Unicode codes ((vl-string->list "ñ") gives (241)); an empty string gives nil. It is the inverse of vl-list->string.

Example

Count the letters A in a string:

(length (vl-remove-if-not '(lambda (c) (= c 65)) (vl-string->list "ABRACADABRA")))   ; 5

See also

vl-list->string · ascii · vl-string-elt

vl-list->string

(vl-list->string list-of-codes)

String with the characters of those codes.

(vl-list->string '(76 50))   ; "L2"

It accepts Unicode codes ((vl-list->string '(241)) gives "ñ"). With nil it gives the empty string. It is the inverse of vl-string->list.

Example

Remove the spaces from a string:

(vl-list->string (vl-remove 32 (vl-string->list "A 1 B")))   ; "A1B"

See also

vl-string->list · chr · strcat

vl-string-elt

(vl-string-elt string position)

Code of the character at that position (from 0).

(vl-string-elt "AB" 1)   ; 66

A position outside the string gives the error bad argument value. To take a piece as a string use substr, which starts counting at 1.

Example

(chr (vl-string-elt "Lince" 0))   ; "L"

See also

substr · ascii · vl-string->list

vl-princ-to-string

(vl-princ-to-string expression)

The expression as princ writes it, in a string.

(vl-princ-to-string '(1 "a"))   ; "(1 a)"

Numbers use the format of princ ((vl-princ-to-string 2.5) gives "2.5") and inner strings lose their quotes. Use it to put any value in a message with strcat.

Example

(strcat "Point: " (vl-princ-to-string '(10 5)))   ; "Point: (10 5)"

See also

vl-prin1-to-string · princ · strcat

vl-prin1-to-string

(vl-prin1-to-string expression)

The expression as prin1 writes it (strings in quotes), in a string.

Useful to store values in text files or in messages while keeping their type: the quotes and escapes of strings are kept. Use vl-princ-to-string to get the plain text without quotes.

(vl-prin1-to-string "a")   ; "\"a\""

angtos

(angtos angle [mode [precision]])

An angle in radians as text. Modes: 0 degrees, 1 degrees/minutes/seconds, 2 grads, 3 radians, 4 surveyor's units. Without mode or precision, AUNITS and AUPREC are used.

(angtos (/ pi 4) 0 2)   ; "45.00"
(angtos (/ pi 4) 1 4)   ; "45d0'0\""

angtof

(angtof string [mode])

Reads an angle written in any angtos format (the d, g and r marks and bearings are recognized on their own; without a mark, according to the mode, degrees by default). Returns radians, or nil.

(angtof "90")   ; 1.5708

distance

(distance point point)

Distance between two points (2D or 3D).

(distance '(0 0) '(3 4))   ; 5.0

The result is always a real. If one of the points only has X and Y, its Z is 0: (distance '(0 0) '(3 4 12)) gives 13.0. To write it in the drawing units use rtos.

Example

A command that measures between two picked points:

(defun c:measure (/ a b)
  (setq a (getpoint "\nFirst point: ")
        b (getpoint a "\nSecond point: "))
  (princ (strcat "\nDistance: " (rtos (distance a b) 2 2)))
  (princ))

With the points 0,0 and 30,40 it writes Distance: 50.

See also

angle · polar · getdist · DIST

angle

(angle point point)

Angle in radians of the line from the first point to the second, measured from the X axis counterclockwise (from 0 to 2π).

The result is always between 0 and 2π, whatever the order of the points; swapping them adds or subtracts π. Only the X and Y coordinates are used. To convert to degrees multiply by 180 and divide by pi.

(angle '(0 0) '(0 5))   ; 1.5708

polar

(polar point angle distance)

Point at that distance from the given one, in the direction of the angle (radians).

(polar '(0 0) 0 10)   ; (10.0 0.0 0.0)

The result always has three coordinates; Z is that of the starting point ((polar '(1 2 3) pi 1) gives (0.0 2.0 3.0)). The angle is measured from the X axis, counterclockwise.

Example

Draw a 10-unit line at 30°:

(command "_line" '(0 0) (polar '(0 0) (/ pi 6) 10) "")

The line ends at (8.66025 5.0 0.0).

See also

angle · distance · inters

inters

(inters p1 p2 p3 p4 [on-segments])

Intersection point of line p1-p2 with line p3-p4, or nil if they are parallel. If the fifth argument is nil, the lines are extended; if it is missing or not nil, the point must lie on both segments.

(inters '(0 0) '(10 10) '(0 10) '(10 0))   ; (5.0 5.0 0.0)

getpoint

(getpoint [base-point] [prompt])

Asks for a point: a click (with object snaps) or typed coordinates. With a base point, shows a rubber band from it. Returns the point, nil on Enter, or the initget keyword.

As in AutoCAD, the base point and the returned one are in the current UCS, like those of getcorner, getdist, getangle, entsel, ssget and command. entmake and entget work in world coordinates: convert the point with (trans p 1 0) before entmake.

With a tilted UCS (rotated about X or Y), the click is taken on the XY plane of the UCS, along the viewing direction, as in AutoCAD; typed coordinates are UCS coordinates (3,4 gives (3.0 4.0 0.0)). @dx,dy is relative to the base point or, without one, to LASTPOINT; @ alone is that same point.

(setq p (getpoint "\nSpecify center: "))

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 · AutoCAD LT alternative