AfraLISP - Learn AutoLISP for AutoCAD productivity

Object Collections - Part 1

by Kenny Ramage

All Objects in AutoCAD are grouped into collections. If you have a look at the AutoCAD Object Model, you will find all the Layers within the Layers collection, all the Blocks within the Blocks collection, etc. etc.

This tutorial will show you how to first, create a reference to the required collection, secondly, extract objects stored within the collection, and last but not least, manipulate these Objects.

Let's start right at the bottom of the AutoCAD Object Model with the Documents collection. Open any two drawings in AutoCAD then open the Visual List Editor and enter this :

_$ (vl-load-com)

_$ (setq acadObject (vlax-get-acad-object))
#<VLA-OBJECT IAcadApplication 00adc088>

Let's have a look at the acadObject in the Inspect window :

Inspect: VLA-OBJECT

The object that we are interested in at this stage is the Documents object. Let's drill down to it :

_$ (setq acadDocuments (vla-get-documents acadObject))
#<VLA-OBJECT IAcadDocuments 01f585d0>

Have a look at the variable acadDocuments in the inspect window :

Inspect window

As you can see, this collection contains 2 objects. But how do we access these objects? Copy and paste the following coding and save it as tempList.lsp :

 (defun tempList (theObject / item dwgName)
  (vl-load-com)
 
  (setq theList '())
 
  (vlax-for item theObject
 
    (setq dwgName (vlax-get-property item 'Name))
 
    (setq theList (append (list dwgName) theList))
 
  )
 
  (setq theList (reverse theList))
 
(princ)
 
);defun

Don't worry to much at this stage about how this function works. We'll get to that later. Load the function and then type the following :

_$ (tempList acadDocuments)

Now examine the variable theList :

_$ theList
("Kenny.dwg" "is handsome.dwg")

This list now holds the contents of the Documents collection, or in other words, all the drawings you have open within AutoCAD…

Let's go further in the AutoCAD Object Model and have a look at the Layers collection. We first get a reference to the "Active Document" :

_$ (vl-load-com)
_$ (setq acadDocument (vla-get-activedocument (vlax-get-acad-object)))
#<VLA-OBJECT IAcadDocument 00ee0f84>

Next, a reference to the Layer's collection :

(setq theLayers (vla-get-layers acadDocument))
#<VLA-OBJECT IAcadLayers 01f5b0a4>

Now, let's extract all the Layer names into a list :

_$(tempList theLayers)
_$ theList
("0" "1" "2" "3" "4" "5" "6" "7" "DEFPOINTS" "8" "9" "10")

Great, we now have a list of all our Layers within the current drawing.

To manipulate objects within a collection, we first need to iterate through the collection and extract a reference to the object, or objects that we are interested in. Visual Lisp provides us with a few functions to help us with this task. Let's say for example that we wanted to change all the Layers in the drawing to Color 3. Consider this :

(defun laycol ()
 
(vl-load-com)
  
(setq acadDocument (vla-get-activedocument (vlax-get-acad-object)))
 
(setq theLayers (vla-get-layers acadDocument))
 
(setq i 0)
  
   (repeat (vla-get-count theLayers)
  
	(setq aLayer (vla-item theLayers i))
  
       	(vla-put-color alayer 3)
 
	(setq i (1+ i))
  
   );repeat
 
(princ)
 
);defun

Here we've used the standard AutoLISP function repeat to loop through the collection. We used the vla-get-count function to count the number of objects in the collection, and the function vla-item function to extract each object from the collection.

In Part 2 we'll have a look at a few more functions that will make your life a lot easier when dealing with collections.