Files
OpenNest/OpenNest.Engine/Strategies/FillContext.cs
T
ajandClaude Opus 5.5 4b3d7d57aa feat(fill): fill the J-bar strip with whole-pair grids and grid-end inserts
Fill placed 30 J-shaped bars (30 x 7.5, 0.3 spacing) on a 48 x 96 sheet where
33 fit by hand. With pairs settled tight (07f30c12), two columns leave a 30.2"
strip, but the grid topped its last column up with single halves of pairs
and took that strip. The work that reached 33 was done on 2026-11-09 and left
uncommitted; this ports it onto master.

- PairFiller also tiles each pair grid with whole pairs only (FillLinear
  PartialCopies = false), after the others, and refills the strip it leaves;
  that grid wins only by placing more parts. Only fills without a quantity
  try it: in Auto Nest's capped fills it ended N26-2ER's sheet at 103.83
  instead of 103.08. The cutout lattice turns it off, since it crops its fill.
- GridEndInserter completes a strip's densest pair grid: it moves the grid
  to each corner, slides copies of its parts in from the open sides and fills
  the strip beyond. The J-bar strip takes three pairs stood on end, one more
  bar beside them and a pair underneath: 33 parts, as laid out by hand.

Corpus: the J-bar fill places 33 (baseline 30), N26-29X keeps 136, and
every auto-nest nest keeps its saved layout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 14:21:34 -05:00

122 lines
5.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading;
using OpenNest.Engine;
using OpenNest.Engine.Fill;
using OpenNest.Engine.Jobs.Placement.Fillers;
using OpenNest.Geometry;
namespace OpenNest.Engine.Strategies
{
public class FillContext
{
public NestItem Item { get; init; }
public Box WorkArea { get; init; }
public Plate Plate { get; init; }
public int PlateNumber { get; init; }
public CancellationToken Token { get; init; }
public IProgress<NestProgress> Progress { get; init; }
public FillPolicy Policy { get; init; } = new FillPolicy(new DefaultFillComparer());
/// <summary>
/// When positive, strategies may stop placing parts once a candidate holds this
/// many (see <see cref="FillLinear.MaxParts"/>). Zero fills the whole work area.
/// </summary>
public int MaxQuantity { get; init; }
/// <summary>
/// Whether the pairs phase also tries its grids with whole pairs only
/// (<see cref="Fill.PairFiller.WholePairGrids"/>).
/// </summary>
public bool WholePairGrids { get; init; } = true;
public PartType PartType { get; set; }
/// <summary>
/// The pre-canonicalization drawing that <see cref="Item"/>'s canonical copy was built
/// from. When set, <see cref="ReportProgress"/> rebinds reported parts to this drawing
/// before they reach <see cref="Progress"/>, so intermediate previews (e.g. the Nesting
/// Progress dialog, PlateView's active-parts overlay) show the drawing's real/visible
/// orientation instead of the transient canonical (MBR-axis-aligned) one. Null when the
/// caller isn't operating in canonical frame (e.g. tests driving a strategy directly).
/// </summary>
public Drawing OriginalDrawing { get; init; }
public List<Part> CurrentBest { get; set; }
/// <summary>
/// A cheap layout shown before the slow strategies run, so it can be accepted early. It
/// never becomes <see cref="CurrentBest"/> while strategies run, so they pick the same
/// winner as without it; the pipeline returns it only when canceled while it still leads.
/// </summary>
public List<Part> Provisional { get; set; }
/// <summary>True when <see cref="Provisional"/> beats <paramref name="parts"/>.</summary>
internal bool ProvisionalLeads(List<Part> parts) =>
Provisional != null && Policy.Comparer.IsBetter(Provisional, parts, WorkArea);
/// <summary>For progress reporting only; comparisons use Policy.Comparer.</summary>
public FillScore CurrentBestScore { get; set; }
public NestPhase WinnerPhase { get; set; }
public NestPhase ActivePhase { get; set; }
public List<PhaseResult> PhaseResults { get; } = new();
public List<AngleResult> AngleResults { get; } = new();
public Dictionary<string, object> SharedState { get; } = new();
/// <summary>
/// Standard progress reporting for strategies and fillers. Reports intermediate
/// results using the current ActivePhase, PlateNumber, and WorkArea.
/// When the reported parts beat the current pipeline best, promotes the
/// result to IsOverallBest so the UI updates immediately.
/// </summary>
public void ReportProgress(List<Part> parts, string description)
{
var isNewBest =
parts != null
&& parts.Count > 0
&& Policy.Comparer.IsBetter(parts, CurrentBest, WorkArea);
if (isNewBest)
{
CurrentBest = parts;
CurrentBestScore = FillScore.Compute(parts, WorkArea);
WinnerPhase = ActivePhase;
}
// The shown layout never regresses below the provisional one.
var shown = ProvisionalLeads(CurrentBest) ? Provisional : CurrentBest;
NestProgressReporter.Report(
Progress,
new ProgressReport
{
Phase = ActivePhase,
PlateNumber = PlateNumber,
Parts = ToOriginalFrame(shown),
WorkArea = WorkArea,
Description = description,
IsOverallBest = isNewBest && ReferenceEquals(shown, parts),
}
);
}
/// <summary>
/// Rebinds <paramref name="parts"/> to <see cref="OriginalDrawing"/> for outward-facing
/// progress reports. Uses a shallow list copy (not per-part <see cref="Part.Clone"/>) so
/// <see cref="CanonicalFrame.RebindToOriginal"/>'s in-place slot replacement can't corrupt
/// the caller's list (e.g. <see cref="CurrentBest"/>) — <c>Part.Clone()</c> re-derives its
/// target rotation from <c>BaseDrawing.Program.Rotation + Rotation</c>, which double-counts
/// the canonical drawing's baked source angle whenever it's non-zero. No-op when
/// <see cref="OriginalDrawing"/> isn't set. Internal so callers that build their own
/// <see cref="ProgressReport"/> outside <see cref="ReportProgress"/> (e.g. the fallback
/// report in <c>PlateFiller.RunPipeline</c> for strategies that don't self-report)
/// can apply the same rebind before reaching the UI.
/// </summary>
internal List<Part> ToOriginalFrame(List<Part> parts)
{
if (parts == null || parts.Count == 0 || OriginalDrawing == null)
return parts;
return CanonicalFrame.RebindToOriginal(new List<Part>(parts), OriginalDrawing);
}
}
}