C#netdxf库读、写、绘制CAD的dxf文件
C#netdxf库读、写、绘制CAD的dxf文件
1.netdxf介绍
是.net框架下C#开发的读写AutoCAD DXF文件的开源库。支持AutoCad2000, AutoCad2004, AutoCad2007, AutoCad2010, AutoCad2013, and AutoCad2018 DXF的文本及二进制格式。源码面向对象开发的程度极高,在AutoDesk的官方说明文档里几乎都能找到对应的类,自己做应用开发时遇到问题后首先要从该文档入手。
源码地址:https://github.com/haplokuon/netDxf。有了.net core的环境后,找到.sln解决方案文件,打开后可以直接编译运行。包含两个项目,一个是netdxf类库项目,一个测试样例应用程序项目。由于源码的面向对象质量很高,应用开发直接学习样例项目中的编程写法即可。
2.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 themVector3 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 themVector3 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 existsif (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 onedoc.DrawingVariables.RemoveCustomVariable("$MEASUREMENT");doc.DrawingVariables.AddCustomVariable(new HeaderVariable("$MEASUREMENT", 70, (short) 0));//保存dxf到指定路径doc.Save("test.dxf");}
3.应用开发案例
项目需要绘制断面图,有跨平台的需求,开发时间也紧张。故利用c#开发效率高,同时net core支持跨平台的特点,选用netdxf库。部分源码如下(删减掉部分代码,仅供参考):
DxfDocument doc = new DxfDocument(DxfVersion.AutoCad2007);AciColor textColor = new AciColor(0, 0, 0);#region Headerdoc.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 FrameLayer 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);
C#netdxf库读、写、绘制CAD的dxf文件相关推荐
- 三、使用Teigha.net打开CAD(.dwg/.dxf)文件,并显示到panel界面绑定事件
目录 一.Teigha.net直接操作CAD(.dwg/.dxf)文件 01 .直接打开CAD(.dwg/.dxf) 02 .读取实体,修改后的CAD(.dwg/.dxf)文件进行保存 03 .Tei ...
- python导出dxf图,使用Python操作CAD的dxf文件,批量绘制变形图的方法记录
使用Python的ezdxf包,结合excel表中的坐标数据,绘制了变形图,表格,代码,结果如下: DK41+175 左上偏距 左上高程 拱顶偏距 拱顶高程 右上偏距 右上高程 422 上导第二层拱架 ...
- 使用Python操作CAD的dxf文件,批量绘制变形图的方法记录
使用Python的ezdxf包,结合excel表中的坐标数据,绘制了变形图,表格,代码,结果如下: DK41+175 左上偏距 左上高程 拱顶偏距 拱顶高程 右上偏距 右上高程 422 上导第二层 ...
- Numpy简易教程7——读/写文件
读/写文件 NumPy的文件读/写主要有二进制的文件读/写和文件列表形式的数据读/写两种形式.学会读/写文件是利用NumPy进行数据处理的基础.NumPy提供了若干函数,可以把结果保存到二进制或文本文 ...
- IBM SPSS的Sav文件读/写
本文只要介绍通过IBM提供的库读/写sav文件格式. 在github上有.net版本和python版本,本文主要介绍用C读写sav文件. 参考文档:Input-Output Module.pdf,该文 ...
- Pandas读/写PG数据库
直接上代码,使用pandas库读写操作pgsql数据库,第一段代码数据库操作类pg_connecting.py,第二段数据库连接信息类setting.py,第三段实例化 # coding: utf-8 ...
- 实验九 使用异步方式实现文件读\写
实验九 使用异步方式实现文件读\写 一.实验目的 了解Windows系统异步文件读/写的概念. 熟悉Windows系统文件读/写相关的API. 掌握采用异步方式实现文件读/写的相关参数设置. 二.实验 ...
- 一、[专栏内容简介-免费试读-修改链接]使用Teigha.net完成.net winfrom界面修改读取dwg/dxf文件,类似CAD看图王软件无AutoCAD环境下操作显示CAD文件的功能
目标 使用teigha.net完成部分CAD看图王软件功能,实现对CAD(dwg/dxf文件)的读取,修改,保存,添加实体等操作,基本功能如下所示,类似cad看图王软件功能的界面布局. 使用类库与重点 ...
- CAD的DXF解析中LWPOLYLINE多线段凸度的相关概念和弧度转换
最近解析CAD的DXF文件时需要对合并后的多线段LWPOLYLINE组码进行解析,网上搜集了些相关资料,借鉴并摘录,同时记录下链接,以防重新搜索. 1."已知圆弧的起点.端点和凸度,计算圆心 ...
- CAD中的dxf文件解析(三):多段线篇
1.前言 在前面的CAD中的dxf文件解析(二)中讲到了一些CAD的dxf文件解析点.线.圆弧.圆.块等的思路.下面提供链接: (二): CAD中的dxf文件解析(二):dxflib的使用_不爱学习 ...
最新文章
- c++ 共享内存_关于Linux共享内存的实验 [二] - 原因
- python3.6字典有序_为什么从Python 3.6开始字典有序并效率更高
- java web与android互通的aes算法
- MongoDB增删改
- php有哪些程序结构,PHP常用控制结构
- n平方的求和公式_高中数学:数列求和及数列的综合应用,掌握常见模型
- 《从零开始学习jQuery》及《jQuery风暴》学习笔记
- 创建variant二维数组
- 算法之【辗转相除法】
- 限制UITextView输入字数(兼容iOS7)
- 分布式中使用redis进行session共享
- django3.0入门教程【三】:Hello,world!(完整踩坑笔记)
- 【Javascript】length属性
- python的运行机制是什么_Python 程序运行机制
- 3种重新启动或强制关闭任何Mac死机的方法
- jupyter notebook 安装nbextension 不显示插件怎么办?
- 手风琴效果(vue实现)
- Gohead学习笔记
- extjs json 数据的操作 自由操作服务器返回的json数据
- QT报错:“pure virtual method called; terminate called without an active exception“
热门文章
- 超实用VS Code插件推荐
- 唐宇迪 python 的免费课程 分享
- 资源分配博弈之纳什均衡和斯塔克尔伯格模型
- VS2015的下载地址和安装教程
- Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you‘re try
- Mongo 多语言模糊匹配
- Unity URP 内置的 ParticlesLit.shader 中的 fallback 拼写错误,导致 fallback 找不到 SimpleLit,改为 Simple Lit即可
- java导入jdk源码_eclipse导入JDK源码
- 基于KNX技术设计的智能照明控制系统在医院的设计与应用
- Altium Designer20 PCB封装库制作