mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-04 07:19:24 -05:00
Job numbers were going into Customer ("GM / 4980-88R"), which breaks
sorting, searching and grouping by customer. A nest now has its own
Job # field: one or several jobs separated by commas, normalized to
"a, b". It is saved in nest.json and the server (schema v21), edited
beside Customer in Edit Nest (WinForms and Avalonia), shown as a
sortable, searchable column in the Open list, and printed on the nest
report and in MCP output.
Deploy the server before the desktop: the open overlay takes the
server's job number, and an older server has none.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
209 lines
8.5 KiB
C#
209 lines
8.5 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 and its name,
|
|
/// material and grade, customer and job number, 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; }
|
|
|
|
/// <summary>The shop's name for the thickness, such as "10 ga"; blank when it has none.</summary>
|
|
public string ThicknessName { get; init; } = "";
|
|
|
|
public string MaterialName { get; init; } = "";
|
|
|
|
public string MaterialGrade { get; init; } = "";
|
|
|
|
public string Customer { get; init; } = "";
|
|
|
|
/// <summary>The job number, or several separated by commas; blank when the nest has none.</summary>
|
|
public string JobNumber { 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,
|
|
JobNumber = nest.JobNumber ?? "",
|
|
DateCreated = nest.DateCreated,
|
|
DateLastModified = nest.DateLastModified,
|
|
Thickness = nest.Thickness,
|
|
ThicknessName = nest.ThicknessName ?? "",
|
|
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.JobNumber = JobNumber;
|
|
nest.DateCreated = DateCreated;
|
|
nest.DateLastModified = DateLastModified;
|
|
nest.Thickness = Thickness;
|
|
nest.ThicknessName = ThicknessName;
|
|
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.JobNumber, nest.Thickness, nest.ThicknessName, 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 ?? "";
|
|
}
|
|
}
|