fix(sequencing): cut parts no drawn line crosses last in Sequence by Path

Every part was claimed by its nearest line even when the line never reached
it, so a part beyond a line's start projected to distance 0 along it and was
cut near the front. Parts now belong only to lines crossing their bounding
box; uncrossed parts follow all the others, ordered by their nearest line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-10-21 13:50:11 -04:00
1 parent 35359906fe
commit d1c77b8ec7
3 files changed
+73 -10

No files matched your search

@@ -8,9 +8,10 @@ namespace OpenNest.Engine.Sequencing
{
/// <summary>
/// Sequences a plate along lines the user draws over it, in the order they were drawn.
/// Each part belongs to the line passing closest to its center, and is cut in the order
/// that nearest point falls along the line; where a line passes a part more than once,
/// the closest pass decides. The jump from one line to the next is a rapid and claims
/// Each part belongs to the crossing line passing closest to its center, and is cut in the
/// order that nearest point falls along the line; where a line passes a part more than once,
/// the closest pass decides. Parts no line crosses are cut after the rest, ordered the same
/// way by their nearest line. The jump from one line to the next is a rapid and claims
/// nothing. Cut-first parts stay ahead of the rest and the cutoff rules still apply.
/// </summary>
public static class DrawnPathSequencing
@@ -43,12 +44,22 @@ namespace OpenNest.Engine.Sequencing
return PlateSequencing.OrderCutOffsFirst(ordered, plate);
}
/// <summary>The parts in line order; ties keep their current order.</summary>
/// <summary>
/// The parts in line order, then the parts no line crosses, in the order they would
/// follow their nearest line; ties keep their current order.
/// </summary>
public static List<Part> Order(IEnumerable<Part> parts, IReadOnlyList<IReadOnlyList<Vector>> lines)
{
return parts
.Select(part => (Part: part, Station: Nearest(part.BoundingBox.Center, lines)))
.OrderBy(item => item.Station.Line)
.Select(part =>
{
var box = part.BoundingBox;
var crossing = Nearest(box.Center, lines, line => Crosses(line, box));
var missed = crossing.Line == int.MaxValue;
return (Part: part, Missed: missed, Station: missed ? Nearest(box.Center, lines, _ => true) : crossing);
})
.OrderBy(item => item.Missed)
.ThenBy(item => item.Station.Line)
.ThenBy(item => item.Station.Along)
.ThenBy(item => item.Station.Distance)
.Select(item => item.Part)
@@ -97,13 +108,51 @@ namespace OpenNest.Engine.Sequencing
return length > Tolerance.Epsilon;
}
// The earliest of equally near lines wins.
private static (int Line, double Along, double Distance) Nearest(Vector point, IReadOnlyList<IReadOnlyList<Vector>> lines)
/// <summary>Whether any segment of the path touches the box.</summary>
public static bool Crosses(IReadOnlyList<Vector> path, Box box)
{
for (var i = 1; i < path.Count; i++)
{
if (SegmentTouchesBox(path[i - 1], path[i], box))
return true;
}
return false;
}
// Liang-Barsky clip of the segment against the box, edges included.
private static bool SegmentTouchesBox(Vector start, Vector end, Box box)
{
var dx = end.X - start.X;
var dy = end.Y - start.Y;
var enter = 0.0;
var exit = 1.0;
return Clip(-dx, start.X - box.Left, ref enter, ref exit)
&& Clip(dx, box.Right - start.X, ref enter, ref exit)
&& Clip(-dy, start.Y - box.Bottom, ref enter, ref exit)
&& Clip(dy, box.Top - start.Y, ref enter, ref exit);
}
private static bool Clip(double p, double q, ref double enter, ref double exit)
{
if (System.Math.Abs(p) <= Tolerance.Epsilon)
return q >= -Tolerance.Epsilon;
var t = q / p;
if (p < 0)
enter = System.Math.Max(enter, t);
else
exit = System.Math.Min(exit, t);
return enter <= exit;
}
// The earliest of equally near lines wins; int.MaxValue when no line qualifies.
private static (int Line, double Along, double Distance) Nearest(
Vector point, IReadOnlyList<IReadOnlyList<Vector>> lines, Func<IReadOnlyList<Vector>, bool> qualifies)
{
var best = (Line: int.MaxValue, Along: 0.0, Distance: double.PositiveInfinity);
for (var i = 0; i < lines.Count; i++)
{
if (!HasLength(lines[i]))
if (!HasLength(lines[i]) || !qualifies(lines[i]))
continue;
var (along, distance) = Project(point, lines[i]);
if (distance < best.Distance - Tolerance.Epsilon)
@@ -89,6 +89,20 @@ public class DrawnPathSequencingTests
Assert.Equal(new[] { a, b, stray }, plate.Parts);
}
[Fact]
public void Apply_CutsPartsNoLineCrossesLast()
{
// Four stacked squares; the line starts in the second and runs up through the
// fourth. The bottom square is nearest the line's start but the line never reaches it.
var drawing = TestHelpers.MakeSquareDrawing();
var stack = Enumerable.Range(0, 4).Select(i => new Part(drawing, new Vector(0, i * 15))).ToArray();
var plate = TestHelpers.MakePlate(60, 120, stack[3], stack[1], stack[0], stack[2]);
Assert.True(DrawnPathSequencing.Apply(plate, Lines(new[] { new Vector(5, 20), new Vector(5, 52) })));
Assert.Equal(new[] { stack[1], stack[2], stack[3], stack[0] }, plate.Parts);
}
[Fact]
public void Apply_PartPassedTwiceFollowsTheNearerPass()
{
+1 -1
View File
@@ -45,7 +45,7 @@ Existing full-width vertical cut-offs inside the used span become fixed strip bo
For **Plate > Sequence Parts > Manual Sequence**, choose the one-based sequence number and hover a part on the sheet; only the part under the pointer is outlined. Click it to place it at that exact position, then the number advances up to the last part. Clicking empty sheet space changes nothing. Use **Done**, the window close button, right-click on the sheet, or Escape to leave the tool. Manual reordering changes neither geometry nor quantities.
For **Plate > Sequence Parts > Sequence by Path**, drag lines over the sheet with the left button in the order you want the parts cut. Lifting the button between lines is a rapid: the dashed jump to the next line's numbered start claims no parts. Each part belongs to the line passing closest to its bounding-box center, and lines are cut in drawing order, each in the order its nearest points fall along it; where a line passes a part more than once, the closest pass decides. The numbers update as you drag, and the plate is reordered when you release. **Undo Line**, Backspace or Ctrl+Z removes the last line; **Clear** or right-click removes them all and restores the order the tool started with. Cut-first parts stay ahead of the rest, and cutoffs follow the same dependency rules as Part Sequencing below. Escape mid-drag drops only that line; otherwise Escape, **Done** or closing the window leaves the tool.
For **Plate > Sequence Parts > Sequence by Path**, drag lines over the sheet with the left button in the order you want the parts cut. Lifting the button between lines is a rapid: the dashed jump to the next line's numbered start claims no parts. Each part belongs to the line crossing its bounding box that passes closest to its bounding-box center, and lines are cut in drawing order, each in the order its nearest points fall along it; where a line passes a part more than once, the closest pass decides. Parts no line crosses are cut after all the others, in the order they would follow their nearest line. The numbers update as you drag, and the plate is reordered when you release. **Undo Line**, Backspace or Ctrl+Z removes the last line; **Clear** or right-click removes them all and restores the order the tool started with. Cut-first parts stay ahead of the rest, and cutoffs follow the same dependency rules as Part Sequencing below. Escape mid-drag drops only that line; otherwise Escape, **Done** or closing the window leaves the tool.
After adding cutoffs, apply **Part Sequencing** to the current plate or all plates. Each cutoff is moved earlier as needed so it is cut before every part its nominal line passes through. Ordinary parts retain their relative order from the chosen sequencing route. A cutoff that splits the whole sheet — one continuous cut from sheet edge to sheet edge, such as a tail separator — goes after every part and every other cutoff, so the head never travels back across the separated edge. Cutoffs that cross no part but leave gaps keep their normal route position. Cutoff geometry, clearance, part programs, placements, and quantities are unchanged.