diff --git a/OpenNest.Core/CutOff.cs b/OpenNest.Core/CutOff.cs index dc41e922..d3dbc09e 100644 --- a/OpenNest.Core/CutOff.cs +++ b/OpenNest.Core/CutOff.cs @@ -149,6 +149,28 @@ namespace OpenNest return result; } + /// + /// The cut strokes of a cut-off , offset by : + /// one line per cutting move, from wherever the previous motion ended. Walks the motions rather + /// than pairing rapid and cut codes, because a head-locked far-edge segment carries lock + /// comments and a rapid return between its cuts. + /// + public static IEnumerable CutStrokes(Program program, Vector offset = default) + { + if (program == null) + yield break; + Vector? position = null; + foreach (var code in program.Codes) + { + if (code is not Motion motion) + continue; + var end = motion.EndPoint + offset; + if (motion is LinearMove && position is { } start) + yield return new Line(start, end); + position = end; + } + } + /// /// Whether the far edge, along this axis on , is the high /// coordinate: the sheet runs from the origin toward positive coordinates on that axis. diff --git a/OpenNest.WinForms.Tests/CutOffWindowSelectionTests.cs b/OpenNest.WinForms.Tests/CutOffWindowSelectionTests.cs index 7f6a7d89..75ec078d 100644 --- a/OpenNest.WinForms.Tests/CutOffWindowSelectionTests.cs +++ b/OpenNest.WinForms.Tests/CutOffWindowSelectionTests.cs @@ -61,6 +61,45 @@ public class CutOffWindowSelectionTests Assert.Empty(view.Selection.GetCutOffsFromWindow(full, SelectionType.Contains)); }); + // A head-locked far-edge segment carries lock comments (and, cut from the far edge, a rapid + // return) between its cuts, so its codes do not pair up as rapid/cut at even positions. + [Theory] + [InlineData(false)] + [InlineData(true)] + public void HeadLockedCutOffIsSelectableByClickAndWindow(bool farEdgeFirst) => RunSta(() => + { + var square = new CNC.Program(); + square.MoveTo(8, 8); + square.LineTo(12, 8); + square.LineTo(12, 12); + square.LineTo(8, 12); + square.LineTo(8, 8); + var plate = new Plate(20, 20); + plate.Parts.Add(new Part(new Drawing("square", square))); + var cutoff = new CutOff(new Vector(10, 0), CutOffAxis.Vertical) + { + HeadLockOvercut = 0.25, + FarEdgeFirst = farEdgeFirst, + }; + plate.CutOffs.Add(cutoff); + plate.RegenerateCutOffs(new CutOffSettings()); + Assert.Contains(cutoff.Drawing.Program.Codes, HeadHeightLock.IsLock); + + using var view = new PlateView { Plate = plate }; + Assert.Same(cutoff, view.GetCutOffAtPoint(new Vector(10, 3), 0.1)); + Assert.Same(cutoff, view.GetCutOffAtPoint(new Vector(10, 17), 0.1)); + Assert.Same(cutoff, view.GetCutOffAtPoint(new Vector(10, 20.2), 0.1)); + Assert.Null(view.GetCutOffAtPoint(new Vector(10, 10), 0.1)); + + var farSegment = GraphRect(view, new Vector(9, 16), new Vector(11, 18)); + var full = GraphRect(view, new Vector(9, -1), new Vector(11, 21)); + var beforeOvercut = GraphRect(view, new Vector(9, -1), new Vector(11, 20)); + Assert.Contains(cutoff, view.Selection.GetCutOffsFromWindow(farSegment, SelectionType.Intersect)); + Assert.Contains(cutoff, view.Selection.GetCutOffsFromWindow(full, SelectionType.Contains)); + // The overcut past the sheet is part of the line, so a window short of it does not contain it. + Assert.Empty(view.Selection.GetCutOffsFromWindow(beforeOvercut, SelectionType.Contains)); + }); + [Fact] public void ActionSelectsPartsAndCutOffsTogetherAndDeleteRemovesBoth() => RunSta(() => { diff --git a/OpenNest/Actions/ActionCutOff.cs b/OpenNest/Actions/ActionCutOff.cs index 073168b9..95c6e459 100644 --- a/OpenNest/Actions/ActionCutOff.cs +++ b/OpenNest/Actions/ActionCutOff.cs @@ -151,17 +151,11 @@ namespace OpenNest.Actions DashStyle = DashStyle.Dash, }; - for (var i = 0; i < program.Codes.Count - 1; i += 2) + foreach (var stroke in CutOff.CutStrokes(program)) { - if ( - program.Codes[i] is RapidMove rapid - && program.Codes[i + 1] is LinearMove linear - ) - { - var pt1 = plateView.PointWorldToGraph(rapid.EndPoint); - var pt2 = plateView.PointWorldToGraph(linear.EndPoint); - e.Graphics.DrawLine(pen, pt1, pt2); - } + var pt1 = plateView.PointWorldToGraph(stroke.StartPoint); + var pt2 = plateView.PointWorldToGraph(stroke.EndPoint); + e.Graphics.DrawLine(pen, pt1, pt2); } DrawRotateTag(e.Graphics); diff --git a/OpenNest/Controls/CutOffHandler.cs b/OpenNest/Controls/CutOffHandler.cs index b962b3c8..a49c098a 100644 --- a/OpenNest/Controls/CutOffHandler.cs +++ b/OpenNest/Controls/CutOffHandler.cs @@ -108,21 +108,10 @@ namespace OpenNest.Controls foreach (var cutoff in view.Plate.CutOffs) { - var program = cutoff.Drawing?.Program; - if (program == null) - continue; - - for (var i = 0; i < program.Codes.Count - 1; i += 2) + foreach (var line in CutOff.CutStrokes(cutoff.Drawing?.Program)) { - if ( - program.Codes[i] is RapidMove rapid - && program.Codes[i + 1] is LinearMove linear - ) - { - var line = new Line(rapid.EndPoint, linear.EndPoint); - if (line.ClosestPointTo(point).DistanceTo(point) <= tolerance) - return cutoff; - } + if (line.ClosestPointTo(point).DistanceTo(point) <= tolerance) + return cutoff; } } diff --git a/OpenNest/Controls/PlateRenderer.cs b/OpenNest/Controls/PlateRenderer.cs index 9d6fe119..674abd1b 100644 --- a/OpenNest/Controls/PlateRenderer.cs +++ b/OpenNest/Controls/PlateRenderer.cs @@ -420,20 +420,11 @@ namespace OpenNest.Controls } } - // Walk the motions rather than pairing rapid/cut codes: a head-locked far-edge segment - // carries lock comments between its cuts, and its overcut past the sheet must dash too. + // The head-locked overcut past the sheet dashes too; selection hit-tests the same strokes. private void DrawCutOffSegments(Graphics g, Program program, Vector offset, Pen pen) { - Vector? position = null; - foreach (var code in program.Codes) - { - if (code is not Motion motion) - continue; - var end = motion.EndPoint + offset; - if (motion is LinearMove && position is { } start) - DrawLine(g, start, end, pen); - position = end; - } + foreach (var stroke in CutOff.CutStrokes(program, offset)) + DrawLine(g, stroke.StartPoint, stroke.EndPoint, pen); } /// diff --git a/OpenNest/Controls/SelectionManager.cs b/OpenNest/Controls/SelectionManager.cs index 7445fd6f..e78e439a 100644 --- a/OpenNest/Controls/SelectionManager.cs +++ b/OpenNest/Controls/SelectionManager.cs @@ -345,23 +345,15 @@ namespace OpenNest.Controls var result = new List(); foreach (var cutOff in view.Plate.CutOffs) { - var program = cutOff.Drawing?.Program; - if (program == null) - continue; - var hasSegment = false; var matches = selectionType == SelectionType.Contains; // Match the actual cut strokes drawn by PlateRenderer, not the nominal // full-length line: a gap around a part is not selectable material. - for (var i = 0; i < program.Codes.Count - 1; i += 2) + foreach (var stroke in CutOff.CutStrokes(cutOff.Drawing?.Program)) { - if (program.Codes[i] is not RapidMove rapid - || program.Codes[i + 1] is not LinearMove linear) - continue; - hasSegment = true; - var start = view.PointWorldToGraph(rapid.EndPoint); - var end = view.PointWorldToGraph(linear.EndPoint); + var start = view.PointWorldToGraph(stroke.StartPoint); + var end = view.PointWorldToGraph(stroke.EndPoint); var hit = selectionType == SelectionType.Contains ? ContainsInclusive(rect, start) && ContainsInclusive(rect, end) : SegmentIntersects(rect, start, end);