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 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-11-29 15:46:17 -05:00
1 parent 197a1ac760
commit 40defbeeb4
3 files changed
+55 -1

No files matched your search

+22
View File
@@ -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);
}
/// <summary>
/// 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.
/// </summary>
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)
{
+32
View File
@@ -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(
"<g id='p-0'><path d='M0 0H2V2H0Z'/><text x='1' y='1'>Wall <tspan>1</tspan></text></g>"
+ "<g id='p-1'><title>lid</title><path d='M3 0H5V2H3Z'/><text>ignored</text></g>"
+ "<g><path d='M6 0H8V2H6Z'/><text>one</text><text>two</text></g>"
+ "<g><path d='M0 3H2V5H0Z'/><text style='display:none'>hidden</text><text> </text></g>"
+ "<g data-layer='CUT'><path d='M3 3H5V5H3Z'/><text>layer note</text></g>"
+ "<g xmlns:inkscape='http://www.inkscape.org/namespaces/inkscape' inkscape:groupmode='layer'"
+ " inkscape:label='Layer 1'><path d='M6 3H8V5H6Z'/><text>layer note</text></g>"
+ "<g><text>no geometry</text></g>"));
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(
"<g id='p-0'><path d='M0 0H2V2H0Z'/><text>Top</text></g>"
+ "<g id='p-1'><path d='M3 0H5V2H3Z'/><text>Bottom</text></g>"));
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()
{
+1 -1
View File
@@ -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 `<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).
- 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