C# 方法,提示用户选择一个组并获取该组';身份证
我试图编写一个方法,提示用户选择一个组并返回该组的ObjectId,以便以后使用。现在,该方法如下所示:C# 方法,提示用户选择一个组并获取该组';身份证,c#,autocad-plugin,C#,Autocad Plugin,我试图编写一个方法,提示用户选择一个组并返回该组的ObjectId,以便以后使用。现在,该方法如下所示: public static ObjectId PromptUserForGroup() { using (Transaction tr = _database.TransactionManager.StartTransaction()) using (DocumentLock docLock = _activeDocument.
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;
}
}
当我调用该方法时,它会像我希望的那样提示用户。但是,当我选择组时,它总是返回ObjectId.Null,这意味着它没有意识到我正在选择一个组。我不知道出了什么问题,也不知道如何修复它。实际上,组不是从实体派生的,因此不在模型空间(BlockTableRecord)上。因此,图形上没有组,而字典上没有组 当用户选择某个对象时,您需要找到该对象所属的组。下面是一个示例代码:
[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();
}
}
资料来源: