diff --git a/OpenNest.IO/SvgReader.cs b/OpenNest.IO/SvgReader.cs
index 018fd7ba..6600ea5d 100644
--- a/OpenNest.IO/SvgReader.cs
+++ b/OpenNest.IO/SvgReader.cs
@@ -48,6 +48,13 @@ namespace OpenNest.IO
private readonly XElement root;
private readonly double tolerance;
+
+ ///
+ /// 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.
+ ///
+ private double ClosingGap => tolerance * 20;
private readonly XNamespace svgNamespace;
private readonly Dictionary elementsById;
private readonly Dictionary layers = new Dictionary(
@@ -55,6 +62,7 @@ namespace OpenNest.IO
);
private readonly Dictionary skipped = new Dictionary();
private int droppedLoops;
+ private int closedGaps;
private readonly HashSet captions = new HashSet();
private readonly List lines = new List();
private readonly List arcs = new List();
@@ -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;
diff --git a/OpenNest.Tests/IO/SvgImportTests.cs b/OpenNest.Tests/IO/SvgImportTests.cs
index 26281ce4..e66a2315 100644
--- a/OpenNest.Tests/IO/SvgImportTests.cs
+++ b/OpenNest.Tests/IO/SvgImportTests.cs
@@ -151,6 +151,34 @@ namespace OpenNest.Tests.IO
Assert.Equal(5, line.Length, 6);
}
+ [Fact]
+ public void ClosesAnOutlineThatStopsJustShortOfItsStart()
+ {
+ // Boxes.py with burn 0.1 leaves out the last corner: the outline ends 0.14 mm short.
+ var mm = "width='100mm' height='100mm' viewBox='0 0 100 100'";
+ var result = Import(
+ Svg("", mm),
+ new CadImportOptions { DrawingUnits = Units.Millimeters });
+
+ Assert.All(ShapeBuilder.GetShapes(result.Entities), s => Assert.True(s.IsClosed()));
+ Assert.Equal(1, PartBodies.Find(result.Entities).Count);
+ Assert.Contains(result.Warnings, w => w.StartsWith("Closed 1 outline"));
+ }
+
+ [Theory]
+ // A gap wider than 0.5 mm is drawn that way on purpose.
+ [InlineData("M1 0H10V10H0V1")]
+ // Too few segments to be an outline.
+ [InlineData("M0 0H10V0.1")]
+ public void LeavesOtherOpenPathsOpen(string path)
+ {
+ var mm = "width='100mm' height='100mm' viewBox='0 0 100 100'";
+ var result = Import(Svg($"", mm), new CadImportOptions { DrawingUnits = Units.Millimeters });
+
+ Assert.False(Assert.Single(ShapeBuilder.GetShapes(result.Entities)).IsClosed());
+ Assert.Empty(result.Warnings);
+ }
+
[Fact]
public void ConvertsToMillimeterDrawings()
{
diff --git a/README.md b/README.md
index 27b02e69..8ab67ae4 100644
--- a/README.md
+++ b/README.md
@@ -119,7 +119,7 @@ Default compares the built-in Irregular, Rectangles, Circles and Raster engines
2. Open the SVG in the CAD Converter. The file states its size in millimeters, so there is no scale prompt.
3. Click **Separate** to split the sheet into one drawing per piece (named from the design's labels where it has them), then delete the 100 mm burn-test strip.
-A burn above 0 enlarges every part and leaves loops at inside corners; import drops the loops and warns.
+A burn above 0 enlarges every part, leaves loops at inside corners and stops some outlines just short of closing; import drops the loops, closes those outlines and warns.
## Keyboard Shortcuts
diff --git a/docs/svg-import-export.md b/docs/svg-import-export.md
index a6688192..83284b4e 100644
--- a/docs/svg-import-export.md
+++ b/docs/svg-import-export.md
@@ -24,6 +24,7 @@ Import takes path centerlines: `path`, `rect` (with rounded corners), `circle`,
- 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.
+- A subpath with no `Z` that draws at least three segments and ends within 0.02 in (0.5 mm) of its start is closed with a line, with a warning. Boxes.py leaves out an outline's last corner when its burn is above 0 (a gap of burn × √2), and an open outline could not hold its part's holes when the file is separated. A wider gap, or a shorter path, stays open.
- Elements with `display` none or `visibility` hidden or collapse are skipped, as are definition elements such as `defs` and `clipPath`. `