Drawing Object

class ezdxf.document.Drawing

The Drawing class manages all entities and tables related to a DXF drawing.


Actual DXF version like 'AC1009', set by ezdxf.new() or ezdxf.readfile().

For supported DXF versions see Document Management


The AutoCAD release name like 'R12' or 'R2000' for actual dxfversion.


Text encoding of Drawing, the default encoding for new drawings is 'cp1252'. Starting with DXF R2007 (AC1021), DXF files are written as UTF-8 encoded text files, regardless of the attribute encoding. Text encoding can be changed to encodings listed below.

see also: DXF File Encoding

supported encodings
'cp874' Thai
'cp932' Japanese
'gbk' UnifiedChinese
'cp949' Korean
'cp950' TradChinese
'cp1250' CentralEurope
'cp1251' Cyrillic
'cp1252' WesternEurope
'cp1253' Greek
'cp1254' Turkish
'cp1255' Hebrew
'cp1256' Arabic
'cp1257' Baltic
'cp1258' Vietnam

Returns required output encoding for saving to filesystem or encoding to binary data.


Drawing filename, if loaded by ezdxf.readfile() else None.


Reference to the root dictionary of the OBJECTS section.


Reference to the HeaderSection, get/set drawing settings as header variables.


Reference to the EntitySection of the drawing, where all graphical entities are stored, but only from modelspace and the active paperspace layout. Just for your information: Entities of other paperspace layouts are stored as BlockLayout in the BlocksSection.


Reference to the objects section, see also ObjectsSection.


Reference to the blocks section, see also BlocksSection.


Reference to the tables section, see also TablesSection.


Reference to the classes section, see also ClassesSection.


Reference to the layout manager, see also Layouts.


Collection of all groups, see also GroupCollection.

requires DXF R13 or later


Shortcut for Drawing.tables.layers

Reference to the layers table, where you can create, get and remove layers, see also Table and Layer


Shortcut for Drawing.tables.styles

Reference to the styles table, see also Style.


Shortcut for Drawing.tables.dimstyles

Reference to the dimstyles table, see also DimStyle.


Shortcut for Drawing.tables.linetypes

Reference to the linetypes table, see also Linetype.


Shortcut for Drawing.tables.views

Reference to the views table, see also View.


Shortcut for Drawing.tables.viewports

Reference to the viewports table, see also Viewport.


Shortcut for Drawing.tables.ucs

Reference to the ucs table, see also UCS.


Shortcut for Drawing.tables.appids

Reference to the appids table, see also AppID.


MaterialCollection of all Material objects.


MLineStyleCollection of all MLineStyle objects.


MLeaderStyleCollection of all MLeaderStyle objects.


Get and set the document/modelspace base units as enum, for more information read this: DXF Units.

save(encoding: str = None, fmt: str = 'asc') → None

Write drawing to file-system by using the filename attribute as filename. Override file encoding by argument encoding, handle with care, but this option allows you to create DXF files for applications that handles file encoding different than AutoCAD.

  • encoding – override default encoding as Python encoding string like 'utf-8'
  • fmt'asc' for ASCII DXF (default) or 'bin' for Binary DXF
saveas(filename: Union[str, Path], encoding: str = None, fmt: str = 'asc') → None

Set Drawing attribute filename to filename and write drawing to the file system. Override file encoding by argument encoding, handle with care, but this option allows you to create DXF files for applications that handles file encoding different than AutoCAD.

  • filename – file name as string
  • encoding – override default encoding as Python encoding string like 'utf-8'
  • fmt'asc' for ASCII DXF (default) or 'bin' for Binary DXF
write(stream: Union[TextIO, BinaryIO], fmt: str = 'asc') → None

Write drawing as ASCII DXF to a text stream or as Binary DXF to a binary stream. For DXF R2004 (AC1018) and prior open stream with drawing encoding and mode='wt'. For DXF R2007 (AC1021) and later use encoding='utf-8', or better use the later added Drawing property output_encoding which returns the correct encoding automatically. The correct and required error handler is errors='dxfreplace'!

If writing to a StringIO stream, use Drawing.encode() to encode the result string from StringIO.get_value():

binary = doc.encode(stream.get_value())
  • stream – output text stream or binary stream
  • fmt'asc' for ASCII DXF (default) or 'bin' for binary DXF
encode_base64() → bytes

Returns DXF document as base64 encoded binary data.

encode(s: str) → bytes

Encode string s with correct encoding and error handler.

query(query: str = '*') → ezdxf.query.EntityQuery

Entity query over all layouts and blocks, excluding the OBJECTS section.

Parameters:query – query string
groupby(dxfattrib='', key=None) → dict

