From 40defbeeb461c0702056c86e351fc5508c027183 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Sun, 29 Nov 2026 15:46:17 -0500 Subject: [PATCH] feat(io): name SVG groups from their single text label A group with no title that holds exactly one visible text child now labels its geometry with that text, so Separate Parts names the pieces of a Boxes.py sheet ("Top", "Wall 1"). Titles still take precedence, and layer groups never use their text as a name. Co-Authored-By: Claude Opus 5.5 --- OpenNest.IO/SvgReader.cs | 22 ++++++++++++++++++++ OpenNest.Tests/IO/SvgImportTests.cs | 32 +++++++++++++++++++++++++++++ docs/svg-import-export.md | 2 +- 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/OpenNest.IO/SvgReader.cs b/OpenNest.IO/SvgReader.cs index da272ae2..c5a64eac 100644 --- a/OpenNest.IO/SvgReader.cs +++ b/OpenNest.IO/SvgReader.cs @@ -269,6 +269,8 @@ namespace OpenNest.IO return; var title = element.Element(svgNamespace + "title")?.Value; + if (string.IsNullOrWhiteSpace(title)) + title = GroupCaption(element, name); if (!string.IsNullOrWhiteSpace(title)) { var counts = (lines.Count, arcs.Count, circles.Count); @@ -280,6 +282,26 @@ namespace OpenNest.IO WalkElement(element, name, parent, parentLayer); } + /// + /// The one visible text a group holds directly, which names the group's geometry when it + /// has no title: Boxes.py writes each piece as a group with its name ("Wall 1") as text. + /// Layers are left out, since a layer's text is a note about many shapes, not a name. + /// + private string GroupCaption(XElement element, string name) + { + if (name != "g" + || element.Attribute("data-layer") != null + || element.Attributes().Any(a => a.Name.LocalName == "groupmode")) + return null; + + var texts = element + .Elements(svgNamespace + "text") + .Where(t => !IsHidden(t) && !string.IsNullOrWhiteSpace(t.Value)) + .Take(2) + .ToList(); + return texts.Count == 1 ? texts[0].Value : null; + } + // An element's title names the geometry it draws, as the region that geometry covers. private void AddLabel(string title, (int Lines, int Arcs, int Circles) before) { diff --git a/OpenNest.Tests/IO/SvgImportTests.cs b/OpenNest.Tests/IO/SvgImportTests.cs index e947a431..b6edfa32 100644 --- a/OpenNest.Tests/IO/SvgImportTests.cs +++ b/OpenNest.Tests/IO/SvgImportTests.cs @@ -69,6 +69,38 @@ namespace OpenNest.Tests.IO Assert.Equal(-1, star.Top, 6); } + [Fact] + public void GroupTextNamesTheGroupsGeometryWhenItHasNoTitle() + { + // Boxes.py writes each piece as a top-level group with its name as text. + var result = Import(Svg( + "Wall 1" + + "lidignored" + + "onetwo" + + "hidden " + + "layer note" + + "layer note" + + "no geometry")); + + Assert.Equal(new[] { "Wall 1", "lid" }, result.Labels.Select(l => l.Name)); + var wall = result.Labels[0].Bounds; + Assert.Equal(0, wall.Left, 6); + Assert.Equal(2, wall.Right, 6); + } + + [Fact] + public void GroupTextNamesSeparatedParts() + { + var result = Import(Svg( + "Top" + + "Bottom")); + + 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); + } + [Fact] public void ConvertsToMillimeterDrawings() { diff --git a/docs/svg-import-export.md b/docs/svg-import-export.md index 1d9c5413..2e80c493 100644 --- a/docs/svg-import-export.md +++ b/docs/svg-import-export.md @@ -26,7 +26,7 @@ Import takes path centerlines: `path`, `rect` (with rounded corners), `circle`, - Text, images, nested `svg`, `use` of a `symbol`, and shapes sized in percentages are skipped and listed in `CadImportResult.Warnings`. - Layers come from `data-layer`, an Inkscape layer label, or the `id` of a top-level group (Illustrator). `ETCH`, `SCRIBE`, `RAPID` and `PLATE` are dropped, so an exported plate reads back as its parts. - The file's DTD may parse, because older Illustrator files declare namespaces in one, but external fetches are off and entity expansion is capped. -- An element's `` (not the root's) is kept in `CadImportResult.Labels` as a name for the region its geometry covers. Labels never change geometry; they name the parts when the CAD Converter [separates a file](separate-parts.md). +- An element's `<title>` (not the root's) is kept in `CadImportResult.Labels` as a name for the region its geometry covers. Labels never change geometry; they name the parts when the CAD Converter [separates a file](separate-parts.md). A group with no title but exactly one visible `<text>` among its direct children takes that text as its label instead, so Boxes.py sheets (each piece a group holding its name, such as "Wall 1") name their parts. Layer groups (`data-layer` or an Inkscape layer) never do, since their text is a note, not a name. The text itself is still skipped as geometry. - Each `line` on a `BEND` layer is a bend, as a center line on a DXF's `BEND` layer is; nothing else there is read. Its `data-bend-direction` (`up` or `down`), `data-bend-angle` (degrees), `data-bend-radius` (scaled like the line) and `data-bend-note` fill in the bend when present. `CadImportOptions.DetectBends` false ignores them; the detector choice does not apply. ## What export writes