Files
OpenNest/docs/svg-import-export.md
T
ajandClaude Opus 5.5 db18075bec 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>
2026-11-29 19:01:29 -05:00

6.8 KiB
Raw Blame History

SVG import and export

OpenNest.IO.Svg reads and writes SVG alongside DXF and DWG. Everything goes through CadImporter, so an SVG drawing behaves like any other import after loading. .svg is not in DrawingFileIndex: BOM imports still look for DXF and DWG only.

Scale

A DXF's numbers are taken as drawing units. An SVG's are not, so import converts to CadImportOptions.DrawingUnits (the nest's units in the desktop app) and never guesses.

The root element states a real-world size only when width or height uses an absolute unit: in, cm, mm, Q, pt or pc. Then:

  • With a viewBox, inches per user unit is the page size over the view box size. Mismatched aspect ratios fit uniformly, as browsers do, unless preserveAspectRatio starts with none.
  • Without one, a user unit is a CSS pixel, 1/96 inch.

Everything else does not count: a missing size, percentages and px or bare numbers. px is excluded on purpose, because a CSS pixel is 1/96 inch but Illustrator writes px at 72 per inch.

With no stated size, CadImportOptions.SvgUserUnitsPerInch supplies the scale, and without it the import throws InvalidDataException. A stated size always wins over that option. The desktop app asks with SvgScaleForm, which shows the size each choice produces; the console takes --svg-units-per-inch (--cad-units mm sets the drawing units) and MCP's import_dxf takes svgUnitsPerInch.

What import reads

Import takes path centerlines: path, rect (with rounded corners), circle, ellipse, line, polyline, polygon, g and use. Stroke widths and fills are ignored. Transforms are applied before an entity type is chosen, so a circle under a non-uniform scale becomes an ellipse.

  • 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.
  • 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. <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. 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

ExportProgram, ExportPlate and ExportEntities write one <g data-layer> per layer; the ExportEntities overload that takes bends adds a BEND group with each bend's line and data-bend-* attributes, plus its note as text for people. width and height are in the given units and match the viewBox, so one user unit is one drawing unit and the file needs no scale prompt. Arcs stay A commands. The plate outline and edge spacing go on PLATE. Plate export leaves out the rapids DXF draws between parts.

For laser software

SvgExportOptions.LaserSoftware writes a file to cut from directly in hobby laser software such as LightBurn, which makes one cut layer per stroke color and ignores data-layer. File > Export offers it as SVG for laser software, e.g. LightBurn (WinForms only; the Avalonia picker chooses a format by extension, and .svg stays the plain export).

  • CutAndMarksOnly leaves out what is never cut: PLATE, RAPID, DISPLAY, DISPLAY PERIMETER and TAB.
  • LaserLayerColors strokes from LightBurn's palette: CUT with its leads and COMMON LINE C00 black, SCRIBE C01 blue, BREAK C02 red, and any other layer the next color from C03 in order of first appearance.

Units and arcs are written as in the default export, so the file still needs no scale prompt. The palette values are not yet checked against a LightBurn install.

CAD Converter export

Export... writes the current drawing with CadExporter: DXF R14 to 2018, DWG 2004 to 2018 or SVG. Entities keep their layers. Bends go on BEND as CENTER lines with an MText note such as UP 90° R 0.06, the form SolidWorksBendDetector reads, so importing the file again gives back the same bends; a line picked as a bend is written only as that bend. Etch marks go on ETCH, which import drops and makes again from the bends. Files before 2007 write non-ASCII note characters as \U+XXXX escapes. ACadSharp cannot write DWG 2007, and its DWG R14 and 2000 output loses entities and layers, so those are not offered.