Files
OpenNest/OpenNest.Desktop/NestCommands.cs
T
ajandClaude Opus 5.5 6f1d21173d feat(desktop): share nest info, Clear Nest and the nest defaults choices
NestInfoValues is what Nest > Edit edits (name, dates, thickness,
material and grade, customer, status, author, units, notes and the plate
defaults): read from a nest, when Done is enabled, how it writes the
nest, which Made By to remember, the material and grade lists and the
fields undo puts back. NestCommands holds its commit as one "Nest Info"
step (a changed material or thickness takes the machine's spacing and an
untouched trailing sheet follows), the defaults "Use these defaults for
new nests" saves, and Clear Nest as one "Clear Nest" step with its
question and busy message. NestDefaultsChoices holds the Tools > Nest
Defaults dialog's machine order, common-line list and box ranges.

The WinForms Edit Nest dialog, nest window and Nest Defaults dialog
delegate to them with unchanged behavior.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 18:42:20 -05:00

76 lines
3.5 KiB
C#

using System;
using OpenNest.Data;
using OpenNest.Undo;
namespace OpenNest.Desktop;
/// <summary>
/// The Nest menu's whole-nest commands, toolkit-neutral: Edit (Nest Info) and Clear Nest, each
/// one undo step under its WinForms label opened with <paramref name="beginChange"/> (the
/// window's <c>BeginChange</c>), and the defaults Edit's "Use these defaults for new nests" saves.
/// </summary>
public static class NestCommands
{
/// <summary>The Clear Nest question, answered Yes to clear.</summary>
public const string ClearNestQuestion = "Remove all plates and parts from this nest?\nOne empty plate will remain.";
/// <summary>What a refused Clear Nest tells the user (WinForms also waits for a save).</summary>
public const string ClearNestBusyMessage = "Finish or cancel the current nesting, save or plate action first.";
/// <summary>
/// Writes Nest Info's <paramref name="values"/> to <paramref name="nest"/> as one "Nest Info"
/// step: a changed material or thickness takes <paramref name="machine"/>'s spacing (the
/// nest's machine configuration, null when it has none on file), and an untouched trailing
/// sheet follows the new plate defaults. The name, status, author and creation date stay as
/// they are through undo, so an edit of only those records nothing.
/// <paramref name="afterApply"/> runs inside the step, for the view's settings that follow
/// the material, thickness or units (its cut-off defaults).
/// </summary>
/// <returns>Whether the step recorded a change.</returns>
public static bool EditNestInfo(Nest nest, PlateManager plates, NestInfoValues values, MachineConfig machine,
Func<string, ChangeScope> beginChange, Action afterApply = null)
{
ArgumentNullException.ThrowIfNull(nest);
ArgumentNullException.ThrowIfNull(plates);
ArgumentNullException.ThrowIfNull(values);
ArgumentNullException.ThrowIfNull(beginChange);
using var change = beginChange("Nest Info");
var before = NestInfoValues.UndoableKey(nest);
plates.ChangePlateDefaults(() => MachineCuttingDefaults.ApplySpacingAfter(nest, machine, () => values.ApplyTo(nest)));
var changed = NestInfoValues.UndoableKey(nest) != before;
if (changed)
change.MarkChanged();
afterApply?.Invoke();
change.Complete();
return changed;
}
/// <summary>
/// The defaults new nests start from when Nest Info's "Use these defaults for new nests" is
/// checked: the nest's units, plate defaults and common-line usage, and its machine, or the
/// <paramref name="saved"/> choice for a nest without one.
/// </summary>
public static NestDefaults NewNestDefaults(Nest nest, NestDefaults saved)
{
ArgumentNullException.ThrowIfNull(nest);
var defaults = NestDefaults.FromNest(nest);
defaults.MachineId = nest.Machine?.Id ?? saved?.MachineId;
return defaults;
}
/// <summary>
/// Nest &gt; Clear Nest, as one "Clear Nest" step: every plate but the first is removed and
/// the first is emptied. A nest that is already one empty plate records nothing.
/// </summary>
public static void ClearNest(PlateManager plates, Func<string, ChangeScope> beginChange)
{
ArgumentNullException.ThrowIfNull(plates);
ArgumentNullException.ThrowIfNull(beginChange);
using var change = beginChange("Clear Nest");
if (plates.CanClearAll)
change.MarkChanged();
plates.ClearAll();
change.Complete();
}
}