使用工具==CAD2019+VS2022
主要作用==选择多条直线,将直线的长度分段汇入表格,并以单行文字的形式将表格序号绘制在图中
代码内容一:添加实体到cad中
//添加实体
public static ObjectId AddToModelSpace(this Database db, Entity ent)
{
ObjectId entId;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
entId = btr.AppendEntity(ent);
trans.AddNewlyCreatedDBObject(ent, true);
trans.Commit();
return entId;
}
代码内容二:
public void Test()
{
Database db =HostApplicationServices.WorkingDatabase;
Editor ed = AutoCad.Application.DocumentManager.MdiActiveDocument.Editor;
List<Point3d> points = new List<Point3d>();//点集合
Line xian = new Line();//直线
List<Line> lines = new List<Line>(); //直线集合
List<double> lineLenth = new List<double>(); //直线长集合
int nums = lineLenth.Count; //计量
double allLength = 0; // 直线总长
//获取图层
string firstLayName = null;//初始图层名
PromptEntityResult pEntr = ed.GetEntity("\n" + "指定图层所在实体");
if (pEntr.Status == PromptStatus.OK)
{
ObjectId entId = pEntr.ObjectId;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
Entity ent = trans.GetObject(entId, OpenMode.ForRead) as Entity;
firstLayName = ent.Layer;
trans.Commit();
}
}
wind.MessageBox.Show("指定的图层为:" + firstLayName);
//按图层获取数据
TypedValue[] values = new TypedValue[]
{
new TypedValue((int)DxfCode.LayerName,firstLayName),
new TypedValue((int)DxfCode.Start,"LINE"),
};
SelectionFilter filter = new SelectionFilter(values);
PromptSelectionResult psResult = ed.GetSelection(filter);
if (psResult.Status == PromptStatus.OK)
{
ObjectId[] ids = psResult.Value.GetObjectIds();
using (Transaction trans = db.TransactionManager.StartTransaction())
{
foreach (ObjectId item in ids)
{
Entity ents = trans.GetObject(item, OpenMode.ForRead) as Entity;
if (ents is Line)
{
xian = ents as Line;
lines.Add(xian);
lineLenth.Add(xian.Length);
}
else
{
wind.MessageBox.Show("选择的不是直线类型");
}
}
//allLength = CalculateSum(lineLenth);//统计
trans.Commit();
}
//绘制表格
Table tab = new Table();
for (int j = 0; j < lineLenth.Count; j++)
{
PromptPointResult psPoint = ed.GetPoint("选择表格插入位置");
tab.Position = new Point3d(330.4926, 198.9996, 0);
tab.SetSize(j + 2, 5);
//宽度
//tab.Width = 60;
tab.Columns[0].Width = 20;
tab.Columns[1].Width = 20;
tab.Columns[2].Width = 30;
tab.Columns[3].Width = 30;
tab.Columns[4].Width = 30;
//表头
tab.Cells[0, 0].TextString = "统计表";
tab.Cells[1, 0].TextString = "点名";
tab.Cells[1, 1].TextString = "线长";
tab.Cells[1, 2].TextString = "图层名";
tab.Cells[1, 3].TextString = "X坐标";
tab.Cells[1, 4].TextString = "Y坐标";
//内容
tab.Cells[1 + j, 0].TextString = j.ToString();//点名
tab.Cells[1 + j, 1].TextString = lineLenth[j].ToString("0.00");//线长
tab.Cells[1 + j, 2].TextString = firstLayName; //图层名
tab.Cells[1 + j, 3].TextString = lines[j].StartPoint.X.ToString("0.000"); //X坐标
tab.Cells[1 + j, 4].TextString = lines[j].StartPoint.Y.ToString("0.000"); //Y坐标
}
Tools.AddToModelSpace(db, tab);
//添加文字
for (int j = 0; j < lineLenth.Count; j++)
{
DBText dText = new DBText();
Point3d point3 = new Point3d(lines[j].StartPoint.X, lines[j].StartPoint.Y, 0);
points.Add(point3);
dText.Position = points[j];
dText.TextString = j.ToString();//点名;
Tools.AddToModelSpace(db, dText);
}
}
//没有选择
else
{
wind.MessageBox.Show("选择不能为空");
}