Accessing and Iterating Groups

The Groups collection is accessed via a Document object. In this code, ThisDrawing is used as the active document:

Dim objGroups As AcadGroups

Set objGroups = ThisDrawing.Groups

To set a reference to an existing Group object, use the Item method of the Groups collection: Dim objGroup As AcadGroup

Set objGroup = objGroups.Item(2)

Set objGroup = objGroups.Item("My Group")

The parameter of this method is either a number representing the position of the desired Group object within the Groups collection or a string representing the name of the desired Group object. If you use an index number, it must be between 0 and N-1, in which N is the total number of groups in the collection.

Like other AutoCAD collections, Item is the default method for Groups. This means that the method name may be omitted, and the parameter passed straight to the Groups reference. Again, however, Microsoft guidelines recommend against using default methods and properties. You should always make a habit of invoking methods and accessing properties explicitly.

You may iterate through the Groups collection in the same way as you do the SelectionSets collection discussed at the beginning of this chapter.

Public Sub ListGroups()

Dim objGroup As AcadGroup Dim strGroupList As String

For Each objGroup In ThisDrawing.Groups strGroupList = strGroupList & vbCr & objGroup.Name

Next

MsgBox strGroupList, vbOKOnly, "List of Groups" End Sub

+1 0

Post a comment