Loading

Inkscape to OpenSCAD converter v6

by dnewman, published

Inkscape to OpenSCAD converter v6 by dnewman Jun 15, 2012

Description

___UPDATE: added version 6 of Thing #24808___
___This is version 6 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.
___29 May 2013:___ Corrected an issue whereby the converted could exit with an error when attempting to warn about images embedded in the SVG file or Inkscape drawing. Images are not allowed and the converter cannot convert them. However, it should gracefully warn about the issue and exit normally.
___30 May 2013:___ Fixed a bug in the v4 version.
___16 Aug 2013:___ Possible workaround to Inkscape bug experienced by some users, "terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct null not valid"

Recent Comments

view all
You should be able to use `PYTHONPATH` to include the extensions directory when using the code. If not you may want to create an issue at github.com/l0b0/paths2openscad/issues
I am responsible for the GitHub repository at github.com/l0b0/paths2openscad - you can contribute suggestions and issues there. I'm not sure why you think the link should be removed, especially since some minor development still happens there.
For the love of all things 3D; if you have no connection to the github repo, please remove the link.

More from Tools

view more

Makes

Liked By

view all

License

GNU - GPL
Inkscape to OpenSCAD converter v6 by dnewman is licensed under the GNU - GPL license.

Give a Shout Out

If you print this Thing and display it in public proudly give attribution by printing and displaying this tag. Print Thing Tag

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.3.1.r4s.zip . This will also give you Eggbot extensions, many of which I wrote. (An Eggbot is a cool pen plotter for plotting on Eggs.)
The link you have to eggbot extensions for OSX is for a depreciated
version (so a warning appears). The newer version is code.google.com/p/eggbotcode/downloads/detail?name=EggBot2.3.1.r4s.zip&;can=2&q=eggbot
This extension looks great so far. I have not been having luck with the DXF export after getting it to work once.One thing I would suggest is putting some instructions on how to install it on the GitHub site. It took me a while to first realize the "plugins" folder is not where it goes, and then to realize the "extensions" folder is under "share". Then I had to figure out that it can't be in a sub-folder since I cloned from GitHub. I had to copy it to the "shareextensions" folder itself to get it working.Once I did all that, it works!
3 days ago - Modified 3 days ago
l0b0 - in reply to noisygecko
You should be able to use `PYTHONPATH` to include the extensions directory when using the code. If not you may want to create an issue at github.com/l0b0/paths2openscad/issues
I have no connection to the github repo. You should contact whomever is responsible for it.As to installation directions, they are part of this Thing here. There's the Description section which appears when you first visit any Thing in thingiverse. But there's also a Instructions section. That section for this Thing has fairly complete directions including where to place the extension. Since I do not own, control, or otherwise have any connection to the github repo, I cannot place instructions there. Moreover, if instructions were there that would be a maintenance problem: there would then be two sets of instructions to keep in sync.
For the love of all things 3D; if you have no connection to the github repo, please remove the link.
3 days ago - Modified 3 days ago
l0b0 - in reply to bioradio
I am responsible for the GitHub repository at github.com/l0b0/paths2openscad - you can contribute suggestions and issues there. I'm not sure why you think the link should be removed, especially since some minor development still happens there.
Hi, I've had a scaling issue with the plugin; maybe you can spot where I went wrong from my description or maybe it's a bug. See here: thingiverse.com/thing:169068

Basically, the resulting SCAD seems to be almost exactly 80% of the proper scaling. The SCAD source includes scale factors of 25.4/90, so apparently the coordinates exported from Inkscape are expected to have 90 units to the inch? This would seemingly need to be 72 units / inch to give me the right size SCAD forms. What do you think?
You need to fix your SVG to have units or correct units. SVG files should specify the units used (cm, ft, in, m, mm, pc, or pt) and if no units are specified then user units are assumed as per the SVG specification. Moreover 1 user unit is defined to be 1px. Further, the conversion from px to mm is 90px to 25.4 mm. (More precisely, 90px is defined to be 1inch.) So, if things are scaled improperly, the issue is likely with your SVG and you need to correct the usage of units in your SVG file.

