netDxf读取dxf文件菜鸟教程


netDxf
netDxf 版权所有 (C) 2009-2023 Daniel Carvajal,根据 MIT 许可证获得许可

GitHub描述

haplokuon/netDxf: .net dxf 读写器 (github.com)

netDxf 是一个用 C# 编程的 .net 库,用于读取和写入 AutoCAD DXF 文件。它支持文本和二进制格式的 AutoCad2000、AutoCad2004、AutoCad2007、AutoCad2010、AutoCad2013 和 AutoCad2018 DXF 数据库版本。

该库易于使用,我尽量保持程序简单明了,例如,您不需要用图层、样式或线型定义填充表格部分。每次添加新项目时,DxfDocument 都会处理这个问题。(摘录自gitgub介绍)

支持框架,Net Framework 4.8 和 NET 6.0 的预定义框架。 如果预定义的框架中未列出所需的版本,请手动编辑 netdxf.csproj 文件并相应地设置 TargetFrameworks。

下载

1.1github下载
下载压缩包解压缩后有一个.sln解决方案还有两个项目文件,一个是netDxf的源码另一个是官方的测试代码。官方的测试代码写的非常好,逻辑清晰注释明确,包含文件读取创建和保存,非常推荐大家从头到位看一遍。

下载完成后右键点击你要用到netDxf的项目,右键点击项目图标,引用,引用项目文件,游览NetDxf下载目录添加后既可以使用。

在这里插入图片描述
1.2.通过VS的NuGet下载,优点下载和版本切换比较方便

请添加图片描述
以上下载方法各有优劣,按个人情况选择
​但无论采用哪种下载方法都要注意版本和框架的问题

2.基本操作

这里采用目标框架 .NET 6.0 ,NetDxf 2022.11.2也是唯一支持.NET 5.0以上框架的版本,其他版本也可以安装但会显示可能有些功能不兼容。
如果你采用的是之前的netDxf版本,例如netDxf 2.2.0.1,就要注意实体名称跟最新版本的区别,检查Github上的 changelog.txt
会发现个别实体名称在2021年12月发生了改变,比较引起报错的应该是 LwPolyline 重命名为 Polyline2D,Polyline 重命名Polyline3D
两种多段线的区别网上有仔细介绍,这里不多言。

[2021/12/06]

  • Rename Face3d to Face3D.
  • Rename Face3dEdgeFlags to Face3DEdgeFlags.
  • Rename DxfDocument.Entities.Faces3d to DxfDocument.Entities.Faces3D.
  • Rename LwPolyline to Polyline2D.
  • Rename LwPolylineVertex to Polyline2DVertex.
  • Rename EntityType.LwPolyline to EntityType.Polyline2D.
  • Rename DxfDocument.Entities.LwPolylines to DxfDocument.Entities.Polylines2D.
  • Rename Polyline to Polyline3D.
  • Rename EntityType.Polyline to EntityType.Polyline3D.
  • Rename Polyline.ToLwPolyline to Polyline3D.ToPolyline2D.
  • Rename DxfDocument.Entities.Polylines to DxfDocument.Entities.Polylines3D.
  • Rename Spline.ToPolyline to Spline.ToPolyline3D.
  • Rename Spline.ToLwPolyline to Spline.ToPolyline2D.
  • Rename Circle.ToPolyline to Circle.ToPolyline2D.
  • Rename Arc.ToPolyline to Arc.ToPolyline2D.
  • Added the method Transform to the UCS class to transform points from world coordinates (WCS) to local/object coordinates (OCS), and vice
    versa.
  • Deleted the property Elevation from the UCS class, although it appears as a parameter in the DXF, it is unnecessary. It seems that it
    does not play any role and it is always zero.
  • Added the property CurrentUCS to the DrawingVariables, that represents the current/active UCS. It encapsulates the the
    DrawingVariables UcsOrg, UcsXDir, and UcsYDir.
  • Deleted the DrawingVariables UcsOrg, UcsXDir, and UcsYDir.
  • Added the property Description to the Layer class. In the DXF, this information is stored as extended data.
  • The list of control points of the Spline entity will not include duplicate points required to build closed periodic splines, they will
    be internally handled.
  • The creation method of all splines loaded from a DXF without fit points will be considered as constructed by control points, despite of
    the value stored in the DXF.
  • The SmoothType (None, Quadratic, or Cubic) can now be set for Polyline2D entity (formerly LwPolyline) and Polyline3D (formerly
    Polyline).
  • Updated the file “netDxf Documentation.chm”.

[2021/12/06]

  • 将 Face3d 重命名为 Face3D。
  • 将 Face3dEdgeFlags 更名为 Face3DEdgeFlags。
  • 将 DxfDocument.Entities.Faces3d 重命名为 DxfDocument.Entities.Faces3D。
  • 将 LwPolyline 重命名为 Polyline2D。
  • 将 LwPolylineVertex 重命名为 Polyline2DVertex。
  • 将实体类型.LwPolyline 重命名为实体类型.Polyline2D。
  • 将 DxfDocument.Entities.LwPolylines 重命名为 DxfDocument.Entities.Polylines2D。
  • 将 DxfDocument.Entities.LwPolylines 重命名为 DxfDocument.Entities.Polylines2D
  • 将实体类型.Polyline 重命名为实体类型.Polyline3D。
  • 将 Polyline.ToLwPolyline 重命名为 Polyline3D.ToPolyline2D
  • 将 DxfDocument.Entities.Polylines 重命名为 DxfDocument.Entities.Polylines3D。

2.1dxf文件读取

//引用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using netDxf.Entities;
using netDxf;//导入文件不要只导入一个netDxf其他的也要导入
using netDxf.Header;
using netDxf.Tables;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Units;
using netDxf.IO;
using System.Diagnostics;
namespace test
{
   
    public class ReadDxf
    {
      

       #region 创建DxfDocument对象,读取dxf文件
  	   //如果文件不存在,则返回空的DxfDocument
		public DxfDocument ReadDxfFile(string file)
        {
   
            DxfDocument dxf = new DxfDocument();

            //如果你只想要一个空的DxfDocument,而不是加载已存在的dxf文件,可以直接return dxf;
            //判断文件是否合法
            if (CheckFile(file)&&CheckVersion(file))
            {
   
                dxf=DxfDocument.Load(file);
                Console.WriteLine("读取成功", file);
                return dxf;
            }

            Console.WriteLine("读取失败", file);
            return dxf;
        }
      
		private bool CheckFile(string file)
        {
   
            FileInfo fileInfo = new FileInfo(file);

            if (!fileInfo.Exists)
            {
   
                Console
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值