forked from JMG1/ExplodedAssembly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExplodedAssembly.py
More file actions
672 lines (557 loc) · 25.8 KB
/
ExplodedAssembly.py
File metadata and controls
672 lines (557 loc) · 25.8 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
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
# -*- coding: utf-8 -*-
# Exploded Assembly Animation workbench for FreeCAD
# (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 program 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 *
#* *
#***************************************************************************/
from __future__ import division
import os
import time
import FreeCAD
import Part
# Container python folder 'ExplodedAssembly'
class ExplodedAssemblyFolder:
def __init__(self, obj):
obj.addProperty('App::PropertyPythonObject', 'InitialPlacements').InitialPlacements = {}
obj.addProperty('App::PropertyInteger', 'AnimationStep').AnimationStep = 0
obj.addProperty('App::PropertyInteger', 'CurrentTrajectory').CurrentTrajectory = 0
obj.addProperty('App::PropertyBool', 'ResetAnimation').ResetAnimation = False
obj.addProperty('App::PropertyBool', 'InAnimation').InAnimation = False
obj.addProperty('App::PropertyBool', 'RemoveAllTrajectories').RemoveAllTrajectories = False
obj.Proxy = self
def execute(self, fp):
if fp.ResetAnimation:
resetPlacement()
fp.ResetAnimation = False
if fp.RemoveAllTrajectories:
fp.RemoveAllTrajectories = False
for obj in fp.Group:
FreeCAD.ActiveDocument.removeObject(obj.Name)
# reset initial placement dict
fp.InitialPlacements = {}
class ExplodedAssemblyFolderViewProvider:
def __init__(self, obj):
obj.Proxy = self
def getIcon(self):
__dir__ = os.path.dirname(__file__)
return __dir__ + '/icons/WorkbenchIcon.svg'
def checkDocumentStructure():
# checks the existence of the folder 'ExplodedAssembly' and creates it if not
try:
folder = FreeCAD.ActiveDocument.ExplodedAssembly
except:
folder = FreeCAD.ActiveDocument.addObject('App::DocumentObjectGroupPython', 'ExplodedAssembly')
ExplodedAssemblyFolder(folder)
ExplodedAssemblyFolderViewProvider(folder.ViewObject)
# Bolt group trajectory ########################################################
class BoltGroupObject:
def __init__(self, obj):
obj.addProperty('App::PropertyPythonObject', 'names').names = []
obj.addProperty('App::PropertyPythonObject', 'dir_vectors').dir_vectors = []
obj.addProperty('App::PropertyPythonObject', 'rot_vectors').rot_vectors = []
obj.addProperty('App::PropertyPythonObject', 'rot_centers').rot_centers = []
#
obj.addProperty('App::PropertyFloat', 'Distance').Distance = 20.0
obj.addProperty('App::PropertyFloat', 'Revolutions').Revolutions = 0.0
obj.addProperty('App::PropertyFloat', 'AnimationStepTime').AnimationStepTime = 0.0
obj.addProperty('App::PropertyInteger', 'AnimationSteps').AnimationSteps = 20
obj.Proxy = self
def onChanged(self, fp, prop):
pass
def execute(self, fp):
resetPlacement()
goToEnd()
FreeCAD.ActiveDocument.ExplodedAssembly.CurrentTrajectory = -1
class BoltGroupObjectViewProvider:
def __init__(self, obj):
obj.Proxy = self
def getIcon(self):
__dir__ = os.path.dirname(__file__)
return __dir__ + '/icons/BoltGroup.svg'
def createBoltDisassemble():
# add object to the Document and initialize it
SDObj = FreeCAD.ActiveDocument.addObject('App::DocumentObjectGroupPython','BoltGroup')
BoltGroupObject(SDObj)
BoltGroupObjectViewProvider(SDObj.ViewObject)
# retrieve selection
selection = FreeCAD.Gui.Selection.getSelectionEx()
# try to find the face wich its normal gives the disassemble direction
dir_vector = FreeCAD.Vector(0, 0, 1)
for sel_obj in selection:
for subObject in sel_obj.SubObjects:
try:
dir_vector = subObject.normalAt(0, 0)
break
except:
pass
for sel_obj in selection:
for subObject in sel_obj.SubObjects:
try:
if str(subObject.Curve)[0:6] == 'Circle':
# append object name
SDObj.names.append(sel_obj.Object.Name)
# append unmount direction
JSON_dir_vector = (dir_vector[0], dir_vector[1], dir_vector[2])
SDObj.dir_vectors.append(JSON_dir_vector)
# apend rotation axis
JSON_rot_axis = (dir_vector[0], dir_vector[1], dir_vector[2])
SDObj.rot_vectors.append(JSON_rot_axis)
# append rotation center
rot_center = subObject.Curve.Center
JSON_rot_center = (rot_center[0], rot_center[1], rot_center[2])
SDObj.rot_centers.append(JSON_rot_center)
# append initial values for distance, revs, steps
SDObj.distance.append(10.0)
SDObj.revolutions.append(0)
SDObj.animation_steps.append(20.0)
except:
pass
EAFolder = FreeCAD.ActiveDocument.ExplodedAssembly
# add initial placement if this is the first movement of the parts
for name in SDObj.names:
try:
plm = EAFolder.InitialPlacements[name]
except:
an_object = FreeCAD.ActiveDocument.getObject(name)
# prepare object placement for JSON serialization
# store base placement as vector
plm = an_object.Placement
base = (plm.Base[0], plm.Base[1], plm.Base[2])
# store rotation as a quaternion
rot = plm.Rotation.Q
placement = (base, rot)
EAFolder.InitialPlacements[name] = placement
# place inside ExplodedAssembly folder and update document
EAFolder.addObject(SDObj)
updateTrajectoryLines()
FreeCAD.ActiveDocument.recompute()
# simple group trajectory ########################################################
class SimpleGroupObject:
def __init__(self, obj):
obj.addProperty('App::PropertyPythonObject', 'names').names = []
obj.addProperty('App::PropertyPythonObject', 'dir_vectors').dir_vectors = []
obj.addProperty('App::PropertyPythonObject', 'rot_vectors').rot_vectors = []
obj.addProperty('App::PropertyPythonObject', 'rot_centers').rot_centers = []
#
obj.addProperty('App::PropertyFloat', 'Distance').Distance = 20.0
obj.addProperty('App::PropertyFloat', 'Revolutions').Revolutions = 0.0
obj.addProperty('App::PropertyFloat', 'AnimationStepTime').AnimationStepTime = 0.0
obj.addProperty('App::PropertyInteger', 'AnimationSteps').AnimationSteps = 20
obj.Proxy = self
def onChanged(self, fp, prop):
pass
def execute(self, fp):
resetPlacement()
goToEnd()
FreeCAD.ActiveDocument.ExplodedAssembly.CurrentTrajectory = -1
class SimpleGroupObjectViewProvider:
def __init__(self, obj):
obj.Proxy = self
def getIcon(self):
__dir__ = os.path.dirname(__file__)
return __dir__ + '/icons/SimpleGroup.svg'
def createSimpleDisassemble():
# add object to the Document and initialize it
SDObj = FreeCAD.ActiveDocument.addObject('App::DocumentObjectGroupPython', 'SimpleGroup')
SimpleGroupObject(SDObj)
SimpleGroupObjectViewProvider(SDObj.ViewObject)
# retrieve selection
selection = FreeCAD.Gui.Selection.getSelectionEx()
# the last face of the last object selected determines the disassemble dir vector
dir_vector = selection[-1].SubObjects[-1].normalAt(0, 0)
# the rotation center is the center of mass of the last face selected
rot_center = selection[-1].SubObjects[-1].CenterOfMass
# ignore last object if it cannot be moved, it is only used for positioning
if FreeCAD.ActiveDocument.getObject(selection[-1].Object.Name) is None:
del selection[-1]
# create trajectory data
for sel_obj in selection:
# append object name
SDObj.names.append(sel_obj.Object.Name)
# append unmount direction
JSON_dir_vector = (dir_vector[0], dir_vector[1], dir_vector[2])
SDObj.dir_vectors.append(JSON_dir_vector)
# apend rotation axis
JSON_rot_axis = (dir_vector[0], dir_vector[1], dir_vector[2])
SDObj.rot_vectors.append(JSON_rot_axis)
# append rotation center
JSON_rot_center = (rot_center[0], rot_center[1], rot_center[2])
SDObj.rot_centers.append(JSON_rot_center)
EAFolder = FreeCAD.ActiveDocument.ExplodedAssembly
# add initial placement if this is the first move of the parts
for name in SDObj.names:
try:
plm = EAFolder.InitialPlacements[name]
except:
an_object = FreeCAD.ActiveDocument.getObject(name)
# prepare object placement for JSON serialization
# store base placement as vector
plm = an_object.Placement
base = (plm.Base[0], plm.Base[1], plm.Base[2])
# store rotation as a quaternion
rot = plm.Rotation.Q
placement = (base, rot)
EAFolder.InitialPlacements[name] = placement
# place inside ExplodedAssembly folder and update document
EAFolder.addObject(SDObj)
updateTrajectoryLines()
FreeCAD.ActiveDocument.recompute()
# wire group trajectory ########################################################
class WireGroupObject:
def __init__(self, obj):
obj.addProperty('App::PropertyPythonObject', 'names').names = []
obj.addProperty('App::PropertyFloat', 'AnimationStepTime').AnimationStepTime = 0.0
obj.addProperty('App::PropertyInteger', 'AnimationStepsEdge').AnimationStepsEdge = 10
obj.Proxy = self
def onChanged(self, fp, prop):
pass
def execute(self, fp):
resetPlacement()
goToEnd()
FreeCAD.ActiveDocument.ExplodedAssembly.CurrentTrajectory = -1
class WireGroupObjectViewProvider:
def __init__(self, obj):
obj.Proxy = self
def getIcon(self):
__dir__ = os.path.dirname(__file__)
return __dir__ + '/icons/WireTrajectory.svg'
def createWireDisassemble():
# select the objects and finally the trajectory objects
# Animation will run over the edges of the trajectory object
sel_objects = FreeCAD.Gui.Selection.getSelection()[0:-2]
sel_wire = FreeCAD.Gui.Selection.getSelection()[-1]
# Initialize object
WDObj = FreeCAD.ActiveDocument.addObject('App::DocumentObjectGroupPython', 'WireGroup')
WireGroupObject(WDObj)
#WireGroupObjectViewProvider(WDObj.ViewObject)
# add object names
for obj in sel_objects:
obj_name = obj.Name
WDObj.names.append(obj_name)
# add trajectory objecto to this new wire group
WDObj.addObject(sel_wire)
# place inside ea folder
EAFolder = FreeCAD.ActiveDocument.ExplodedAssembly
EAFolder.addObject(WDObj)
FreeCAD.ActiveDocument.recompute()
def resetPlacement():
# restore the placement of all objects
EAFolder = FreeCAD.ActiveDocument.ExplodedAssembly
# set everything to its initial position
for traj in EAFolder.Group:
for name in traj.names:
obj = FreeCAD.ActiveDocument.getObject(name)
# create placement from initial placement list
plm = EAFolder.InitialPlacements[name]
base = FreeCAD.Vector(plm[0][0], plm[0][1], plm[0][2])
rot = FreeCAD.Rotation(plm[1][0], plm[1][1], plm[1][2], plm[1][3])
obj.Placement = FreeCAD.Placement(base, rot)
# Inclusion to import the Connection module
MC_CONNECTION = False
def connectionMC(option = False):
global MC_CONNECTION
if option == True:
MC_CONNECTION = True
else:
MC_CONNECTION = False
# End of inclusion
def runAnimation(start=0, end=0, mode='complete', direction='forward'):
# runs the animation from a start step number to the end step number
# mode: 'complete', 'toPoint'
# 'complete' means forward from start to end and backwars if already in end
# 'forward' means disassemble from start to end
# 'backward' means assemble from start to end
# 'toPoint' means go to an especific end point without animation
# toggle 'InAnimation variable' to disable other icons temporally
FreeCAD.ActiveDocument.ExplodedAssembly.InAnimation = True
# # complete mode
# start animation
EAFolder = FreeCAD.ActiveDocument.ExplodedAssembly.Group
if mode == 'complete':
number_of_trajectories = len(EAFolder)
elif mode == 'toPoint':
number_of_trajectories = len(EAFolder) - start - end
traj_iterator = range(number_of_trajectories)
if direction == 'backward':
traj_iterator = range(number_of_trajectories-1, -1, -1)
animation_paused = False
for r in traj_iterator:
# break animation loop if not InAnimation (this is where pause animation takes place):
EA = FreeCAD.ActiveDocument.ExplodedAssembly
if not(EA.InAnimation):
animation_paused = True
break
if direction == 'forward':
traj = EAFolder[r+start]
# set current stop point
EA.CurrentTrajectory = r+start
elif direction == 'backward':
traj = EAFolder[r]
# set current stop point
EA.CurrentTrajectory = r-1
# highligh current trajectory
FreeCAD.Gui.Selection.addSelection(traj)
# If trajectory is a bolt group or simple group:
if traj.Name[0:11] == 'SimpleGroup' or traj.Name[0:9] == 'BoltGroup':
# buffer objects
objects = []
for name in traj.names:
objects.append(FreeCAD.ActiveDocument.getObject(name))
inc_D = traj.Distance / float(traj.AnimationSteps)
inc_R = traj.Revolutions / float(traj.AnimationSteps)
if direction == 'backward':
inc_D = inc_D*-1.0
inc_R = inc_R*-1.0
for i in range(traj.AnimationSteps):
if i == 0:
dir_vectors = []
rot_vectors = []
rot_centers = []
for s in range(len(objects)):
dir_vectors.append(FreeCAD.Vector(tuple(traj.dir_vectors[s])))
rot_vectors.append(FreeCAD.Vector(tuple(traj.rot_vectors[s])))
rot_centers.append(FreeCAD.Vector(tuple(traj.rot_centers[s])))
for n in range(len(objects)):
obj = objects[n]
obj_base = dir_vectors[n]*inc_D
obj_rot = FreeCAD.Rotation(rot_vectors[n], inc_R*360.0)
obj_rot_center = rot_centers[n]
incremental_placement = FreeCAD.Placement(obj_base, obj_rot, obj_rot_center)
obj.Placement = incremental_placement.multiply(obj.Placement)
# Inclusion to connects with Movie Camera module
if MC_CONNECTION == True:
import MovieConnection as co
co.connectionEA()
# End of inclusion
FreeCAD.Gui.updateGui()
time.sleep(traj.AnimationStepTime)
else:
# if traj is a WireGroup object
steps = traj.AnimationStepsEdge
edges = traj.Shape.Edges
for edge in edges:
points = edge.discretize(steps)
vectors = []
for i in range(len(points)-1):
pa = points[i]
pb = points[i+1]
v = (pb[0]+pa[0], pb[1]+pa[1], pb[2]+pa[2])
vectors.append(v)
if direction == 'backward':
vectors.reverse()
for v in vectors:
for i in range((len(traj.names))):
obj = FreeCAD.ActiveDocument.getObject(traj.names[i])
# incremental placement
obj.Placement.x = obj.Placement.x + v[0]
obj.Placement.y = obj.Placement.y + v[1]
obj.Placement.z = obj.Placement.z + v[2]
FreeCAD.Gui.udpateGui()
time.sleep(traj.AnimationStepTime)
# clear selection
FreeCAD.Gui.Selection.clearSelection()
# set pointer to current trajectory index
EA = FreeCAD.ActiveDocument.ExplodedAssembly
# toggle InAnimation to activate icons deactivated before
EA.InAnimation = False
# set CurrentTrajectory number
if not(animation_paused):
if direction == 'forward' and end == 0:
EA.CurrentTrajectory = -1
if direction == 'backward' and start == 0:
EA.CurrentTrajectory = 0
def goToEnd():
# start animation
EAFolder = FreeCAD.ActiveDocument.ExplodedAssembly.Group
for traj in EAFolder:
objects = []
for name in traj.names:
objects.append(FreeCAD.ActiveDocument.getObject(name))
inc_D = traj.Distance / float(1)
inc_R = traj.Revolutions / float(1)
for i in range(1):
if i == 0:
dir_vectors = []
rot_vectors = []
rot_centers = []
for s in range(len(objects)):
dir_vectors.append(FreeCAD.Vector(tuple(traj.dir_vectors[s])))
rot_vectors.append(FreeCAD.Vector(tuple(traj.rot_vectors[s])))
rot_centers.append(FreeCAD.Vector(tuple(traj.rot_centers[s])))
for n in range(len(objects)):
obj = objects[n]
obj_base = dir_vectors[n]*inc_D
obj_rot = FreeCAD.Rotation(rot_vectors[n], inc_R*360)
obj_rot_center = rot_centers[n]
incremental_placement = FreeCAD.Placement(obj_base, obj_rot, obj_rot_center)
obj.Placement = incremental_placement.multiply(obj.Placement)
FreeCAD.Gui.updateGui()
def goToSelectedTrajectory():
# retrieve selection
traj_name = FreeCAD.Gui.Selection.getSelectionEx()[0].Object.Name
# start animation
EAFolder = FreeCAD.ActiveDocument.ExplodedAssembly.Group
for r in range(len(EAFolder)):
traj = EAFolder[r]
objects = []
update_gui = False
for name in traj.names:
objects.append(FreeCAD.ActiveDocument.getObject(name))
if traj_name == traj.Name:
inc_D = traj.Distance / float(traj.AnimationSteps)
inc_R = traj.Revolutions / float(traj.AnimationSteps)
animation_range = traj.AnimationSteps
update_gui = True
else:
inc_D = traj.Distance / float(1)
inc_R = traj.Revolutions / float(1)
animation_range = 1
for i in range(animation_range):
if i == 0:
dir_vectors = []
rot_vectors = []
rot_centers = []
for s in range(len(objects)):
dir_vectors.append(FreeCAD.Vector(tuple(traj.dir_vectors[s])))
rot_vectors.append(FreeCAD.Vector(tuple(traj.rot_vectors[s])))
rot_centers.append(FreeCAD.Vector(tuple(traj.rot_centers[s])))
for n in range(len(objects)):
obj = objects[n]
obj_base = dir_vectors[n]*inc_D
obj_rot = FreeCAD.Rotation(rot_vectors[n], inc_R*360)
obj_rot_center = rot_centers[n]
incremental_placement = FreeCAD.Placement(obj_base, obj_rot, obj_rot_center)
obj.Placement = incremental_placement.multiply(obj.Placement)
if update_gui:
FreeCAD.Gui.updateGui()
if traj_name == traj.Name:
# exit once selected trajectory has been reached
break
# set current trajectory cursor to current trajectory
FreeCAD.ActiveDocument.ExplodedAssembly.CurrentTrajectory = r
def placeBeforeSelectedTrajectory():
# select the trajectoreis you want to reallocate and finally,
# the trajectory before wich you want to place them
sel_traj = FreeCAD.Gui.Selection.getSelectionEx()
# retrieve EA folder
EAFolder = FreeCAD.ActiveDocument.ExplodedAssembly
# create an auxiliary folder to re organizate exploded assembly
aux_folder = FreeCAD.ActiveDocument.addObject('App::DocumentObjectGroup','ea_aux')
before_traj = sel_traj[-1].Object
for traj in EAFolder.Group:
if traj.Name == before_traj.Name:
for sel in sel_traj:
aux_folder.addObject(sel.Object)
elif traj.Name != before_traj.Name:
add_traj = True
for sel in sel_traj:
if traj.Name == sel.Object.Name:
add_traj = False
break
if add_traj:
aux_folder.addObject(traj)
# save the attributes of EAFolder'
EAF_InitialPlacements = EAFolder.InitialPlacements
EAF_CurrentTrajectory = EAFolder.CurrentTrajectory
EAF_ResetAnimation = EAFolder.ResetAnimation
EAF_InAnimation = EAFolder.InAnimation
EAF_RemoveAllTrajectories = EAFolder.RemoveAllTrajectories
FreeCAD.ActiveDocument.removeObject(EAFolder.Name)
# create the EAFolder
EAFolder = FreeCAD.ActiveDocument.addObject('App::DocumentObjectGroupPython', 'ExplodedAssembly')
ExplodedAssemblyFolder(EAFolder)
ExplodedAssemblyFolderViewProvider(EAFolder.ViewObject)
# restore its content
for traj in aux_folder.Group:
EAFolder.addObject(traj)
EAFolder.InitialPlacements = EAF_InitialPlacements
EAFolder.CurrentTrajectory = EAF_CurrentTrajectory
EAFolder.ResetAnimation = EAF_ResetAnimation
EAFolder.InAnimation = EAF_InAnimation
EAFolder.RemoveAllTrajectories = EAF_RemoveAllTrajectories
# remove auxiliary folder
FreeCAD.ActiveDocument.removeObject(aux_folder.Name)
# recompute document
FreeCAD.ActiveDocument.recompute()
FreeCAD.Gui.updateGui()
def modifyIndividualObjectTrajectory():
# multi direction for simple trajectory(at the moment)
# selection: trajectory_obj + shape + normal direction
# selection: trajectory_obj + normal_direction(selected over the shape)
sel_traj = FreeCAD.Gui.Selection.getSelectionEx()[0].Object
sel_objects = FreeCAD.Gui.Selection.getSelectionEx()[1:]
if len(sel_objects) == 1:
obj_name = sel_objects[0].Object.Name
sel_face = sel_objects[0].SubObjects[0]
else:
obj_name = sel_objects[0].Object.Name
sel_face = sel_objects[1].SubObjects[0]
for i in range(len(sel_traj.names)):
name = sel_traj.names[i]
if obj_name == name:
# if selected trajectory is a simple trajectory:
if sel_traj.Name[0:11] == 'SimpleGroup':
# asign new explosion direction to sel normal
v = sel_face.normalAt(0,0)
sel_traj.dir_vectors[i] = (v[0], v[1], v[2])
break
# if selected trajectory is a bolt group
elif sel_traj.Name[0:9] == 'BoltGroup':
# assign new exploded direction, rotation centre and vector
v = sel_face.normalAt(0,0)
c = sel_face.CenterOfMass
sel_traj.dir_vectors[i] = (v[0], v[1], v[2])
sel_traj.rot_vectors[i] = (v[0], v[1], v[2])
sel_traj.rot_centers[i] = (c[0], c[1], c[2])
break
def updateTrajectoryLines():
EAFolder = FreeCAD.ActiveDocument.ExplodedAssembly.Group
# remove all the previous trajectory lines
for traj in EAFolder:
for lines in traj.Group:
FreeCAD.ActiveDocument.removeObject(lines.Name)
# re-draw all trajectories
for traj in EAFolder:
lines_compound = []
objects = []
for name in traj.names:
objects.append(FreeCAD.ActiveDocument.getObject(name))
inc_D = traj.Distance
dir_vectors = []
rot_centers = []
for s in range(len(objects)):
dir_vectors.append(FreeCAD.Vector(tuple(traj.dir_vectors[s])))
rot_centers.append(FreeCAD.Vector(tuple(traj.rot_centers[s])))
for n in range(len(objects)):
pa = rot_centers[n]# objects[n].Placement.Base
pb = rot_centers[n] + dir_vectors[n]*inc_D
lines_compound.append(Part.makeLine(pa, pb))
l_obj = FreeCAD.ActiveDocument.addObject('Part::Feature','trajectory_line')
l_obj.Shape = Part.makeCompound(lines_compound)
l_obj.ViewObject.DrawStyle = "Dashed"
l_obj.ViewObject.LineWidth = 1.0
traj.addObject(l_obj)
FreeCAD.Gui.updateGui()
def visibilityTrajectoryLines(show):
# show or hide trajectory lines
EAFolder = FreeCAD.ActiveDocument.ExplodedAssembly.Group
for traj in EAFolder:
for lines in traj.Group:
lines.ViewObject.Visibility = show