Files
OpenNest/OpenNest.Data/ThicknessNames.cs
T
ajandClaude Opus 5.5 6b161d7a8b feat(data): store a nest's thickness name and show it
Nests showed only the decimal (0.135), while the shop thinks in gauges.
A nest now keeps a ThicknessName in its file, record and the server
(schema v20), set from Nest Information's new box, which suggests the
machine's name or the nearest standard gauge. The Open list, status bar
and reports show "10 ga (0.135)"; nests saved without a name get one
looked up for display only.

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

69 lines
3.1 KiB
C#

using System.Globalization;
namespace OpenNest.Data;
/// <summary>
/// The shop's names for thicknesses, such as "10 ga" for 0.135, and the "10 ga (0.135)" text
/// that shows them. A nest stores its name (<see cref="Nest.ThicknessName"/>); one without is
/// named here: the machine's name for its material and thickness first, otherwise the nearest
/// standard gauge or plate fraction.
/// </summary>
public static class ThicknessNames
{
// As machine thicknesses match: decimals typed to three places (0.135 for 10 ga, 0.188 for
// 3/16") still name their gauge, and gauges are far further apart.
private const double ToleranceInches = 0.001;
/// <summary>"10 ga (0.135)", or the plain decimal when <paramref name="name"/> is blank.</summary>
public static string Format(string? name, double thickness)
{
var number = thickness.ToString("0.####", CultureInfo.CurrentCulture);
return string.IsNullOrWhiteSpace(name) ? number : $"{name.Trim()} ({number})";
}
/// <summary>The thickness text for <paramref name="nest"/>: its name, or one looked up, then the decimal.</summary>
public static string Format(Nest nest, IEnumerable<MachineConfig?> machines)
{
ArgumentNullException.ThrowIfNull(nest);
var name = string.IsNullOrWhiteSpace(nest.ThicknessName) ? For(machines, nest) : nest.ThicknessName;
return Format(name, nest.Thickness);
}
/// <summary>The thickness text for a saved nest: its stored name, or one looked up, then the decimal.</summary>
public static string Format(NestRecord record, IEnumerable<MachineConfig?> machines)
{
ArgumentNullException.ThrowIfNull(record);
var name = string.IsNullOrWhiteSpace(record.ThicknessName)
? For(machines, record.Material, record.MaterialGrade, record.Thickness, record.Units)
: record.ThicknessName;
return Format(name, record.Thickness);
}
/// <summary>The name for the thickness of <paramref name="nest"/>; blank when nothing names it.</summary>
public static string For(IEnumerable<MachineConfig?> machines, Nest nest)
{
ArgumentNullException.ThrowIfNull(nest);
return For(machines, nest.Material?.Name, nest.Material?.Grade, nest.Thickness, nest.Units);
}
/// <summary>
/// The name for <paramref name="thickness"/> (in <paramref name="units"/>) of
/// <paramref name="material"/>: the first of <paramref name="machines"/> that names it, otherwise
/// the nearest standard gauge or plate fraction; blank when nothing names it.
/// </summary>
public static string For(IEnumerable<MachineConfig?> machines, string? material, string? grade, double thickness, Units units)
{
if (!double.IsFinite(thickness) || thickness <= 0)
return "";
foreach (var machine in machines ?? Enumerable.Empty<MachineConfig?>())
{
var name = MachineCuttingDefaults.Find(machine, material, grade, thickness, units)?.Name;
if (!string.IsNullOrWhiteSpace(name))
return name.Trim();
}
return StandardThicknesses.FindNearest(thickness, units, ToleranceInches)?.Name ?? "";
}
}