About Working With Collection Objects (AutoLISP/ActiveX)

Collections are used to group objects of the same or similar type.

The concept of collections was introduced in About Understanding the AutoCAD Object Model (AutoLISP). 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. Visual LISP 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-object function)

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 symbol collection [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.

Retrieve Member Objects in a Collection

The Item method retrieves a member object from a collection. The Count property shows the number of items in a collection. Using the Item method and Count property, you can individually process each object in a collection. For example, you can look at each object in a model space, determine the type of object, and process only the types of objects you are interested in. The following code prints the start angle for each arc object in a model space:

(setq index 0)
(repeat (vla-get-count mspace)
  (if (= "AcDbArc" (vla-get-objectname (vla-item mspace index)))
    (progn
      (princ "\nThe start angle of the arc is ")
      (princ (vla-get-startangle (vla-item mspace index)))
    )
  )
  (setq index (+ index 1))
)

Note that Item and Count also apply to groups and selection sets.