Inkscape and dxf files

Inkscape is a really nice open-source vector graphics program. The latest version as of this writing is 0.46. In the architectural design workflow it’s nice to use such programs for drawings presentations etc. The default installation of inkscape is able to save to dxf but can’t open dxf files. There are 2 methods of importing dxf files into inkscape.

Method A: dxf2svg extension

There seems to be an extension included with inkscape for importing dxf files, but it depends on a dxf2svg executable which is nowhere to be found. At the inkscape wiki there is a link to a sourceforge projet but the zip file there is corrupted and of no good use. The only way to get the dxf2svg executable is by compiling through the cvs sources and placing the resulting dxf2svg program in your PATH (ie /usr/bin/dxf2svg).

[More details on howto compile later]

The pros of this method is that it’s simple and that it automatically translates dxf layers to svg layers and imports the drawing in it’s real dimensions so scaling is easy.

The cons of this method is that it doesn’t support lots of entities from dxf like for example hatch or text. Also it doesn’t retain colors in the result svg.

Method B: kabeja dxf2svg

The alternative method for importing dxf files in inkscape is called kabeja and it’s another sourceforge project that uses java to convert from dxf to svg. The latest version of kabeja is 0.4. The problem with kabeja is that the inkscape extension package they have doesn’t work out-of-the-box with inkscape 0.46. With a little bit of tweaking you can get it to work properly. Here’s how:

Download kabeja from here.

Note: download the binary, this howto uses the “pure” java version not the .NET extension

Extract the file somewhere

Edit the file called kabeja inside the inkscape-extension folder and append “$2” at the end of the 3rd line like this:

java -jar  $KABEJA_HOME/kabeja-dxf2svg.jar "$1" "$2"

Save the file and give it executable permissions (ie right-click->Properties->Permissions->”allow executing file as a program” )

As sudo copy all the files from the inkscape-extension folder  to /usr/share/inkscape/extensions

Now, fire up Inkscape and test it by IMPORTING a dxf file. This is crucial. Do not open the dxf file, import it. The difference is that if you open it then it will be scaled to fit the default page size and scaling to a specific scale (ie 1:50, 1:100) will be more difficult.

Note: dxf 2000 or r14 work well i don’t know about other versions.

Note: you need to have sun-java6-jre installed. Search for sun-java in synaptic.

To import the dxf file under scale you need the “magic number”. On import the units are translated to pixels. This is confusing. Inkscape uses 90dpi resolution as specified by the svg standard. With some simple math (for metric drawings) you get the number 35.433070866. Now depending on the units you used when drawing the dxf you will have to scale the imported geometry in multiples of this number. For example a dxf with 1unit=1meter will have to be scaled by 3543.307% to be under 1:100 scale in inkscape. (Usually, you scale to 1:100 which is a convenient scale and then scale to he desired one. To scale under inkscape you can use the transformations dialog (Shift+Ctrl+M).

One thing you’ll notice is that the  resulting svg file has no separate layers for the imported objects. Wouldn’t it be nice to have your layer structure imported to inkscape so as to work with your drawings more efficiently? Here comes I to the rescue. I’ve written a small extension for inkscape 0.46 that fixes layers for kabeja-imported dxf files. you need to apply this effect right after importing the dxf, without deselecting the imported geometry. There are two files:

#!/usr/bin/env python

import inkex

class GroupsToLayers(inkex.Effect):

    def __init__(self):
        # Call base class construtor.

    def effect(self):
        selected = self.selected
        for id, node in selected.iteritems():
            if node.tag == inkex.addNS('g','svg'):
                layername = id[3:]

effect = GroupsToLayers()


    <_name>Fix Kabeja Layers</_name>
    <dependency type="executable" location="extensions"></dependency>
    <dependency type="executable" location="extensions"></dependency>
            <submenu _name="Examples"/>
        <command reldir="extensions" interpreter="python"></command>

Copy paste to gedit and get the appropriate names for the files. Put them into /usr/share/inkscape/extensions/ folder. Now open inkscape again, open a dxf file and go to Effects->Examples->Fix Kabeja Layers. After a bit your svg file has all the layers the dxf file had.