您的位置:首页 > 移动开发 > Objective-C

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
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: