I'm scripting some shapes, drawing them on layer 1, saving as DXF via KLayout's GUI, then attempting to open in Inkscape to convert to SVG (for laser cutting).
A few questions to get out of the way:
* Why involve Inkscape? Our laser cutter needs SVG, but KLayout only exports DXF.
* Why not script it in Inkscape? Because I prefer the KLayout API.
* Why not draw it by hand? Because I need to script it.
Other DXFs open fine in Inkscape (latest version), so I don't suspect it's an Inkscape problem, just a compatibility problem.
Inkscape tells me:
Traceback (most recent call last):
File "dxf_input.py", line 401, in <module>
layer_colors[layername] = int(line[1])
NameError: name 'layername' is not defined
Even though "dxf_input.py" is an Inkscape python script, that python script does work for a DXF file from SolidWorks. So Inkscape script can swallow Solidworks DXF, but chokes on KLayout's DXF -- perhaps it's KLayout's different interpretation of some grey area in the DXF specification.
I tried all sorts of combinations of options in KLayout's File > Save As... dialog box (e.g. Polyline, LWPolyline, etc), but always the same result.
Any ideas?
Comments
When I say "other DXFs open fine in Inkscape" -- I haven't tried a lot, but only simple drawings saved from SolidWorks. For example here is a simple 1x2 rectangle I drew in SolidWorks and saved as DXF. Inkscape opens it just fine.
Sorry for all the long lines. And here is a 1x2 (micron) rectangle from KLayout, saved by KLayout as DXF, that doesn't open in Inkscape. Not knowing the DXF format, it is hard for me to see where to look for the problem. I thought perhaps your trained eye might know where to look...
Hi,
DXF is all gray area ... there is no format as badly documented as DXF.
I could reproduce the problem, but frankly I am not really in a mood to debug Inkscape's importer. I am able to read my DXF files into SolidWorks, AutoDesk's TrueView and a couple of online viewers (for example try this: https://a360.autodesk.com/viewer/ ). All of them work.
I have debugged the DXF importer so far to understand the error you see: Inkscape requires the layer name to be specified before the color:
dxf_import.py
parses the LAYER table and looks for various records. If record type "2" (layer name) is seen, it will store the layer name in thelayername
variable. If record type "62" is seen (color), it will store the color in thelayer_colors[layername]
dict. Now if record "2" comes after "62",layername
is not defined and hence an error is thrown. I edited my DXF file and changed the order of records which made the DXF importer get along a bit further but not for long: now I see other errors and I finally stopped debugging.AFAIK the record order is not specified in DXF, that's why other readers don't bail out on KLayout's DFX files.
Maybe over all it's easier to write a SVG exporter for KLayout than to get Inkscape's importer working. I started that once but seeing the poor performance of SVG I stopped that effort. Maybe I am able to dig out my old files.
Matthias
Thanks Matthias,
I do indeed see now that your DXFs do import into other tools, so it is a fault of poorly-documented DXF format and inconsistent interpretation.
Thanks anyway for taking a look.
An SVG exporter would be a nice feature
Thanks,
David
Hi David,
here is a simple SVG exporter:
It does not translate fill pattern or frame settings into stroke styles - only the fill color is kept.
Maybe you can give some feedback or improved code. I will then add it to the "useful scripts".
Matthias
Wow, thanks Matthias, that seems great!
I can't get it working yet though -- it complains on the line that has RBA::Vector -- and I don't see a "Vector" class in the API. Probably this is some upcoming API feature that you haven't released yet. I'm using the latest KLayout (0.24.8, win64). I see what it is trying to do though. Am I right about the Vector class not being in the 0.24.8 API?
David
Oh sorry .. that was already a preview to a new version ... :-)
That reminds me of keeping an eye on backward compatibility.
I have edited the code above. Please try again.
Matthias
Works like a charm!
You even match the stipple colors now. Wow, really useful, thanks Matthias!
Thanks for the SVG export code, @Matthias .
The above code has some trouble maintaining sizes when importing into other software. The addition of a viewBox to the
<svg>
tag in addition to specifying units on the width and height of the<svg>
tag seem to fix the problem.Here is the code I have been using:
I think it would be great if this could be added to the "useful scripts."