Wednesday
AutoLISP, ProgrammingAutoLISP.6: Managing Lists in AutoLISP (List Handling)
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.
![]() |
rectangle.zip 448 bytes | |
| A sample AutoLISP program that draws rectangle. Orhan Toker | ||
| Added On | December 10, 2008 and downloaded 693 times. | |
Post Tags: AutoLISP, Programming
Related Posts
Popular
- Hip tip: Increasing view resolution with VIEWRES system variable
07/03/2009 04:39 am
7 Comments - Sirince – IZMIR
07/02/2009 11:02 am
1 Comment - Visual LISP: Programming user interfaces with OpenDCL
07/01/2009 10:15 pm
10 Comments - 3D Modeling 7: EXTRUDE
06/30/2009 02:14 pm
10 Comments - Ask DailyAutoCAD: Which Notebook to Buy?
06/30/2009 12:17 pm
1 Comment
Featured Articles
- AutoCAD 2009 Update 3 Released
- Speed Up As AutoCAD 2009 Ribbon Menu Helps You!
- MEP Analysis Extension for AutoCAD MEP extended to March 31, 2009
- Recap of AutoCAD-related technologies on Autodesk Labs
- Technology Preview of MEP Analysis Extension extended to 12/31/08
- Autodesk Labs: Forward and Backward utility for AutoCAD
|



Feb 7, 2007
Reply
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.
Feb 8, 2007
Reply
Thanks Jon,
There are tons of example AutoLISP programs on the way. I’ll try to evaluate your comments in my upcoming articles.
Feb 12, 2007
Reply
This article is very interesting, please i’ll you to send me more about how to start LISP in AutoCAD.
Thanks.
Mar 6, 2007
Reply
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
Mar 7, 2007
Reply
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??
Mar 7, 2007
Reply
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
Sep 27, 2007
Reply
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.
Feb 24, 2008
Reply
hi
what is the lisp command to subtract a hole from a rectangular base. please help me
Apr 11, 2008
Reply
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.?
Dec 10, 2008
Reply
Hi Orhan,
i have a question about lists: is there a command to check whether a certain value is member of a list? I Know you can do this with a while-loop, but if there is a command to do that…
Keep up the good work!
Frank
Jan 7, 2009
Reply
Hey nice work.
I have a question, don’t know if it is related to this.
1) Any simple method to check if a list is a point list or a list of any other things?
2) Any simple method to draw the rectangle starting from the lowerleft corner?
3) assoc seems only find the first matching pair in the dotted pairs, am I right? any simple method to find the next pair(s)?
yC.
Apr 27, 2009
Reply
Hi Orhan,
Can’t seem to find an answer for this out there… so I thought you may know.
How do you make a list of user selected points???
this works when you know how many points will be selected:
(setq ptList (append (list (getpoint “\Enter First Point : “)) ptList))
(setq ptList (append (list (getpoint “\Enter Second Point : “)) ptList))
(setq ptList (append (list (getpoint “\Enter Third Point : “)) ptList))
(setq ptList (append (list (getpoint “\Enter Fourth Point : “)) ptList))
!ptList
((15.8645 78.7754 0.0) (12.1109 76.2372 0.0) (9.50541
78.8806 0.0) (5.75176 76.3423 0.0))
But what if you don’t know how many points they are going to select???
or better stated you don’t care how many points they select you just want to add them all to a list???
thanks
Brian