-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Import_Yml.py
348 lines (315 loc) · 13.6 KB
/
Import_Yml.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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import os
import sys
import FreeCAD as App, Mesh, Part
from yaml import safe_load
if App.GuiUp:
import FreeCADGui as Gui
if not sys.version_info.major == 3:
print("This script requires Python 3.x")
print("You are using Python {}.{}.".format(sys.version_info.major, sys.version_info.minor))
sys.exit(1)
pythonopen = open
predefined_colors = {
'red': (1.0, 0.0, 0.0),
'darkRed': (0.67, 0.0, 0.0),
'green': (0.0, 1.0, 0.0),
'darkGreen': (0.0, 0.67, 0.0),
'blue': (0.0, 0.0, 1.0),
'darkBlue': (0.0, 0.0, 0.67),
'yellow': (1.0, 1.0, 0.0),
'cyan': (0.0, 1.0, 1.0),
'purple': (1.0, 0.0, 1.0),
'white': (1.0, 1.0, 1.0),
'lightGray': (0.75, 0.75, 0.75),
'gray': (0.5, 0.5, 0.5),
'darkGray': (0.25, 0.25, 0.25),
'black': (0.0, 0.0, 0.0),
}
def insertObject(directory, filename, document, group, attributes = None):
if not os.path.isfile(os.path.join(directory, filename)):
directory = os.path.expanduser('~/.FreeCAD/Mod/yaml-workspace')
if not os.path.isfile(os.path.join(directory, filename)):
print('ERROR: `{}` not found!'.format(filename))
return
if filename[-4:] in ['.stp', '.igs', 'iges', 'step']:
return insertPart(directory, filename, document, group, attributes)
insertMesh(directory, filename, document, group, attributes)
def insertMesh(directory, filename, document, group, attributes = None):
mesh = Mesh.Mesh(u'{}/{}'.format(directory, filename))
object_name = filename[:-4]
if 'objectName' in attributes:
object_name = attributes['objectName']
new_mesh = document.addObject("Mesh::Feature", object_name)
new_mesh.Mesh = mesh
if attributes:
color = getColor(attributes)
if color:
new_mesh.ViewObject.ShapeColor = color
transparency = getTransparency(attributes)
if transparency:
new_mesh.ViewObject.Transparency = transparency
placement = getPlacement(attributes)
rotation = getRotation(attributes)
new_mesh.Placement = App.Placement(placement, rotation)
group.addObject(new_mesh)
def insertPart(directory, filename, document, group, attributes = None):
if not os.path.isfile(os.path.join(directory, filename)):
directory = os.path.expanduser('~/.FreeCAD/Mod/yaml-workspace')
if not os.path.isfile(os.path.join(directory, filename)):
print('ERROR: `{}` not found!'.format(filename))
return
part = Part.Shape()
part = Part.read(u'{}/{}'.format(directory, filename))
object_name = filename[:-4]
if 'objectName' in attributes:
object_name = attributes['objectName']
new_part = document.addObject("Part::Feature", object_name)
new_part.Shape = part
if attributes:
color = getColor(attributes)
if color:
new_part.ViewObject.ShapeColor = color
transparency = getTransparency(attributes)
if transparency:
new_part.ViewObject.Transparency = transparency
placement = getPlacement(attributes)
rotation = getRotation(attributes)
new_part.Placement = App.Placement(placement, rotation)
group.addObject(new_part)
def insertSolid(name, document, group, attributes):
if attributes['solid'] == 'cylinder':
return insertCylinder(name, document, group, attributes)
if attributes['solid'] == 'sphere':
return insertSphere(name, document, group, attributes)
if attributes['solid'] == 'ellipsoid':
return insertEllipsoid(name, document, group, attributes)
if attributes['solid'] == 'box':
return insertBox(name, document, group, attributes)
if attributes['solid'] == 'cone':
return insertCone(name, document, group, attributes)
if attributes['solid'] == 'torus':
return insertTorus(name, document, group, attributes)
if attributes['solid'] == 'prism':
return insertPrism(name, document, group, attributes)
if attributes['solid'] == 'wedge':
return insertWedge(name, document, group, attributes)
print('ERROR: Unsupported solid tyle {}'.format(attributes['solid']))
def insertCylinder(name, document, group, attributes):
solid = document.addObject("Part::Cylinder","Cylinder")
solid.Label = name
solid.Radius = '{} mm'.format(attributes['radius'])
solid.Height = '{} mm'.format(attributes['height'])
if 'angle' in attributes:
solid.Angle = '{} deg'.format(attributes['angle'])
color = getColor(attributes)
if color:
solid.ViewObject.ShapeColor = color
transparency = getTransparency(attributes)
if transparency:
solid.ViewObject.Transparency = transparency
placement = getPlacement(attributes)
rotation = getRotation(attributes)
solid.Placement = App.Placement(placement, rotation)
group.addObject(solid)
def insertSphere(name, document, group, attributes):
solid = document.addObject("Part::Sphere","Sphere")
solid.Label = name
solid.Radius = '{} mm'.format(attributes['radius'])
if 'angle1' in attributes:
solid.Angle1 = '{} deg'.format(attributes['angle1'])
if 'angle2' in attributes:
solid.Angle2 = '{} deg'.format(attributes['angle2'])
if 'angle3' in attributes:
solid.Angle3 = '{} deg'.format(attributes['angle3'])
color = getColor(attributes)
if color:
solid.ViewObject.ShapeColor = color
transparency = getTransparency(attributes)
if transparency:
solid.ViewObject.Transparency = transparency
placement = getPlacement(attributes)
rotation = getRotation(attributes)
solid.Placement = App.Placement(placement, rotation)
group.addObject(solid)
def insertEllipsoid(name, document, group, attributes):
solid = document.addObject("Part::Ellipsoid","Ellipsoid")
solid.Label = name
solid.Radius1 = '{} mm'.format(attributes['radius1'])
solid.Radius2 = '{} mm'.format(attributes['radius2'])
solid.Radius3 = '{} mm'.format(attributes['radius3'])
if 'angle1' in attributes:
solid.Angle1 = '{} deg'.format(attributes['angle1'])
if 'angle2' in attributes:
solid.Angle2 = '{} deg'.format(attributes['angle2'])
if 'angle3' in attributes:
solid.Angle3 = '{} deg'.format(attributes['angle3'])
color = getColor(attributes)
if color:
solid.ViewObject.ShapeColor = color
transparency = getTransparency(attributes)
if transparency:
solid.ViewObject.Transparency = transparency
placement = getPlacement(attributes)
rotation = getRotation(attributes)
solid.Placement = App.Placement(placement, rotation)
group.addObject(solid)
def insertBox(name, document, group, attributes):
solid = document.addObject("Part::Box","Box")
solid.Label = name
solid.Length = '{} mm'.format(attributes['length'])
solid.Width = '{} mm'.format(attributes['width'])
solid.Height = '{} mm'.format(attributes['height'])
color = getColor(attributes)
if color:
solid.ViewObject.ShapeColor = color
transparency = getTransparency(attributes)
if transparency:
solid.ViewObject.Transparency = transparency
placement = getPlacement(attributes)
rotation = getRotation(attributes)
solid.Placement = App.Placement(placement, rotation)
group.addObject(solid)
def insertCone(name, document, group, attributes):
solid = document.addObject("Part::Cone","Cone")
solid.Label = name
solid.Radius1 = '{} mm'.format(attributes['radius1'])
solid.Radius2 = '{} mm'.format(attributes['radius2'])
solid.Height = '{} mm'.format(attributes['height'])
if 'angle' in attributes:
solid.Angle = '{} deg'.format(attributes['angle'])
color = getColor(attributes)
if color:
solid.ViewObject.ShapeColor = color
transparency = getTransparency(attributes)
if transparency:
solid.ViewObject.Transparency = transparency
placement = getPlacement(attributes)
rotation = getRotation(attributes)
solid.Placement = App.Placement(placement, rotation)
group.addObject(solid)
def insertTorus(name, document, group, attributes):
solid = document.addObject("Part::Torus","Torus")
solid.Label = name
solid.Radius1 = '{} mm'.format(attributes['radius1'])
solid.Radius2 = '{} mm'.format(attributes['radius2'])
if 'angle1' in attributes:
solid.Angle1 = '{} deg'.format(attributes['angle1'])
if 'angle2' in attributes:
solid.Angle2 = '{} deg'.format(attributes['angle2'])
if 'angle3' in attributes:
solid.Angle3 = '{} deg'.format(attributes['angle3'])
color = getColor(attributes)
if color:
solid.ViewObject.ShapeColor = color
transparency = getTransparency(attributes)
if transparency:
solid.ViewObject.Transparency = transparency
placement = getPlacement(attributes)
rotation = getRotation(attributes)
solid.Placement = App.Placement(placement, rotation)
group.addObject(solid)
def insertPrism(name, document, group, attributes):
solid = document.addObject("Part::Prism","Prism")
solid.Label = name
solid.Polygon = int(attributes['polygon'])
solid.Circumradius = '{} mm'.format(attributes['radius'])
solid.Height = '{} mm'.format(attributes['height'])
color = getColor(attributes)
if color:
solid.ViewObject.ShapeColor = color
transparency = getTransparency(attributes)
if transparency:
solid.ViewObject.Transparency = transparency
placement = getPlacement(attributes)
rotation = getRotation(attributes)
solid.Placement = App.Placement(placement, rotation)
group.addObject(solid)
def insertWedge(name, document, group, attributes):
solid = document.addObject("Part::Wedge","Wedge")
solid.Label = name
solid.Xmin = '{} mm'.format(attributes['xmin'])
solid.Ymin = '{} mm'.format(attributes['ymin'])
solid.Zmin = '{} mm'.format(attributes['zmin'])
solid.X2min = '{} mm'.format(attributes['x2min'])
solid.Z2min = '{} mm'.format(attributes['z2min'])
solid.Xmax = '{} mm'.format(attributes['xmax'])
solid.Ymax = '{} mm'.format(attributes['ymax'])
solid.Zmax = '{} mm'.format(attributes['zmax'])
solid.X2max = '{} mm'.format(attributes['x2max'])
solid.Z2max = '{} mm'.format(attributes['z2max'])
color = getColor(attributes)
if color:
solid.ViewObject.ShapeColor = color
transparency = getTransparency(attributes)
if transparency:
solid.ViewObject.Transparency = transparency
placement = getPlacement(attributes)
rotation = getRotation(attributes)
solid.Placement = App.Placement(placement, rotation)
group.addObject(solid)
def getColor(json_data):
color_data = json_data.get('color', None)
if not color_data:
return None
if not isinstance(color_data, list):
if color_data not in predefined_colors:
raise Exception('Color data needs to be an array of RGB floats or one of predefined colors!!!')
return predefined_colors[color_data]
return (color_data[0], color_data[1], color_data[2])
def getTransparency(json_data):
return json_data.get('transparency', None)
def getPlacement(json_data):
placement = App.Vector(.0, .0, .0)
placement_config = json_data.get('placement', None)
if placement_config:
placement = App.Vector(*placement_config)
return placement
def getRotation(json_data):
rotation_vector = json_data.get('rotationVector', (.0, .0, 1.0))
rotation_angle = json_data.get('rotationAngle', 0.0)
return App.Rotation(App.Vector(*rotation_vector), rotation_angle)
def open(filename):
base_directory = os.path.dirname(filename)
sub_directory = None
print('Reading: {}'.format(filename))
print('Base: {}'.format(base_directory))
yaml_data = None
with pythonopen(filename) as f:
yaml_data = safe_load(f)
if yaml_data is None:
raise Exception("Error reading YAML file: {}".format(filename))
print('YML data: {}'.format(yaml_data))
if 'settings' in yaml_data:
if 'subDirectory' in yaml_data['settings']:
base_directory += '/{}'.format(yaml_data['settings']['subDirectory'])
print('Base: {}'.format(base_directory))
if 'import' not in yaml_data:
raise Exception('No \'import\' section in YAML file!!!')
yaml_data = yaml_data['import']
for document_name, document_data in yaml_data.items():
document = App.newDocument(document_name)
for group_name, group_data in document_data.items():
document_group = document.addObject("App::DocumentObjectGroup", group_name)
if isinstance(group_data, str):
insertObject(base_directory, group_data, document, document_group)
continue
if isinstance(group_data, list):
for file in group_data:
insertObject(base_directory, file, document, document_group)
continue
for file, file_data in group_data.items():
if file == 'files':
for f in file_data:
insertObject(base_directory, f, document, document_group)
continue
if not isinstance(file_data, list):
if 'solid' not in file_data:
insertObject(base_directory, file, document, document_group, file_data)
else:
insertSolid(file, document, document_group, file_data)
else:
for file_data2 in file_data:
insertObject(base_directory, file, document, document_group, file_data2)
document.recompute()
Gui.activeDocument().activeView().viewAxonometric()
Gui.SendMsgToActiveView("ViewFit")