-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFPSimButton.py
156 lines (137 loc) · 7.48 KB
/
FPSimButton.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
import FreeCAD
import FPEventDispatcher
import FPUtils
from FPInitialPlacement import InitialPlacements
import FPSimServer
import generated.python.FPSimulation_pb2 as Proto
buttonState = dict()
def has5dButtonTraits(buttonObj):
return hasattr(buttonObj, 'PressureResolutionLin') and \
(buttonObj.PressureResolutionLin.x != 0 or buttonObj.PressureResolutionLin.y != 0)
def has3dButtonTraits(buttonObj):
return (hasattr(buttonObj, 'PressureResolutionLin') and (buttonObj.PressureResolutionLin.z != 0)) or \
(hasattr(buttonObj, 'VelocityResolution') and (buttonObj.VelocityResolution != 0))
class FPSimButton(InitialPlacements):
def __init__(self, obj):
InitialPlacements.__init__(self, obj)
buttonState[obj.Name] = Proto.BUTTON_RELEASED
obj.addProperty('App::PropertyVector', 'RotationAxis').RotationAxis = (0,0,0)
obj.addProperty('App::PropertyFloat', 'RotationAngle').RotationAngle = 0
obj.addProperty('App::PropertyVector', 'RotationCenter').RotationCenter = (0,0,0)
obj.addProperty('App::PropertyVector', 'Translation').Translation = (0,0,0)
obj.addProperty('App::PropertyBool', 'SwitchMode').SwitchMode = False
obj.addProperty('App::PropertyVector', 'PressureResolutionLin').PressureResolutionLin = (0,0,0)
obj.addProperty('App::PropertyVector', 'PressureResolutionAng').PressureResolutionAng = (0,0,0)
obj.addProperty('App::PropertyInteger', 'VelocityResolution').VelocityResolution = 0
obj.Proxy = self
def _registerEventCallbacks(self, objName):
FPEventDispatcher.eventDispatcher.registerForButtonEvent(objName, self.onButtonEvent)
FPEventDispatcher.eventDispatcher.registerForHoverKeyPress(objName, FPEventDispatcher.FPEventDispatcher.CTRL_KEYCODE,self.onKeyEvent)
def _unregisterEventCallbacks(self, objName):
FPEventDispatcher.eventDispatcher.unregisterForButtonEvent(objName)
FPEventDispatcher.eventDispatcher.unregisterHoverKeyPress(objName, FPEventDispatcher.FPEventDispatcher.CTRL_KEYCODE)
def onChanged(self, obj, prop):
#FreeCAD.Console.PrintMessage("in onChanged obj.Name: " + str(obj.Name) + " obj.Label: " + str(obj.Label) + " prop: " + str(prop) + "\n")
if prop == 'Proxy':
# Called at loading existing object on first place(Placement is not valid yet )
# Called at creation on first place(ToCheck: I think Placement is not valid here yet)
self._registerEventCallbacks(obj.Name)
elif prop == 'Group':
# Always called when the group changes(new group member inserted or removed)
# or gets created :
# - called after 'proxy'-cb
# - Placement is valid
# - strange thing is at this point there is no child object inside
if not obj.Group:
# Called when Removing all objects from group or when group-obj gets deleted
#FreeCAD.Console.PrintMessage(str(obj.Label) + " Obj has no Group attribute\n")
self._unregisterEventCallbacks(obj.Name)
elif self.hasNoChilds(obj):
# Called at object creation
#FreeCAD.Console.PrintMessage(str(obj.Label) + " Obj has Group attribute but no childs\n")
self._registerEventCallbacks(obj.Name)
#FreeCAD.Console.PrintMessage("Group cb: Saving initial Placement\n")
self.saveInitialPlacements(obj) # needed at creation
elif prop == 'ExpressionEngine':
#FreeCAD.Console.PrintMessage("ExpressionEngine\n")
# Called at loading existing object at last cb(Placement is valid now)
try:
#FreeCAD.Console.PrintMessage("Move to initial placement\n")
self.moveToInitialPlacement(obj)
except KeyError:
#FreeCAD.Console.PrintMessage("Key Error exception\n")
self.saveInitialPlacements(obj)
def execute(self, fp):
# moving objects in group result in calling this, so reset initial placements to new pos
pass
def onButtonEvent(self, objName, state, pos):
obj = FreeCAD.ActiveDocument.getObject(objName)
arrayRootObj = FPUtils.arrayRootObject(obj)
if(has5dButtonTraits(arrayRootObj)):
FPSimServer.dataAquisitionCBHolder.setButton5dCB(objName, self.getState)
elif(has3dButtonTraits(arrayRootObj)):
FPSimServer.dataAquisitionCBHolder.setButton3dCB(objName, self.getState)
else:
FPSimServer.dataAquisitionCBHolder.setButtonCB(objName, self.getState)
rot = FreeCAD.Rotation(obj.RotationAxis, obj.RotationAngle)
if state == FPEventDispatcher.FPEventDispatcher.PRESSED:
if not obj.SwitchMode:
buttonState[objName] = Proto.BUTTON_PRESSED
else:
if buttonState[objName] == Proto.BUTTON_PRESSED:
buttonState[objName] = Proto.BUTTON_RELEASED
elif buttonState[objName] == Proto.BUTTON_RELEASED:
buttonState[objName] = Proto.BUTTON_PRESSED
else:
if not obj.SwitchMode:
buttonState[objName] = Proto.BUTTON_RELEASED
else:
return
for child in FPUtils.getChildsWithPlacement(obj):
if buttonState[objName] == Proto.BUTTON_PRESSED:
pos = child.Placement.Base + obj.Translation
rotPlacement = FreeCAD.Placement(pos, rot, obj.RotationCenter - child.Placement.Base)
newRot = rotPlacement.Rotation.multiply( child.Placement.Rotation )
newBase = rotPlacement.Base
child.Placement.Base = newBase
child.Placement.Rotation = newRot
elif buttonState[objName] == Proto.BUTTON_RELEASED:
child.Placement = self.getInitialPlacement(obj, child.Name)
def onKeyEvent(self, objName, keyCode, state):
self.onButtonEvent(objName, state, "")
def getState(self, objName):
return buttonState[objName]
class FPSimButtonViewProvider:
def __init__(self, obj):
obj.Proxy = self
def getIcon(self):
import FPSimDir
return FPSimDir.__dir__ + '/icons/Button.svg'
def createFPSimRotButton():
buttonObj = FreeCAD.ActiveDocument.addObject('App::DocumentObjectGroupPython', 'FPSimButton')
FPSimButton(buttonObj)
FPSimButtonViewProvider(buttonObj.ViewObject)
selection = FreeCAD.Gui.Selection.getSelectionEx()
try:
buttonObj.RotationAxis = selection[-1].SubObjects[-1].normalAt(0,0)
buttonObj.RotationCenter = selection[-1].SubObjects[-1].CenterOfMass
for sel_obj in selection:
buttonObj.addObject(sel_obj.Object)
except IndexError:
FreeCAD.Console.PrintError("Usage Error, select objects and a rotation surface\n")
def createFPSimLinButton():
buttonObj = FreeCAD.ActiveDocument.addObject('App::DocumentObjectGroupPython', 'FPSimButton')
FPSimButton(buttonObj)
FPSimButtonViewProvider(buttonObj.ViewObject)
selection = FreeCAD.Gui.Selection.getSelectionEx()
try:
buttonObj.Translation = selection[-1].SubObjects[-1].normalAt(0, 0).negative()
for sel_obj in selection:
theObj = sel_obj.Object
if theObj.InList:
for parent in theObj.InList:
buttonObj.addObject(parent)
else:
buttonObj.addObject(theObj)
except IndexError:
FreeCAD.Console.PrintError("Usage Error, select objects and a surface\n")