eNotInDatabase,btr.AppendEntity(attNew);必须在AddNewlyCreatedDBObject前面

发布时间 2023-04-24 16:07:39作者: swtool

 

    public static void AddAttsToBlock(this ObjectId blockId, List<AttributeDefinition> atts, bool copyEntity = false)
    {
      Entity attNew;
      Database db = blockId.Database;//获取数据库对象
                                     //打开块表记录为写的状态
      BlockTableRecord btr = (BlockTableRecord)blockId.GetObject(OpenMode.ForWrite);
      //遍历属性定义对象列表
      foreach (AttributeDefinition att in atts)
      {
        attNew = att;
        if (copyEntity)
        {
          attNew = (Entity)att.Clone();
        }
        //此句必须在AddNewlyCreatedDBObject前面
        btr.AppendEntity(attNew);//为块表记录添加属性
        db.TransactionManager.AddNewlyCreatedDBObject(attNew, true);//通知事务处理
      }
      btr.DowngradeOpen();//为了安全,将块表记录的状态改为读
    }