Groups DXF entities of all layouts and blocks (excluding the OBJECTS section) by a DXF attribute or a key function.

  • dxfattrib – grouping DXF attribute like 'layer'
  • key – key function, which accepts a DXFEntity as argument and returns a hashable grouping key or None to ignore this entity.

See also

groupby() documentation

modelspace() → ezdxf.layouts.layout.Modelspace

Returns the modelspace layout, displayed as 'Model' tab in CAD applications, defined by block record named '*Model_Space'.

layout(name: str = None) → Layout

Returns paperspace layout name or returns first layout in tab order if name is None.

active_layout() → Layout

Returns the active paperspace layout, defined by block record name '*Paper_Space'.

layout_names() → Iterable[str]

Returns all layout names (modelspace 'Model' included) in arbitrary order.

layout_names_in_taborder() → Iterable[str]

Returns all layout names (modelspace included, always first name) in tab order.

new_layout(name, dxfattribs=None) → Layout

Create a new paperspace layout name. Returns a Layout object. DXF R12 (AC1009) supports only one paperspace layout, only the active paperspace layout is saved, other layouts are dismissed.

  • name – unique layout name
  • dxfattribs – additional DXF attributes for the DXFLayout entity

DXFValueErrorLayout name already exist

delete_layout(name: str) → None

Delete paper space layout name and all entities owned by this layout. Available only for DXF R2000 or later, DXF R12 supports only one paperspace and it can’t be deleted.

add_image_def(filename: str, size_in_pixel: Tuple[int, int], name=None)

Add an image definition to the objects section.

Add an ImageDef entity to the drawing (objects section). filename is the image file name as relative or absolute path and size_in_pixel is the image size in pixel as (x, y) tuple. To avoid dependencies to external packages, ezdxf can not determine the image size by itself. Returns a ImageDef entity which is needed to create an image reference. name is the internal image name, if set to None, name is auto-generated.

Absolute image paths works best for AutoCAD but not really good, you have to update external references manually in AutoCAD, which is not possible in TrueView. If the drawing units differ from 1 meter, you also have to use: set_raster_variables().

  • filename – image file name (absolute path works best for AutoCAD)
  • size_in_pixel – image size in pixel as (x, y) tuple
  • name – image name for internal use, None for using filename as name (best for AutoCAD)
set_raster_variables(frame: int = 0, quality: int = 1, units: str = 'm')

Set raster variables.

  • frame0 = do not show image frame; 1 = show image frame
  • quality0 = draft; 1 = high
  • units

    units for inserting images. This defines the real world unit for one drawing unit for the purpose of inserting and scaling images with an associated resolution.

    mm Millimeter
    cm Centimeter
    m Meter (ezdxf default)
    km Kilometer
    in Inch
    ft Foot
    yd Yard
    mi Mile

Set wipeout variables.

Parameters:frame0 = do not show image frame; 1 = show image frame
add_underlay_def(filename: str, fmt: str = 'ext', name: str = None)

Add an UnderlayDef entity to the drawing (OBJECTS section). filename is the underlay file name as relative or absolute path and fmt as string (pdf, dwf, dgn). The underlay definition is required to create an underlay reference.

  • filename – underlay file name
  • fmt – file format as string 'pdf'|'dwf'|'dgn' or 'ext' for getting file format from filename extension
  • name – pdf format = page number to display; dgn format = 'default'; dwf: ????
add_xref_def(filename: str, name: str, flags: int = 20)

Add an external reference (xref) definition to the blocks section.

  • filename – external reference filename
  • name – name of the xref block
  • flags – block flags
layouts_and_blocks() → Iterator[GenericLayoutType]

Iterate over all layouts (modelspace and paperspace) and all block definitions.

chain_layouts_and_blocks() → Iterator[DXFEntity]

Chain entity spaces of all layouts and blocks. Yields an iterator for all entities in all layouts and blocks.


Reset fingerprint GUID.


Reset version GUID.

set_modelspace_vport(height, center=(0, 0)) → VPort

Set initial view/zoom location for the modelspace, this replaces the current “*Active” viewport configuration.

  • height – modelspace area to view
  • center – modelspace location to view in the center of the CAD application window.
audit() → ezdxf.audit.Auditor

Checks document integrity and fixes all fixable problems, not fixable problems are stored in Auditor.errors.

If you are messing around with internal structures, call this method before saving to be sure to export valid DXF documents, but be aware this is a long running task.

validate(print_report=True) → bool

Simple way to run an audit process. Fixes all fixable problems, return False if not fixable errors occurs, to get more information about not fixable errors use audit() method instead.

Parameters:print_report – print report to stdout

Returns: True if no errors occurred

ezdxf_metadata() → MetaData

Returns the ezdxf ezdxf.document.MetaData object, which manages ezdxf and custom metadata in DXF files. For more information see: Ezdxf Metadata.

New in version 0.17.