feat: switch from netDxf to ACadSharp for DXF operations
Replace vendored netDxf git submodule with ACadSharp NuGet package (v3.1.32). This aligns the DXF dependency with the parent project. API migration: DxfDocument→CadDocument, Vector3→XYZ, Vector2→XY, MText.Position→MText.InsertPoint, Line.Linetype→Line.LineType, DxfDocument.Load()→DxfReader, doc.Save()→DxfWriter. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
3
.gitmodules
vendored
3
.gitmodules
vendored
@@ -1,3 +0,0 @@
|
|||||||
[submodule "netDxf"]
|
|
||||||
path = netDxf
|
|
||||||
url = https://github.com/haplokuon/netDxf.git
|
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
using netDxf;
|
using ACadSharp.Entities;
|
||||||
using netDxf.Entities;
|
|
||||||
using netDxf.Tables;
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
@@ -69,7 +67,7 @@ namespace EtchBendLines
|
|||||||
|
|
||||||
public double? Angle { get; set; }
|
public double? Angle { get; set; }
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
=> $"{Direction} {(Angle?.ToString("0.##") ?? "?")}° R{(Radius?.ToString("0.##") ?? "?")}";
|
=> $"{Direction} {(Angle?.ToString("0.##") ?? "?")}° R{(Radius?.ToString("0.##") ?? "?")}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
using netDxf;
|
using ACadSharp;
|
||||||
using netDxf.Entities;
|
using ACadSharp.Entities;
|
||||||
|
using ACadSharp.IO;
|
||||||
|
using CSMath;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@@ -11,12 +13,13 @@ namespace EtchBendLines
|
|||||||
{
|
{
|
||||||
public BendLineExtractor(string dxfFile)
|
public BendLineExtractor(string dxfFile)
|
||||||
{
|
{
|
||||||
DxfDocument = DxfDocument.Load(dxfFile);
|
using var reader = new DxfReader(dxfFile);
|
||||||
|
Document = reader.Read();
|
||||||
}
|
}
|
||||||
|
|
||||||
public BendLineExtractor(DxfDocument dxfDocument)
|
public BendLineExtractor(CadDocument document)
|
||||||
{
|
{
|
||||||
DxfDocument = dxfDocument;
|
Document = document;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -37,7 +40,7 @@ namespace EtchBendLines
|
|||||||
RegexOptions.Compiled | RegexOptions.IgnoreCase
|
RegexOptions.Compiled | RegexOptions.IgnoreCase
|
||||||
);
|
);
|
||||||
|
|
||||||
public DxfDocument DxfDocument { get; private set; }
|
public CadDocument Document { get; private set; }
|
||||||
|
|
||||||
public List<Bend> GetBendLines()
|
public List<Bend> GetBendLines()
|
||||||
{
|
{
|
||||||
@@ -47,7 +50,7 @@ namespace EtchBendLines
|
|||||||
if (ReplaceSharpRadius)
|
if (ReplaceSharpRadius)
|
||||||
FixSharpBends();
|
FixSharpBends();
|
||||||
|
|
||||||
foreach (var line in DxfDocument.Lines)
|
foreach (var line in Document.Entities.OfType<Line>())
|
||||||
{
|
{
|
||||||
if (!IsBendLine(line))
|
if (!IsBendLine(line))
|
||||||
continue;
|
continue;
|
||||||
@@ -68,7 +71,7 @@ namespace EtchBendLines
|
|||||||
|
|
||||||
private bool IsBendLine(Line line)
|
private bool IsBendLine(Line line)
|
||||||
{
|
{
|
||||||
if (line.Linetype.Name != "CENTERX2")
|
if (line.LineType.Name != "CENTERX2")
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
switch (line.Layer.Name.ToUpperInvariant())
|
switch (line.Layer.Name.ToUpperInvariant())
|
||||||
@@ -84,7 +87,7 @@ namespace EtchBendLines
|
|||||||
|
|
||||||
private List<MText> GetBendNotes()
|
private List<MText> GetBendNotes()
|
||||||
{
|
{
|
||||||
return DxfDocument.MTexts
|
return Document.Entities.OfType<MText>()
|
||||||
.Where(t => GetBendDirection(t) != BendDirection.Unknown)
|
.Where(t => GetBendDirection(t) != BendDirection.Unknown)
|
||||||
.ToList();
|
.ToList();
|
||||||
}
|
}
|
||||||
@@ -159,7 +162,7 @@ namespace EtchBendLines
|
|||||||
for (int i = bendNotesList.Count - 1; i >= 0; i--)
|
for (int i = bendNotesList.Count - 1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
var note = bendNotesList[i];
|
var note = bendNotesList[i];
|
||||||
var notePos = note.Position.ToVector2();
|
var notePos = note.InsertPoint.ToXY();
|
||||||
var perpendicularPoint = bendLine.PointPerpendicularTo(notePos);
|
var perpendicularPoint = bendLine.PointPerpendicularTo(notePos);
|
||||||
var dist = notePos.DistanceTo(perpendicularPoint);
|
var dist = notePos.DistanceTo(perpendicularPoint);
|
||||||
var maxAcceptableDist = note.Height * 2.0;
|
var maxAcceptableDist = note.Height * 2.0;
|
||||||
@@ -172,14 +175,14 @@ namespace EtchBendLines
|
|||||||
return null;
|
return null;
|
||||||
|
|
||||||
var closestNote = bendNotesList.First();
|
var closestNote = bendNotesList.First();
|
||||||
var p1 = closestNote.Position.ToVector2();
|
var p1 = closestNote.InsertPoint.ToXY();
|
||||||
var p2 = bendLine.ClosestPointOnLineTo(p1);
|
var p2 = bendLine.ClosestPointOnLineTo(p1);
|
||||||
var dist2 = p1.DistanceTo(p2);
|
var dist2 = p1.DistanceTo(p2);
|
||||||
|
|
||||||
for (int i = 1; i < bendNotesList.Count; i++)
|
for (int i = 1; i < bendNotesList.Count; i++)
|
||||||
{
|
{
|
||||||
var note = bendNotesList[i];
|
var note = bendNotesList[i];
|
||||||
var p3 = note.Position.ToVector2();
|
var p3 = note.InsertPoint.ToXY();
|
||||||
var p4 = bendLine.ClosestPointOnLineTo(p3);
|
var p4 = bendLine.ClosestPointOnLineTo(p3);
|
||||||
var dist = p3.DistanceTo(p4);
|
var dist = p3.DistanceTo(p4);
|
||||||
|
|
||||||
|
|||||||
@@ -11,11 +11,8 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="ACadSharp" Version="3.1.32" />
|
||||||
<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.0" />
|
<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<ProjectReference Include="..\netDxf\netDxf\netDxf.csproj" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
using netDxf;
|
using ACadSharp;
|
||||||
using netDxf.Entities;
|
using ACadSharp.Entities;
|
||||||
using netDxf.Tables;
|
using ACadSharp.Tables;
|
||||||
|
using ACadSharp.IO;
|
||||||
|
using CSMath;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
@@ -12,12 +14,12 @@ namespace EtchBendLines
|
|||||||
{
|
{
|
||||||
public readonly Layer BendLayer = new Layer("BEND")
|
public readonly Layer BendLayer = new Layer("BEND")
|
||||||
{
|
{
|
||||||
Color = AciColor.Yellow
|
Color = Color.Yellow
|
||||||
};
|
};
|
||||||
|
|
||||||
static readonly Layer EtchLayer = new Layer("ETCH")
|
static readonly Layer EtchLayer = new Layer("ETCH")
|
||||||
{
|
{
|
||||||
Color = AciColor.Green,
|
Color = Color.Green,
|
||||||
};
|
};
|
||||||
|
|
||||||
private const double DefaultEtchLength = 1.0;
|
private const double DefaultEtchLength = 1.0;
|
||||||
@@ -27,33 +29,34 @@ namespace EtchBendLines
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public double MaxBendRadius { get; set; } = 4.0;
|
public double MaxBendRadius { get; set; } = 4.0;
|
||||||
|
|
||||||
private DxfDocument LoadDocument(string path)
|
private CadDocument LoadDocument(string path)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return DxfDocument.Load(path)
|
using var reader = new DxfReader(path);
|
||||||
|
return reader.Read()
|
||||||
?? throw new InvalidOperationException("DXF load returned null");
|
?? throw new InvalidOperationException("DXF load returned null");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex) when (ex is not InvalidOperationException)
|
||||||
{
|
{
|
||||||
throw new ApplicationException($"Failed to load DXF '{path}'", ex);
|
throw new ApplicationException($"Failed to load DXF '{path}'", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Bend> ExtractBends(DxfDocument doc)
|
private List<Bend> ExtractBends(CadDocument doc)
|
||||||
{
|
{
|
||||||
var extractor = new BendLineExtractor(doc);
|
var extractor = new BendLineExtractor(doc);
|
||||||
return extractor.GetBendLines();
|
return extractor.GetBendLines();
|
||||||
}
|
}
|
||||||
|
|
||||||
private HashSet<string> BuildExistingKeySet(DxfDocument doc)
|
private HashSet<string> BuildExistingKeySet(CadDocument doc)
|
||||||
=> new HashSet<string>(
|
=> new HashSet<string>(
|
||||||
doc.Lines
|
doc.Entities.OfType<Line>()
|
||||||
.Where(l => IsEtchLayer(l.Layer))
|
.Where(l => IsEtchLayer(l.Layer))
|
||||||
.Select(l => KeyFor(l.StartPoint, l.EndPoint))
|
.Select(l => KeyFor(l.StartPoint, l.EndPoint))
|
||||||
);
|
);
|
||||||
|
|
||||||
private void InsertEtchLines(DxfDocument doc, IEnumerable<Bend> bends, HashSet<string> existingKeys, double etchLength)
|
private void InsertEtchLines(CadDocument doc, IEnumerable<Bend> bends, HashSet<string> existingKeys, double etchLength)
|
||||||
{
|
{
|
||||||
foreach (var bend in bends)
|
foreach (var bend in bends)
|
||||||
{
|
{
|
||||||
@@ -63,28 +66,31 @@ namespace EtchBendLines
|
|||||||
if (existingKeys.Contains(key))
|
if (existingKeys.Contains(key))
|
||||||
{
|
{
|
||||||
// ensure correct layer
|
// ensure correct layer
|
||||||
var existing = doc.Lines.First(l => KeyFor(l) == key);
|
var existing = doc.Entities.OfType<Line>().First(l => KeyFor(l) == key);
|
||||||
existing.Layer = EtchLayer;
|
existing.Layer = EtchLayer;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
etch.Layer = EtchLayer;
|
etch.Layer = EtchLayer;
|
||||||
doc.AddEntity(etch);
|
doc.Entities.Add(etch);
|
||||||
existingKeys.Add(key);
|
existingKeys.Add(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SaveDocument(DxfDocument doc, string path)
|
private void SaveDocument(CadDocument doc, string path)
|
||||||
{
|
{
|
||||||
doc.Save(path);
|
using (var writer = new DxfWriter(path, doc, false))
|
||||||
|
{
|
||||||
|
writer.Write();
|
||||||
|
}
|
||||||
Console.WriteLine($"→ Saved with etch lines: {path}");
|
Console.WriteLine($"→ Saved with etch lines: {path}");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string KeyFor(Line l) => KeyFor(l.StartPoint, l.EndPoint);
|
private static string KeyFor(Line l) => KeyFor(l.StartPoint, l.EndPoint);
|
||||||
|
|
||||||
private static string KeyFor(Vector3 a, Vector3 b) => $"{a.X:F3},{a.Y:F3}|{b.X:F3},{b.Y:F3}";
|
private static string KeyFor(XYZ a, XYZ b) => $"{a.X:F3},{a.Y:F3}|{b.X:F3},{b.Y:F3}";
|
||||||
|
|
||||||
public void AddEtchLines(string filePath, double etchLength = DefaultEtchLength)
|
public void AddEtchLines(string filePath, double etchLength = DefaultEtchLength)
|
||||||
{
|
{
|
||||||
@@ -129,8 +135,8 @@ namespace EtchBendLines
|
|||||||
{
|
{
|
||||||
var lines = new List<Line>();
|
var lines = new List<Line>();
|
||||||
|
|
||||||
var startPoint = new Vector2(bendLine.StartPoint.X, bendLine.StartPoint.Y);
|
var startPoint = new XY(bendLine.StartPoint.X, bendLine.StartPoint.Y);
|
||||||
var endPoint = new Vector2(bendLine.EndPoint.X, bendLine.EndPoint.Y);
|
var endPoint = new XY(bendLine.EndPoint.X, bendLine.EndPoint.Y);
|
||||||
var bendLength = startPoint.DistanceTo(endPoint);
|
var bendLength = startPoint.DistanceTo(endPoint);
|
||||||
|
|
||||||
if (bendLength < (etchLength * 3.0))
|
if (bendLength < (etchLength * 3.0))
|
||||||
@@ -151,26 +157,26 @@ namespace EtchBendLines
|
|||||||
var topY1 = Math.Max(startPoint.Y, endPoint.Y);
|
var topY1 = Math.Max(startPoint.Y, endPoint.Y);
|
||||||
var topY2 = topY1 - etchLength;
|
var topY2 = topY1 - etchLength;
|
||||||
|
|
||||||
var p1 = new Vector2(x, bottomY1);
|
var p1 = new XYZ(x, bottomY1, 0);
|
||||||
var p2 = new Vector2(x, bottomY2);
|
var p2 = new XYZ(x, bottomY2, 0);
|
||||||
var p3 = new Vector2(x, topY1);
|
var p3 = new XYZ(x, topY1, 0);
|
||||||
var p4 = new Vector2(x, topY2);
|
var p4 = new XYZ(x, topY2, 0);
|
||||||
|
|
||||||
lines.Add(new Line(p1, p2));
|
lines.Add(new Line(p1, p2));
|
||||||
lines.Add(new Line(p3, p4));
|
lines.Add(new Line(p3, p4));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var start = bendLine.StartPoint.ToVector2();
|
var start = bendLine.StartPoint.ToXY();
|
||||||
var end = bendLine.EndPoint.ToVector2();
|
var end = bendLine.EndPoint.ToXY();
|
||||||
|
|
||||||
var dx = Math.Cos(angle) * etchLength;
|
var dx = Math.Cos(angle) * etchLength;
|
||||||
var dy = Math.Sin(angle) * etchLength;
|
var dy = Math.Sin(angle) * etchLength;
|
||||||
|
|
||||||
var p1 = new Vector2(start.X, start.Y);
|
var p1 = new XYZ(start.X, start.Y, 0);
|
||||||
var p2 = new Vector2(start.X + dx, start.Y + dy);
|
var p2 = new XYZ(start.X + dx, start.Y + dy, 0);
|
||||||
var p3 = new Vector2(end.X, end.Y);
|
var p3 = new XYZ(end.X, end.Y, 0);
|
||||||
var p4 = new Vector2(end.X - dx, end.Y - dy);
|
var p4 = new XYZ(end.X - dx, end.Y - dy, 0);
|
||||||
|
|
||||||
lines.Add(new Line(p1, p2));
|
lines.Add(new Line(p1, p2));
|
||||||
lines.Add(new Line(p3, p4));
|
lines.Add(new Line(p3, p4));
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
using netDxf;
|
using ACadSharp.Entities;
|
||||||
using netDxf.Entities;
|
using CSMath;
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace EtchBendLines
|
namespace EtchBendLines
|
||||||
@@ -8,12 +8,12 @@ namespace EtchBendLines
|
|||||||
{
|
{
|
||||||
const double TwoPI = Math.PI * 2.0;
|
const double TwoPI = Math.PI * 2.0;
|
||||||
|
|
||||||
public static Vector2 ToVector2(this Vector3 pt)
|
public static XY ToXY(this XYZ pt)
|
||||||
{
|
{
|
||||||
return new Vector2(pt.X, pt.Y);
|
return new XY(pt.X, pt.Y);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool IsEqualTo(this Vector3 pt, Vector3 pt1)
|
public static bool IsEqualTo(this XYZ pt, XYZ pt1)
|
||||||
{
|
{
|
||||||
return pt.X.IsEqualTo(pt1.X) && pt.Y.IsEqualTo(pt1.Y) && pt.Z.IsEqualTo(pt1.Z);
|
return pt.X.IsEqualTo(pt1.X) && pt.Y.IsEqualTo(pt1.Y) && pt.Z.IsEqualTo(pt1.Z);
|
||||||
}
|
}
|
||||||
@@ -50,10 +50,10 @@ namespace EtchBendLines
|
|||||||
return Math.Round(p1.Y - slope * p1.X, 4);
|
return Math.Round(p1.Y - slope * p1.X, 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Vector2 PointPerpendicularTo(this Line line, Vector2 pt)
|
public static XY PointPerpendicularTo(this Line line, XY pt)
|
||||||
{
|
{
|
||||||
var startPoint = line.StartPoint.ToVector2();
|
var startPoint = line.StartPoint.ToXY();
|
||||||
var endPoint = line.EndPoint.ToVector2();
|
var endPoint = line.EndPoint.ToXY();
|
||||||
|
|
||||||
var d1 = pt - startPoint;
|
var d1 = pt - startPoint;
|
||||||
var d2 = endPoint - startPoint;
|
var d2 = endPoint - startPoint;
|
||||||
@@ -61,20 +61,20 @@ namespace EtchBendLines
|
|||||||
var lengthSquared = d2.X * d2.X + d2.Y * d2.Y;
|
var lengthSquared = d2.X * d2.X + d2.Y * d2.Y;
|
||||||
var param = dotProduct / lengthSquared;
|
var param = dotProduct / lengthSquared;
|
||||||
|
|
||||||
return new Vector2(
|
return new XY(
|
||||||
startPoint.X + param * d2.X,
|
startPoint.X + param * d2.X,
|
||||||
startPoint.Y + param * d2.Y);
|
startPoint.Y + param * d2.Y);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Vector2 MidPoint(this Line line)
|
public static XY MidPoint(this Line line)
|
||||||
{
|
{
|
||||||
var x = (line.StartPoint.X + line.EndPoint.X) * 0.5;
|
var x = (line.StartPoint.X + line.EndPoint.X) * 0.5;
|
||||||
var y = (line.StartPoint.Y + line.EndPoint.Y) * 0.5;
|
var y = (line.StartPoint.Y + line.EndPoint.Y) * 0.5;
|
||||||
|
|
||||||
return new Vector2(x, y);
|
return new XY(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static double DistanceTo(this Vector2 startPoint, Vector2 endPoint)
|
public static double DistanceTo(this XY startPoint, XY endPoint)
|
||||||
{
|
{
|
||||||
var x = endPoint.X - startPoint.X;
|
var x = endPoint.X - startPoint.X;
|
||||||
var y = endPoint.Y - startPoint.Y;
|
var y = endPoint.Y - startPoint.Y;
|
||||||
@@ -82,7 +82,7 @@ namespace EtchBendLines
|
|||||||
return Math.Sqrt(x * x + y * y);
|
return Math.Sqrt(x * x + y * y);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static double AngleTo(this Vector2 startPoint, Vector2 endPoint)
|
public static double AngleTo(this XY startPoint, XY endPoint)
|
||||||
{
|
{
|
||||||
var x = endPoint.X - startPoint.X;
|
var x = endPoint.X - startPoint.X;
|
||||||
var y = endPoint.Y - startPoint.Y;
|
var y = endPoint.Y - startPoint.Y;
|
||||||
@@ -133,10 +133,10 @@ namespace EtchBendLines
|
|||||||
return Math.Abs(b - a) <= tolerance;
|
return Math.Abs(b - a) <= tolerance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Vector2 ClosestPointOnLineTo(this Line line, Vector2 pt)
|
public static XY ClosestPointOnLineTo(this Line line, XY pt)
|
||||||
{
|
{
|
||||||
var startPoint = line.StartPoint.ToVector2();
|
var startPoint = line.StartPoint.ToXY();
|
||||||
var endPoint = line.EndPoint.ToVector2();
|
var endPoint = line.EndPoint.ToXY();
|
||||||
|
|
||||||
var diff1 = pt - startPoint;
|
var diff1 = pt - startPoint;
|
||||||
var diff2 = endPoint - startPoint;
|
var diff2 = endPoint - startPoint;
|
||||||
@@ -150,7 +150,7 @@ namespace EtchBendLines
|
|||||||
return endPoint;
|
return endPoint;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return new Vector2(
|
return new XY(
|
||||||
startPoint.X + param * diff2.X,
|
startPoint.X + param * diff2.X,
|
||||||
startPoint.Y + param * diff2.Y);
|
startPoint.Y + param * diff2.Y);
|
||||||
}
|
}
|
||||||
|
|||||||
1
netDxf
1
netDxf
Submodule netDxf deleted from 67789bfe70
Reference in New Issue
Block a user