Inkscape to OpenSCAD converter v3

5585
Downloads
6718
Views
Published on June 15, 2012

Description

___UPDATE: added version 3 of Thing #24808___
___This is version 3 of Thing #24808___

Libre Graphics World has a nice write up by Alexandre Prokoudine of this extension at libregraphicsworld.org/blog/entry/inkscape-gets-openscad-converter

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.

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

___21 June 2012:___ Correct a typo in the extension. Would not affect content generated by Inkscape but might have affected SVG content generated elsewhere and loaded into Inkscape.

___9 May 2013:___ Modified the code to remove non alphanumeric characters from the strings used to generate the OpenSCAD module names. Some SVG paths had non alphanumeric characters in their path ids and those ids were being used to generate OpenSCAD module names. OpenSCAD doesn't allow that.

Instructions

___Downloading___

MAC 10.7 or later users, see the second note in the Notes section! This means Lion, Mt. Lion, and beyond.

Download the paths2openscad-2.zip file from this page. After unzipping that archive, you should have two files: paths2openscad.py and paths2openscad.inx .

___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___

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

2. OS X Lion (10.7) and Mt. Lion (10.8) users need a "patch" to allow Inkscape extensions to work correctly. This is a general Inkscape issue and not specific to this extension. At issue is the lack of a Python package, lxml, which Inkscape extensions use to read (parse) SVG files. Install the patch from code.google.com/p/eggbotcode/downloads/detail?name=EggBot2.2.2.r2.mpkg.zip . This will also give you Eggbot extensions, many of which I wrote. (An Eggbot is a cool pen plotter for plotting on Eggs.)
Tags
This Thing has no tags.
Report as inappropriate

You must be logged in to post a comment.

I am using inkscape 0.48 on Ubuntu 13.04; I have downloaded v3 of the perl script and inx file and placed it in ~/.config/inkscape/extensions. Every time I run the script I get an internal error. Running from the shell shows the following additional information. All other scripts work fine. Can you advise please?

terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct null not valid

That's an error from Inkscape and C++. The extension itself is Python code. If it was a problem with the extension code, you'd see a Python error and not a C++ error. Your issue is with Inkscape proper and you need to bring it up on an Inkscape forum. It may be that the extension is not properly installed (e.g., permissions) or some other problem. But this is not the extension itself. The extension doesn't even provide output which Inkscape reads back. Which is to say, it's not even possible for the extension to be providing incorrect output back to Inkscape.

Also, Ubuntu 13.04 is very new. It's not even clear if Inkscape 0.48 works correctly on Ubuntu 13.04. Another reason to brink this up in an Inkscape forum.

I can't use it, when I try on osx 10.7.5 running python 2.7.1, it fails and tells me to install the 'fantastic lxml wrapper'. And that, is effectively impossible for me. I googled for 20 minutes trying to figure it out and it's a no go. Has anybody here gotten this working using the above mentioned versions? Thanks.

Runs fine on OS X 10.7.5 for me with Inkscape 0.48. The version of Python which Inkscape runs is often/typically not the version you see from the command line. As to the missing lxml, that is a generic Inkscape installation issue. You would be best to post to the Inkscape users group.

Nice plugin, but I keep having inkscape crash on me when I am trying to use it on a particular SVG. Any Advice?

That's a bug in Inkscape then. Extensions do not run within Inkscape. Inkscape writes the SVG to a temporary file and then Inkscape launches a separate process -- concurrently runs another program as though that program is another user. That separate process runs Python and executes the extension which reads in the SVG and then writes it back out again. Inkscape waits for that separate process to finish and then reads back in the SVG output by the separate process. If Inkscape is then dying, then Inkscape has a bug of some sort. While it's possible that the extension is putting out SVG which isn't what Inkscape wants to see (and may even be bad SVG), Inkscape should not crash. Instead it should report an error message.

So, you may want to make sure that you are running a recent version of Inkscape. OTOH, this may be a new bug in Inkscape. Also, it could be that the extension is putting out incomplete SVG, but I'd have to see the input file fed to the extension by Inkscape to tell. (It's some intermediate form of the SVG file you loaded into Inkscape.) Armed with that input and if there's a problem in the extension, then I could fix that. However, the bug in Inkscape would still remain -- the bug that makes it crash when it sees extension output it doesn't like.

can it convert arc or circle to arc, not to poligon?

No. Largely in part because Inkscape itself tends to use cubics for much of what it does anyway.

I just want to note for Mac users to remove the leading tilde and slash and to check in Inkscape.app/Contents/Resources/extensions for your files.

For some reason because Inkscape is using X11, it save the file in the actual .app bundle. If I save it to "~/filename.scad", I haven't been able to find it, whereas "filename.scad" appears in the the bundle.

Thanks for posting this tip!

Alternatively, replace ~ with /Users/your-account-name/ to put it in your login directory or /Users/your-account-name/Desktop to put it on your Desktop.

At issue will be what does the HOME environment variable translate to in the process which Inkscape forks to run Python and the extension script. This can conceivably change with the X11 implementation (there are several for OS X), the Inkscape version (many), and maybe even your account's .profile file or other (hidden) login scripts. Hard to say. I'm a Mac user and I don't see this behavior on OS X 10.7.5 (Lion). But clearly YMWV (your mileage will vary).

The nesting works perfectly, great job! This converter will definitively be part of my tool chain from now on. Thank you very much!

Thanks for the feedback. It is helpful!

I may add support for arbitrary depth within a path. Handling nesting between paths and clipping within a path and between paths may be overkill AND would definitely introduce dependencies on add'l Python libraries as well as some C/C++ shared libraries. Fortunately, an Inkscape user can simply
rearrange their paths to deal with those issues: it doesn't have to be dealt with in the converter.

And I need to plant to look at Polymaker's path converter for possible inclusion.