gpt4 book ai didi

c# - 使用 WPF ArcSegment 绘制 DXF 圆弧

转载 作者:行者123 更新时间:2023-11-30 15:29:30 26 4
gpt4 key购买 nike

我正在尝试使用 WPF 在 Canvas 上绘制导入的 DXF 文件。我一直在使用 DXFLib(可用 here)来读取和解析各种文件,它似乎工作得很好。

我现在正在绘制 DXF 中的所有实体,但我受困于 ARC。我的问题与这篇文章中的问题类似(一些弧线绘制方向错误):

DXF Parser : Ellipses angle direction

在 DXF 文件中,ARC 存储为:Center、Radius、StartAngle、EndAngle;在 WPF 中,ArcSegment 被描述为:StartPoint、EndPoint、Size、IsLargeArc 和 SweepDirection。

我认为后者是导致问题的原因,因为我无法通过 DXF 文件确定方向。在上面的问题中声明使用“拉伸(stretch)方向”,它恰好不包含在我的文件中。

按照相关代码:

private Load(string filename)
{
DXFLib.DXFDocument doc = new DXFLib.DXFDocument();
doc.Load(filename);

if (doc.Entities.Count > 0)
{
foreach (DXFLib.DXFEntity entity in doc.Entities)
{
if (entity is DXFLib.DXFLine)
{
DXFLib.DXFLine line = (DXFLib.DXFLine)entity;
PointF start = new PointF((float)line.Start.X, (float)line.Start.Y);
PointF end = new PointF((float)line.End.X, (float)line.End.Y);

Line drawLine = new Line();
drawLine.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
drawLine.X1 = end.X * scaleX;
drawLine.X2 = start.X * scaleX;
drawLine.Y1 = end.Y * scaleY;
drawLine.Y2 = start.Y * scaleY;
drawLine.StrokeThickness = 1;
canvas.Children.Add(drawLine);
}
else if (entity is DXFLib.DXFCircle)
{
DXFLib.DXFCircle circle = (DXFLib.DXFCircle)entity;
Ellipse drawCircle = new Ellipse();
SolidColorBrush solidBrush = new SolidColorBrush();
solidBrush.Color = System.Windows.Media.Color.FromArgb(255, 255, 255, 0);
drawCircle.Fill = solidBrush;
drawCircle.StrokeThickness = 1;
drawCircle.Stroke = System.Windows.Media.Brushes.RoyalBlue;
drawCircle.Width = circle.Radius * 2 * scaleX;
drawCircle.Height = circle.Radius * 2 * scaleY;
drawCircle.Margin = new Thickness((circle.Center.X.Value - circle.Radius) * scaleX, (circle.Center.Y.Value - circle.Radius) * scaleY, 0, 0);
canvas.Children.Add(drawCircle);
}
else if (entity is DXFLib.DXFArc)
{
DXFLib.DXFArc arc = (DXFLib.DXFArc)entity;

Path path = new Path();
path.Stroke = System.Windows.Media.Brushes.Black;
path.StrokeThickness = 1;

System.Windows.Point endPoint = new System.Windows.Point(
(arc.Center.X.Value + Math.Cos(arc.EndAngle * Math.PI / 180) * arc.Radius) * scaleX,
(arc.Center.Y.Value + Math.Sin(arc.EndAngle * Math.PI / 180) * arc.Radius) * scaleY);

System.Windows.Point startPoint = new System.Windows.Point(
(arc.Center.X.Value + Math.Cos(arc.StartAngle * Math.PI / 180) * arc.Radius) * scaleX,
(arc.Center.Y.Value + Math.Sin(arc.StartAngle * Math.PI / 180) * arc.Radius) * scaleY);

ArcSegment arcSegment = new ArcSegment();
double sweep = 0.0;
if (arc.EndAngle < arc.StartAngle)
sweep = (360 + arc.EndAngle) - arc.StartAngle;
else sweep = Math.Abs(arc.EndAngle - arc.StartAngle);

arcSegment.IsLargeArc = sweep >= 180;
arcSegment.Point = endPoint;
arcSegment.Size = new System.Windows.Size(arc.Radius * scaleX, arc.Radius * scaleY);
arcSegment.SweepDirection = arc.ExtrusionDirection.Z >= 0 ? SweepDirection.Clockwise : SweepDirection.Counterclockwise;

PathGeometry geometry = new PathGeometry();
PathFigure pathFigure = new PathFigure();
pathFigure.StartPoint = startPoint;
pathFigure.Segments.Add(arcSegment);
geometry.Figures.Add(pathFigure);

path.Data = geometry;
canvas.Children.Add(path);
}
}
}
}

XAML 文件示例如下:

<Window x:Class="DxfViewer.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Canvas Name="canvas">
<Canvas.LayoutTransform>
<ScaleTransform ScaleX="1" ScaleY="-1" CenterX=".5" CenterY=".5" />
</Canvas.LayoutTransform>
</Canvas>
</Window>

**更新**

问题已通过以下方式解决:

ArcSegment arcSegment = new ArcSegment();
double sweep = 0.0;
if (arc.EndAngle < arc.StartAngle)
sweep = (360 + arc.EndAngle) - arc.StartAngle;
else sweep = Math.Abs(arc.EndAngle - arc.StartAngle);

arcSegment.IsLargeArc = sweep >= 180;
arcSegment.Point = endPoint;
arcSegment.Size = new System.Windows.Size(arc.Radius * scaleX, arc.Radius * scaleY);
arcSegment.SweepDirection = arc.ExtrusionDirection.Z >= 0 ? SweepDirection.Clockwise : SweepDirection.Counterclockwise;

最佳答案

根据 AutoDesk,这是弧的 DXF 引用:

Group codes Description
100

Subclass marker (AcDbCircle)
39

Thickness (optional; default = 0)
10

Center point (in OCS)
DXF: X value; APP: 3D point
20, 30

DXF: Y and Z values of center point (in OCS)
40

Radius
100

Subclass marker (AcDbArc)
50

Start angle
51

End angle
210

Extrusion direction. (optional; default = 0, 0, 1)
DXF: X value; APP: 3D vector
220, 230

DXF: Y and Z values of extrusion direction (optional)

DXFLib 的作者在他的 DxfArc 类中考虑了这些,那么这些值在运行时根本没有设置吗?我在他的代码中没有看到任何用于设置默认值的内容,这可能需要添加,因为 AutoCAD 正在做出假设。

挤压方向必须存储为值 220、230,否则默认值应该始终有效。如果情况并非如此,我将仔细查看这些 DFX 文件的运行方式。它们是不支持此操作的早期版本吗?

更新:

我认为确实应该根据代码和您的项目修改 DxfArc 类,将空 ExtrusionDirection 设置为 {0, 0, 1}。我通过以下更改修改了您的主要例程,它似乎可以正常工作:

// Changing the class will make this less ugly
var arc = (DXFLib.DXFArc)entity;
if (arc.ExtrusionDirection.X == null ||
arc.ExtrusionDirection.Y == null ||
arc.ExtrusionDirection.Z == null)
{
arc.ExtrusionDirection.X = 0;
arc.ExtrusionDirection.Y = 0;
arc.ExtrusionDirection.Z = 1;
}

arcSegment.SweepDirection = arc.ExtrusionDirection.Z > 0
? SweepDirection.Clockwise
: SweepDirection.Counterclockwise;

当您在 AutoCAD 中查看圆弧时,ExtrusionDirection 在实体属性下列为“Normal”。

关于c# - 使用 WPF ArcSegment 绘制 DXF 圆弧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23633665/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com