DXF Format

This is a brief description of the DXF format or more precisely: the subset of DXF that KLayout understands. Syntactically, DXF is a very simple format. The basic problem when reading DXF however is how to interpret it. Apparently, it is more or less a dump of the object properties of the CAD tool, and many questions regarding the interpretation of the properties are left open.

The implementation of KLayout is based on a number of test cases and comparison with other viewers. In some cases, the interpretation of features was varying (for example, the interpretation of array instances). In that case, TrueView (see link below) was believed to be the main authority in DXF interpretation.

Here are some links and references to other free viewers:

Basic format

DXF consists of records of a very simple structure:

<Group-Code> <Value>

The group code is an integer which implicitly defines the value type and acts as a key for the value. In ASCII DXF, group codes and values are written in a single line each. These are the group codes and values relevant for layout data:

  • 0: string (keyword)
  • 2: string (arbitrary)
  • 6: string
  • 8: string
  • 10..13: double precision coordinate
  • 20..23: double precision coordinate
  • 30..33: double precision coordinate
  • 39: double precision coordinate
  • 40..45: double precision floating point value
  • 50: double precision angle (in degree)
  • 62: 16 bit integer value
  • 66: 16 bit integer value
  • 70: 16 bit integer value
  • 210: double precision coordinate
  • 220: double precision coordinate
  • 230: double precision coordinate

Pure ASCII DXF allows the lines to contain leading or trailing blanks. KLayout also tries to identify stray blank lines and skip them. Some systems generate such files.

There is also a binary version of the DXF format with this basic structure:

  • Header (22 Bytes): "AutoCAD Binary DXF<CR><LF><SUB><NULL>
  • 16 bit integers: 2 btyes, LSB firs
  • double: 8 bytes, LSB firs
  • string: zero-terminated

Apparently the binary format is rarely used. It suffers from a pretty basic issue: since the data type of a value - hence the byte count - is implicitly determined by the group code, exact knowledge of the data type associated with each group code is required.

General structure

