How to interpret and manipulate an entity definition list (association list) with autolisp in AutoCAD

To see an entity definition list is fine, but our ultimate goal is to be able to use the values associated with certain DXF codes, and often to change the properties of an entity. To do that, we will need to understand the unique properties of an association list so that we can extract an object’s properties and change its definition list.

Explanation: the (assoc) function Extracting association pairs

(assoc code list)

The (assoc) function searches the association list list for the key element code and returns the complete association pair if the code exists in list. If code is not found, it returns nil.

For example, to find the layer association pair for entity ENT1:

Command: (assoc 8 ENT1)
(8 . “0”)

Isolating Data

To isolate just the data associated with a DXF code we use the (cdr) function:

Command: (cdr (assoc 8 ENT1))

As you create more programs that use the data gathered from existing entities, you will use this series of functions quite often:

(cdr (assoc x (entget xx)

  • It would be convenient to create your own user-defined function that calls these three pre-defined functions as a shortcut whenever you wish to extract the data associated with a certain DXF code on a certain entity name. (See additional practice.)
See also  Using Additional Point List Functions with autolisp in AutoCAD


Create a routine that will switch the current layer to the layer of an object the user picks. Use the drawing RATCHET.DWG to test the routine. The solutions below are not completely clean, so you may also want to add Command echo toggles. Estimated time for completion: 10 minutes.


(defun C:SETLAY ()
    (setq OBJECT(car (entsel “\nSelect object for new current layer: “)))
(setq LAYERNAME (cdr (assoc 8 (entget OBJECT))))

    (command “-layer” “set” LAYERNAME “”)

Additional Practice

Create your own (getdxf) function that can be used in the place of the three pre-defined functions cdr, accoc, and entget. Your function will require two arguments: the DXF code number and the Ename of the entity from which the data will be extracted. Then rewrite SETLAY.LSP using the DXF function.


      (cdr (assoc CODE (entget ENAME)))

  • It would be an excellent idea to add the code from this program to your ACADDOC.LSP file, so it will load automatically and be available to any of your programs.

Solution: (setlay) with (GETDXF) code

(defun C:SETLAY ()
(setq OBJECT (car (entsel “\nSelect object whose layer will become current: “)))
(setq LAYERNAME (getdxf 8 OBJECT))
(command “-layer” “set” LAYERNAME “”)

  • Complete solutions to these practices are on your class disk as: SETLAY-A.LSP, GETDXF-A.LSP, and SETLAY-B.LSP.
See also  Accessing Subentities with autolisp in AutoCAD
Back to top button