Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

A Python package to create/manipulate DXF drawings.

Project description

ezdxf

Abstract

A Python package to create and modify DXF drawings, independent from the DXF version. You can open/save every DXF file without losing any content (except comments), Unknown tags in the DXF file will be ignored but preserved for saving. With this behavior it is possible to open also DXF drawings that contains data from 3rd party applications.

Quick-Info

  • ezdxf is a Python package to create new DXF files and read/modify/write existing DXF files
  • the intended audience are developers
  • requires at least CPython 3.5, for Python 2 support use ezdxf < 0.9
  • OS independent
  • additional required packages: pyparsing
  • MIT-License
  • read/write/new support for DXF versions: R12, R2000, R2004, R2007, R2010, R2013 and R2018
  • additional read support for DXF versions R13/R14 (upgraded to R2000)
  • additional read support for older DXF versions than R12 (upgraded to R12)
  • preserves third-party DXF content
  • additional fast DXF R12 writer, that creates just an ENTITIES section with support for the basic DXF entities

A simple example:

import ezdxf

# Create a new DXF document.
doc = ezdxf.new(dxfversion='R2010')

# Create new table entries (layers, linetypes, text styles, ...).
doc.layers.new('TEXTLAYER', dxfattribs={'color': 2})

# DXF entities (LINE, TEXT, ...) reside in a layout (modelspace, 
# paperspace layout or block definition).  
msp = doc.modelspace()

# Add entities to a layout by factory methods: layout.add_...() 
msp.add_line((0, 0), (10, 0), dxfattribs={'color': 7})
msp.add_text(
    'Test', 
    dxfattribs={
        'layer': 'TEXTLAYER'
    }).set_pos((0, 0.2), align='CENTER')

# Save DXF document.
doc.saveas('test.dxf')

Example for the r12writer, which writes a simple DXF R12 file without in-memory structures:

from random import random
from ezdxf.r12writer import r12writer

MAX_X_COORD = 1000
MAX_Y_COORD = 1000

with r12writer("many_circles.dxf") as doc:
    for _ in range(100000):
        doc.add_circle((MAX_X_COORD*random(), MAX_Y_COORD*random()), radius=2)

The r12writer supports only the ENTITIES section of a DXF R12 drawing, no HEADER, TABLES or BLOCKS section is present, except FIXED-TABLES are written, than some additional predefined text styles and line types are available.

Installation

Install with pip:

pip install ezdxf

Install development version (only if you have to)::

pip install git+https://github.com/mozman/ezdxf.git@develop

For Python 2 users:

pip install ezdxf<0.9

or from source:

python setup.py install

Website

https://ezdxf.mozman.at/

Documentation

Documentation of development version at https://ezdxf.mozman.at/docs

Documentation of latest release at http://ezdxf.readthedocs.io/

Contribution

The source code of ezdxf can be found at GitHub:

http://github.com/mozman/ezdxf.git

Only pull requests which can be merged into the develop branch will be accepted.

Feedback

Questions and feedback at Google Groups:

https://groups.google.com/d/forum/python-ezdxf

python-ezdxf@googlegroups.com

Questions at Stack Overflow:

Post questions at stack overflow and use the tag dxf or ezdxf.

Issue tracker at GitHub:

http://github.com/mozman/ezdxf/issues

Contact

Please post questions at the forum or stack overflow to make answers available to other users as well.

ezdxf@mozman.at

Feedback is greatly appreciated.

Manfred

News

Version 0.10.2 - 2019-10-05

  • NEW: Dimension.get_measurement() returns the actual dimension measurement in WCS units, no scaling applied; angular and ordinate dimension are not supported yet.
  • BUGFIX: ordinate dimension exports wrong feature location
  • BUGFIX: Hatch.set_pattern_fill() did not set pattern scale, angle and double values

