KalyanChakravarthy.net

Thoughts, stories and ideas.

Convert DXF to SVG

After discovering a local makerspace which has free open days, I wanted to try their laser cutter, for which I needed to provide a file in SVG format.

One of the challenges I set myself up for was to be able to create model and have series of scripts to do the rest of conversion for me.

I started with a simple sliced model in OpenSCAD, which unfortunately only exported to DXF format

module side() {
    difference() {
        cube([50, 50, 10], center = true);
        translate([-10,0,0]) cube([20, 20, 10], center = true);
    }
}

projection(cut=true) side();

So I first converted to DXF

$ ~/Applications/OpenSCAD/Contents/MacOS/OpenSCAD -o ex1.dxf ex1.scad 

There are no tools out there to ease the conversion to SVG (on mac at least). Inkscape does support it, but getting it to run on Mac was a hassle. So I resorted to using FreeCAD which has nice python bindings, to write an export script

:::python
# exporter.py 
import sys
ifile = sys.argv[0]
ofile = sys.argv[1]

import importDXF
import importSVG

obj = importDXF.open( ifile )
importSVG.export( obj.Objects, ofile )

And then converted the DXF to SVG using this

:::shell
$ ~/Applications/FreeCAD.app/Contents/bin/FreeCADCmd exporter.py ex1.dxf ex1.svg

Here is the final result