Drawing Management

Create New Drawings

ezdxf.new(dxfversion='AC1009')

Create a new drawing from a template-drawing. The template-drawings are located in a template directory, which resides by default in the ezdxf package subfolder templates. The location of the template directory can be changed by the global option ezdxf.options.template_dir. dxfversion can be either 'AC1009' the official DXF version name or 'R12' the AutoCAD release name (release name works since ezdxf 0.7.4). You can only create new drawings for the 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

Open Drawings

You can open DXF drawings from disk or from a text-stream. (byte-stream usage is not implemented yet).

ezdxf.readfile(filename, encoding='auto', legacy_mode=False)

This is the preferred method to open existing DXF files. Read the DXF drawing from the file-system with auto-detection of encoding. Decoding errors will be ignored. Override encoding detection by setting parameter encoding to the estimated encoding. (use Python encoding names like in the open() function).

If parameter legacy_mode is True, ezdxf tries to reorder the coordinates of the LINE entity in DXF files from CAD applications which wrote the coordinates in the order: x1, x2, y1, y2. Additional fixes may be added later. The legacy mode has a speed penalty of around 5%.

Hint

Try option legacy_mode=True if error “Missing required y coordinate near line: …” occurs.

ezdxf.read(stream, legacy_mode=False)

Read DXF drawing from a text-stream, returns a Drawing object. Open the stream in text mode (mode=’rt’) and the correct encoding has to be set at the open function (in Python 2.7 use io.open()), the stream requires at least a readline() method. Since DXF version R2007 (AC1021) file encoding is always ‘utf-8’.

If parameter legacy_mode is True, ezdxf tries to reorder the coordinates of the LINE entity in DXF files from CAD applications which wrote the coordinates in the order: x1, x2, y1, y2, see also readfile() method.

Save Drawings

Save the drawing to the file-system by Drawing.save() or Drawing.saveas(). Write the drawing to a text-stream with Drawing.write(), the text-stream requires at least a write() method.

Global Options

Global options stored in ezdxf.options
ezdxf.options.compress_binary_data

If you don’t need access to binary data of DXF entities, you can compress them in memory for a lower memory footprint, set the global ezdxf.options.compress_binary_data = True to compress binray data for every drawing you open, but data compression cost time, so this option isn’t active by default. You can individually compress the binary data of a drawing with the method Drawing.compress_binary_data().

ezdxf.options.templatedir

Directory where the new() function looks for its template file (AC1009.dxf, AC1015.dxf, …) , default is None, which means the package subfolder templates. But if you want to use your own templates set this option ezdxf.options.template_dir = "my_template_directory". But you don’t really need this, just open your template file with ezdxf.readfile() and save the drawing as new file with the Drawing.saveas() method.

This option is very useful if the ezdxf package resides in a zip archive.

ezdxf.options.check_entity_tag_structures

Check app data (Application-Defined Codes) and xdata (Extended Data) tag structures, set this option to False for a little performance boost, default is True.