Files
OpenNest/OpenNest.Desktop/PlateCommands.cs
T
ajandClaude Opus 5.5 4e831ab6f8 feat(desktop): share the plate list and plate commands
PlateCommands ports the WinForms nest window's plate commands, each one
undo step under its WinForms label: Remove Plate, Remove Empty Plates,
Duplicate Plate (the copy current, with its own cutting parameters),
Move Plate and its Alt+Up/Alt+Down target, Rotate Plate (90 degrees
either way or 180), Resize Plate to its parts by the auto-size factor,
Set Nest Default Plate, and Edit Plate's commit, where an empty plate's
new settings become the nest's defaults and the nested quantities are
counted again. NewNestDefaults is what Set as Default saves for new
nests: the plate, the saved common-line usage and the nest's machine.

PlateEditValues is what Edit Plate edits (quantity, size, part and edge
spacing, quadrant, outline), when Done applies and how it writes the
plate; SheetOutlineImport reads an outline from a CAD file, and SvgScale
holds the scale prompt for an SVG that does not state its size.
PlateListRow is the Plates list's row text and its drag drop slots;
QuadrantPick the quadrant picker's hit test and cells.

The WinForms nest window, plate list, Edit Plate dialog, quadrant
picker, SVG scale prompt and Save Current as Defaults delegate to them
with unchanged behavior.

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

233 lines
9.5 KiB
C#