Version 0.10.1 - 2019-09-07

  • BUGFIX: group code for header var $ACADMAINTVER is 90 for DXF R2018+ and 70 for previous DXF versions. This is a critical bug because AutoCAD 2012/2013 (and possibly earlier versions) will not open DXF files with the new group code 90 for header variable $ACADMAINTVER.

Version 0.10 - 2019-09-01

  • Release notes: https://ezdxf.mozman.at/release-v0-10.html
  • unified entity system for all DXF versions
  • saving as later DXF version than the source DXF version is possible, but maybe data loss if saving as an older DXF version than source DXF version (ezdxf is not a DXF converter)
  • templates no more needed and removed from package
  • CHANGE: DXFEntity
    • renamed DXFEntity.drawing to DXFEntity.doc
    • DXFEntity.get_xdata() keyword xdata_tag renamed to tags
    • DXFEntity.set_xdata() keyword xdata_tag renamed to tags
    • renamed DXFEntity.remove_reactor_handle() renamed to DXFEntity.discard_reactor_handle()
    • DXFEntity.get_extension_dict() returns ExtensionDict object instead of the raw DICTIONARY object
    • renamed DXFEntity.supports_dxf_attrib() to DXFEntity.is_supported_dxf_attrib()
    • renamed DXFEntity.dxf_attrib_exists() to DXFEntity.has_dxf_attrib()
  • CHANGE: Layer entity
    • removed Layer.dxf.line_weight as synonym for Layer.dxf.lineweight
    • renamed Layer.dxf.plot_style_name to Layer.dxf.plotstyle_handle
    • renamed Layer.dxf.material to Layer.dxf.material_handle
  • CHANGE: same treatment of Viewport entity for all DXF versions
  • CHANGE: Polyline.vertices() is now an attribute Polyline.vertices, implemented as regular Python list.
  • CHANGE: Insert.attribs() is now an attribute Insert.attribs, implemented as regular Python list.
  • CHANGE: renamed Viewport.dxf.center_point to Viewport.dxf.center
  • CHANGE: renamed Viewport.dxf.target_point to Viewport.dxf.target
  • CHANGE: direct access to hatch paths (Hatch.paths), pattern (Hatch.pattern) and gradient (Hatch.gradient), context manager to edit this data is not needed anymore, but still available for backward compatibility
  • CHANGE: Options
    • removed template_dir, no more needed
    • new log_unprocessed_tags to log unprocessed (unknown) DXF tags
  • CHANGE: Dimension() removes associated anonymous dimension block at deletion
  • CHANGE: safe block deletion protects not explicit referenced blocks like anonymous dimension blocks and arrow blocks
  • CHANGE: Importer add-on rewritten, API incompatible to previous ezdxf versions, but previous implementation was already broken
  • CHANGE: moved add_attdef() to generic layout interface, adding ATTDEF to model- and paperspace is possible
  • CHANGE: entity query - exclude DXF types from '*' search, by appending type name with a preceding '!' e.g. query for all entities except LINE = "* !LINE"
  • CHANGE: entity query - removed regular expression support for type name match
  • CHANGE: integration of MTextData methods into MText
  • CHANGE: removed edit_data, get_text, set_text methods from MText
  • restructured package, module and test file organization
  • NEW: support for Layer.dxf.true_color and Layer.dxf.transparency attributes (DXF R2004+, undocumented)
  • NEW: Layer.rgb, Layer.color, Layer.description and Layer.transparency properties
  • NEW: renaming a Layer also renames references to this layer, but use with care
  • NEW: support for adding LEADER entities
  • NEW: Dimension.get_geometry_block(), returns the associated anonymous dimension block or None
  • NEW: EntityQuery() got first and last properties, to get first or last entity or None if query result is empty
  • NEW: added ngon(), star() and gear() to ezdxf.render.forms
  • NEW: Source code generator to create Python source code from DXF entities, to recreate this entities by ezdxf. This tool creates only simple structures as a useful starting point for parametric DXF entity creation from existing DXF files. Not all DXF entities are supported!
  • NEW: support for named plot style files (STB)
  • NEW: can open converted Gerber DXF files tagged as "Version 1.0, Gerber Technology."
  • BUGFIX: fixed MTEXT and GEODATA text splitting errors (do not split at '^')
  • BUGFIX: fixed some subclass errors, mostly DXF reference errors
  • BUGFIX: VERTEX entity inherit owner and linetype attribute from POLYLINE entity
  • BUGFIX: MTEXT - replacement of \n by \P at DXF export to avoid invalid DXF files.
  • tested with CPython 3.8
  • removed batch files (.bat) for testing, use tox command instead

