using System;
using OpenNest.Geometry;
namespace OpenNest.Desktop;
///
/// What the Edit Plate dialog edits: quantity, size, part and edge spacing, quadrant and the
/// sheet outline. reads a plate, is when the
/// dialog's Done button is enabled and writes the plate as Done does.
///
public sealed record PlateEditValues
{
/// The range of the dialog's quantity and spacing boxes (the WinForms box defaults).
public const double BoxMinimum = 0;
public const double BoxMaximum = 100;
/// Decimals the spacing boxes show.
public const int SpacingDecimals = 4;
public Size Size { get; init; }
public int Quantity { get; init; } = 1;
public double PartSpacing { get; init; }
public double LeftSpacing { get; init; }
public double TopSpacing { get; init; }
public double RightSpacing { get; init; }
public double BottomSpacing { get; init; }
/// The plate origin's quadrant, 1 to 4.
public int Quadrant { get; init; } = 1;
///
/// The sheet outline the plate gets: null for a rectangular sheet. An outlined sheet's size
/// is its outline's bounds ().
///
public SheetOutline Outline { get; init; }
/// The plate's current settings.
public static PlateEditValues FromPlate(Plate plate)
{
ArgumentNullException.ThrowIfNull(plate);
return new PlateEditValues
{
Size = plate.Size,
Quantity = plate.Quantity,
PartSpacing = plate.PartSpacing,
LeftSpacing = plate.EdgeSpacing.Left,
TopSpacing = plate.EdgeSpacing.Top,
RightSpacing = plate.EdgeSpacing.Right,
BottomSpacing = plate.EdgeSpacing.Bottom,
Quadrant = plate.Quadrant,
Outline = plate.Outline,
};
}
///
/// These settings with (null clears it, keeping the size); an
/// outline sets the size to its bounds.
///
public PlateEditValues WithOutline(SheetOutline outline) =>
outline == null
? this with { Outline = null }
: this with { Outline = outline, Size = outline.Size };
///
/// Whether Done applies: the edge spacings leave room on the sheet both ways.
///
public bool CanApply =>
LeftSpacing + RightSpacing < Size.Length && TopSpacing + BottomSpacing < Size.Width;
///
/// Whether Done applies with typed in the size box: it must
/// parse as a size ("width x length"), and then holds for it.
///
public bool CanApplyWithSize(string sizeText, out Size size) =>
Size.TryParse(sizeText, out size) && (this with { Size = size }).CanApply;
/// Writes the settings to , as the dialog's Done does.
public void ApplyTo(Plate plate)
{
ArgumentNullException.ThrowIfNull(plate);
// The outline carries its own size; setting the size would drop it.
if (Outline != null)
plate.Outline = Outline;
else
{
plate.Size = Size;
plate.Outline = null;
}
plate.EdgeSpacing.Left = LeftSpacing;
plate.EdgeSpacing.Top = TopSpacing;
plate.EdgeSpacing.Right = RightSpacing;
plate.EdgeSpacing.Bottom = BottomSpacing;
plate.PartSpacing = PartSpacing;
plate.Quantity = Quantity;
plate.Quadrant = Quadrant;
}
/// The dialog's outline line: "Rectangular sheet" or its vertices and obstacles.
public static string OutlineText(SheetOutline outline) =>
outline == null
? "Rectangular sheet"
: $"{outline.Points.Count} vertices"
+ (outline.Obstacles.Count == 0 ? "" : $", {outline.Obstacles.Count} obstacle(s)");
/// The step of the spacing boxes: a quarter inch, or one millimeter.
public static double SpacingIncrement(Units units) => units == Units.Inches ? 0.25 : 1;
}