auto CAD .NET二次开发之 添加,删除实体(entity)

您所在的位置:网站首页 VBa删除CAD直线 auto CAD .NET二次开发之 添加,删除实体(entity)

auto CAD .NET二次开发之 添加,删除实体(entity)

2023-12-10 11:35| 来源: 网络整理| 查看: 265

个人感觉添加删除实体这个方法比较水。只要是操作过CAD的.NET程序员都是小菜一碟。不过在这里还是赘述一下。呵呵

添加实体的核心代码只有两句:                                 objId = btr.AppendEntity(entity);                                 trans.AddNewlyCreatedDBObject(entity, true);

删除实体的核心代码只有一句:

   entity.Erase(true);

具体实现方法如下:

 #region "添加实体"         ///         /// 添加实体。         /// 将实体添加到当前模型空间上。         ///         /// 实体ID         /// true:成功 false:失败         public bool AddEntity(Entity entity, out ObjectId objId)         {             //返回结果id             objId = ObjectId.Null;             try             {                            using (Database db = HostApplicationServices.WorkingDatabase)                 {                                        using (Transaction trans = db.TransactionManager.StartTransaction())                     {                                                using (BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite))                         {                                                          objId = btr.AppendEntity(entity);                             trans.AddNewlyCreatedDBObject(entity, true);                         }                                                  trans.Commit();                     }                 }             }             catch             {                                return false;             }             return true;         }

        ///         /// 添加实体。         ///         ///         ///         public bool AddEntity(Entity entity)         {             //返回结果id             ObjectId objId = ObjectId.Null;             try             {                 using (DocumentLock docLock = Application.DocumentManager.MdiActiveDocument.LockDocument())                 {                                     using (Database db = HostApplicationServices.WorkingDatabase)                     {                                              using (Transaction trans = db.TransactionManager.StartTransaction())                         {                                                       using (BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite))                             {                                                              objId = btr.AppendEntity(entity);                                 trans.AddNewlyCreatedDBObject(entity, true);                             }                                                       trans.Commit();                         }                     }                 }             }             catch             {                 //添加失败                 return false;             }             return true;         }         #endregion         #endregion

        #region "添加实体(多个)"         ///         /// 添加实体。         /// 将实体添加到当前模型空间上。         ///         /// 实体ID         /// true:成功 false:失败         public bool AddEntity(Entity[] ents, out ObjectIdCollection objId)         {                        objId = new ObjectIdCollection();             try             {                              using (Database db = HostApplicationServices.WorkingDatabase)                 {                                       using (Transaction trans = db.TransactionManager.StartTransaction())                     {                                                using (BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite))                         {                             foreach (Entity ent in ents)                             {                                                               objId.Add(btr.AppendEntity(ent));                                 trans.AddNewlyCreatedDBObject(ent, true);                             }                         }                                              trans.Commit();                     }                 }             }             catch             {                              return false;             }             return true;         }         #endregion

        #region "删除实体"         ///         /// 删除实体。         /// 删除当前模型空间上的实体。         ///         /// 实体ID         /// true:成功 false:失败         public bool DelEntity(ObjectId id)         {             try             {                                  if (!id.IsNull)                 {                                       using (Database db = HostApplicationServices.WorkingDatabase)                     {                                               using (Transaction trans = db.TransactionManager.StartTransaction())                         {                                                        Entity entity = (Entity)trans.GetObject(id, OpenMode.ForWrite, true);                                                      entity.Erase(true);                                                        trans.Commit();                         }                     }                 }                 else                 {                     return false;                 }             }             catch             {                              return false;             }             return true;         }         #endregion

        #region "删除实体"         ///         /// 删除实体。         /// 删除当前模型空间上的实体。         ///         /// 实体ID         /// true:成功 false:失败         public bool DelEntity(ObjectIdCollection ids)         {             try             {                              using (Database db = HostApplicationServices.WorkingDatabase)                 {                                      using (Transaction trans = db.TransactionManager.StartTransaction())                     {                                              using (BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite))                         {                             foreach (ObjectId id in ids)                             {                                                                 Entity entity = (Entity)trans.GetObject(id, OpenMode.ForWrite, true);                                 if (entity == null || entity.IsErased == true || entity is ProxyEntity)                                 {                                     continue;                                 }                                                               entity.Erase(true);                             }

                        }                         trans.Commit();                     }                 }             }             catch             {                 return false;             }             return true;         }         #endregion

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3