c# - 使用 C# 在分解后删除 AutoCAD 绘图对象

标签 c# automation autocad

我正在处理一个任务,其中代码自动打开用户选择的绘图 [在 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/

相关文章:

c# - mysql 的登录页面

ruby - 如何使用 Ruby 自动化 Windows Run 应用程序

automation - 自动创建docker容器并启动python脚本

c# - 使用 WPF ArcSegment 绘制 DXF 圆弧

c# - 在 AutoCAD .Net 插件中执行多线程作业

c# - 如何在不枚举查询的情况下将 string 转换为 int

c# - 在这种情况下什么时候调用我的析构函数? (C#)

php - 将行的 MySQL 字段提取到 PHP 变量和数组中

three.js - 三JS : is it possible to simplify an object/reduce the number of vertexes?

c# - 如何从查询字符串中检索多个 GET 变量?