About Collections of Objects (AutoLISP/ActiveX)

All objects in the AutoCAD object model are grouped in collections.

Note: ActiveX support in AutoLISP is limited to Windows only.

For example, the Blocks collection is made up of all blocks in an AutoCAD drawing, and the ModelSpace collection comprises all graphical objects (circles, lines, polylines, and so on) in the drawing's model space.

The following table lists the collections that are part of the AutoCAD object model:

AutoCAD collection objects

Block

ModelSpace

Blocks

PaperSpace

DimStyles

PlotConfigurations

Documents

PopMenus

Groups

RegisteredApplications

Hyperlinks

SelectionSets

Layers

TextStyles

Layouts

Toolbars

Linetypes

UCSs

Materials

Viewports

MenuGroups

Views

Retrieving a Member from a Collection

The Item method is used to retrieve a member object from a collection, while the Count property returns 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 model space:

(setq index 0)
(repeat (vla-get-count mspace)
  (if (= "AcDbArc" (vla-get-objectname (vla-item mspace index)))
    (progn
      (prompt "\nThe start angle of the arc is ")
      (prompt (rtos (vla-get-startangle (vla-item mspace index))))
    )
  )
  (setq index (+ index 1))
)
Note: Item and Count also apply to groups and selection sets.

Applying a Function to All Items in a Collection

You can use vlax-map-collection to apply a single function to every object in a collection. This can be helpful when you want to list the value of a specific property for each object in a collection, such as each member’s name.

The syntax for the function 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: The preceding example does not show every property returned by vlax-dump-object.

Applying Multiple Expressions to All Items in a Collection

You can use vlax-for to evaluate a series of functions with each object in a collection. This function is much more flexible than using vlax-map-collection. Like the foreach function, vlax-for returns the result of the last expression evaluated inside the for loop.

Note: Modifying the collection (that is, adding or removing members) while iterating through it may cause an error.

The syntax for the function is:

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

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.