-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkill_parts.py
220 lines (168 loc) · 5.45 KB
/
kill_parts.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# Saves root CATProduct and close it. Opens CATPart(s) from source. Copies visible bodies. Pastes as result (while preserving color). Removes everthing else. Saves (keeps UUID intact).
import os
import sys
import zipfile
from datetime import datetime
from pycatia import catia
def zipdir(path, ziph):
# ziph is zipfile handle
for root, dirs, files in os.walk(path):
for file in files:
ziph.write(os.path.join(root, file),
os.path.relpath(os.path.join(root, file),
os.path.join(path, '..')))
caa = catia()
documents = caa.documents
document = caa.active_document
root_doc = document.name
buttons = 4
result = caa.message_box("This will isolate all parts in the product. Are you sure?", buttons=buttons, title="Isolate product")
if result == 6:
document.save()
document.close()
source_directory = "C:/Temp/TCIC_V5_tmp/catuii/"
for root, dirs, files in os.walk(source_directory):
for file in files:
# only get CATParts.
if os.path.splitext(file)[1] == ".CATPart":
# create filename with path.
file_name = os.path.join(source_directory, file)
ref_doc = documents.open(file_name)
document = caa.active_document
part = document.part
bodies = part.bodies
hybrid_bodies = part.hybrid_bodies
parameters = part.parameters
relations = part.relations
axis_systems = part.axis_systems
annotations = part.annotation_sets
product = document.product
publications = product.publications
selection = document.selection
vis_properties = selection.vis_properties
color_list = []
body_list = []
body_count = 0
bodies_copy = bodies
# filter out empty or hidden bodies. if not get color and append to list.
for body in bodies:
if body.shapes.count<1:
body.name = "Skip"
else:
selection.clear()
selection.add(body)
vis_value = vis_properties.get_show()
real_color = vis_properties.get_real_color()
selection.clear()
if vis_value[1] ==1:
body.name = "Skip"
else:
color_list.append(real_color)
body_count+=1
# if there is something to copy, else go ahead with next file.
if body_count>0:
selection.clear()
for body in bodies:
if body.name == "Skip":
continue
else:
selection.add(body)
selection.copy()
selection.paste_special("CATPrtResultWithOutLink")
vis_properties.set_real_color(120, 205, 220, 0)
selection.clear()
part.update()
body_main = bodies.add()
body_main.name = "PartBody_Isolated"
part.main_body = body_main
for body in bodies:
selection.clear()
selection.add(body)
real_color = vis_properties.get_real_color()
if (real_color[1]!=120) and (real_color[2]!=205) and (real_color[3]!=220):
if body.name=="PartBody_Isolated":
continue
else:
body.name = "Delete"
else:
continue
selection.clear()
for body in bodies:
if body.name == "Delete":
selection.add(body)
else:
continue
for axis_system in axis_systems:
selection.add(axis_system)
for annotation in annotations:
selection.add(annotation)
for hybrid_body in hybrid_bodies:
selection.add(hybrid_body)
if selection.count > 0:
selection.delete()
pub_list = []
for publication in publications:
pub_list.append(publication.name)
for x in pub_list:
publications.remove(x)
selection.clear()
for relation in relations:
selection.add(relation)
for parameter in parameters:
if parameter.user_access_mode == 2:
selection.add(parameter)
else:
continue
try:
selection.delete()
body_main.name = "PartBody"
i=0
# loop trough bodies and set color.
for body in bodies:
if body.name=="PartBody":
continue
else:
selection.add(body)
vis_properties.set_real_color(color_list[i][1], color_list[i][2], color_list[i][3], 0)
selection.clear()
i+=1
part.update()
document.save()
document.close()
except:
body_main.name = "PartBody"
i=0
# loop trough bodies and set color.
for body in bodies:
if body.name=="PartBody":
continue
else:
selection.add(body)
vis_properties.set_real_color(color_list[i][1], color_list[i][2], color_list[i][3], 0)
selection.clear()
i+=1
part.update()
document.save()
document.close()
else:
document.close()
for root, dirs, files in os.walk(source_directory):
for file in files:
# only get dat files.
if os.path.splitext(file)[1] == ".dat":
# create filename with path.
file_name = os.path.join(source_directory, file)
os.remove(file_name)
now = datetime.now()
date_stamp = now.strftime("%Y_%m_%d")
with zipfile.ZipFile(f"Status_{date_stamp}.zip", 'w', zipfile.ZIP_DEFLATED) as zipf:
zipdir(source_directory, zipf)
buttons = 4
result = caa.message_box("Operation finished. Do you want to open root product?", buttons=buttons, title="Isolate product")
if result == 6:
file_name = os.path.join(source_directory, root_doc)
ref_doc = documents.open(file_name)
else:
sys.exit()
else:
sys.exit()