-
Notifications
You must be signed in to change notification settings - Fork 27
/
EAInit.py
510 lines (400 loc) · 17.8 KB
/
EAInit.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# -*- coding: utf-8 -*-
# FreeCAD Exploded Assembly Animation Workbench
# (c) 2016 Javier Martínez García
#***************************************************************************
#* (c) Javier Martínez García 2016 *
#* *
#* This program is free software; you can redistribute it and/or modify *
#* it under the terms of the GNU General Public License (GPL) *
#* as published by the Free Software Foundation; either version 2 of *
#* the License, or (at your option) any later version. *
#* for detail see the LICENCE text file. *
#* *
#* This macro is distributed in the hope that it will be useful, *
#* but WITHOUT ANY WARRANTY; without even the implied warranty of *
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
#* GNU Lesser General Public License for more details. *
#* *
#* You should have received a copy of the GNU Library General Public *
#* License along with FreeCAD; if not, write to the Free Software *
#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
#* USA *
#* *
#***************************************************************************/
import os
import FreeCAD
import FreeCADGui
import ExplodedAssembly as ea
import CameraAnimation as ca
__dir__ = os.path.dirname(__file__)
# TODO CHANGE NAME: SIMPLE DISASSEMBLE GROUP, WIRE DISASSEMBLE GROUP
class CreateBoltGroup:
def GetResources(self):
return {'Pixmap': __dir__ + '/icons/BoltGroup.svg',
'MenuText': 'Create Bolt Group',
'ToolTip': 'Create a special exploded group for screws, nuts, bolts... \n Select circular edges of the shape you want to animate, then\n select one face (arbitrary shape) wich has as normal vector\nin the direction in wich you want to move the selected shapes'}
def IsActive(self):
if FreeCADGui.ActiveDocument:
if not(FreeCAD.ActiveDocument.ExplodedAssembly.InAnimation):
return True
else:
return False
def Activated(self):
ea.checkDocumentStructure()
ea.createBoltDisassemble()
class CreateSimpleGroup:
def GetResources(self):
return {'Pixmap': __dir__ + '/icons/SimpleGroup.svg',
'MenuText': 'Create Simple Group',
'ToolTip': 'Select the objects you want to explode and\nfinally the face which its normal is the trajectory director vector'}
def IsActive(self):
if FreeCADGui.ActiveDocument:
if not(FreeCAD.ActiveDocument.ExplodedAssembly.InAnimation):
return True
else:
return False
def Activated(self):
ea.checkDocumentStructure()
ea.createSimpleDisassemble()
class CreateWireGroup:
def GetResources(self):
return {'Pixmap': __dir__ + '/icons/WireTrajectory.svg',
'MenuText': 'Create Route',
'ToolTip': 'Select first one or more shapes and last the wire or sketch that represents the trajectory'}
def IsActive(self):
if FreeCADGui.ActiveDocument:
if not(FreeCAD.ActiveDocument.ExplodedAssembly.InAnimation):
return True
else:
return False
def Activated(self):
pass
class PlaceBeforeSelectedTrajectory:
# execute this function with caution
def GetResources(self):
return {'Pixmap': __dir__ + '/icons/PlaceBeforeTrajectory.svg',
'MenuText': 'PlaceBefore',
'ToolTip': 'Select the trajectories you want to reallocate (in order) and finally\nthe trajectory before which you want to place them'}
def IsActive(self):
if FreeCADGui.ActiveDocument:
if not(FreeCAD.ActiveDocument.ExplodedAssembly.InAnimation):
return True
else:
return False
def Activated(self):
# check selection before running (rearrange objects is dangerous, use with caution)
try:
sel = FreeCAD.Gui.Selection.getSelectionEx()
a = sel[0].Object.Distance
a = sel[1].Object.Distance
# selection are trajectories, proceed
ea.placeBeforeSelectedTrajectory()
ea.resetPlacement()
FreeCAD.Gui.Selection.clearSelection()
FreeCAD.Gui.Selection.addSelection(sel[0].Object)
ea.goToSelectedTrajectory()
except:
FreeCAD.Console.PrintError('\n Select exploded assembly trajectory objects only')
class ModifyIndividualObjectTrajectory:
# execute this function with caution
def GetResources(self):
return {'Pixmap': __dir__ + '/icons/ModifyIndividualObjectTrajectory.svg',
'MenuText': 'Modify Individual Object Trajectory',
'ToolTip': 'Explode objects on several directions at once\nSelect the trajectory, select the object and\nthe face which its normal is the new director\nvector'}
def IsActive(self):
if FreeCADGui.ActiveDocument:
if not(FreeCAD.ActiveDocument.ExplodedAssembly.InAnimation):
return True
else:
return False
def Activated(self):
# check selection before running (rearrange objects is dangerous, use with caution)
ea.modifyIndividualObjectTrajectory()
sel_traj = FreeCAD.Gui.Selection.getSelectionEx()[0].Object
FreeCAD.Gui.Selection.clearSelection()
FreeCAD.Gui.Selection.addSelection(sel_traj)
ea.goToSelectedTrajectory()
# camera commands
class CreateManualCamera:
def GetResources(self):
return {'Pixmap': __dir__ + '/icons/AnimationCameraManual.svg',
'MenuText': 'Manual Animation Camera',
'ToolTip': 'Create a transition between two cameras'}
def IsActive(self):
if FreeCADGui.ActiveDocument:
if not(FreeCAD.ActiveDocument.ExplodedAssembly.InAnimation):
return True
else:
return False
def Activated(self):
pass
class CreateEdgeCamera:
def GetResources(self):
return {'Pixmap': __dir__ + '/icons/AnimationCameraEdge.svg',
'MenuText': 'Manual Animation Camera',
'ToolTip': 'Create a transition between two cameras'}
def IsActive(self):
if FreeCADGui.ActiveDocument:
if not(FreeCAD.ActiveDocument.ExplodedAssembly.InAnimation):
return True
else:
return False
def Activated(self):
pass
class CreateFollowCamera:
def GetResources(self):
return {'Pixmap': __dir__ + '/icons/AnimationCameraFollow.svg',
'MenuText': 'Manual Animation Camera',
'ToolTip': 'Create a transition between two cameras'}
def IsActive(self):
if FreeCADGui.ActiveDocument:
if not(FreeCAD.ActiveDocument.ExplodedAssembly.InAnimation):
return True
else:
return False
def Activated(self):
pass
class PlayForward:
def GetResources(self):
return {'Pixmap': __dir__ + '/icons/PlayForward.svg',
'MenuText': 'Play Forward',
'ToolTip': 'Run the assembly animation'}
def IsActive(self):
if FreeCADGui.ActiveDocument:
if not(FreeCAD.ActiveDocument.ExplodedAssembly.InAnimation):
return True
else:
return False
def Activated(self):
EA = FreeCAD.ActiveDocument.ExplodedAssembly
if EA.CurrentTrajectory <= 0:
# if exploded state = 0 or -1, reset and run
ea.resetPlacement()
ea.runAnimation()
else:
# if animation has been paused in the middle:
cr_traj = EA.Group[EA.CurrentTrajectory]
ea.runAnimation(start=EA.CurrentTrajectory+1, mode='toPoint')
class PlayBackward:
def GetResources(self):
return {'Pixmap': __dir__ + '/icons/PlayBackward.svg',
'MenuText': 'Play Backwards',
'ToolTip': __dir__ + 'icons/TrajectoryEdit.svg'}
def IsActive(self):
if FreeCADGui.ActiveDocument:
if not(FreeCAD.ActiveDocument.ExplodedAssembly.InAnimation):
return True
else:
return False
def Activated(self):
EA = FreeCAD.ActiveDocument.ExplodedAssembly
if EA.CurrentTrajectory <= 0:
# if exploded state = 0 or -1, reset and run
ea.resetPlacement()
ea.goToEnd()
ea.runAnimation(direction='backward')
else:
# if animation has been paused in the middle:
ea.runAnimation(end=-EA.CurrentTrajectory - 1 + len(EA.Group),
mode='toPoint',
direction='backward')
class StopAnimation:
def GetResources(self):
return {'Pixmap': __dir__ + '/icons/Pause.svg',
'MenuText': 'StopAnimation',
'ToolTip': 'Stops the animation at the current trajectory'}
def IsActive(self):
if FreeCADGui.ActiveDocument:
if not(FreeCAD.ActiveDocument.ExplodedAssembly.InAnimation):
return False
else:
return True
else:
return False
def Activated(self):
FreeCAD.ActiveDocument.ExplodedAssembly.InAnimation = False
# FreeCAD.Gui.updateGui()
class GoToStart:
def GetResources(self):
return {'Pixmap': __dir__ + '/icons/GoToStart.svg',
'MenuText': 'Assemble',
'ToolTip': 'Go to the assembled position of the parts'}
def IsActive(self):
if FreeCADGui.ActiveDocument.ExplodedAssembly:
if not(FreeCAD.ActiveDocument.ExplodedAssembly.InAnimation):
return True
else:
return False
def Activated(self):
ea.resetPlacement()
FreeCAD.ActiveDocument.ExplodedAssembly.CurrentTrajectory = 0
class GoToEnd:
def GetResources(self):
return {'Pixmap': __dir__ + '/icons/GoToEnd.svg',
'MenuText': 'Disassemble',
'ToolTip': 'Expand all trajectories'}
def IsActive(self):
if FreeCADGui.ActiveDocument.ExplodedAssembly:
if not(FreeCAD.ActiveDocument.ExplodedAssembly.InAnimation):
return True
else:
return False
def Activated(self):
ea.resetPlacement()
ea.goToEnd()
FreeCAD.ActiveDocument.ExplodedAssembly.CurrentTrajectory = -1
class GoToSelectedTrajectory:
def GetResources(self):
return {'Pixmap': __dir__ + '/icons/ExplodeToSelection.svg',
'MenuText': 'ExplodeToSelection',
'ToolTip': 'Expand up to the selected trajectory'}
def IsActive(self):
if FreeCADGui.ActiveDocument.ExplodedAssembly:
if not(FreeCAD.ActiveDocument.ExplodedAssembly.InAnimation):
return True
else:
return False
def Activated(self):
try:
ea.resetPlacement()
ea.goToSelectedTrajectory()
except:
FreeCAD.Console.PrintError('Error: Select one exploded animation trajectory')
class ToggleTrajectoryVisibility:
def __init__(self):
self.visibility = True
def GetResources(self):
return {'Pixmap': __dir__ + '/icons/TrajectoryLineVisibility.svg',
'MenuText': 'Trajectory Visibility',
'ToolTip': 'Toggle trajectory visibility'}
def IsActive(self):
if FreeCADGui.ActiveDocument.ExplodedAssembly:
return True
else:
return False
def Activated(self):
self.visibility = not(self.visibility)
ea.visibilityTrajectoryLines(self.visibility)
# Assembly tools ###############################################################
class AlignToEdge:
def GetResources(self):
return {'Pixmap': __dir__ + '/icons/AlignToEdge.svg',
'MenuText': 'Align to edge',
'ToolTip': 'Auxiliary tool to align shapes.\nPick one edge of the object you want to align and\nthen the edge of the object used as reference'}
def IsActive(self):
if FreeCADGui.ActiveDocument:
if not(FreeCAD.ActiveDocument.ExplodedAssembly.InAnimation):
return True
else:
return False
def Activated(self):
sel = FreeCAD.Gui.Selection.getSelectionEx()
objA = sel[0].Object
edgeA = sel[0].SubObjects[0]
edgeB = sel[1].SubObjects[0]
# transform object A placement
# edge vector
va = (edgeA.Curve.EndPoint - edgeA.Curve.StartPoint).normalize()
vb = (edgeB.Curve.EndPoint - edgeB.Curve.StartPoint).normalize()
# rot centre
centre = edgeA.Curve.StartPoint
# new placement
new_plm = FreeCAD.Placement(FreeCAD.Vector(0,0,0), FreeCAD.Rotation(va, vb), centre)
# apply placement
objA.Placement = new_plm.multiply(objA.Placement)
class Rotate15:
def GetResources(self):
return {'Pixmap': __dir__ + '/icons/Rotate90.svg',
'MenuText': 'Rotate 15',
'ToolTip': 'Auxiliary tool to rotate a shape 15 degrees.\nSelect a face of the object you want to rotate\n and it will be rotated 15 degrees using its normal as rotation\n axis'}
def IsActive(self):
if FreeCADGui.ActiveDocument:
if not(FreeCAD.ActiveDocument.ExplodedAssembly.InAnimation):
return True
else:
return False
def Activated(self):
sel = FreeCAD.Gui.Selection.getSelectionEx()
objA = sel[0].Object
selFace = sel[0].SubObjects[0]
rot_center = selFace.CenterOfMass
rot_axis = selFace.normalAt(0, 0)
rot = FreeCAD.Rotation(rot_axis, 15)
objA.Placement = FreeCAD.Placement(FreeCAD.Vector(0,0,0), rot, rot_center).multiply(objA.Placement)
class PointToPoint:
def GetResources(self):
return {'Pixmap': __dir__ + '/icons/SharePoint.svg',
'MenuText': 'Point to point',
'ToolTip': 'Auxiliary tool to move point to point.\nSelect one point from the shape you want to move \n and then the point from other shape where you want to place it'}
def IsActive(self):
if FreeCADGui.ActiveDocument:
if not(FreeCAD.ActiveDocument.ExplodedAssembly.InAnimation):
return True
else:
return False
def Activated(self):
sel = FreeCAD.Gui.Selection.getSelectionEx()
objA = sel[0].Object
PA = sel[0].SubObjects[0]
PB = sel[1].SubObjects[0]
v = PB.Point - PA.Point
objA.Placement.Base.x += v.x
objA.Placement.Base.y += v.y
objA.Placement.Base.z += v.z
class PlaceConcentric:
def GetResources(self):
return {'Pixmap': __dir__ + '/icons/ShareCenter.svg',
'MenuText': 'Place concentrically',
'ToolTip': 'Auxiliary tool to place two shapes concentrically\nPlace the first circular edge selected concentric to \nthe second circular edge selected'}
def IsActive(self):
if FreeCADGui.ActiveDocument:
if not(FreeCAD.ActiveDocument.ExplodedAssembly.InAnimation):
return True
else:
return False
def Activated(self):
sel = FreeCAD.Gui.Selection.getSelectionEx()
objA = sel[0].Object
CentreA = sel[0].SubObjects[0].Curve.Center
CentreB = sel[1].SubObjects[0].Curve.Center
v = CentreB - CentreA
objA.Placement.Base.x += v.x
objA.Placement.Base.y += v.y
objA.Placement.Base.z += v.z
# non icon comands
class LoadExampleFile:
def GetResources(self):
return {'Pixmap': '',
'MenuText': 'Load Example File',
'ToolTip': 'Load an exploded assembly example file'}
def IsActive(self):
if not(FreeCADGui.ActiveDocument):
return True
else:
if FreeCAD.ActiveDocument.Name != 'example':
return True
else:
return False
def Activated(self):
FreeCAD.open(__dir__ + '/example.fcstd')
if FreeCAD.GuiUp:
FreeCAD.Gui.addCommand('CreateBoltGroup', CreateBoltGroup())
FreeCAD.Gui.addCommand('CreateSimpleGroup', CreateSimpleGroup())
FreeCAD.Gui.addCommand('CreateWireGroup', CreateWireGroup())
FreeCAD.Gui.addCommand('ModifyIndividualObjectTrajectory', ModifyIndividualObjectTrajectory())
FreeCAD.Gui.addCommand('CreateManualCamera', CreateManualCamera())
FreeCAD.Gui.addCommand('CreateEdgeCamera', CreateEdgeCamera())
FreeCAD.Gui.addCommand('CreateFollowCamera', CreateFollowCamera())
FreeCAD.Gui.addCommand('PlaceBeforeSelectedTrajectory', PlaceBeforeSelectedTrajectory())
FreeCAD.Gui.addCommand('GoToStart', GoToStart())
FreeCAD.Gui.addCommand('PlayBackward', PlayBackward())
FreeCAD.Gui.addCommand('StopAnimation', StopAnimation())
FreeCAD.Gui.addCommand('PlayForward', PlayForward())
FreeCAD.Gui.addCommand('GoToEnd', GoToEnd())
FreeCAD.Gui.addCommand('GoToSelectedTrajectory',GoToSelectedTrajectory())
FreeCAD.Gui.addCommand('ToggleTrajectoryVisibility', ToggleTrajectoryVisibility())
FreeCAD.Gui.addCommand('AlignToEdge', AlignToEdge())
FreeCAD.Gui.addCommand('Rotate15', Rotate15())
FreeCAD.Gui.addCommand('PointToPoint', PointToPoint())
FreeCAD.Gui.addCommand('PlaceConcentric', PlaceConcentric())
FreeCAD.Gui.addCommand('LoadExampleFile', LoadExampleFile())