mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-01 23:41:34 -05:00
fix(io): drop SVG curve loops that pinch off an outline
A Bezier that ends where it starts, inside a subpath that draws anything else, imported as a tiny circle and became a hole. Boxes.py draws one at every inside corner when its burn is above 0, so a SlidingDrawer came in with 248 fake holes. Import now drops such loops and warns, asking for burn 0 since the burn also enlarges every part. A loop that is its subpath's whole drawing still imports. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
40defbeeb4
commit
6ee5633212
3 files changed
+94
-15
No files matched your search
+69
-15
@@ -54,6 +54,7 @@ namespace OpenNest.IO
|
||||
StringComparer.OrdinalIgnoreCase
|
||||
);
|
||||
private readonly Dictionary<string, int> skipped = new Dictionary<string, int>();
|
||||
private int droppedLoops;
|
||||
private readonly List<Line> lines = new List<Line>();
|
||||
private readonly List<Arc> arcs = new List<Arc>();
|
||||
private readonly List<Circle> circles = new List<Circle>();
|
||||
@@ -240,16 +241,24 @@ namespace OpenNest.IO
|
||||
entities.AddRange(lines);
|
||||
entities.AddRange(arcs);
|
||||
|
||||
var warnings = skipped
|
||||
.OrderBy(s => s.Key, StringComparer.Ordinal)
|
||||
.Select(s => $"Skipped {s.Value} <{s.Key}> element(s); convert them to paths to import them.")
|
||||
.ToList();
|
||||
if (droppedLoops > 0)
|
||||
warnings.Add(
|
||||
$"Dropped {droppedLoops} curve loop(s) that close on their own outline. Boxes.py draws "
|
||||
+ "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."
|
||||
);
|
||||
|
||||
return new DxfImportResult
|
||||
{
|
||||
Entities = entities,
|
||||
RawEntities = raw,
|
||||
Bends = bends,
|
||||
Labels = labels,
|
||||
Warnings = skipped
|
||||
.OrderBy(s => s.Key, StringComparer.Ordinal)
|
||||
.Select(s => $"Skipped {s.Value} <{s.Key}> element(s); convert them to paths to import them.")
|
||||
.ToList(),
|
||||
Warnings = warnings,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -630,10 +639,54 @@ namespace OpenNest.IO
|
||||
var previous = '\0';
|
||||
var command = '\0';
|
||||
|
||||
// A curve that ends where it starts is a loop. Inside an outline that draws anything
|
||||
// else it pinches off at one point, which no cut can follow: Boxes.py draws one at each
|
||||
// inside corner to compensate for its burn (kerf), and import would make each a hole.
|
||||
// It is kept only when it is all its subpath draws.
|
||||
var drawn = 0;
|
||||
var loops = new List<(Vector P0, Vector C1, Vector C2, Vector P1)>();
|
||||
|
||||
void EndSubpath()
|
||||
{
|
||||
if (drawn > 0)
|
||||
droppedLoops += loops.Count;
|
||||
else
|
||||
foreach (var loop in loops)
|
||||
AddCubic(loop.P0, loop.C1, loop.C2, loop.P1, t, layer);
|
||||
drawn = 0;
|
||||
loops.Clear();
|
||||
}
|
||||
|
||||
bool Closes(Vector from, Vector to) => t.Apply(from).DistanceTo(t.Apply(to)) < Tolerance.Epsilon;
|
||||
|
||||
void Curve(Vector p0, Vector c1, Vector c2, Vector p1)
|
||||
{
|
||||
if (Closes(p0, p1))
|
||||
loops.Add((p0, c1, c2, p1));
|
||||
else
|
||||
{
|
||||
AddCubic(p0, c1, c2, p1, t, layer);
|
||||
drawn++;
|
||||
}
|
||||
}
|
||||
|
||||
void Quadratic(Vector p0, Vector c, Vector p1) =>
|
||||
Curve(p0, p0 + (c - p0) * (2.0 / 3), p1 + (c - p1) * (2.0 / 3), p1);
|
||||
|
||||
void Straight(Vector from, Vector to)
|
||||
{
|
||||
AddLine(from, to, t, layer);
|
||||
if (!Closes(from, to))
|
||||
drawn++;
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (!scanner.HasCommand && !scanner.HasNumber)
|
||||
{
|
||||
EndSubpath();
|
||||
break;
|
||||
}
|
||||
|
||||
if (scanner.HasCommand)
|
||||
command = scanner.ReadCommand();
|
||||
@@ -655,18 +708,20 @@ namespace OpenNest.IO
|
||||
switch (char.ToUpperInvariant(command))
|
||||
{
|
||||
case 'M':
|
||||
EndSubpath();
|
||||
current = start = Next();
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
AddLine(current, start, t, layer);
|
||||
Straight(current, start);
|
||||
EndSubpath();
|
||||
current = start;
|
||||
break;
|
||||
|
||||
case 'L':
|
||||
{
|
||||
var end = Next();
|
||||
AddLine(current, end, t, layer);
|
||||
Straight(current, end);
|
||||
current = end;
|
||||
break;
|
||||
}
|
||||
@@ -674,7 +729,7 @@ namespace OpenNest.IO
|
||||
case 'H':
|
||||
{
|
||||
var end = new Vector(scanner.ReadNumber() + origin.X, current.Y);
|
||||
AddLine(current, end, t, layer);
|
||||
Straight(current, end);
|
||||
current = end;
|
||||
break;
|
||||
}
|
||||
@@ -682,7 +737,7 @@ namespace OpenNest.IO
|
||||
case 'V':
|
||||
{
|
||||
var end = new Vector(current.X, scanner.ReadNumber() + origin.Y);
|
||||
AddLine(current, end, t, layer);
|
||||
Straight(current, end);
|
||||
current = end;
|
||||
break;
|
||||
}
|
||||
@@ -692,7 +747,7 @@ namespace OpenNest.IO
|
||||
var c1 = Next();
|
||||
control = Next();
|
||||
var end = Next();
|
||||
AddCubic(current, c1, control, end, t, layer);
|
||||
Curve(current, c1, control, end);
|
||||
current = end;
|
||||
break;
|
||||
}
|
||||
@@ -704,7 +759,7 @@ namespace OpenNest.IO
|
||||
: current;
|
||||
control = Next();
|
||||
var end = Next();
|
||||
AddCubic(current, c1, control, end, t, layer);
|
||||
Curve(current, c1, control, end);
|
||||
current = end;
|
||||
break;
|
||||
}
|
||||
@@ -713,7 +768,7 @@ namespace OpenNest.IO
|
||||
{
|
||||
control = Next();
|
||||
var end = Next();
|
||||
AddQuadratic(current, control, end, t, layer);
|
||||
Quadratic(current, control, end);
|
||||
current = end;
|
||||
break;
|
||||
}
|
||||
@@ -724,7 +779,7 @@ namespace OpenNest.IO
|
||||
? current * 2 - control
|
||||
: current;
|
||||
var end = Next();
|
||||
AddQuadratic(current, control, end, t, layer);
|
||||
Quadratic(current, control, end);
|
||||
current = end;
|
||||
break;
|
||||
}
|
||||
@@ -738,6 +793,8 @@ namespace OpenNest.IO
|
||||
var sweep = scanner.ReadFlag();
|
||||
var end = Next();
|
||||
AddSvgArc(current, rx, ry, rotation, large, sweep, end, t, layer);
|
||||
if (!Closes(current, end))
|
||||
drawn++;
|
||||
current = end;
|
||||
break;
|
||||
}
|
||||
@@ -758,9 +815,6 @@ namespace OpenNest.IO
|
||||
lines.Add(Tag(new Line(start, end), layer));
|
||||
}
|
||||
|
||||
private void AddQuadratic(Vector p0, Vector c, Vector p1, SvgTransform t, Layer layer) =>
|
||||
AddCubic(p0, p0 + (c - p0) * (2.0 / 3), p1 + (c - p1) * (2.0 / 3), p1, t, layer);
|
||||
|
||||
/// <summary>
|
||||
/// Samples the curve within half the tolerance and fits arcs and lines through the
|
||||
/// samples within the other half, so the result stays within the tolerance.
|
||||
|
||||
@@ -89,6 +89,30 @@ namespace OpenNest.Tests.IO
|
||||
Assert.Equal(2, wall.Right, 6);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DropsCurveLoopsThatPinchOffAnOutline()
|
||||
{
|
||||
// Boxes.py's burn draws inside corners as curves that end where they start.
|
||||
var result = Import(Svg(
|
||||
"<path d='M0 0H6V5C6 4.9 5.9 5 6 5H10V8Q9 9 10 8H0Z'/>"));
|
||||
|
||||
Assert.DoesNotContain(result.Entities, e => e is Circle);
|
||||
Assert.DoesNotContain(result.Entities, e => e is Arc);
|
||||
var shape = Assert.Single(ShapeBuilder.GetShapes(result.Entities));
|
||||
Assert.True(shape.IsClosed());
|
||||
var warning = Assert.Single(result.Warnings);
|
||||
Assert.StartsWith("Dropped 2 curve loop(s)", warning);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void KeepsACurveLoopThatIsItsWholeSubpath()
|
||||
{
|
||||
var result = Import(Svg("<path d='M0 0C4 -4 4 4 0 0ZM5 5H6V6H5Z'/>"));
|
||||
|
||||
Assert.Empty(result.Warnings);
|
||||
Assert.Equal(2, ShapeBuilder.GetShapes(result.Entities).Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GroupTextNamesSeparatedParts()
|
||||
{
|
||||
|
||||
@@ -22,6 +22,7 @@ Import takes path centerlines: `path`, `rect` (with rounded corners), `circle`,
|
||||
- SVG's Y axis points down, so import negates Y. The view box origin is not subtracted, which keeps OpenNest's own exports at their original coordinates.
|
||||
- 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.
|
||||
- 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.
|
||||
|
||||
Reference in new issue
Block a user