Working with Collection Objects

AutoCAD AutoLISP & Visual LISP

 
Working with Collection Objects
 
 
 

The concept of collections was introduced in Understanding the AutoCAD Object Model Recall that all ActiveX objects in the AutoCAD object model are grouped in collections. For example, the Blocks collection is made up of all blocks in an AutoCAD document. VLISP provides functions to help you work with collections of AutoCAD objects. These functions are vlax-map-collection and vlax-for.

The vlax-map-collection function applies a function to every object in a collection. The syntax is:

(vlax-map-collection collection-objectfunction)

For example, the following command displays all properties of every object in a drawing's model space:

$ (vlax-map-collection
(vla-get-ModelSpace acadDocument) 'vlax-dump-Object)
; IAcadLWPolyline: AutoCAD Lightweight Polyline Interface
; Property values:
;   Application (RO) = #<VLA-OBJECT IAcadApplication 00b3b91c>
;   Area (RO) = 3.67152
;   Closed = -1
;   Color = 256
;   Coordinates = (9.59247 4.44872 9.25814 5.34715 4.1991 5.679 ...)
;   EntityName (RO) = "AcDbPolyline"
;   EntityType (RO) = 24
;   Handle (RO) = "4C"
;   Layer = "0"
;  .
;  .
;  .
;   Thickness = 0.0
;   Visible = -1

(Note that the preceding example does not show every property returned by vlax-dump-Object.)

To evaluate a series of functions with each object in a collection, use vlax-for:

(vlax-for symbolcollection [expressions] ...)

Like the foreach function, vlax-for returns the result of the last expression evaluated inside the for loop. Note that modifying the collection (that is, adding or removing members) while iterating through it may cause an error.

The following example defines a function that uses vlax-for to show color statistics for each object in the active drawing:

(defun show-Color-Statistics (/ objectColor colorSublist colorList)
   (setq modelSpace (vla-get-ModelSpace
       (vla-get-ActiveDocument (vlax-get-Acad-Object))
    )
   )
   (vlax-for obj modelSpace
      (setq objectColor (vla-get-Color obj))
      (if (setq colorSublist (assoc objectColor colorList))
        (setq colorList
           (subst (cons objectColor (1+(cdr colorSublist)))
                          colorSublist
                          colorList
           )
        )
        (setq colorList (cons (cons objectColor 1) colorList))
     )
  )
  (if colorList
     (progn (setq
        colorList (vl-sort colorList
                    '(lambda (lst1 lst2) (< (car lst1) (car lst2)))
                  )
            )
            (princ "\nColorList = ")
            (princ colorList)
            (foreach subList colorList
               (princ "\nColor ")
               (princ (car subList))
               (princ " is found in ")
               (princ (setq count (cdr subList)))
               (princ " object")
               (princ (if (= count 1)
                         "."
                         "s."
                       )
 )    )    )   )   
 (princ)
)

This function lists each color in the drawing and the number of objects where the color is found.