dxf【C#】

2023-12-07 08:44
文章标签 c# .net netcore dxf

本文主要是介绍dxf【C#】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1加载dxf文件: 

以下是一个C#提取dxf文件的demo:

using netDxf;
using System;namespace DxfHelper
{class Program{static void Main(string[] args){string path = @"C:\example.dxf";DxfDocument dxfDocument = DxfDocument.Load(path);Console.WriteLine("DXF文件中有{0}个实体。", dxfDocument.Entities.Count);}}
}

这个demo使用了netDxf库来加载dxf文件,并输出了文件中实体的数量。在使用这个demo之前,需要先在Visual Studio中安装netDxf库。

2案例

以下是C#提取dxf文件并显示在窗口上的步骤:

1. 首先,需要安装一个DXF解析库,例如netDxf。

2. 在Visual Studio中创建一个新的Windows Forms应用程序。

3. 在项目中添加netDxf库的引用。

4. 创建一个PictureBox控件,用于显示DXF文件。

5. 创建一个打开文件对话框,以便用户选择要显示的DXF文件。

6. 在打开文件对话框的FileOk事件处理程序中,使用netDxf库加载DXF文件并将其显示在PictureBox控件中。

以下是示例代码:

using System;
using System.Windows.Forms;
using netDxf;
using netDxf.Entities;namespace DXFViewer
{public partial class MainForm : Form{public MainForm(){InitializeComponent();}private void openToolStripMenuItem_Click(object sender, EventArgs e){OpenFileDialog openFileDialog = new OpenFileDialog();openFileDialog.Filter = "DXF Files (*.dxf)|*.dxf";if (openFileDialog.ShowDialog() == DialogResult.OK){DxfDocument dxf = DxfDocument.Load(openFileDialog.FileName);if (dxf != null){pictureBox.Image = dxf.Draw();}}}}
}

3dxf转g代码:

根据提供的引用内容,我们可以使用.netDxf库来读取DXF文件,然后将其转换为Gcode文件。具体步骤如下:

1. 首先,我们需要使用.netDxf库来读取DXF文件。可以使用以下代码:

DxfDocument dxf = DxfDocument.Load("example.dxf");

2. 接下来,我们需要遍历DXF文件中的实体,并将其转换为Gcode指令。可以使用以下代码:

foreach (DxfEntity entity in dxf.Entities)
{// 将实体转换为Gcode指令string gcode = ConvertEntityToGcode(entity);// 将Gcode指令写入文件File.AppendAllText("example.gcode", gcode);
}

3. 在ConvertEntityToGcode方法中,我们需要将DXF实体转换为Gcode指令。这需要根据实体类型进行不同的处理。以下是一些常见实体类型的转换方法:

   - 直线:

DxfLine line = (DxfLine)entity;
string gcode = $"G1 X{line.StartPoint.X} Y{line.StartPoint.Y} Z{line.StartPoint.Z} X{line.EndPoint.X} Y{line.EndPoint.Y} Z{line.EndPoint.Z}\n";

   - 圆弧:

DxfArc arc = (DxfArc)entity;
string gcode = $"G2 X{arc.EndPoint.X} Y{arc.EndPoint.Y} Z{arc.EndPoint.Z} I{arc.Center.X} J{arc.Center.Y} K{arc.Center.Z}\n";

   - 多段线:

DxfPolyline polyline = (DxfPolyline)entity;
string gcode = "";
for (int i = 0; i < polyline.Vertexes.Count - 1; i++)
{DxfVertex start = polyline.Vertexes[i];DxfVertex end = polyline.Vertexes[i + 1];gcode += $"G1 X{start.Location.X} Y{start.Location.Y} Z{start.Location.Z} X{end.Location.X} Y{end.Location.Y} Z{end.Location.Z}\n";
}

4. 最后,我们将所有的Gcode指令写入到一个文件中,即可得到Gcode文件。

完整代码如下:

