mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-28 19:51:33 -05:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
90a1a087f0
commit
7ea21f3afe
6 files changed
+149
-9
No files matched your search
@@ -0,0 +1,42 @@
|
||||
namespace OpenNest.Data;
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public static class MaterialDensityLookup
|
||||
{
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public static double Resolve(
|
||||
string? name,
|
||||
string? grade,
|
||||
MachineConfig? machine,
|
||||
IEnumerable<MaterialConfig>? catalog
|
||||
)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
return 0;
|
||||
|
||||
var machineMaterials = machine?.Materials ?? Enumerable.Empty<MaterialConfig>();
|
||||
var allMaterials = machineMaterials.Concat(catalog ?? Enumerable.Empty<MaterialConfig>()).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;
|
||||
}
|
||||
}
|
||||
@@ -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 <paramref name="cutParameters"/>,
|
||||
/// each plate also gets an estimated cut time from <see cref="CutSimulation"/>.
|
||||
/// <paramref name="catalogDensity"/> is the nest material's density from the machine material
|
||||
/// defaults (lb/in^3), used when the nest material carries none of its own.
|
||||
/// </summary>
|
||||
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<ReportDrawing>();
|
||||
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
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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;
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
|
||||
Reference in new issue
Block a user