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
    }
}

最新文章

  1. CentOS7 安装Mono及Jexus
  2. 直接拿来用,最火的.NET开源项目
  3. Linux中的历史命令
  4. Sharepoint创建List
  5. CF460C Present (二分 + 差分数列)
  6. linux的信号机制
  7. BDC、CATT批量数据维护
  8. html5—— 应用程序缓存
  9. JAVA的JDBC连接与sql操作
  10. acdream 1682 吃不完的糖果(环形最大子段和)
  11. 45度地图遮挡问题解决方案(cocos2d-x)
  12. Java_spring_定时执行任务
  13. 写实例学习html5 WebSocket
  14. Openstack安装
  15. 使用SOCKET TCP
  16. Ajax中send方法参数的使用
  17. 机器人局部避障的动态窗口法(dynamic window approach) (转)
  18. mongo索引
  19. Spring IOC知识点一网打尽!
  20. 第一次AOP,附上使用DEMO,可用在简单生产环境了

热门文章

  1. Linux显示内存状态
  2. FFMPEG:压缩之H264编码(YUV420P-&gt;H264)
  3. HTML基础加强
  4. 通过ApplicationContext.getBean(beanName)动态加载数据。
  5. C#图解教程 第十九章 LINQ
  6. flash数据交互
  7. 又把JDK改回JDK1.8的过程
  8. [JSOI2004]平衡点
  9. Infinite Fraction Path HDU 6223 2017沈阳区域赛G题题解
  10. 初步谈谈 C# 多线程、异步编程与并发服务器