-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path10.3and4.MotifList.py
327 lines (265 loc) · 11 KB
/
10.3and4.MotifList.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
#!/usr/bin/env python
###
# Copyright (c) 2002-2007 Systems in Motion
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
###
# This is an example from The Inventor Mentor
# chapter 10, example 3.
#
# The scene graph has 4 objects which may be
# selected by picking with the left mouse button
# (use shift key to extend the selection to more
# than one object).
#
# Hitting the up arrow key will increase the size of
# each selected object; hitting down arrow will decrease
# the size of each selected object.
#
# This also demonstrates selecting objects from a Motif
# list, and calling select/deselect functions on the
# coin.SoSelection node to change the selection. Use the Shift
# key to extend the selection (i.e. pick more than one
# item in the motif list.)
#
from __future__ import print_function
import os
import sys
import FreeCAD as App
import FreeCADGui as Gui
import pivy.coin as coin
#WARGNING: THIS CODE WILL NOT WORK AS THE CALLBACK IS DISABLED : TODO: FIXME:
# Global data
motifList = None
cubeTransform, sphereTransform, coneTransform, cylTransform = None, None, None, None
###############################################################
# CODE FOR The Inventor Mentor STARTS HERE
CUBE, SPHERE, CONE, CYL, NUM_OBJECTS = 0, 1, 2, 3, 4
objectNames= (
"Cube",
"Sphere",
"Cone",
"Cylinder"
)
# CODE FOR The Inventor Mentor ENDS HERE
###############################################################
#WARNING: This example will not work as it is based on Linux OS. TODO: FIXME: Mariwan
# # Create the object list widget
# def createList(display, selection):
# global motifList
# args = [None, None, None, None]
# # Create a new shell window for the list
# n = 0
# XtSetArg(args[n], XmNtitle, "Selection")
# n += 1
# shell = XtAppCreateShell("example", "Inventor",
# topLevelShellWidgetClass, display, args, n)
# ###############################################################
# # CODE FOR The Inventor Mentor STARTS HERE (part 3)
# # Create a table of object names
# table = XmString[NUM_OBJECTS]
# for i in range(NUM_OBJECTS):
# table[i] = XmStringCreate(objectNames[i],
# XmSTRING_DEFAULT_CHARSET)
# # Create the list widget
# n = 0
# XtSetArg(args[n], XmNitems, table)
# n += 1
# XtSetArg(args[n], XmNitemCount, NUM_OBJECTS)
# n += 1
# XtSetArg(args[n], XmNselectionPolicy, XmEXTENDED_SELECT)
# n += 1
# motifList = XmCreateScrolledList(shell, "funcList", args, n)
# XtAddCallback(motifList, XmNextendedSelectionCallback,
# myListPickCB, selection)
# # CODE FOR The Inventor Mentor ENDS HERE
# ###############################################################
# # Free the name table
# for i in range(NUM_OBJECTS):
# XmStringFree(table[i])
# del table
# # Manage the list and return the shell
# XtManageChild(motifList)
# return shell
# This callback is invoked every time the user picks
# an item in the Motif list.
def myListPickCB(Widget, selection, listData):
mySearchAction = SoSearchAction()
# Remove the selection callbacks so that we don't get
# called back while we are updating the selection list
selection.removeSelectionCallback(mySelectionCB, True)
selection.removeDeselectionCallback(mySelectionCB, False)
###############################################################
# CODE FOR The Inventor Mentor STARTS HERE (part 4)
# Clear the selection node, then loop through the list
# and reselect
selection.deselectAll()
# Update the coin.SoSelection based on what is selected in
# the motif list. We do this by extracting the string
# from the selected XmString, and searching for the
# object of that name.
for i in range(listData.selected_item_count):
mySearchAction.setName(SoGui.decodeString(listData.selected_items[i]))
mySearchAction.apply(selection)
selection.select(mySearchAction.getPath())
# CODE FOR The Inventor Mentor ENDS HERE
###############################################################
# Add the selection callbacks again
selection.addSelectionCallback(mySelectionCB, True)
selection.addDeselectionCallback(mySelectionCB, False)
# This is called whenever an object is selected or deselected.
# if userData is True, then it's a selection else deselection.
# (we set this convention up when we registered this callback).
# The function updates the Motif list to reflect the current
# selection.
def mySelectionCB(isSelection, selectionPath):
global motifList
args = [None]
# We have to temporarily change the selection policy to
# MULTIPLE so that we can select and deselect single items.
XtSetArg(args[0], XmNselectionPolicy, XmMULTIPLE_SELECT)
XtSetValues(motifList, args, 1)
node = selectionPath.getTail()
for i in range(NUM_OBJECTS):
if node.getName() == objectNames[i]:
if isSelection:
XmListSelectPos(motifList, i+1, False)
else: XmListDeselectPos(motifList, i+1)
XmUpdateDisplay(motifList)
break
# Restore the selection policy to extended.
XtSetArg(args[0], XmNselectionPolicy, XmEXTENDED_SELECT)
XtSetValues(motifList, args, 1)
###############################################################
# CODE FOR The Inventor Mentor STARTS HERE (Example 10-4)
# Scale each object in the selection list
def myScaleSelection(selection, sf):
global cubeTransform, sphereTransform, coneTransform, cylTransform
# Scale each object in the selection list
for i in range(selection.getNumSelected()):
selectedPath = selection.getPath(i)
xform = None
# Look for the shape node, starting from the tail of the
# path. Once we know the type of shape, we know which
# transform to modify
for j in range(selectedPath.getLength()):
if xform != None: break
n = selectedPath.getNodeFromTail(j)
if n.isOfType(coin.SoCube.getClassTypeId()):
xform = cubeTransform
elif n.isOfType(coin.SoCone.getClassTypeId()):
xform = coneTransform
elif n.isOfType(coin.SoSphere.getClassTypeId()):
xform = sphereTransform
elif n.isOfType(coin.SoCylinder.getClassTypeId()):
xform = cylTransform
# Apply the scale
scaleFactor = xform.scaleFactor.getValue()
scaleFactor *= sf
xform.scaleFactor = scaleFactor
# If the event is down arrow, then scale down every object
# in the selection list if the event is up arrow, scale up.
# The userData is the selectionRoot from main().
def myKeyPressCB(selection, eventCB):
event = eventCB.getEvent()
# check for the Up and Down arrow keys being pressed
if coin.SoKeyboardEvent.isKeyPressEvent(event, coin.SoKeyboardEvent.UP_ARROW):
myScaleSelection(selection, 1.1)
eventCB.setHandled()
elif coin.SoKeyboardEvent.isKeyPressEvent(event, coin.SoKeyboardEvent.DOWN_ARROW):
myScaleSelection(selection, 1.0/1.1)
eventCB.setHandled()
# CODE FOR The Inventor Mentor ENDS HERE
###############################################################
def MotifListExec():
global cubeTransform, sphereTransform, coneTransform, cylTransform
# Print out usage message
print("Left mouse button - selects object")
print("<shift>Left mouse button - selects multiple objects")
print("Up and Down arrows - scale selected objects")
# Create and set up the selection node
selectionRoot = coin.SoSelection()
selectionRoot.policy = coin.SoSelection.SHIFT
selectionRoot.addSelectionCallback(mySelectionCB, True)
selectionRoot.addDeselectionCallback(mySelectionCB, False)
# Add a camera and some light
myCamera = coin.SoPerspectiveCamera()
selectionRoot.addChild(myCamera)
selectionRoot.addChild(coin.SoDirectionalLight())
# Add an event callback so we can receive key press events
myEventCB = coin.SoEventCallback()
myEventCB.addEventCallback(coin.SoKeyboardEvent.getClassTypeId(),
myKeyPressCB, selectionRoot)
selectionRoot.addChild(myEventCB)
# Add some geometry to the scene
# a red cube
cubeRoot = coin.SoSeparator()
cubeMaterial = coin.SoMaterial()
cubeTransform = coin.SoTransform()
cube = coin.SoCube()
cubeRoot.addChild(cubeTransform)
cubeRoot.addChild(cubeMaterial)
cubeRoot.addChild(cube)
cubeTransform.translation = (-2, 2, 0)
cubeMaterial.diffuseColor = (.8, 0, 0)
selectionRoot.addChild(cubeRoot)
# a blue sphere
sphereRoot = coin.SoSeparator()
sphereMaterial = coin.SoMaterial()
sphereTransform = coin.SoTransform()
sphere = coin.SoSphere()
sphereRoot.addChild(sphereTransform)
sphereRoot.addChild(sphereMaterial)
sphereRoot.addChild(sphere)
sphereTransform.translation = (2, 2, 0)
sphereMaterial.diffuseColor = (0, 0, .8)
selectionRoot.addChild(sphereRoot)
# a green cone
coneRoot = coin.SoSeparator()
coneMaterial = coin.SoMaterial()
coneTransform = coin.SoTransform()
cone = coin.SoCone()
coneRoot.addChild(coneTransform)
coneRoot.addChild(coneMaterial)
coneRoot.addChild(cone)
coneTransform.translation = (2, -2, 0)
coneMaterial.diffuseColor = (0, .8, 0)
selectionRoot.addChild(coneRoot)
# a magenta cylinder
cylRoot = coin.SoSeparator()
cylMaterial = coin.SoMaterial()
cylTransform = coin.SoTransform()
cyl = coin.SoCylinder()
cylRoot.addChild(cylTransform)
cylRoot.addChild(cylMaterial)
cylRoot.addChild(cyl)
cylTransform.translation = (-2, -2, 0)
cylMaterial.diffuseColor = (.8, 0, .8)
selectionRoot.addChild(cylRoot)
###############################################################
# CODE FOR The Inventor Mentor STARTS HERE (part 2)
cube.setName(objectNames[CUBE])
sphere.setName(objectNames[SPHERE])
cone.setName(objectNames[CONE])
cyl.setName(objectNames[CYL])
# CODE FOR The Inventor Mentor ENDS HERE
###############################################################
# Create a render area for viewing the scene
view = Gui.ActiveDocument.ActiveView
sg = view.getSceneGraph()
sg.addChild(cylRoot)
sg.addChild(coneRoot)
sg.addChild(cubeRoot)
sg.addChild(sphereRoot)
sg.addChild(selectionRoot)