using System;
using System.Threading;
using OpenNest.Engine.Jobs.Adapters;
namespace OpenNest.Engine.Jobs.Placement;
///
/// Migrated built-in placement strategy for the whole-job runner. Reuses
/// iterative shrink-fill/pack geometry with the same run-scoped bookkeeping as :
/// remaining demand is read from the request and placement counts are derived from returned placements.
///
///
/// The identity/progress boundary mechanics live in : a private
/// per requirement is created once per solve and reused across trials
/// (safe: the engine mutates per-trial and canonical copies, never the
/// shared Drawing). Identity is by Drawing reference. Each trial gets a fresh private .
///
public sealed class StripPlateNester : IPlateNester
{
private readonly Func engineFactory;
private readonly CandidatePlacementContext context = new();
public StripPlateNester()
: this(static plate => new StripNestEngine(plate)) { }
/// Injectable for tests; defaults to .
public StripPlateNester(Func engineFactory)
{
this.engineFactory =
engineFactory ?? throw new ArgumentNullException(nameof(engineFactory));
}
public PlateCandidate Place(
PlatePlacementRequest request,
IProgress progress = null,
CancellationToken token = default
)
{
ArgumentNullException.ThrowIfNull(request);
token.ThrowIfCancellationRequested();
var plate = DrawingJobMapper.CreatePlate(request.Stock);
var items = context.CreateItems(request.Parts);
var engine =
engineFactory(plate)
?? throw new InvalidOperationException("Engine factory returned null.");
var legacyProgress = CandidateProgressBridge.Create(progress, request.Stock.Id);
var parts = engine.Nest(items, legacyProgress, token);
token.ThrowIfCancellationRequested();
if (parts == null)
throw new InvalidOperationException("Engine returned null placements.");
return new PlateCandidate(context.MapPlacements(parts));
}
}