C#/AutoCAD 2018/ObjectArx/二次开发添加删除实体的工具函数(四)
2024-10-18 22:26:40
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 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 { 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 } }
最新文章
- CentOS7 安装Mono及Jexus
- 直接拿来用,最火的.NET开源项目
- Linux中的历史命令
- Sharepoint创建List
- CF460C Present (二分 + 差分数列)
- linux的信号机制
- BDC、CATT批量数据维护
- html5—— 应用程序缓存
- JAVA的JDBC连接与sql操作
- acdream 1682 吃不完的糖果(环形最大子段和)
- 45度地图遮挡问题解决方案(cocos2d-x)
- Java_spring_定时执行任务
- 写实例学习html5 WebSocket
- Openstack安装
- 使用SOCKET TCP
- Ajax中send方法参数的使用
- 机器人局部避障的动态窗口法(dynamic window approach) (转)
- mongo索引
- Spring IOC知识点一网打尽!
- 第一次AOP,附上使用DEMO,可用在简单生产环境了