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 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-11-29 17:36:41 -05:00
1 parent 6ee5633212
commit e9d9d025f7
3 files changed
+81 -6

No files matched your search

+53 -5
View File
@@ -55,6 +55,7 @@ namespace OpenNest.IO
);
private readonly Dictionary<string, int> skipped = new Dictionary<string, int>();
private int droppedLoops;
private readonly HashSet<XElement> captions = new HashSet<XElement>();
private readonly List<Line> lines = new List<Line>();
private readonly List<Arc> arcs = new List<Arc>();
private readonly List<Circle> circles = new List<Circle>();
@@ -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);
/// <summary>
/// Whether the line from <paramref name="corner"/> to <paramref name="end"/> runs straight
/// back along the line from <paramref name="start"/> to <paramref name="corner"/>.
/// </summary>
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);
}
/// <summary>The SVG endpoint-to-center arc conversion (SVG 2 appendix B.2.4).</summary>
private void AddSvgArc(
Vector from,
+26
View File
@@ -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($"<path d='{path}'/>"));
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("<path d='M0 0H5H0'/>"));
var line = Assert.IsType<Line>(Assert.Single(result.Entities));
Assert.Equal(5, line.Length, 6);
}
[Fact]
+2 -1
View File
@@ -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`. `<style>` sheets are not read, so CSS-class hiding is not honored.
- 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). 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.
- 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 is still not imported as geometry, but it is not reported as skipped.
- 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