Working with DXFs

edited August 2016 in Ruby Scripting
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

  • edited August 2016

    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.

      0
    SECTION
      2
    HEADER
      9
    $ACADVER
      1
    AC1015
      9
    $ACADMAINTVER
     70
         6
      9
    $DWGCODEPAGE
      3
    ANSI_1252
      9
    $INSBASE
     10
    0.0
     20
    0.0
     30
    0.0
      9
    $EXTMIN
     10
    0.0
     20
    0.0
     30
    0.0
      9
    $EXTMAX
     10
    1.0
     20
    2.0
     30
    0.0
      9
    $LIMMIN
     10
    0.0
     20
    0.0
      9
    $LIMMAX
     10
    12.0
     20
    9.0
      9
    $ORTHOMODE
     70
         0
      9
    $REGENMODE
     70
         1
      9
    $FILLMODE
     70
         1
      9
    $QTEXTMODE
     70
         0
      9
    $MIRRTEXT
     70
         1
      9
    $LTSCALE
     40
    1.0
      9
    $ATTMODE
     70
         1
      9
    $TEXTSIZE
     40
    0.1377952756
      9
    $TRACEWID
     40
    0.05
      9
    $TEXTSTYLE
      7
    Standard
      9
    $CLAYER
      8
    0
      9
    $CELTYPE
      6
    ByLayer
      9
    $CECOLOR
     62
       256
      9
    $CELTSCALE
     40
    1.0
      9
    $DISPSILH
     70
         0
      9
    $DIMSCALE
     40
    1.0
      9
    $DIMASZ
     40
    0.13
      9
    $DIMEXO
     40
    0.0393700787
      9
    $DIMDLI
     40
    0.38
      9
    $DIMRND
     40
    0.0
      9
    $DIMDLE
     40
    0.0
      9
    $DIMEXE
     40
    0.0393700787
      9
    $DIMTP
     40
    0.0
      9
    $DIMTM
     40
    0.0
      9
    $DIMTXT
     40
    0.1377952756
      9
    $DIMCEN
     40
    0.09
      9
    $DIMTSZ
     40
    0.0
      9
    $DIMTOL
     70
         0
      9
    $DIMLIM
     70
         0
      9
    $DIMTIH
     70
         1
      9
    $DIMTOH
     70
         1
      9
    $DIMSE1
     70
         0
      9
    $DIMSE2
     70
         0
      9
    $DIMTAD
     70
         1
      9
    $DIMZIN
     70
         0
      9
    $DIMBLK
      1
    
      9
    $DIMASO
     70
         1
      9
    $DIMSHO
     70
         1
      9
    $DIMPOST
      1
    
      9
    $DIMAPOST
      1
    
      9
    $DIMALT
     70
         0
      9
    $DIMALTD
     70
         2
      9
    $DIMALTF
     40
    25.4
      9
    $DIMLFAC
     40
    1.0
      9
    $DIMTOFL
     70
         0
      9
    $DIMTVP
     40
    0.0
      9
    $DIMTIX
     70
         0
      9
    $DIMSOXD
     70
         0
      9
    $DIMSAH
     70
         0
      9
    $DIMBLK1
      1
    
      9
    $DIMBLK2
      1
    
      9
    $DIMSTYLE
      2
    Standard
      9
    $DIMCLRD
     70
         0
      9
    $DIMCLRE
     70
         0
      9
    $DIMCLRT
     70
         0
      9
    $DIMTFAC
     40
    1.0
      9
    $DIMGAP
     40
    0.06
      9
    $DIMJUST
     70
         0
      9
    $DIMSD1
     70
         0
      9
    $DIMSD2
     70
         0
      9
    $DIMTOLJ
     70
         1
      9
    $DIMTZIN
     70
         0
      9
    $DIMALTZ
     70
         0
      9
    $DIMALTTZ
     70
         0
      9
    $DIMUPT
     70
         0
      9
    $DIMDEC
     70
         2
      9
    $DIMTDEC
     70
         2
      9
    $DIMALTU
     70
         2
      9
    $DIMALTTD
     70
         2
      9
    $DIMTXSTY
      7
    Standard
      9
    $DIMAUNIT
     70
         0
      9
    $DIMADEC
     70
         2
      9
    $DIMALTRND
     40
    0.0
      9
    $DIMAZIN
     70
         0
      9
    $DIMDSEP
     70
        46
      9
    $DIMATFIT
     70
         3
      9
    $DIMFRAC
     70
         0
      9
    $DIMLDRBLK
      1
    
      9
    $DIMLUNIT
     70
         2
      9
    $DIMLWD
     70
        -2
      9
    $DIMLWE
     70
        -2
      9
    $DIMTMOVE
     70
         0
      9
    $LUNITS
     70
         2
      9
    $LUPREC
     70
         2
      9
    $SKETCHINC
     40
    0.1
      9
    $FILLETRAD
     40
    0.5
      9
    $AUNITS
     70
         0
      9
    $AUPREC
     70
         2
      9
    $MENU
      1
    .
      9
    $ELEVATION
     40
    0.0
      9
    $PELEVATION
     40
    0.0
      9
    $THICKNESS
     40
    0.0
      9
    $LIMCHECK
     70
         0
      9
    $CHAMFERA
     40
    0.5
      9
    $CHAMFERB
     40
    0.5
      9
    $CHAMFERC
     40
    1.0
      9
    $CHAMFERD
     40
    0.0
      9
    $SKPOLY
     70
         0
      9
    $TDCREATE
     40
    2457620.932563947
      9
    $TDUCREATE
     40
    2457621.224230614
      9
    $TDUPDATE
     40
    2457620.932612882
      9
    $TDUUPDATE
     40
    2457621.224279549
      9
    $TDINDWG
     40
    0.0000000116
      9
    $TDUSRTIMER
     40
    0.0000000116
      9
    $USRTIMER
     70
         1
      9
    $ANGBASE
     50
    0.0
      9
    $ANGDIR
     70
         0
      9
    $PDMODE
     70
         0
      9
    $PDSIZE
     40
    -1.0
      9
    $PLINEWID
     40
    0.0
      9
    $SPLFRAME
     70
         0
      9
    $SPLINETYPE
     70
         6
      9
    $SPLINESEGS
     70
         8
      9
    $HANDSEED
      5
    48
      9
    $SURFTAB1
     70
         6
      9
    $SURFTAB2
     70
         6
      9
    $SURFTYPE
     70
         6
      9
    $SURFU
     70
         6
      9
    $SURFV
     70
         6
      9
    $UCSBASE
      2
    
      9
    $UCSNAME
      2
    
      9
    $UCSORG
     10
    0.0
     20
    0.0
     30
    0.0
      9
    $UCSXDIR
     10
    1.0
     20
    0.0
     30
    0.0
      9
    $UCSYDIR
     10
    0.0
     20
    1.0
     30
    0.0
      9
    $UCSORTHOREF
      2
    
      9
    $UCSORTHOVIEW
     70
         0
      9
    $UCSORGTOP
     10
    0.0
     20
    0.0
     30
    0.0
      9
    $UCSORGBOTTOM
     10
    0.0
     20
    0.0
     30
    0.0
      9
    $UCSORGLEFT
     10
    0.0
     20
    0.0
     30
    0.0
      9
    $UCSORGRIGHT
     10
    0.0
     20
    0.0
     30
    0.0
      9
    $UCSORGFRONT
     10
    0.0
     20
    0.0
     30
    0.0
      9
    $UCSORGBACK
     10
    0.0
     20
    0.0
     30
    0.0
      9
    $PUCSBASE
      2
    
      9
    $PUCSNAME
      2
    
      9
    $PUCSORG
     10
    0.0
     20
    0.0
     30
    0.0
      9
    $PUCSXDIR
     10
    1.0
     20
    0.0
     30
    0.0
      9
    $PUCSYDIR
     10
    0.0
     20
    1.0
     30
    0.0
      9
    $PUCSORTHOREF
      2
    
      9
    $PUCSORTHOVIEW
     70
         0
      9
    $PUCSORGTOP
     10
    0.0
     20
    0.0
     30
    0.0
      9
    $PUCSORGBOTTOM
     10
    0.0
     20
    0.0
     30
    0.0
      9
    $PUCSORGLEFT
     10
    0.0
     20
    0.0
     30
    0.0
      9
    $PUCSORGRIGHT
     10
    0.0
     20
    0.0
     30
    0.0
      9
    $PUCSORGFRONT
     10
    0.0
     20
    0.0
     30
    0.0
      9
    $PUCSORGBACK
     10
    0.0
     20
    0.0
     30
    0.0
      9
    $USERI1
     70
         0
      9
    $USERI2
     70
         0
      9
    $USERI3
     70
         0
      9
    $USERI4
     70
         0
      9
    $USERI5
     70
         0
      9
    $USERR1
     40
    0.0
      9
    $USERR2
     40
    0.0
      9
    $USERR3
     40
    0.0
      9
    $USERR4
     40
    0.0
      9
    $USERR5
     40
    0.0
      9
    $WORLDVIEW
     70
         1
      9
    $SHADEDGE
     70
         3
      9
    $SHADEDIF
     70
        70
      9
    $TILEMODE
     70
         1
      9
    $MAXACTVP
     70
        64
      9
    $PINSBASE
     10
    0.0
     20
    0.0
     30
    0.0
      9
    $PLIMCHECK
     70
         0
      9
    $PEXTMIN
     10
    1.0000000000E+20
     20
    1.0000000000E+20
     30
    1.0000000000E+20
      9
    $PEXTMAX
     10
    -1.0000000000E+20
     20
    -1.0000000000E+20
     30
    -1.0000000000E+20
      9
    $PLIMMIN
     10
    0.0
     20
    0.0
      9
    $PLIMMAX
     10
    12.0
     20
    9.0
      9
    $UNITMODE
     70
         0
      9
    $VISRETAIN
     70
         1
      9
    $PLINEGEN
     70
         0
      9
    $PSLTSCALE
     70
         1
      9
    $TREEDEPTH
     70
      3020
      9
    $CMLSTYLE
      2
    Standard
      9
    $CMLJUST
     70
         0
      9
    $CMLSCALE
     40
    1.0
      9
    $PROXYGRAPHICS
     70
         1
      9
    $MEASUREMENT
     70
         0
      9
    $CELWEIGHT
    370
        -1
      9
    $ENDCAPS
    280
         0
      9
    $JOINSTYLE
    280
         0
      9
    $LWDISPLAY
    290
         1
      9
    $INSUNITS
     70
         1
      9
    $HYPERLINKBASE
      1
    
      9
    $STYLESHEET
      1
    
      9
    $XEDIT
    290
         1
      9
    $CEPSNTYPE
    380
         0
      9
    $PSTYLEMODE
    290
         1
      9
    $FINGERPRINTGUID
      2
    {4093D1D6-4344-43A8-8DED-D243C3977CDE}
      9
    $VERSIONGUID
      2
    {FAEB1C32-E019-11D5-929B-00C0DF256EC4}
      9
    $EXTNAMES
    290
         1
      9
    $PSVPSCALE
     40
    0.0
      9
    $OLESTARTUP
    290
         0
      0
    ENDSEC
      0
    SECTION
      2
    CLASSES
      0
    CLASS
      1
    ACDBDICTIONARYWDFLT
      2
    AcDbDictionaryWithDefault
      3
    ObjectDBX Classes
     90
            0
    280
         0
    281
         0
      0
    CLASS
      1
    VISUALSTYLE
      2
    AcDbVisualStyle
      3
    ObjectDBX Classes
     90
         4095
    280
         0
    281
         0
      0
    CLASS
      1
    MATERIAL
      2
    AcDbMaterial
      3
    ObjectDBX Classes
     90
         1153
    280
         0
    281
         0
      0
    CLASS
      1
    ACDBPLACEHOLDER
      2
    AcDbPlaceHolder
      3
    ObjectDBX Classes
     90
            0
    280
         0
    281
         0
      0
    CLASS
      1
    LAYOUT
      2
    AcDbLayout
      3
    ObjectDBX Classes
     90
            0
    280
         0
    281
         0
      0
    ENDSEC
      0
    SECTION
      2
    TABLES
      0
    TABLE
      2
    VPORT
      5
    8
    330
    0
    100
    AcDbSymbolTable
     70
         1
      0
    VPORT
      5
    29
    330
    8
    100
    AcDbSymbolTableRecord
    100
    AcDbViewportTableRecord
      2
    *Active
     70
         0
     10
    0.0
     20
    0.0
     11
    1.0
     21
    1.0
     12
    0.5
     22
    1.0
     13
    0.0
     23
    0.0
     14
    0.5
     24
    0.5
     15
    0.5
     25
    0.5
     16
    0.0
     26
    0.0
     36
    1.0
     17
    0.0
     27
    0.0
     37
    0.0
     40
    2.04
     41
    0.5
     42
    50.0
     43
    0.0
     44
    0.0
     50
    0.0
     51
    0.0
     71
         0
     72
       100
     73
         1
     74
         3
     75
         0
     76
         0
     77
         0
     78
         0
    281
         0
     65
         1
    110
    0.0
    120
    0.0
    130
    0.0
    111
    1.0
    121
    0.0
    131
    0.0
    112
    0.0
    122
    1.0
    132
    0.0
     79
         0
    146
    0.0
      0
    ENDTAB
      0
    TABLE
      2
    LTYPE
      5
    5
    330
    0
    100
    AcDbSymbolTable
     70
         6
      0
    LTYPE
      5
    14
    330
    5
    100
    AcDbSymbolTableRecord
    100
    AcDbLinetypeTableRecord
      2
    ByBlock
     70
         0
      3
    
     72
        65
     73
         0
     40
    0.0
      0
    LTYPE
      5
    15
    330
    5
    100
    AcDbSymbolTableRecord
    100
    AcDbLinetypeTableRecord
      2
    ByLayer
     70
         0
      3
    
     72
        65
     73
         0
     40
    0.0
      0
    LTYPE
      5
    16
    330
    5
    100
    AcDbSymbolTableRecord
    100
    AcDbLinetypeTableRecord
      2
    Continuous
     70
         0
      3
    Solid line
     72
        65
     73
         0
     40
    0.0
      0
    LTYPE
      5
    3F
    330
    5
    100
    AcDbSymbolTableRecord
    100
    AcDbLinetypeTableRecord
      2
    HIDDEN
     70
         0
      3
    Hidden __ __ __ __ __ __ __ __ __ __ __ __ __ __
     72
        65
     73
         2
     40
    0.075
     49
    0.05
     74
         0
     49
    -0.025
     74
         0
      0
    LTYPE
      5
    40
    330
    5
    100
    AcDbSymbolTableRecord
    100
    AcDbLinetypeTableRecord
      2
    PHANTOM
     70
         0
      3
    Phantom ______  __  __  ______  __  __  ______
     72
        65
     73
         6
     40
    0.5
     49
    0.25
     74
         0
     49
    -0.05
     74
         0
     49
    0.05
     74
         0
     49
    -0.05
     74
         0
     49
    0.05
     74
         0
     49
    -0.05
     74
         0
      0
    LTYPE
      5
    41
    330
    5
    100
    AcDbSymbolTableRecord
    100
    AcDbLinetypeTableRecord
      2
    CENTER
     70
         0
      3
    Center ____ _ ____ _ ____ _ ____ _ ____ _ ____
     72
        65
     73
         4
     40
    0.4
     49
    0.25
     74
         0
     49
    -0.05
     74
         0
     49
    0.05
     74
         0
     49
    -0.05
     74
         0
      0
    LTYPE
      5
    42
    330
    5
    100
    AcDbSymbolTableRecord
    100
    AcDbLinetypeTableRecord
      2
    CENTERX2
     70
         0
      3
    Center (2x) ________  __  ________  __  _____
     72
        65
     73
         4
     40
    0.8
     49
    0.5
     74
         0
     49
    -0.1
     74
         0
     49
    0.1
     74
         0
     49
    -0.1
     74
         0
      0
    LTYPE
      5
    43
    330
    5
    100
    AcDbSymbolTableRecord
    100
    AcDbLinetypeTableRecord
      2
    DOT2
     70
         0
      3
    Dot (.5x) ........................................
     72
        65
     73
         2
     40
    0.025
     49
    0.0
     74
         0
     49
    -0.025
     74
         0
      0
    ENDTAB
      0
    TABLE
      2
    LAYER
      5
    2
    330
    0
    100
    AcDbSymbolTable
     70
         1
      0
    LAYER
      5
    10
    330
    2
    100
    AcDbSymbolTableRecord
    100
    AcDbLayerTableRecord
      2
    0
     70
         0
     62
         7
      6
    Continuous
    370
        -3
    390
    F
      0
    ENDTAB
      0
    TABLE
      2
    STYLE
      5
    3
    330
    0
    100
    AcDbSymbolTable
     70
         1
      0
    STYLE
      5
    11
    330
    3
    100
    AcDbSymbolTableRecord
    100
    AcDbTextStyleTableRecord
      2
    Standard
     70
         0
     40
    0.0
     41
    1.0
     50
    0.0
     71
         0
     42
    0.1377952756
      3
    txt
      4
    
      0
    ENDTAB
      0
    TABLE
      2
    VIEW
      5
    6
    330
    0
    100
    AcDbSymbolTable
     70
         0
      0
    ENDTAB
      0
    TABLE
      2
    UCS
      5
    7
    330
    0
    100
    AcDbSymbolTable
     70
         0
      0
    ENDTAB
      0
    TABLE
      2
    APPID
      5
    9
    330
    0
    100
    AcDbSymbolTable
     70
         1
      0
    APPID
      5
    12
    330
    9
    100
    AcDbSymbolTableRecord
    100
    AcDbRegAppTableRecord
      2
    ACAD
     70
         0
      0
    ENDTAB
      0
    TABLE
      2
    DIMSTYLE
      5
    A
    330
    0
    100
    AcDbSymbolTable
     70
         1
    100
    AcDbDimStyleTable
      0
    DIMSTYLE
    105
    27
    330
    A
    100
    AcDbSymbolTableRecord
    100
    AcDbDimStyleTableRecord
      2
    Standard
     70
         0
    178
         0
    340
    11
      0
    ENDTAB
      0
    TABLE
      2
    BLOCK_RECORD
      5
    1
    330
    0
    100
    AcDbSymbolTable
     70
         1
      0
    BLOCK_RECORD
      5
    1F
    330
    1
    100
    AcDbSymbolTableRecord
    100
    AcDbBlockTableRecord
      2
    *Model_Space
    340
    22
      0
    BLOCK_RECORD
      5
    1B
    330
    1
    100
    AcDbSymbolTableRecord
    100
    AcDbBlockTableRecord
      2
    *Paper_Space
    340
    1E
      0
    BLOCK_RECORD
      5
    23
    330
    1
    100
    AcDbSymbolTableRecord
    100
    AcDbBlockTableRecord
      2
    *Paper_Space0
    340
    26
      0
    ENDTAB
      0
    ENDSEC
      0
    SECTION
      2
    BLOCKS
      0
    BLOCK
      5
    20
    330
    1F
    100
    AcDbEntity
      8
    0
    100
    AcDbBlockBegin
      2
    *Model_Space
     70
         0
     10
    0.0
     20
    0.0
     30
    0.0
      3
    *Model_Space
      1
    
      0
    ENDBLK
      5
    21
    330
    1F
    100
    AcDbEntity
      8
    0
    100
    AcDbBlockEnd
      0
    BLOCK
      5
    1C
    330
    1B
    100
    AcDbEntity
     67
         1
      8
    0
    100
    AcDbBlockBegin
      2
    *Paper_Space
     70
         0
     10
    0.0
     20
    0.0
     30
    0.0
      3
    *Paper_Space
      1
    
      0
    ENDBLK
      5
    1D
    330
    1B
    100
    AcDbEntity
     67
         1
      8
    0
    100
    AcDbBlockEnd
      0
    BLOCK
      5
    24
    330
    23
    100
    AcDbEntity
      8
    0
    100
    AcDbBlockBegin
      2
    *Paper_Space0
     70
         0
     10
    0.0
     20
    0.0
     30
    0.0
      3
    *Paper_Space0
      1
    
      0
    ENDBLK
      5
    25
    330
    23
    100
    AcDbEntity
      8
    0
    100
    AcDbBlockEnd
      0
    ENDSEC
      0
    SECTION
      2
    ENTITIES
      0
    LINE
      5
    44
    330
    1F
    100
    AcDbEntity
      8
    0
      6
    Continuous
     62
         7
    100
    AcDbLine
     10
    0.0
     20
    0.0
     30
    0.0
     11
    1.0
     21
    0.0
     31
    0.0
      0
    LINE
      5
    45
    330
    1F
    100
    AcDbEntity
      8
    0
      6
    Continuous
     62
         7
    100
    AcDbLine
     10
    0.0
     20
    0.0
     30
    0.0
     11
    0.0
     21
    2.0
     31
    0.0
      0
    LINE
      5
    46
    330
    1F
    100
    AcDbEntity
      8
    0
      6
    Continuous
     62
         7
    100
    AcDbLine
     10
    0.0
     20
    2.0
     30
    0.0
     11
    1.0
     21
    2.0
     31
    0.0
      0
    LINE
      5
    47
    330
    1F
    100
    AcDbEntity
      8
    0
      6
    Continuous
     62
         7
    100
    AcDbLine
     10
    1.0
     20
    0.0
     30
    0.0
     11
    1.0
     21
    2.0
     31
    0.0
      0
    ENDSEC
      0
    SECTION
      2
    OBJECTS
      0
    DICTIONARY
      5
    C
    330
    0
    100
    AcDbDictionary
    281
         1
      3
    ACAD_GROUP
    350
    D
      3
    ACAD_LAYOUT
    350
    1A
      3
    ACAD_MLINESTYLE
    350
    17
      3
    ACAD_PLOTSETTINGS
    350
    19
      3
    ACAD_PLOTSTYLENAME
    350
    E
      0
    DICTIONARY
      5
    D
    102
    {ACAD_REACTORS
    330
    C
    102
    }
    330
    C
    100
    AcDbDictionary
    281
         1
      0
    DICTIONARY
      5
    1A
    102
    {ACAD_REACTORS
    330
    C
    102
    }
    330
    C
    100
    AcDbDictionary
    281
         1
      3
    Layout1
    350
    1E
      3
    Layout2
    350
    26
      3
    Model
    350
    22
      0
    DICTIONARY
      5
    17
    102
    {ACAD_REACTORS
    330
    C
    102
    }
    330
    C
    100
    AcDbDictionary
    281
         1
      3
    Standard
    350
    18
      0
    DICTIONARY
      5
    19
    102
    {ACAD_REACTORS
    330
    C
    102
    }
    330
    C
    100
    AcDbDictionary
    281
         1
      0
    ACDBDICTIONARYWDFLT
      5
    E
    102
    {ACAD_REACTORS
    330
    C
    102
    }
    330
    C
    100
    AcDbDictionary
    281
         1
      3
    Normal
    350
    F
    100
    AcDbDictionaryWithDefault
    340
    F
      0
    LAYOUT
      5
    1E
    102
    {ACAD_REACTORS
    330
    1A
    102
    }
    330
    1A
    100
    AcDbPlotSettings
      1
    
      2
    none_device
      4
    
      6
    
     40
    0.0
     41
    0.0
     42
    0.0
     43
    0.0
     44
    0.0
     45
    0.0
     46
    0.0
     47
    0.0
     48
    0.0
     49
    0.0
    140
    0.0
    141
    0.0
    142
    1.0
    143
    1.0
     70
       688
     72
         0
     73
         0
     74
         5
      7
    
     75
        16
    147
    1.0
    148
    0.0
    149
    0.0
    100
    AcDbLayout
      1
    Layout1
     70
         1
     71
         1
     10
    0.0
     20
    0.0
     11
    12.0
     21
    9.0
     12
    0.0
     22
    0.0
     32
    0.0
     14
    1.0000000000E+20
     24
    1.0000000000E+20
     34
    1.0000000000E+20
     15
    -1.0000000000E+20
     25
    -1.0000000000E+20
     35
    -1.0000000000E+20
    146
    0.0
     13
    0.0
     23
    0.0
     33
    0.0
     16
    1.0
     26
    0.0
     36
    0.0
     17
    0.0
     27
    1.0
     37
    0.0
     76
         0
    330
    1B
      0
    LAYOUT
      5
    26
    102
    {ACAD_REACTORS
    330
    1A
    102
    }
    330
    1A
    100
    AcDbPlotSettings
      1
    
      2
    none_device
      4
    
      6
    
     40
    0.0
     41
    0.0
     42
    0.0
     43
    0.0
     44
    0.0
     45
    0.0
     46
    0.0
     47
    0.0
     48
    0.0
     49
    0.0
    140
    0.0
    141
    0.0
    142
    1.0
    143
    1.0
     70
       688
     72
         0
     73
         0
     74
         5
      7
    
     75
        16
    147
    1.0
    148
    0.0
    149
    0.0
    100
    AcDbLayout
      1
    Layout2
     70
         1
     71
         2
     10
    0.0
     20
    0.0
     11
    0.0
     21
    0.0
     12
    0.0
     22
    0.0
     32
    0.0
     14
    0.0
     24
    0.0
     34
    0.0
     15
    0.0
     25
    0.0
     35
    0.0
    146
    0.0
     13
    0.0
     23
    0.0
     33
    0.0
     16
    1.0
     26
    0.0
     36
    0.0
     17
    0.0
     27
    1.0
     37
    0.0
     76
         0
    330
    23
      0
    LAYOUT
      5
    22
    102
    {ACAD_REACTORS
    330
    1A
    102
    }
    330
    1A
    100
    AcDbPlotSettings
      1
    
      2
    none_device
      4
    ANSI_A_(11.00_x_8.50_Inches)
      6
    
     40
    6.35
     41
    19.05
     42
    6.35000508
     43
    19.05000254
     44
    279.4
     45
    215.9
     46
    79.91474746
     47
    -17.97050127
     48
    0.0
     49
    0.0
    140
    0.0
    141
    0.0
    142
    4.2075
    143
    1.0
     70
      1696
     72
         0
     73
         0
     74
         0
      7
    
     75
         0
    147
    0.5913241897
    148
    0.0
    149
    0.0
    100
    AcDbLayout
      1
    Model
     70
         1
     71
         0
     10
    0.0
     20
    0.0
     11
    12.0
     21
    9.0
     12
    0.0
     22
    0.0
     32
    0.0
     14
    0.0
     24
    0.0
     34
    0.0
     15
    1.0
     25
    2.0
     35
    0.0
    146
    0.0
     13
    0.0
     23
    0.0
     33
    0.0
     16
    1.0
     26
    0.0
     36
    0.0
     17
    0.0
     27
    1.0
     37
    0.0
     76
         0
    330
    1F
    331
    29
      0
    MLINESTYLE
      5
    18
    102
    {ACAD_REACTORS
    330
    17
    102
    }
    330
    17
    100
    AcDbMlineStyle
      2
    Standard
     70
         0
      3
    
     62
       256
     51
    90.0
     52
    90.0
     71
         2
     49
    0.5
     62
       256
      6
    BYLAYER
     49
    -0.5
     62
       256
      6
    BYLAYER
      0
    ACDBPLACEHOLDER
      5
    F
    102
    {ACAD_REACTORS
    330
    E
    102
    }
    330
    E
      0
    ENDSEC
      0
    EOF
    
  • edited November -1

    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...

    0
    SECTION
    2
    HEADER
    9
    $ACADVER
    1
    AC1006
    0
    ENDSEC
    0
    SECTION
    2
    TABLES
    0
    TABLE
    2
    LAYER
    70
    1
    0
    LAYER
    70
    0
    62
    1
    6
    CONTINUOUS
    2
    L1D0
    0
    ENDTAB
    0
    ENDSEC
    0
    SECTION
    2
    BLOCKS
    0
    ENDSEC
    0
    SECTION
    2
    ENTITIES
    0
    POLYLINE
    8
    L1D0
    70
    1
    40
    0
    41
    0
    66
    1
    0
    VERTEX
    8
    L1D0
    10
    0
    20
    0
    0
    VERTEX
    8
    L1D0
    10
    0
    20
    2
    0
    VERTEX
    8
    L1D0
    10
    1
    20
    2
    0
    VERTEX
    8
    L1D0
    10
    1
    20
    0
    0
    SEQEND
    0
    ENDSEC
    0
    EOF
    
  • edited August 2016

    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 the layername variable. If record type "62" is seen (color), it will store the color in the layer_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

  • edited November -1

    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

  • edited August 2016

    Hi David,

    here is a simple SVG exporter:

    class SVGWriter
    
      def initialize(file)
        @file = File.open(file, "w")
      end
    
      def start(w, h)
        @file.puts(<<"END")
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <!-- Created with Inkscape (http://www.inkscape.org/) -->
    
    <svg
       xmlns:svg="http://www.w3.org/2000/svg"
       xmlns="http://www.w3.org/2000/svg"
       xmlns:xlink="http://www.w3.org/1999/xlink"
       width="#{'%.12g'%w}"
       height="#{'%.12g'%h}"
       version="1.1">   
    END
      end
    
      def finish
        @file.puts("</svg>")
        @file.close
      end
    
      def begin_layer(lp)
        @stroke_color = lp.frame_color
        @fill_color = lp.fill_color
      end
    
      def polygon(p, dbu)
    
        pts = []
        p.each_point_hull { |pt| pts << ("%.12g %.12g" % [pt.x * dbu, pt.y * dbu]) }
        ctrs = [ "M " + pts.join(" L ") + " z" ]
        p.holes.times do |h|
          pts = []
          p.each_point_hole(h) { |pt| pts << ("%.12g %.12g" % [pt.x * dbu, pt.y * dbu]) }
          ctrs << [ "M " + pts.join(" L ") + " z" ]
        end
        d = ctrs.join(" ")
    
        @file.puts("  <path style=\"fill:##{'%06x' % (@fill_color & 0xffffff)}\"")
        @file.puts("        d=\"#{d}\"/>")
    
      end
    
    end
    
    lv = RBA::LayoutView::current 
    lv || raise("No view opened")
    
    cv = RBA::CellView::active 
    cv || raise("No layout loaded")
    ly = cv.layout 
    cell = cv.cell
    
    out = RBA::FileDialog::ask_save_file_name("Chose SVG file to write", ".", "SVG files (*.svg);;All files (*)")
    if out
    
      layers = []
    
      li = lv.begin_layers
      while !li.at_end?
        if li.current.visible? && !li.current.has_children? && li.current.layer_index >= 0
          layers << li.current
        end
        li.next
      end
    
      bbox = cell.bbox
      dbu = ly.dbu
    
      writer = SVGWriter::new(out)
      writer.start(bbox.width * dbu, bbox.height * dbu)
    
      canvas_tr = RBA::CplxTrans::new(1.0, 0.0, true, RBA::DPoint::new(-bbox.left, bbox.top))
    
      layers.each do |lp|
        writer.begin_layer(lp)
        si = cell.begin_shapes_rec(lp.layer_index)
        while !si.at_end?
          s = si.shape   
          if s.is_path? || s.is_box? || s.is_polygon?
            writer.polygon(s.polygon.transformed(canvas_tr * si.trans), dbu)
          end
          si.next
        end
      end
    
      writer.finish
    
    end
    

    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

  • edited November -1

    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

  • edited November -1

    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

  • edited November -1

    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:

    class SVGWriter
    
      def initialize(file)
        @file = File.open(file, "w")
      end
    
      def start(w, h)
        @file.puts(<<"END")
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <!-- Created with Inkscape (http://www.inkscape.org/) -->
    
    <svg
       xmlns:svg="http://www.w3.org/2000/svg"
       xmlns="http://www.w3.org/2000/svg"
       xmlns:xlink="http://www.w3.org/1999/xlink"
       width="#{'%.12g'%w}mm"
       height="#{'%.12g'%h}mm"
       viewBox="0 0 #{'%.12g'%w} #{'%.12g'%h}"
       version="1.1">   
    END
      end
    
      def finish
        @file.puts("</svg>")
        @file.close
      end
    
      def begin_layer(lp)
        @stroke_color = lp.frame_color
        @fill_color = lp.fill_color
        @file.puts("<g>")
      end
    
      def end_layer(lp)
        @file.puts("</g>")
      end
    
      def polygon(p, dbu)
    
        pts = []
        p.each_point_hull { |pt| pts << ("%.12g %.12g" % [pt.x * dbu, pt.y * dbu]) }
        ctrs = [ "M " + pts.join(" L ") + " z" ]
        p.holes.times do |h|
          pts = []
          p.each_point_hole(h) { |pt| pts << ("%.12g %.12g" % [pt.x * dbu, pt.y * dbu]) }
          ctrs << [ "M " + pts.join(" L ") + " z" ]
        end
        d = ctrs.join(" ")
    
        @file.puts("  <path style=\"fill:##{'%06x' % (@fill_color & 0xffffff)}\"")
        @file.puts("        d=\"#{d}\"/>")
    
      end
    
    end
    
    lv = RBA::LayoutView::current 
    lv || raise("No view opened")
    
    cv = RBA::CellView::active 
    cv || raise("No layout loaded")
    ly = cv.layout 
    cell = cv.cell
    
    out = RBA::FileDialog::ask_save_file_name("Chose SVG file to write", ".", "SVG files (*.svg);;All files (*)")
    if out
    
      layers = []
    
      li = lv.begin_layers
      while !li.at_end?
        if li.current.visible? && !li.current.has_children? && li.current.layer_index >= 0
          layers << li.current
        end
        li.next
      end
    
      bbox = cell.bbox
      dbu = ly.dbu/1000
    
      writer = SVGWriter::new(out)
      writer.start(bbox.width * dbu, bbox.height * dbu)
    
      canvas_tr = RBA::CplxTrans::new(1.0, 0.0, true, RBA::DPoint::new(-bbox.left, bbox.top))
    
      layers.each do |lp|
        writer.begin_layer(lp)
        si = cell.begin_shapes_rec(lp.layer_index)
        while !si.at_end?
          s = si.shape   
          if s.is_path? || s.is_box? || s.is_polygon?
            writer.polygon(s.polygon.transformed(canvas_tr * si.trans), dbu)
          end
          si.next
        end
        writer.end_layer(lp)
      end
    
      writer.finish
    
    end
    

    I think it would be great if this could be added to the "useful scripts."

Sign In or Register to comment.