1、添加刪除實體 C# ObjectARX二次開發添加刪除實體是非常容易主要代碼如下: 添加實體: objId = btr.AppendEntity(entity); trans.AddNewlyCreatedDBObject(entity, true); 刪除實體: entity.Erase(tr ...
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 { 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 } }