|
|
"""Compare file read vs stream read""" |
|
|
|
|
|
import ezdxf |
|
|
|
|
|
dxf_path = "/Volumes/WorkSpace/Project/REMB/examples/663409.dxf" |
|
|
|
|
|
|
|
|
print("Method 1: Direct readfile()") |
|
|
doc1 = ezdxf.readfile(dxf_path) |
|
|
msp1 = doc1.modelspace() |
|
|
lines1 = len(list(msp1.query('LINE'))) |
|
|
print(f" LINE entities: {lines1}") |
|
|
|
|
|
|
|
|
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}") |
|
|
|
|
|
|
|
|
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) |
|
|
|