using System;
using System.Linq;
using OpenNest.Collections;
using OpenNest.Data;
using OpenNest.IO;
using OpenNest.Undo;
namespace OpenNest.Desktop;
/// <summary>
/// The nest window's plate commands, toolkit-neutral: the Plates list's Remove, Duplicate and
/// Move, Nest &gt; Remove Empty Plates and the Plate menu's Set as Default, Rotate, Resize to
/// Fit and Edit. Each one is one undo step under the WinForms label, opened with
/// <paramref name="beginChange"/> (the window's <c>BeginChange</c>). The plate manager keeps the
/// trailing empty plate inside the step and says which plate is current afterwards.
/// </summary>
public static class PlateCommands
{
/// <summary>
/// Removes the current plate as one "Remove Plate" step. With fewer than two plates nothing
/// is removed and nothing is recorded; the empty plate removed from the end comes back.
/// </summary>
public static void RemoveCurrentPlate(PlateManager plates, Func<string, ChangeScope> beginChange)
{
ArgumentNullException.ThrowIfNull(plates);
ArgumentNullException.ThrowIfNull(beginChange);
using var change = beginChange("Remove Plate");
if (plates.Count >= 2)
change.MarkChanged();
plates.RemoveCurrent();
change.Complete();
}
/// <summary>
/// Whether <see cref="RemoveEmptyPlates"/> would remove a plate: an empty one before the
/// trailing empty plate, which comes back as the new-plate sentinel.
/// </summary>
public static bool HasEmptyPlatesToRemove(Nest nest)
{
ArgumentNullException.ThrowIfNull(nest);
return nest.Plates.Count >= 2 && nest.Plates.SkipLast(1).Any(plate => plate.Parts.Count == 0);
}
/// <summary>Nest &gt; Remove Empty Plates, as one "Remove Empty Plates" step.</summary>
public static void RemoveEmptyPlates(Nest nest, Func<string, ChangeScope> beginChange)
{
ArgumentNullException.ThrowIfNull(nest);
ArgumentNullException.ThrowIfNull(beginChange);
using var change = beginChange("Remove Empty Plates");
if (HasEmptyPlatesToRemove(nest))
change.MarkChanged();
nest.Plates.RemoveEmptyPlates();
change.Complete();
}
/// <summary>
/// Inserts a copy of the plate at <paramref name="fromIndex"/> so it lands at
/// <paramref name="toIndex"/>, as one "Duplicate Plate" step; the copy becomes the current
/// plate and gets its own cutting parameters, so editing one sheet's leaves the other's alone.
/// </summary>
/// <returns>The copy, or null when the plate cannot be copied there (nothing is recorded).</returns>
public static Plate DuplicatePlate(
PlateManager plates, int fromIndex, int toIndex, Func<string, ChangeScope> beginChange)
{
ArgumentNullException.ThrowIfNull(plates);
ArgumentNullException.ThrowIfNull(beginChange);
if (!plates.CanDuplicatePlate(fromIndex, toIndex))
return null;
using var change = beginChange("Duplicate Plate");
change.MarkChanged();
var copy = plates.DuplicatePlate(fromIndex, toIndex);
if (copy.CuttingParameters != null)
copy.CuttingParameters = CuttingParametersSerializer.FromDto(
CuttingParametersSerializer.ToDto(copy.CuttingParameters));
change.Complete();
return copy;
}
/// <summary>
/// Moves the plate at <paramref name="fromIndex"/> to <paramref name="toIndex"/> as one
/// "Move Plate" step, keeping it the current plate.
/// </summary>
/// <returns>Whether the order changed; when it would not, nothing is recorded.</returns>
public static bool MovePlate(
PlateManager plates, int fromIndex, int toIndex, Func<string, ChangeScope> beginChange)
{
ArgumentNullException.ThrowIfNull(plates);
ArgumentNullException.ThrowIfNull(beginChange);
if (!plates.CanMovePlate(fromIndex, toIndex))
return false;
using var change = beginChange("Move Plate");
change.MarkChanged();
plates.MovePlate(fromIndex, toIndex);
change.Complete();
return true;
}
/// <summary>
/// The plate Alt+Up (<paramref name="up"/>) or Alt+Down moves the current plate to: one
/// place earlier or later.
/// </summary>
public static int StepTarget(int currentIndex, bool up) => up ? currentIndex - 1 : currentIndex + 1;
/// <summary>
/// Plate &gt; Rotate: the plate and its parts turned a quarter turn either way or a half
/// turn, keeping the quadrant, as one "Rotate Plate" step.
/// </summary>
public static void Rotate(Plate plate, PlateRotation rotation, Func<string, ChangeScope> beginChange)
{
ArgumentNullException.ThrowIfNull(plate);
ArgumentNullException.ThrowIfNull(beginChange);
using var change = beginChange("Rotate Plate");
change.MarkChanged();
switch (rotation)
{
case PlateRotation.Clockwise:
plate.Rotate90(RotationType.CW);
break;
case PlateRotation.CounterClockwise:
plate.Rotate90(RotationType.CCW);
break;
case PlateRotation.HalfTurn:
plate.Rotate180();
break;
default:
throw new ArgumentOutOfRangeException(nameof(rotation));
}
change.Complete();
}
/// <summary>
/// Plate &gt; Resize to Fit: the plate shrunk to its parts, rounded up to
/// <paramref name="roundingFactor"/> (Tools &gt; Options' auto-size factor), as one
/// "Resize Plate" step.
/// </summary>
public static void ResizeToFit(Plate plate, double roundingFactor, Func<string, ChangeScope> beginChange)
{
ArgumentNullException.ThrowIfNull(plate);
ArgumentNullException.ThrowIfNull(beginChange);
using var change = beginChange("Resize Plate");
change.MarkChanged();
plate.AutoSize(roundingFactor);
change.Complete();
}
/// <summary>
/// Plate &gt; Set as Default, the nest's part: <paramref name="plate"/>'s size, quadrant and
/// spacing become the nest's plate defaults, saved with the nest, as one "Set Nest Default
/// Plate" step. The trailing empty plate follows them while untouched. The front end then
/// saves the plate as the defaults new nests start from (<see cref="NewNestDefaults"/>).
/// </summary>
public static void SetAsNestDefault(
Nest nest, PlateManager plates, Plate plate, Func<string, ChangeScope> beginChange)
{
ArgumentNullException.ThrowIfNull(nest);
ArgumentNullException.ThrowIfNull(plates);
ArgumentNullException.ThrowIfNull(plate);
ArgumentNullException.ThrowIfNull(beginChange);
using var change = beginChange("Set Nest Default Plate");
change.MarkChanged();
plates.ChangePlateDefaults(() => nest.PlateDefaults.SetFromExisting(plate));
change.Complete();
}
/// <summary>
/// The defaults new nests start from after Plate &gt; Set as Default (or Tools &gt; Save
/// Current as Defaults): the plate's size, quadrant and spacing in the nest's units, the
/// saved common-line usage (a plate holds none), and the nest's machine, or the saved choice
/// for a nest without one.
/// </summary>
public static NestDefaults NewNestDefaults(Nest nest, Plate plate, NestDefaults saved)
{
ArgumentNullException.ThrowIfNull(nest);
ArgumentNullException.ThrowIfNull(plate);
ArgumentNullException.ThrowIfNull(saved);
var defaults = NestDefaults.FromPlate(nest.Units, plate, saved);
defaults.MachineId = nest.Machine?.Id ?? saved.MachineId;
return defaults;
}
/// <summary>
/// Plate &gt; Edit's commit: <paramref name="settings"/> written to the plate as one "Edit
/// Plate" step (null, a cancelled dialog, records nothing). An empty plate's new settings
/// become the nest's plate defaults (<see cref="FollowEditedPlate"/>), and the drawings'
/// nested quantities are counted again, the plate's quantity being one of their factors.
/// </summary>
public static void EditPlate(
Nest nest, PlateManager plates, Plate plate, PlateEditValues settings, Func<string, ChangeScope> beginChange)
{
ArgumentNullException.ThrowIfNull(nest);
ArgumentNullException.ThrowIfNull(plates);
ArgumentNullException.ThrowIfNull(plate);
ArgumentNullException.ThrowIfNull(beginChange);
using var change = beginChange("Edit Plate");
if (settings != null)
{
change.MarkChanged();
settings.ApplyTo(plate);
FollowEditedPlate(nest, plates, plate);
}
nest.UpdateDrawingQuantities();
change.Complete();
}
/// <summary>
/// After Edit Plate wrote <paramref name="plate"/>: an empty plate's settings become the
/// nest's plate defaults, the trailing empty plate following them while untouched.
/// </summary>
public static void FollowEditedPlate(Nest nest, PlateManager plates, Plate plate)
{
ArgumentNullException.ThrowIfNull(nest);
ArgumentNullException.ThrowIfNull(plates);
ArgumentNullException.ThrowIfNull(plate);
if (plate.Parts.Count == 0)
plates.ChangePlateDefaults(() => nest.PlateDefaults.SetFromExisting(plate));
}
}
/// <summary>The turns of Plate &gt; Rotate.</summary>
public enum PlateRotation
{
/// <summary>90° CW.</summary>
Clockwise,
/// <summary>90° CCW.</summary>
CounterClockwise,
/// <summary>180°.</summary>
HalfTurn,
}