diff --git a/OpenNest.Reporting/NestPdfWriter.cs b/OpenNest.Reporting/NestPdfWriter.cs index 13398b88..a2eed8d7 100644 --- a/OpenNest.Reporting/NestPdfWriter.cs +++ b/OpenNest.Reporting/NestPdfWriter.cs @@ -23,7 +23,7 @@ public static class NestPdfWriter private const int CardsPerRow = 4; private const double CardGap = 8; private const double CardWidth = (PortraitWidth - (CardsPerRow - 1) * CardGap) / CardsPerRow; - private const double CardThumbnailHeight = 66; + private const double CardThumbnailHeight = 50; private const double CardFontSize = 8; private const double PortraitWidth = 540; private const double LandscapeWidth = 720; @@ -236,7 +236,9 @@ public static class NestPdfWriter /// /// One card per drawing, to a row. Cards are table cells with - /// unbordered spacer columns between them; MigraDoc never splits a row across pages. + /// unbordered spacer columns between them; MigraDoc never splits a row across pages. Each + /// row is its own table: rows of one table share borders across a page break, which drew the + /// next cards' top edges under the last row on a page. /// private static void AddPartCards(Section section, NestReportSnapshot snapshot) { @@ -247,24 +249,24 @@ public static class NestPdfWriter columns.Add(CardGap); columns.Add(CardWidth); } - var table = AddTable(section, columns.ToArray()); - table.Borders.Visible = false; - table.LeftPadding = table.RightPadding = Unit.FromPoint(CellPadding); - table.TopPadding = table.BottomPadding = Unit.FromPoint(CellPadding); - table.Format.Font.Size = CardFontSize; Row? row = null; for (var index = 0; index < snapshot.Drawings.Length; index++) { if (index % CardsPerRow == 0) { - if (row != null) + if (index > 0) { - var gap = table.AddRow(); - gap.HeightRule = RowHeightRule.Exactly; - gap.Height = Unit.FromPoint(CardGap); - gap.TopPadding = gap.BottomPadding = Unit.Zero; + // An empty line exactly the gap tall; at a page's foot it is just blank. + var gap = section.AddParagraph(); + gap.Format.LineSpacingRule = LineSpacingRule.Exactly; + gap.Format.LineSpacing = Unit.FromPoint(CardGap); } + var table = AddTable(section, columns.ToArray()); + table.Borders.Visible = false; + table.LeftPadding = table.RightPadding = Unit.FromPoint(CellPadding); + table.TopPadding = table.BottomPadding = Unit.FromPoint(CellPadding); + table.Format.Font.Size = CardFontSize; row = table.AddRow(); row.VerticalAlignment = VerticalAlignment.Top; } @@ -399,21 +401,88 @@ public static class NestPdfWriter gap.Format.Font.Size = 1; gap.Format.SpaceAfter = Unit.FromPoint(4); - var names2 = snapshot.Drawings.ToDictionary(drawing => drawing.Id, drawing => drawing.Name, StringComparer.Ordinal); + AddPlatePartsTable(section, snapshot, plate, colors, tabbed); + } + + /// + /// The plate's drawings as two side-by-side tables sharing one MigraDoc table, so twice as + /// many fit on the diagram's page. Entries run left to right, then down, which keeps ID order + /// intact when the table continues on later pages; an unbordered column separates the halves. + /// + private static void AddPlatePartsTable(Section section, NestReportSnapshot snapshot, ReportPlate plate, + IReadOnlyDictionary colors, HashSet tabbed) + { + const int Halves = 2; + const double Gap = 16; + double[] half = [40, 0, 86, 92]; + half[1] = (LandscapeWidth - Gap) / Halves - half.Sum(); + var widths = new List(half); + widths.Add(Gap); + widths.AddRange(half); + var stride = half.Length + 1; + + var names = snapshot.Drawings.ToDictionary(drawing => drawing.Id, drawing => drawing.Name, StringComparer.Ordinal); var perSheet = plate.Parts.GroupBy(part => part.ReportId, StringComparer.Ordinal) .ToDictionary(group => group.Key, group => (long)group.Count(), StringComparer.Ordinal); - var parts = AddTable(section, 60, 400, 110, 150); - AddHeader(parts, "ID", "Drawing", "Parts per plate", $"Total ({plate.Copies} plates)"); - foreach (var drawing in snapshot.Drawings.Where(drawing => perSheet.ContainsKey(drawing.Id))) + var drawings = snapshot.Drawings.Where(drawing => perSheet.ContainsKey(drawing.Id)).ToList(); + + var parts = AddTable(section, widths.ToArray()); + string?[] heading = ["ID", "Drawing", "Parts per plate", $"Total ({plate.Copies} plates)"]; + var header = AddRow(parts, "Table heading", true, [.. heading, null, .. heading]); + header.HeadingFormat = true; + header.Format.Font.Bold = true; + for (var index = 0; index < widths.Count; index++) { - var count = perSheet[drawing.Id]; - var row = AddRow(parts, $"Plate {plate.Number}, drawing {drawing.Id}", false, - tabbed.Contains(drawing.Id) ? drawing.Id + NestReportDiagram.TabbedMark : 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); + if (index != half.Length) + header.Cells[index].Shading.Color = Colors.LightGray; } + ClearGap(header, half.Length); + + for (var first = 0; first < drawings.Count; first += Halves) + { + var cells = new string?[widths.Count]; + var entries = drawings.Skip(first).Take(Halves).ToList(); + for (var side = 0; side < entries.Count; side++) + { + var drawing = entries[side]; + var count = perSheet[drawing.Id]; + cells[side * stride] = tabbed.Contains(drawing.Id) ? drawing.Id + NestReportDiagram.TabbedMark : drawing.Id; + cells[side * stride + 1] = names[drawing.Id]; + cells[side * stride + 2] = count.ToString(Invariant); + cells[side * stride + 3] = checked(count * plate.Copies).ToString(Invariant); + } + var row = AddRow(parts, $"Plate {plate.Number}, drawing {entries[0].Id}", false, cells); + for (var side = 0; side < entries.Count; side++) + { + // The ID cell carries the part's diagram color: the key when an ID could not be placed. + var color = ColorOf(colors, entries[side].Id); + row.Cells[side * stride].Shading.Color = new Color(color.R, color.G, color.B); + } + ClearGap(row, half.Length); + // An odd count leaves the last right half empty: no grid where there is no entry. + if (entries.Count < Halves) + { + row.Cells[half.Length].Borders.Right.Visible = false; + for (var index = half.Length + 1; index < widths.Count; index++) + ClearBorders(row.Cells[index]); + } + } + } + + /// The separating column draws only the halves' outer edges beside it. + private static void ClearGap(Row row, int index) + { + var cell = row.Cells[index]; + cell.Borders.Top.Visible = false; + cell.Borders.Bottom.Visible = false; + } + + private static void ClearBorders(Cell cell) + { + cell.Borders.Top.Visible = false; + cell.Borders.Bottom.Visible = false; + cell.Borders.Right.Visible = false; + cell.Borders.Left.Visible = false; } /// A fixed-height, never-split table row reserves a vector drawing area in MigraDoc's flow. @@ -505,7 +574,8 @@ public static class NestPdfWriter var font = NestReportDiagram.LabelFont(); foreach (var label in view.Labels.Concat(view.Groups.Select(group => group.Label))) { - gfx.DrawRectangle(XBrushes.White, label.Box); + if (label.Backed) + gfx.DrawRectangle(XBrushes.White, label.Box); gfx.DrawString(label.Id, font, XBrushes.Black, label.Box, XStringFormats.Center); } } diff --git a/OpenNest.Reporting/NestReportDiagram.cs b/OpenNest.Reporting/NestReportDiagram.cs index b7624046..c26365e9 100644 --- a/OpenNest.Reporting/NestReportDiagram.cs +++ b/OpenNest.Reporting/NestReportDiagram.cs @@ -7,8 +7,10 @@ namespace OpenNest.Reporting; /// /// A part ID label in canvas-local page coordinates (Y down). A label set beside a part too thin /// to hold it has a leader on the part's material. +/// labels get a white box: those that overhang their outline, sit off the part or +/// label a group. One clear inside its material or a hole is drawn bare, over the part's own fill. /// -internal sealed record DiagramLabel(int Part, string Id, XRect Box, XPoint? Anchor = null); +internal sealed record DiagramLabel(int Part, string Id, XRect Box, XPoint? Anchor = null, bool Backed = true); /// A dotted group boundary and its shared ID, in canvas coordinates. internal sealed record DiagramGroup(IReadOnlyList Parts, XRect Boundary, DiagramLabel Label); @@ -377,7 +379,7 @@ internal static class NestReportDiagram var test = ModelBox(fit, padded); if (canvas.Holds(box) && !placed.Any(label => label.Box.IntersectsWith(box)) && !obstacles.Any(shape => shape.Crosses(test))) - placed.Add(new DiagramLabel(index, id, box)); + placed.Add(new DiagramLabel(index, id, box, Backed: false)); else failed.Add(index); } @@ -397,7 +399,7 @@ internal static class NestReportDiagram new XPoint(area.X + area.Width / 2, area.Y + area.Height / 2)); if (spot is not { } found) continue; - placed.Add(new DiagramLabel(index, id, found)); + placed.Add(new DiagramLabel(index, id, found, Backed: false)); failed.Remove(index); } diff --git a/OpenNest.Tests/Reporting/NestPdfLayoutTests.cs b/OpenNest.Tests/Reporting/NestPdfLayoutTests.cs index 27c1824c..6ec3cffb 100644 --- a/OpenNest.Tests/Reporting/NestPdfLayoutTests.cs +++ b/OpenNest.Tests/Reporting/NestPdfLayoutTests.cs @@ -88,7 +88,7 @@ public sealed class NestPdfLayoutTests : IDisposable Assert.True(platePages.Count >= 2, $"plate 1 pages: {platePages.Count}"); Assert.All(platePages, page => Assert.Contains("Nest report: Report test job", page)); // Every page carrying plate-table rows repeats the table heading. - var tablePages = platePages.Where(page => Regex.IsMatch(page, @"(?m)^\d+\s+Demand \d{2}\s+1\s+2$")).ToList(); + var tablePages = platePages.Where(page => Regex.IsMatch(page, @"(?m)^\d+\s+Demand \d{2}\s+1\s+2\b")).ToList(); Assert.True(tablePages.Count >= 2, $"plate table pages: {tablePages.Count}"); Assert.All(tablePages, page => Assert.Matches(@"ID\s+Drawing\s+Parts per plate\s+Total \(2 plates\)", page)); var plateText = string.Join("\n", platePages); @@ -130,7 +130,7 @@ public sealed class NestPdfLayoutTests : IDisposable Assert.Contains("Est. cut time:", platePage); Assert.Contains("A dashed rectangle crossed out is a clamp to remove before cutting.", platePage); for (var i = 1; i <= 10; i++) - Assert.Matches($@"(?m)^{i}\s+Part {i:D2}\s+1\s+1$", platePage); + Assert.Matches($@"(?m)(?:^|\s){i}\s+Part {i:D2}\s+1\s+1(?:\s|$)", platePage); } [SkippableFact] @@ -156,7 +156,7 @@ public sealed class NestPdfLayoutTests : IDisposable Assert.True(size > 2, "the long name should need several card lines"); Assert.Equal(longName.Replace(" ", ""), string.Concat(card[1..size]).Replace(" ", "")); var platePage = Array.FindIndex(pages, page => page.Contains("Plate 1 of 4")); - Assert.Equal(longName.Replace(" ", ""), ColumnText(path, platePage + 1, "Drawing", "Parts")); + Assert.Equal(longName.Replace(" ", ""), ColumnText(path, platePage + 1, "Drawing", "Parts", "2")); } [SkippableTheory] @@ -697,19 +697,32 @@ public sealed class NestPdfLayoutTests : IDisposable } /// Text of the 2 row inside one table column, read from word boxes in reading order. - private static string ColumnText(string path, int page, string column, string next) + /// + /// The text of drawing 's cell under in the + /// plate parts table, whichever of its two halves holds the drawing. + /// + private static string ColumnText(string path, int page, string column, string next, string id) { + const double idColumn = 60; var words = ReportPdf.Words(path, page); - var heading = words.First(word => word.Text == column); - var limit = words.First(word => word.Text == next && System.Math.Abs(word.Top - heading.Top) < 1); - var above = words.First(word => word.Text == "1" && word.Left < heading.Left && word.Top > heading.Top); - var below = words.First(word => word.Text == "3" && word.Left < heading.Left && word.Top > above.Top); - var cell = words.Where(word => word.Left >= heading.Left - 1 && word.Right <= limit.Left - && word.Top > above.Bottom && word.Bottom < below.Top) + var heading = words.Where(word => word.Text == column) + .Single(candidate => words.Any(word => word.Text == id && word.Top > candidate.Bottom + && word.Left < candidate.Left && word.Left > candidate.Left - idColumn)); + var limit = words.Where(word => word.Text == next && System.Math.Abs(word.Top - heading.Top) < 1 + && word.Left > heading.Left).MinBy(word => word.Left)!; + bool InIdColumn((double Left, double Top, double Right, double Bottom, string Text) word) => word.Left < heading.Left && word.Left > heading.Left - idColumn; + var row = words.First(word => word.Text == id && InIdColumn(word) && word.Top > heading.Bottom); + // Cells are vertically centered, so a wrapped cell rises above its ID: the row runs + // between the IDs (or heading) above and below it. + var start = words.Where(word => InIdColumn(word) && word.Bottom < row.Top) + .Select(word => word.Bottom).DefaultIfEmpty(heading.Bottom).Max(); + var end = words.Where(word => InIdColumn(word) && word.Top > row.Bottom) + .Select(word => word.Top).DefaultIfEmpty(double.MaxValue).Min(); + bool InRow((double Left, double Top, double Right, double Bottom, string Text) word) => word.Top > start && word.Bottom < end; + var cell = words.Where(word => word.Left >= heading.Left - 1 && word.Right <= limit.Left && InRow(word)) .OrderBy(word => word.Top).ThenBy(word => word.Left); // Nothing from this cell may extend into the next column. - Assert.DoesNotContain(words, word => word.Left < limit.Left && word.Right > limit.Left - && word.Top > above.Bottom && word.Bottom < below.Top); + Assert.DoesNotContain(words, word => word.Left < limit.Left && word.Right > limit.Left && InRow(word)); return string.Concat(cell.Select(word => word.Text)); } diff --git a/docs/nest-reports.md b/docs/nest-reports.md index 647acffb..8f6749af 100644 --- a/docs/nest-reports.md +++ b/docs/nest-reports.md @@ -128,7 +128,10 @@ the weight of one part and of all nested copies, required and nested counts, and its plates. A shortage (**SHORT n**, in red) or extra (**EXTRA n**) sits beside the ID; complete drawings show no status. Cards without a weight show "-" with a note under the Parts heading saying what is missing. A card row is -a table row, so a card never splits across pages. +a one-row table, so a card never splits across pages, and the thumbnail is +short enough (50 pt) that a full page holds five rows of typical cards. Each +row being its own table keeps MigraDoc from drawing the next row's top edges +under the last row on a page. The summary's header lists the job and material on the left and the totals on the right, to leave room for part cards on the first page. When the nest @@ -143,6 +146,11 @@ the totals. The summary's Plates table adds a **Sheet weight** column when any plate's weight is known; a plate with copies shows one sheet's weight, then all copies on a second line. +A plate page's parts table is split into two side-by-side halves, so twice as +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. + 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 @@ -152,6 +160,11 @@ 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. +An ID clear inside its part's material or hole is drawn bare over the part's fill. +Every other label (one overhanging a thin strip's outline, one beside its part on +a leader, a group's shared ID) keeps a white box so outlines and leaders under it +do not cross the text. + 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,