P.S. Technically, if no units are specified then the measurement defaults to "user" units. But 1px is defined to be 1 user unit so you can use them interchangeably.
I'll look into the SVG. Do you have thoughts on why I would end up with 72 units / inch being the correct scale instead of 90?
Whatever software you used to create the SVG must have decided to use 72 units / inch. It should have, using standardized SVG, specified that it was using units of printer's points, pt, which are 72 pt to 1 inch. Looks as though it didn't, or there's a bug in the openscad converter which isn't handling the unit specification correctly.
Hola, si yo lo convierto en vector primero, lo hago como dice el video tutorial de you tobe,de como pasar jpg a stl y no lo encuentro despues nose donde lo archiva.. saludos y gracias por responder
Hola, instale Inkscape y cargue la extension de Paths to OpenSCAD, pero cuando hago todo el procedimiento para pasar JPG a STL, no se donde va el archivo, no lo encuentro alguin me puede ayudar??
The converter only operates on SVG vector elements. It does not operate on images embedded in SVG. You need to convert the images to vector graphics first. Inkscape does have a bitmap trace function which might be able to do that, but it also leaves the bitmap in the file and you have to then remove the embedded bitmap.
Running OSX 10.8.5 but when I select the Paths to OpenSCAD extension, I see a quick popup window that it's working, but no file is created. Seached entire drive with Finder JIC it was put somewhere else. Any ideas?
Works fine for me on OS X 10.7 and 10.9. When you run the extension, there's a popup window entitled "Paths to OpenSCAD". In that window, you select an output file name. (Default is a file in your login directory and with the extension .scad.) The conversion isn't done until you click the pop-up window's "Apply" button. If you're not getting the initial pop-up window, then something is wrong with your Inkscape or Python installation. If you get that window but no output file when you click the Apply button, then you may have a permissions problem.
Is there a summary somewhere of how this approach compares to the Export DXF method?
No. But the obvious difference is that this approach puts out a format from which a 3D model in STL, OFF, DXF, or CSG can be saved: open the .scad file in OpenSCAD, render it, and then write as a 3D model. If you produce DXF from Inkscape, then you only have a 2D rendering in DXF. You may then be able to extrude that in SolidWorks or similar.
Oh, you can't use OpenSCAD to extrude the imported DXF? (I went ahead and used your code for my purpose already and it worked very nicely; thank you!)
You can try: I've not had much luck. The Save As... DXF in Inkscape for me at least produces DXF which OpenSCAD does not support. I tried a simple circle and output it from Inkscape with all four combinations of the two DXF options. None of the four worked with OpenSCAD.
I see; would be helpful to include this information in the description. Thanks again!
Hey. Just wanted to say that you are my hero. I've been trying to model/print out a project for about a year now without much luck. I read about your script and thought, what the heck. It took a little while, but I'm in the middle of printing it (successfully) now.

Of course, now that the 'simple project' worked, I'm trying a more complex version. (SCad's been running for about 2 hours now and I'm ~10% done:)

Seriously, thank you for putting this out there. And thanks from my 3d animation friends b/c I'll stop bugging them!
When I run this I get:

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

Inkscape 0.48, version 5 of your tool.
That's an Inkscape bug which depends upon what platform you are using. I myself do not see it on OS X 10.7.5 nor on Windows 7 with Inkscape 0.48. Consequently, I cannot diagnose it. However, stwice a while back did some spelunking and determined that on his/her computer a cosmetic change in the .inx file resolved the issue. I've uploaded a v6 that implements that change at the expense of not presenting quite as nice of a pop up window. (The .inx file describes to Inkscape what the window should look like. Inkscape creates the window, not the extension. And, it seems, that Inkscape still has some bugs in this department -- extension dialogs.)
v6 Works Great. I'm using ubuntu 12.04 so it looks like a linux version issue. Thanks for the quick fix! I love this tool.
Thanks for the follow up -- it is appreciated. Especially in as much as I simply could not reproduce this one myself.
Is there a public repository for this? I'd love to keep up to date with the code rather than the "thing", and maybe clone to help out.
No public repo. Feel free to make one yourself and post back.
Thanks. And it's now noted in line 3 of the description.
I got the below message when I open Inkscape, opened one of the samples, and started the extension.

