Files
OpenNest/OpenNest.Tests/Engine/BandedNestingTests.cs
T
ajandClaude Fable 5.2 d2c392270d feat(engine): band every group and give each a trial in Irregular
On a saved nest of the README sample parts (130 copies, 60 x 120, 0.3
spacing, bottom clamp rail) Auto Nest ended at 105.8 where a hand layout
of tidy bands ends at 103.1. Three things kept the banded fill from it:

- Lattice's first pattern for the 24 gussets set two columns 0.04 apart.
  The band's certification rejected it and the band search stopped, so
  the gussets had no band and filled the gaps as ragged columns. The
  search now asks again just short of the rejected pattern
  (BlockCatalog.RejectedBandLength).
- Building patterns is charged to the same work meter as the trials of
  narrower and lower bands, and with the lower bands it spent the whole
  budget, so the last types in the order never had a trial. Trials now
  go round the types and may spend 25,000 work whatever the budget.
- A band taller than the height clear of the clamps lost members to
  them. The first lower band is now asked for at that height and
  searched like the full band; where such a band would stand whole in
  place of one that loses members, the bands are searched a second time
  with it there and the better sheet kept. A lower band no longer than
  the full one stands first in its place, leaving a strip beside it.

That nest now ends at 103.1, the same parts built from the README DXFs
at 104.0 instead of 106.6, the help tutorial nest at 33.6 instead of
34.2; N26-2HH stays at 103.9 and the other benchmark jobs are unchanged.
Solves of these jobs take about a tenth longer.

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

93 lines
4.5 KiB
C#

using OpenNest.Benchmark;
using OpenNest.Clamps;
using OpenNest.Engine.Jobs;
using OpenNest.Engine.Jobs.Adapters;
using OpenNest.Engine.NestingEngines.Irregular;
using OpenNest.Geometry;
using OpenNest.Tests.CuttingPlanning;
namespace OpenNest.Tests.Engine;
/// <summary>
/// Seven unlike sample parts (130 copies: base plates, L brackets, gussets, flanges, links, clamp
/// jaws, hex covers) on a 60 x 120 sheet. Before banding, Irregular ended the layout at 107.7":
/// each group packed as ragged columns, the links scattered into the gaps between them. Banded,
/// each group stands in its own full-height column and the layout ends at 102.9".
/// </summary>
public class BandedNestingTests
{
[Fact]
public void MixedSamplePartsNestInTidyBands()
{
var manifest = Path.Combine(PlanningCharacterizationTests.RepositoryRoot(), "test-data", "readme-parts",
"readme-mixed.manifest.json");
var job = DxfManifestLoader.Load(manifest).BuildNestJob(1);
var result = new IrregularNestingEngine().Solve(job);
Assert.Empty(NestLayoutCheck.Violations(job, result));
var plate = Assert.Single(NestResultMaterializer.Materialize(job, result).Nest.Plates);
Assert.Equal(130, plate.Parts.Count);
var right = plate.Parts.Max(p => p.BoundingBox.Right);
Assert.True(right < 104.5, $"Layout ends at {right:F3}");
}
/// <summary>
/// Lattice's first pattern for 24 gussets on this sheet (59" of height to fill) sets two
/// columns closer than the part spacing, so the band's certification rejects it. The band
/// search used to stop there, and the gussets, left without a band, filled the gaps as ragged
/// columns: the layout ended at 39.6". It now asks again just short of the rejected pattern
/// and bands them in 18.2", ending the layout at 39.1".
/// </summary>
[Fact]
public void GroupBandsWhenItsFirstPatternIsRejected()
{
var job = SampleJob(new NestPlateStock("sheet", new Size(60, 120), null, 0.3,
new Spacing(0.3, 0.75, 0.3, 0.25)), "Gusset", "Base Plate");
var result = new IrregularNestingEngine().Solve(job);
Assert.Empty(NestLayoutCheck.Violations(job, result));
var plate = Assert.Single(NestResultMaterializer.Materialize(job, result).Nest.Plates);
Assert.Equal(38, plate.Parts.Count);
var right = plate.Parts.Max(p => p.BoundingBox.Right);
Assert.True(right < 39.3, $"Layout ends at {right:F3}");
}
/// <summary>
/// All seven parts on the desktop's sheet: a wider bottom edge and a clamp rail along it.
/// Irregular ended at 106.6": the gussets had no band, building the lower bands spent the work
/// budget before the narrower bands of the last groups in the order had their trial, and the
/// hex covers stood in a band taller than another just as narrow, leaving no strip above them
/// for the links held back. It now ends at 104.0". (A saved nest of these parts, whose
/// drawings start their contours elsewhere, ends at 103.1", as a hand layout of it does.)
/// </summary>
[Fact]
public void MixedSamplePartsBandAboveAClampRail()
{
var clamps = Enumerable.Range(1, 6)
.Select(i => new ClampZone($"clamp-{i}", new Box(i * 20 - 0.75, 0, 1.5, 0.625))).ToList();
var job = SampleJob(new NestPlateStock("sheet", new Size(60, 120), null, 0.3,
new Spacing(0.3, 0.75, 0.3, 0.25), 1, clamps));
var result = new IrregularNestingEngine().Solve(job);
Assert.Empty(NestLayoutCheck.Violations(job, result));
var plate = Assert.Single(NestResultMaterializer.Materialize(job, result).Nest.Plates);
Assert.Equal(130, plate.Parts.Count);
var right = plate.Parts.Max(p => p.BoundingBox.Right);
Assert.True(right < 104.5, $"Layout ends at {right:F3}");
}
/// <summary>The sample parts (all of them, or those named) at their manifest quantities on one sheet.</summary>
private static NestJob SampleJob(NestPlateStock stock, params string[] names)
{
var manifest = Path.Combine(PlanningCharacterizationTests.RepositoryRoot(), "test-data", "readme-parts",
"readme-mixed.manifest.json");
var parts = DxfManifestLoader.Load(manifest).Requests
.Where(r => names.Length == 0 || names.Contains(r.Drawing.Name))
.Select(r => DrawingJobMapper.FromDrawing(r.Drawing.Id.ToString(), r.Drawing, r.Quantity));
return new NestJob(parts, new[] { stock }, new NestJobOptions(maxPlates: 1));
}
}