diff --git a/OpenNest.Reporting/NestPdfWriter.cs b/OpenNest.Reporting/NestPdfWriter.cs index 6611e48d..13398b88 100644 --- a/OpenNest.Reporting/NestPdfWriter.cs +++ b/OpenNest.Reporting/NestPdfWriter.cs @@ -381,9 +381,17 @@ public static class NestPdfWriter // 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. + // Tabbed parts are marked too: the tabs must be broken to free them. Both lines share one. + var tabbed = plate.Parts.Where(part => part.Geometry.StrokeOnly).Select(part => part.ReportId) + .ToHashSet(StringComparer.Ordinal); + var notes = new List(); + if (tabbed.Count > 0) + notes.Add($"{NestReportDiagram.TabbedMark} Tabbed: held in the sheet by tabs."); if (plate.Clamps.Any(clamp => clamp.Removed)) + notes.Add("A dashed rectangle crossed out is a clamp to remove before cutting."); + if (notes.Count > 0) { - var note = section.AddParagraph("A dashed rectangle crossed out is a clamp to remove before cutting."); + var note = section.AddParagraph(string.Join(" ", notes)); note.Format.Font.Size = 7.5; note.Format.SpaceBefore = Unit.FromPoint(4); } @@ -399,7 +407,8 @@ public static class NestPdfWriter foreach (var drawing in snapshot.Drawings.Where(drawing => perSheet.ContainsKey(drawing.Id))) { var count = perSheet[drawing.Id]; - var row = 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, + 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); @@ -587,6 +596,16 @@ public static class NestPdfWriter { if (geometry.StrokeOnly) { + // A tabbed part's outline is open at each tab. It is still filled, across the gaps, + // so its color identifies it; the outline keeps its gaps on top. + var region = new XGraphicsPath { FillMode = XFillMode.Alternate }; + foreach (var loop in FillLoops(geometry.Contours)) + { + foreach (var contour in loop) + AddContour(region, contour, fit); + region.CloseFigure(); + } + gfx.DrawPath(new XSolidBrush(fill), region); StrokeContours(gfx, geometry, fit, pen); return; } @@ -601,6 +620,45 @@ public static class NestPdfWriter gfx.DrawPath(pen, new XSolidBrush(fill), path); } + /// + /// The contours of a part with open ones, chained into loops for filling: each closed + /// contour is its own loop, and the open pieces of a tabbed outline or cutout are joined end + /// to nearest start until the next piece's start is farther than the loop's own start, which + /// bridges each tab gap with a straight line. + /// + internal static List> FillLoops(IEnumerable contours) + { + static double Distance(ReportPoint a, ReportPoint b) => System.Math.Sqrt( + System.Math.Pow(a.X - b.X, 2) + System.Math.Pow(a.Y - b.Y, 2)); + + var loops = new List>(); + var open = new List(); + foreach (var contour in contours) + { + if (contour.Closed) + loops.Add([contour]); + else + open.Add(contour); + } + while (open.Count > 0) + { + var loop = new List { open[0] }; + open.RemoveAt(0); + var first = loop[0].Segments[0].Start; + while (open.Count > 0) + { + var end = loop[^1].Segments[^1].End; + var next = open.MinBy(contour => Distance(end, contour.Segments[0].Start))!; + if (Distance(end, next.Segments[0].Start) > Distance(end, first)) + break; + loop.Add(next); + open.Remove(next); + } + loops.Add(loop); + } + return loops; + } + /// /// PDFsharp's StartFigure does not emit a move-to after an open figure, which would draw a /// false segment across a tab gap to the next contour. Stroke each contour as its own path. diff --git a/OpenNest.Reporting/NestReportDiagram.cs b/OpenNest.Reporting/NestReportDiagram.cs index 9af5a356..b7624046 100644 --- a/OpenNest.Reporting/NestReportDiagram.cs +++ b/OpenNest.Reporting/NestReportDiagram.cs @@ -95,6 +95,13 @@ internal static class NestReportDiagram internal static double PolePrecision(ReportBounds bounds) => System.Math.Max(System.Math.Max(bounds.Length, bounds.Width) * 0.005, 1e-9); + /// Appended to a tabbed part's ID: its outline is open at the tabs holding it in the sheet. + internal const string TabbedMark = "*"; + + /// A part's diagram label: its report ID, marked when the part is tabbed. + internal static string LabelText(ReportPlate plate, int index) => + plate.Parts[index].ReportId + (plate.Parts[index].Geometry.StrokeOnly ? TabbedMark : ""); + internal static DiagramView Plan(ReportPlate plate, double width, double height) { var shapes = plate.Parts.Select(part => new Shape(part.Geometry)).ToList(); @@ -245,7 +252,8 @@ internal static class NestReportDiagram || groups.Any(group => group.Boundary.IntersectsWith(boundary) || group.Label.Box.IntersectsWith(boundary))) continue; - var size = ReportText.Size(id, font); + var text = members.Any(member => plate.Parts[member].Geometry.StrokeOnly) ? id + TabbedMark : id; + var size = ReportText.Size(text, font); var labelWidth = size.Width + 2 * LabelPadding; var labelHeight = size.Height + 2 * LabelPadding; var centerX = boundary.X + (boundary.Width - labelWidth) / 2; @@ -270,7 +278,7 @@ internal static class NestReportDiagram var spot = BestSpot(candidates, obstacles, others, fit, Blocked, target) ?? BestSpot(inside, others, others, fit, Blocked, target, required: 0, preferRoom: false); if (spot is { } box) - groups.Add(new DiagramGroup(members, boundary, new DiagramLabel(seed, id, box))); + groups.Add(new DiagramGroup(members, boundary, new DiagramLabel(seed, text, box))); } return groups; } @@ -358,7 +366,7 @@ internal static class NestReportDiagram var placed = new List(); foreach (var index in targets) { - var id = plate.Parts[index].ReportId; + var id = LabelText(plate, index); var size = ReportText.Size(id, font); var width = size.Width + 2 * LabelPadding; var height = size.Height + 2 * LabelPadding; @@ -379,7 +387,7 @@ internal static class NestReportDiagram // C shape's opening) reads as belonging to whatever is near it, so that part gets a leader. foreach (var index in failed.ToList()) { - var id = plate.Parts[index].ReportId; + var id = LabelText(plate, index); var size = ReportText.Size(id, font); var area = fit.Rect(shapes[index].Bounds); var others = obstacles.Where((_, other) => other != index).ToList(); @@ -398,7 +406,7 @@ internal static class NestReportDiagram // 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 id = LabelText(plate, index); var size = ReportText.Size(id, font); var width = size.Width + 2 * LabelPadding; var height = size.Height + 2 * LabelPadding; @@ -445,14 +453,14 @@ internal static class NestReportDiagram .Where(group => group.Count() > 1).Select(group => group.Key).ToHashSet(); foreach (var index in unlabeled) { - var id = plate.Parts[index].ReportId; + var id = LabelText(plate, index); var size = ReportText.Size(id, font); var width = size.Width + 2 * LabelPadding; var height = size.Height + 2 * LabelPadding; // Leaders are for thin parts. Tiny repeated parts that could not share a group label // would bury the sheet in leaders, so they stay unlabeled; a lone tiny part gets one. var extent = fit.Rect(shapes[index].Bounds); - if (shared.Contains(id) && System.Math.Max(extent.Width, extent.Height) < 2 * height) + if (shared.Contains(plate.Parts[index].ReportId) && System.Math.Max(extent.Width, extent.Height) < 2 * height) continue; var anchor = fit.Point(shapes[index].Pole.X, shapes[index].Pole.Y); var others = obstacles.Where((_, other) => other != index).ToList(); diff --git a/OpenNest.Tests/Reporting/NestPdfWriterTests.cs b/OpenNest.Tests/Reporting/NestPdfWriterTests.cs index 711be5fb..f91bc203 100644 --- a/OpenNest.Tests/Reporting/NestPdfWriterTests.cs +++ b/OpenNest.Tests/Reporting/NestPdfWriterTests.cs @@ -144,12 +144,81 @@ public sealed class NestPdfWriterTests : IDisposable var path = WriteReport(nest); using var pdf = PdfReader.Open(path, PdfDocumentOpenMode.Import); Assert.Equal(2, pdf.PageCount); - // The vector diagram is the last content stream on the plate page. Each stroke-only - // contour must begin its own subpath; a missing move-to draws a false connecting line. - // The drawing area's clip path ("W* n") precedes the geometry. + var (fill, stroke) = TabbedPaths(pdf); + // The fill closes the outline across its tab gap (one subpath per loop: outline and hole). + Assert.Equal(NestPdfWriter.FillLoops(geometry.Contours).Count, Regex.Matches(fill, @"(?m)^[-\d. ]+ m$").Count); + // Each stroke-only contour must begin its own subpath; a missing move-to draws a false + // connecting line across the tab gap. + Assert.Equal(geometry.Contours.Length, Regex.Matches(stroke, @"(?m)^[-\d. ]+ m$").Count); + Assert.Contains("1*", ReportPdf.Pages(path)[1]); + } + + [Fact] + public void Write_OutlineInTwoTabbedPiecesFillsAsOneShape() + { + // A 5 x 1 in bar cut in two pieces around two 0.1 in tabs. + var program = new OpenNest.CNC.Program(); + program.Codes.Add(new OpenNest.CNC.RapidMove(0, 0)); + program.Codes.Add(new OpenNest.CNC.LinearMove(0, 1)); + program.Codes.Add(new OpenNest.CNC.LinearMove(4.9, 1)); + program.Codes.Add(new OpenNest.CNC.RapidMove(5, 0.9)); + program.Codes.Add(new OpenNest.CNC.LinearMove(5, 0)); + program.Codes.Add(new OpenNest.CNC.LinearMove(0.1, 0)); + var drawing = new Drawing("Two pieces", program); + var nest = new Nest("Two pieces") { Units = Units.Inches }; + nest.Drawings.Add(drawing); + var plate = new Plate(24, 48) { Quantity = 1 }; + plate.Parts.Add(new Part(drawing, new Vector(2, 2))); + nest.Plates.Add(plate); + var geometry = NestReportBuilder.Capture(nest, NestReportTestData.GeneratedAt).Plates[0].Parts[0].Geometry; + Assert.Equal(2, geometry.Contours.Length); + Assert.All(geometry.Contours, contour => Assert.False(contour.Closed)); + + var path = WriteReport(nest); + using var pdf = PdfReader.Open(path, PdfDocumentOpenMode.Import); + var (fill, stroke) = TabbedPaths(pdf); + + Assert.Single(Regex.Matches(fill, @"(?m)^[-\d. ]+ m$")); + Assert.Equal(2, Regex.Matches(stroke, @"(?m)^[-\d. ]+ m$").Count); + var page = ReportPdf.Pages(path)[1]; + Assert.Matches(@"(?m)^1\*\s+Two pieces\s+1\s+1$", page); + Assert.Contains("* Tabbed: held in the sheet by tabs.", page); + } + + [Fact] + public void FillLoops_ChainsOpenPiecesNearestEndToStartAndKeepsClosedContoursApart() + { + static ReportContour Line(double x1, double y1, double x2, double y2, bool closed = false) => + new([new ReportSegment(new ReportPoint(x1, y1), new ReportPoint(x2, y2), null, 0, 0, 0)], closed); + var hole = new ReportContour([new ReportSegment(new ReportPoint(3, 2), new ReportPoint(3, 2), + new ReportPoint(2, 2), 1, 0, 2 * System.Math.PI)], true); + // An outline in three pieces listed out of order, a closed hole and a separate open slot. + var top = Line(9.9, 10, 0.1, 10); + var left = Line(0, 9.9, 0, 0.1); + var rest = new ReportContour([ + new ReportSegment(new ReportPoint(0.1, 0), new ReportPoint(10, 0), null, 0, 0, 0), + new ReportSegment(new ReportPoint(10, 0), new ReportPoint(10, 9.9), null, 0, 0, 0)], false); + var slot = Line(20, 20, 25, 20); + + var loops = NestPdfWriter.FillLoops([rest, hole, top, slot, left]); + + Assert.Equal(3, loops.Count); + Assert.Equal([hole], loops[0]); + Assert.Equal([rest, top, left], loops[1]); + Assert.Equal([slot], loops[2]); + } + + /// + /// The plate diagram's fill and stroke operators for its one tabbed part: the vector diagram + /// is the last content stream on the plate page, after the drawing area's clip ("W* n"). + /// + private static (string Fill, string Stroke) TabbedPaths(PdfSharp.Pdf.PdfDocument pdf) + { var diagram = ReportPdf.ContentStreams(pdf.Pages[1]).Last(); diagram = diagram[(diagram.IndexOf("W* n", StringComparison.Ordinal) + 4)..]; - Assert.Equal(geometry.Contours.Length, Regex.Matches(diagram, @"(?m)^[-\d. ]+ m$").Count); + var fillEnd = diagram.IndexOf("\nf*", StringComparison.Ordinal); + Assert.True(fillEnd > 0, "The tabbed part is not filled."); + return (diagram[..fillEnd], diagram[fillEnd..]); } [Fact] diff --git a/docs/nest-reports.md b/docs/nest-reports.md index 3611e2fb..7f0ab59b 100644 --- a/docs/nest-reports.md +++ b/docs/nest-reports.md @@ -84,9 +84,12 @@ PartPrintoutWriter.Write(snapshot, destinationPath); - Actual placed programs already contain rotations. Capture their material paths with the location applied once. Preserve holes and intentional tab gaps; exclude rapid, scribe, lead-in and lead-out paths. Keep Cut and Display contours. -- If any material contour of a part is open, show all its contours without fill. - Do not close the gap or substitute the drawing's nominal outline. Cutoffs are - separate strokes with no product ID or quantity. +- If any material contour of a part is open (a tabbed part), its outline keeps + every gap: do not close it or substitute the drawing's nominal outline. It is + still filled in its drawing's color, the fill bridging each tab gap with a + straight line, and its ID is marked `*` on the diagram and in the plate's parts + table, with a "* Tabbed" legend line under the diagram. Cutoffs are separate + strokes with no product ID or quantity. - Snapshots retain values, not live drawings, parts, CNC programs or mutable geometry. Invalid/missing/nonfinite geometry fails with drawing/plate context. - A drawing's weight is its net area (holes excluded) times the nest thickness @@ -189,7 +192,8 @@ Machine clamps appear on the plate diagram as white, diagonally hatched rectangles drawn under the parts, and IDs keep clear of them like any other obstacle. A clamp taken off the sheet is a dashed rectangle crossed out, drawn over the parts (they may sit where it was) so the operator sees it must come off -before cutting. Only a removed clamp gets a legend line under the diagram; hatched +before cutting. Only a removed clamp (and a tabbed part) gets a legend line under +the diagram, the two sharing one line; hatched clamps and the rulers explain themselves, which leaves room for ten parts-table rows on a plate page even with an estimated cut time. @@ -213,8 +217,10 @@ Card thumbnails and the sheet diagram are vector paths, never raster images. Each thumbnail is a small PDFsharp page embedded by MigraDoc as a form XObject. The diagram is drawn into a fixed-height table row reserved in MigraDoc's flow, located after layout with `DocumentRenderer.GetRenderInfoFromPage`. Closed parts -use one even-odd filled path, so holes stay unfilled. Parts with any open contour, -and cutoffs (dashed), stroke each contour as its own path: PDFsharp's +use one even-odd filled path, so holes stay unfilled. A part with any open contour +is filled first as one even-odd path of loops (each closed contour alone, open +pieces chained end to nearest start until the loop's own start is nearer), with +no stroke. Its contours, and cutoffs (dashed), then stroke each contour as its own path: PDFsharp's `StartFigure` does not start a new subpath after an open figure, and would otherwise draw a false segment across a tab gap.