From 59f5a327547f6927145b57be66514e3cd44c093f Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Mon, 30 Nov 2026 20:13:25 -0500 Subject: [PATCH] fix(core): read an absolute program's end point from its last code Program.EndPoint started its backward search at Codes.Count, one past the last code, so every absolute-mode program threw an ArgumentOutOfRangeException, including through Timing.GetTimingInfo for cut-time estimates. Start at Count - 1; an empty program still returns the origin. Co-Authored-By: Claude Opus 5.5 --- OpenNest.Core/CNC/Program.cs | 2 +- OpenNest.Tests/CNC/ProgramEndPointTests.cs | 36 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 OpenNest.Tests/CNC/ProgramEndPointTests.cs diff --git a/OpenNest.Core/CNC/Program.cs b/OpenNest.Core/CNC/Program.cs index bf873d1c..31c45492 100644 --- a/OpenNest.Core/CNC/Program.cs +++ b/OpenNest.Core/CNC/Program.cs @@ -255,7 +255,7 @@ namespace OpenNest.CNC { case Mode.Absolute: { - for (int i = Codes.Count; i >= 0; --i) + for (int i = Codes.Count - 1; i >= 0; --i) { var code = Codes[i]; var motion = code as Motion; diff --git a/OpenNest.Tests/CNC/ProgramEndPointTests.cs b/OpenNest.Tests/CNC/ProgramEndPointTests.cs new file mode 100644 index 00000000..bfc34444 --- /dev/null +++ b/OpenNest.Tests/CNC/ProgramEndPointTests.cs @@ -0,0 +1,36 @@ +using OpenNest.CNC; +using OpenNest.Geometry; + +namespace OpenNest.Tests.CNC; + +public class ProgramEndPointTests +{ + [Fact] + public void Absolute_ReturnsTheLastMotionsEndPoint() + { + var program = new Program(Mode.Absolute); + program.MoveTo(1, 1); + program.LineTo(4, 5); + program.LineTo(7, 2); + + Assert.Equal(new Vector(7, 2), program.EndPoint()); + } + + [Fact] + public void Absolute_Empty_ReturnsTheOrigin() + { + var program = new Program(Mode.Absolute); + + Assert.Equal(new Vector(0, 0), program.EndPoint()); + } + + [Fact] + public void Incremental_SumsTheMotions() + { + var program = new Program(Mode.Incremental); + program.MoveTo(1, 1); + program.LineTo(3, 4); + + Assert.Equal(new Vector(4, 5), program.EndPoint()); + } +}