-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcad_find.py
202 lines (174 loc) · 6.32 KB
/
cad_find.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# -*- coding: utf-8 -*-
import datetime
import os
import sys
from multiprocessing import Pool
import ezdxf
from openpyxl import Workbook
WORK_DIR = 'C:\\test'
FILES_DWG = ['BSH-LAB_QMT-PW-A.dwg']
FILES_DWG = sys.argv[1:] if len(sys.argv) > 1 else FILES_DWG
ACAD_PATH = None
def find_acad():
global ACAD_PATH
if ACAD_PATH:
return ACAD_PATH
for p, d, f in os.walk('c:\\'):
if 'acad.exe' in f:
ACAD_PATH = p + '\\acad.exe'
return (p + '\\acad.exe')
if 'acadlt.exe' in f:
ACAD_PATH = p + '\\acadlt.exe'
return (p + '\\acadlt.exe')
def openDrawning(FILE):
if FILE.endswith('dwg'):
if FILE.rstrip('dwg') + 'dxf' not in os.listdir():
createDXF(FILE)
return ezdxf.readfile(FILE.rstrip('dwg') + 'dxf')
elif FILE.endswith('dxf'):
return ezdxf.readfile(FILE)
def createScript(FILE):
# SCRIPT = 'FILEDIA\n0\nPURGE\nA\n*\nN\nAUDIT\nY\nSAVEAS\nDXF\n16\n' + \
# os.path.join(os.getcwd(), FILE.rstrip('dwg') + 'dxf') + "\nQUIT"
SCRIPT = 'FILEDIA\n0\nSAVEAS\nDXF\n16\n' + \
os.path.join(os.getcwd(), FILE.rstrip('dwg') + 'dxf') + "\nQUIT\n\n"
tmpDWG = open('SCRIPT.scr', 'w')
tmpDWG.write(SCRIPT)
tmpDWG.close()
def runDWG(FILE, AcadExePath):
tmpDir = os.getcwd()
os.chdir(os.path.split(AcadExePath)[0])
os.system(os.path.split(AcadExePath)[1] + ' "' + os.path.join(
tmpDir, FILE) + '" /nologo /nossm /b "' + os.path.join(
tmpDir, 'SCRIPT.scr') + '"')
os.chdir(tmpDir)
os.remove('SCRIPT.scr')
def createDXF(FILE):
print('creating dxf for ', FILE)
AcadExePath = find_acad()
createScript(FILE)
if FILE.rstrip('dwg') + 'dxf' in os.listdir():
os.remove(FILE.rstrip('dwg') + 'dxf')
runDWG(FILE, AcadExePath)
os.remove(FILE.rstrip('dwg') + 'bak')
def job(z):
nr, FILE, RYSUNKI = z
createDXF(FILE)
print('opening ', FILE.rstrip('dwg') + 'dxf')
dxf = ezdxf.readfile(FILE.rstrip('dwg') + 'dxf')
tabelki = dxf.entities.query('INSERT[name=="TABELKA"]')
for tab in tabelki:
NR = tab.get_attrib_text('NR_RYSUNKI')
SKALA = tab.get_attrib_text('SKALA')
NAZWA = ''
for attr in tab.attribs():
if attr.dxf.tag.startswith('NAZWA_RYSUNKU'):
NAZWA += attr.dxf.text + ' '
RYSUNKI.append((NR, SKALA, NAZWA.rstrip(' ')))
print("'{}','{}','{}'".format((NR, SKALA, NAZWA.rstrip(' '))))
os.remove(FILE.rstrip('dwg') + 'dxf')
def main():
RYSUNKI = []
pool = Pool()
z = zip(range(len(FILES_DWG)), FILES_DWG, [
RYSUNKI for x in range(len(FILES_DWG))])
for t in pool.imap(job, z):
pass
for rys in RYSUNKI:
print("'{}','{}','{}'".format(*rys))
return RYSUNKI
def find_in_files():
for dwgfile in os.listdir(WORK_DIR):
dxf = openDrawning(dwgfile)
for e in dxf.entities:
try:
NR = e.get_attrib_text('NR')
if NR == 'SZ5':
print(dwgfile)
except:
pass
def count_area():
tables = []
for dwgfile in os.listdir(WORK_DIR):
if dwgfile.endswith('.dwg'):
tmp_tables = []
dxf = openDrawning(dwgfile)
metki = dxf.entities.query('INSERT[name=="METKA2"]')
for met in metki:
NR = met.get_attrib_text('NR')
NAZWA_POM = met.get_attrib_text('NAZWA_POM')
POW = float(met.get_attrib_text('POW').replace(',', '.'))
tmp_tables.append((NR, NAZWA_POM, POW))
print("{},{},{}".format(NR, NAZWA_POM, POW))
# f = open(dwgfile+'.csv', 'w')
# f.writelines([','.join(t)+'\n' for t in tmp_tables])
tables += tmp_tables
# f = open('all.csv', 'w')
# f.writelines([','.join(t)+'\n' for t in tables])
write_2_xls(tables, 'all')
def areas2xls4dabki():
files = []
l = len(os.listdir())
for nr, f in enumerate(os.listdir()):
if f.endswith('.dwg'):
files.append(openDrawning(f))
print('\rOpening files... remains {:>2}'.format(l - nr), end='')
print('\r')
metki = []
for dxf in files:
tmp_metki = dxf.entities.query('INSERT[name=="METKA2"]')
metki += list(tmp_metki)
metki = sorted(metki, key=lambda x: x.get_attrib_text('NR'))
today = datetime.date.today().isoformat()
f = open('DĄBKI ZESTAWIENIE POWIERZCHNI {}.csv'.format(today),
'w', encoding='utf-8')
for nr, m in enumerate(metki):
float_error = False
NR = m.get_attrib_text('NR')
try:
POW = float(m.get_attrib_text('POW').replace(',', '.'))
except(ValueError):
POW = m.get_attrib_text('POW').replace(',', '.')
float_error = True
NAZWA = m.get_attrib_text('NAZWA_POM')
if not float_error:
print('{},\t{:>6.2f}m2,\t{}'.format(NR, POW, NAZWA))
f.write("{},{:.2f},{}\n".format(NR, POW, NAZWA))
else:
print('{},\t{}m2,\t{}'.format(NR, POW, NAZWA))
f.write("{},{},{}\n".format(NR, POW, NAZWA))
f.close()
def write_2_xls(data, name):
wb = Workbook()
# grab the active worksheet
ws = wb.active
# Data can be assigned directly to cells
ws['A1'] = 42
# Rows can also be appended
for d in data:
ws.append(d)
wb.save(name + ".xlsx")
def searchFiles(path):
pack = []
for p, d, files in os.walk(path):
for f in files:
if f.endswith('.dwg'):
# dxf = openDrawning(os.path.join(p,f))
size = os.stat(os.path.join(p, f)).st_size
mb = size / 1024 / 1024
if mb > 5:
pack.append(os.path.join(p, f))
try:
print('{:6.2f}, {}'.format(mb, f))
except UnicodeEncodeError:
pass
f = open('lista.txt', 'w')
f.write(pack)
f.close()
if __name__ == '__main__':
os.chdir(WORK_DIR)
# areas2xls4dabki()
print('test')
# searchFiles('\\\\R2srv\\projekty')
# find_in_files()
count_area()