Files
OpenNest/OpenNest.Desktop/ReleaseRenest.cs
T
aj c5b6e680cb fix(release): keep ledger codes in the undo step and share bundles per sheet
Review fixes for ledger-first Make Nests:

- The release renest (offer, staged run, codes, note) moves into
  OpenNest.Desktop.ReleaseRenest, shared by the headless panel, the
  live main window and the tests. Codes are given inside each commit, so
  the live tab's Auto Nest undo step records them; before, undo and redo
  dropped every ledger code.
- A bundle repeats one code per sheet: a later nest of the run lost the
  whole bundle once an earlier nest took one sheet. Taken sheets are now
  counted per code (LedgerCodeFilter.ExcludeCopies).
- A stage whose layout is not kept lists every copy it asked for as not
  placed (the proposal cannot be kept), and a rejected ledger layout
  says "MaterialLedger not used: Auto Nest could not keep a layout on
  its stock" instead of a bare "No MaterialLedger stock used".
- "no free stock" is said only when the ledger had none; otherwise the
  note counts what was left out (on other nests, taken by earlier nests
  of this release, too small, unknown shape).
- The ledger is not read for a nest with nothing to nest, the live read
  runs under the tab's stop token, a reason's own full stop is not
  doubled, and the sheet count is the codes given.
2026-11-03 17:53:26 -05:00

104 lines
4.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using OpenNest.Data.MaterialLedger;
using OpenNest.Engine;
using OpenNest.Engine.Jobs;
namespace OpenNest.Desktop
{
/// <summary>
/// What Make Nests' renest step kept, its note and the ledger codes' locations.
/// </summary>
public sealed record ReleaseRenestResult(bool Nested, string Note,
IReadOnlyDictionary<string, string> LedgerLocations);
/// <summary>
/// Make Nests' Auto Nest of one released nest: on the run's MaterialLedger stock first,
/// then on the nest's plate defaults (<see cref="NestRenest.RunStagedAsync"/>), with the
/// ledger codes given to the plates inside each commit, so they are part of its undo step.
/// Shared by the headless and the live (main window) release.
/// </summary>
public static class ReleaseRenest
{
/// <summary>
/// Attaches a kept result's sheets to the nest's plates and returns them. It must call
/// <c>finish</c> with those plates before its undo step closes; that gives them their
/// ledger codes.
/// </summary>
public delegate IReadOnlyList<Plate> Commit(NestPipelineResult result,
Action<IReadOnlyList<Plate>> finish);
/// <summary>
/// Nests <paramref name="nest"/>, which has no parts placed, on <paramref name="ledger"/>'s
/// stock (null: the plate defaults only) and then its plate defaults. The ledger is not
/// read when nothing has a quantity to nest.
/// </summary>
public static async Task<ReleaseRenestResult> RunAsync(Nest nest, ReleaseLedgerStock ledger,
string engineName, double minimumSalvageDimension,
Func<NestPipelineRequest, Task<NestPipelineResult>> solve, Commit commit,
CancellationToken token)
{
ArgumentNullException.ThrowIfNull(nest);
ArgumentNullException.ThrowIfNull(solve);
ArgumentNullException.ThrowIfNull(commit);
var wanted = nest.Drawings.Any(d => !d.IsCutOff && d.Quantity.Remaining > 0);
var offer = ledger != null && wanted ? await ledger.OfferAsync(nest, token) : null;
var locations = new Dictionary<string, string>(StringComparer.Ordinal);
var renested = await NestRenest.RunStagedAsync(nest, engineName, offer?.Options,
minimumSalvageDimension, solve,
result => commit(result, plates =>
{
if (offer == null)
return;
var stock = NestPipelineCommit.PairWithStock(result, plates)
.Select(p => (p.Plate, p.Stock.Id))
.ToList();
foreach (var (code, location) in ledger.AssignCodes(nest, offer, stock))
locations[code] = location;
}),
token);
var ledgerNote = offer == null
? null
: ReleaseLedgerStock.Note(offer, nest, renested.FirstStockNotKept);
return new ReleaseRenestResult(renested.Nested, Join(renested.Note, ledgerNote),
locations);
}
/// <summary>
/// <see cref="RunAsync"/> for a nest no one is looking at: every plate is removed, the
/// layouts are solved on the calling thread and attached as new plates.
/// </summary>
public static Task<ReleaseRenestResult> RunHeadlessAsync(Nest nest,
ReleaseLedgerStock ledger, INestingEngine engine, string engineName,
double minimumSalvageDimension,
CancellationToken token)
{
ArgumentNullException.ThrowIfNull(nest);
ArgumentNullException.ThrowIfNull(engine);
token.ThrowIfCancellationRequested();
nest.Plates.Clear();
nest.UpdateDrawingQuantities();
return RunAsync(nest, ledger, engineName, minimumSalvageDimension,
request => Task.FromResult(
NestPipeline.Run(engine, engineName, request, null, token)),
(result, finish) =>
{
var plates = NestPipelineCommit.ApplyToPlates(result, nest.CreatePlate,
token: token);
finish(plates);
return plates;
},
token);
}
/// <summary>Auto Nest's note, then what MaterialLedger stock the nest used.</summary>
public static string Join(string renestNote, string ledgerNote) =>
string.Join(" ", new[] { renestNote, ledgerNote }.Where(n => !string.IsNullOrEmpty(n)));
}
}