-
Notifications
You must be signed in to change notification settings - Fork 0
/
ose-developer-test.FCMacro
345 lines (260 loc) · 12.2 KB
/
ose-developer-test.FCMacro
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
"""
OSE Developer Test Macro
Requirements:
* Import 8 hole tubing from OSE Part Library and build a cube
* Fasten a bolt through one of the corners with a nut
* Pocket initials into any face of any tube
8 Hole Tubing Analysis
----------------------
+---+
/ /|
+---+ |
| | | H
| | |
| | +
| |/ S
+---+
S
Width and length are equal making the tubing a square prism.
"""
import os
import Draft
import FreeCAD
import FreeCADGui
import ScrewMaker
from FreeCAD import Placement, Rotation, Vector
from Part import ArcOfCircle, Circle, LineSegment
from Sketcher import Constraint
def main():
tubing_filename = '8 hole tubing.fcstd'
document = open_tubing(tubing_filename)
initial_tube = document.Clone
base_tube_label = tubing_filename.split('.')[0]
tubes = clone_tubes_for_cube(initial_tube, base_tube_label)
arrange_tubes_into_cube(document, tubes)
last_tube = tubes[-1]
create_bolt_and_nut_through_corner_of_cube(last_tube)
create_pocketed_initials(document, last_tube, 'Face4')
def open_tubing(filename):
"""
Opens tubing file from tubing library.
"""
script_path = os.path.dirname(os.path.abspath(__file__))
tubing_path = os.path.join(script_path, 'Tubing Library', filename)
return FreeCAD.open(tubing_path)
def get_dimensions(document, tubing):
edges = tubing.Shape.Edges
bottom_horizontal_edge = edges[1]
side = calculate_edge_distance(document, bottom_horizontal_edge)
vertical_side_edge = edges[0]
height = calculate_edge_distance(document, vertical_side_edge)
return side, height
def calculate_edge_distance(document, edge):
p1, p2 = edge.Vertexes
dimension = Draft.makeDimension(p1.Point, p2.Point)
distance_value = dimension.Distance.Value
document.removeObject('Dimension')
return distance_value
def clone_tubes_for_cube(initial_tube, base_label):
"""
Clones initial tube 12 times for cube.
"""
label = base_label + ' %s'
initial_tube.Label = label % 1
num_tubing_clones = 12
tubes = [initial_tube]
for i in range(2, num_tubing_clones + 1):
cloned_tube = Draft.clone(initial_tube)
cloned_tube.Label = label % i
tubes.append(cloned_tube)
return tubes
def arrange_tubes_into_cube(document, tubes):
"""
Arrange list of 12 tubes into a cube based on dimensions of first tube.
Assumes all tubes are clones of each other and have the same dimensions.
"""
if len(tubes) != 12:
raise ValueError('Must have 12 tubes to arrange into cube')
side, height = get_dimensions(document, tubes[0])
half_side = side / 2
# Bottom layer of base
tubes[0].Placement = Placement(Vector(-half_side, side, half_side), Rotation(Vector(0, 1, 0), 90))
tubes[4].Placement = Placement(Vector(-half_side, (height - side * 2), half_side), Rotation(Vector(0, 1, 0), 90))
# Top layer of base
tubes[2].Placement = Placement(Vector(side, -half_side, (side + half_side)), Rotation(Vector(-1, 0, 0), 90))
tubes[6].Placement = Placement(Vector((height - side * 2), -half_side, (side + half_side)), Rotation(Vector(-1, 0, 0), 90))
# Move 3 clones for pillars
# The last tube (#12) isn't moved and acts as the fourth pillar
tubes[1].Placement = Placement(Vector((height - side), 0, 0), Rotation(Vector(0, 0, 1), 0))
tubes[3].Placement = Placement(Vector(0, (height - side), 0), Rotation(Vector(0, 0, 1), 0))
tubes[5].Placement = Placement(Vector((height - side), (height - side), 0), Rotation(Vector(0, 0, 1), 0))
# Bottom layer of top
tubes[7].Placement = Placement(Vector(side, -half_side, (height - side - half_side)), Rotation(Vector(-1, 0, 0), 90))
tubes[8].Placement = Placement(Vector((height - side * 2), -half_side, (height - side - half_side)), Rotation(Vector(-1, 0, 0), 90))
# Top-most layer of top
tubes[9].Placement = Placement(Vector(-half_side, side, height - half_side), Rotation(Vector(0, 1, 0), 90))
tubes[10].Placement = Placement(Vector(-half_side, (height - side * 2), height - half_side), Rotation(Vector(0, 1, 0), 90))
def create_bolt_and_nut_through_corner_of_cube(tube):
# TODO: Don't hardcode Face of tube
face_edges = tube.Shape.Faces[13].Edges
# Find top hole in face of tube
holes = list(filter(is_edge_circular, face_edges))
holes_by_z_descending = sorted(holes, key=get_z_of_hole, reverse=True)
top_hole = holes_by_z_descending[0]
# Find top hole edge index in tube edges
edges = tube.Shape.Edges
edge_index = [i for i, e in enumerate(edges) if e.isSame(top_hole)][0]
# Select top hole
FreeCADGui.Selection.addSelection(tube, 'Edge{}'.format(edge_index + 1))
# Simulate clicking ISO 4014 Hex head bolt button in GUI
# Fasteners workbench must be loaded in GUI first for this to work
FreeCADGui.runCommand('FSISO4014')
# Invert and set length of bolt
bolt = FreeCAD.ActiveDocument.Screw
# TODO: Don't hardcode length
bolt.length = '220'
bolt.invert = True
FreeCAD.ActiveDocument.recompute()
# Create nut
sm = ScrewMaker.Instance()
nut = sm.createFastener('ISO4032', '(M33)', 0, 'simple')
# TODO: Don't hard-code bottom edge of bolt
bottom_bolt_edge = bolt.Shape.Edges[40]
inverted = True
offset = 1
move_to_object = ScrewMaker.FastenerBase.FSMoveToObject
move_to_object(nut, bottom_bolt_edge, not inverted, offset)
def is_edge_circular(edge):
return isinstance(edge.Curve, Circle)
def get_z_of_hole(hole):
return hole.Vertexes[0].Z
def create_pocketed_initials(document, tube, face):
"""
Create pocketed initials on a particular face of a given tube.
"""
guiDocument = FreeCADGui.ActiveDocument
body = create_body(document, guiDocument, tube, face)
sketch = create_sketch(guiDocument, body, face)
create_initials_in_sketch(sketch)
create_pocket(document, guiDocument, body, sketch)
def create_body(document, guiDocument, tube, face):
body_name = 'Body'
body = document.addObject('PartDesign::Body', body_name)
body.BaseFeature = tube
tube.ViewObject.Visibility = False
guiDocument.ActiveView.setActiveObject('pdbody', body)
return body
def create_sketch(guiDocument, body, face):
sketch_name = 'Sketch'
sketch = body.newObject('Sketcher::SketchObject', sketch_name)
sketch.Support = (body.BaseFeature, [face])
sketch.MapMode = 'FlatFace'
guiDocument.setEdit(sketch_name)
return sketch
def create_initials_in_sketch(sketch):
create_capital_g_in_left_side_of_sketch(sketch)
create_capital_r_in_right_side_of_sketch(sketch)
def create_capital_g_in_left_side_of_sketch(sketch):
center_x = -24.5
center = Vector(center_x, 0, 0)
bottom_midline_y = -4.3
bottom_midline = Vector(center_x, bottom_midline_y, 0)
# Outer arc of G
sketch.addGeometry(ArcOfCircle(Circle(center, Vector(0, 0, 1), 20.7), 0.75, 6), False)
sketch.addConstraint(Constraint('PointOnObject', 0, 3, -1))
sketch.addConstraint(Constraint('PointOnObject', 0, 2, -1))
# Horizontal mid-line of G
sketch.addGeometry(LineSegment(Vector(-3.9, 0, 0), center), False)
sketch.addConstraint(Constraint('Coincident', 1, 1, 0, 2))
# Inner arc of G
sketch.addGeometry(ArcOfCircle(Circle(center, Vector(0, 0, 1), 15.5), 0.75, 6), False)
sketch.addConstraint(Constraint('Coincident', 2, 3, 1, 2))
# Lower horizontal mid-line of G
sketch.addGeometry(LineSegment(Vector(-9.5, bottom_midline_y, 0), bottom_midline), False)
sketch.addConstraint(Constraint('Coincident', 3, 1, 2, 2))
sketch.addConstraint(Constraint('Horizontal', 3))
# Connecting line between two horizontal mid-lines
sketch.addGeometry(LineSegment(center, bottom_midline), False)
sketch.addConstraint(Constraint('Coincident', 4, 1, 0, 3))
sketch.addConstraint(Constraint('Coincident', 4, 2, 3, 2))
sketch.addConstraint(Constraint('Coincident', 0, 3, 1, 2))
sketch.addConstraint(Constraint('Vertical', 4))
# Connecting line between outer and inner arcs
sketch.addGeometry(LineSegment(Vector(-9.5, 13.8, 0), Vector(-13.8, 11.2, 0)), False)
sketch.addConstraint(Constraint('Coincident', 5, 1, 0, 1))
sketch.addConstraint(Constraint('Coincident', 5, 2, 2, 1))
def create_capital_r_in_right_side_of_sketch(sketch):
# Left vertical line of R
sketch.addGeometry(LineSegment(Vector(7.4, -20.4, 0), Vector(7.7, 19.6, 0)), False)
sketch.addConstraint(Constraint('Vertical', 6))
# Top horizontal line of R
sketch.addGeometry(LineSegment(Vector(7.4, 19.6, 0), Vector(29.3, 19.5, 0)), False)
sketch.addConstraint(Constraint('Coincident', 6, 2, 7, 1))
sketch.addConstraint(Constraint('Horizontal', 7))
# Top right arc R
sketch.addGeometry(ArcOfCircle(Circle(Vector(26.2, 9.4, 0), Vector(0, 0, 1), 10.2), -1.1, 1.3), False)
sketch.addConstraint(Constraint('Coincident', 8, 2, 7, 2))
# Midline connecting to bottom point of top right arc
sketch.addGeometry(LineSegment(Vector(30.7, -0.3, 0), Vector(16.6, -0.6, 0)), False)
sketch.addConstraint(Constraint('Coincident', 9, 1, 8, 1))
sketch.addConstraint(Constraint('PointOnObject', 9, 2, -1))
sketch.addConstraint(Constraint('Horizontal', 9))
# Outer vertical slanted line of right leg connecting to left point of midline
sketch.addGeometry(LineSegment(Vector(16.6, 0, 0), Vector(32.2, -20.7, 0)), False)
sketch.addConstraint(Constraint('Coincident', 10, 1, 9, 2))
# Horizontal bottom line of right leg
sketch.addGeometry(LineSegment(Vector(32.2, -20.7, 0), Vector(24, -20.7, 0)), False)
sketch.addConstraint(Constraint('Coincident', 11, 1, 10, 2))
sketch.addConstraint(Constraint('Horizontal', 11))
# Inner vertical slanted line of right leg
sketch.addGeometry(LineSegment(Vector(24, -20.7, 0), Vector(14.8, -6.8, 0)), False)
sketch.addConstraint(Constraint('Coincident', 12, 1, 11, 2))
# Vertical right side of left leg
sketch.addGeometry(LineSegment(Vector(14.8, -6.8, 0), Vector(14.9, -20.4, 0)), False)
sketch.addConstraint(Constraint('Coincident', 13, 1, 12, 2))
sketch.addConstraint(Constraint('Vertical', 13))
# Horizontal bottom line of left leg connecting to left vertical line of R
sketch.addGeometry(LineSegment(Vector(14.8, -20.4, 0), Vector(7.4, -20.4, 0)), False)
sketch.addConstraint(Constraint('Coincident', 14, 1, 13, 2))
sketch.addConstraint(Constraint('Coincident', 14, 2, 6, 1))
sketch.addConstraint(Constraint('Horizontal', 14))
# Left vertical line of inner top hole
sketch.addGeometry(LineSegment(Vector(17, 13, 0), Vector(17.3, 7, 0)), False)
sketch.addConstraint(Constraint('Vertical', 15))
# Top horizontal line of inner top hole
sketch.addGeometry(LineSegment(Vector(17, 13, 0), Vector(25.3, 13, 0)), False)
sketch.addConstraint(Constraint('Coincident', 16, 1, 15, 1))
sketch.addConstraint(Constraint('Horizontal', 16))
# Right arc of inner top hole
sketch.addGeometry(ArcOfCircle(Circle(Vector(24.5, 9.6, 0), Vector(0, 0, 1), 3.5), -1, 1.3), False)
sketch.addConstraint(Constraint('Coincident', 17, 2, 16, 2))
# Bottom horizontal line of inner top hole connecting right arc and left vertical line
sketch.addGeometry(LineSegment(Vector(26.3, 6.6, 0), Vector(17.3, 6.5, 0)), False)
sketch.addConstraint(Constraint('Coincident', 18, 1, 17, 1))
sketch.addConstraint(Constraint('Coincident', 18, 2, 15, 2))
sketch.addConstraint(Constraint('Horizontal', 18))
def create_pocket(document, guiDocument, body, sketch):
pocket_name = 'Pocket'
pocket = body.newObject('PartDesign::Pocket', pocket_name)
pocket.Profile = sketch
pocket.Length = 5.0
pocket.Length2 = 100.0
pocket.Type = 0
pocket.UpToFace = None
pocket.Reversed = 0
pocket.Midplane = 0
pocket.Offset = 0.0
guiDocument.hide(sketch.Name)
pocket_view = pocket.ViewObject
body_view = body.ViewObject
pocket_view.ShapeColor = body_view.ShapeColor
pocket_view.LineColor = body_view.LineColor
pocket_view.PointColor = body_view.PointColor
pocket_view.Transparency = body_view.Transparency
pocket_view.DisplayMode = body_view.DisplayMode
guiDocument.setEdit(pocket_name, 0)
guiDocument.hide(sketch.Name)
guiDocument.hide('BaseFeature')
guiDocument.resetEdit()
if __name__ == '__main__':
main()