AfraLisp Blog

The AutoLisp/Visual Lisp/VBA Resource Website

AfraLisp Blog

Home Newsletter Utter Rubbish Coding Tips AutoCAD Tips Contact Downloads WAUN

AutoLisp Quick Start

This tutorial is aimed at the AutoCAD users who would like to start learning AutoLisp.
I suggest that you go through this tutorial along with the AutoCAD Programmers Reference Guide. You can then lookup the relevant AutoLisp commands for a more detailed explanation. Hope this helps you and Good Luck in your Lisping - Kenny Ramage


All AutoLisp programs must contain the suffix LSP otherwise AutoCAD will not access them when loading. (eg. CHTEXT.LSP).
Use a simple text processor such as Notepad to create and edit your lisp files.

FUNCTION (Which is simply the program)

Is a pre-defined set of instructions that describes a set of actions that AutoLisp is to perform, divided into three sections :

  • OPERATOR - Getting input.
  • ARGUMENT - Manipulating the input.
  • COMMAND - Using the manipulated input.


Draw out or write out in English what you want your program to do.


These are like empty boxes in which to store data, to be used later. In AutoLisp, variables may be a collection of letters or numbers as long as they begin with the letters.
Example of legal variables are as follows :

  • A
  • ARC1
  • POINT1
  • PNT1
  • D3

An AutoLisp variable may contain more than one value in a single variable. A value can be anything, such as :

  • Real number
  • String
  • Integer
  • Pickset

Therefore a variable can store just about anything.


Structure your program in such a way that it is easy to understand, by yourself and everyone else. e.g. Keep input statements together. Keep your arguments together. Keep your commands together. Track your work with the semicolon. When you begin a line with a semicolon, anything you write after will be ignored by AutoLisp. It is used for documentation and explanation of your program. Write notes about your program, what you are doing and what the variables are. A semicolon does not have to begin the line.

(prompt "This line will print"); This is a comment

From where the semicolon begins, the remainder of the line is a comment statement.


Parentheses are vital to writing AutoLisp programs. All commands are surrounded by parentheses. AutoLisp uses parentheses to nest, allowing you to write a command that acts on (evaluates) another command. In turn, that command can act on another. As you add parentheses, you're nesting commands become deeper and deeper. Remember to come out of the nest with an equal number of parentheses.

NOTE: Always close what you open.


In AutoLisp the name of the program or function must be defined in the first statement, which is done by using the command


