本例通过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));}}
}