Nick Gilbert
Nick Gilbert

Reputation: 4240

Method that prompts user to pick a group and gets that group's ID

I'm trying to write a method that prompts the user to pick a group and returns the ObjectId of the group so I can use it later. Right now that method looks like this:

public static ObjectId PromptUserForGroup()
        {
            using (Transaction tr = _database.TransactionManager.StartTransaction())
            using (DocumentLock docLock = _activeDocument.LockDocument())
            {

                PromptSelectionResult activeSelectionPrompt = _editor.GetSelection();
                if (activeSelectionPrompt.Status == PromptStatus.OK)
                {
                    ObjectId[] ids = activeSelectionPrompt.Value.GetObjectIds();
                    foreach (ObjectId id in ids)
                    {
                        Group groupToCheck = tr.GetObject(id, OpenMode.ForWrite) as Group;
                        if (groupToCheck != null)
                        {
                            return groupToCheck.Id;
                        }
                    }
                }
                else
                {
                    throw new IOException();
                }
                return ObjectId.Null;
            }
        }

When I call the method it prompts the user like I want it to. However, when I pick the group it always returns ObjectId.Null meaning it isn't realizing I'm picking a group. I don't know what's wrong or how to fix it.

Upvotes: 4

Views: 296

Answers (1)

Augusto Goncalves
Augusto Goncalves

Reputation: 8604

Actually a Group is not derived from Entity, therefore is not on the modelspace (BlockTableRecord). As a result, there is no Group on the drawing, but on the Dictionary.

When a user select something, you need to find the group that it belongs. Here is a sample code:

[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 + " group\n");

      }
    }

    if (!bPartOfGroup)
      ed.WriteMessage(
              "Entity is Not part of any group\n");
    Tx.Commit();
  }
}

Source: http://adndevblog.typepad.com/autocad/2012/04/how-to-detect-whether-entity-is-belong-to-any-group-or-not.html

Upvotes: 4

Related Questions