Putting Association Lists to Use

AutoCAD Visual LISP

 
Putting Association Lists to Use
 
 
 

When you use association lists, you should document what your key values represent. For the garden path, the key values of 10, 11, 40, 41, and 50 will mean the following:

  • 10 indicates the 3D coordinate of the start point of the path.
  • 11 indicates the 3D coordinate of the endpoint of the path.
  • 40 indicates the width (not the half-width) of the path.
  • 41 indicates the length of the path, from start to end.
  • 50 indicates the primary vector (or angle) of the path.

The following is an updated version of the gp:getPointInput function. Within it, an AutoLISP function called cons (short for construct a list) builds the keyed sublists that belong to the association list. Copy this version to the Console window, press ENTER, and run (gp:getPointInput) again:

(defun gp:getPointInput	(/ StartPt EndPt HalfWidth)
  (if (setq StartPt (getpoint "\nStart point of path: "))
    (if	(setq EndPt (getpoint StartPt "\nEndpoint of path: "))
      (if (setq HalfWidth (getdist EndPt "\nhalf-width of path: "))
            ;; if you've made it this far, build the association list
            ;; as documented above.  This will be the return value
            ;; from the function.
            (list
              (cons 10 StartPt)
              (cons 11 EndPt)
              (cons 40 (* HalfWidth 2.0))
              (cons 50 (angle StartPt EndPt))
              (cons 41 (distance StartPt EndPt))
                    )
      )
    )
  )
)

Notice that, when building the list, the program converts the half-width specified by the user into a full width by multiplying its value by 2.

The Console window shows output similar to the following:

_$ (gp:getPointInput)
((10 2.16098 1.60116 0.0) (11 12.7126 7.11963 0.0) (40 . 0.592604) (50 . 0.481876) (41 . 11.9076))
_$