A method that prompts the user to select a group and gets the group identifier

I am trying to write a method that prompts the user to select a group and returns the ObjectId of the group, so I can use it later. Now this method is as follows:

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 asks the user how I want it. However, when I select a group, it always returns ObjectId.Null means that I do not understand that I select a group. I do not know what is wrong or how to fix it.

+4
source share
1 answer

In fact, the group is not derived from Entity, therefore it is not in the model space (BlockTableRecord). As a result, there is no group in the drawing, but in the dictionary.

-, , . :

[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();
  }
}

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

+2

Source: https://habr.com/ru/post/1598763/


All Articles