如图所示,CAD图中line和pline坐标和图层数据导出到txt文本。
程序运行后导出如下文件:
附部分源代码:
public static void DwgToTxt(this Database db)
{
// var db = Z.db;
var ed = Z.ed;
//Point3d pt ;
BlockData data = new BlockData();
List<BlockData> datas = new List<BlockData>();
List<string> str = new List<string>();
using (var tr = db.TransactionManager.StartTransaction())
{
var bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
var btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
int i = 0;
int linenum = 1;
int plinenum = 1;
foreach (var obj in btr)
{
//Double x;
//double y;
//Point3d pt = new Point3d(x, y, 0);
Entity ent = obj.GetObject(OpenMode.ForRead) as Entity;
if (ent != null)
{
if (ent is Curve)
{
if (ent is Line)
{
str.Add("\n" + linenum + ": Line:\n");
Line line = (Line)ent;
data.layerName = "图层:" + line.Layer + "\n";
data.X = line.StartPoint.X.ToString("F3");
data.Y = line.StartPoint.Y.ToString("F3");
datas.Add(data);
str.Add(datas[i].layerName + datas[i].X + "," + datas[i].Y);
i = i + 1;
linenum = linenum + 1;
}
else if (ent is Polyline)
{
str.Add("\n" + plinenum + "PLine:\n");
Polyline pline = (Polyline)ent;
for (int k = 0; k < pline.NumberOfVertices; k++)
{
// Polyline pline = (Polyline)ent;
data.layerName = "图层:" + pline.Layer + "\n";
data.X = pline.GetPoint3dAt(k).X.ToString("F3") ;
data.Y = pline.GetPoint3dAt(k).Y.ToString("F3");
datas.Add(data);
str.Add(datas[i].layerName + datas[i].X + "," + datas[i].Y);
i = i + 1;
}
plinenum = plinenum + 1;
}
}
}
}
}
SaveFileDialog savefd;
DialogResult saveDlgRes;
db.Sfd(out savefd, out saveDlgRes);
if (saveDlgRes == Wform.DialogResult.OK)
{
string[] contents = new string[str.Count];
for (int j = 0; j < datas.Count; j++)
{
contents[j] = str[j];//datas[j].layerName + ",\n" + datas[j].X + "\n" + datas[j].Y;
}
File.WriteAllLines(savefd.FileName, contents);
}
}
部分方法为个人封装方法,如打开文件对话框,保存文件对话框。部分字段也进行了封装,如定义数据库database db等。
CAD二开插件定制:↓↓↓
标签:pline,db,ent,str,二次开发,txt,data,datas,CAD From: https://blog.csdn.net/yongshiqq/article/details/142938411