Version 0.9 - 2019-02-24

  • Release notes: https://ezdxf.mozman.at/release-v0-9.html
  • IMPORTANT: Python 2 support REMOVED, if Python 2 support needed: add ezdxf<0.9 to your requirements.txt
  • NEW: testing on Manjaro Linux in a VM by tox
  • CHANGE: converted NEWS.rst to NEWS.md and README.rst to README.md
  • CHANGE: moved Importer() from ezdxf.tools to ezdxf.addons - internal structures of modern DXF files are too complex and too undocumented to support importing data in a reliable way - using Importer() may corrupt your DXF files or just don't work!
  • NEW: type annotations to core package and add-ons.
  • NEW: argument setup in ezdxf.new('R12', setup=True) to setup default line types, text styles and dimension styles, this feature is disabled by default.
  • NEW: Duplicate table entries: dwg.styles.duplicate_entry('OpenSans', new_name='OpenSansNew'), this works for all tables, but is intended to duplicate STYLES and DIMSTYLES.
  • CHANGED: replaced proprietary fonts in style declarations by open source fonts
  • NEW: open source fonts to download https://github.com/mozman/ezdxf/tree/master/fonts
  • OpenSansCondensed-Light font used for default dimension styles
  • NEW: subpackage ezdxf.render, because of DIMENSION rendering
  • NEW: support for AutoCAD standard arrows
  • NEW: support for creating linear DIMENSION entities
  • NEW: background color support for MTEXT
  • CHANGE: DXF template cleanup, removed non standard text styles, dimension styles, layers and blocks
  • CHANGE: text style STANDARD uses txt font
  • CHANGE: renamed subpackage ezdxf.algebra to ezdxf.math
  • CHANGE: moved addons.curves to render.curves
  • CHANGE: moved addons.mesh to render.mesh
  • CHANGE: moved addons.r12spline to render.r12spline
  • CHANGE: moved addons.forms to render.forms
  • CHANGE: renamed construction helper classes into Construction...()
    • Ray2D() renamed to ConstructionRay()
    • Circle() renamed to ConstructionCircle()
    • Arc() renamed to ConstructionArc()
  • NEW: construction tools ConstructionLine() and ConstructionBox()
  • REMOVED: almost_equal use math.isclose
  • REMOVED: almost_equal_points use ezdxf.math.is_close_points
  • BUGFIX: closed LWPOLYLINE did not work in AutoCAD (tag order matters), introduced with v0.8.9 packed data structure
  • BUGFIX: UCS.to_ocs_angle_deg() corrected

