From 7ea21f3afeb7dac1c4408ccb1031c8e9b410bff1 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Wed, 4 Nov 2026 13:51:23 -0500 Subject: [PATCH] fix(reporting): weigh parts with the machine catalog's material density Nest reports showed "-" for every part weight on N26-295 because the nest and its drawings store Stainless Steel 310 with density 0: materials are picked by name and grade, and nothing copies a density onto them. The densities live in the machine material defaults, and for this nest not even on its own machine (CL-980 lists only 304; 310 is on CI FIBER 4020). The desktop now resolves a catalog density before capturing the report: the nest machine's exact name and grade, any machine's exact name and grade, then the same material name. A density stored on the nest still wins, and the drawing's own material stays the last fallback. Co-Authored-By: Claude Opus 5.5 --- OpenNest.Data/MaterialDensityLookup.cs | 42 +++++++++++++ OpenNest.Reporting/NestReportBuilder.cs | 18 ++++-- .../Data/MaterialDensityLookupTests.cs | 59 +++++++++++++++++++ .../Reporting/NestReportBuilderTests.cs | 21 +++++++ OpenNest/Forms/EditNestForm.cs | 10 +++- docs/nest-reports.md | 8 ++- 6 files changed, 149 insertions(+), 9 deletions(-) create mode 100644 OpenNest.Data/MaterialDensityLookup.cs create mode 100644 OpenNest.Tests/Data/MaterialDensityLookupTests.cs diff --git a/OpenNest.Data/MaterialDensityLookup.cs b/OpenNest.Data/MaterialDensityLookup.cs new file mode 100644 index 00000000..cd043d85 --- /dev/null +++ b/OpenNest.Data/MaterialDensityLookup.cs @@ -0,0 +1,42 @@ +namespace OpenNest.Data; + +/// +/// Finds a density (pounds per cubic inch) for a nest material from the machine material +/// catalogs. Nest and drawing materials are usually picked by name and grade only, so they +/// carry no density of their own; the machines' material defaults are where densities live. +/// +public static class MaterialDensityLookup +{ + /// + /// The first positive density, in order: the nest's machine for the exact name and grade, + /// any machine for the exact name and grade, then the nest's machine and any machine for + /// the name alone (grades of one material differ by about a percent). Zero when none. + /// + public static double Resolve( + string? name, + string? grade, + MachineConfig? machine, + IEnumerable? catalog + ) + { + if (string.IsNullOrWhiteSpace(name)) + return 0; + + var machineMaterials = machine?.Materials ?? Enumerable.Empty(); + var allMaterials = machineMaterials.Concat(catalog ?? Enumerable.Empty()).ToList(); + var nameKey = SharedListNames.Key(name); + var sameName = allMaterials + .Where(m => m != null && SharedListNames.Key(m.Name) == nameKey && double.IsFinite(m.Density) && m.Density > 0) + .ToList(); + + if (!string.IsNullOrWhiteSpace(grade)) + { + var gradeKey = SharedListNames.Key(grade); + var exact = sameName.FirstOrDefault(m => SharedListNames.Key(m.Grade) == gradeKey); + if (exact != null) + return exact.Density; + } + + return sameName.FirstOrDefault()?.Density ?? 0; + } +} diff --git a/OpenNest.Reporting/NestReportBuilder.cs b/OpenNest.Reporting/NestReportBuilder.cs index aa311405..a2dcf2e4 100644 --- a/OpenNest.Reporting/NestReportBuilder.cs +++ b/OpenNest.Reporting/NestReportBuilder.cs @@ -11,8 +11,11 @@ public static class NestReportBuilder /// Capture synchronously while the caller keeps the nest stable. No quantity refresh, /// domain cloning, or event-wired temporary plates are used. With , /// each plate also gets an estimated cut time from . + /// is the nest material's density from the machine material + /// defaults (lb/in^3), used when the nest material carries none of its own. /// - public static NestReportSnapshot Capture(Nest nest, DateTimeOffset generatedAt, CutParameters? cutParameters = null) + public static NestReportSnapshot Capture(Nest nest, DateTimeOffset generatedAt, CutParameters? cutParameters = null, + double catalogDensity = 0) { ArgumentNullException.ThrowIfNull(nest); var registry = new DrawingRegistry(); @@ -35,7 +38,7 @@ public static class NestReportBuilder var drawings = ImmutableArray.CreateBuilder(); foreach (var item in registry.Ordered) - drawings.Add(CaptureDrawing(item, nest)); + drawings.Add(CaptureDrawing(item, nest, catalogDensity)); return new NestReportSnapshot(nest.Name ?? "", nest.Customer ?? "", nest.Notes ?? "", nest.Material?.Name ?? "", nest.Material?.Grade ?? "", nest.Thickness, units, generatedAt, @@ -126,7 +129,7 @@ public static class NestReportBuilder return bounds; } - private static ReportDrawing CaptureDrawing(DrawingCount item, Nest nest) + private static ReportDrawing CaptureDrawing(DrawingCount item, Nest nest, double catalogDensity) { var drawing = item.Drawing; var context = $"Drawing {item.Id} ('{drawing.Name}')"; @@ -138,7 +141,7 @@ public static class NestReportBuilder System.Math.Max(checked(required - item.Nested), 0), System.Math.Max(checked(item.Nested - required), 0), item.Plates.ToImmutableArray(), geometry) { - Weight = PartWeight(drawing, nest), + Weight = PartWeight(drawing, nest, catalogDensity), }; } @@ -148,11 +151,14 @@ public static class NestReportBuilder /// /// Net area x nest thickness x density. Density is pounds per cubic inch, as in the /// machine material defaults; millimeter jobs convert the volume and report kilograms. - /// The nest material's density wins; the drawing's own material is the fallback. + /// The nest material's density wins, then the machine catalog's density for that material; + /// the drawing's own material is the last fallback. /// - internal static double? PartWeight(Drawing drawing, Nest nest) + internal static double? PartWeight(Drawing drawing, Nest nest, double catalogDensity = 0) { var density = nest.Material?.Density ?? 0; + if (!(density > 0)) + density = catalogDensity; if (!(density > 0)) density = drawing.Material?.Density ?? 0; var area = drawing.Area; diff --git a/OpenNest.Tests/Data/MaterialDensityLookupTests.cs b/OpenNest.Tests/Data/MaterialDensityLookupTests.cs new file mode 100644 index 00000000..98e56ef4 --- /dev/null +++ b/OpenNest.Tests/Data/MaterialDensityLookupTests.cs @@ -0,0 +1,59 @@ +using OpenNest.Data; + +namespace OpenNest.Tests.Data; + +public class MaterialDensityLookupTests +{ + private static MaterialConfig M(string name, string grade, double density) => + new() { Name = name, Grade = grade, Density = density }; + + private static MachineConfig Machine(params MaterialConfig[] materials) => + new() { Name = "CL-980", Materials = materials.ToList() }; + + [Fact] + public void Resolve_PrefersTheNestMachineExactGrade() + { + var machine = Machine(M("Stainless Steel", "310", 0.287)); + var catalog = new[] { M("Stainless Steel", "310", 0.3) }; + + Assert.Equal(0.287, MaterialDensityLookup.Resolve("Stainless Steel", "310", machine, catalog)); + } + + [Fact] + public void Resolve_ExactGradeOnAnotherMachineBeatsTheSameNameOnTheNestMachine() + { + // N26-295: CL-980 lists only 304; the 310 grade lives on another machine. + var machine = Machine(M("Stainless Steel", "304", 0.289)); + var catalog = new[] { M("Stainless Steel", "304", 0.289), M("stainless steel", " 310 ", 0.287) }; + + Assert.Equal(0.287, MaterialDensityLookup.Resolve("Stainless Steel", "310", machine, catalog)); + } + + [Fact] + public void Resolve_FallsBackToTheMaterialNameWhenNoGradeMatches() + { + var machine = Machine(M("Stainless Steel", "304", 0.289)); + + Assert.Equal(0.289, MaterialDensityLookup.Resolve("Stainless Steel", "17-4", machine, null)); + } + + [Fact] + public void Resolve_SkipsEntriesWithoutADensity() + { + var machine = Machine(M("Stainless Steel", "310", 0)); + var catalog = new[] { M("Stainless Steel", "310", 0.287) }; + + Assert.Equal(0.287, MaterialDensityLookup.Resolve("Stainless Steel", "310", machine, catalog)); + } + + [Theory] + [InlineData(null)] + [InlineData("")] + [InlineData("Brass")] + public void Resolve_IsZeroWhenNothingMatches(string? name) + { + var machine = Machine(M("Stainless Steel", "310", 0.287)); + + Assert.Equal(0, MaterialDensityLookup.Resolve(name, "310", machine, null)); + } +} diff --git a/OpenNest.Tests/Reporting/NestReportBuilderTests.cs b/OpenNest.Tests/Reporting/NestReportBuilderTests.cs index 03849b16..87a7a690 100644 --- a/OpenNest.Tests/Reporting/NestReportBuilderTests.cs +++ b/OpenNest.Tests/Reporting/NestReportBuilderTests.cs @@ -63,6 +63,27 @@ public class NestReportBuilderTests Assert.Equal(expected.Value, weight!.Value, 9); } + [Theory] + [InlineData(0, 0.287, 0.1, 0.0358750)] + [InlineData(0.2836, 0.287, 0.1, 0.03545)] + [InlineData(0, 0, 0.1, 0.0125)] + [InlineData(0, 0, 0, null)] + public void Capture_CatalogDensityStandsInWhenTheNestMaterialHasNone(double nestDensity, double catalogDensity, + double drawingDensity, double? expected) + { + var nest = new Nest("Catalog") { Units = Units.Inches, Thickness = 0.125, Material = new Material("Stainless Steel", "310", nestDensity) }; + var drawing = NestReportTestData.Rectangle("Plate", 1, 1); + drawing.Material = new Material("Stainless Steel", "310", drawingDensity); + nest.Drawings.Add(drawing); + + var weight = NestReportBuilder.Capture(nest, NestReportTestData.GeneratedAt, catalogDensity: catalogDensity).Drawings[0].Weight; + + if (expected == null) + Assert.Null(weight); + else + Assert.Equal(expected.Value, weight!.Value, 9); + } + [Fact] public void Capture_WithCutTimeEstimatesEachPlateLikeCalculateCutTime() { diff --git a/OpenNest/Forms/EditNestForm.cs b/OpenNest/Forms/EditNestForm.cs index 704d6adf..7903ba98 100644 --- a/OpenNest/Forms/EditNestForm.cs +++ b/OpenNest/Forms/EditNestForm.cs @@ -809,10 +809,18 @@ namespace OpenNest.Forms // Reporting must have no accounting side effects: no quantity refresh, no // selection or dirty-state change. Only the detached snapshot reaches the writer. + var material = targets.Nest.Material; + var catalogDensity = Data.MaterialDensityLookup.Resolve( + material?.Name, + material?.Grade, + MachineSettings.FindMachine(targets.Nest.Machine), + MachineSettings.LoadMaterials() + ); var snapshot = NestReportBuilder.Capture( targets.Nest, DateTimeOffset.Now, - SavedCutParameters.Load(targets.Nest.Units) + SavedCutParameters.Load(targets.Nest.Units), + catalogDensity ); NestPdfWriter.Write(snapshot, destination); } diff --git a/docs/nest-reports.md b/docs/nest-reports.md index bb0220ec..4037f9bd 100644 --- a/docs/nest-reports.md +++ b/docs/nest-reports.md @@ -53,8 +53,12 @@ the selected plate, or certify that a layout passed geometry/pre-post checks. geometry. Invalid/missing/nonfinite geometry fails with drawing/plate context. - A drawing's weight is its net area (holes excluded) times the nest thickness times the material density, read as **pounds per cubic inch** as in the machine - material defaults (steel 0.2836). The nest material's density wins; the - drawing's own material is the fallback. Millimeter jobs convert the volume to + material defaults (steel 0.2836). The nest material's density wins. Nest + materials picked by name and grade carry none, so the desktop passes + `Capture(..., catalogDensity)` from `MaterialDensityLookup`: the nest machine's + exact name and grade, any machine's exact name and grade, then the same material + name on the nest machine or any machine. The drawing's own material is the last + fallback. Millimeter jobs convert the volume to cubic inches and report kilograms. With no thickness or no density the weight is unknown, never zero.