003集—— CAD批量划线和text文字(CAD—C#二次开发入门)

 本例通过for循环创建255条线,颜色不同,并在线的右端点处注记文字。

效果如下:

本文有个事务的封装函数,如下:

  private ObjectId AppendEntity(Entity  entity)
  {
      ObjectId objectId;
      Database db = HostApplicationServices.WorkingDatabase;
          using (Transaction tr = db.TransactionManager.StartTransaction())
          {
              BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
              BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace ], OpenMode.ForWrite );
              objectId = btr.AppendEntity(entity);
              tr.AddNewlyCreatedDBObject(entity, true);
              tr.Commit ();   
          }
              return objectId;  
  }

完整代码如下:

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AcTools;
namespace AcTools
{
    
    public class Class1
    {
        private ObjectId AppendEntity(Entity  entity)
        {
            ObjectId objectId;
            Database db = HostApplicationServices.WorkingDatabase;
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace ], OpenMode.ForWrite );
                    objectId = btr.AppendEntity(entity);
                    tr.AddNewlyCreatedDBObject(entity, true);
                    tr.Commit ();   
                }
                    return objectId;  
        }

        private Entity[]  Mydemol( List <Line > mya )
        {   
            Entity[] ent = new Entity[mya.Count ];
            for (int i = 0; i < mya.Count; i++)
            {
             
                mya[i].ColorIndex = i;
               
            
                ent[i] = (Entity)mya[i];   
            };
            return ent;
        }
        private Entity[] Mydemot(List<DBText > mya)
        {
            Entity[] ent = new Entity[mya.Count];
            for (int i = 0; i < mya.Count; i++)
            {

                mya[i].ColorIndex = i;


                ent[i] = (Entity)mya[i];
            };
            return ent;
        }
        [CommandMethod("xx")]
        public void Caddemo()
        {
            
            ///
            Database db = HostApplicationServices.WorkingDatabase;
            //for (int i = 0; i < 1000; i++)
            //{
            // Circle circle = new Circle(new Point3d(0,0,0),new Vector3d (0,0,1),(double)i);
            //    AppendEntity(circle);
            //    Line line = new Line(new Point3d(i, i, 0), new Point3d(i + 100, i + 100, 0));
            //    AppendEntity(line );
            //   // AddEntityTools .AddEntityToModeSpace (db,line);
            //}
            CircularArc3d c3 = new CircularArc3d(new Point3d(0, 0, 0), new Point3d(0, 100, 0), new Point3d(100, 100, 0));
            //Line[] line = new Line[100]; 
            List<Line> line1  = new List<Line>();
            List <DBText > text1 = new List<DBText>();
            for (int i = 0; i < 255; i++)
            {
               
                Line  line = new Line(new Point3d(0, i, 0), new Point3d(100, i , 0));
                line.ColorIndex = i;
                DBText text = new DBText();
                text.TextString  = i.ToString();
                text.Position = new Point3d(100, i, 0); 
                text.Height  = i / 20 + 1;
                text.ColorIndex = i;
                text1.Add(text);
              
                line1.Add(line); 
                AppendEntity(text);
                AppendEntity(line);
            }
           //db.AddEntityToModeSpace ( Mydemol(line1));
           //db.AddEntityToModeSpace(Mydemot(text1));



        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值