This is the general structure of a DXF file as KLayout understands it. This is a schematic representation. The "//" comments are not part of the file - they are provided for readability. In addition, group codes and values have been written on one line. In ASCII DXF, group codes and values are on separate lines. The indentation indicates coherent sections. "..." indicates that other group code / value pairs may be present which are read over:

   // header section
   0  "SECTION"
   2  "HEADER"
   ...
   0  "ENDSEC"
   ...
   // tables section
   0  "SECTION"
   2  "TABLES"
   ... 
     // layer table
     0  "TABLE"
     2  "LAYER"
     70 (# of layers - do not use for reading)
     ...
       // each layer
       0  "LAYER"
       2  (layer name)
       62 (color code)
       6  (line style)
       ...
     0  "ENDTAB"
   ...
   0  "ENDSEC"     
   ...
   // blocks section 
   0  "SECTION"
   2  "BLOCKS"
   ...
     // each block
     0  "BLOCK"
     8  (layer name - always 0?)
     2  (block name)
     70 (flags - always 64?)
     10 (base point X)
     20 (base point Y)
     ...
       // each entity
       0  (entity type)
       ... (specific for entity)
     ...
     0  "ENDBLK"
   ...
   0  "ENDSEC"     
   ...
   // entities (top level cell)
   0  "SECTION"
   2  "ENTITIES"
   ...
     // each entity
     0  (entity type)
     ... (specific for entity)
   ...
   0  "ENDSEC"     
   ...
   // end of file
   0  "EOF"

KLayout reads these entities which are described in detail below:

  • POLYLINE: polygon, paths
  • LWPOLYLINE: polygon, paths
  • SPLINE: a spline line segment
  • TRACE: a four-point polygon
  • ARC: an arc segment
  • ELLIPSE: an elliptic segment
  • INSERT: cell reference
  • LINE: parts of polygon contours, path
  • SOLID: triangle or tetragon
  • HATCH: a filled area (polygon)
  • CIRCLE: a circle
  • DIMENSION: a dimension drawing
  • TEXT: text
  • ATTRIB: text
  • MTEXT: multi-line text

POLYLINE entity

  0  "POLYLINE"
  8  (layer name)
  210 (extrusion direction x)
  220 (extrusion direction y)
  230 (extrusion direction z)
  70 (flags - bit 0 for closed (1) or open (0) polyline)
  40 (start width)
  41 (end width)
  ...
    // each vertex
    0  "VERTEX"
    8  (layer name - what for?)
    10 (position X)
    20 (position Y)
    42 (bulge)
    40 (start width)
    41 (end width)
  ...
  0  "SEQEND"

A closed polyline with a width of 0 usually creates a polygon (unless in "keep lines" mode - see below). A polyline with a width > 0 creates a path. A non-closed polyline with a width of 0 creates a path with width 0 or contributes to the edges that will be merged in "merge lines" mode.

Individual widths are not supported - all widths must be equal or just a common width must be given.

For paths: no round ends are provided. Variable extensions have to be emulated by extending the first and last segment.

LWPOLYLINE entity for polygons

  0  "LWPOLYLINE"
  8  (layer name)
  210 (extrusion direction x)
  220 (extrusion direction y)
  230 (extrusion direction z)
  70 (flags - see POLYLINE)
  42 (bulge)
  43 (common width)
  ...
    // each vertex
    10 (position X)
    20 (position Y)
    40 (start width of segment)
    41 (end width  of segment)
  ...
  0  "SEQEND"

LWPOLYLINE entities are alternative representations of POLYLINE entities and are treated alike.

ARC entity

  0  "ARC"
  8  (layer name)
  210 (extrusion direction x)
  220 (extrusion direction y)
  230 (extrusion direction z)
  10 (center X)
  20 (center Y)
  50 (start angle)
  51 (end angle)
  ...

An ARC creates a path with width 0 or contributes to the edges that will be merged in "merge lines" mode. The arc will be interpolated according to the number of points per circle specified in the reader options.

ELLIPSE entity

  0  "ELLIPSE"
  8  (layer name)
  210 (extrusion direction x)
  220 (extrusion direction y)
  230 (extrusion direction z)
  10 (center X)
  20 (center Y)
  11 (main axis X)
  21 (main axis Y)
  40 (minor axis radius)
  41 (start angle)
  42 (end angle)
  ...

An ELLIPSE creates a path with width 0 or contributes to the edges that will be merged in "merge lines" mode. The arc will be interpolated according to the number of points per circle specified in the reader options.

SPLINE entity

  0  "SPLINE"
  8  (layer name)
  210 (extrusion direction x)
  220 (extrusion direction y)
  230 (extrusion direction z)
  71  (degree)
  ...
    // each vertex
    10 (position X)
    20 (position Y)
  ...
    // each knot
    40 (knot value)
  ...

A spline creates a path with width 0 or contributes to the edges that will be merged in "merge lines" mode.

TRACE entity

  0  "TRACE"
  8  (layer name)
  210 (extrusion direction x)
  220 (extrusion direction y)
  230 (extrusion direction z)
  2  (block name)
  10 (position1 X)
  20 (position1 Y)
  11 (position2 X)
  21 (position2 Y)
  12 (position3 X)
  22 (position3 Y)
  13 (position4 X)
  23 (position4 Y)
  ...

A TRACE entity generates a polygon with four vertices.

SOLID entity

  0  "SOLID"
  8  (layer name)
  210 (extrusion direction x)
  220 (extrusion direction y)
  230 (extrusion direction z)
  2  (block name)
  10 (position1 X)
  20 (position1 Y)
  11 (position2 X)
  21 (position2 Y)
  12 (position3 X)
  22 (position3 Y)
  13 (position4 X)
  23 (position4 Y)
  ...

To get a correctly ordered tetragon, points 3 and 4 must be swapped. A triangle is formed by setting position 3 and 4 to the same coordinates.

DIMENSION entity

  0  "DIMENSION"
  2  (block name)
  8  (layer)
  ...

A DIMENSION entity is only implemented as importing the drawing block without a displacement using the given layer on "0" wildcard layers inside the block.

INSERT entity

  0  "INSERT"
  8  (layer name)
  2  (block name)
  10 (position X)
  20 (position Y)
  41 (scale factor X - can be negative for mirroring)
  42 (scale factor Y - can be negative for mirroring)
  50 (rotation angle)
  70 (number of columns - optional)
  71 (number of rows - optional)
  44 (column spacing - optional)
  45 (row spacing - optional)
  ...

The layer specified overrides the "0" layer inside the block. This requires layer specific variants. This override is inherited by child cells as well.

The array vectors specified by number of column spacing and row spacing is rotated by the given angle, but not scaled or mirrored.

LINE entity

  0  "LINE"
  8  (layer name)
  210 (extrusion direction x)
  220 (extrusion direction y)
  230 (extrusion direction z)
  10 (start position X)
  20 (start position Y)
  11 (start position X)
  21 (start position Y)
  41 (scale factor X - can be -1 for mirroring)
  42 (scale factor Y - can be -1 for mirroring)
  39 (thickness - can be 0)
  ...

Lines are converted into paths with the specified width or contribute to the lines merged in "merge lines" mode.

CIRCLE entity

  0  "CIRCLE"
  8  (layer name)
  210 (extrusion direction x)
  220 (extrusion direction y)
  230 (extrusion direction z)
  10 (center position X)
  20 (center position Y)
  40 (radius)
  ...

Circles are converted to single-point, round-ended paths.

TEXT entity

  0  "TEXT"
  8  (layer name)
  210 (extrusion direction x)
  220 (extrusion direction y)
  230 (extrusion direction z)
  10 (position X)
  20 (position Y)
  11 (second alignment position X)
  21 (second alignment position Y)
  40 (height)
  50 (rotation)
  71 (mirror flags)
  72 (horizontal alignment)
  73 (vertical alignment)
  1  (text string)

MTEXT entity

  0  "MTEXT"
  8  (layer name)
  210 (extrusion direction x)
  220 (extrusion direction y)
  230 (extrusion direction z)
  10 (position X)
  20 (position Y)
  11 (baseline vector X)
  21 (baseline vector Y)
  40 (height)
  41 (width)
  46 (alternative width)
  50 (rotation)
  71 (alignment flags)
  75 (columns flag)
  1  (text string)
  3  (text string continuation)

ATTRIB entity

The ATTRIB entity is basically handled like the TEXT entity except that group 74 is used for the vertical alignment.

HATCH entity

  0  "HATCH"
  8  (layer name)
  210 (extrusion direction x)
  220 (extrusion direction y)
  230 (extrusion direction z)
  91 (number of loops (contours))
    //  each loop:
    92 (flags, usually 3: External (bit 0) | Polyline (bit 1))
    93 (number of edges in the first loop)
      //  each point:
      10 (position X)
      20 (position Y)
      ... more points with 10/20 group codes
    ... more loops (group codes 92, 93, 10, 20 ...)
  ...

A hatch entity creates a polygon. Individual polygon parts are combined with the even-odd union rule: an overlapping contour can cancel an area of another contour even if the orientation of both is the same.

Other topics

Polygon formation and LINE/POLYLINE interpretation

The are several ways to form polygons from DXF input, which are controlled by the LINE/POLYLINE mode setting on the reader options page. The following modes are provided:

  • Automatic
  • Keep lines
  • Create polygons from closed polylines with width = 0
  • Merge lines with width = 0 into polygons
  • Merge lines and auto-close open contours

"Automatic" mode will select the following modes:

  • "Keep lines" if at least one SOLID or HATCH entity is present
  • "Create polygons from closed polylines with width = 0" if at least one closed POLYLINE or LWPOLYLINE entity with width = 0 is present
  • "Merge lines with width = 0 into polygons" otherwise.

The modes have the following effect:

  • SOLID entities always form filled tetragons or triangles.
  • HATCH entities always form complex polygons.
  • Closed POLYLINE or LWPOLYLINE entities with width = 0 form polygons unless "keep lines" mode is selected. In "auto-close" mode, non-closed polylines will form a closed polygon by connecting the first and last point.
  • Multiple segments specified by either SPLINE,ARC,ELLIPSE,LINE or POLYLINE/LWPOLYLINE entities with width = 0 are joined and, if they form a loop, create a polygon in the "merge lines" modes. In "auto-close" mode, open contours will be closed by connecting the first and last point. Reverse-oriented loops can create holes in outer loops.

Extrusion direction

The extrusion direction specified by the group codes 210, 220 and 230 is by default (0,0,1). This is the normal case. Extrusion direction (0,0,-1) is also supported. In this case, the shapes will be mirrored at the Y axis.

INSERT entities with layer specification

Layer "0" is a "wildcard" layer and can be overridden on a per-instance basis by a instance specific layer. If the instance has itself "0" layer assigned, no override takes place (or it does not have any effect).