工程项目中CAD作为常用的设计软件
有大量的设计数据及信息需要在项目中使用,通过C#的netDxf reader读取cad文件的元数据
github地址:
GitHub - haplokuon/netDxf: .net dxf Reader-Writer
代码示例:
写入dxf
public static void Main()
{
// your DXF file name
string file = "sample.dxf";
// create a new document, by default it will create an AutoCad2000 DXF version
DxfDocument doc = new DxfDocument();
// an entity
Line entity = new Line(new Vector2(5, 5), new Vector2(10, 5));
// add your entities here
doc.AddEntity(entity);
// save to file
doc.Save(file);
}
读取dxf
public static void Main()
{
// your DXF file name
String file = "sample.dxf";
bool isBinary;
DxfVersion dxfVersion = DxfDocument.CheckDxfFileVersion(file, out isBinary);
// check if the file is a dxf
if (dxfVersion == DxfVersion.Unknown)
{
Console.WriteLine("THE FILE {0} IS NOT A VALID DXF OR THE DXF DOES NOT INCLUDE VERSION INFORMATION IN THE HEADER SECTION", file);
Console.WriteLine();
return null;
}
// check if the dxf file version is supported
if (dxfVersion < DxfVersion.AutoCad2000)
{
Console.WriteLine("THE FILE {0} IS NOT A SUPPORTED DXF", file);
Console.WriteLine();
Console.WriteLine("FILE VERSION: {0}", dxfVersion);
Console.WriteLine();
return null;
}
DxfDocument dxf = DxfDocument.Load(file);
var LwPolylines = dxf.LwPolylines.ToList();
Console.WriteLine("多段线条数:" + LwPolylines.Count);
}