-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
BimSelect.py
103 lines (83 loc) · 3.47 KB
/
BimSelect.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
import FreeCAD
class CyclicSelectionObserver:
def addSelection(self, document, object, element, position):
import FreeCADGui
if not FreeCAD.ActiveDocument:
return
if not hasattr(FreeCAD, "CyclicSelectionObserver"):
return
FreeCADGui.Selection.removeSelection(FreeCAD.ActiveDocument.getObject(object))
FreeCADGui.Selection.removeObserver(FreeCAD.CyclicSelectionObserver)
del FreeCAD.CyclicSelectionObserver
preselection = FreeCADGui.Selection.getPreselection()
FreeCADGui.Selection.addSelection(
FreeCAD.ActiveDocument.getObject(preselection.Object.Name),
preselection.SubElementNames[0],
)
FreeCAD.ActiveDocument.recompute()
class CyclicObjectSelector:
def __init__(self):
self.selectableObjects = []
self.objectIndex = 0
def selectObject(self, event_callback):
import FreeCADGui
from pivy import coin
if not FreeCAD.ActiveDocument:
return
event = event_callback.getEvent()
if (
event.getState() != coin.SoMouseButtonEvent.DOWN
or not self.selectableObjects
):
return
pos = event.getPosition().getValue()
element_list = FreeCADGui.ActiveDocument.ActiveView.getObjectsInfo(
(int(pos[0]), int(pos[1]))
)
if not element_list:
self.selectableObjects = []
if hasattr(FreeCAD, "CyclicSelectionObserver"):
FreeCADGui.Selection.removeObserver(FreeCAD.CyclicSelectionObserver)
del FreeCAD.CyclicSelectionObserver
return
FreeCAD.CyclicSelectionObserver = CyclicSelectionObserver()
FreeCADGui.Selection.addObserver(FreeCAD.CyclicSelectionObserver)
def cycleSelectableObjects(self, event_callback):
import FreeCADGui
if not FreeCAD.ActiveDocument:
return
event = event_callback.getEvent()
if not event.isKeyPressEvent(event, event.TAB):
return
pos = event.getPosition().getValue()
selectableObjects = FreeCADGui.ActiveDocument.ActiveView.getObjectsInfo(
(int(pos[0]), int(pos[1]))
)
if not selectableObjects:
return
if self.selectableObjects != selectableObjects:
self.selectableObjects = selectableObjects
self.objectIndex = 0
elif self.objectIndex < len(self.selectableObjects) - 1:
self.objectIndex += 1
else:
self.objectIndex = 0
object_name = self.selectableObjects[self.objectIndex]["Object"]
subelement_name = self.selectableObjects[self.objectIndex]["Component"]
FreeCADGui.getMainWindow().showMessage(
"Cycle preselected (TAB): {} - {}".format(object_name, subelement_name), 0
)
FreeCADGui.Selection.setPreselection(
FreeCAD.ActiveDocument.getObject(object_name), subelement_name
)
class Setup:
def slotActivateDocument(self, doc):
from pivy import coin
cos = CyclicObjectSelector()
if doc and doc.ActiveView and hasattr(doc.ActiveView, "getSceneGraph"):
self.callback = doc.ActiveView.addEventCallbackPivy(
coin.SoMouseButtonEvent.getClassTypeId(), cos.selectObject
)
self.callback = doc.ActiveView.addEventCallbackPivy(
coin.SoKeyboardEvent.getClassTypeId(), cos.cycleSelectableObjects
)