mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-02 05:21:42 -05:00
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>
199 lines
8.0 KiB
C#
199 lines
8.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using OpenNest.Data;
|
|
using OpenNest.Geometry;
|
|
|
|
namespace OpenNest.Desktop;
|
|
|
|
/// <summary>
|
|
/// What Nest > Edit (the Edit Nest dialog) edits: the nest's name, dates, thickness,
|
|
/// material and grade, customer, status, author, units, notes and its plate defaults (size,
|
|
/// part and edge spacing, quadrant). <see cref="FromNest"/> reads a nest, <see cref="CanApply"/>
|
|
/// is when the dialog's Done button is enabled and <see cref="ApplyTo"/> writes the nest as
|
|
/// Done does.
|
|
/// </summary>
|
|
public sealed record NestInfoValues
|
|
{
|
|
/// <summary>The range of the dialog's spacing boxes (the WinForms box limits).</summary>
|
|
public const double SpacingMinimum = 0;
|
|
|
|
public const double SpacingMaximum = 999999;
|
|
|
|
/// <summary>Decimals the spacing boxes show.</summary>
|
|
public const int SpacingDecimals = 4;
|
|
|
|
/// <summary>The step of the spacing boxes, in either unit (the WinForms boxes' 0.25).</summary>
|
|
public const double SpacingIncrement = 0.25;
|
|
|
|
public string Name { get; init; } = "";
|
|
|
|
public DateTime DateCreated { get; init; }
|
|
|
|
public DateTime DateLastModified { get; init; }
|
|
|
|
/// <summary>The thickness in <see cref="Units"/>.</summary>
|
|
public double Thickness { get; init; }
|
|
|
|
public string MaterialName { get; init; } = "";
|
|
|
|
public string MaterialGrade { get; init; } = "";
|
|
|
|
public string Customer { get; init; } = "";
|
|
|
|
public NestStatus Status { get; init; } = NestStatus.Quote;
|
|
|
|
public string MadeBy { get; init; } = "";
|
|
|
|
public Units Units { get; init; } = Units.Inches;
|
|
|
|
public string Notes { get; init; } = "";
|
|
|
|
/// <summary>The default plate size as typed ("width x length").</summary>
|
|
public string SizeText { get; init; } = "";
|
|
|
|
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; }
|
|
|
|
/// <summary>The default plate origin's quadrant, 1 to 4.</summary>
|
|
public int Quadrant { get; init; } = 1;
|
|
|
|
/// <summary>The nest's current info, as the dialog opens on it.</summary>
|
|
public static NestInfoValues FromNest(Nest nest)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(nest);
|
|
var defaults = nest.PlateDefaults;
|
|
return new NestInfoValues
|
|
{
|
|
Name = nest.Name,
|
|
Notes = nest.Notes,
|
|
Customer = nest.Customer,
|
|
DateCreated = nest.DateCreated,
|
|
DateLastModified = nest.DateLastModified,
|
|
Thickness = nest.Thickness,
|
|
MaterialName = nest.Material?.Name ?? "",
|
|
MaterialGrade = nest.Material?.Grade ?? "",
|
|
Status = nest.Status,
|
|
// Shown as stored: a new nest got its author when it was made, and viewing an
|
|
// existing nest's info must not sign it.
|
|
MadeBy = nest.MadeBy ?? "",
|
|
SizeText = defaults.Size.ToString(),
|
|
PartSpacing = defaults.PartSpacing,
|
|
LeftSpacing = defaults.EdgeSpacing.Left,
|
|
TopSpacing = defaults.EdgeSpacing.Top,
|
|
RightSpacing = defaults.EdgeSpacing.Right,
|
|
BottomSpacing = defaults.EdgeSpacing.Bottom,
|
|
Quadrant = defaults.Quadrant,
|
|
Units = nest.Units,
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Whether Done applies: a name, a thickness that reads (<paramref name="thicknessValid"/>),
|
|
/// a size that parses and edge spacings that leave room on the plate both ways.
|
|
/// </summary>
|
|
public bool CanApply(bool thicknessValid)
|
|
{
|
|
if (Name.Trim().Length == 0 || !thicknessValid)
|
|
return false;
|
|
if (!Size.TryParse(SizeText, out var size))
|
|
return false;
|
|
return LeftSpacing + RightSpacing < size.Length && TopSpacing + BottomSpacing < size.Width;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Writes the values to <paramref name="nest"/>, as the dialog's Done does. The material
|
|
/// keeps its catalog details when its name and grade are unchanged
|
|
/// (<see cref="NestMaterialSelection.Apply"/>). The size must parse (<see cref="CanApply"/>).
|
|
/// </summary>
|
|
public void ApplyTo(Nest nest)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(nest);
|
|
nest.Name = Name.Trim();
|
|
nest.Units = Units;
|
|
nest.Notes = Notes;
|
|
nest.Customer = Customer;
|
|
nest.DateCreated = DateCreated;
|
|
nest.DateLastModified = DateLastModified;
|
|
nest.Thickness = Thickness;
|
|
nest.Material = NestMaterialSelection.Apply(nest.Material, MaterialName, MaterialGrade);
|
|
nest.Status = Status;
|
|
nest.MadeBy = MadeBy;
|
|
nest.PlateDefaults.Size = Size.Parse(SizeText);
|
|
nest.PlateDefaults.PartSpacing = PartSpacing;
|
|
nest.PlateDefaults.EdgeSpacing = new Spacing(LeftSpacing, BottomSpacing, RightSpacing, TopSpacing);
|
|
nest.PlateDefaults.Quadrant = Quadrant;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The Made By to remember for later new nests (see <see cref="NewNestSource.DefaultMadeBy"/>)
|
|
/// when the user changed the shown <paramref name="shown"/> to <paramref name="typed"/>, trimmed
|
|
/// (blank falls back to the user name again); null when it was left as shown.
|
|
/// </summary>
|
|
public static string MadeByToRemember(string shown, string typed)
|
|
{
|
|
var trimmed = (typed ?? "").Trim();
|
|
return trimmed == (shown ?? "").Trim() ? null : trimmed;
|
|
}
|
|
|
|
/// <summary>
|
|
/// What Nest Info edits that undo puts back: everything but the identity fields (name,
|
|
/// status, author and dates), so an edit of only those records nothing.
|
|
/// </summary>
|
|
public static string UndoableKey(Nest nest)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(nest);
|
|
var defaults = nest.PlateDefaults;
|
|
return string.Join("|", nest.Units, nest.Notes, nest.Customer, nest.Thickness, nest.Material?.Name,
|
|
nest.Material?.Grade, defaults.Size, defaults.PartSpacing, defaults.EdgeSpacing.Left,
|
|
defaults.EdgeSpacing.Bottom, defaults.EdgeSpacing.Right, defaults.EdgeSpacing.Top, defaults.Quadrant);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The material list: <paramref name="listed"/> (the posts' materials and any already
|
|
/// listed) and the catalog's names, one per name ignoring case and spacing, sorted.
|
|
/// </summary>
|
|
public static IReadOnlyList<string> MaterialNames(IEnumerable<string> listed, IEnumerable<MaterialConfig> catalog) =>
|
|
(listed ?? Enumerable.Empty<string>())
|
|
.Concat((catalog ?? Enumerable.Empty<MaterialConfig>()).Select(m => m.Name))
|
|
.Where(name => !string.IsNullOrWhiteSpace(name))
|
|
.GroupBy(SharedListNames.Key)
|
|
.Select(group => group.First())
|
|
.OrderBy(name => name, StringComparer.OrdinalIgnoreCase)
|
|
.ToList();
|
|
|
|
/// <summary>The grades on file in <paramref name="catalog"/> for <paramref name="material"/>, sorted.</summary>
|
|
public static IReadOnlyList<string> Grades(IEnumerable<MaterialConfig> catalog, string material)
|
|
{
|
|
var key = SharedListNames.Key(material);
|
|
return (catalog ?? Enumerable.Empty<MaterialConfig>())
|
|
.Where(m => SharedListNames.Key(m.Name) == key && !string.IsNullOrWhiteSpace(m.Grade))
|
|
.Select(m => m.Grade)
|
|
.GroupBy(SharedListNames.Key)
|
|
.Select(group => group.First())
|
|
.OrderBy(grade => grade, StringComparer.OrdinalIgnoreCase)
|
|
.ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// The grade kept when the material's key changes from <paramref name="previousKey"/> to
|
|
/// that of <paramref name="material"/>: a grade belongs to its material, so a new material
|
|
/// clears a grade it does not have.
|
|
/// </summary>
|
|
public static string GradeAfterMaterialChange(string previousKey, string material, string grade,
|
|
IReadOnlyList<string> grades)
|
|
{
|
|
var key = SharedListNames.Key(material);
|
|
if (key != previousKey && !grades.Any(g => SharedListNames.Key(g) == SharedListNames.Key(grade)))
|
|
return "";
|
|
return grade ?? "";
|
|
}
|
|
}
|