mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-06 13:59:25 -05:00
save_nest rebuilt the nest without the loaded nest's machine, common-line usage and minimum remnant size, and re-adding plates reset their support bed. Building the nest moves to NestSession.Snapshot, which keeps them, so posting can share it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
208 lines
7.2 KiB
C#
208 lines
7.2 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using OpenNest.Engine.CuttingPlanning;
|
|
|
|
namespace OpenNest.Mcp
|
|
{
|
|
public class NestSession
|
|
{
|
|
private Nest nest;
|
|
|
|
public Nest Nest
|
|
{
|
|
get => nest;
|
|
set
|
|
{
|
|
nest = value;
|
|
ReconcileStockReservations();
|
|
}
|
|
}
|
|
public List<Plate> Plates { get; } = new();
|
|
public List<Drawing> Drawings { get; } = new();
|
|
|
|
// A repeated layout owns its otherwise-empty physical sheet placeholders.
|
|
// Clearing/removing that owner releases only its copies, never another layout's.
|
|
// User-authored plates and pre-existing quantities imply no reservations.
|
|
private readonly Dictionary<Plate, HashSet<Plate>> stockReservations = new(ReferenceEqualityComparer.Instance);
|
|
|
|
internal bool IsConsumedStockPlate(Plate plate)
|
|
{
|
|
ReconcileStockReservations();
|
|
return stockReservations.Values.Any(copies => copies.Contains(plate));
|
|
}
|
|
|
|
internal void ReserveStockCopies(Plate owner, IEnumerable<Plate> copies)
|
|
{
|
|
var reserved = new HashSet<Plate>(copies, ReferenceEqualityComparer.Instance);
|
|
if (reserved.Count > 0)
|
|
stockReservations[owner] = reserved;
|
|
}
|
|
|
|
internal void ReleaseStockCopies(Plate owner) => stockReservations.Remove(owner);
|
|
|
|
private void ReconcileStockReservations()
|
|
{
|
|
if (stockReservations.Count == 0)
|
|
return;
|
|
|
|
// Both collections are publicly mutable. Reconcile by reference, not index,
|
|
// so removals/replacements cannot retain stale plates or free unrelated stock.
|
|
var live = new HashSet<Plate>(Plates, ReferenceEqualityComparer.Instance);
|
|
if (Nest != null)
|
|
live.UnionWith(Nest.Plates);
|
|
foreach (var (owner, copies) in stockReservations.ToArray())
|
|
{
|
|
if (!live.Contains(owner) || owner.Parts.Count == 0)
|
|
{
|
|
stockReservations.Remove(owner);
|
|
continue;
|
|
}
|
|
copies.RemoveWhere(copy => !live.Contains(copy));
|
|
if (copies.Count == 0)
|
|
stockReservations.Remove(owner);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Whole-job engine <c>autonest_plate</c> uses when a call does not pass an explicit
|
|
/// <c>engine</c> argument; fill tools default to the Fill strategy instead. Instance state
|
|
/// only — the process-global legacy registry is never read or written.
|
|
/// </summary>
|
|
public string DefaultEngineName { get; set; } = "Default";
|
|
|
|
/// <summary>
|
|
/// The last applicable <c>plan_cutting</c> proposal, awaiting <c>apply_cutting_plan</c>.
|
|
/// Apply re-checks every plate against its captured state, so edits since planning make it stale.
|
|
/// </summary>
|
|
internal CuttingPlanProposal CuttingProposal { get; set; }
|
|
|
|
public Plate GetPlate(int index)
|
|
{
|
|
if (index < 0)
|
|
return null;
|
|
|
|
if (Nest != null && index < Nest.Plates.Count)
|
|
return Nest.Plates[index];
|
|
|
|
var adjustedIndex = index - (Nest?.Plates.Count ?? 0);
|
|
if (adjustedIndex >= 0 && adjustedIndex < Plates.Count)
|
|
return Plates[adjustedIndex];
|
|
|
|
return null;
|
|
}
|
|
|
|
/// <summary>Remove only a genuinely unused sheet; loaded sheets precede appended sheets.</summary>
|
|
public bool TryDeleteEmptyPlate(int index, out string reason)
|
|
{
|
|
var plate = GetPlate(index);
|
|
if (plate == null)
|
|
{
|
|
reason = $"plate {index} not found";
|
|
return false;
|
|
}
|
|
|
|
if (plate.Parts.Count != 0)
|
|
{
|
|
reason = $"plate {index} is occupied";
|
|
return false;
|
|
}
|
|
|
|
if (plate.CutOffs.Count != 0)
|
|
{
|
|
reason = $"plate {index} has cutoff definitions";
|
|
return false;
|
|
}
|
|
|
|
var loadedCount = Nest?.Plates.Count ?? 0;
|
|
if (index < loadedCount)
|
|
Nest.Plates.RemoveAt(index);
|
|
else
|
|
Plates.RemoveAt(index - loadedCount);
|
|
|
|
ReconcileStockReservations();
|
|
reason = null;
|
|
return true;
|
|
}
|
|
|
|
public Drawing GetDrawing(string name)
|
|
{
|
|
if (Nest != null)
|
|
{
|
|
foreach (var d in Nest.Drawings)
|
|
{
|
|
if (d.Name == name)
|
|
return d;
|
|
}
|
|
}
|
|
|
|
foreach (var d in Drawings)
|
|
{
|
|
if (d.Name == name)
|
|
return d;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public List<Plate> AllPlates()
|
|
{
|
|
ReconcileStockReservations();
|
|
var all = new List<Plate>();
|
|
if (Nest != null)
|
|
all.AddRange(Nest.Plates);
|
|
all.AddRange(Plates);
|
|
return all;
|
|
}
|
|
|
|
/// <summary>
|
|
/// A nest holding every drawing and plate in the session, with the loaded nest's header
|
|
/// (material, thickness, units, defaults) or defaults when none is loaded. The drawings and
|
|
/// plates are the session's own objects, not copies.
|
|
/// </summary>
|
|
public Nest Snapshot(string name)
|
|
{
|
|
name ??= Nest?.Name ?? "";
|
|
var source = Nest;
|
|
var nest = new Nest
|
|
{
|
|
Name = name,
|
|
Customer = source?.Customer ?? "",
|
|
Notes = source?.Notes ?? "",
|
|
AssistGas = source?.AssistGas ?? "",
|
|
Status = source?.Status ?? NestStatus.Quote,
|
|
MadeBy = source?.MadeBy ?? "",
|
|
Thickness = source?.Thickness ?? 0,
|
|
Material = source?.Material ?? new Material(),
|
|
Units = source?.Units ?? default,
|
|
DateCreated = source?.DateCreated ?? default,
|
|
DateLastModified = source?.DateLastModified ?? default,
|
|
PlateDefaults = source?.PlateDefaults ?? new Nest.PlateSettings(),
|
|
PlateOptions = source?.PlateOptions ?? new(),
|
|
SalvageRate = source?.SalvageRate ?? 0.5,
|
|
Machine = source?.Machine,
|
|
CommonLineUsage = source?.CommonLineUsage ?? default,
|
|
MinimumRemnantDimension = source?.MinimumRemnantDimension ?? Nest.DefaultMinimumRemnantDimension,
|
|
};
|
|
// Before the plates: adding a plate gives it the nest's bed, which must stay the loaded one.
|
|
nest.SetSupportBed(source?.SupportBed);
|
|
|
|
foreach (var drawing in AllDrawings())
|
|
nest.Drawings.Add(drawing);
|
|
|
|
foreach (var plate in AllPlates())
|
|
nest.Plates.Add(plate);
|
|
|
|
return nest;
|
|
}
|
|
|
|
public List<Drawing> AllDrawings()
|
|
{
|
|
var all = new List<Drawing>();
|
|
if (Nest != null)
|
|
all.AddRange(Nest.Drawings);
|
|
all.AddRange(Drawings);
|
|
return all;
|
|
}
|
|
}
|
|
}
|