diff --git a/OpenNest.IO/Dxf.cs b/OpenNest.IO/Dxf.cs
index b454956f..68b2c49f 100644
--- a/OpenNest.IO/Dxf.cs
+++ b/OpenNest.IO/Dxf.cs
@@ -98,9 +98,7 @@ namespace OpenNest.IO
foreach (var part in plate.Parts)
{
- var endpt = part.Location.ToAcadXYZ();
- ctx.AddLine(ctx.CurPos, endpt, ctx.RapidLayer);
- ctx.CurPos = part.Location.ToAcadXYZ();
+ ctx.MoveFrame(part.Location.ToAcadXYZ());
ctx.AddProgram(part.Program);
}
@@ -248,6 +246,10 @@ namespace OpenNest.IO
private Mode mode;
+ // Where the tool really is while CurPos has jumped to a part or sub-program origin
+ // that the tool never visits. The next move is drawn as a rapid from here.
+ private XYZ? travelFrom;
+
public ExportContext()
{
Document = new CadDocument();
@@ -273,6 +275,27 @@ namespace OpenNest.IO
Document.Entities.Add(ln);
}
+ ///
+ /// Makes the reference for the next moves without drawing
+ /// a rapid to it: the travel is drawn to where the next move actually goes.
+ ///
+ public void MoveFrame(XYZ origin)
+ {
+ travelFrom ??= CurPos;
+ CurPos = origin;
+ }
+
+ // Draws the pending travel as a rapid to the start of a cut.
+ private void FlushTravel()
+ {
+ if (travelFrom is not XYZ from)
+ return;
+
+ travelFrom = null;
+ if (from.X != CurPos.X || from.Y != CurPos.Y)
+ AddLine(from, CurPos, RapidLayer);
+ }
+
public void AddPlateOutline(Plate plate)
{
XYZ pt1,
@@ -336,6 +359,9 @@ namespace OpenNest.IO
public void AddProgram(Program program)
{
+ // A sub-program's Offset is measured from where this program started, not from
+ // the tool position at the call (the same frame ConvertProgram.ToGeometry uses).
+ var frameOrigin = CurPos;
mode = program.Mode;
for (var i = 0; i < program.Length; ++i)
@@ -358,7 +384,15 @@ namespace OpenNest.IO
case CodeType.SubProgramCall:
var tmpmode = mode;
- AddProgram(((SubProgramCall)code).Program);
+ var call = (SubProgramCall)code;
+ MoveFrame(
+ new XYZ(
+ frameOrigin.X + call.Offset.X,
+ frameOrigin.Y + call.Offset.Y,
+ 0
+ )
+ );
+ AddProgram(call.Program);
mode = tmpmode;
break;
}
@@ -372,6 +406,7 @@ namespace OpenNest.IO
if (mode == Mode.Incremental)
pt = new XYZ(pt.X + CurPos.X, pt.Y + CurPos.Y, 0);
+ FlushTravel();
AddLine(CurPos, pt, CutLayer);
CurPos = pt;
}
@@ -383,7 +418,8 @@ namespace OpenNest.IO
if (mode == Mode.Incremental)
pt = new XYZ(pt.X + CurPos.X, pt.Y + CurPos.Y, 0);
- AddLine(CurPos, pt, RapidLayer);
+ AddLine(travelFrom ?? CurPos, pt, RapidLayer);
+ travelFrom = null;
CurPos = pt;
}
@@ -398,6 +434,8 @@ namespace OpenNest.IO
center = new XYZ(center.X + CurPos.X, center.Y + CurPos.Y, 0);
}
+ FlushTravel();
+
var startAngle = PortableMath.Atan2(CurPos.Y - center.Y, CurPos.X - center.X);
var endAngle = PortableMath.Atan2(endpt.Y - center.Y, endpt.X - center.X);
diff --git a/OpenNest.Tests/IO/DxfRoundtripTests.cs b/OpenNest.Tests/IO/DxfRoundtripTests.cs
index 68e53fc6..de8399d9 100644
--- a/OpenNest.Tests/IO/DxfRoundtripTests.cs
+++ b/OpenNest.Tests/IO/DxfRoundtripTests.cs
@@ -1,3 +1,4 @@
+using OpenNest.CNC;
using OpenNest.Converters;
using OpenNest.Geometry;
using OpenNest.IO;
@@ -148,4 +149,71 @@ public class DxfRoundtripTests
Assert.Equal(origMinY, rtMinY, Tolerance);
Assert.Equal(origMaxY, rtMaxY, Tolerance);
}
+
+ [Fact]
+ public void PlateExport_PlacesSubProgramHolesAtTheirOffsets()
+ {
+ // A planned part: each hole is a sub-program called at an offset from the part origin.
+ var hole = new Program(Mode.Incremental);
+ hole.Codes.Add(new RapidMove(0.5, 0));
+ hole.Codes.Add(new ArcMove(new Vector(0, 0), new Vector(-0.5, 0), RotationType.CCW));
+
+ var pgm = new Program(Mode.Incremental);
+ pgm.SubPrograms[1] = hole;
+ pgm.Codes.Add(new SubProgramCall { Id = 1, Program = hole, Offset = new Vector(2, 2) });
+ pgm.Codes.Add(new SubProgramCall { Id = 1, Program = hole, Offset = new Vector(8, 3) });
+ pgm.Codes.Add(new RapidMove(-8.5, -3));
+ pgm.Codes.Add(new LinearMove(10, 0));
+ pgm.Codes.Add(new LinearMove(0, 5));
+ pgm.Codes.Add(new LinearMove(-10, 0));
+ pgm.Codes.Add(new LinearMove(0, -5));
+
+ var plate = new Plate(100, 50) { Quadrant = 1 };
+ var part = new Part(new Drawing("Holes") { Program = pgm });
+ part.Location = new Vector(20, 30);
+ plate.Parts.Add(part);
+
+ using var stream = new MemoryStream();
+ Dxf.ExportPlate(plate, stream);
+ var exported = Dxf.GetGeometry(new MemoryStream(stream.ToArray()));
+
+ // The exporter must agree with the geometry the plate view draws.
+ var expected = ConvertProgram.ToGeometry(part.Program).OfType().ToList();
+ foreach (var circle in expected)
+ circle.Offset(part.Location);
+ var actual = FilterByLayer(exported, "Cut");
+
+ Assert.Equal(2, expected.Count);
+ Assert.Equal(expected.Count, actual.Count);
+ foreach (var circle in expected)
+ Assert.Contains(
+ actual,
+ c =>
+ c.Center.DistanceTo(circle.Center) < Tolerance
+ && System.Math.Abs(c.Radius - circle.Radius) < Tolerance
+ );
+
+ var cutLines = FilterByLayer(exported, "Cut");
+ Assert.Equal(part.BoundingBox.Left, cutLines.Min(l => l.BoundingBox.Left), Tolerance);
+ Assert.Equal(part.BoundingBox.Bottom, cutLines.Min(l => l.BoundingBox.Bottom), Tolerance);
+
+ // Rapids follow the tool from pierce to pierce: none detours through the part origin
+ // or a hole center, which the tool never visits.
+ var rapids = FilterByLayer(exported, "Rapid");
+ var stops = new[]
+ {
+ new Vector(0, 0),
+ new Vector(22.5, 32),
+ new Vector(28.5, 33),
+ new Vector(20, 30),
+ };
+ Assert.Equal(stops.Length - 1, rapids.Count);
+ for (var i = 0; i < rapids.Count; i++)
+ Assert.Contains(
+ rapids,
+ r =>
+ r.StartPoint.DistanceTo(stops[i]) < Tolerance
+ && r.EndPoint.DistanceTo(stops[i + 1]) < Tolerance
+ );
+ }
}