Finding the group name to which entity belongs

By Virupaksha Aithal

Groups are stored as persistent reactors in the AutoCAD entity so, “DbObject::GetPersistentReactorIds” API can be used to identify the group associated with AutoCAD entity. Below code shows the procedure to use “GetPersistentReactorIds” to identify the group’s to which a selected entity is belongs.

 

[CommandMethod("FindGroup")]
static public void FindGroup()
{
    Document doc = 
        Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;
    Editor ed = doc.Editor;
    PromptEntityResult acSSPrompt = 
        ed.GetEntity("Select the entity to find the group");
 
    if (acSSPrompt.Status != PromptStatus.OK)
        return;
 
    using (Transaction Tx = 
        db.TransactionManager.StartTransaction())
    {
        Entity ent = Tx.GetObject(acSSPrompt.ObjectId, 
                                OpenMode.ForRead) as Entity;
        ObjectIdCollection ids = ent.GetPersistentReactorIds();
 
        bool bPartOfGroup = false;
        foreach (ObjectId id in ids)
        {
            DBObject obj = Tx.GetObject(id, OpenMode.ForRead);
 
            if (obj is Group)
            {
                Group group = obj as Group;
                bPartOfGroup = true;
                ed.WriteMessage(
                    "Entity is part of " + group.Name + " groupn");
 
            }
        }
 
        if(!bPartOfGroup)
            ed.WriteMessage(
                   "Entity is Not part of any groupn");
        Tx.Commit();
    } 
}

Comments

2 responses to “Finding the group name to which entity belongs”

  1. Just what I needed. Thank you!!!

  2. Thanks for sharing

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading