dxfgrabber 0.6.0 documentation

last updated January 09, 2014.

dxfgrabber is a Python library to grab information from DXF drawings - all DXF versions supported.

Python compatibility: dxfgrabber is tested with CPython 2.7, CPython3.3 and PyPy.

License: dxfgrabber is licensed under the MIT license.

simple usage:

dxf = dxfgrabber.readfile("drawing.dxf")
print("DXF version: {}".format(dxf.dxfversion))
header_var_count = len(dxf.header) # dict of dxf header vars
layer_count = len(dxf.layers) # collection of layer definitions
block_definition_count(len(dxf.blocks)) #  dict like collection of block definitions
entitiy_count = len(dxf.entities) # list like collection of entities

Read DXF files

readfile(filename[, options=None])

Read DXF file filename from the file system, and returns an object Drawing. options is a dict with options for reading DXF files.

read(stream[, options=None])

Like readfile(), but reads the DXF data from a stream. stream only requires a method readline()

Options dict for reading DXF files

default options:

DEFAULT_OPTIONS = {
    "grab_blocks": True
}
key description
grab_blocks if True read block definitions from DXF file, else the dict Drawing.blocks is empty.

Drawing Content

class Drawing

Contains all collected data from the DXF file.

Drawing.dxfversion

DXF version as string.

DXF AutoCAD Version
AC1009 AutoCAD R12
AC1015 AutoCAD R2000
AC1018 AutoCAD R2004
AC1021 AutoCAD R2007
AC1024 AutoCAD R2010
AC1027 AutoCAD R2013
Drawing.encoding

content encoding, default is cp1252

Drawing.filename

filename if read from a file.

Drawing.header

Contains all the DXF header vars in a dict like object. For explanation of DXF header vars and their content see the DXF specifications from Autodesk. Header var content are basic Python types like string, int, and float as simple types and tuples of float values for 2D- and 3D points.

Drawing.layers

Contains all layer definitions in an object of type LayerTable.

Drawing.styles

Contains all text style definitions in an object of type StyleTable.

Drawing.linetypes

Contains all linetype definitions in an object of type LinetypeTable.

Drawing.blocks

Contains all block definitions in a dict like object of type BlocksSection.

Drawing.entities

Contains all drawing entities in a list like object of type EntitySection.

Drawing.modelspace()

Iterate over all DXF entities in modelspace.

Drawing.paperspace()

Iterate over all DXF entities in paperspace.

Layer Table

class LayerTable

Contains all layer definitions as objects of type Layer.

LayerTable.get(name)

Return layer name as object of type Layer. Raises KeyError

LayerTable.__getitem__(name)

Support for index operator: dwg.layers[name]

LayerTable.names(name)

Returns a sorted list of all layer names.

LayerTable.__iter__()

Iterate over all layers, yields Layer objects.

LayerTable.__len__()

Returns count of layers, support for standard len() function.

Layer

class Layer
Layer.name

Layer name as string

Layer.color

Layer color as int in range 1 to 255.

Layer.linetype

Layer linetype as string.

Layer.locked

type is bool

Layer.frozen

type is bool

Layer.on

type is bool

Style Table

class StyleTable

Contains all text style definitions as objects of type Style.

StyleTable.get(name)

Return text style name as object of type Style. Raises KeyError

StyleTable.__getitem__(name)

Support for index operator: dwg.styles[name]

StyleTable.names(name)

Returns a sorted list of all text style names.

StyleTable.__iter__()

Iterate over all text styles, yields Style objects.

StyleTable.__len__()

Returns count of text styles, support for standard len() function.

Style

class Style

TODO

Linetype Table

class LinetypeTable

Contains all linetype definitions as objects of type Linetype.

LinetypeTable.get(name)

Return linetype name as object of type Linetype. Raises KeyError

LinetypeTable.__getitem__(name)

Support for index operator: dwg.linetypes[name]

LinetypeTable.names(name)

Returns a sorted list of all linetype names.

