From bf9b0df8169b2dede177bf552e7d2c11d18767ce Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Sat, 28 Nov 2026 14:40:14 -0500 Subject: [PATCH] fix(reports): draw arcs as Bezier curves to stop an out-of-memory crash PDFsharp 6.2.4's XGraphicsPath.AddArc loops until memory runs out on some arcs ending on a quadrant boundary within rounding, which nested part geometry produces. Nest reports and part printouts now build arcs from Bezier curves themselves. Co-Authored-By: Claude Sonnet 5.5 --- OpenNest.Reporting/NestPdfWriter.cs | 30 +++++++++++++++--- .../Reporting/NestPdfWriterArcTests.cs | 31 +++++++++++++++++++ 2 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 OpenNest.Tests/Reporting/NestPdfWriterArcTests.cs diff --git a/OpenNest.Reporting/NestPdfWriter.cs b/OpenNest.Reporting/NestPdfWriter.cs index c887067e..9451adb1 100644 --- a/OpenNest.Reporting/NestPdfWriter.cs +++ b/OpenNest.Reporting/NestPdfWriter.cs @@ -765,15 +765,35 @@ public static class NestPdfWriter path.AddLine(fit.Point(segment.Start), fit.Point(segment.End)); continue; } - var center = fit.Point(segment.Center); - var radius = segment.Radius * fit.Scale; - var box = new XRect(center.X - radius, center.Y - radius, 2 * radius, 2 * radius); // Page Y points down, so model angles and sweeps change sign. - path.AddArc(box, -Degrees(segment.StartAngle), -Degrees(segment.SweepAngle)); + AddArc(path, fit.Point(segment.Center), segment.Radius * fit.Scale, + -segment.StartAngle, -segment.SweepAngle); } } - private static double Degrees(double radians) => radians * 180 / System.Math.PI; + /// + /// Adds an arc as Bezier curves of at most a quarter turn each. Never use PDFsharp's AddArc: + /// in 6.2.4 some arcs ending on a quadrant boundary, within rounding, loop until memory + /// runs out. Angles are in page radians. + /// + private static void AddArc(XGraphicsPath path, XPoint center, double radius, double start, double sweep) + { + // The margin keeps a quarter turn that is a hair long from becoming two curves. + var count = System.Math.Max(1, (int)System.Math.Ceiling(System.Math.Abs(sweep) / (System.Math.PI / 2) - 1e-9)); + var step = sweep / count; + var handle = 4.0 / 3 * System.Math.Tan(step / 4) * radius; + for (var index = 0; index < count; index++) + { + var (sin0, cos0) = System.Math.SinCos(start + index * step); + var (sin1, cos1) = System.Math.SinCos(start + (index + 1) * step); + var from = new XPoint(center.X + radius * cos0, center.Y + radius * sin0); + var to = new XPoint(center.X + radius * cos1, center.Y + radius * sin1); + path.AddBezier(from, + new XPoint(from.X - handle * sin0, from.Y + handle * cos0), + new XPoint(to.X + handle * sin1, to.Y - handle * cos1), + to); + } + } private static XFont BodyFont(bool bold) => new(ReportFonts.Family, BodyFontSize, bold ? XFontStyleEx.Bold : XFontStyleEx.Regular); diff --git a/OpenNest.Tests/Reporting/NestPdfWriterArcTests.cs b/OpenNest.Tests/Reporting/NestPdfWriterArcTests.cs new file mode 100644 index 00000000..b286a5d0 --- /dev/null +++ b/OpenNest.Tests/Reporting/NestPdfWriterArcTests.cs @@ -0,0 +1,31 @@ +using System.Collections.Immutable; +using OpenNest.Reporting; +using PdfSharp.Drawing; + +namespace OpenNest.Tests.Reporting; + +public sealed class NestPdfWriterArcTests +{ + // Start and sweep, in model degrees, of arcs ending on a quadrant boundary within rounding. + // PDFsharp's own AddArc runs out of memory on these. + [Theory] + [InlineData(180.00000000000003, 180)] + [InlineData(270.0000000000001, 89.9999999999999)] + [InlineData(359.999, 0.001)] + [InlineData(360, 1e-14)] + [InlineData(0, 360)] + [InlineData(90, -360)] + public void AddContour_DrawsArcsEndingOnAQuadrantBoundary(double start, double sweep) + { + var from = start * System.Math.PI / 180; + var span = sweep * System.Math.PI / 180; + ReportPoint At(double angle) => new(10 * System.Math.Cos(angle), 10 * System.Math.Sin(angle)); + var contour = new ReportContour(ImmutableArray.Create( + new ReportSegment(At(from), At(from + span), new ReportPoint(0, 0), 10, from, span)), false); + var fit = Fit.Create(new ReportBounds(-10, -10, 10, 10), new XRect(0, 0, 100, 100)); + + var exception = Record.Exception(() => NestPdfWriter.AddContour(new XGraphicsPath(), contour, fit)); + + Assert.Null(exception); + } +}