Version 0.8.9 - 2018-11-28

  • Release notes: https://ezdxf.mozman.at/release-v0-8-9.html

  • IMPORTANT: Python 2 support will be dropped in ezdxf v0.9.0, because Python 2 support get more and more annoying.

  • CHANGE: refactoring of internal tag representation for a smaller memory footprint, but with some speed penalty

  • NEW: packed data for LWPOLYLINE points, faster __getitem__; added __setitem__, __delitem__, insert() and append() methods; renamed discard_points() in clear(); removed get_rstrip_points() and ctx manager rstrip_points(); user defined point format;

  • NEW: packed data for SPLINE, knots, weights, fit- and control points are stored as array.array(); Spline.get_knot_values(), Spline.get_weights(), Spline.get_control_points() and Spline.get_fit_points() are deprecated, direct access to this attributes by Spline.knot_values, Spline.weights, Spline.control_points and Spline.fit_points, all attributes with a list-like interface. Knot, control point and fit point counter updated automatically, therefore counters are read only now.

  • NEW: packed data for MESH, vertices, faces, edges and edge crease values stored as array.array(), high level interface unchanged

  • NEW: Drawing.layouts_and_blocks(), iterate over all layouts (mode space and paper space) and all block definitions.

  • NEW: Drawing.chain_layouts_and_blocks(), chain entity spaces of all layouts and blocks. Yields an iterator for all entities in all layouts and blocks

  • NEW: Drawing.query(), entity query over all layouts and blocks

  • NEW: Drawing.groupby(), groups DXF entities of all layouts and blocks by an DXF attribute or a key function

  • NEW: Layout.set_redraw_order() and Layout.get_redraw_order(), to change redraw order of entities in model space and paper space layouts

  • NEW: BlockLayout.is_layout_block, True if block is a model space or paper space block definition

  • NEW: ezdxf.algebra.Arc helper class to create arcs from 2 points and an angle or radius, or from 3 points

  • NEW: ezdxf.algebra.Arc.add_to_layout() with UCS support to create 3D arcs

  • NEW: rename paper space layouts by Drawing.layouts.rename(old_name, new_name)

  • NEW: Basic support for embedded objects (new in AutoCAD 2018), ezdxf reads and writes the embedded data as it is, no interpretation no modification, just enough to not break DXF files with embedded objects at saving.

  • CHANGE: Drawing.blocks.delete_block(name, safe=True), new parameter save, check if block is still referenced (raises DXFValueError)

  • CHANGE: Drawing.blocks.delete_all_blocks(safe=True), if parameter safe is True, do not delete blocks that are still referenced

  • BUGFIX: invalid CLASS definition for DXF version R2000 (AC1015) fixed, bug was only triggered at upgrading from R13/R14 to R2000

  • BUGFIX: fixed broken Viewport.AcDbViewport property

  • BASIC read support for many missing DXF entities/objects

    • ACAD_PROXY_GRAPHIC
    • HELIX
    • LEADER
    • LIGHT
    • MLEADER (incomplete)
    • MLINE (incomplete)
    • OLEFRAME
    • OLE2FRAME
    • SECTION
    • TABLE (incomplete)
    • TOLERANCE
    • WIPEOUT
    • ACAD_PROXY_OBJECT
    • DATATABLE
    • DICTIONARYVAR
    • DIMASSOC
    • FIELD (incomplete)
    • FIELDLIST (not documented by Autodesk)
    • IDBUFFER
    • LAYER_FILTER
    • MATERIAL
    • MLEADERSTYLE
    • MLINESTYLE
    • SORTENTSTABLE
    • SUN
    • SUNSTUDY (incomplete) (no real world DXF files with SUNSTUDY for testing available)
    • TABLESTYLE (incomplete)
    • VBA_PROJECT (no real world DXF files with embedded VBA for testing available)
    • VISUALSTYLE
    • WIPEOUTVARIABLES
    • for all unsupported entities/objects exist only raw DXF tag support

