using System;
using System.Linq;
using OpenNest.Collections;
using OpenNest.Data;
using OpenNest.IO;
using OpenNest.Undo;
namespace OpenNest.Desktop;
///
/// The nest window's plate commands, toolkit-neutral: the Plates list's Remove, Duplicate and
/// Move, Nest > 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
/// (the window's BeginChange). The plate manager keeps the
/// trailing empty plate inside the step and says which plate is current afterwards.
///
public static class PlateCommands
{
///
/// 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.
///
public static void RemoveCurrentPlate(PlateManager plates, Func beginChange)
{
ArgumentNullException.ThrowIfNull(plates);
ArgumentNullException.ThrowIfNull(beginChange);
using var change = beginChange("Remove Plate");
if (plates.Count >= 2)
change.MarkChanged();
plates.RemoveCurrent();
change.Complete();
}
///
/// Whether would remove a plate: an empty one before the
/// trailing empty plate, which comes back as the new-plate sentinel.
///
public static bool HasEmptyPlatesToRemove(Nest nest)
{
ArgumentNullException.ThrowIfNull(nest);
return nest.Plates.Count >= 2 && nest.Plates.SkipLast(1).Any(plate => plate.Parts.Count == 0);
}
/// Nest > Remove Empty Plates, as one "Remove Empty Plates" step.
public static void RemoveEmptyPlates(Nest nest, Func beginChange)
{
ArgumentNullException.ThrowIfNull(nest);
ArgumentNullException.ThrowIfNull(beginChange);
using var change = beginChange("Remove Empty Plates");
if (HasEmptyPlatesToRemove(nest))
change.MarkChanged();
nest.Plates.RemoveEmptyPlates();
change.Complete();
}
///
/// Inserts a copy of the plate at so it lands at
/// , 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.
///
/// The copy, or null when the plate cannot be copied there (nothing is recorded).
public static Plate DuplicatePlate(
PlateManager plates, int fromIndex, int toIndex, Func 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;
}
///
/// Moves the plate at to as one
/// "Move Plate" step, keeping it the current plate.
///
/// Whether the order changed; when it would not, nothing is recorded.
public static bool MovePlate(
PlateManager plates, int fromIndex, int toIndex, Func 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;
}
///
/// The plate Alt+Up () or Alt+Down moves the current plate to: one
/// place earlier or later.
///
public static int StepTarget(int currentIndex, bool up) => up ? currentIndex - 1 : currentIndex + 1;
///
/// Plate > Rotate: the plate and its parts turned a quarter turn either way or a half
/// turn, keeping the quadrant, as one "Rotate Plate" step.
///
public static void Rotate(Plate plate, PlateRotation rotation, Func 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();
}
///
/// Plate > Resize to Fit: the plate shrunk to its parts, rounded up to
/// (Tools > Options' auto-size factor), as one
/// "Resize Plate" step.
///
public static void ResizeToFit(Plate plate, double roundingFactor, Func beginChange)
{
ArgumentNullException.ThrowIfNull(plate);
ArgumentNullException.ThrowIfNull(beginChange);
using var change = beginChange("Resize Plate");
change.MarkChanged();
plate.AutoSize(roundingFactor);
change.Complete();
}
///
/// Plate > Set as Default, the nest's part: '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 ().
///
public static void SetAsNestDefault(
Nest nest, PlateManager plates, Plate plate, Func 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();
}
///
/// The defaults new nests start from after Plate > Set as Default (or Tools > 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.
///
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;
}
///
/// Plate > Edit's commit: 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 (), and the drawings'
/// nested quantities are counted again, the plate's quantity being one of their factors.
///
public static void EditPlate(
Nest nest, PlateManager plates, Plate plate, PlateEditValues settings, Func 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();
}
///
/// After Edit Plate wrote : an empty plate's settings become the
/// nest's plate defaults, the trailing empty plate following them while untouched.
///
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));
}
}
/// The turns of Plate > Rotate.
public enum PlateRotation
{
/// 90° CW.
Clockwise,
/// 90° CCW.
CounterClockwise,
/// 180°.
HalfTurn,
}