onsdag 4. mai 2011

Inkscape to OpenSCAD dxf tutorial

As some people are having problems with this, I present a small tutorial, as I believe I have found both an explanation and a solution. Inkscape is a wonderful program, and teamed up with OpenSCAD it makes a lot of cool stuff possible.

It works with Inkscape 0.47 and 0.48 without no extra plugins.

Update 2012.06.01 : Changed code on step 9 to use the preferred child import().
Update 2012.09.16 : Added information about "add nodes" on step 6.
Update 2012.10.09 : Added information about "flatten beziers" on step 6. Thanks to zeptomoon for the suggestion!

If you don't want to read my ramblings, the trick is easy: Make all the curved segments lines before you export to dxf.

Now, commence indepth tutorial:

1 First off, let's make a simple object, like this ellipse.

Note the square and round handles on the object, which indicates this is an .svg style object which you can modify according to the specific rules of said object.

2  This is of course no good, as OpenSCAD / dxf can't read this format. What we need to do is convert it to a path. [Shift+Ctrl+C]

3  Select the node tool [N], and notice how the handles on the object now look like gray squares. This indicates we now have a path.

Drag-select, or press [Ctrl+A] to select every node. With the nodes selected, notice how the handles changed again. The lines with spheres at the end tells us the segments between the nodes are curved. (Try fiddling with the points if you like).

This, again, is no good for the standard .dxf plugin in Inkscape, which only exports straight lines. (See point 8).

5  With every node selected, press the indicated button to "Make selected segment lines".

The lines with spheres on the handles are gone, so we now have only straight lines between the nodes. This would work, but doesn't look very smooth, does it? Undo your last action and continue to next point in the tutorial, where you learn to do it the right way. (Sorry about that).

6  With every node selected, initialize some mouseclicking action on the indicated button "Insert new nodes into selected segments".

This, well, inserts new nodes into selected segments, following the curve.

Press a few times if you want a smoother object, but don't overdo it now, you hear?

You could also use "Extensions - Modify Path - Add Nodes" for dense, even distribution of nodes all over complex parts.

Even better is to use "Extensions - Modify Path - Flatten Beziers", which will only add nodes at curved areas, resulting in less useless nodes. A value of 0.3 is a good value to start with. Thanks to zeptomoon for the suggestion!

7  Feel free to once more click "Make selected segment lines".

This looks much better. The lines are straight, but due to the sheer numbers, they simulate a curve! Victory!

You can of course select only part of the nodes, if you only want part of the model smooth.

Now save the file as "test.dxf". You will need to select "Desktop Cutting Plotter (R13)" in the type drop down box.

Notice the help tells us we can only export certain elements. What we have done now is create only lowpolylines, which exports beautifully.

9 Start up OpenSCAD and use the following code to open up the file. (You need to put the .scad and .dxf file in the same directory).
linear_extrude(height = 10) import("test.dxf");
There you go! Enjoy your Inkscape paths in OpenSCAD.

10  This works fine with any objects, advanced or not. Just remember to union the different paths.

Also note the part "Rap" is not just white text, it's a proper hole made with difference.

You can of course export several .dxf files and combine them in OpenSCAD if you need different height on some parts.

It can sometimes be difficult work with the .dxf file in OpenSCAD, as Inkscape exports the entire page, and it can be a bit tricky to locate where the object are. I recommend you select everything, go to Document Properties [Shift+Ctrl+D] and "Fit page to selection".  Edit: See below. The problem is a bit difficult to explain, but you'll understand it once you have experienced it.

Take care not to overwrite your original .svg file if you want to be able to edit things later. It's much easier to work with objects than paths.

Edit 16.09.2011: I have found it easier to align the objects to point 0x0 (the bottom left corner of the page in Inkscape) and not center the object in OpenSCAD. Then you can easily stack several .dxf files in different heights.

Now go forth and make something awesome! Remember to share your .svg files on thingiverse, to go with the .scad files!

I hope this helped, and clarified some things. If you have further questions or suggestions, feel free to comment.