using System.IO;
using netDxf;class Program
{static void Main(string[] args){DxfDocument dxf = DxfDocument.Load("example.dxf");foreach (DxfEntity entity in dxf.Entities){string gcode = ConvertEntityToGcode(entity);File.AppendAllText("example.gcode", gcode);}}static string ConvertEntityToGcode(DxfEntity entity){if (entity is DxfLine){DxfLine line = (DxfLine)entity;return $"G1 X{line.StartPoint.X} Y{line.StartPoint.Y} Z{line.StartPoint.Z} X{line.EndPoint.X} Y{line.EndPoint.Y} Z{line.EndPoint.Z}\n";}else if (entity is DxfArc){DxfArc arc = (DxfArc)entity;return $"G2 X{arc.EndPoint.X} Y{arc.EndPoint.Y} Z{arc.EndPoint.Z} I{arc.Center.X} J{arc.Center.Y} K{arc.Center.Z}\n";        }else if (entity is DxfPolyline){DxfPolyline polyline = (DxfPolyline)entity;string gcode = "";for (int i = 0; i < polyline.Vertexes.Count - 1; i++){DxfVertex start = polyline.Vertexes[i];DxfVertex end = polyline.Vertexes[i + 1];gcode += $"G1 X{start.Location.X} Y{start.Location.Y} Z{start.Location.Z} X{end.Location.X} Y{end.Location.Y} Z{end.Location.Z}\n";}return gcode;}else{// 其他实体类型的转换方法return "";}}
}

这篇关于dxf【C#】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/465231

相关文章

2. c#从不同cs的文件调用函数

1.文件目录如下: 2. Program.cs文件的主函数如下 using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using System.Windows.Forms;namespace datasAnalysis{internal static

C#实战|大乐透选号器[6]:实现实时显示已选择的红蓝球数量

哈喽,你好啊,我是雷工。 关于大乐透选号器在前面已经记录了5篇笔记,这是第6篇; 接下来实现实时显示当前选中红球数量,蓝球数量; 以下为练习笔记。 01 效果演示 当选择和取消选择红球或蓝球时,在对应的位置显示实时已选择的红球、蓝球的数量; 02 标签名称 分别设置Label标签名称为:lblRedCount、lblBlueCount

用命令行的方式启动.netcore webapi

用命令行的方式启动.netcore web项目 进入指定的项目文件夹,比如我发布后的代码放在下面文件夹中 在此地址栏中输入“cmd”,打开命令提示符,进入到发布代码目录 命令行启动.netcore项目的命令为:  dotnet 项目启动文件.dll --urls="http://*:对外端口" --ip="本机ip" --port=项目内部端口 例: dotnet Imagine.M

C# dateTimePicker 显示年月日,时分秒

dateTimePicker默认只显示日期,如果需要显示年月日,时分秒,只需要以下两步: 1.dateTimePicker1.Format = DateTimePickerFormat.Time 2.dateTimePicker1.CustomFormat = yyyy-MM-dd HH:mm:ss Tips:  a. dateTimePicker1.ShowUpDown = t

C#关闭指定时间段的Excel进程的方法

private DateTime beforeTime;            //Excel启动之前时间          private DateTime afterTime;               //Excel启动之后时间          //举例          beforeTime = DateTime.Now;          Excel.Applicat

C# 防止按钮botton重复“点击”的方法

在使用C#的按钮控件的时候,经常我们想如果出现了多次点击的时候只让其在执行的时候只响应一次。这个时候很多人可能会想到使用Enable=false, 但是实际情况是还是会被多次触发,因为C#采用的是消息队列机制,这个时候我们只需要在Enable = true 之前加一句 Application.DoEvents();就能达到防止重复点击的问题。 private void btnGenerateSh

C# double[] 和Matlab数组MWArray[]转换

C# double[] 转换成MWArray[], 直接赋值就行             MWNumericArray[] ma = new MWNumericArray[4];             double[] dT = new double[] { 0 };             double[] dT1 = new double[] { 0,2 };

C# Hash算法之MD5、SHA

MD5我们用的还是比较多的,一般用来加密存储密码。但是现在很多人觉MD5可能不太安全了,所以都用上了SHA256等来做加密(虽然我觉得都差不多,MD5还是能玩)。 还是跟上一篇说的一样,当一个算法的复杂度提高的同时肯定会带来效率的降低,所以SHA和MD5比较起来的话,SHA更安全,MD5更高效。 由于HASH算法的不可逆性,所以我认为MD5和SHA主要还是应用在字符串的"加密"上。 由于

C#线程系列(1):BeginInvoke和EndInvoke方法

一、线程概述 在操作系统中一个进程至少要包含一个线程,然后,在某些时候需要在同一个进程中同时执行多项任务,或是为了提供程序的性能,将要执行的任务分解成多个子任务执行。这就需要在同一个进程中开启多个线程。我们使用 C# 编写一个应用程序(控制台或桌面程序都可以),然后运行这个程序,并打开 windows 任务管理器,这时我们就会看到这个应用程序中所含有的线程数,如下图所示。

C#设计模式(1)——单例模式(讲解非常清楚)

一、引言 最近在学设计模式的一些内容,主要的参考书籍是《Head First 设计模式》,同时在学习过程中也查看了很多博客园中关于设计模式的一些文章的,在这里记录下我的一些学习笔记,一是为了帮助我更深入地理解设计模式,二同时可以给一些初学设计模式的朋友一些参考。首先我介绍的是设计模式中比较简单的一个模式——单例模式(因为这里只牵涉到一个类) 二、单例模式的介绍 说到单例模式,大家第一