Version 0.8.8 - 2018-04-02

  • Release notes: https://ezdxf.mozman.at/release-v0-8-8.html
  • NEW: read/write support for GEODATA entity
  • NEW: read/(limited)write support for SURFACE, EXTRUDEDSURFACE, REVOLVEDSURFACE, LOFTEDSURFACE and SWEPTSURFACE entity
  • NEW: support for extension dictionaries
  • NEW: add_spline_control_frame(), create and add B-spline control frame from fit points
  • NEW: add_spline_approx(), approximate B-spline by a reduced count of control points
  • NEW: ezdxf.setup_linetypes(dwg), setup standard line types
  • NEW: ezdxf.setup_styles(dwg), setup standard text styles
  • NEW: LWPolyline.vertices() yields all points as (x, y) tuples in OCS, LWPolyline.dxf.elevation is the z-axis value
  • NEW: LWPolyline.vertices_in_wcs() yields all points as (x, y, z) tuples in WCS
  • NEW: basic __str__() and __repr__() support for DXF entities, returns just DXF type and handle
  • NEW: bulge related function in module ezdxf.algebra.bulge
  • NEW: Object Coordinate System support by DXFEntity.ocs() and OCS() class in module ezdxf.algebra
  • NEW: User Coordinate System support by UCS() class in module ezdxf.algebra
  • CHANGE: DXFEntity.set_app_data() and Entity.set_xdata accept also list of tuples as tags, DXFTag() is not required
  • BUGFIX: entity structure validator excepts group code >= 1000 before XDATA section (used in AutoCAD Civil 3D and AutoCAD Map 3D)

Version 0.8.7 - 2018-03-04

  • Release notes: https://ezdxf.mozman.at/release-v0-8-7.html
  • NEW: entity.get_layout() returns layout in which entity resides or None if unassigned
  • NEW: copy any DXF entity by entity.copy() without associated layout, add copy to any layout you want, by layout.add_entity().
  • NEW: copy entity to another layout by entity.copy_to_layout(layout)
  • NEW: move entity from actual layout to another layout by entity.move_to_layout(layout)
  • NEW: support for splines by control points: add_open_spline(), add_closed_spline(), add_rational_spline(), add_closed_rational_spline()
  • NEW: bspline_control_frame() calculates B-spline control points from fit points, but not the same as AutoCAD
  • NEW: R12Spline add-on, 2d B-spline with control frame support by AutoCAD, but curve is just an approximated POLYLINE
  • NEW: added entity.get_flag_state() and entity.set_flag_state() for easy access to binary coded flags
  • NEW: set new $FINGERPRINTGUID for new drawings
  • NEW: set new $VERSIONGUID on saving a drawing
  • NEW: improved IMAGE support, by adding RASTERVARIABLES entity, use Drawing.set_raster_variables(frame, quality, units)
  • BUGFIX: closing user defined image boundary path automatically, else AutoCAD crashes

Version 0.8.6 - 2018-02-17

  • Release notes: https://ezdxf.mozman.at/release-v0-8-6.html
  • NEW: ezdxf project website: https://ezdxf.mozman.at/
  • CHANGE: create all missing tables of the TABLES sections for DXF R12
  • BUGFIX: entities on new layouts will be saved
  • NEW: Layout.page_setup() and correct 'main' viewport for DXF R2000+; For DXF R12 page_setup() exists, but does not provide useful results. Page setup for DXF R12 is still a mystery to me.
  • NEW: Table(), MText(), Ellipse(), Spline(), Bezier(), Clothoid(), LinearDimension(), RadialDimension(), ArcDimension() and AngularDimension() composite objects from dxfwrite as add-ons, these add-ons support DXF R12
  • NEW: geometry builder as add-ons: MeshBuilder(), MeshVertexMerger(), MengerSponge(), SierpinskyPyramid(), these add-ons require DXF R2000+ (MESH entity)
  • BUGFIX: fixed invalid implementation of context manager for r12writer

