fix(cut-off): select head-locked cut-offs by click and window

Clicking and window selection paired the cut-off program's codes as
rapid/cut at even positions. A head-locked far-edge segment carries lock
comments (and, cut from the far edge, a rapid return) between its cuts,
so no pair matched and the line could not be selected, though it drew.

CutOff.CutStrokes now walks the motions once for the renderer, click
hit test, window selection and the Sheet Cut-Off preview, so what is
selectable matches what is drawn.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-11-05 20:36:58 -05:00
1 parent 2b23150801
commit f88b805e68
6 files changed
+74 -47

No files matched your search

+22
View File
@@ -149,6 +149,28 @@ namespace OpenNest
return result;
}
/// <summary>
/// The cut strokes of a cut-off <paramref name="program"/>, offset by <paramref name="offset"/>:
/// 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.
/// </summary>
public static IEnumerable<Line> 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;
}
}
/// <summary>
/// Whether the far edge, along this axis on <paramref name="plate"/>, is the high
/// coordinate: the sheet runs from the origin toward positive coordinates on that axis.
@@ -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(() =>
{
+4 -10
View File
@@ -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);
+3 -14
View File
@@ -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;
}
}
+3 -12
View File
@@ -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);
}
/// <summary>
+3 -11
View File
@@ -345,23 +345,15 @@ namespace OpenNest.Controls
var result = new List<CutOff>();
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);