fix(io): close SVG outlines that stop just short of their start

Boxes.py with a burn above 0 leaves out an outline's last corner, so
the curved BreadBox sides ended 0.14 mm open and Separate split their
slots and edges into stray parts. Import now closes a subpath of three
or more segments that ends within 0.5 mm of its start, and warns.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-11-29 19:01:29 -05:00
1 parent 3b19fda767
commit db18075bec
4 files changed
+60 -1

No files matched your search

+30
View File
@@ -48,6 +48,13 @@ namespace OpenNest.IO
private readonly XElement root;
private readonly double tolerance;
/// <summary>
/// The widest gap between an open outline's end and its start that import closes: 0.02 in
/// (0.5 mm), twenty times the curve tolerance, past Boxes.py's burn gap of burn times the
/// square root of 2.
/// </summary>
private double ClosingGap => tolerance * 20;
private readonly XNamespace svgNamespace;
private readonly Dictionary<string, XElement> elementsById;
private readonly Dictionary<string, Layer> layers = new Dictionary<string, Layer>(
@@ -55,6 +62,7 @@ namespace OpenNest.IO
);
private readonly Dictionary<string, int> skipped = new Dictionary<string, int>();
private int droppedLoops;
private int closedGaps;
private readonly HashSet<XElement> captions = new HashSet<XElement>();
private readonly List<Line> lines = new List<Line>();
private readonly List<Arc> arcs = new List<Arc>();
@@ -252,6 +260,12 @@ namespace OpenNest.IO
+ "one at each inside corner when its burn is above 0, which also enlarges every part: "
+ "set burn to 0, since OpenNest compensates for kerf itself."
);
if (closedGaps > 0)
warnings.Add(
$"Closed {closedGaps} outline(s) that stopped short of their start by less than "
+ $"{ClosingGap:0.###} drawing units. Boxes.py leaves this gap "
+ "when its burn is above 0."
);
return new DxfImportResult
{
@@ -666,6 +680,20 @@ namespace OpenNest.IO
run = null;
}
// An open subpath that draws an outline and stops just short of its start is closed:
// Boxes.py leaves out the last corner when its burn is above 0, and an open outline
// cannot hold its part's holes when the file is separated.
void CloseShortfall()
{
if (drawn + (run == null ? 0 : 1) < 3)
return;
var gap = t.Apply(current).DistanceTo(t.Apply(start));
if (gap < Tolerance.Epsilon || gap > ClosingGap)
return;
Straight(current, start);
closedGaps++;
}
void EndSubpath()
{
EndRun();
@@ -718,6 +746,7 @@ namespace OpenNest.IO
{
if (!scanner.HasCommand && !scanner.HasNumber)
{
CloseShortfall();
EndSubpath();
break;
}
@@ -742,6 +771,7 @@ namespace OpenNest.IO
switch (char.ToUpperInvariant(command))
{
case 'M':
CloseShortfall();
EndSubpath();
current = start = Next();
break;