"""
charles@charles-lenovot420:~$ inkscape
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct null not valid
Emergency save activated!
Emergency save completed. Inkscape will close now.
If you can reproduce this crash, please file a bug at inkscape.org
with a detailed description of the steps leading to the crash, so we can fix it.
Aborted (core dumped)
charles@charles-lenovot420:~$

"""
Charles, another user tracked this down to a known (via google) issue with Inkscape and .inx file. v6 appears to remedy this on Ubuntu 12.04 at least. (I never had any luck reproducing it myself on OS X nor Windows.)
Charles,

1. I found an error in a fix I uploaded last night. So, in a few minutes there will be a fix to the fix.

2. As to Inkscape itself aborting, that suggests a bug in Inkscape. The extension is merely a Python script which Inkscape launches. It reads some data from Inkscape, does its work, writes its own output file (which Inkscape doesn't know or care about), and then exits. It passes no information back to Inkscape. If Inkscape is then crashing, that suggests a bug in Inkscape, possibly associated with running extensions but outside the control of the extension. (If this is a Mac with Lion, there's a known bug in Inkscape 0.48.x with extensions. However, your prompt of "lenovot420" suggests otherwise.)

Thanks for the report,
Dan
Using Mac 10.6.8 + inkscape 0.48.2, I get this error when using the extension:

Traceback (most recent call last):

File "http://paths2openscad.py", line 912, in <module>

e.affect()

File "/Applications/Inkscape.app/Contents/Resources/extensions/http://inkex.py", line 215, in affect

self.effect()

File "http://paths2openscad.py", line 869, in effect

self.recursivelyTraverseSvg( [self.selected[id]], transform )

File "http://paths2openscad.py", line 781, in recursivelyTraverseSvg

if not self.warnings.has_key( 'image' ):

AttributeError: OpenSCAD instance has no attribute 'warnings'</module>
Thanks for posting that. I'll fix it. However, realize that the underlying problem is that you tried to process an Inkscape drawing with an image in it. The converter was intentionally aborting the operation. It then encountered an error as it was trying to tell you why it was aborting the operation. Had it successfully told you what the problem was, it still would have aborted. It just would have done so gracefully.

So, even when I fix this you still will be unable to process that Inkscape drawing or SVG file. You have to remove the image from it. Either that, or put the image in another layer and deactivate that layer. Or only select the part of the drawing you want converted, omitting the image.
Thanks for the explanation. Yes, there was an image, as the svg was a traced bitmap. Out of curiosity, why is it impossible to ignore images when creating the output?
They can be ignored. What's not possible is for the extension to read your mind and know if you wanted it ignored or not.
OpenSCAD doesn't do images, so why would it ever be useful to include them in the output?
0. OpenSCAD doesn't do SVG (either)1. SVG does do images.
2. The point behind this extension is to convert SVG to OpenSCAD
Thus people may incorrectly assume that this extension converts images in their SVG document to OpenSCAD.

Humans are good at learning. The extension leaves it to humans to figure out what to do with their SVG elements that cannot be handled by the extension. For example, they can use the "bitmap trace" capabilities of Inkscape to convert the image to SVG paths and then convert those paths to OpenSCAD. Or humans can decide that the bitmap was to be ignored and remove it from the document or otherwise hide it.
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 had the same problem, but finally found a solution after some googling: inkscape does not seem to like empty _param elements in the .inx extension descriptor. After removing the empty _param element with name="footer" the extension works fine.
Thanks for pointing this out. I cannot reproduce the problem on OS X 10.7.5 nor Windows 7. But I've gone ahead and posted v6 with a modified .inx file. Thanks.
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.
dnewman - in reply to
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.
Ok, thanks for the help and if you want the plugin to work with Inkscape 0.48 on mac 10.7.5 then this patch mentioned below worked for me. I removed Inkscape installation just to be sure, re-installed from the 'snowlion-universal' dmg then installed the eggbot patch. Now it works. Thank you.

