Create an anonymous group

By Virupaksha Aithal

Anonymous groups can be created using the function “SetAt”, on the group dictionary. Please refer to the sample code shown below

 

[CommandMethod("creatAnonymGroup")]
public void creatAnonymGroup()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Editor ed = doc.Editor;
    Database db = doc.Database;
 
    PromptSelectionResult result = ed.GetSelection();
    if (result.Status != PromptStatus.OK)
        return;
 
    using (Transaction Tx =
        db.TransactionManager.StartTransaction())
    {
 
        DBDictionary groupDic =
            (DBDictionary)Tx.GetObject(db.GroupDictionaryId,
                                              OpenMode.ForWrite);
        Group anonyGroup = new Group();
        groupDic.SetAt("*", anonyGroup);
 
        foreach (SelectedObject acSSObj in result.Value)
        {
            anonyGroup.Append(acSSObj.ObjectId);
        }
        //groupDic
        Tx.AddNewlyCreatedDBObject(anonyGroup, true);
 
        Tx.Commit();
    }
}

Comments

2 responses to “Create an anonymous group”

  1. Important note in the doc:

    Group plants persistent reactors on its entries when the entries are added to the group. To accomplish this, it must have an objectId. When the group is added to the group dictionary, it is also added to the database, and thus it obtains an objectId. Therefore, do not attempt to add entries to a newly created group before adding the group to the group dictionary.
    So be carefull, add the group to the dictionnary first before appending entities. And from my experience, entities must be closed.

  2. James Maeding Avatar
    James Maeding

    what is groupDic.SetAnonymous() for?

Leave a Reply to MaxenceCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading