Inkscape to OpenSCAD converter
Description
UPDATED - paths2openscad-2.zip
See Thing 25036 for version 2 and 3
thingiverse.com/thing:25036
This Thing is an Inkscape extension to export Inkscape paths to OpenSCAD. There has been at least one prior Thing attempting this, Thing #9376. However, it only handled a small subset of SVG. This extension handles SVG arcs, clones, circles, ellipses, groups, lines, paths, polygons, polylines, rects, and splines. It also follows document transforms as well as viewports. Either an entire document or just the selected portions of a document are processed.
SVG text must first be converted to a path within Inkscape using Inkscape's "Path > Object to Path" menu item.
Note that another approach to importing SVG into OpenSCAD is to save the SVG to DXF from within Inkscape. Then use OpenSCAD's import() function to import the DXF. [Older versions of OpenSCAD used import_dxf().]
Much of the core code in this extension is derived from work done by myself and others while developing the Inkscape driver for the Eggbot.
9 May 2013: Modified the code to remove non alphanumeric characters from the names of the generated OpenSCAD modules.
15 June 2012: Added support for a single level of polygon nesting. I.e., subtract from a polygon, the polygons contained within it AND from the same Inkscape path. This works well for most fonts. You can tell Inkscape to combine multiple polygons into a single path by selecting the polygons and then using "Path > Combine". (You do not need to do this for text converted to a path: Inkscape already does the proper combining.)
15 June 2012: Corrected Windows issue with handling of Unix-style file paths.
Instructions
Download the paths2openscad-2.zip file from this page. After unzipping that archive, you should have two files: paths2openscad.py and paths2openscad.inx .
The paths2openscad.zip file is the original release of the extension. It has been superseded by paths2openscad-2.zip.
Installing
Once you have downloaded paths2openscad.zip, open the zip file and place its contents in your local Inkscape extension folder,
~/.config/inkscape/extensions/
on Linux and OS X. On Windows systems, you can place them directly into the Inkscape extension directory. For example, if Inkscape is installed in C:/Program Files/Inkscape then the extension directory will be C:/Program Files/Inkscape/share/extensions.
Once you have placed the two files in the proper directory, exit and then restart Inkscape. The extension should appear under the "Extensions > Generate from Path" menu as "Paths to OpenSCAD".
Using
To use the extension,
0. You may find it helpful to set your document units to millimeters and the document dimension to millimeters as well as a reasonable document size (e.g., 100 x 100 mm). Do this with the "File > Document Properties" menu item. The document units is in the upper part of the dialog box while the document dimensions are near the bottom of that same box.
1. To see what the extension sees, set Inkscape's display mode to "outline". This is done with the "Display Mode" item of the "View" menu.
2. Next ensure that within Inkscape, the desired objects have been converted to paths. Use the "Path > Object to Path" menu item to convert objects such as text to paths.
3. Select the desired objects to generate an OpenSCAD program for. If you wish to generate a program for the entire document, then select nothing (i.e., "Edit > Deselect").
4. From the "Extensions" menu, select "Generate from Path > Paths to OpenSCAD".
5. In the dialog window which appears, enter the name of the output file to generate. (Windows users: the "~" notation indicates that the file goes into your "Document and Settings" folder.) You can also enter a extrusion height and a smoothing parameter. For most purposes, a smoothing parameter of 0.2 is sufficient. If your paths have lots of tight, twisty curves which are described using arcs, circles, ellipses, or other SVG elements which aren't collections of straight line segments, then you may want to use a smaller value.
6. Click the "Apply" button. A pop-up window will appear while the extension is running and will then disappear when it is done running. After the extension is done running, you can click the "Close" button to dismiss the extension's dialog window. If an error occurs, you will see an error window appear when the extension is running.
7. From OpenSCAD, open the output file you generated with the extension and render it.
Limitations
If you wish to build solids involving subtracting one polygon from another, be sure to combine the two polygons into a single path: select the polygons and then combine them with "Path > Combine". Only a single level of nesting is handled at present. You do not need to do this for text converted to a path: Inkscape already does the proper combining.
I have seen a few complex, but legitimate polygons, compile and render with F5 but not with F6 in OpenSCAD. They instead trigger some internal error detection code in OpenSCAD.
Notes
The sample .svg, .stl, and .scad files serve as examples: you do not need to download them to use this Inkscape extension. You only need the .zip file.
You must be logged in to post a comment.
Very clever, thank you for sharing! Works fine, though it didn't want to export to "~" on my windows machine for some reason, so I used "D:" instead. Having it export to the same folder and with the same name as the source file by default, would save time locating and moving files around.
The polygon issue with not rendering "o"'s is a downside though. Is there any way to detect this, and export the holes as a csg difference() or something? One could do it manually, but then dxf would seem like the faster option anyway.
This needs to be submitted to the Inkscape codebase, so we ca
n have .scad export as default!
Updated to handle rendering of font characters such as "o", "8", etc. Also fixed the issue with handling Unix style file names on Windows.
You can pull the new paths2openscad-2.zip file for the update.
For things like the letter "o", a simple solution may well be to track the bounding box of each polygon. Then, when one polygon is entirely contained within another one, assume that a difference() is called for. The code needs to be a little smarter than that so that it can handle deeper nestings (e.g., a bunch of concentric circles). Should be fairly easy to implement and test. Once I'm done, I'll put a revised version of the extension up here.
Hmmm... Must be a windows or python version thing as the ~ works on my XP SP3 system (and I'm using the Python routine intended to deal with the issue in a cross platform fashion). I don't know if I can detect the directory containing the original document or not: the extensions are a separate process running python which is then fed over a pipe (or via a temp file) the current version of the SVG document. It may be that Inkscape puts the document name
&
amp; path into the document. If so, then I can grab it and use it. Note that the extension's dialog box itself cannot do any extension specific processing. The dialog is described via a static XML document which Inkscape loads when it starts running and then the main Inkscape process builds the dialog from that XML description. No system whereby code running with/for the dialog can be specified. Thus, it's not possible to within the dialog to present a document specific file name. Would have to be a check box along the lines of "save the file with a .scad extension in the same folder as the source document".
It is possible to detect the topology of the objects. For instance, intersect lines with them and count the crossings. (Indeed, I wrote the hatch and cross hatch extensions which get used with Inkscape.)
As to submitting to Inkscape, I'm fine with someone doing that. However, I'm not myself in
terested in maintaining it for Inkscape as that involves using Bazaar and I'm not interested in using that particular source control system in my free time. (The Inkscape folks have asked for me to contribute a couple of extensions: I've always told them that they can have 'em but then they want me
to maintain them in Bazaar.)
My DXF export plugin (http://www.thingiverse.com/thi... automatically convert objects (such as text) to paths for you. You are welcome to pull that code into this project if you like.


Fantastically useful program!!!
I do encounter names like:
module poly_path7776-1-8-7-3-9-8-4-9-8(h)
which my copy of OpenSCAD chokes on. I solve this by taking the .scad file into Notepad++ and doing a regex replace:
Find What: ([0-9])(-)([0-9])
Replace With: \1\3
I have to run it twice, but it removes the dashes and OpenSCAD seems happy with the results.
Thanks again!
Interesting. Was the SVG originated in Inkscape or imported to Inkscape? Either way, I'll look into dealing with this in the extension -- pretty simple to handle and I should have the first time round. However, I'm curious if this signifies that Inkscape has changed its path naming conventions or if it's just a side effect of SVG from another application.
Thanks,
Dan