C#CAD二次开发:封装事务处理函数

添加图形的工具事务类

    public partial class addEntityTool
    {
    	/// <summary>
        /// 将图形添加到文件中
        /// </summary>
        /// <param name="db">图形数据库</param>
        /// <param name="ent">图形对象</param>
        /// <returns>图形的ObjectId</returns>
        public static ObjectId addEntityToolModelSpace(Database db, Entity ent ) 
        {
            //声明ObjectID ,用于返回
            ObjectId entId = ObjectId.Null;
            
            //开启事务处理
            using (Transaction tran = db.TransactionManager.StartTransaction())
            { 
                //打开块表
                BlockTable bt = (BlockTable)tran.GetObject(db.BlockTableId, OpenMode.ForRead);
                //打开块表记录
                BlockTableRecord btr = (BlockTableRecord)tran.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                //添加图形到块表记录
                entId = btr.AppendEntity(ent);
                //更新数据信息
                tran.AddNewlyCreatedDBObject(ent, true);
                //提交事务
                tran.Commit();

            }
            return entId;
        }
    }

实现画直线的类

public class LineExam
    {
        [CommandMethod("LineDemo")]

        public void LineDemo()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Line line1 = new Line(new Point3d(100, 100, 0), new Point3d(200, 200, 0));

            //使用addEntityTool这个类
            addEntityTool.addEntityToolModelSpace(db, line1);
        }
    }

扩展:画多条直线,把图形的参数变ent成可变参数

        /// <summary>
        /// 将图形添加到文件中
        /// </summary>
        /// <param name="db">图形数据库</param>
        /// <param name="ent">图形对象,可变参数</param>
        /// <returns>图形的ObjectId,数组图层</returns>
        public static ObjectId addEntityToolModelSpace(this Database db, params Entity[] ent)
        {
            //声明ObjectID[] ,用于返回
            ObjectId[] entId = new ObjectId[ent.Length];

            //开启事务处理
            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                //打开块表
                BlockTable bt = (BlockTable)tran.GetObject(db.BlockTableId, OpenMode.ForRead);
                //打开块表记录
                BlockTableRecord btr = (BlockTableRecord)tran.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                
                for (int i = 0; i < ent.Length; i++)
                {
                    //添加图形到块表记录
                    entId[i] = btr.AppendEntity(ent[i]);
                    //更新数据信息
                    tran.AddNewlyCreatedDBObject(ent[i], true);
                }            
                //提交事务
                tran.Commit();
            }
            return entId[ent.Length-1];
        }

画线

            Line line2 = new Line(new Point3d(100, 100, 0), new Point3d(200, 200, 0));
            Line line3 = new Line(new Point3d(200, 100, 0), new Point3d(200, 800, 0));
            Line line4 = new Line(new Point3d(300, 300, 0), new Point3d(500, 220, 0));
            Line line5 = new Line(new Point3d(1000, 105, 0), new Point3d(300, 700, 0));
            Line line6 = new Line(new Point3d(1003, 100, 0), new Point3d(200, 400, 0));

            db.addEntityToolModelSpace(line1, line2, line3, line4, line5, line6);

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

稻田里展望者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值