mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-22 04:17:40 -05:00
The last strip line stops at the remnant's full-length horizontal separator, so it never counted as separating the sheet and the planner cut it wherever the route passed it, often first (N26-264). That frees the X-tail remnant early and loses the mass that absorbs heat while the parts are cut. A continuous line from a sheet edge or separator to another now counts as a separator too (CutOff.Separators, found in rounds), so Plan Cutting and Part Sequencing cut it after every part, just before the separator it stops on. The problem-nest harness now adds scrap cut-offs with KeepHorizontalRemnant, as both desktop entry points do. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
534 lines
25 KiB
C#
534 lines
25 KiB
C#
using OpenNest.CNC;
|
|
using OpenNest.CNC.CuttingPlanning;
|
|
using OpenNest.CNC.CuttingStrategy;
|
|
using OpenNest.Engine.CuttingPlanning;
|
|
using OpenNest.Geometry;
|
|
using static OpenNest.Tests.TestHelpers;
|
|
|
|
namespace OpenNest.Tests.CuttingPlanning;
|
|
|
|
public class CuttingDependencyTests
|
|
{
|
|
// Started just above P, nearest-first cuts P before the far cutoff start. P leaves upward
|
|
// and the cutoff's trimmed cut stops short of P, so P, cutoff, Q crosses nothing: only the
|
|
// nominal-span dependency puts the cutoff first.
|
|
[Theory]
|
|
[InlineData(false)]
|
|
[InlineData(true)]
|
|
public void Plan_CutOffWhoseNominalSpanCrossesAPart_IsCutFirstAndAppliedThatWay(bool regenerate)
|
|
{
|
|
var parameters = ExplicitContourTests.Parameters();
|
|
var (_, plate, p, q, cut) = CutOffPlate(parameters, orphan: false);
|
|
var request = CuttingPlanRequest.ForPlate(plate, new Vector(11, 12.5),
|
|
confirmedParameters: regenerate ? parameters : null);
|
|
var snapshot = CuttingPlanService.Capture(request);
|
|
Assert.Equal(new[] { 2 }, snapshot.Dependencies.PrerequisitesOf(0));
|
|
Assert.Empty(snapshot.Dependencies.PrerequisitesOf(1));
|
|
Assert.Empty(snapshot.Dependencies.PrerequisitesOf(2));
|
|
|
|
var result = CuttingPlanService.Plan(snapshot);
|
|
|
|
Assert.True(result.Status == CuttingPlanStatus.Ready, Describe(result));
|
|
Assert.True(result.IndependentlyReplayed);
|
|
var order = result.ProposedOrder.Select(o => o.SourcePart).ToList();
|
|
Assert.True(order.IndexOf(cut) < order.IndexOf(p), string.Join(",", result.ProposedOrder.Select(o => o.SourceOrdinal)));
|
|
Assert.Equal(regenerate, result.ProposedOrder.Single(o => ReferenceEquals(o.SourcePart, p)).IsRegenerated);
|
|
Assert.False(result.ProposedOrder.Single(o => ReferenceEquals(o.SourcePart, cut)).IsRegenerated);
|
|
|
|
Assert.Equal(CuttingCommitStatus.Applied, CuttingPlanService.Apply([result]).Status);
|
|
Assert.Equal(order, plate.Parts);
|
|
Assert.Contains(q, plate.Parts);
|
|
}
|
|
|
|
[Fact]
|
|
public void Plan_SheetSeparatingCutOff_IsCutAfterEveryPartAndOtherCutOff()
|
|
{
|
|
var parameters = ExplicitContourTests.Parameters();
|
|
var (plate, separator, crossing, left, right) = SeparatedPlate();
|
|
var snapshot = CuttingPlanService.Capture(CuttingPlanRequest.ForPlate(plate, confirmedParameters: parameters));
|
|
var separatorIndex = plate.Parts.IndexOf(separator);
|
|
Assert.Equal(Enumerable.Range(0, plate.Parts.Count).Where(i => i != separatorIndex),
|
|
snapshot.Dependencies.PrerequisitesOf(separatorIndex));
|
|
|
|
var result = CuttingPlanService.Plan(snapshot);
|
|
|
|
Assert.True(result.Status == CuttingPlanStatus.Ready, Describe(result));
|
|
var order = result.ProposedOrder.Select(o => o.SourcePart).ToList();
|
|
Assert.Same(separator, order[^1]);
|
|
Assert.True(result.ProposedOrder[^1].SeparatesSheet);
|
|
foreach (var segment in crossing)
|
|
{
|
|
Assert.False(result.ProposedOrder.Single(o => ReferenceEquals(o.SourcePart, segment)).SeparatesSheet);
|
|
Assert.True(order.IndexOf(segment) < order.IndexOf(left));
|
|
}
|
|
Assert.Equal(CuttingCommitStatus.Applied, CuttingPlanService.Apply([result]).Status);
|
|
Assert.Same(separator, plate.Parts[^1]);
|
|
Assert.Contains(right, plate.Parts);
|
|
}
|
|
|
|
// N26-264: the last strip line stops at the remnant's full-length horizontal separator. It
|
|
// frees the corner remnant with the separator, so both are cut after every part, keeping the
|
|
// remnant's mass on the sheet to absorb heat; the line goes first, as it stops on the separator.
|
|
[Fact]
|
|
public void Plan_LineStoppingOnTheRemnantSeparator_IsCutLastBeforeTheSeparator()
|
|
{
|
|
var nest = new Nest();
|
|
var plate = nest.CreatePlate();
|
|
plate.Size = new Size(100, 100);
|
|
var low = new Part(new Drawing("low", ExplicitContourTests.Square(false)), new Vector(1, 1));
|
|
var high = new Part(new Drawing("high", ExplicitContourTests.Square(false)), new Vector(1, 20));
|
|
plate.Parts.Add(low);
|
|
plate.Parts.Add(high);
|
|
var vertical = new CutOff(new Vector(40, 0), CutOffAxis.Vertical) { EndLimit = 40 };
|
|
var horizontal = new CutOff(new Vector(0, 40), CutOffAxis.Horizontal);
|
|
plate.CutOffs.Add(vertical);
|
|
plate.CutOffs.Add(horizontal);
|
|
plate.RegenerateCutOffs(new CutOffSettings());
|
|
var line = plate.Parts.Single(p => ReferenceEquals(p.BaseDrawing, vertical.Drawing));
|
|
var separator = plate.Parts.Single(p => ReferenceEquals(p.BaseDrawing, horizontal.Drawing));
|
|
plate.Parts.Reorder([line, low, separator, high]);
|
|
var snapshot = CuttingPlanService.Capture(CuttingPlanRequest.ForPlate(plate,
|
|
confirmedParameters: ExplicitContourTests.Parameters()));
|
|
Assert.Equal(new[] { 1, 3 }, snapshot.Dependencies.PrerequisitesOf(0));
|
|
Assert.Equal(new[] { 0, 1, 3 }, snapshot.Dependencies.PrerequisitesOf(2));
|
|
|
|
var result = CuttingPlanService.Plan(snapshot);
|
|
|
|
Assert.True(result.Status == CuttingPlanStatus.Ready, Describe(result));
|
|
Assert.Equal(new[] { line, separator }, result.ProposedOrder.Skip(2).Select(o => o.SourcePart));
|
|
Assert.All(result.ProposedOrder.Skip(2), o => Assert.True(o.SeparatesSheet));
|
|
}
|
|
|
|
[Fact]
|
|
public void Plan_KeptOrderCuttingTheSeparatorFirst_IsAConstraintConflictThatSaysWhy()
|
|
{
|
|
var (plate, separator, _, _, _) = SeparatedPlate();
|
|
Assert.Same(separator, plate.Parts[0]);
|
|
|
|
var result = CuttingPlanService.Plan(CuttingPlanService.Capture(CuttingPlanRequest.ForPlate(plate,
|
|
confirmedParameters: ExplicitContourTests.Parameters(), preservePartOrder: true)));
|
|
|
|
Assert.Equal(CuttingPlanStatus.ConstraintConflict, result.Status);
|
|
Assert.Contains(result.Findings, f => f.Message.Contains("sheet-separating cutoff")
|
|
&& f.Message.Contains("before the sheet is separated"));
|
|
}
|
|
|
|
// A column of two wide parts under three narrow ones, beside a sheet separator at X 12.5
|
|
// pierced at (12.5, 0). Climbing the column ends at the top, where the straight approach to
|
|
// that pierce crosses the wide parts, and the separator cannot be cut any earlier. The search
|
|
// learns the other way round, the part the approach left from before the parts it crossed,
|
|
// and so finishes beside the pierce. It used to learn nothing and spend its whole budget.
|
|
[Fact]
|
|
public void Plan_ApproachIntoASeparatorCrossingCutParts_LearnsToLeaveFromBeyondThem()
|
|
{
|
|
var (plate, column, separator) = ColumnBesideSeparator();
|
|
|
|
var result = CuttingPlanService.Plan(CuttingPlanRequest.ForPlate(plate,
|
|
confirmedParameters: ExplicitContourTests.Parameters()));
|
|
|
|
Assert.True(result.Status == CuttingPlanStatus.Ready, Describe(result));
|
|
var order = result.ProposedOrder.Select(o => o.SourcePart).ToList();
|
|
Assert.Same(separator, order[^1]);
|
|
Assert.Contains(order[^2], column[..2]);
|
|
Assert.True(result.Expansions < 400, Describe(result));
|
|
}
|
|
|
|
private static (Plate, Part[] Column, Part Separator) ColumnBesideSeparator()
|
|
{
|
|
var nest = new Nest();
|
|
var plate = nest.CreatePlate();
|
|
plate.Size = new Size(100, 100);
|
|
Part[] column =
|
|
[
|
|
Rectangle(2, 2, 10, 10), Rectangle(2, 14, 10, 10),
|
|
Rectangle(2, 26, 5, 10), Rectangle(2, 38, 5, 10), Rectangle(2, 50, 5, 10),
|
|
];
|
|
foreach (var part in column)
|
|
plate.Parts.Add(part);
|
|
plate.CutOffs.Add(new CutOff(new Vector(12.5, 0), CutOffAxis.Vertical));
|
|
plate.RegenerateCutOffs(new CutOffSettings());
|
|
return (plate, column, plate.Parts.Single(p => p.BaseDrawing.IsCutOff));
|
|
}
|
|
|
|
// The tour cuts B, then the locked rectangle A (leaving right), then the cutoff pierced at
|
|
// (10, 0) below-left of A: that approach crosses A. Replanning B's entries can never fix it,
|
|
// so the search learns "cutoff first" at once instead of retrying each of B's entries.
|
|
[Fact]
|
|
public void Plan_ApproachIntoACutOffCrossingCutParts_LearnsToCutItFirstWithoutRetrying()
|
|
{
|
|
var parameters = ExplicitContourTests.Parameters();
|
|
var nest = new Nest();
|
|
var plate = nest.CreatePlate();
|
|
plate.Size = new Size(100, 100);
|
|
var b = Emitted(parameters, new Vector(20, 60));
|
|
var a = Rectangle(20, 40, 10, 10);
|
|
a.LeadInsLocked = true;
|
|
var cutOff = new CutOff(new Vector(10, 0), CutOffAxis.Vertical) { StartLimit = 0, EndLimit = 40 };
|
|
var program = new Program();
|
|
program.Codes.Add(new RapidMove(10, 0));
|
|
program.Codes.Add(new LinearMove(10, 40) { Layer = LayerType.Cut });
|
|
cutOff.Drawing.Program = program;
|
|
plate.CutOffs.Add(cutOff);
|
|
var cut = new Part(cutOff.Drawing);
|
|
plate.Parts.Add(b);
|
|
plate.Parts.Add(a);
|
|
plate.Parts.Add(cut);
|
|
|
|
var result = CuttingPlanService.Plan(CuttingPlanRequest.ForPlate(plate, new Vector(21, 70),
|
|
confirmedParameters: parameters));
|
|
|
|
Assert.True(result.Status == CuttingPlanStatus.Ready, Describe(result));
|
|
var order = result.ProposedOrder.Select(o => o.SourcePart).ToList();
|
|
Assert.True(order.IndexOf(cut) < order.IndexOf(a), string.Join(",", result.ProposedOrder.Select(o => o.SourceOrdinal)));
|
|
// Learning at once takes 40 expansions (22, then B planned again to face the cutoff that
|
|
// now follows it); retrying every entry of B first took 60.
|
|
Assert.True(result.Expansions < 50, Describe(result));
|
|
}
|
|
|
|
// 100 x 100 sheet: squares at X 1-11 and 60-70; a cutoff at X 5 is trimmed around the left
|
|
// square into two segment parts, while a continuous line at X 40 splits the sheet. Both
|
|
// cutoffs start the sequence.
|
|
private static (Plate, Part Separator, Part[] Crossing, Part Left, Part Right) SeparatedPlate()
|
|
{
|
|
var nest = new Nest();
|
|
var plate = nest.CreatePlate();
|
|
plate.Size = new Size(100, 100);
|
|
var left = new Part(new Drawing("left", ExplicitContourTests.Square(false)), new Vector(1, 1));
|
|
var right = new Part(new Drawing("right", ExplicitContourTests.Square(false)), new Vector(60, 1));
|
|
plate.Parts.Add(left);
|
|
plate.Parts.Add(right);
|
|
var separator = new CutOff(new Vector(40, 0), CutOffAxis.Vertical);
|
|
var crossing = new CutOff(new Vector(5, 0), CutOffAxis.Vertical);
|
|
plate.CutOffs.Add(separator);
|
|
plate.CutOffs.Add(crossing);
|
|
plate.RegenerateCutOffs(new CutOffSettings());
|
|
var cuts = plate.Parts.Where(p => p.BaseDrawing.IsCutOff).ToArray();
|
|
Assert.Equal(3, cuts.Length);
|
|
var separatorPart = cuts.Single(p => ReferenceEquals(p.BaseDrawing, separator.Drawing));
|
|
var crossingParts = cuts.Where(p => ReferenceEquals(p.BaseDrawing, crossing.Drawing)).ToArray();
|
|
Assert.Equal(2, crossingParts.Length);
|
|
plate.Parts.Reorder([separatorPart, .. crossingParts, left, right]);
|
|
return (plate, separatorPart, crossingParts, left, right);
|
|
}
|
|
|
|
[Fact]
|
|
public void Apply_CutOffReclassifiedAfterPlanning_IsStale()
|
|
{
|
|
var (_, plate, p, q, cut) = CutOffPlate(ExplicitContourTests.Parameters(), orphan: false);
|
|
var result = CuttingPlanService.Plan(CuttingPlanRequest.ForPlate(plate, new Vector(11, 12.5)));
|
|
Assert.Equal(CuttingPlanStatus.Ready, result.Status);
|
|
cut.BaseDrawing.IsCutOff = false;
|
|
|
|
var commit = CuttingPlanService.Apply([result]);
|
|
|
|
Assert.Equal(CuttingCommitStatus.Stale, commit.Status);
|
|
Assert.Equal(new[] { p, q, cut }, plate.Parts);
|
|
}
|
|
|
|
[Fact]
|
|
public void Plan_OrphanedCutOff_PrecedesEveryPart()
|
|
{
|
|
var (_, plate, p, q, cut) = CutOffPlate(ExplicitContourTests.Parameters(), orphan: true);
|
|
var snapshot = CuttingPlanService.Capture(CuttingPlanRequest.ForPlate(plate, new Vector(11, 12.5)));
|
|
Assert.Equal(new[] { 2 }, snapshot.Dependencies.PrerequisitesOf(0));
|
|
Assert.Equal(new[] { 2 }, snapshot.Dependencies.PrerequisitesOf(1));
|
|
|
|
var result = CuttingPlanService.Plan(snapshot);
|
|
|
|
Assert.True(result.Status == CuttingPlanStatus.Ready, Describe(result));
|
|
Assert.Same(cut, result.ProposedOrder[0].SourcePart);
|
|
}
|
|
|
|
[Fact]
|
|
public void Plan_PreservedOrderCuttingAPartBeforeItsCutOff_IsAConstraintConflict()
|
|
{
|
|
var (_, plate, p, _, cut) = CutOffPlate(ExplicitContourTests.Parameters(), orphan: false);
|
|
|
|
var result = CuttingPlanService.Plan(CuttingPlanRequest.ForPlate(plate, new Vector(11, 12.5), preservePartOrder: true));
|
|
|
|
Assert.Equal(CuttingPlanStatus.ConstraintConflict, result.Status);
|
|
Assert.Empty(result.ProposedOrder);
|
|
var finding = Assert.Single(result.Findings);
|
|
Assert.Same(p, finding.SourcePart);
|
|
Assert.Same(cut, finding.OtherSourcePart);
|
|
Assert.Contains("cutoff", finding.Message);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(false)]
|
|
[InlineData(true)]
|
|
public void Replay_RechecksDependenciesInsteadOfTrustingTheSearch(bool regenerate)
|
|
{
|
|
var parameters = ExplicitContourTests.Parameters();
|
|
var (_, plate, _, _, _) = CutOffPlate(parameters, orphan: false);
|
|
var snapshot = CuttingPlanService.Capture(CuttingPlanRequest.ForPlate(plate, new Vector(11, 12.5),
|
|
confirmedParameters: regenerate ? parameters : null));
|
|
var ready = CuttingPlanService.Plan(snapshot);
|
|
Assert.Equal(CuttingPlanStatus.Ready, ready.Status);
|
|
// Same verified placements, counterfeit order: crossed part before its cutoff.
|
|
var counterfeit = ready.ProposedOrder.OrderBy(o => o.SourceOrdinal).ToArray();
|
|
|
|
var replayed = regenerate
|
|
? CuttingPlanService.ReplayPrograms(snapshot, counterfeit, 0, default)
|
|
: CuttingPlanService.Replay(snapshot, counterfeit.Select(o => o.SourceOrdinal).ToArray(), 0, default);
|
|
|
|
Assert.Equal(CuttingPlanStatus.InvalidInput, replayed.Status);
|
|
Assert.Empty(replayed.ProposedOrder);
|
|
Assert.Contains(replayed.Findings, f => f.SourceOrdinal == 0 && f.OtherSourceOrdinal == 2);
|
|
}
|
|
|
|
[Fact]
|
|
public void Capture_DetachedListWithACutOff_IsUnsupported()
|
|
{
|
|
var (_, plate, _, _, _) = CutOffPlate(ExplicitContourTests.Parameters(), orphan: false);
|
|
|
|
var result = CuttingPlanService.Plan(new CuttingPlanRequest(plate.Parts.ToArray()));
|
|
|
|
Assert.Equal(CuttingPlanStatus.UnsupportedGeometry, result.Status);
|
|
Assert.Contains("plate-scoped", Assert.Single(result.Findings).Message);
|
|
}
|
|
|
|
[Fact]
|
|
public void Plan_PartInsideAHostCutout_IsCutBeforeTheHost()
|
|
{
|
|
var (plate, host, inner) = NestedPlate(Rectangle(2.7, 2.7, 0.6, 0.6));
|
|
var snapshot = CuttingPlanService.Capture(CuttingPlanRequest.ForPlate(plate));
|
|
Assert.Equal(new[] { 1 }, snapshot.Dependencies.PrerequisitesOf(0));
|
|
Assert.Empty(snapshot.Dependencies.PrerequisitesOf(1));
|
|
|
|
var result = CuttingPlanService.Plan(snapshot);
|
|
|
|
Assert.True(result.Status == CuttingPlanStatus.Ready, Describe(result));
|
|
Assert.Equal(new[] { inner, host }, result.ProposedOrder.Select(o => o.SourcePart));
|
|
|
|
var preserved = CuttingPlanService.Plan(CuttingPlanRequest.ForPlate(plate, preservePartOrder: true));
|
|
Assert.Equal(CuttingPlanStatus.ConstraintConflict, preserved.Status);
|
|
var finding = Assert.Single(preserved.Findings);
|
|
Assert.Same(host, finding.SourcePart);
|
|
Assert.Same(inner, finding.OtherSourcePart);
|
|
Assert.Contains("nested in its cutout", finding.Message);
|
|
}
|
|
|
|
// Started beside the regular parts, a nearest-first tour would cut the far coupons last;
|
|
// cut-first makes each coupon a prerequisite of every other part.
|
|
[Fact]
|
|
public void Plan_CutFirstParts_PrecedeEveryOtherPartHoweverFarAway()
|
|
{
|
|
var nest = new Nest();
|
|
var plate = nest.CreatePlate();
|
|
var parameters = ExplicitContourTests.Parameters();
|
|
var near = Emitted(parameters, new Vector(0, 0));
|
|
var nearer = Emitted(parameters, new Vector(4, 0));
|
|
var coupon = Emitted(parameters, new Vector(40, 0));
|
|
var other = Emitted(parameters, new Vector(44, 0));
|
|
coupon.BaseDrawing.CutFirst = true;
|
|
other.BaseDrawing.CutFirst = true;
|
|
foreach (var part in new[] { near, nearer, coupon, other })
|
|
plate.Parts.Add(part);
|
|
var snapshot = CuttingPlanService.Capture(CuttingPlanRequest.ForPlate(plate, new Vector(-1, 1),
|
|
confirmedParameters: parameters));
|
|
Assert.Equal(new[] { 2, 3 }, snapshot.Dependencies.PrerequisitesOf(0));
|
|
Assert.Equal(new[] { 2, 3 }, snapshot.Dependencies.PrerequisitesOf(1));
|
|
Assert.Empty(snapshot.Dependencies.PrerequisitesOf(2));
|
|
Assert.Empty(snapshot.Dependencies.PrerequisitesOf(3));
|
|
|
|
var result = CuttingPlanService.Plan(snapshot);
|
|
|
|
Assert.True(result.Status == CuttingPlanStatus.Ready, Describe(result));
|
|
Assert.Equal(new[] { coupon, other }, result.ProposedOrder.Take(2).Select(o => o.SourcePart).OrderBy(plate.Parts.IndexOf));
|
|
|
|
var preserved = CuttingPlanService.Plan(CuttingPlanRequest.ForPlate(plate, new Vector(-1, 1),
|
|
confirmedParameters: parameters, preservePartOrder: true));
|
|
Assert.Equal(CuttingPlanStatus.ConstraintConflict, preserved.Status);
|
|
Assert.Contains("cut-first part", preserved.Findings[0].Message);
|
|
}
|
|
|
|
[Fact]
|
|
public void Plan_PartInsideACutFirstHostCutout_StillPrecedesTheHost()
|
|
{
|
|
var (plate, host, inner) = NestedPlate(Rectangle(2.7, 2.7, 0.6, 0.6));
|
|
host.BaseDrawing.CutFirst = true;
|
|
var outside = Rectangle(20, 0, 2, 2);
|
|
plate.Parts.Add(outside);
|
|
var snapshot = CuttingPlanService.Capture(CuttingPlanRequest.ForPlate(plate));
|
|
Assert.Equal(new[] { 1 }, snapshot.Dependencies.PrerequisitesOf(0));
|
|
Assert.Empty(snapshot.Dependencies.PrerequisitesOf(1));
|
|
Assert.Equal(new[] { 0 }, snapshot.Dependencies.PrerequisitesOf(2));
|
|
|
|
var result = CuttingPlanService.Plan(snapshot);
|
|
|
|
Assert.True(result.Status == CuttingPlanStatus.Ready, Describe(result));
|
|
Assert.Equal(new[] { inner, host, outside }, result.ProposedOrder.Select(o => o.SourcePart));
|
|
}
|
|
|
|
[Fact]
|
|
public void Dependencies_NonmaterialMotionsCannotHideANestedPart()
|
|
{
|
|
// Same insert as above plus a remote rapid and scribe mark: material is unchanged, so
|
|
// the inner-before-host prerequisite must not depend on whole-program bounds.
|
|
var (plate, host, inner) = NestedPlate(Marked(Rectangle(2.7, 2.7, 0.6, 0.6)));
|
|
var snapshot = CuttingPlanService.Capture(CuttingPlanRequest.ForPlate(plate));
|
|
Assert.Null(snapshot.Findings.FirstOrDefault()?.Message);
|
|
Assert.True(inner.BoundingBox.Left < host.BoundingBox.Left);
|
|
|
|
Assert.Equal(new[] { 1 }, snapshot.Dependencies.PrerequisitesOf(0));
|
|
var result = CuttingPlanService.Plan(snapshot);
|
|
Assert.True(result.Status == CuttingPlanStatus.Ready, Describe(result));
|
|
Assert.Equal(new[] { inner, host }, result.ProposedOrder.Select(o => o.SourcePart));
|
|
}
|
|
|
|
[Fact]
|
|
public void Capture_PartStraddlingAHostCutoutEdge_IsAmbiguousContainment()
|
|
{
|
|
var (plate, host, inner) = NestedPlate(Rectangle(3.6, 2.8, 0.8, 0.4));
|
|
|
|
var result = CuttingPlanService.Plan(CuttingPlanRequest.ForPlate(plate));
|
|
|
|
Assert.Equal(CuttingPlanStatus.UnsupportedGeometry, result.Status);
|
|
var finding = Assert.Single(result.Findings);
|
|
Assert.Same(inner, finding.SourcePart);
|
|
Assert.Same(host, finding.OtherSourcePart);
|
|
Assert.Contains("Containment", finding.Message);
|
|
}
|
|
|
|
[Fact]
|
|
public void Capture_PartInAConcavePocketOutsideTheHost_HasNoDependency()
|
|
{
|
|
var nest = new Nest();
|
|
var plate = nest.CreatePlate();
|
|
var host = Pocketed();
|
|
var inner = Rectangle(4.5, 5, 1, 1);
|
|
plate.Parts.Add(host);
|
|
plate.Parts.Add(inner);
|
|
|
|
var snapshot = CuttingPlanService.Capture(CuttingPlanRequest.ForPlate(plate));
|
|
|
|
Assert.Null(snapshot.Findings.FirstOrDefault()?.Message);
|
|
Assert.Empty(snapshot.Dependencies.PrerequisitesOf(0));
|
|
Assert.Empty(snapshot.Dependencies.PrerequisitesOf(1));
|
|
Assert.Equal(CuttingPlanStatus.Ready, CuttingPlanService.Plan(snapshot).Status);
|
|
}
|
|
|
|
[Fact]
|
|
public void DependencyGraph_ReportsCyclesAndFirstViolation()
|
|
{
|
|
Assert.NotNull(CuttingDependencyGraph.FromPrerequisites([[1], [0], []]).FindCycle());
|
|
var chain = CuttingDependencyGraph.FromPrerequisites([[], [0], [1]]);
|
|
Assert.Null(chain.FindCycle());
|
|
Assert.Null(chain.FirstViolation([0, 1, 2]));
|
|
Assert.Equal((2, 1), chain.FirstViolation([0, 2, 1]));
|
|
Assert.True(chain.IsReady(1, [0]));
|
|
Assert.False(chain.IsReady(2, [0]));
|
|
}
|
|
|
|
private static (Nest, Plate, Part P, Part Q, Part Cut) CutOffPlate(CuttingParameters parameters, bool orphan)
|
|
{
|
|
var nest = new Nest();
|
|
var plate = nest.CreatePlate();
|
|
var p = Emitted(parameters, new Vector(10, 10));
|
|
var q = Emitted(parameters, new Vector(40, 20));
|
|
var cutOff = new CutOff(new Vector(11, 0), CutOffAxis.Vertical) { StartLimit = 0, EndLimit = 30 };
|
|
var program = new Program();
|
|
program.Codes.Add(new RapidMove(11, 30));
|
|
program.Codes.Add(new LinearMove(11, 12.5) { Layer = LayerType.Cut });
|
|
cutOff.Drawing.Program = program;
|
|
if (!orphan)
|
|
plate.CutOffs.Add(cutOff);
|
|
var cut = new Part(cutOff.Drawing);
|
|
plate.Parts.Add(p);
|
|
plate.Parts.Add(q);
|
|
plate.Parts.Add(cut);
|
|
return (nest, plate, p, q, cut);
|
|
}
|
|
|
|
// A regenerable 2x2 square with its emitted lead at the top-edge midpoint.
|
|
private static Part Emitted(CuttingParameters parameters, Vector location)
|
|
{
|
|
var clean = LeadPathValidationTests.Rectangle(0, 0, 2, 2);
|
|
var prepared = PreparedContours.Capture(clean, parameters);
|
|
var part = new Part(new Drawing("same", clean), location);
|
|
Assert.True(part.RestoreLeadInProgram(prepared.Emit([prepared.ClosestEntry(0, new Vector(1, 3))]), false));
|
|
part.CuttingParameters = parameters;
|
|
return part;
|
|
}
|
|
|
|
// Host: 10x10 square with circular cutouts of radius 1 at (3,3) and (7,3), cut hole, hole,
|
|
// perimeter from the right so its own program has no completed-contour crossing.
|
|
private static (Plate, Part Host, Part Inner) NestedPlate(Part inner)
|
|
{
|
|
var clean = PreparedContourTests.Holes();
|
|
var parameters = ExplicitContourTests.Parameters();
|
|
var prepared = PreparedContours.Capture(clean, parameters);
|
|
var program = prepared.Emit(
|
|
[
|
|
prepared.Entry(0, 0, new Vector(4, 3)),
|
|
prepared.Entry(1, 0, new Vector(8, 3)),
|
|
prepared.ClosestEntry(2, new Vector(12, 3))
|
|
]);
|
|
var host = new Part(new Drawing("same", clean));
|
|
Assert.True(host.RestoreLeadInProgram(program, false));
|
|
var nest = new Nest();
|
|
var plate = nest.CreatePlate();
|
|
plate.Parts.Add(host);
|
|
plate.Parts.Add(inner);
|
|
return (plate, host, inner);
|
|
}
|
|
|
|
// Prefixes a remote rapid and scribe mark to both the clean and placed programs.
|
|
private static Part Marked(Part part)
|
|
{
|
|
static Program Prefix(Program source)
|
|
{
|
|
var program = new Program();
|
|
program.Codes.Add(new RapidMove(-20, -20));
|
|
program.Codes.Add(new LinearMove(-19, -20) { Layer = LayerType.Scribe });
|
|
program.Codes.AddRange(source.Codes);
|
|
return program;
|
|
}
|
|
|
|
var marked = new Part(new Drawing("same", Prefix(part.BaseDrawing.Program)), part.Location);
|
|
Assert.True(marked.RestoreLeadInProgram(Prefix(part.Program), false));
|
|
return marked;
|
|
}
|
|
|
|
// A U-shaped part open at the top between x 3 and 7, lead on its right edge.
|
|
private static Part Pocketed()
|
|
{
|
|
var outline = new[] { (10.0, 0.0), (0.0, 0.0), (0.0, 10.0), (3.0, 10.0), (3.0, 3.0), (7.0, 3.0),
|
|
(7.0, 10.0), (10.0, 10.0), (10.0, 5.0) };
|
|
var clean = new Program();
|
|
clean.MoveTo(10, 5);
|
|
foreach (var (x, y) in outline)
|
|
clean.LineTo(x, y);
|
|
var placed = new Program();
|
|
placed.MoveTo(10.25, 5);
|
|
placed.Codes.Add(new LinearMove(10, 5) { Layer = LayerType.Leadin });
|
|
foreach (var (x, y) in outline)
|
|
placed.LineTo(x, y);
|
|
placed.Codes.Add(new LinearMove(10.25, 5) { Layer = LayerType.Leadout });
|
|
var part = new Part(new Drawing("same", clean));
|
|
Assert.True(part.RestoreLeadInProgram(placed, false));
|
|
return part;
|
|
}
|
|
|
|
// Fixed-program rectangle at (x, y), contour starting at its right-edge midpoint with a
|
|
// 0.25 straight lead-in and lead-out to the right.
|
|
private static Part Rectangle(double x, double y, double width, double height)
|
|
{
|
|
var clean = new Program();
|
|
clean.MoveTo(width, height / 2);
|
|
TraceRectangleFromRightMidpoint(clean, width, height);
|
|
var part = new Part(new Drawing("same", clean), new Vector(x, y));
|
|
var placed = new Program();
|
|
placed.MoveTo(width + 0.25, height / 2);
|
|
placed.Codes.Add(new LinearMove(width, height / 2) { Layer = LayerType.Leadin });
|
|
TraceRectangleFromRightMidpoint(placed, width, height);
|
|
placed.Codes.Add(new LinearMove(width + 0.25, height / 2) { Layer = LayerType.Leadout });
|
|
Assert.True(part.RestoreLeadInProgram(placed, false));
|
|
return part;
|
|
}
|
|
|
|
private static string Describe(CuttingPlanResult r) =>
|
|
$"{r.Status}, expanded {r.Expansions}: " + string.Join("; ", r.Findings.Select(f => f.Message));
|
|
}
|