/
optimize_polyfaces.py
100 lines (85 loc) · 3.16 KB
/
optimize_polyfaces.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Purpose: open example files with big polyface models
# Created: 23.04.2014
# Copyright (c) 2014-2020, Manfred Moitzi
# License: MIT License
import time
from pathlib import Path
import ezdxf
from ezdxf.render import MeshVertexMerger
SRCDIR = Path(r'D:\Source\dxftest\CADKitSamples')
OUTDIR = Path('~/Desktop/Outbox').expanduser()
def optimize_polyfaces(polyfaces):
count = 0
runtime = 0
vertex_diff = 0
print("start optimizing...")
for polyface in polyfaces:
count += 1
start_vertex_count = len(polyface)
start_time = time.time()
polyface.optimize()
end_time = time.time()
end_vertex_count = len(polyface)
runtime += end_time - start_time
vertex_diff += start_vertex_count - end_vertex_count
print(f"removed {vertex_diff} vertices in {runtime:.2f} seconds.")
def optimize(name: str):
filename = SRCDIR / name
new_filename = OUTDIR / ('optimized_' + name)
print(f'opening DXF file: {filename}')
start_time = time.time()
doc = ezdxf.readfile(filename)
msp = doc.modelspace()
end_time = time.time()
print(f'time for reading: {end_time - start_time:.1f} seconds')
print(f"DXF version: {doc.dxfversion}")
print(f"Database contains {len(doc.entitydb)} entities.")
polyfaces = (polyline for polyline in msp.query('POLYLINE') if polyline.is_poly_face_mesh)
optimize_polyfaces(polyfaces)
print(f'saving DXF file: {new_filename}')
start_time = time.time()
doc.saveas(new_filename)
end_time = time.time()
print(f'time for saving: {end_time - start_time:.1f} seconds')
def save_as(name):
filename = SRCDIR / name
print(f'opening DXF file: {filename}')
start_time = time.time()
doc = ezdxf.readfile(filename)
msp = doc.modelspace()
end_time = time.time()
print(f'time for reading: {end_time - start_time:.1f} seconds')
print(f"DXF version: {doc.dxfversion}")
print(f"Database contains {len(doc.entitydb)} entities.")
polyfaces = (polyline for polyline in msp.query('POLYLINE') if polyline.is_poly_face_mesh)
# create a new documents
doc1 = ezdxf.new()
msp1 = doc1.modelspace()
doc2 = ezdxf.new()
msp2 = doc2.modelspace()
for polyface in polyfaces:
b = MeshVertexMerger.from_polyface(polyface)
b.render(msp1, dxfattribs={
'layer': polyface.dxf.layer,
'color': polyface.dxf.color,
})
b.render_polyface(msp2, dxfattribs={
'layer': polyface.dxf.layer,
'color': polyface.dxf.color,
})
new_filename = OUTDIR / ('mesh_' + name)
print(f'saving as mesh DXF file: {new_filename}')
start_time = time.time()
doc1.saveas(new_filename)
end_time = time.time()
print(f'time for saving: {end_time - start_time:.1f} seconds')
new_filename = OUTDIR / ('recreated_polyface_' + name)
print(f'saving as polyface DXF file: {new_filename}')
start_time = time.time()
doc2.saveas(new_filename)
end_time = time.time()
print(f'time for saving: {end_time - start_time:.1f} seconds')
if __name__ == '__main__':
optimize('fanuc-430-arm.dxf')
optimize('cnc machine.dxf')
save_as('fanuc-430-arm.dxf')