From e9d9d025f7d0fe8d5ed4a11cd96747366ba78c93 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Sun, 29 Nov 2026 17:36:41 -0500 Subject: [PATCH] fix(io): keep Boxes.py outlines closed and quiet its name labels Boxes.py runs past each corner and straight back before turning. Merging those overlapping lines dropped the point where the outline turned, so a BreadBox's curved strips imported open and Separate counted their pieces as parts. A straight segment that turns back along the previous one now leaves only their net line; one that returns exactly to its start keeps the line, as a slit cut there and back. Text that import reads as a group's name is no longer reported as a skipped element, so a Boxes.py sheet opens without the partial-import message. Co-Authored-By: Claude Opus 5.5 --- OpenNest.IO/SvgReader.cs | 58 ++++++++++++++++++++++++++--- OpenNest.Tests/IO/SvgImportTests.cs | 26 +++++++++++++ docs/svg-import-export.md | 3 +- 3 files changed, 81 insertions(+), 6 deletions(-) diff --git a/OpenNest.IO/SvgReader.cs b/OpenNest.IO/SvgReader.cs index e88db9af..018fd7ba 100644 --- a/OpenNest.IO/SvgReader.cs +++ b/OpenNest.IO/SvgReader.cs @@ -55,6 +55,7 @@ namespace OpenNest.IO ); private readonly Dictionary skipped = new Dictionary(); private int droppedLoops; + private readonly HashSet captions = new HashSet(); private readonly List lines = new List(); private readonly List arcs = new List(); private readonly List circles = new List(); @@ -274,7 +275,7 @@ namespace OpenNest.IO return; var name = element.Name.LocalName; - if (IgnoredElements.Contains(name) || IsHidden(element)) + if (IgnoredElements.Contains(name) || IsHidden(element) || captions.Contains(element)) return; var title = element.Element(svgNamespace + "title")?.Value; @@ -308,7 +309,12 @@ namespace OpenNest.IO .Where(t => !IsHidden(t) && !string.IsNullOrWhiteSpace(t.Value)) .Take(2) .ToList(); - return texts.Count == 1 ? texts[0].Value : null; + if (texts.Count != 1) + return null; + + // Read as the group's name, the text is not reported as skipped. + captions.Add(texts[0]); + return texts[0].Value; } // An element's title names the geometry it draws, as the region that geometry covers. @@ -646,8 +652,23 @@ namespace OpenNest.IO var drawn = 0; var loops = new List<(Vector P0, Vector C1, Vector C2, Vector P1)>(); + // A straight run is held back so the next one can shorten it: Boxes.py runs past a + // corner and straight back ("H 483.732 H 483.350"). That spike encloses nothing, but + // merging the overlapping lines later would drop the point where the outline turns. + (Vector From, Vector To)? run = null; + + void EndRun() + { + if (run is not { } r) + return; + AddLine(r.From, r.To, t, layer); + drawn++; + run = null; + } + void EndSubpath() { + EndRun(); if (drawn > 0) droppedLoops += loops.Count; else @@ -661,6 +682,7 @@ namespace OpenNest.IO void Curve(Vector p0, Vector c1, Vector c2, Vector p1) { + EndRun(); if (Closes(p0, p1)) loops.Add((p0, c1, c2, p1)); else @@ -675,9 +697,21 @@ namespace OpenNest.IO void Straight(Vector from, Vector to) { - AddLine(from, to, t, layer); - if (!Closes(from, to)) - drawn++; + if (Closes(from, to)) + return; + + if (run is { } r && TurnsBack(r.From, from, to)) + { + // The run and its return leave only the line from where the run began. A + // return all the way to that start is a line cut there and back (a slit), so + // the run stays. + if (!Closes(r.From, to)) + run = (r.From, to); + return; + } + + EndRun(); + run = (from, to); } while (true) @@ -792,6 +826,7 @@ namespace OpenNest.IO var large = scanner.ReadFlag(); var sweep = scanner.ReadFlag(); var end = Next(); + EndRun(); AddSvgArc(current, rx, ry, rotation, large, sweep, end, t, layer); if (!Closes(current, end)) drawn++; @@ -853,6 +888,19 @@ namespace OpenNest.IO private static double Length2(Vector v) => System.Math.Sqrt(v.X * v.X + v.Y * v.Y); + /// + /// Whether the line from to runs straight + /// back along the line from to . + /// + private static bool TurnsBack(Vector start, Vector corner, Vector end) + { + var first = corner - start; + var second = end - corner; + var cross = first.X * second.Y - first.Y * second.X; + var dot = first.X * second.X + first.Y * second.Y; + return dot < 0 && System.Math.Abs(cross) <= 1e-9 * Length2(first) * Length2(second); + } + /// The SVG endpoint-to-center arc conversion (SVG 2 appendix B.2.4). private void AddSvgArc( Vector from, diff --git a/OpenNest.Tests/IO/SvgImportTests.cs b/OpenNest.Tests/IO/SvgImportTests.cs index 3e3c8f15..26281ce4 100644 --- a/OpenNest.Tests/IO/SvgImportTests.cs +++ b/OpenNest.Tests/IO/SvgImportTests.cs @@ -123,6 +123,32 @@ namespace OpenNest.Tests.IO var bodies = PartBodies.Find(result.Entities); var names = Enumerable.Range(0, bodies.Count).Select(i => bodies.NameOf(i, result.Labels)); Assert.Equal(new[] { "Top", "Bottom" }, names); + // Text read as a name was not skipped. + Assert.Empty(result.Warnings); + } + + [Theory] + // Boxes.py runs past a corner and back before turning. + [InlineData("M0 0H10H9.5V5H0Z", 9.5)] + // A return past where the run began keeps the line from that point. + [InlineData("M5 0H5.5H0V5H5Z", 5)] + public void RunsThatTurnStraightBackLeaveTheirNetLine(string path, double length) + { + var result = Import(Svg($"")); + + var shape = Assert.Single(ShapeBuilder.GetShapes(result.Entities)); + Assert.True(shape.IsClosed()); + Assert.Equal(4, result.Entities.Count); + Assert.Equal(length, result.Bounds.Length, 6); + } + + [Fact] + public void KeepsASlitCutThereAndBack() + { + var result = Import(Svg("")); + + var line = Assert.IsType(Assert.Single(result.Entities)); + Assert.Equal(5, line.Length, 6); } [Fact] diff --git a/docs/svg-import-export.md b/docs/svg-import-export.md index d51ea125..a6688192 100644 --- a/docs/svg-import-export.md +++ b/docs/svg-import-export.md @@ -23,11 +23,12 @@ Import takes path centerlines: `path`, `rect` (with rounded corners), `circle`, - Circular arcs stay arcs. Ellipses go through `EllipseConverter`, and Bézier curves are sampled and fitted by `SplineConverter`, both within 0.001 in or 0.025 mm. - Malformed path data fails the import instead of importing part of a shape. - A Bézier curve that ends where it starts is dropped, with a warning, when its subpath draws anything else: it would pinch off the outline at one point and import as a hole no cut can make. Boxes.py draws one at each inside corner when its burn (kerf) is above 0, and that burn also enlarges every part, so the warning asks for burn 0. A loop that is its subpath's whole drawing stays. +- A straight segment that turns straight back along the one before it leaves only their net line, so an overshoot past a corner and back (Boxes.py does this at its corners) does not break the outline when collinear lines are merged. A segment that returns exactly to the previous one's start keeps that line, as a slit cut there and back. - Elements with `display` none or `visibility` hidden or collapse are skipped, as are definition elements such as `defs` and `clipPath`. `