LinetypeTable.__iter__()

Iterate over all linetypes, yields Linetype objects.

LinetypeTable.__len__()

Returns count of linetypes, support for standard len() function.

Linetype

class Linetype

TODO

Blocks Section

class BlocksSection

Contains all block definitions as objects of type Block.

BlocksSection.__len__()

Returns count of blocks, support for standard len() function.

BlocksSection.__iter__()

Iterates over blocks, yields Block objects.

BlocksSection.__contains__(self, name)

Returns True if a block name exists, support for standard in operator.

BlocksSection.__getitem__(name)

Returns block name, support for the index operator: block = dwg.blocks[name]. Raises KeyError

BlocksSection.get(name[, default=None])

Returns block name if exists or default.

Entity Section

class EntitySection

Contains all drawing entities.

EntitySection.__len__()

Returns count of entities, support for standard len() function.

EntitySection.__iter__()

Iterates over all entities.

EntitySection.__getitem__(index)

Returns entity a location index, slicing is possible, support for the index operator dwg.entity = entities[index]. Raises IndexError

example for accessing entities:

dwg = dxfgrabber.readfile('test.dxf')
all_layer_0_entities = [entity for entity in dwg.entities if entity.layer == '0']

Entity Types

Base Class Shape

class Shape

Base class for all drawing entities.

Shape.paperspace

True for paperspace and False for modelspace.

Shape.dxftype

DXF entity name, like CIRCLE or LINE

Shape.layer

Layer name as string

Shape.linetype

Linetype as string or None, None means linetype by layer.

Shape.thickness

Element thickness as float.

Shape.ltscale

Linetype scale as float

Shape.invisible

True if entity is invisible.

Shape.color

Entity color as int, where 256 means color by layer and 0 means color by block.

Block

class Block(Shape)
Block.basepoint

Base point of block definition as 2D- or 3D point of type tuple.

Block.name

Block name as string

Block.flags

Block flags as int, for explanation see the DXF specifications from Autodesk and see also Block.is_... properties.

Block.xrefpath

Path to external reference as string

Block.is_xref

True if block is an external reference.

Block.is_xref_overlay

True if block is an external overlay reference.

Block.is_anonymous

True if block is an anonymous block, created by hatch or dimension.

Block.__iter__:

Support for iterator protocol, iterates over all block entities.

Block.__getitem__(index):

Returns block entity at location index, slicing is supported.

Block.__len__():

Returns count of block entities, support for standard len() function.

Line

class Line(Shape)
Line.start

Start point of line (x, y[, z]) as tuple

Line.end

End point of line (x, y[, z]) as tuple

Point

class Point(Shape)
Point.point

Location of point (x, y[, z]) as tuple

Circle

class Circle(Shape)
Circle.center

Location of circle center point (x, y[, z]) as tuple

Circle.radius

Circle radius as float

Arc

class Arc(Shape)
Arc.center

Location of arc center point (x, y[, z]) as tuple

arc.radius

Arc radius as float

arc.startangle

Arc startangle in degrees as float. (full circle = 360 degrees)

arc.endangle

Arc endangle in degrees as float. (full circle = 360 degrees)

Solid

class Solid(Shape)

A solid filled shape with 4 points. For Triangles point 3 and point 4 has the same location.

Solid.points

List of points (x, y[, z]) as tuple.

Trace

class Trace(Solid)

Same as Solid.

Face

class Face(Trace)

A solid filled 3D shape with 4 points. For Triangles point 3 and point 4 has the same location. DXF entity 3DFACE

Face.points

List of points (x, y, z) as tuple.

Face.is_edge_invisible(index)

Returns True if edge index is invisible, index in [0, 1, 2, 3].

Text

class Text(Shape)
Text.insert

Location of text (x, y, z) as tuple.

Text.text

Text content as string.

Text.height

Text height as float

Text.rotation

Rotation angle in degrees as float. (full circle = 360 degrees)

Text.style

Text style name as string

