-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.py
More file actions
333 lines (274 loc) · 9.9 KB
/
core.py
File metadata and controls
333 lines (274 loc) · 9.9 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
"""
Core Program
"""
import inventor
import autocad
import system
import pandas as pd
import zipfile
import os
def process_assembly(assembly, app):
"""Process Assembly
Assembly must be an Inventor file and have pick list on the first page.
1) Open assembly drawing (ipt)
2) Pull drawing info to dict
3) Save to spreadsheet - drawing_info.xlsx
4) Export print, pdf and dxf files
5) Save to spreadsheet - part_list.xlsx
6) Open assembly part (iam)
7) Save spreadsheet - bom.xlsx
Parameters
----------
assembly : str
AGR part number usually in 'AGR0000-000-00' format
app : obj
Inventor Application COM Object
"""
# 1) Open assembly drawing (ipt)
path = system.find_path(assembly, 'idw')
idw = inventor.Drawing(path, app)
# 2) Pull drawing info to dict
rs = pd.DataFrame(columns=['partcode', 'rev', 'desc', 'material', 'finish', 'size'])
drawing_info = idw.get_drawing_info()
rs = rs.append(drawing_info, ignore_index=True)
# 3) Save to spreadsheet - drawing_info.xlsx
path = system.EXPORT_DIR.joinpath(assembly).joinpath('drawing_info.xlsx')
rs.to_excel(str(path), index=False)
# 4) Export print, pdf and dxf file
_export_inventer_drawing(idw, assembly, drawing_info, is_assy=True)
# 5) Save to spreadsheet - part_list.xlsx
idw.export_part_list('xlsx')
idw.close()
# 6) Open assembly part (iam)
path = system.find_path(assembly, 'iam')
iam = inventor.Assembly(path, app)
# 7) Save spreadsheet - bom.xlsx
iam.export_bom()
iam.close()
def _export_inventer_drawing(idw, assembly, drawing_info, is_assy=False):
"""Export print, pdf and dxf files from one drawing
Used in 'process_assembly(assembly, app)' and
process_parts(assembly, app) functions.
Parameters
----------
idw: obj
Drawing Document class from inventor.py
assembly: str
AGR part number usually in 'AGR0000-000-00' format
drawing_info: dict
Drawing information
is_assy: bool
is the part an assenmbly?
"""
print_size = drawing_info['size']
if print_size == 'A2':
print_size = 'A3'
print_dir = assembly + '/print/' + print_size + '/'
idw.export_to(print_dir, 'pdf')
idw.export_to(assembly + '/pdf/', 'pdf')
if not is_assy:
# idw.export_to(assembly + '/dxf/', 'dxf')
# idw.export_to(assembly + '/dwg/', 'dwg')
pass
def _load_children(assembly):
"""Load children from parent
Used in 'create_format_matrix(assembly)' function.
From the assembly idw's part list and iam's bom, return a list
of all the drawings used under this section.
Parameters
----------
assembly: str
AGR part number usually in 'AGR0000-000-00' format
Returns
-------
partcodes: 'obj' of 'str'
list of drawings
"""
path1 = system.EXPORT_DIR.joinpath(assembly).joinpath('part_list.xlsx')
df1 = pd.read_excel(str(path1))
p1 = df1.loc[df1['Dwg_No'].notnull(), 'Dwg_No']
path2 = system.EXPORT_DIR.joinpath(assembly).joinpath('bom.xlsx')
df2 = pd.read_excel(str(path2))
p2 = df2.loc[df2['Part Number'].notnull(), 'Part Number']
partcodes = [*p1, *p2]
partcodes = list(dict.fromkeys(partcodes))
return partcodes
def create_format_matrix(assembly):
"""File format type spreadsheeet
Find all ipt, iam, idw and dwg files under the assembly,
create a file format report.
Parameters
----------
assembly: str
AGR part number usually in 'AGR0000-000-00' format
"""
ipt_paths = []
iam_paths = []
idw_paths = []
dwg_paths = []
partcodes = _load_children(assembly)
for partcode in partcodes:
ipt_paths.append(system.find_path(partcode, 'ipt') is not None)
iam_paths.append(system.find_path(partcode, 'iam') is not None)
idw_paths.append(system.find_path(partcode, 'idw') is not None)
dwg_paths.append(system.find_path(partcode, 'dwg') is not None)
df = pd.DataFrame()
df['partcode'] = partcodes
df['ipt'] = ipt_paths
df['iam'] = iam_paths
df['idw'] = idw_paths
df['dwg'] = dwg_paths
path = system.EXPORT_DIR.joinpath(assembly).joinpath('format_type.xlsx')
df.to_excel(str(path), index=False)
def process_parts(assembly, app):
"""Process Parts
1) Load spreadsheet - drawing_info.xlsx
2) Load spreadsheet - format_type.xlsx
3) Create a list of idw paths
4) Open each drawings (idw)
5) Pull drawing info to dict
6) Export print, pdf and dxf files
7) Save spreadsheet - drawing_info.xlsx
8) Create a list of dwg paths
9) export pdf files (AutoCAD)
10) Unzip files
Parameters
----------
assembly : str
AGR part number usually in 'AGR0000-000-00' format
app : obj
Inventor Application COM Object
"""
# 1) Load spreadsheet - drawing_info.xlsx
info_path = system.EXPORT_DIR.joinpath(assembly).joinpath('drawing_info.xlsx')
if os.path.exists(str(info_path)):
rs = pd.read_excel(str(info_path))
else:
rs = pd.DataFrame(columns=['partcode', 'rev', 'desc', 'material', 'finish', 'size'])
# 2) Load spreadsheet - format_type.xlsx, create a list of idw paths
path = system.EXPORT_DIR.joinpath(assembly).joinpath('format_type.xlsx')
df = pd.read_excel(str(path))
inv_df = df.loc[df['idw']==True, ['partcode', 'iam']]
atc_df = df.loc[df['dwg']==True, ['partcode']]
# 3) Create a list of idw paths
paths = []
for partcode in inv_df['partcode']:
path = system.find_path(partcode, 'idw')
paths.append(path)
# 4) Open each drawings
# 5) Pull drawing info to dict
# 6) export print, pdf and dxf files
# 7) Save spreadsheet - drawing_info.xlsx
if len(paths) > 0:
for path, is_assy in zip(paths, df['iam']):
idw = inventor.Drawing(path, app)
drawing_info = idw.get_drawing_info()
rs = rs.append(drawing_info, ignore_index=True)
_export_inventer_drawing(idw, assembly, drawing_info, is_assy)
idw.close()
rs.to_excel(str(info_path), index=False)
# 8) Create a list of dwg paths
paths = []
for partcode in atc_df['partcode']:
path = system.find_path(partcode, 'dwg')
paths.append(path)
# 9) export pdf files (AutoCAD)
autocad_app = autocad.application()
if len(paths) > 0:
for path in paths:
dwg = autocad.Drawing(path, autocad_app)
dwg.export_to(assembly + r'\from_autocad', 'pdf')
dwg.close()
# 10) Unzip files
# for partcode in inv_df['partcode']:
# file = partcode + '.zip'
# export_path = system.EXPORT_DIR.joinpath(assembly).joinpath('dxf')
# with zipfile.ZipFile(str(export_path.joinpath(file)), 'r') as zip_ref:
# zip_ref.extractall(str(export_path))
# os.remove(str(export_path.joinpath(file)))
def batch_export(assembly):
"""Main - Batch Export Drawing
1) Create project folder
2) Connect to Inventor COM API
3) Process assembly
4) Find all idw part files in the vault
5) Process parts
"""
system.create_project(assembly)
app = inventor.application()
process_assembly(assembly, app)
create_format_matrix(assembly)
process_parts(assembly, app)
def batch_export_from(filename, filetype):
app = inventor.application()
with open(str(system.EXPORT_DIR.joinpath(filename))) as file:
partcodes = [line.strip() for line in file]
paths = []
ipt_convert = [
'CATPart', 'jt', 'ipt', 'igs', 'iges', 'sat',
'smt', 'stl', 'step', 'stp', 'xgl', 'zgl'
]
for partcode in partcodes:
if filetype in ipt_convert:
path = system.find_path(partcode, 'ipt')
else:
path = system.find_path(partcode, 'idw')
paths.append(path)
for path in paths:
if filetype in ipt_convert:
inv = inventor.Part(path, app)
else:
inv = inventor.Drawing(path, app)
inv.export_to(system.EXPORT_DIR, filetype)
inv.close()
for partcode in partcodes:
try:
file = partcode + '.zip'
export_path = system.EXPORT_DIR
with zipfile.ZipFile(str(export_path.joinpath(file)), 'r') as zip_ref:
zip_ref.extractall(str(export_path))
os.remove(str(export_path.joinpath(file)))
except:
pass
def export_to(partcode, filetype):
"""Main - Export to ...
Export one drawing to the specified file format
"""
app = inventor.application()
ipt_convert = [
'CATPart', 'jt', 'ipt', 'igs', 'iges', 'sat',
'smt', 'stl', 'step', 'stp', 'xgl', 'zgl'
]
if filetype in ipt_convert:
path = system.find_path(partcode, 'ipt')
inv = inventor.Part(path, app)
else:
path = system.find_path(partcode, 'idw')
inv = inventor.Drawing(path, app)
inv.export_to(system.EXPORT_DIR, filetype)
inv.close()
try:
file = partcode + '.zip'
export_path = system.EXPORT_DIR
with zipfile.ZipFile(str(export_path.joinpath(file)), 'r') as zip_ref:
zip_ref.extractall(str(export_path))
os.remove(str(export_path.joinpath(file)))
except:
pass
def cnc_batch_export_from(filename):
"""export dxf files and save them on a memory stick for cnc"""
batch_export_from(filename, 'dxf')
with open(str(system.EXPORT_DIR.joinpath(filename))) as file:
partcodes = [line.strip() for line in file]
for partcode in partcodes:
file = partcode + '.dxf'
path = system.EXPORT_DIR.joinpath(file)
os.remove("G:/" + file)
os.rename(str(path), "G:/" + file)
def cnc_export_to(partcode):
"""export dxf file and save them on a memory stick for cnc"""
export_to(partcode, 'dxf')
file = partcode + '.dxf'
path = system.EXPORT_DIR.joinpath(file)
os.remove("G:/" + file)
os.rename(str(path), "G:/" + file)