Defun is the first actual command and is followed by the name of the function (or program). There are different ways of starting a function for example :

  • (Defun drawline ()
  • (Defun drawline (/ pntl pnt2)
  • (Defun C:drawline ()
  • (Defun C:drawline (/ pntl pnt2)
  • (Defun drawline (a / pntl pnt2)

The first way, you are saying that all variables you plan to use in the program are GLOBAL, which are variables that do not lose their value after the program ends.

The second way, you are saying that the variables you are using are LOCAL variables, which are variables that have value only for that program and while that program is running.

The third way, as the first BUT the C: tells AutoCAD to treat that function as any other AutoCAD command.

The fourth way, as the second, but an AutoCAD command.

The last, variable a receives the first value to it from outside the program.


Integers - Is a number ranging between -32768 and +32767 without decimal points eg: 1

Reals - Is a number with a decimal point eg: 1.0

Strings - Strings can be up to a maximum length of 100 characters.

Lists - A list is a variable that has more than one element. A point in your drawing is described by the value of the X co-ordinate and the value of the Y co-ordinate. In AutoLisp, that point can be described by a single variable, a list of two elements, the first being the X value and the second being the Y value eg :

  • ( 7 10 )
  • ( 5 9 7 2 )
  • (1.5 2.3 4.9 )

Atoms - If a variable has only one value it is an atom e.g. 6

The Type command - will return the data type of variable.

Input Commands -(Getting info from the user)

  • getpoint - Needs you to pick a point on the screen.
  • getint - Needs a Integer eg: 1
  • getreal - Needs a real number eg: 10.00
  • getcorner - Needs a second point on the screen and draws an elastic window from a previous point.
  • getstring - Needs a string of text.
  • getdist - Needs a value either by picking on the screen or a number from the keyboard.
  • getangle - Needs an angle either by pointing on the screen or by typing an angle, which will be returned in radians.
  • getkword - Needs a keyword from the user.
  • getvar - Will get the value of an AutoCAD system variable.
  • initget - Establishes various options for use by the next GET XXX function.
  • getorient - Is similar to the GETANGLE but is affected by the zero-degree base and the direction. It will always give you the absolute angle using 0 degree base as EAST.

Input Command Examples

1. (getpoint "\n Pick POINT on the screen:")

  • \n - The n MUST always be lower case, \n takes you to the next line.
  • Your prompt must always be between " " (Quotes), and after picking a point will return a value of that point.

2. (getint "\Enter your age :") Type a number and AutoLisp will return that number.

3. (getreal "\nEnter a number:") Type a real number eg 10.51 and AutoLisp will return that number.

4. (getcorner pntl "\n Pick second point:") Will create an elastic window from variable pntl.

5. (getstring 1 "\What is the day today ?:") Type some text and Autolisp will return that text.

6. (getdist "\nHow long is the line ?:") Pick two points or type a length and AutoLisp will return that length.

7. (getangle "\nWhat is the angle ?:") Pick two points for the angle or type an angle and AutoLisp will return that angle in radians.

8. (initget 1 "Yes No")

(getkword "\nAre you going? ("Yes or NO):") Initget will control the next getxxx function, getkword will accept only one word. The initget will accept that one word only to be Yes or No.

9. (getvar "FILLETRAD") Would return the set fillet radius eg : 0. 5

Initget Bits

  • 1 Disallow null input
  • 2 Disallow zero values
  • 4 Disallow negative values
  • 8 DO not check limits, even if LIMECHECK is on.
  • 16 Return 3D points rather than 2D points.
  • 32 Use dashed lines when drawing rubber band or box.
    (initget (+ 1 2 4))
    (getint "\nHow old are you?:") Will only accept an Integer eg : 21
  • 11. (getorient "\nWhat is the angle?:") Pick two points for the angle or type an angle and AutoLisp will return an angle in radians relative to 0 degrees at East.

Setq Command (Set Equal)

(Setq) is an assignment command, eg : it assigns the value of one variable or constant to another variable.

NOTE : (Setq) is the primary assignment command in AutoLisp. The = is not used as an assignment. = does exist but only as a non assignment statement. It does not have the ability to make one variable equal to another. (It is used for a comparison of variables, numbers or strings)

(setq a b)

This statement assigns the value of b to the variable a.

NOTE : The first variable after the (setq) is the one that receives the value.

Print Commands  

This command is used simply to print a message on the command line. A line feed (\n) is not included so two consecutive prompt commands will print both messages on the same line. Therefore , any printing after the prompt must have the (terpri) or \n command.


This is a line feed that causes the next printed text to appear on the next line. It generally follows the prompt command. eg:

  • (prompt "Hello, how are you ?")(terpri) or
  • (prompt "\nHello, how are you ?")  


This function prints the expression on the screen and returns the expression. It can be any expression and need not only be a string. The expression can also be written to a file and will appear in the file exactly as it would on the screen.

  • (prin1 "Hello") would print Hello
  • (prin1 a) would print the value of variable a
  • (prin1 a f) would print the value of variable a to an open file named in variable f  


Is the same as prinl except that the control characters ("") are not printed. Can also be used to print a blank line by using no statement after princ. 


Same as prinl except that a new line is printed before the expression and a space is printed after the expression. eg:

  • (print "Hello") would return "Hello" "Hello" 


This function sets an AutoCAD system variable to a given value and returns that value. The variable name must be in double quotes. eg:

  • (setvar "blipmode" 0) returns 0

Will switch blipmode off. 1 would switch it on again.

Doing arithmetic

  • (+ 1 1) returns 2
  • (- 2 1) returns 1
  • (* 2 2) returns 4
  • (/ 2 1) returns 2
  • (1+ 1) returns 2 (Incremented)
  • (1- 2) returns 1 (Decremented)



This function returns the point at an angle (in radians) and distance from a point.

  • (polar pntl angl distl)


Examines two lines and returns the point where they intersect even if they do not cross one another.

  • (inters pntl pnt2 pnt3 pnt4)

AutoCAD commands in AutoLISP

Any AutoCAD command can be used inside your lisp program BUT one must remember that they have to be used exactly as you would in AutoCAD and your RETURN is a double set of Quotes (""). eg:

(command "line" pnt1 pnt2 "") This will draw a line from pntl to pnt2 and the "" acts as a return to terminate your line command.

(graphscr) would return you to the graphics screen while the program is running.

(textscr) would return you to the text screen while the program is running.

Entities from a list

When you used (setq a (getpoint)) you assigned the X and Y coordinate numbers to variable a. That Variable now is a list that may look like (5 10). If you want to look at the list in variable a, AutoLISP gives you a convenient way to do that from the command line.

!a Placing the ! in front of the variable will display the value or values of the variable.  

(car) X COORDINATE (lst element)

The primary command for taking a list apart, (car) gives you the first element of the list. If the value of a is a list of: (5 10)

Then (setq b (car a)) would assign to the variable b the value of the first element in a which is 5.  


This is the secondary command for taking a list apart. (cdr) gives you the second and remaining elements of the list. If variable a is a list of

(2 5 7 9 11)

Then (setq b (cdr a)) would assign to variable b the second and remaining elements of the list in variable a

(5 7 9 11).

(cadr) Y COORDINATE (2nd element)

This always produces the second element of a list. Assuming a still has the list (5 10),

Then (setq b (cadr a)) would produce 10.

(caddr) Z COORDINATE (3rd element)

This always produces the third element of a list. Assuming a has a list (3 7 5)

Then (setq c (caddr a)) would produce 5.

This program draws a rectangle by pointing to two points.

(defun c:retan (/ pl p2 p3 p4)

(setq pl (getpoint "\nfirst corner of rectangle: "))

(setq p3 (getcorner "\nsecond corner of rectangle: "))

(setq p2 (list (car pl)(cadr p3)))

(setq p4 (list (car p3)(cadr pl)))

(command "line" pl p2 p3 p4 "c")



DTR converts degrees to radians.

(defun dtr (a)

(* pi (/ a 180)) )

RTD converts radians to degrees.

(defun rtd (a)

(/ (* a 180) pi)

Things to strings

strcase (string case)

Changes a string of text from lower case to upper case, leaving upper case characters as they are. eg:

(strcase "Hello") returns "HELLO"

(strcase a) returns the alphabetic characters in variable a from lower case to upper case.

strcat (string cat)

Returns two or more separate strings as one. eg:

(strcat "H" "ello") returns "Hello"

(strcat a b) returns two strings in variable a & b as one.

strlen (string length)

Returns the length, of the characters of a string. eg:

(strlen "hello") returns 5.

(strlen a) returns the length of a string in variable a.

substr (substitute string)

Returns a part of a string, from a specified position, ending either at the end or another specified position. eg:

(substr "Hello 2) returns "ello".

(substr "Hello 2 1) returns "e".

(substr "Hello" 3 2) returns "ll".

List Manipulation

The apostrophe ' serves a special function in AutoLISP, for example if a group of items is preceded by an apostrophe ' it is treated as a list. eg:

'(20 10 5) is treated as a list.


Returns an angle between two points in radians. To use that angle in AutoCAD you have to convert it back to decimal degrees. eg:

(setq a (angle pntl pnt2)) sets the angle between pntl and pnt2 to the variable a.
To use a:

(command "text" pntl "40" a t) The text command with a height of 40, rotation angle assigned to variable a and text to variable t. But a is not the correct rotation angle because it is in radians.


Takes any number of specified lists and joins them together as one list. eg:

(append '(10 20) '(30 40)) returns the list: (10 20 30 40).

(append a b) returns the list in variable a and the list in variable b as one.


Measures the distance from two known points. eg:

(setq distl (distance pntl pnt2)) returns the distance between pntl and pnt2 and assigns the distance to a variable called dist1.


Returns the number of elements in a list. eg:

(length '(a b c d)) returns 4.


Looks for a duplicate and returns that and the rest of the list eg:

(member 'c '(a b c d e)) returns (C D E).


Returns the nth element in a list, where n is the number of the element to return. (Zero is the first element). eg:

(nth 3 '(a b c d)) returns d.

Assoc (associative)

Often used with (subst) command, this command lets you search for a specific element, then assign that element to a variable.

Lets assume variable b is the list ((10 5.5 2.7)(40 5)) and you looking for a code 40. You want to pull out the entire element and assign it to a variable c. eg:

(setq c (assoc 40 b))

This assigns the entire element containing the 40 in the list b to variable c. Now c is

a list that looks like this: (40 5).

Subst (subsitute)

Allows you to substitute one aspect for another. When substituting ALWAYS substitute the new item for the old in the list. Now lets substitute 20 for the 5 in the variable c.

(setq bl (subst '(40 20) c b))

Now bl is the new list.

'(40 20) is the new element substituted for the old element (40 5) c, found in list b.

If you want to use a variable which represents the value.

(setq bl (subst '(40 h) c b)), It looks like it should work, but it does not. The new element will look like this: (40 h).

(subst) cannot interpret variables. You need to construct a new variable containing the entire list element, then use

the new variable in the (subst) command.

Cons (Construct)

Constructs a new list with the new element placed at the begining. Assume variable c contains the following list: (40 5).

Also, assume variable h contains the real number 20.0 then:

(setq d (cons (car c) h)) Remember (car c) gives you 40. Therefore, (car c) is the new first element, followed by the value h. Thus it produces a new list d (40 20.0).

Now we substitute:

(setq bl (subst d c b)) That substitutes the new element found in variable d for the old element found in variable c. (In the list found in variable b) and assigns the new list to bl.



Takes an angle in radians and converts it into a string, into a specific format.

Angtos Mode Format :

  • 0 Degrees
  • 1 Degrees/minutes/seconds
  • 2 Grads
  • 3 Radians
  • 4 Surveyor's units

Assuming variable a has an angle in radians. eg:

(angtos a 0 0) returns "180"

(angtos a 0 4) returns "180.0000"

(angtos a 1 4) returns "180d0"0""


This function returns the convention of a real number to an Integer. eg:

(fix 8) returns 8

(fix 8.6) returns 8


This function returns the convention of an Integer to a real number. (One can use either real or an Integer.) eg:

(float 8) returns 8.0000

(float 8.6) returns 8.6000


Returns the convention of the first character of a string into its ASCII character code. (An Integer) eg:

(ascii "a") returns 65

(ascii "BLUE") returns 66


Returns the convention of an Integer representing an ASCII character code into a single character string. eg:

(chr 65) returns "A"

(chr 66) returns "B"


Returns the convention of a string into a real number. eg:

(atof "9.3") returns 9.3000

(atof "2") returns 2.0000


Returns the convention of a real number to a string into a

specified format.

Rtos Mode Format

  • 1 Scientific
  • 2 Decimal
  • 3 Engineering(feet & decimal inches)
  • 4 Architectural(feet & fractional inches)
  • 5 Arbituary fractional units

The real number can be set according to mode and precision. eg:

(rtos 7.2 1 4) returns "7.200OE+00"

(rtos 7.2 2 2) returns "7.20"


Returns the convention of an Integer into a string. eg:

(itoa 25) returns "25"


Returns the convention of a string into an Integer. eg:

(itoi "25") returns 25



Is not an assignment command, only (setq) is. The (=) command is used only to test if items are equal. It does not make them equal.


(if) is the standard if-then-else statement. In AutoLISP you may only match one if statement with a then statement. eg:

(if (= a b) (setq b 5 (setq b 6))

If a is equal to b, then b will be assigned the value of 5.
If it is not then b will be assigned the value of 6.

Cond (Conditional)

This function accepts any number of lists as arguments. It evaluates the first item in each list (in order supplied) until one of these items are a value other than nil.

eg: A user's response string is variable s,


((= s "Y") 1)

((= s "y") 1)

((= s "N") 0)

((= s "n") 0)

(t nil)


This function tests the response and returns 1 if it is "Y" or "y", and 0 if it is "N" or "n", and nil otherwise.


Similar to a loop but repeat will only go through the commands as many times as is told. eg:

(setq a 10)

(setq b 100)

(repeat 4

(setq a (+ a 10))

(setq b (+ b 10))

Returns 140.


Is another loop control command available in AutoLISP. A loop is necessary if you want to repeat a command. However, unless you have a way of controlling it, the program will run forever and hang you up. eg:

(setq a "w")

Sets up the controlling variable to a value other than nil.

(while a

The loop will continue, begining with the commands that follow, until the variable a is set to nil.


(xxxx) Are the commands that are performed in the loop.

(if (= c d) (setq a nil))

Evaluates if c is equal to d, and if so, sets the loop controlling variable a to nil to end the loop.


While parenthesis closes loop, and program will continue with the commands after this.


An entity is the smallest object you can place on your screen. The following are entities: LINE, CIRCLE, ARC, TEXT, POLYLINES, etc. Entities are stored and referenced in the drawing database. They can be changed and manipulated using AutoLISP to suit your needs. Each entity has a massive definition in AutoCAD's database. eg: The data for a single line contains the following info:

Entity name, Entity type, Layer, Color, Beginning X Y coordinate, Ending X Y coordinate, Line type, etc. You can modify any of the above aspects. An example of an entity list:

( - 1 <Entity name: 60000014) (0 "CIRCLE") (8 . "LAYER1")

(10 . 50.000 100.000) (40 . 60.000)

It is an entity list of a circle on layer LAYER1, center point relative to 0,0 of 50.0,100.0 , and a radius of 60.0

Ssget and Entsel (select entities)

Both give you a way of selecting the entities for the selection set. (entsel) only selects one entity at a time. You may not use WINDOW or CROSSING to select entities. (ssget) however lets use WINDOW or CROSSING as well as other selection techniques. You will mostly be using (ssget).

(setq a (ssget)) will prompt you to select objects. You have now created a selection set with a specific name, <Selection set:l> , assigned to variable a, or use filter option (setq a (ssget "X" '((0 . "TEXT")))) to search database for certain entities or codes.

Ssname (get entity name)

Lets you secure the name of the entity. The name of the entity is realy a hexadecimal number, therefore don't expect to see a name like LINE, or CIRCLE etc. The name of your entity might be 60000018.

Lets assume variable a is the selection set and variable i is set to 0. (setq i 0) To set Counter variable. (setq na (ssname a i)) This assigns na the entity name found in the selection set a at index number i. Remember that a selection set may contain more than one entity name. You can point to each entity by using its relative number in the selection set. eg: Entity 1 is Index 0 , Entity 2 is Index 1 , etc.

Entget (get entity list)

This command actually pulls out, or extracts, the entity list. The entity list can be assigned to a variable. (setq b (entget na)) That assigns to b the entire entity list for that entity name.

Subst (substitute new for old)

Allows you to substitute one aspect for another. Assume variable b is the name of the list and variable c contains the value of the element: (40 . 60.0000) (setq bl (subst '(40 . 30.0000) c b)) ;bl is now the new list. '(40 . 30.0000) is the new element substituted for the old element c found in list b.


Gives you the length or number of selections made.

Entmod (entity modification)

Gives you the ability to take the newly modified entity list and write it back to the database to update the drawing. Now that you have a new list in the variable b1, you want to make bl the permanent list in your drawing database. (entmod bl) You should see the change appear on the screen.

Change Cross Hair Angle

This program permits you to draw lines perpendicular to other lines. The program measures the angle of the line chosen, and shifts the SNAP ANGLE to the angle of that line. Use ORTHO ON and draw perpendicular to your chosen line.

(defun c:perpdon (/ a b pntl pnt2 angl)


(setq a (entsel))

(setq b (entget (car a)))

(setq pntl (cdr (assoc 10 b)))

(setq pnt2 (cdr (assoc 11 b)))

(setq angl (angle pntl pnt2))

(setvar "snapang" ang1)



(defun c:perpdoff

(setvar "snapang" 0)



Erase Screen

Erases everything on the drawing screen. If you are in a ZOOM ALL position, the program erases everything within the limits of the drawing.

Note: if you accidentally invoke this command, you can recover with OOPS.

(defun c:erasescr (/ l u)

(setq l (getvar "limmin"))

(setq u (getvar "limmax"))

(command "erase" "w" l u "")



Change Layer

Lets you select objects by any selection method and change their layer. The target layer is chosen by simply pointing to an object on the desired layer. All objects selected will then change to that target layer. To test this program, you will need to create a drawing with objects on different layers.

(defun c:chlayer (/ a1 a2 n index b1 b2 d1 d2 b3)


(prompt "\nselect entities to be changed: ")

(setq a1 (ssget))

(prompt "\npoint to entity on target layer: ")

(setq a2 (entsel))

(setq n (sslength a1))

(setq index 0)

(setq b2 (entget (car a2)))

(setq d2 (assoc 8 b2))

(repeat n

(setq b1 (entget (ssname a1 index)))

(setq d1 (assoc 8 b1))

(setq b3 (subst d2 d1 b1))

(entmod b3)

(setq index (+ index 1))





Now examine the program line by line.

(defun c:chlayer (/ a1 a2 n index b1 b2 d1 d2 b3)

Defines the function with all local variables.


Changes to graphics screen.

(prompt "\nSelect entities to be changed: ")

This is a prompt statement.

(setq a1 (ssget))

Allows you to select the objects to be changed. The selection set is assigned to variable al.

(prompt "\npoint to entity on target layer: ")

This is a prompt statement.

(setq a2 (entsel))

This is a special type of selection statement that allows you to select only one entity.

(setq n (sslength a1))

Measures the number of entities in the selection set in variable a1.

(setq index 0)

Sets the variable called index to 0.

(setq b2 (entget (car a2)))

This statement gets the entity list from a2. Thus, a2 is assigned the entity list.

(setq d2 (assoc 8 b2))

This looks for the code 8 in entity list a2, then assigns the sublist to d2.

(repeat n

This begins the loop that pages through the selection set.

(setq bl (entget (ssname a1 index)))

This gets the entity list and assigns it to b1.

(setq d1 (assoc 8 b1))

Gets the sublist code 8 (the layer).

(setq b3 (subst d2 d1 b1))

Substitutes the new d2 layer for the old d1 layer in the entity list a1, and assigns it to the new entity list b3.

(entmod b3)

Updates the new entity list in the database.

(setq index (+ index 1))

Increases the index variable by 1, making it ready for the next loop.

The first ) closes the repeat loop. (princ) exits quitly. The second ) closes the function.

Substitute text

This program lets you choose a line of text and substitute another line at exactly the same place.

(defun c:subtext (/ a b d e d1 b1 y)

(prompt "\nSelect text line: ")

(setq a (entsel))

(setq b (entget (car a)))

(setq d (assoc 1 b))

(prompt (cdr d))(terpri)

(setq e (getstring 1))

(setq d1 (cons (car d) e))

(setq b1 (subst d1 d b))

(entmod b1)

(setq y (getstring "\nIs this correct - Y : "))

(if (= (srtcase y) "N") (entmod b))





This program lets you change the distance between multiple text lines. In addition to the standard start point and height, you are asked to enter the distance between text lines. You may enter as many text lines as you wish. To stop the program, enter an asterix (*).

(defun tex (/ p1 a b c d e f)

(setq pl (getpoint "\nStarting point: "))

(setq a (getdist p1 "\nEnter height: "))

(setq c (getdist p1 "\nline spacing: "))

(setq d "T")

(while d

(setq e (getstring 1 "Text: "))

(command "text" pl a "0" e)

(setq pl (list (car p1)(- (cadr p1) c)))

(setq f (getstring))

(if (= f "*") (setq d nil))






This program allows you to globally change the size of text within a WINDOW or CROSSING without affecting other entities.

(defun chtext (/ a ts n index b1 b c d b2)

(setq a (ssget))

(setq ts (getreal "\nEnter new text size"))

(setq n (sslength a))

(setq index 0)

(repeat n

(setq b1 (entget (ssname a index)))

(setq index (1+ index))

(setq b (assoc 0 b1))

(if (= "TEXT" (cdr b))


(setq c (assoc 40 b1))

(setq d (cons (car c) ts))

(setq b2 (subst d c b1))

(entmod b2))))



The AutoLisp/Visual Lisp/VBA Resource Website

Copyright 1999-Perpetuity by AfraLisp

All rights reserved.
Information in this document is subject to change without notice.
Site created and maintained by Kenny Ramage

The AutoLisp/Visual Lisp/VBA Resource Website