diff --git a/OpenNest.Reporting/PartPrintoutWriter.cs b/OpenNest.Reporting/PartPrintoutWriter.cs
index 16b86409..4d1b70dc 100644
--- a/OpenNest.Reporting/PartPrintoutWriter.cs
+++ b/OpenNest.Reporting/PartPrintoutWriter.cs
@@ -17,13 +17,15 @@ public static class PartPrintoutWriter
private const double RowHeight = 22;
private const double LabelWidth = 100;
private const double ValueWidth = ContentWidth / 2 - LabelWidth;
- private const double DrawingPadding = 14;
+ private const double DrawingPadding = 28;
private const double FooterTop = PageHeight - Margin - 10;
private static readonly XColor Ink = XColor.FromArgb(0x1a, 0x1a, 0x1a);
private static readonly XColor Muted = XColor.FromArgb(0x64, 0x74, 0x8b);
private static readonly XColor Line = XColor.FromArgb(0xcb, 0xd5, 0xe1);
private static readonly XColor LabelFill = XColor.FromArgb(0xf8, 0xfa, 0xfc);
+ // The Shape Library's dimension color.
+ private static readonly XColor Dimension = XColor.FromArgb(230, 120, 20);
private static readonly CultureInfo Invariant = CultureInfo.InvariantCulture;
/// Thread-safe; renders are serialized with nest reports.
@@ -111,7 +113,7 @@ public static class PartPrintoutWriter
var drawing = new XRect(Margin, y, ContentWidth, tableTop - 14 - y);
gfx.DrawRoundedRectangle(new XPen(Line, 1.5), drawing, new XSize(10, 10));
- DrawGeometry(gfx, part.Geometry, Inset(drawing, DrawingPadding));
+ DrawPart(gfx, part, units, Inset(drawing, DrawingPadding));
var rowTop = tableTop;
foreach (var (l1, v1, l2, v2) in rows)
@@ -140,14 +142,18 @@ public static class PartPrintoutWriter
}
///
- /// Strokes every contour, scaled uniformly and centered in the area. Each contour is its own
- /// path so a tab gap never gets a false joining segment.
+ /// Strokes every contour and the overall dimensions around them, scaled uniformly and
+ /// centered in the area together. Each contour is its own path so a tab gap never gets a
+ /// false joining segment.
///
- private static void DrawGeometry(XGraphics gfx, ReportGeometry geometry, XRect area)
+ private static void DrawPart(XGraphics gfx, PrintoutPart part, string units, XRect area)
{
- var fit = Fit.Create(geometry.Bounds, area);
+ var labelFont = Font(8, false);
+ var layout = PrintoutDimensions.Layout(part.Geometry, area, Length(part.Length, units),
+ Length(part.Width, units), text => ReportText.Size(text, labelFont));
+ var fit = layout.Fit;
var pen = new XPen(Ink, 0.75) { LineCap = XLineCap.Round, LineJoin = XLineJoin.Round };
- foreach (var contour in geometry.Contours)
+ foreach (var contour in part.Geometry.Contours)
{
var path = new XGraphicsPath();
NestPdfWriter.AddContour(path, contour, fit);
@@ -155,6 +161,30 @@ public static class PartPrintoutWriter
path.CloseFigure();
gfx.DrawPath(pen, path);
}
+
+ var dimensionPen = new XPen(Dimension, 0.75);
+ var dimensionBrush = new XSolidBrush(Dimension);
+ foreach (var (a, b) in layout.Lines)
+ gfx.DrawLine(dimensionPen, a, b);
+ foreach (var (tip, from) in layout.Arrows)
+ DrawArrow(gfx, dimensionBrush, tip, from);
+ var ink = new XSolidBrush(Ink);
+ foreach (var label in layout.Labels)
+ {
+ gfx.DrawRectangle(dimensionPen, XBrushes.White, label.Box);
+ gfx.DrawString(label.Text, labelFont, ink, label.Box, XStringFormats.Center);
+ }
+ }
+
+ private static void DrawArrow(XGraphics gfx, XBrush brush, XPoint tip, XPoint from)
+ {
+ var direction = from - tip;
+ if (direction.Length < 1)
+ return;
+ direction.Normalize();
+ var back = tip + direction * PrintoutDimensions.ArrowLength;
+ var across = new XVector(-direction.Y, direction.X) * PrintoutDimensions.ArrowHalfWidth;
+ gfx.DrawPolygon(brush, [tip, back + across, back - across], XFillMode.Winding);
}
private static XFont Font(double size, bool bold) =>
diff --git a/OpenNest.Reporting/PrintoutDimensions.cs b/OpenNest.Reporting/PrintoutDimensions.cs
new file mode 100644
index 00000000..9c210bfc
--- /dev/null
+++ b/OpenNest.Reporting/PrintoutDimensions.cs
@@ -0,0 +1,209 @@
+using PdfSharp.Drawing;
+
+namespace OpenNest.Reporting;
+
+/// A dimension label: its box on the page and its text.
+internal sealed record DimensionLabel(XRect Box, string Text);
+
+/// Where a printout draws its part and the overall dimensions around it, in page points.
+internal sealed record PrintoutDrawingLayout(Fit Fit, IReadOnlyList<(XPoint A, XPoint B)> Lines,
+ IReadOnlyList<(XPoint Tip, XPoint From)> Arrows, IReadOnlyList Labels);
+
+///
+/// Lays out a part with its overall dimensions as the Shape Library draws them: the X extent
+/// below the part and the Y extent to its right, each with arrows at both ends and a boxed label
+/// centered on its line. The line sits at least out from the part's bounds, and
+/// far enough that its label box stays clear of them. Extension lines
+/// start short of the part's outline (not its bounding box) and
+/// run past the dimension line. The part and its dimensions are
+/// scaled and centered together in the area.
+///
+internal static class PrintoutDimensions
+{
+ // Page points; the Shape Library's screen sizes in pixels.
+ internal const double Gap = 18;
+ internal const double LabelClearance = 14;
+ internal const double ExtensionOverrun = 5;
+ internal const double ExtensionClearance = 6;
+ internal const double ArrowLength = 8;
+ internal const double ArrowHalfWidth = 3;
+ internal const double LabelPadding = 3;
+ private const double ExtensionPrecision = 0.25;
+ private const int MaxExtensionSteps = 64;
+
+ /// The size of a label's text in the label font.
+ internal static PrintoutDrawingLayout Layout(ReportGeometry geometry, XRect area, string lengthText,
+ string widthText, Func measure)
+ {
+ var bounds = geometry.Bounds;
+ var hasLength = bounds.Length > 0;
+ var hasWidth = bounds.Width > 0;
+ var lengthLabel = hasLength ? LabelSize(lengthText, measure) : new XSize();
+ var widthLabel = hasWidth ? LabelSize(widthText, measure) : new XSize();
+
+ // How far each dimension reaches past the part, independent of scale: the line's gap,
+ // then the overrun or half the label straddling the line, whichever is farther.
+ var lengthGap = LineGap(lengthLabel.Height);
+ var widthGap = LineGap(widthLabel.Width);
+ var below = hasLength ? lengthGap + System.Math.Max(ExtensionOverrun, lengthLabel.Height / 2) : 0;
+ var right = hasWidth ? widthGap + System.Math.Max(ExtensionOverrun, widthLabel.Width / 2) : 0;
+ // A label wider than the part it measures sticks out past the part's ends.
+ var lengthSpill = 0.0;
+ var widthSpill = 0.0;
+
+ var fit = default(Fit);
+ for (var pass = 0; pass < 3; pass++)
+ {
+ var scale = Scale(bounds, area.Width - right - 2 * lengthSpill, area.Height - below - 2 * widthSpill);
+ var groupWidth = bounds.Length * scale + right;
+ var groupHeight = bounds.Width * scale + below;
+ fit = new Fit(bounds, scale,
+ area.X + (area.Width - groupWidth) / 2,
+ area.Y + (area.Height - groupHeight) / 2);
+ var nextLengthSpill = hasLength ? System.Math.Max(0, (lengthLabel.Width - bounds.Length * scale) / 2) : 0;
+ var nextWidthSpill = hasWidth ? System.Math.Max(0, (widthLabel.Height - bounds.Width * scale) / 2) : 0;
+ if (nextLengthSpill <= lengthSpill && nextWidthSpill <= widthSpill)
+ break;
+ lengthSpill = System.Math.Max(lengthSpill, nextLengthSpill);
+ widthSpill = System.Math.Max(widthSpill, nextWidthSpill);
+ }
+
+ var lines = new List<(XPoint, XPoint)>();
+ var arrows = new List<(XPoint, XPoint)>();
+ var labels = new List();
+ var part = fit.Rect(bounds);
+ if (hasLength)
+ AddLinear(geometry, fit, part, new ReportPoint(bounds.Left, bounds.Bottom),
+ new ReportPoint(bounds.Right, bounds.Bottom), new XVector(0, 1), lengthGap, lengthText, lengthLabel,
+ lines, arrows, labels);
+ if (hasWidth)
+ AddLinear(geometry, fit, part, new ReportPoint(bounds.Right, bounds.Bottom),
+ new ReportPoint(bounds.Right, bounds.Top), new XVector(1, 0), widthGap, widthText, widthLabel,
+ lines, arrows, labels);
+ return new PrintoutDrawingLayout(fit, lines, arrows, labels);
+ }
+
+ /// How far out a dimension line sits for a label points across it.
+ private static double LineGap(double across) => System.Math.Max(Gap, across / 2 + LabelClearance);
+
+ private static double Scale(ReportBounds bounds, double width, double height)
+ {
+ var scales = new List(2);
+ if (bounds.Length > 0)
+ scales.Add(width / bounds.Length);
+ if (bounds.Width > 0)
+ scales.Add(height / bounds.Width);
+ var scale = scales.Count == 0 ? 1 : scales.Min();
+ if (!double.IsFinite(scale) || scale <= 0)
+ throw new InvalidOperationException("The part and its dimensions cannot be fitted to the page.");
+ return scale;
+ }
+
+ private static XSize LabelSize(string text, Func measure)
+ {
+ var size = measure(text);
+ return new XSize(size.Width + 2 * LabelPadding, size.Height + LabelPadding);
+ }
+
+ /// Page-space unit vector from the part toward the dimension line.
+ private static void AddLinear(ReportGeometry geometry, Fit fit, XRect part, ReportPoint start, ReportPoint end,
+ XVector side, double gap, string text, XSize labelSize, List<(XPoint, XPoint)> lines,
+ List<(XPoint, XPoint)> arrows, List labels)
+ {
+ var a = fit.Point(start);
+ var b = fit.Point(end);
+ var extreme = new[] { part.TopLeft, part.TopRight, part.BottomLeft, part.BottomRight }.Max(c => Dot(c, side));
+ var reachA = extreme - Dot(a, side) + gap;
+ var reachB = extreme - Dot(b, side) + gap;
+ var lineA = a + side * reachA;
+ var lineB = b + side * reachB;
+ var extent = System.Math.Abs(side.X) > System.Math.Abs(side.Y) ? part.Width : part.Height;
+ lines.Add((a + side * ExtensionStart(geometry, fit, start, side, reachA, extent), lineA + side * ExtensionOverrun));
+ lines.Add((b + side * ExtensionStart(geometry, fit, end, side, reachB, extent), lineB + side * ExtensionOverrun));
+ lines.Add((lineA, lineB));
+ arrows.Add((lineA, lineB));
+ arrows.Add((lineB, lineA));
+ var center = new XPoint((lineA.X + lineB.X) / 2, (lineA.Y + lineB.Y) / 2);
+ labels.Add(new DimensionLabel(new XRect(center.X - labelSize.Width / 2, center.Y - labelSize.Height / 2,
+ labelSize.Width, labelSize.Height), text));
+ }
+
+ ///
+ /// How far from the measured point (in points along ; negative past
+ /// it, toward the part) its extension line starts: walking in from the dimension line,
+ /// points out, by the distance known to be free at each step until
+ /// it comes from the outline. A measured point off the
+ /// outline, such as a bounding-box corner beside a round part, is passed; the walk ends at
+ /// the part's far side at the latest.
+ ///
+ private static double ExtensionStart(ReportGeometry geometry, Fit fit, ReportPoint point, XVector side,
+ double reach, double extent)
+ {
+ // Page Y points down; the model's points up.
+ var modelSide = new XVector(side.X, -side.Y);
+ var limit = reach + extent;
+ var travelled = 0.0;
+ for (var i = 0; i < MaxExtensionSteps && travelled < limit; i++)
+ {
+ var along = (reach - travelled) / fit.Scale;
+ var at = new ReportPoint(point.X + modelSide.X * along, point.Y + modelSide.Y * along);
+ var free = Distance(geometry, at) * fit.Scale - ExtensionClearance;
+ if (free <= ExtensionPrecision)
+ break;
+ travelled += free;
+ }
+ return reach - System.Math.Min(travelled, limit);
+ }
+
+ /// Distance from to the nearest line or arc of the part.
+ internal static double Distance(ReportGeometry geometry, ReportPoint point)
+ {
+ var nearest = double.MaxValue;
+ foreach (var contour in geometry.Contours)
+ {
+ foreach (var segment in contour.Segments)
+ nearest = System.Math.Min(nearest, Distance(segment, point));
+ }
+ return nearest;
+ }
+
+ private static double Distance(ReportSegment segment, ReportPoint p)
+ {
+ if (segment.Center is not { } c)
+ return SegmentDistance(segment.Start, segment.End, p);
+
+ var dx = p.X - c.X;
+ var dy = p.Y - c.Y;
+ var fromCenter = System.Math.Sqrt(dx * dx + dy * dy);
+ if (System.Math.Abs(segment.SweepAngle) >= 2 * System.Math.PI - 1e-9 || fromCenter == 0)
+ return System.Math.Abs(fromCenter - segment.Radius);
+
+ // Within the swept angles the nearest point is on the arc; otherwise it is an end.
+ var turn = segment.SweepAngle > 0
+ ? Normalize(System.Math.Atan2(dy, dx) - segment.StartAngle)
+ : Normalize(segment.StartAngle - System.Math.Atan2(dy, dx));
+ return turn <= System.Math.Abs(segment.SweepAngle)
+ ? System.Math.Abs(fromCenter - segment.Radius)
+ : System.Math.Min(PointDistance(segment.Start, p), PointDistance(segment.End, p));
+ }
+
+ private static double Normalize(double angle)
+ {
+ var turn = angle % (2 * System.Math.PI);
+ return turn < 0 ? turn + 2 * System.Math.PI : turn;
+ }
+
+ private static double SegmentDistance(ReportPoint a, ReportPoint b, ReportPoint p)
+ {
+ var abx = b.X - a.X;
+ var aby = b.Y - a.Y;
+ var lengthSquared = abx * abx + aby * aby;
+ var t = lengthSquared == 0 ? 0 : System.Math.Clamp(((p.X - a.X) * abx + (p.Y - a.Y) * aby) / lengthSquared, 0, 1);
+ return PointDistance(new ReportPoint(a.X + abx * t, a.Y + aby * t), p);
+ }
+
+ private static double PointDistance(ReportPoint a, ReportPoint b) =>
+ System.Math.Sqrt((a.X - b.X) * (a.X - b.X) + (a.Y - b.Y) * (a.Y - b.Y));
+
+ private static double Dot(XPoint p, XVector d) => p.X * d.X + p.Y * d.Y;
+}
diff --git a/OpenNest.Tests/Reporting/PartPrintoutTests.cs b/OpenNest.Tests/Reporting/PartPrintoutTests.cs
index 3799ae3d..a1d9f00a 100644
--- a/OpenNest.Tests/Reporting/PartPrintoutTests.cs
+++ b/OpenNest.Tests/Reporting/PartPrintoutTests.cs
@@ -1,5 +1,7 @@
using OpenNest.CNC;
+using OpenNest.Geometry;
using OpenNest.Reporting;
+using PdfSharp.Drawing;
using PdfSharp.Pdf.IO;
namespace OpenNest.Tests.Reporting;
@@ -156,6 +158,59 @@ public sealed class PartPrintoutTests : IDisposable
Assert.False(File.Exists(report));
}
+ [Theory]
+ [InlineData(10, 10)]
+ [InlineData(40, 0.75)]
+ [InlineData(0.5, 30)]
+ public void Dimensions_StayInsideTheAreaWithThePartBetweenThem(double length, double width)
+ {
+ var geometry = Capture(NestReportTestData.Rectangle("part", length, width).Program);
+ var area = new XRect(100, 100, 400, 300);
+
+ var layout = PrintoutDimensions.Layout(geometry, area, "L", "W", _ => new XSize(30, 9));
+
+ var part = layout.Fit.Rect(geometry.Bounds);
+ Assert.True(Contains(area, part));
+ foreach (var (a, b) in layout.Lines)
+ Assert.True(Contains(area, a) && Contains(area, b));
+ foreach (var label in layout.Labels)
+ Assert.True(Contains(area, label.Box), $"{label.Text} {label.Box}");
+ // X extent below the part, Y extent to its right, each label box clear of the part.
+ var (lengthLabel, widthLabel) = (layout.Labels[0], layout.Labels[1]);
+ Assert.Equal(part.Bottom + PrintoutDimensions.LabelClearance, lengthLabel.Box.Top, 6);
+ Assert.Equal(part.Right + PrintoutDimensions.LabelClearance, widthLabel.Box.Left, 6);
+ }
+
+ [Fact]
+ public void Dimensions_ExtensionLinesStopShortOfTheOutlineNotTheBounds()
+ {
+ var disc = new Program();
+ disc.Codes.Add(new RapidMove(10, 5));
+ disc.Codes.Add(new ArcMove(new Vector(10, 5), new Vector(5, 5), RotationType.CW));
+ var geometry = Capture(disc);
+
+ var layout = PrintoutDimensions.Layout(geometry, new XRect(0, 0, 500, 500), "L", "W", _ => new XSize(30, 9));
+
+ var fit = layout.Fit;
+ var extensions = layout.Lines.Where((_, index) => index % 3 != 2).ToList(); // two per dimension, then its line
+ Assert.Equal(4, extensions.Count);
+ foreach (var (start, _) in extensions)
+ {
+ var model = fit.Model(start);
+ var clearance = PrintoutDimensions.Distance(geometry, new ReportPoint(model.X, model.Y)) * fit.Scale;
+ Assert.InRange(clearance, PrintoutDimensions.ExtensionClearance - 0.01, PrintoutDimensions.ExtensionClearance + 0.5);
+ }
+ }
+
+ private static ReportGeometry Capture(Program program) =>
+ NestReportGeometry.Capture(program, new Vector(), "test");
+
+ private static bool Contains(XRect area, XRect rect) =>
+ rect.Left >= area.Left - 1e-6 && rect.Right <= area.Right + 1e-6
+ && rect.Top >= area.Top - 1e-6 && rect.Bottom <= area.Bottom + 1e-6;
+
+ private static bool Contains(XRect area, XPoint point) => Contains(area, new XRect(point, new XSize()));
+
[Fact]
public void Write_RejectsTextTheBundledFontCannotShow()
{
diff --git a/docs/nest-reports.md b/docs/nest-reports.md
index 8f8d2428..d781b5cb 100644
--- a/docs/nest-reports.md
+++ b/docs/nest-reports.md
@@ -37,7 +37,12 @@ selected one. Both render to `%TEMP%\OpenNest\Reports\` and open the report view
suggesting `.printout.pdf` or `.printout.pdf`. Each part
gets one Letter portrait page, bookmarked by name, with the name and customer (the drawing's, else the
nest's) as the header, the part's material contours stroked to fit (leads, rapids
-and marks omitted), a details table and the generated time. The table shows the
+and marks omitted) with its overall dimensions, a details table and the generated
+time. The dimensions follow the Shape Library's: X extent below the part, Y extent
+to its right, each line at least 18 pt out with arrows and a boxed label kept
+14 pt clear of the part, and extension lines that stop 6 pt short of the outline
+itself (not its bounding box). The part and its dimensions are scaled and
+centered together 28 pt inside the drawing border. The table shows the
bounding box (Y extent x X extent), nest material and thickness (led by the
machine config's thickness name, as in the nest report), cut length and
pierce count, net area, weight