Creating a List of Points with autolisp in AutoCAD

Now that we have seen how useful the (foreach) function is in working with a list of data, we need to be able to easily gather data into lists. We already know one function, (list), that can be used to create a list. The (cons) function can help add data to a list, especially when used in a loop.

Explanation: the (cons) function

(cons new_first_element list)

The (cons) function creates a new list by adding new_first_element to the beginning of list.

For example, if the list NLIST was set to (“Bob” “Susan” “Hussein”), then the expression:

(cons “Keisha” NLIST)

would return:

(“Keisha” “Bob” “Susan” “Hussein”)

Creating a List of Points

To create a list of points, we would use a programming loop to repeat the (cons) function.

(repeat 5
      (setq PTLIST (cons (getpoint “Pick a point:”)

This would prompt the user five times to pick a point, and return a list of the points selected. Now, (foreach) can be used to process these points.

  • If the list argument is nil, (cons) will return a single element list consisting of whatever is returned by the first expression.
  • The list will be in reverse order.
  • The (reverse list) function reverses the list so the numbers will be placed in order.
See also  Formatting Strings with Control Codes with autolisp in AutoCAD


(setq CE-SAV (getvar “cmdecho”))
(setvar “cmdecho” 0)
(initget (+ 1 2 4))
(setq NUMBER (getint “\nNumber of doortags: “))
(initget (+ 1 2 4))
(setq TAGNUMBER (getint “\nTag starting number: “))
(repeat NUMBER
           (setq POINTLIST(cons (getpoint “\nLocation of doortag: “) POINTLIST)
              ) ;_ end of setq
) ;_ end of repeat
(foreach PT (reverse POINTLIST)
(command “circle” PT (/ (getvar “dimscale”) 4.0))
(command “text”
(* (getvar “dimscale”) 0.125)

) ;_ end of command
) ;_ end of foreach
(setvar “cmdecho” CE-SAV)
) ;_ end of defun

  • The empty parentheses in the (defun) function have been filled with the names of the symbols used in the routine. This localizes the variables so that they remain in memory only while the routine is executing.
  • The (itoa) function converts atoms of an integer data type to a string data type. (Data conversion will be covered later in the course.)
  • A complete solution to this exercise is on your class disk as DOORNUMBER-A.LSP.
Back to top button