Skip to content

ezdxf readfile() overwrites $TDCREATE with the current time; saveas() stamps $TDUPDATE

Scrubbing a DXF with ezdxf 1.4.4: I set $TDCREATE/$TDUPDATE/$TDUCREATE/$TDUUPDATE to a fixed Julian date before doc.saveas(), then re-read the file with ezdxf.readfile() to verify. The check doc.header['$TDCREATE'] == 2459580.5 fails: the re-read document reports $TDCREATE as today's date and time (e.g. 2461329.58), and the saved file's $TDUPDATE is the wall-clock save time. $TDUCREATE/$TDUUPDATE keep my value. It looks like the header write is being ignored, but grepping the DXF shows $TDCREATE was written correctly and only $TDUPDATE changed.

doc.header['$TDCREATE'] = 2459580.5
doc.saveas('out.dxf')
print(ezdxf.readfile('out.dxf').header['$TDCREATE'])  # 2461329.58..., not 2459580.5
1 solution
ranked by outcome — not votes
Accepted

Two separate ezdxf behaviours (1.4.4, ezdxf/document.py):

  1. Loading stamps $TDCREATE. Drawing._finalize_setup() runs for new and loaded drawings and calls _setup_metadata(), which does self.header["$TDCREATE"] = juliandate(datetime.now()). Any document returned by ezdxf.readfile() / recover.readfile() therefore reports the load time, not the file's value.
  2. Saving stamps $TDUPDATE and the version GUID. _update_metadata() (called from write/saveas) sets $TDUPDATE = juliandate(now) and calls reset_version_guid(). It also writes an EZDXF_META marker that includes a timestamp.

Fix for deterministic output:

import ezdxf
ezdxf.options.write_fixed_meta_data_for_testing = True  # before saveas()
# all four TD* dates -> juliandate(2000-01-01) == 2451545.0, constant GUIDs and marker

Verify from the serialized header, not from a re-loaded Drawing:

head = open('out.dxf').read().split('ENDSEC', 1)[0].splitlines()
dates = {head[i].strip(): float(head[i + 2]) for i in range(len(head) - 2)
         if head[i].strip() in {'$TDCREATE', '$TDUPDATE', '$TDUCREATE', '$TDUUPDATE'}}
assert set(dates.values()) == {2451545.0}