File size: 1,044 Bytes
44cdbab |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
"""Compare file read vs stream read"""
import ezdxf
dxf_path = "/Volumes/WorkSpace/Project/REMB/examples/663409.dxf"
# Method 1: Direct readfile
print("Method 1: Direct readfile()")
doc1 = ezdxf.readfile(dxf_path)
msp1 = doc1.modelspace()
lines1 = len(list(msp1.query('LINE')))
print(f" LINE entities: {lines1}")
# Method 2: Read bytes, decode, StringIO
import io
with open(dxf_path, 'rb') as f:
content = f.read()
print("\nMethod 2: bytes -> latin-1 -> StringIO")
text = content.decode('latin-1')
stream = io.StringIO(text)
doc2 = ezdxf.read(stream)
msp2 = doc2.modelspace()
lines2 = len(list(msp2.query('LINE')))
print(f" LINE entities: {lines2}")
# Method 3: tempfile
import tempfile
import os
print("\nMethod 3: bytes -> tempfile -> readfile()")
with tempfile.NamedTemporaryFile(mode='wb', suffix='.dxf', delete=False) as tmp:
tmp.write(content)
tmp_path = tmp.name
doc3 = ezdxf.readfile(tmp_path)
msp3 = doc3.modelspace()
lines3 = len(list(msp3.query('LINE')))
print(f" LINE entities: {lines3}")
os.unlink(tmp_path)
|