diff --git a/OpenNest.Mcp/NestSession.cs b/OpenNest.Mcp/NestSession.cs index 5332c8bf..f7d62bd0 100644 --- a/OpenNest.Mcp/NestSession.cs +++ b/OpenNest.Mcp/NestSession.cs @@ -154,6 +154,47 @@ namespace OpenNest.Mcp return all; } + /// + /// 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. + /// + 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 AllDrawings() { var all = new List(); diff --git a/OpenNest.Mcp/Tools/InputTools.cs b/OpenNest.Mcp/Tools/InputTools.cs index 0e45f511..c061bcb6 100644 --- a/OpenNest.Mcp/Tools/InputTools.cs +++ b/OpenNest.Mcp/Tools/InputTools.cs @@ -70,30 +70,7 @@ namespace OpenNest.Mcp.Tools [Description("Name for the nest (optional)")] string name = null ) { - var source = _session.Nest; - var nest = new Nest - { - Name = name ?? (string.IsNullOrWhiteSpace(source?.Name) ? Path.GetFileNameWithoutExtension(path) : source.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, - }; - - foreach (var drawing in _session.AllDrawings()) - nest.Drawings.Add(drawing); - - foreach (var plate in _session.AllPlates()) - nest.Plates.Add(plate); + var nest = _session.Snapshot(name ?? DefaultName(path)); if (nest.Drawings.Count == 0) return "Error: no drawings in session to save"; @@ -109,6 +86,9 @@ namespace OpenNest.Mcp.Tools return $"Saved nest to {path}\n Drawings: {nest.Drawings.Count}\n Plates: {nest.Plates.Count}"; } + private string DefaultName(string path) => + string.IsNullOrWhiteSpace(_session.Nest?.Name) ? Path.GetFileNameWithoutExtension(path) : _session.Nest.Name; + [McpServerTool(Name = "import_dxf")] [Description("Import a DXF, DWG or SVG file as a new drawing. Returns drawing name and bounding box.")] public string ImportDxf(