Inkscape DXF Export Extension
About
Better DXF Output is an extension for Inkscape 0.46 that improves the DXF output to be more suitable for CNC operations.
I use SheetCam to generate gcode from DXF and the Solidworks eDrawings DXF previewer. I really wanted to use a very simple vector drawing program to create the artwork, and I had some experience with Inkscape and noticed it had DXF output, however I was initially quite disappointed and thought I wouldn't be able to use Inkscape for generating DXF.
First I discovered that the Inkscape DXF export doesn't support layers. It also doesn't support objects other than paths (Inkscape treats rectangles and circles and such as objects other than basic paths) so my exported files were mostly blank. So I started to dig into the problem in the Inkscape bug database and sure enough there were two additional problems highlighted:
- the DXF export did not support objects with the "transform" attribute; this meant that sometimes objects were simply in the wrong position and it was hard to know when it could happen without exceptionally detailed proof-reading
- the DXF export didn't produce "usable" DXF for at least some readers; SheetCam wasn't mentioned by name but it seemed that maybe it wasn't an uncommon problem
Through my own experimentation I found that SheetCam didn't import the SPLINE objects from the DXF correctly (they were highly distorted). I will say that the SheetCam support was very helpful in explaining the problem as well as making a couple of useful suggestions that I could do inside Inkscape manually. It worked, but I really wanted an automated solution to as many of these problems as possible. Fortunately I discovered that Inkscape's export module is written in Python and I could easily modify it. So I rolled up my sleeves and made my own version of the DXF export module that solves my problems.
I started with the updated version from a Inkscape bug that highlighted the problems with the very basic DXF output from Inkscape (bug #192923). I added logic to save the layer identifier for each object and (using a separate Inkscape module) convert cubic paths into line segments (flattening) automatically. I also fixed up the problem with the "transform" attribute. It still doesn't deal with arbitrary object types but that is pretty easy to solve (Select All, Convert Objects to Paths).
I also added a feature that creates POINT entities instead of LINE entities for objects on layers that end in the word "drill". SheetCam uses POINT objects for drilling and I didn't have an easy way to create them. There really isn't a way to turn this behavior off (other than not end your layer names with "drill" so if you end up using this extension, be aware of this). It works best with small rectangles because it will compute the center point of the path bounding box as the location for the POINT. I just create small rectangles the same size as my drill hole.
Updates
August 3, 2011
I received an email from Linda Moehsmer reporting an issue generating DXF output for a specific file. This file contains a path which cannot be flattened, and my code entered an infinite loop. Now the code gives up flattening after incrementing the flatness up to 10. Depending on the content this might make a file that is unusable, or you might get something usable. Hard to know, since this issue is very specific to particular content.
February 23, 2009
I received an email from Jamie Tremaine with a bug report when used with the version of Inkscape shipped with Ubuntu 8.10. I was finally able to get some time to investigate and indeed there is a bug that I've since been able to fix. Odd that it only appeared in the Ubuntu build, but it was definitely wrong.
I received an email from Tim Gipson with an excellent suggestion to add a LAYER table to the output DXF. Before I could find time to implement it he actually did it himself. You can find his version "Better Better DXF Output" on his website here: http://tim.cexx.org/?p=590. I hope to find a bit of time to fold his improvement back into my version but I haven't been able to do it yet.
License
All of these files, like Inkscape itself, are licensed under the GNU Public License v2. I do not claim credit for anything more than extending what was already available either from the Inkscape distribution or from the Inkscape bug database.
The Details
There are definitely some issues to be aware of when using this software:
- I have only tested this with Inkscape 0.46 on Windows and 0.48 on Ubuntu
- Test carefully, I don't provide any assurances of quality or correctness
- Be sure to convert all objects to paths before export otherwise expect rectangles, ovals, text, etc. to disappear
- Some DXF importers have strict requirements for layer names; Inkscape allows spaces, where AutoCAD does not, so you may need to rename layers before export
Installation
Download the zip file better_dxf_output.zip (6kb). It contains:
- dxf_templates.py (this is from Inkscape bug 192923)
- better_dxf_outlines.inx
- better_dxf_outlines.py
Note: previous versions included a file simpletransform.py that was intended to replace the original from Inkscape 0.46. That bug has since been fixed in Inkscape 0.48, you will need to upgrade if you haven't already.
Extract the three files and install them into C:\Program Files\Inkscape\share\extensions (Windows) or /usr/share/inkscape/extensions (Linux) then restart Inkscape to activate.
Using the Software
In the Save As... dialog choose the entry Better DXF Output to get my version rather than the standard one.
Credits & References
Inkscape, the free vector drawing program
Inkscape bug 192923, Desktop Cutting Plotter (*.dxf) output generates unreadable SPLINE
Inkscape bug 220025, typos/errors in simpletransform.py (closed as duplicate of 241565)
Inkscape bug 241565, parseTransform() in simpletransform.py does not parse valid transformations (fix shipped)
Jamie Tremaine, for pointing out a bug in the use of self.document.getroot().xpath()
Andrew Poth for the tip regarding layer names conforming to AutoCAD requirements