我正在处理一个任务,其中代码自动打开用户选择的绘图 [在 UI 中] 并选择绘图中的所有对象并开始分解所有对象,直到它们无法再分解为止。这样做时我遇到了一个问题,原始的(未分解的 3D 对象)仍然存在于绘图中,由分解的对象叠加。 Explode 函数的每次递归调用都会为该对象创建一个新的分解 3D 对象。
这是我正在处理的代码片段:
PromptSelectionResult ss = ed.SelectAll();
using (DocumentLock acLckDoc = doc.LockDocument())
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
objs = new DBObjectCollection();
foreach (SelectedObject so in ss.Value)
{
Entity ent = (Entity)tr.GetObject(so.ObjectId, OpenMode.ForWrite);
if (!(ent is Solid3d))
{
ent.Explode(objs);
ent.UpgradeOpen();
ent.Erase();
}
}
tr.Commit();
}
}
一旦控件进入 ent.Erase() 语句 - 它就会抛出异常,eCannotBeErasedByCaller。我不知道为什么?我已解锁所有图层,打开写入实体,CommandFlags 已设置为 Session 和 UsePickSet(全部随机播放)。
有人有什么建议吗?
最佳答案
看你的描述,你可能需要一个递归爆炸。前段时间我针对其他类型的实体围绕此编写了代码,但您可以对其进行调整。
private List<DBObject> FullExplode(Entity ent)
{
// final result
List<DBObject> fullList = new List<DBObject>();
// explode the entity
DBObjectCollection explodedObjects = new DBObjectCollection();
ent.Explode(explodedObjects);
foreach (Entity explodedObj in explodedObjects)
{
// if the exploded entity is a blockref or mtext
// then explode again
if (explodedObj.GetType() == typeof(BlockReference) ||
explodedObj.GetType() == typeof(MText))
{
fullList.AddRange(FullExplode(explodedObj));
}
else
fullList.Add(explodedObj);
}
return fullList;
}
来源:http://adndevblog.typepad.com/infrastructure/2013/04/get-cogopoint-label-text.html
关于c# - 使用 C# 在分解后删除 AutoCAD 绘图对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31994887/