AutoLISP.6: Managing Lists in AutoLISP (List Handling)
Print This Post
![]() |
Author | Orhan Toker |
| Profession | Architect M.Sc. | |
| Autodesk Authorized Consultant | ||
| Database Specialist | ||
All AutoLISP versions
Lists are one of the most important aspects of AutoLISP projects. Points, entities, tables and files that you have to control in the drawings are bound to the lists. An AutoLISP list is a series of variables in which more then one value is stored. This is similar to the one dimensional arrays in other programming langugages. For example:
Dim a(5);Line defines an a variable with 5 components in programming languages like BASIC, Pascal. Inr order to assign a value to variable a, we use equalities like:
a(0) = 5
a(1) = 8
a(2) = 3
a(3) = “example”
a(4) = 6.8
While using these equations, we need such an equality:
some_text = a(3);This equality assigns the “example” value to some_text variable.
Corresponding expression for this operations in AutoLISP is:
(setq a (list 5 8 3 “example” 6.8))
By this way, values of 5 8 3 “example” and 6.8 are assigned to variable a. Ans its usage is as follows:
(setq some_text (nth 3 a))
As it cen be seen, the only difference between the lists and the arrays in other programming languages is that lists are one dimensional. However, if you treat them carefully, you can also create one list inside another (nested lists).
$_(setq b (list 3 “two” a 1 4 “one”))
(3 “two” (5 8 3 “example” 6.8) 1 4 “one”)
As it can be seen here, b 3. member is a list which includes another list.
Point Lists:
AutoLISP enables us to use lists that include 2 or 3 variables for managing graphical coordinates. These lists are called point lists.
2-B Point list:
(34.5 27.3 )represents coordinates of x=34.5 and Y=27.3.3-B Point list:
(34.5 27.3 0.0)represents coordinates of x=34.5, Y=27.3 and Z=0.0.
Point lists will be extremely useful to you is you would like to create any AutoCAD entity.
(setq p1 (list 40 40))
(setq p2 (list 100 60))
(command “_.line” p1 p2 “”)
This example is used to create a line from 40,40 point to 100,60 point. For such kind of simple examples similar to this example, what you have learned up to now is enough. However, if we try to make more complicated examples, we will have to manage the lists. For example, let’s make our own rectangle drawing command.
An AutoLISP example for understanding point lists: rectangle.lsp
We can ask the user to enter the both corners of the rectangle. We can do this by using (getpoint) and (getcorner) functions. However, by using these commands, we can only get the opposite two corners of the rectangle. These points are enough for us to calculate the other two corners. We can also do these calculations by using list handling functions. Let’s take a look at the figure:

Fig.1 Structure of the rectangle.
We can get points p2 and p3 from the user. And the other points, p2 and p4 are indeed:
p2(x) = p3(x), p2(y) = p1(y)
p4(x) = p1(x), p4(y) = p3(y)
You can get x, y and z values from point lists by using (nth) function as well as you can get them by using (car) (cadr) and (caddr) functions. For example:
(car p1)gives the x value of point 1. Also(nth 0 p1)does the same job.
(cadr p1)gives the y value of point 1. Also(nth 1 p1)does the same job.
(caddr p1)gives the z value of point 1. Also(nth 2 p1)does the same job.
Accordingly, our function that draws a rectangle will be like this:
Code.1 To download rectangle.lsp click on the picture.
Dotted Pairs:
Another good way of re-organizing the list that you will create in AutoLISP is creating dotted pairs. This actually is list paris which are seperated from each other by dots (.). You can think of element on the left side as the name of the pair and the one on the right side as its value. For example:
((layername . “TAL_WALL” ) (linetck . 0.5))
List stores the values of layer name and line thickness values of an entity. In order to create a dotted pair, you should use (cons) function:
$_(cons ‘layername “TAL_WALL”)
(layername . “TAL_WALL”)
Every time you use the (cons) function, another dotted pair is also added at the end of the list:
$_(setq a (cons ‘name “Orhan”))
(name . “Orhan”)
$_(setq a (list a (cons ‘surname “Toker”) (cons ‘age 35)))
((name . “Orhan”) (surname . “Toker”) (text . 35))
Entity Lists:
Another importance of dotted pairs is that, you can reach the database of AutoCAD entities by using these lists. AutoCAD gives an [Entity Name] to each of the entities in AutoCAD drawing and stores them by using DXF codes inside a dotted pair list that is connected to this entity name. For example, when you get a line entity by using (entget) function (we will deal with entity management later on), you will obtain the following list:

Data search inside doted pairs list:
In our first intermediate level application (An AutoLISP program that draws a bubble at the end of axes), you can examine how we are extracting information from this entity data. Now, assume that the above mentioned dotted pair list is loaded on “aks_hand” variable and learn how to reach data inside them. Searching data inside lists that are made up of dotted pairs is easier that searching inside lists arrangement of which you don’t know. (assoc) function is for this purpose.
(setq entity_name (cdr (assoc 0 aks_hand)))
List that is shown above queries the right side element of the pair, left side element of which is 0 inside the contents of “el” list. You should know that, 0 represents the entity name inside DXF codes. I will not tell any more about DXF codes, as they are not the subject of this lesson.
Making operations over list elements by using (mapcar):
(mapcar) function applies an expression over all list functions. For example, assume that you have a list as (1 2 3 4).
$_(mapcar ‘1+ ‘(1 2 3 4))
(2 4 5 6)
(mapcar) increased the values of all of the list elements by one, by applying (1+) function to them. Or again by using (mapcar) , you can reverse a list like (T nil T T (= 3 3.0)) by this way:
$_(mapcat ‘null (T nil T T (= 3 3.0)))
(nil T nil nil nil)
In this lecture, I tried to explain about controlling lists and functions used for this purpose. Examples that I have used for the functions explains the way that they most frequently used for. In advanced lessons, we will use such functions in more efficient ways.
Realted downloads
Similar Posts
Rate this article:


February 7th, 2007 20:16
Any chance you could try to explain these lessons in more detail? I’m trying to follow along but you have lost me during the last 2-3 articles. You are doing a good job doing the step by step explanation, but you’re not giving a cause & effect analysis (ie. if you add this to your lisp, you will get this output because…). After all you don’t shove someone into a car & show them the brake & gas then tell them which is which and expect them to be able to drive. You have a whole world of explaining and showing them what to do before setting them loose if you really want them to be successful.
February 8th, 2007 09:17
Thanks Jon,
There are tons of example AutoLISP programs on the way. I’ll try to evaluate your comments in my upcoming articles.
February 12th, 2007 16:05
This article is very interesting, please i’ll you to send me more about how to start LISP in AutoCAD.
Thanks.
March 6th, 2007 20:16
First- I would like to say good job on this Autolisp “how-to” format. It has been a lot of help. Keep it coming!!
Second- I am trying to set a point variable from values I have calculated. How do I send this information to a point list (X, Y, and Z)? For example:
(setq X_POINT (+ 1 2))
(setq Y_POINT (+ 3 4))
(setq Z_POINT (+ 5 6))
(setq pt1 (list (X_POINT Y_POINT Z_POINT) ;?not sure?
(command “.point” pt1)
Any help with this would be greatly appreciated -
Signed-
“I know enough to be dangerous”- Robert
March 7th, 2007 00:41
Thank you for your prompt response-
I changed that line I was unsure of and …
I wrote a simple sample of the program…shown below
;Test program
(defun c:pt1()
(setq px (+ 1 3))
(setq py (+ 1 4))
(setq pz (+ 1 5))
(setq pt1 ‘(px py pz))
(princ pt1)
)
This program returns the value for pt1 as (px py pz) and
not the numerical values of (4 5 6). Why??
March 7th, 2007 13:37
I have found the answer!!
(setq pt1 (list px py pz))
This returns the numerical values-
Thank you for your time and help-
I have so many more questions-
But, I will read and study before asking-
Robert
September 27th, 2007 05:32
I would like to know how to set AutoCAD 2007 to recognize right click as a pt value. Where you do not have to prompt for user for the pick point. I am trying to insert a block in a set location of placement from the right click. In 2004 we only had to leave the p off of the lisp for pt. Also would like to use a lisp command to flip blocks up and down from the insertion point, and direction change of blocks from the insertion point. Thank You for any help you can provide.
February 24th, 2008 03:44
hi
what is the lisp command to subtract a hole from a rectangular base. please help me
April 11th, 2008 13:56
thanx for the valuable tips. im having problems in designing a CAD system using Autolisp to draw a plan view of a hexagonal nut. The program should be ble to draw nuts of different sizes after the user has specified the major radius for the nut. any suggestions.?