I have written a workaround patch and made it available for download (as part of the Eggbot extensions for Inkscape), for anyone needs a short-term workaround for this issue-- that is, before an official solution is released by the Inkscape developers.

You can download it here: code.google.com/p/eggbotcode/downloads/detail?name=EggBot2.2.2.r2.mpkg.zip

Full disclosure:
A. This is a totally unofficial hack, not endorsed in any way by the Inkscape developers. :)

B. The download is a standard Mac installer package for the Eggbot extensions for Inkscape, which installs the following software:

1) The Eggbot extensions for Inkscape, a set of Inkscape Extensions for creating drawings for and operating an Eggbot,
2) The Hershey Text extension for Inkscape, a "render" extension for drawing stroke-based text, and
3) The patch to Inkscape.app, for version 0.48 and newer, which allows extensions to operate properly under MacOS 10.7.

C. This is free software, licensed under the GPL, released in the hopes that it will be helpful, but released strictly on an as-is basis.
txoof - in reply to
The patch you link to above is dated from 2011. Is there a newer patch? I keep getting the same error you mention at the start of this thread.
txoof - in reply to txoof
I have tried backing down to 0.47 and several releases between that and 0.48.2. Each time I have run the eggbot patch linked to in the instructions, but with the same result: please install the lxml wrapper...
I've purged everything in the ~/.config directory just to be sure and reinstalled inkscape several times. Any help would be greatly appreciated!
txoof - in reply to txoof
The following eggbot patch does the job under ML 10.8 with Inkscape 0.48: code.google.com/p/eggbotcode/downloads/detail?name=EggBot2.3.1.r3s.mpkg.zip&;can=2&q=

Everything appears to work, but now I get the following errors when I try to compile:
Parsing design (AST generation)...
Compiling design (CSG Tree generation)...
Compilation finished.
Compiling design (CSG Products generation)...
ERROR: CSG generation failed! (no top level object found)
PolySets in cache: 17
Polygons in cache: 4328
CGAL Polyhedrons in cache: 59
Vertices in cache: 14412

Any idea what might be going on?
Without seeing the actual OpenSCAD file, I have no way of knowing what the issue might be. You can send me the file using my e-mail address.
dnewman - in reply to
Head slap! With my involvement with Eggbot development, you'd think I would have remembered that patch.... Indeed, it's why, ummm, things work for me on 10.7.5. (I didn't make the patch, but I tested it for Windel.) Sigh. I'll mention this in the instructions above.
Oh, and don't bother with the 'ports' or 'macports' or 'homebrew' - I tried it all, none of it worked for me and probably left my mac's python zone a diaster area. The eggbot patch worked. Nothing else did.
And, I can confirm that on OS X 10.7.5 there is a known Inkscape issue with their latest distro, 0.48.2-1-SNOWLEOPARD.dmg that will give you that error. It's a generic Inkscape issue and not specific to this extension. You should take this up in the Inkscape forums. Or, run an older version of Inkscape. I'm using 0.48.2 r9819 on my OS X 10.7.5.
Lastly, your plugin works nicely and I appreciate it. Mahalo.
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.
Thanks for the fast reply. I managed to get it to work. The SVG was originally made in Illustrator, which seemed to be causing the problem. Pasted the objects into a new SVG, converted to paths and it processed with no errors. The only odd thing I have noticed is that it doesn't like to render paths within a void if the void is within a filled solid, so for instance if I have a circular filled object, render a circular space through Paths -> Difference, then another solid circle within that space (think a bullseye), the inner most part of the circle does not want to always render.

It is a fantastic plugin and has saved me a heck of a lot of work. Promised my fiancee a custom wax seal for our wedding invites, and this is making it go faster. Just have to tinker a bit to get it how I want it to.
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.
Top