Text.halign

Horizontal alignment as int.

Text.valign

Vertical alignment as int.

Text.alignpoint

Second alignment point as tuple or None.

Attrib

class Attrib(Text)

A text entity, in usual cases attached to a block reference entity Insert, inherits from Text.

Attrib.tag

The attribute tag as string.

Attdef

Same as Attrib, but located in a block definition entity Block.

Insert

class Insert(Shape)
Insert.name

Name of block definition as string.

Insert.insert

Location of block reference (x, y, z) as tuple.

Insert.rotation

Rotation angle in degrees as float. (full circle = 360 degrees)

Insert.scale

(x, y, z) block scaling as tuple, default is (1.0, 1.0, 1.0)

Insert.attribs

List of Attrib entities attached to the Insert entity.

Insert.find_attrib(tag):

Get Attrib entity by tag, returns None if not found.

Polyline

class Polyline(Shape)

Multiple 2D- or 3D vertices connected by lines. The DXF entity POLYLINE is also used to define Polyfaces and Polymeshes, dxfgrabber defines separated classes for this entities see: Polyface and Polymesh.

is_closed

True if polyline is closed.

Polyline.__getitem__(index)

Returns vertex index as Vertex entity. support for standard operator vertex = polyline[index]. Raises IndexError

Polyline.__len__()

Returns count of vertices.

Polyline.__iter__()

Iterate of all vertices, as Vertex entity.

Polyline.points()

Returns a generator over all vertex locations (x, y, z) as tuple.

Vertex

class Vertex(Shape)
Vertex.location

Location (x, y, z) as tuple.

Vertex.bulge

The bulge is the tangent of one fourth the included angle for an arc segment, made negative if the arc goes clockwise from the start point to the endpoint. A bulge of 0 indicates a straight segment, and a bulge of 1 is a semicircle. If you have questions ask Autodesk.

Vertex.tangent

Curve fitting tangent in degrees as float or None. (full circle = 360 degrees)

Polyface

class Polyface(Shape)

Dxftype is POLYFACE, which is a POLYLINE DXF entity.

Polyface.__getitem__(index)

Returns face index as SubFace. support for standard operator face = polyface[index]. Raises IndexError

Polyface.__len__()

Returns count of faces.

Polyface.__iter__()

Iterate of all faces, as SubFaces.

SubFace

A SubFace is a list of Vertex, as part of a Polyface, the location of a vertex of a SubFace is: subface[index].location

Polymesh

class Polymesh(Shape)

Dxftype is POLYMESH, which is a POLYLINE DXF entity.

A Polymesh is a grid of m x n vertices, where every vertex has its own 3D location.

Polymesh.mcount

Count of vertices in m direction as int.

Polymesh.ncount

Count of vertices in n direction as int.

Polymesh.is_mclosed

True if Polymesh is closed in m direction.

Polymesh.is_nclosed

True if Polymesh is closed in n direction.

Polymesh.get_vertex(pos)

Returns the Vertex at pos, where pos is a tuple (m, n). First vertex is (0, 0).

Polymesh.get_location(pos)

Returns the location (x, y, z) as tuple at pos, where pos is a tuple (m, n). First vertex is (0, 0).

LWPolyline

class LWPolyline(Shape)

LWPolyline is a lightweight only 2D Polyline.

LWPolyline.points

List of 2D polyline points (x, y) as tuple.

LWPolyline.is_closed

True if the polyline is closed.

LWPolyline.__len__()

Returns the count of polyline points.

LWPolyline.__getitem__(index)

Returns polyline point (x, y) as tuple at position index, slicing is supported. Raises IndexError

LWPolyline.__iter__()

Iterate over all polyline points (x, y) as tuple.

Ellipse

class Ellipse(Shape)
Ellipse.center

Location of ellipse center point (x, y[, z]) as tuple

Ellipse.majoraxis

End point of major axis (x, y[, z]) as tuple

Ellipse.ratio

Ratio of minor axis to major axis as float.

