mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-23 00:57:41 -05:00
fix(reports): label packed thin strips and color parts by drawing
N26-2CN's 13 x 1 in bars are 9.6 pt tall at overview scale, just under the 10 pt ID, so none passed the in-part test. Two drawings share rows, so each drawing's group rectangle took in the other's bars and was refused, and leaders reached only the edge bars: most of the sheet had no ID. A strip that thin now takes its ID at its pole, overhanging its own outline by at most the clearance and touching nothing else. As a last resort for parts no ID can reach, each drawing is filled in a light palette color on the plate diagram and its card thumbnail, and its ID cell in the parts table carries the same color as the key. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7d9113f94c
commit
f7aa7f82dd
4 files changed
+130
-9
No files matched your search
@@ -30,6 +30,26 @@ public static class NestPdfWriter
|
||||
private const double OverviewHeight = 265;
|
||||
|
||||
private static readonly XColor Fill = XColor.FromArgb(222, 222, 222);
|
||||
|
||||
// Each drawing's fill on the plate diagram, its card thumbnail and its parts-table ID cell, so
|
||||
// color identifies a part the ID cannot label. Light tints (ColorBrewer Set3, gray moved out
|
||||
// since it is the unknown fill) keep black outlines and IDs legible and print in grayscale;
|
||||
// drawings past the palette's end reuse it, the ID still telling them apart.
|
||||
private static readonly XColor[] Palette =
|
||||
[
|
||||
XColor.FromArgb(141, 211, 199), XColor.FromArgb(255, 255, 179), XColor.FromArgb(190, 186, 218),
|
||||
XColor.FromArgb(251, 128, 114), XColor.FromArgb(128, 177, 211), XColor.FromArgb(253, 180, 98),
|
||||
XColor.FromArgb(179, 222, 105), XColor.FromArgb(252, 205, 229), XColor.FromArgb(188, 128, 189),
|
||||
XColor.FromArgb(204, 235, 197), XColor.FromArgb(255, 237, 111), XColor.FromArgb(166, 206, 227),
|
||||
];
|
||||
|
||||
/// <summary>Each report ID's palette color, assigned in drawing (ID) order.</summary>
|
||||
internal static IReadOnlyDictionary<string, XColor> DrawingColors(NestReportSnapshot snapshot) =>
|
||||
snapshot.Drawings.Select((drawing, index) => (drawing.Id, Color: Palette[index % Palette.Length]))
|
||||
.ToDictionary(entry => entry.Id, entry => entry.Color, StringComparer.Ordinal);
|
||||
|
||||
private static XColor ColorOf(IReadOnlyDictionary<string, XColor> colors, string id) =>
|
||||
colors.TryGetValue(id, out var color) ? color : Fill;
|
||||
private static readonly CultureInfo Invariant = CultureInfo.InvariantCulture;
|
||||
|
||||
// PDFsharp/MigraDoc layout and font state is process-wide and not safe for concurrent
|
||||
@@ -279,7 +299,8 @@ public static class NestPdfWriter
|
||||
var picture = cell.AddParagraph();
|
||||
picture.Format.Alignment = ParagraphAlignment.Center;
|
||||
picture.Format.SpaceBefore = picture.Format.SpaceAfter = Unit.FromPoint(2);
|
||||
var image = picture.AddImage(Thumbnail(drawing.Geometry, inner, CardThumbnailHeight));
|
||||
var image = picture.AddImage(Thumbnail(drawing.Geometry, inner, CardThumbnailHeight,
|
||||
ColorOf(DrawingColors(snapshot), drawing.Id)));
|
||||
image.Width = Unit.FromPoint(inner);
|
||||
image.Height = Unit.FromPoint(CardThumbnailHeight);
|
||||
|
||||
@@ -330,6 +351,7 @@ public static class NestPdfWriter
|
||||
private static void AddPlate(Document document, NestReportSnapshot snapshot, ReportPlate plate,
|
||||
DiagramView plan, List<(Table, Action<XGraphics, XRect>[])> areas)
|
||||
{
|
||||
var colors = DrawingColors(snapshot);
|
||||
var heading = $"Plate {plate.Number} of {snapshot.Plates.Length}";
|
||||
var header = new List<string> { $"Nest report: {snapshot.Name}", heading, SheetSize(snapshot, plate) };
|
||||
// Unset material/thickness are stated in the page body, not repeated in every header.
|
||||
@@ -355,7 +377,7 @@ public static class NestPdfWriter
|
||||
Duration(cutTime * plate.Copies) + (plate.Copies == 1 ? " (1 plate)" : $" ({plate.Copies} plates)"));
|
||||
|
||||
var overview = AddReservedArea(section, plan.Size.Height);
|
||||
areas.Add((overview, [(gfx, _) => DrawRulers(gfx, plate, plan, snapshot.Units), (gfx, _) => DrawView(gfx, plate, plan)]));
|
||||
areas.Add((overview, [(gfx, _) => DrawRulers(gfx, plate, plan, snapshot.Units), (gfx, _) => DrawView(gfx, plate, plan, colors)]));
|
||||
|
||||
// Only a removed clamp needs words: it is an action before cutting. Hatched clamps and the
|
||||
// rulers (their unit sits in the corner) explain themselves, and the room goes to parts rows.
|
||||
@@ -377,8 +399,11 @@ public static class NestPdfWriter
|
||||
foreach (var drawing in snapshot.Drawings.Where(drawing => perSheet.ContainsKey(drawing.Id)))
|
||||
{
|
||||
var count = perSheet[drawing.Id];
|
||||
AddRow(parts, $"Plate {plate.Number}, drawing {drawing.Id}", false, drawing.Id, names2[drawing.Id],
|
||||
var row = AddRow(parts, $"Plate {plate.Number}, drawing {drawing.Id}", false, drawing.Id, names2[drawing.Id],
|
||||
count.ToString(Invariant), checked(count * plate.Copies).ToString(Invariant));
|
||||
// The ID cell carries the part's diagram color: the key when an ID could not be placed.
|
||||
var color = ColorOf(colors, drawing.Id);
|
||||
row.Cells[0].Shading.Color = new Color(color.R, color.G, color.B);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -429,7 +454,8 @@ public static class NestPdfWriter
|
||||
}
|
||||
}
|
||||
|
||||
private static void DrawView(XGraphics gfx, ReportPlate plate, DiagramView view)
|
||||
private static void DrawView(XGraphics gfx, ReportPlate plate, DiagramView view,
|
||||
IReadOnlyDictionary<string, XColor> colors)
|
||||
{
|
||||
var fit = view.Fit;
|
||||
gfx.DrawRectangle(new XPen(XColors.Black, 1), fit.Rect(plate.Bounds));
|
||||
@@ -439,7 +465,7 @@ public static class NestPdfWriter
|
||||
DrawClamp(gfx, fit.Rect(clamp.Bounds));
|
||||
var outline = new XPen(XColors.Black, 0.6);
|
||||
foreach (var part in plate.Parts)
|
||||
DrawGeometry(gfx, part.Geometry, fit, outline);
|
||||
DrawGeometry(gfx, part.Geometry, fit, outline, ColorOf(colors, part.ReportId));
|
||||
var removed = new XPen(XColors.Black, 0.6) { DashPattern = [5, 3] };
|
||||
var cross = new XPen(XColors.Black, 0.4);
|
||||
foreach (var clamp in plate.Clamps.Where(clamp => clamp.Removed))
|
||||
@@ -540,7 +566,7 @@ public static class NestPdfWriter
|
||||
XStringFormats.TopRight);
|
||||
}
|
||||
|
||||
private static string Thumbnail(ReportGeometry geometry, double width, double height)
|
||||
private static string Thumbnail(ReportGeometry geometry, double width, double height, XColor fill)
|
||||
{
|
||||
using var document = new PdfDocument();
|
||||
var page = document.AddPage();
|
||||
@@ -549,7 +575,7 @@ public static class NestPdfWriter
|
||||
using (var gfx = XGraphics.FromPdfPage(page))
|
||||
{
|
||||
var fit = Fit.Create(geometry.Bounds, new XRect(2, 2, width - 4, height - 4));
|
||||
DrawGeometry(gfx, geometry, fit, new XPen(XColors.Black, 0.5));
|
||||
DrawGeometry(gfx, geometry, fit, new XPen(XColors.Black, 0.5), fill);
|
||||
}
|
||||
using var stream = new MemoryStream();
|
||||
document.Save(stream, false);
|
||||
@@ -557,7 +583,7 @@ public static class NestPdfWriter
|
||||
return "base64:" + Convert.ToBase64String(stream.ToArray());
|
||||
}
|
||||
|
||||
private static void DrawGeometry(XGraphics gfx, ReportGeometry geometry, Fit fit, XPen pen)
|
||||
private static void DrawGeometry(XGraphics gfx, ReportGeometry geometry, Fit fit, XPen pen, XColor fill)
|
||||
{
|
||||
if (geometry.StrokeOnly)
|
||||
{
|
||||
@@ -572,7 +598,7 @@ public static class NestPdfWriter
|
||||
path.CloseFigure();
|
||||
}
|
||||
path.FillMode = XFillMode.Alternate; // Even-odd: holes stay unfilled.
|
||||
gfx.DrawPath(pen, new XSolidBrush(Fill), path);
|
||||
gfx.DrawPath(pen, new XSolidBrush(fill), path);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -392,6 +392,29 @@ internal static class NestReportDiagram
|
||||
placed.Add(new DiagramLabel(index, id, found));
|
||||
failed.Remove(index);
|
||||
}
|
||||
|
||||
// A strip a hair thinner than the label (closely packed bars) still takes its ID at the
|
||||
// pole: the label may overhang its own outline by up to the clearance, so its core stays
|
||||
// in the part's material, but it never touches another part, cutoff, clamp or label.
|
||||
foreach (var index in failed.ToList())
|
||||
{
|
||||
var id = plate.Parts[index].ReportId;
|
||||
var size = ReportText.Size(id, font);
|
||||
var width = size.Width + 2 * LabelPadding;
|
||||
var height = size.Height + 2 * LabelPadding;
|
||||
var pole = fit.Point(shapes[index].Pole.X, shapes[index].Pole.Y);
|
||||
var box = new XRect(pole.X - width / 2, pole.Y - height / 2, width, height);
|
||||
var core = box;
|
||||
core.Inflate(-Clearance, -Clearance);
|
||||
var model = ModelBox(fit, box);
|
||||
if (!canvas.Holds(box) || placed.Any(label => label.Box.IntersectsWith(box))
|
||||
|| !shapes[index].InMaterial(Center(fit, box))
|
||||
|| obstacles.Any(shape => shape.Crosses(ModelBox(fit, core)))
|
||||
|| obstacles.Where((_, other) => other != index).Any(shape => shape.Crosses(model)))
|
||||
continue;
|
||||
placed.Add(new DiagramLabel(index, id, box));
|
||||
failed.Remove(index);
|
||||
}
|
||||
return placed;
|
||||
}
|
||||
|
||||
|
||||
@@ -342,6 +342,64 @@ public sealed class NestPdfLayoutTests : IDisposable
|
||||
Assert.InRange(center.Y - (bounds.Bottom + bounds.Top) / 2, -0.5, 0.5);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Plan_PackedStripsAHairThinnerThanTheirIdsAreEachLabeled()
|
||||
{
|
||||
// From N26-2CN: 13 x 1 in bars packed 0.25 in apart on a 24 x 72 sheet are 9.6 pt tall,
|
||||
// just under the 10 pt ID, and two drawings share rows, so neither a same-ID group nor a
|
||||
// leader could reach most of them and the middle of the sheet went unlabeled.
|
||||
var nest = new Nest("Packed strips") { Units = Units.Inches };
|
||||
var a = NestReportTestData.Rectangle("Strip A", 13, 1);
|
||||
var b = NestReportTestData.Rectangle("Strip B", 13, 1);
|
||||
nest.Drawings.Add(a);
|
||||
nest.Drawings.Add(b);
|
||||
var plate = new Plate(24, 72) { Quantity = 1, PartSpacing = 0.25 };
|
||||
for (var row = 0; row < 6; row++)
|
||||
for (var column = 0; column < 5; column++)
|
||||
plate.Parts.Add(new Part((row + (column == 4 ? 1 : 0)) % 2 == 0 ? a : b,
|
||||
new Vector(1 + column * 13.25, 1 + row * 1.25)));
|
||||
nest.Plates.Add(plate);
|
||||
var snapshot = NestReportBuilder.Capture(nest, NestReportTestData.GeneratedAt);
|
||||
|
||||
var plan = NestReportDiagram.Plan(snapshot.Plates[0], 720, 265);
|
||||
|
||||
var parts = snapshot.Plates[0].Parts;
|
||||
Assert.Equal(Enumerable.Range(0, parts.Length), plan.Labels.Select(label => label.Part).Order());
|
||||
Assert.All(plan.Labels, label =>
|
||||
{
|
||||
Assert.Null(label.Anchor);
|
||||
// The ID sits on its own bar, overhanging it by at most the clearance, and touches no other.
|
||||
var own = plan.Fit.Rect(parts[label.Part].Geometry.Bounds);
|
||||
own.Inflate(1.5, 1.5);
|
||||
Assert.True(own.Contains(label.Box));
|
||||
for (var other = 0; other < parts.Length; other++)
|
||||
if (other != label.Part)
|
||||
Assert.False(label.Box.IntersectsWith(plan.Fit.Rect(parts[other].Geometry.Bounds)));
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DrawingColors_AreDistinctUntilThePaletteRepeatsAndFollowIdOrder()
|
||||
{
|
||||
var nest = new Nest("Colors") { Units = Units.Inches };
|
||||
var plate = new Plate(48, 96) { Quantity = 1 };
|
||||
for (var i = 0; i < 14; i++)
|
||||
{
|
||||
var drawing = NestReportTestData.Rectangle($"Part {i:D2}", 2, 2);
|
||||
nest.Drawings.Add(drawing);
|
||||
plate.Parts.Add(new Part(drawing, new Vector(1 + i * 3, 1)));
|
||||
}
|
||||
nest.Plates.Add(plate);
|
||||
var snapshot = NestReportBuilder.Capture(nest, NestReportTestData.GeneratedAt);
|
||||
|
||||
var colors = NestPdfWriter.DrawingColors(snapshot);
|
||||
var ordered = snapshot.Drawings.Select(drawing => colors[drawing.Id]).ToList();
|
||||
|
||||
Assert.Equal(12, ordered.Take(12).Distinct().Count());
|
||||
Assert.Equal(ordered[0], ordered[12]);
|
||||
Assert.Equal(ordered[1], ordered[13]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Plan_PartsTooThinForTheirIdsGetLeaderLabelsBesideThem()
|
||||
{
|
||||
|
||||
@@ -149,6 +149,20 @@ room, nearest the part's center. Open space beside a concave part (inside a C
|
||||
shape's opening) never holds its ID that way, since it reads as belonging to
|
||||
whatever is nearby.
|
||||
|
||||
A strip a hair thinner than its ID (closely packed bars) still takes the ID at its
|
||||
pole: the label may overhang the part's own outline by up to the clearance, so its
|
||||
center stays in the part's material, but it never touches another part, cutoff,
|
||||
clamp or label. Without this, packed rows of two drawings got no ID: each drawing's
|
||||
cluster rectangle took in the other's bars, so it could not be grouped, and leaders
|
||||
could only reach the bars on the edge.
|
||||
|
||||
Each drawing has a color, assigned in ID order from a 12-color light palette
|
||||
(ColorBrewer Set3) that keeps black outlines and IDs legible and still reads in
|
||||
grayscale; drawings past the twelfth reuse it. Its parts are filled with it on the
|
||||
plate diagram and its card thumbnail, and its ID cell in the plate's parts table is
|
||||
shaded with it, so the table is the color key for a part whose ID could not be
|
||||
placed.
|
||||
|
||||
A part still unlabeled after the groups below (a thin strip or a narrow band with
|
||||
no hole to hold its ID) gets its ID in the nearest clear space outside it, with a
|
||||
thin leader to a dot at its pole. The label keeps the usual clearance; the leader
|
||||
|
||||
Reference in new issue
Block a user