Drawing Management

Create New Drawings

ezdxf.new(dxfversion='AC1027', setup=False) → Drawing

Create a new Drawing from scratch, dxfversion can be either “AC1009” the official DXF version name or “R12” the AutoCAD release name.

new() can create drawings for following DXF versions:

Version

AutoCAD Release

AC1009

AutoCAD R12

AC1015

AutoCAD R2000

AC1018

AutoCAD R2004

AC1021

AutoCAD R2007

AC1024

AutoCAD R2010

AC1027

AutoCAD R2013

AC1032

AutoCAD R2018

Parameters
  • dxfversion – DXF version specifier as string, default is “AC1027” respectively “R2013”

  • setup

    setup default styles, False for no setup, True to setup everything or a list of topics as strings, e.g. [“linetypes”, “styles”] to setup only some topics:

    Topic

    Description

    linetypes

    setup line types

    styles

    setup text styles

    dimstyles

    setup default ezdxf dimension styles

    visualstyles

    setup 25 standard visual styles

Open Drawings

Open DXF drawings from file system or text stream, byte stream usage is not supported.

DXF files prior to R2007 requires file encoding defined by header variable $DWGCODEPAGE, DXF R2007 and later requires an UTF-8 encoding.

ezdxf supports reading of files for following DXF versions:

Version

Release

Encoding

Remarks

< AC1009

$DWGCODEPAGE

pre AutoCAD R12 upgraded to AC1009

AC1009

R12

$DWGCODEPAGE

AutoCAD R12

AC1012

R13

$DWGCODEPAGE

AutoCAD R13 upgraded to AC1015

AC1014

R14

$DWGCODEPAGE

AutoCAD R14 upgraded to AC1015

AC1015

R2000

$DWGCODEPAGE

AutoCAD R2000

AC1018

R2004

$DWGCODEPAGE

AutoCAD R2004

AC1021

R2007

UTF-8

AutoCAD R2007

AC1024

R2010

UTF-8

AutoCAD R2010

AC1027

R2013

UTF-8

AutoCAD R2013

AC1032

R2018

UTF-8

AutoCAD R2018

ezdxf.readfile(filename: str, encoding: str = None, errors: str = 'surrogateescape') → Drawing

Read the DXF document filename from the file-system.

This is the preferred method to load existing ASCII or Binary DXF files, the required text encoding will be detected automatically and decoding errors will be ignored.

Override encoding detection by setting argument encoding to the estimated encoding. (use Python encoding names like in the open() function).

If this function struggles to load the DXF document and raises a DXFStructureError exception, try the ezdxf.recover.readfile() function to load this corrupt DXF document.

Parameters
  • filename – filename of the ASCII- or Binary DXF document

  • encoding – use None for auto detect (default), or set a specific encoding like “utf-8”, argument is ignored for Binary DXF files

  • errors

    specify decoding error handler

    • ”surrogateescape” to preserve possible binary data (default)

    • ”ignore” to use the replacement char U+FFFD: “�”

    • ”strict” to raise an UnicodeDecodeError

Raises
  • IOError – File filename is not a DXF file or does not exist.

  • DXFStructureError – for invalid or corrupted DXF structures

  • UnicodeDecodeError – if errors is “strict” and a decoding error occurs

Deprecated since version v0.14: argument legacy_mode, use module ezdxf.recover to load DXF documents with structural flaws.

ezdxf.read(stream: TextIO) → Drawing

Read a DXF document from a text-stream. Open stream in text mode (mode='rt') and set correct text encoding, the stream requires at least a readline() method.

Since DXF version R2007 (AC1021) file encoding is always “utf-8”, use the helper function dxf_stream_info() to detect the required text encoding for prior DXF versions. To preserve possible binary data in XRECORD entities use errors='surrogateescape' as error handler for the import stream.

If this function struggles to load the DXF document and raises a DXFStructureError exception, try the ezdxf.recover.read() function to load this corrupt DXF document.

Parameters

stream – input text stream opened with correct encoding

Raises

DXFStructureError – for invalid or corrupted DXF structures

Deprecated since version v0.14: argument legacy_mode, use module ezdxf.recover to load DXF documents with structural flaws.

ezdxf.readzip(zipfile: str, filename: str = None, errors: str = 'surrogateescape') → Drawing

Load a DXF document specified by filename from a zip archive, or if filename is None the first DXF document in the zip archive.

Parameters
  • zipfile – name of the zip archive

  • filename – filename of DXF file, or None to load the first DXF document from the zip archive.

  • errors

    specify decoding error handler

    • ”surrogateescape” to preserve possible binary data (default)

    • ”ignore” to use the replacement char U+FFFD: “�”

    • ”strict” to raise an UnicodeDecodeError

Raises
  • IOError – File filename is not a DXF file or does not exist or if filename is None - no DXF file found.

  • DXFStructureError – for invalid or corrupted DXF structures

  • UnicodeDecodeError – if errors is “strict” and a decoding error occurs

ezdxf.decode_base64(data: bytes, errors: str = 'surrogateescape') → Drawing

Load a DXF document from base64 encoded binary data, like uploaded data to web applications.

Parameters
  • data – DXF document base64 encoded binary data

  • errors

    specify decoding error handler

    • ”surrogateescape” to preserve possible binary data (default)

    • ”ignore” to use the replacement char U+FFFD: “�”

    • ”strict” to raise an UnicodeDecodeError

Raises
  • DXFStructureError – for invalid or corrupted DXF structures

  • UnicodeDecodeError – if errors is “strict” and a decoding error occurs

Hint

This works well with DXF files from trusted sources like AutoCAD or BricsCAD, for loading DXF files with minor or major flaws look at the ezdxf.recover module.

Save Drawings

Save the DXF document to the file system by Drawing methods save() or saveas(). Write the DXF document to a text stream with write(), the text stream requires at least a write() method. Get required output encoding for text streams by property Drawing.output_encoding

Drawing Settings

The HeaderSection stores meta data like modelspace extensions, user name or saving time and current application settings, like actual layer, text style or dimension style settings. These settings are not necessary to process DXF data and therefore many of this settings are not maintained by ezdxf automatically.

Header variables set at new

$ACADVER

DXF version

$TDCREATE

date/time at creating the drawing

$FINGERPRINTGUID

every drawing gets a GUID

Header variables updated at saving

$TDUPDATE

actual date/time at saving

$HANDSEED

next available handle as hex string

$DWGCODEPAGE

encoding setting

$VERSIONGUID

every saved version gets a new GUID