Files
OpenNest/OpenNest.Tests/Diagnostics/PlateEdgeReviewTests.cs
T
ajandClaude Fable 5.2 b137df8428 fix(review): a round part resting on an outlined sheet's edge is not flagged
On a sheet with a custom outline the plate edge review, Auto Nest's layout
check and the runner's candidate validation tested the part's spacing
outline against the usable area. That outline circumscribes arcs by up to
0.001, more than any of their slacks, so a disc whose true bounds rest on
the work-area edge read as inside the outline's edge spacing (or off the
plate at zero spacing) while the same disc on a rectangular sheet, judged
on its true bounds, was clean.

The outline test now works as the part-spacing check does: the spacing
outline finds the candidates and a failure is confirmed on the part's
native contour (NativeContours.Region: arcs circumscribed at 0.000025 and
clipped to the perimeter's true bounds). Both regions contain the material,
so a part past the usable edge by more than the slack still fails; no slack
changed. The runner now calls the layout check's own outline test
(NestLayoutCheck.InsideOutline): its separately flattened candidate could
accept a pose on a slanted edge that the layout check then rejected.

Pushes on an outlined sheet measured the same circumscribed perimeter so a
part would rest where the review passed, and box pushes stopped 0.0012
short of a wall for it. With the review confirming on native contours they
slide the material's own lines and arcs, so a round part rests on a wall
exactly as on the plate edge, and a box push rests the box on the wall.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-20 08:49:00 -05:00

282 lines
11 KiB
C#

using OpenNest.CNC;
using OpenNest.Diagnostics;
using OpenNest.Geometry;
using static OpenNest.Tests.TestHelpers;
namespace OpenNest.Tests.Diagnostics;
public class PlateEdgeReviewTests
{
[Fact]
public void PartInsideWorkArea_IsClear()
{
var report = Analyze(Edges(0.5), Square(1, 1, 4));
Assert.Empty(Find(report, PostVerificationKind.OffPlate));
Assert.Empty(Find(report, PostVerificationKind.EdgeSpacing));
Assert.DoesNotContain("Off the plate", report.ToDisplayText());
Assert.DoesNotContain("Outside work area", report.ToDisplayText());
}
[Fact]
public void PartAtEdgeSpacing_IsClear()
{
var report = Analyze(Edges(0.5), Square(0.5, 0.5, 4));
Assert.Empty(Find(report, PostVerificationKind.EdgeSpacing));
}
[Fact]
public void PartInsideEdgeSpacing_WarnsWithGapAndSide()
{
var report = Analyze(Edges(0.5), Square(0.2, 2, 4));
var finding = Assert.Single(Find(report, PostVerificationKind.EdgeSpacing));
Assert.Equal((1, 1), (finding.PlateNumber, finding.PartNumber));
Assert.Equal("'square' is outside the work area: 0.200 from the plate's left edge, inside its "
+ "0.500 edge spacing.", finding.Message);
Assert.Equal(0.1, finding.Location!.Value.X, 6);
Assert.Contains("Outside work area: 1 warning(s)", report.ToDisplayText());
Assert.False(report.CanPost(false));
}
[Fact]
public void FarEdges_FollowThePlateSize()
{
// Default plate is 120 long (X) and 60 wide (Y).
var finding = Assert.Single(Find(Analyze(Edges(0.5), Square(115.7, 20, 4)),
PostVerificationKind.EdgeSpacing));
Assert.Contains("right edge", finding.Message);
finding = Assert.Single(Find(Analyze(Edges(0.5), Square(20, 55.9, 4)), PostVerificationKind.EdgeSpacing));
Assert.Contains("top edge", finding.Message);
}
[Fact]
public void ShortfallWithinSlack_IsClear()
{
var report = Analyze(Edges(0.5), Square(0.5 - SpacingOutline.SpacingSlack / 2, 2, 4));
Assert.Empty(Find(report, PostVerificationKind.EdgeSpacing));
}
[Fact]
public void PartPastSheet_IsOffPlateNotEdgeSpacing()
{
var report = Analyze(Edges(0.5), Square(-1, 2, 4));
var finding = Assert.Single(Find(report, PostVerificationKind.OffPlate));
Assert.Contains("1.000 past the plate's left edge", finding.Message);
Assert.Equal(0, finding.Location!.Value.X, 6);
Assert.Empty(Find(report, PostVerificationKind.EdgeSpacing));
Assert.Contains("Off the plate: 1 warning(s)", report.ToDisplayText());
}
[Fact]
public void PartEntirelyOffSheet_IsOffPlate()
{
var finding = Assert.Single(Find(Analyze(new Spacing(), Square(130, 10, 4)), PostVerificationKind.OffPlate));
Assert.Contains("past the plate's right edge", finding.Message);
}
[Fact]
public void ZeroEdgeSpacing_AllowsContactWithTheEdge()
{
var report = Analyze(new Spacing(), Square(0, 0, 4), Square(116, 56, 4));
Assert.Empty(Find(report, PostVerificationKind.OffPlate));
Assert.Empty(Find(report, PostVerificationKind.EdgeSpacing));
}
[Fact]
public void LeadInPastSheet_IsOffPlate()
{
// The part keeps zero edge spacing, but its lead-in starts 0.25 off the left edge.
var program = new Program();
program.MoveTo(-0.25, 2);
program.Codes.Add(new LinearMove(0, 2) { Layer = LayerType.Leadin });
program.LineTo(0, 0);
program.LineTo(4, 0);
program.LineTo(4, 4);
program.LineTo(0, 4);
program.LineTo(0, 2);
var part = new Part(new Drawing("leaded", program), new Vector(0, 0));
var report = Analyze(new Spacing(), part);
var finding = Assert.Single(Find(report, PostVerificationKind.OffPlate));
Assert.Contains("0.250 past the plate's left edge", finding.Message);
}
[Fact]
public void LeadInsideEdgeSpacing_IsClear()
{
// Leads may run into the edge spacing; only material is held to it.
var program = new Program();
program.MoveTo(-0.25, 2);
program.Codes.Add(new LinearMove(0, 2) { Layer = LayerType.Leadin });
program.LineTo(0, 0);
program.LineTo(4, 0);
program.LineTo(4, 4);
program.LineTo(0, 4);
program.LineTo(0, 2);
var part = new Part(new Drawing("leaded", program), new Vector(0.5, 0.5));
var report = Analyze(Edges(0.5), part);
Assert.Empty(Find(report, PostVerificationKind.OffPlate));
Assert.Empty(Find(report, PostVerificationKind.EdgeSpacing));
}
[Fact]
public void Quadrant3Sheet_ChecksItsSignedFrame()
{
var nest = new Nest();
nest.PlateDefaults.Quadrant = 3;
var plate = nest.CreatePlate();
plate.EdgeSpacing = Edges(0.5);
plate.Parts.Add(Square(-4.2, -10, 4));
var finding = Assert.Single(Find(PostVerificationAnalyzer.Analyze(nest), PostVerificationKind.EdgeSpacing));
Assert.Contains("right edge", finding.Message);
}
[Fact]
public void OutlinedSheet_WarnsOverObstacleAndNearOutline()
{
var outline = new SheetOutline(
new[] { new Vector(0, 0), new Vector(120, 0), new Vector(120, 60), new Vector(0, 60) },
new[] { new[] { new Vector(50, 20), new Vector(70, 20), new Vector(70, 40), new Vector(50, 40) } });
var over = NestWith(Edges(0.5), Square(48, 25, 4));
over.Plates[0].Outline = outline;
Assert.Single(Find(PostVerificationAnalyzer.Analyze(over), PostVerificationKind.OffPlate));
var near = NestWith(Edges(0.5), Square(45.8, 25, 4));
near.Plates[0].Outline = outline;
var report = PostVerificationAnalyzer.Analyze(near);
Assert.Empty(Find(report, PostVerificationKind.OffPlate));
var finding = Assert.Single(Find(report, PostVerificationKind.EdgeSpacing));
Assert.Contains("is outside the work area: inside the 0.500 edge spacing of the plate's outline "
+ "or one of its obstacles", finding.Message);
var clear = NestWith(Edges(0.5), Square(10, 10, 4));
clear.Plates[0].Outline = outline;
report = PostVerificationAnalyzer.Analyze(clear);
Assert.Empty(Find(report, PostVerificationKind.OffPlate));
Assert.Empty(Find(report, PostVerificationKind.EdgeSpacing));
}
// The outline test circumscribes arcs by up to 0.001, more than the slack, so a disc whose
// true bounds rest on the work-area edge used to warn on an outlined sheet only.
[Theory]
[InlineData(20, 0.25)] // bottom edge of the sheet's box
[InlineData(0.25, 10)] // left edge
[InlineData(20, 23.75)] // top edge
[InlineData(56.75, 10)] // right edge
[InlineData(1, 3.25)] // the notch's top, an outline wall inside the box
[InlineData(6.25, 0.25)] // the notch's side and the bottom edge at once
public void OutlinedSheet_RoundPartRestingOnTheUsableEdge_IsClear(double x, double y)
{
var report = PostVerificationAnalyzer.Analyze(Notched(Edges(0.25), Disc(x, y)));
Assert.Empty(Find(report, PostVerificationKind.OffPlate));
Assert.Empty(Find(report, PostVerificationKind.EdgeSpacing));
}
[Fact]
public void OutlinedSheet_RoundPartOnTheSheetEdge_IsNotOffPlate()
{
// No edge spacing: the disc rests on the sheet's own edge and on the notch's top.
var report = PostVerificationAnalyzer.Analyze(Notched(new Spacing(), Disc(20, 0), Disc(1, 3)));
Assert.Empty(Find(report, PostVerificationKind.OffPlate));
Assert.Empty(Find(report, PostVerificationKind.EdgeSpacing));
}
[Fact]
public void OutlinedSheet_RoundPartPastTheUsableEdge_StillWarns()
{
// Within the slack passes; 0.001 into the notch's edge spacing does not.
var within = PostVerificationAnalyzer.Analyze(Notched(Edges(0.25), Disc(1, 3.25 - 0.0003)));
Assert.Empty(Find(within, PostVerificationKind.EdgeSpacing));
var past = PostVerificationAnalyzer.Analyze(Notched(Edges(0.25), Disc(1, 3.249)));
Assert.Empty(Find(past, PostVerificationKind.OffPlate));
var finding = Assert.Single(Find(past, PostVerificationKind.EdgeSpacing));
Assert.Contains("inside the 0.250 edge spacing of the plate's outline", finding.Message);
var beside = PostVerificationAnalyzer.Analyze(Notched(Edges(0.25), Disc(6.249, 0.25)));
Assert.Single(Find(beside, PostVerificationKind.EdgeSpacing));
// 0.001 into the missing corner itself is off the plate.
var off = PostVerificationAnalyzer.Analyze(Notched(new Spacing(), Disc(1, 2.999)));
var offPlate = Assert.Single(Find(off, PostVerificationKind.OffPlate));
Assert.Contains("lies outside the plate's outline", offPlate.Message);
}
[Fact]
public void CutOffParts_AreExempt()
{
var drawing = new Drawing("cutoff", Line(-1, 30, 125, 30)) { IsCutOff = true };
var report = Analyze(Edges(0.5), new Part(drawing, new Vector(0, 0)));
Assert.Empty(Find(report, PostVerificationKind.OffPlate));
Assert.Empty(Find(report, PostVerificationKind.EdgeSpacing));
}
[Fact]
public void InvalidEdgeSpacing_IsIncomplete()
{
var report = Analyze(new Spacing(double.NaN, 0, 0, 0), Square(10, 10, 4));
Assert.Contains(report.Findings, f => f.Kind == PostVerificationKind.Incomplete
&& f.Message.StartsWith("Plate edge check:"));
}
private static Program Line(double x1, double y1, double x2, double y2)
{
var program = new Program();
program.MoveTo(x1, y1);
program.LineTo(x2, y2);
return program;
}
private static Spacing Edges(double all) => new(all, all, all, all);
private static PostVerificationFinding[] Find(PostVerificationReport report, PostVerificationKind kind) =>
report.Findings.Where(f => f.Kind == kind).ToArray();
private static PostVerificationReport Analyze(Spacing edges, params Part[] parts) =>
PostVerificationAnalyzer.Analyze(NestWith(edges, parts));
private static Nest NestWith(Spacing edges, params Part[] parts)
{
var nest = new Nest();
var plate = nest.CreatePlate();
plate.EdgeSpacing = edges;
foreach (var part in parts)
plate.Parts.Add(part);
return nest;
}
private static Part Square(double x, double y, double size, string name = "square") =>
new(new Drawing(name, MakeSquareProgram(0, 0, size)), new Vector(x, y));
/// <summary>A radius 1.5 disc whose bounds start at (x, y).</summary>
private static Part Disc(double x, double y) =>
new(new Drawing("disc", MakeCircleProgram(1.5, 1.5, 1.5)), new Vector(x, y));
/// <summary>A 60 x 27 sheet missing a 6 x 3 rectangle at its lower-left corner.</summary>
private static Nest Notched(Spacing edges, params Part[] parts)
{
var nest = NestWith(edges, parts);
nest.Plates[0].Outline = new SheetOutline(new[]
{
new Vector(6, 0), new Vector(60, 0), new Vector(60, 27),
new Vector(0, 27), new Vector(0, 3), new Vector(6, 3),
});
return nest;
}
}