Version 0.8.5 - 2018-01-28

  • Release notes: https://ezdxf.mozman.at/release-v0-8-5.html
  • CHANGE: block names are case insensitive 'TEST' == 'Test' (like AutoCAD)
  • CHANGE: table entry (layer, linetype, style, dimstyle, ...) names are case insensitive 'TEST' == 'Test' (like AutoCAD)
  • CHANGE: raises DXFInvalidLayerName() for invalid characters in layer names: <>/":;?*|=`
  • CHANGE: audit process rewritten
  • CHANGE: skip all comments, group code 999
  • CHANGE: removed compression for unused sections (THUMBNAILSECTION, ACDSDATA)
  • NEW: write DXF R12 files without handles: set dwg.header['$HANDLING']=0, default value is 1
  • added subclass marker filter for R12 and prior files in legacy_mode=True (required for malformed DXF files)
  • removed special check for Leica Disto Unit files, use readfile(filename, legacy_mode=True) (malformed DXF R12 file, see previous point)

Version 0.8.4 - 2018-01-14

  • Release notes: https://ezdxf.mozman.at/release-v0-8-4.html

  • NEW: Support for complex line types with text or shapes

  • NEW: DXF file structure validator at SECTION level, tags outside of sections will be removed

  • NEW: Basic read support for DIMENSION

  • CHANGE: improved exception management, in the future ezdxf should only raise exceptions inherited from DXFError for DXF related errors, previous exception classes still work

    • DXFValueError(DXFError, ValueError)
    • DXFKeyError(DXFError, KeyError)
    • DXFAttributeError(DXFError, AttributeError)
    • DXFIndexError(DXFError, IndexError)
    • DXFTableEntryError(DXFValueError)
  • speedup low level tag reader around 5%, and speedup tag compiler around 5%

Version 0.8.3 - 2018-01-02

  • CHANGE: Lwpolyline - suppress yielding z coordinates if they exists (DXFStructureError: z coordinates are not defined in the DXF standard)
  • NEW: setup creates a script called 'dxfpp' (DXF Pretty Printer) in the Python script folder
  • NEW: basic support for DXF format AC1032 introduced by AutoCAD 2018
  • NEW: ezdxf use logging and writes all logs to a logger called 'ezdxf'. Logging setup is the domain of the application!
  • NEW: warns about multiple block definitions with the same name in a DXF file. (DXFStructureError)
  • NEW: legacy_mode parameter in ezdxf.read() and ezdxf.readfile(): tries do fix coordinate order in LINE entities (10, 11, 20, 21) by the cost of around 5% overall speed penalty at DXF file loading

Version 0.8.2 - 2017-05-01

  • NEW: Insert.delete_attrib(tag) - delete ATTRIB entities from the INSERT entity
  • NEW: Insert.delete_all_attribs() - delete all ATTRIB entities from the INSERT entity
  • BUGFIX: setting attribs_follow=1 at INSERT entity before adding an attribute entity works

Version 0.8.1 - 2017-04-06

  • NEW: added support for constant ATTRIB/ATTDEF to the INSERT (block reference) entity
  • NEW: added ATTDEF management methods to BlockLayout (has_attdef, get_attdef, get_attdef_text)
  • NEW: added (read/write) properties to ATTDEF/ATTRIB for setting flags (is_const, is_invisible, is_verify, is_preset)

Version 0.8.0 - 2017-03-28

  • added groupby(dxfattrib='', key=None) entity query function, it is supported by all layouts and the query result container: Returns a dict, where entities are grouped by a dxfattrib or the result of a key function.
  • added ezdxf.audit() for DXF error checking for drawings created by ezdxf - but not very capable yet
  • dxfattribs in factory functions like add_line(dxfattribs=...), now are copied internally and stay unchanged, so they can be reused multiple times without getting modified by ezdxf.
  • removed deprecated Drawing.create_layout() -> Drawing.new_layout()
  • removed deprecated Layouts.create() -> Layout.new()
  • removed deprecated Table.create() -> Table.new()
  • removed deprecated DXFGroupTable.add() -> DXFGroupTable.new()
  • BUFIX in EntityQuery.extend()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for ezdxf, version 0.10.2
Filename, size File type Python version Upload date Hashes
Filename, size ezdxf-0.10.2-py3-none-any.whl (458.7 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size ezdxf-0.10.2.zip (787.6 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page