Ellipse.startparam

Start parameter (this value is 0.0 for a full ellipse).

Ellipse.endparam

End parameter (this value is 2pi for a full ellipse)

Ray

class Ray(Shape)
start
Location of the ray start point (x, y, z) as tuple
unitvector

Ray direction as unit vector (x, y, z) as tuple

XLine

class XLine(Ray)

Same as Ray, except a XLine (construction line) has no beginning and no end.

Spline

class Spline(Shape)
Spline.degree

Degree of the spline curve as int

Spline.starttangent

Start tangent as (x, y, z) as tuple or None

Spline.endtangent

End tangent as (x, y, z) as tuple or None

Spline.controlpoints

List of control points (x, y, z) as tuple

Spline.fitpoints

List of fit points (x, y, z) as tuple

Spline.knots

List of knot values as float

Spline.weights

List of weight values as float

Spline.normalvector

Normal vector if spline is planar else None.

Spline.is_closed
Spline.is_periodic
Spline.is_rational
Spline.is_planar
Spline.is_linear

MText

class MText(Shape)

Multi line text entity.

MText.insert

Location of text (x, y, z) as tuple.

MText.rawtext

Whole text content as one string.

MText.height

Text height as float

MText.linespacing

Text line spacing as float, valid from 0.25 to 4.00.

MText.attachmentpoint

Text attachment point as int.

Value Description
1 Top left
2 Top center
3 Top right
4 Middle left
5 Middle center
6 Middle right
7 Bottom left
8 Bottom center
9 Bottom right
MText.style

Text style name as string.

MText.xdirection

X-Axis direction vector as (x, y, z) as tuple. (unit vector)

MText.lines()

Returns a list of lines. It is the MText.rawtext splitted into lines by the \P character.

Howtos

Open a DXF file

Open files from file system:

dwg = readfile("myfile.dxf")

To read file from a stream use: read()

Query Header Variables

The HEADER section of a DXF file contains the settings of variables associated with the drawing.

Example:

dxfversion = dwg.header['$ACADVER']

For available HEADER variables and their meaning see: DXF Reference

Query Entities

All entities of the DXF drawing, independent from modelspace or paperspace, resides in the Drawing.entities attribute and is an EntitySection object. Iterate over all entities with the in operator:

all_lines = [entity for entity in dwg.entities if entity.dxftype == 'LINE']
all_entities_at_layer_0 = [entity for entity in dwg.entities if entity.layer == '0']

Query Blocks

Block references are just DXF entities called INSERT.

Get all block references for block TestBlock:

references = [entity for entity in dwg.entities if entity.dxftype == 'INSERT' and entity.name == 'TestBlock']

See available attributes for the Insert entity.

To examine the Block content, get the block definition from the blocks section:

test_block = dwg.blocks['TestBlock']

and use the in operator (Iterator protocol):

circles_in_block = [entity for entity in test_block if entity.dxftype == 'CIRCLE']

Layers

Layers are nothing special, they are just another attribute of the DXF entity, dxfgrabber stores the layer as a simple string. The DXF entitiy can inherit some attributes from the layer: color, linetype

To get the real value of an attribute value == BYLAYER, get the layer definition:

layer = dwg.layers[dxf_entity.layer]
color = layer.color if dxf_entity.color == dxfgrabber.BYLAYER else dxf_entity.color
linetype = layer.linetype if dxf_entity.linetype is None else dxf_entity.linetype

Layers can be locked (if True else unlocked), on (if True else off) or frozen (if True else thawed).

Layouts (Modelspace or Paperspace)

dxfgrabber just supports the paperspace attribute, it is not possible to examine in which layout a paperspace object resides (DXF12 has only one paperspace).

Get all modelspace entities:

modelspace_entities = [entity for entity in dwg.entities if not entity.paperspace]

shortcuts since 0.5.1:

modelspace_entities = list(dwg.modelspace())
paperspace_entities = list(dwg.paperspace())