是.net框架下C#开发的读写AutoCAD DXF文件的开源库。支持AutoCad2000, AutoCad2004, AutoCad2007, AutoCad2010, AutoCad2013, and AutoCad2018 DXF的文本及二进制格式。源码面向对象开发的程度极高,在AutoDesk的官方说明文档里几乎都能找到对应的类,自己做应用开发时遇到问题后首先要从该文档入手。
源码地址:https://github.com/haplokuon/netDxf。有了.net core的环境后,找到.sln解决方案文件,打开后可以直接编译运行。包含两个项目,一个是netdxf类库项目,一个测试样例应用程序项目。由于源码的面向对象质量很高,应用开发直接学习样例项目中的编程写法即可。
源码片段1(program.cs):
DxfDocument doc = Test(@"sample.dxf");//读dxf文件
HeaderVariable headerVariable;//dxf文件头
// The ExtMin and ExtMax header variables cannot be directly accessed, now they will be added as custom header variables if the DXF has them
// they have been deleted since netDxf does not calculate them
Vector3 extMin;
//读cad自定义全局变量的写法,全局变量的含义及用途参考CAD官方文件
if (doc.DrawingVariables.TryGetCustomVariable("$EXTMIN", out headerVariable))
{
extMin = (Vector3)headerVariable.Value;
}
Vector3 extMax;
if (doc.DrawingVariables.TryGetCustomVariable("$EXTMAX", out headerVariable))
{
extMax = (Vector3)headerVariable.Value;
}
Vector2 limMin;
if (doc.DrawingVariables.TryGetCustomVariable("LIMMIN", out headerVariable))
{
limMin = (Vector2)headerVariable.Value;
}
Vector2 limMax;
if (doc.DrawingVariables.TryGetCustomVariable("LIMMAX", out headerVariable))
{
limMax = (Vector2)headerVariable.Value;
}
源码片段2:
public static void AddHeaderVariable()
{
//DxfDocument doc = DxfDocument.Load(@"sample.dxf");
//写dxf文件
DxfDocument doc = new DxfDocument();
HeaderVariable headerVariable;
// The ExtMin and ExtMax header variables cannot be directly accessed, now they will be added as custom header variables if the DXF has them
// they have been deleted since netDxf does not calculate them
Vector3 extMin;
if (doc.DrawingVariables.TryGetCustomVariable("$EXTMIN", out headerVariable))
{
extMin = (Vector3) headerVariable.Value;
}
Vector3 extMax;
if (doc.DrawingVariables.TryGetCustomVariable("$EXTMAX", out headerVariable))
{
extMax = (Vector3) headerVariable.Value;
}
// you can try to get a header variable and modify it or create a new one if it does not exists
if (doc.DrawingVariables.TryGetCustomVariable("$SPLINESEGS", out headerVariable))
{
headerVariable.Value = (short) 5; // make sure you pass the correct value type, the code group 70 corresponds to a short
}
else
{
doc.DrawingVariables.AddCustomVariable(new HeaderVariable("$SPLINESEGS", 70, (short) 5));
}
//移除并添加自定义变量的方法,如果没有已存在该变量,直接添加会报错
// or you can remove a header variable, even if it does not exist and add a new one
doc.DrawingVariables.RemoveCustomVariable("$MEASUREMENT");
doc.DrawingVariables.AddCustomVariable(new HeaderVariable("$MEASUREMENT", 70, (short) 0));
//保存dxf到指定路径
doc.Save("test.dxf");
}
项目需要绘制断面图,有跨平台的需求,开发时间也紧张。故利用c#开发效率高,同时net core支持跨平台的特点,选用netdxf库。部分源码如下(删减掉部分代码,仅供参考):
DxfDocument doc = new DxfDocument(DxfVersion.AutoCad2007);
AciColor textColor = new AciColor(0, 0, 0);
#region Header
doc.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
doc.DrawingVariables.LUnits = netDxf.Units.LinearUnitType.Architectural;
doc.DrawingVariables.LUprec = 8;
//hdm是自己写的类
Vector2 vmin = new Vector2(hdm.FrameMinX, hdm.FrameMinY);
Vector2 vmax = new Vector2(hdm.FrameMaxX, hdm.FrameMaxY + 0.04);
//第一个参数是CAD的全局变量,其含义参考CAD的官方文档;第二参数是groupid,其值也参考官方文档
HeaderVariable limmin = new HeaderVariable("$LIMMIN", 20, vmin);
doc.DrawingVariables.AddCustomVariable(limmin);
HeaderVariable limmax = new HeaderVariable("$LIMMAX", 20, vmax);
doc.DrawingVariables.AddCustomVariable(limmax);
HeaderVariable extmin = new HeaderVariable("$EXTMIN", 20, vmin);
doc.DrawingVariables.AddCustomVariable(extmin);
HeaderVariable extmax = new HeaderVariable("$EXTMAX", 20, vmax);
doc.DrawingVariables.AddCustomVariable(extmax);
//全局变量的不同写法
doc.DrawingVariables.LwDisplay = true;//控制显示线宽,如果不设置会导致线宽不生效
//全局比例因子,配合线型对象的比例因子参数使用,否则虚线不生效
doc.DrawingVariables.LtScale = 1000.0;
#endregion
...
#region Frame
Layer lyrFrame = new Layer("FrameLine")
{
Color = textColor,
IsVisible = true,
Linetype = Linetype.Continuous,
Lineweight = Lineweight.W40
};
//以下代码是绘制四条直线段构成边框;Line是直线段对象,同CAD一样还有MLine,Polyline,Rec等都可以用来绘制边框,这个版本的MLine有bug,在CAD打开后线条显示有偏移。
Vector2 p1 = new Vector2(hdm.FrameMinX, hdm.FrameMaxY);
Vector2 p2 = new Vector2(hdm.FrameMaxX, hdm.FrameMaxY);
Vector2 p3 = new Vector2(hdm.FrameMaxX, hdm.FrameMinY);
Vector2 p4 = new Vector2(hdm.FrameMinX, hdm.FrameMinY);
Line fram1 = new Line(p1, p2) { Layer = lyrFrame };
doc.Entities.Add(fram1);
Line fram2 = new Line(p2, p3) { Layer = lyrFrame };
doc.Entities.Add(fram2);
Line fram3 = new Line(p3, p4) { Layer = lyrFrame };
doc.Entities.Add(fram3);
Line fram4 = new Line(p4, p1) { Layer = lyrFrame };
doc.Entities.Add(fram4);
#endregion
...
//如果要显示中文,得用simsun字体,否则会乱码
TextStyle generalTextStyle = new TextStyle("text", "simsun", netDxf.Tables.FontStyle.Regular);
AciColor textColor = new AciColor(0, 0, 0);
Layer lyrTxt = new Layer("Text")
{
Color = textColor,
IsVisible = true
};
double fontHeight = 0.006;//字体高度根据自身需要设置,我这里基本单位按照1m的长度搭配
string titlestr = zdm.Title;
double titlex = zdm.FrameMinX + (zdm.FrameMaxX - zdm.FrameMinX) / 2.0;
double titley = zdm.FrameMaxY + 0.01;
Text titletext = new Text(titlestr, new Vector2(titlex, titley), 0.01)
{
Layer = lyrTxt,
Style = generalTextStyle,
Rotation = 0,
Alignment = TextAlignment.BaselineCenter
};
//添加标题文本
doc.Entities.Add(titletext);
...
//画一条虚线直线段
Layer lyrAssistLine = new Layer("dashedLine")
{
Color = new AciColor(0, 0, 0),
IsVisible = true,
Linetype = Linetype.Dashed,
Lineweight=Lineweight.W13
};
Vector2 assist1 = new Vector2(centerPosX, ptBottomY);
Vector2 assist2 = new Vector2(centerPosX,assitMinY);
Line assistline = new Line(assist1, assist2) { Layer = lyrAssistLine, Linetype = Linetype.Dashed,Color=ptcolor , LinetypeScale = 0.00001};
doc.Entities.Add(assistline);
...
// 保存文件,第二个参数true是保存为二进制格式,false是保存为文本格式
doc.Save(dxffile, false);