Document Management¶
Create New Drawings¶
- ezdxf.new(dxfversion='AC1027', setup=False, units=6) 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
The units argument defines th document and modelspace units. The header variable $MEASUREMENT will be set according to the given units, 0 for inch, feet, miles, … and 1 for metric units. For more information go to module
ezdxf.units
- 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
units – document and modelspace units, default is 6 for meters
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 | PathLike, encoding: str | None = 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 theezdxf.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 fileserrors –
specify decoding error handler
”surrogateescape” to preserve possible binary data (default)
”ignore” to use the replacement char U+FFFD “�” for invalid data
”strict” to raise an
UnicodeDecodeError
exception for invalid data
- Raises:
IOError – not a DXF file or file does not exist
DXFStructureError – for invalid or corrupted DXF structures
UnicodeDecodeError – if errors is “strict” and a decoding error occurs
- 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 areadline()
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 useerrors='surrogateescape'
as error handler for the import stream.If this function struggles to load the DXF document and raises a
DXFStructureError
exception, try theezdxf.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
- ezdxf.readzip(zipfile: str | PathLike, filename: str | None = 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 “�” for invalid data
”strict” to raise an
UnicodeDecodeError
exception for invalid data
- Raises:
IOError – not a DXF file or file does not exist or if filename is
None
- no DXF file foundDXFStructureError – 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 “�” for invalid data
”strict” to raise an
UnicodeDecodeError
exception for invalid data
- 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 |
See also
Howto: Set/Get Header Variables
Howto: Set DXF Drawing Units
Ezdxf Metadata¶
Store internal metadata like ezdxf version and creation time for a new created document as metadata in the DXF file. Only standard DXF features are used to store meta data and this meta data is preserved by Autodesk products, BricsCAD and of course ezdxf. Other 3rd party DXF libraries may remove this meta data.
For DXF R12 the meta data is stored as XDATA by AppID EZDXF
in the model
space BLOCK entity in the BLOCKS section.
For DXF R2000+ the meta data is stored in the “root” DICTIONARY in the
OBJECTS section as a DICTIONARY object by the key EZDXF_META
.
The MetaData
object has a dict-like interface and can also store
custom metadata:
metadata = doc.ezdxf_metadata()
# set data
metadata["MY_CUSTOM_META_DATA"] = "a string with max. length of 254"
# get data, raises a KeyError() if key not exist
value = metadata["MY_CUSTOM_META_DATA"]
# get data, returns an empty string if key not exist
value = metadata.get("MY_CUSTOM_META_DATA")
# delete entry, raises a KeyError() if key not exist
del metadata["MY_CUSTOM_META_DATA"]
# discard entry, does not raise a KeyError() if key not exist
metadata.discard("MY_CUSTOM_META_DATA")
Keys and values are limited to strings with a max. length of 254 characters
and line ending \n
will be replaced by \P
.
Keys used by ezdxf:
WRITTEN_BY_EZDXF
: ezdxf version and UTC time in ISO format
CREATED_BY_EZDXF
: ezdxf version and UTC time in ISO format
Example of the ezdxf marker string: 0.16.4b1 @ 2021-06-12T07:35:34.898808+00:00
- class ezdxf.document.MetaData¶
- abstract MetaData.__contains__(key: str) bool ¶
Returns key
in
self.
- abstract MetaData.__getitem__(key: str) str ¶
Returns the value for self[key].
- Raises:
KeyError – key does not exist
- MetaData.get(key: str, default: str = '') str ¶
Returns the value for key. Returns default if key not exist.
- abstract MetaData.__setitem__(key: str, value: str) None ¶
Set self[key] to value.
- abstract MetaData.__delitem__(key: str) None ¶
Delete self[key].
- Raises:
KeyError – key does not exist
- MetaData.discard(key: str) None ¶
Remove key, does not raise an exception if key not exist.