feat(reports): grow the plate diagram into a short parts table's spare room

Where the whole parts table fits on the diagram's page, the diagram takes
the room left over and the table sits at the foot of the page. Longer
tables keep the 265 pt diagram and continue on later pages.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Sonnet 5.5 committed 2026-11-28 15:40:46 -05:00
1 parent 8b77d52f98
commit 71273a7925
3 files changed
+107 -17

No files matched your search

+70 -17
View File
@@ -26,8 +26,13 @@ public static class NestPdfWriter
private const double CardThumbnailHeight = 50;
private const double CardFontSize = 8;
private const double PortraitWidth = 540;
// Letter, landscape.
private const double PageHeight = 612;
private const double LandscapeWidth = 720;
// The diagram's least height; it grows into whatever a short parts table leaves of its page.
private const double OverviewHeight = 265;
// Room kept below a grown diagram's table, so rounding never pushes its last row over.
private const double FitSlack = 2;
private static readonly XColor Fill = XColor.FromArgb(222, 222, 222);
@@ -75,20 +80,21 @@ public static class NestPdfWriter
ValidateText(snapshot);
ReportFonts.Initialize();
// Plan every diagram before layout; nothing below touches the destination until Save.
var plans = snapshot.Plates.Select(plate =>
NestReportDiagram.Plan(plate, LandscapeWidth, OverviewHeight)).ToList();
var document = new Document();
document.Info.Title = snapshot.Name;
var normal = document.Styles[StyleNames.Normal]
?? throw new InvalidOperationException("MigraDoc normal style is unavailable.");
normal.Font.Name = ReportFonts.Family;
normal.Font.Size = BodyFontSize;
var document = CreateDocument(snapshot);
var areas = new List<(Table Table, Action<XGraphics, XRect>[] Layers)>();
// The summary comes first, so text that cannot be laid out is reported in page order.
AddSummary(document, snapshot);
// Plan every diagram before layout; nothing below touches the destination until Save.
var plans = snapshot.Plates.Zip(DiagramHeights(snapshot), (plate, height) =>
NestReportDiagram.Plan(plate, LandscapeWidth, height)).ToList();
var colors = DrawingColors(snapshot);
for (var index = 0; index < snapshot.Plates.Length; index++)
AddPlate(document, snapshot, snapshot.Plates[index], plans[index], areas);
{
var plate = snapshot.Plates[index];
var plan = plans[index];
var (overview, _) = AddPlate(document, snapshot, plate, plan.Size.Height);
areas.Add((overview, [(gfx, _) => DrawRulers(gfx, plate, plan, snapshot.Units), (gfx, _) => DrawView(gfx, plate, plan, colors)]));
}
var renderer = new PdfDocumentRenderer { Document = document };
renderer.RenderDocument();
@@ -116,6 +122,52 @@ public static class NestPdfWriter
AtomicReportFile.Write(destination, stream => pdf.Save(wrapOutput?.Invoke(stream) ?? stream, false));
}
private static Document CreateDocument(NestReportSnapshot snapshot)
{
var document = new Document();
document.Info.Title = snapshot.Name;
var normal = document.Styles[StyleNames.Normal]
?? throw new InvalidOperationException("MigraDoc normal style is unavailable.");
normal.Font.Name = ReportFonts.Family;
normal.Font.Size = BodyFontSize;
return document;
}
/// <summary>
/// Each plate's diagram height. The plate pages are laid out once at the least height; where
/// the whole parts table lands on the diagram's page, the diagram takes the room left below
/// it, which puts the table at the page's foot. A longer table keeps the least height.
/// </summary>
internal static double[] DiagramHeights(NestReportSnapshot snapshot)
{
var heights = new double[snapshot.Plates.Length];
Array.Fill(heights, OverviewHeight);
if (heights.Length == 0)
return heights;
var document = CreateDocument(snapshot);
var tables = snapshot.Plates.Select(plate => AddPlate(document, snapshot, plate, OverviewHeight)).ToList();
var renderer = new DocumentRenderer(document);
renderer.PrepareDocument();
for (var page = 1; page <= renderer.FormattedDocument.PageCount; page++)
{
var infos = renderer.GetRenderInfoFromPage(page);
var index = tables.FindIndex(plate => infos.Any(info => ReferenceEquals(info.DocumentObject, plate.Overview)));
if (index < 0)
continue;
var parts = tables[index].Parts;
var info = infos.FirstOrDefault(info => ReferenceEquals(info.DocumentObject, parts));
if (info?.FormatInfo is not TableFormatInfo format || format.EndRow != parts.Rows.Count - 1)
continue;
var content = info.LayoutInfo.ContentArea;
var section = document.Sections[index] ?? throw new InvalidOperationException("Report section is unavailable.");
var bodyBottom = PageHeight - section.PageSetup.BottomMargin.Point;
var spare = bodyBottom - content.Y.Point - content.Height.Point - FitSlack;
heights[index] += System.Math.Max(0, spare);
}
return heights;
}
private static void ValidateText(NestReportSnapshot snapshot)
{
ReportFonts.ValidateText(snapshot.Name, "Nest name");
@@ -350,8 +402,9 @@ public static class NestPdfWriter
_ => value.ToString("#,0", Invariant),
};
private static void AddPlate(Document document, NestReportSnapshot snapshot, ReportPlate plate,
DiagramView plan, List<(Table, Action<XGraphics, XRect>[])> areas)
/// <summary>Returns the table reserving the diagram's area and the plate's parts table.</summary>
private static (Table Overview, Table Parts) AddPlate(Document document, NestReportSnapshot snapshot,
ReportPlate plate, double diagramHeight)
{
var colors = DrawingColors(snapshot);
var heading = $"Plate {plate.Number} of {snapshot.Plates.Length}";
@@ -378,8 +431,7 @@ public static class NestPdfWriter
AddInfoRow(info, "Est. cut time:", Duration(cutTime) + " per plate", "Total cut time:",
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, colors)]));
var overview = AddReservedArea(section, diagramHeight);
// 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.
@@ -404,7 +456,7 @@ public static class NestPdfWriter
gap.Format.Font.Size = 1;
gap.Format.SpaceAfter = Unit.FromPoint(4);
AddPlatePartsTable(section, snapshot, plate, colors, tabbed);
return (overview, AddPlatePartsTable(section, snapshot, plate, colors, tabbed));
}
/// <summary>
@@ -413,7 +465,7 @@ public static class NestPdfWriter
/// intact when the table continues on later pages; an unbordered column separates the halves.
/// A job with formed parts adds a Formed column to every plate's table.
/// </summary>
private static void AddPlatePartsTable(Section section, NestReportSnapshot snapshot, ReportPlate plate,
private static Table AddPlatePartsTable(Section section, NestReportSnapshot snapshot, ReportPlate plate,
IReadOnlyDictionary<string, XColor> colors, HashSet<string> tabbed)
{
const int Halves = 2;
@@ -478,6 +530,7 @@ public static class NestPdfWriter
ClearBorders(row.Cells[index]);
}
}
return parts;
}
/// <summary>The separating column draws only the halves' outer edges beside it.</summary>
@@ -133,6 +133,36 @@ public sealed class NestPdfLayoutTests : IDisposable
Assert.Matches($@"(?m)(?:^|\s){i}\s+Part {i:D2}\s+1\s+1(?:\s|$)", platePage);
}
[Fact]
public void DiagramHeights_GrowIntoTheRoomAShortPartsTableLeaves()
{
double Height(int drawings)
{
var nest = new Nest("Heights") { Units = Units.Inches };
var plate = new Plate(60, 96) { Quantity = 1 };
for (var i = 0; i < drawings; i++)
{
var drawing = NestReportTestData.Rectangle($"Part {i:D2}", 1, 1);
nest.Drawings.Add(drawing);
plate.Parts.Add(new Part(drawing, new Vector(1 + i % 40 * 2, 1 + i / 40 * 2)));
}
nest.Plates.Add(plate);
var snapshot = NestReportBuilder.Capture(nest, NestReportTestData.GeneratedAt);
// Rendering also proves the grown diagram still shares its page with the whole table.
Write(nest);
return Assert.Single(NestPdfWriter.DiagramHeights(snapshot));
}
ReportFonts.Initialize();
var two = Height(2);
var ten = Height(10);
Assert.True(two > ten, $"2 drawings: {two}, 10 drawings: {ten}");
Assert.True(ten > 265, $"10 drawings: {ten}");
// Too many rows for one page: the diagram keeps its least height and the table continues.
Assert.Equal(265, Height(80));
}
[SkippableFact]
public void Write_LongNamesAndNotesWrapWithoutLosingTextOrOverflowingColumns()
{
+7
View File
@@ -160,6 +160,13 @@ many drawings fit beside the diagram. Entries run left to right, then down (IDs
in order across each row), which keeps the order intact when the table continues
on later pages; each continuation repeats both halves' headings.
The diagram is at least 265 pt tall. Where the whole parts table fits on the
diagram's page below that, the diagram grows into the room left over and the table
sits at the foot of the page, so a plate with few drawings gets a larger picture.
The plate pages are laid out once to measure that room, then again with the grown
heights. A wide sheet stops growing in size at the page width. A table too long
for the page keeps the 265 pt diagram and continues on later pages.
Individual IDs use 7 pt text centered on the part's pole of inaccessibility
(`PolyLabel`, as in `PlateView`), clear of edges, holes, cutoffs and other labels.
The search uses a placement-independent quantized copy for consistent placement