C#/AutoCAD 2018/ObjectArx/二次开发添加删除实体的工具函数(四)
2018-04-01 14:28
811 查看
C#/AutoCAD 2018/ObjectArx/二次开发添加删除实体的工具函数(四)
1、添加删除实体C# ObjectARX二次开发添加删除实体是非常容易主要代码如下:
添加实体:
objId = btr.AppendEntity(entity);
trans.AddNewlyCreatedDBObject(entity, true);
删除实体: entity.Erase(true);
2、基本过程
在代码YunyouXueYuan命名空间,新增Tools类。这个例子非常简单,想学习AutoCAD二次开发(c#、C++等语言)、BIM软件二次开发、Java、python等在线课程的同学,请关注云幽学院的视频课程 yunyou.ke.qq.com。
3、代码实现具体实现方法如下:
using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace YunyouXueYuan { public class Tools { #region "添加实体" /// <summary> /// 添加实体。 /// 将实体添加到当前模型空间上。 /// </summary> /// <param name="entityID">实体ID</param> /// <returns>true:成功 false:失败</returns> public bool AddEntity(Entity entity, out ObjectId objId) { 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 { b98d return false; } return true; } /// <summary> /// 添加实体。 /// </summary> /// <param name="entity"></param> /// <returns></returns> 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 #region "添加多个实体" /// <summary> /// 添加实体。 /// 将实体添加到当前模型空间上。 /// </summary> /// <param name="entityID">实体ID</param> /// <returns>true:成功 false:失败</returns> 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 "删除实体" /// <summary> /// 删除实体。 /// 删除当前模型空间上的实体。 /// </summary> /// <param name="id">实体ID</param> /// <returns>true:成功 false:失败</returns> 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 "删除多个实体" /// <summary> /// 删除实体。 /// 删除当前模型空间上的实体。 /// </summary> /// <param name="entityID">实体ID</param> /// <returns>true:成功 false:失败</returns> 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 } }
相关文章推荐
- C#/AutoCAD 2018/ObjectArx/二次开发添加圆对象的的例子(五)
- C#/AutoCAD 2018/ObjectArx/二次开发添加圆对象的的例子(五)
- C#/AutoCAD 2018/ObjectArx/二次开发再说实体(六)
- 使用.NET开发AutoCAD——C#/AutoCAD 2018/ObjectArx/二次开发入门(二)
- 发布一个ObjectARX .NET AutoCAD 二次开发 添加各种实体的类
- 利用C#进行AutoCAD的二次开发(一)
- C#软件开发实例.私人订制自己的屏幕截图工具(六)添加配置管理功能
- ObjectARX_将实体添加到图形数据库的模型空间相关函数
- (c#)AutoCAD二次开发调试时,netload后,输入命令,却显示为未知命令
- 利用C#进行AutoCAD的二次开发 颜色和样式
- C#软件开发实例.私人订制自己的屏幕截图工具(七)添加放大镜的功能
- AutoCAD二次开发三种添加插件按钮的方法之三
- ObjectContext 是开发人员在查询、添加和删除其实体实例以及将新状态保存回数据库时用到的主要构造
- AUTOCAD二次开发-----删除一个图层里面的所有对象
- 利用C#进行AutoCAD的二次开发(一)
- Android 开发即时聊天工具 YQ :(八) 添加/删除好友
- 利用C#进行AutoCAD的二次开发(三)(转自明经通道)
- C#软件开发实例.私人订制自己的屏幕截图工具(八)添加键盘操作截图的功能
- 实体定位 autoCAD 二次开发
- AutoCad二次开发:ObjectARX