A group is a container object that maintains an ordered collection of database entities. Groups can be thought of as named persistent selection sets. They do not have an ownership link to the entities they contain.
When an entity is erased, it is automatically removed from the groups that contain it. If an entity is unerased, it is automatically reinserted into the group.
Use the AcDbGroup::newIterator() function to obtain an iterator and step through the entities in the group. The AcDbGroup class also provides functions for appending and prepending entities to the group, inserting entities at a particular index in the group, removing entities, and transferring entities from one position in the group to another. See AcDbGroup in the ObjectARX Reference.
You can also assign properties to all members of a group using the setColor(), setLayer(), setLinetype(), setVisibility(), and setHighlight() functions of the AcDbGroup class. These operations have the same effect as opening each entity in the group and setting its property directly.
Groups should always be stored in the GROUP dictionary, which can be obtained as follows:
AcDbDictionary* pGrpDict = acdbHostApplicationServices()->working Database()-> getGroupDictionary(pGroupDict, AcDb::kForWrite);
An alternative way to obtain the GROUP dictionary is to look up “ACAD_GROUP” in the named object dictionary.
The following functions are part of an application that first prompts the user to select some entities that are placed into a group called “ASDK_GROUPTEST”. Then it calls the function removeAllButLines() to iterate over the group and remove all the entities that are not lines. Finally, it changes the remaining entities in the group to red.
void groups() { AcDbGroup *pGroup = new AcDbGroup("grouptest"); AcDbDictionary *pGroupDict; acdbHostApplicationServices()->workingDatabase() ->getGroupDictionary(pGroupDict, AcDb::kForWrite); AcDbObjectId groupId; pGroupDict->setAt("ASDK_GROUPTEST", pGroup, groupId); pGroupDict->close(); pGroup->close(); makeGroup(groupId); removeAllButLines(groupId); } // Prompts the user to select objects to add to the group, // opens the group identified by "groupId" passed in as // an argument, then adds the selected objects to the group. // void makeGroup(AcDbObjectId groupId) { ads_name sset; int err = acedSSGet(NULL, NULL, NULL, NULL, sset); if (err != RTNORM) { return; } AcDbGroup *pGroup; acdbOpenObject(pGroup, groupId, AcDb::kForWrite); // Traverse the selection set, exchanging each ads_name // for an object ID, then adding the object to the group. // long i, length; ads_name ename; AcDbObjectId entId; acedSSLength(sset, &length); for (i = 0; i < length; i++) { acedSSName(sset, i, ename); acdbGetObjectId(entId, ename); pGroup->append(entId); } pGroup->close(); acedSSFree(sset); } // Accepts an object ID of an AcDbGroup object, opens it, // then iterates over the group, removing all entities that // are not AcDbLines and changing all remaining entities in // the group to color red. // void removeAllButLines(AcDbObjectId groupId) { AcDbGroup *pGroup; acdbOpenObject(pGroup, groupId, AcDb::kForWrite); AcDbGroupIterator *pIter = pGroup->newIterator(); AcDbObject *pObj; for (; !pIter->done(); pIter->next()) { pIter->getObject(pObj, AcDb::kForRead); // If it is not a line or descended from a line, // close it and remove it from the group. Otherwise, // just close it. // if (!pObj->isKindOf(AcDbLine::desc())) { // AcDbGroup::remove() requires that the object // to be removed be closed, so close it now. // pObj->close(); pGroup->remove(pIter->objectId()); } else { pObj->close(); } } delete pIter; // Now change the color of all the entities in the group // to red (AutoCAD color index number 1). // pGroup->setColorIndex(1); pGroup->close(); }