When testing code for Observers, I was curious about what exactly was being passed to the Observer event methods. I would normally trust the documentation, but in this case... So anyway, here's a little trick. If you prefix an asterisk (*) to the variable name in the method def, every argument passed to the method gets put in the variable as an array element.
The ToolObsever methods below normally accept 3 parameters, according to the API docs;
- A Tools object
- The tool name
- The toolid.
In the code below, the args variable is prefixed with an asterisk, which make it into an array and each argument passed to the method are added to the array. This makes an easy way to inspect all of the arguments.
class MyToolsObserver < Sketchup;;ToolsObserver
def onActiveToolChanged(*args)
print "onActiveToolChanged; "
p args
end
def onToolStateChanged(*args)
print "onToolStateChanged; "
p args
end
end
Sketchup.active_model.tools.add_observer(MyToolsObserver.new)
And a look at some output:
onToolStateChanged; [#<Sketchup;;Tools;0xc681940>, "CircleTool", 21096, 0]
onToolStateChanged; [#<Sketchup;;Tools;0xc681940>, "CircleTool", 21096, 0]
onActiveToolChanged; [#<Sketchup;;Tools;0xc681940>, "CircleTool", 21096]
onActiveToolChanged; [#<Sketchup;;Tools;0xc681940>, "CircleTool", 21096]
onToolStateChanged; [#<Sketchup;;Tools;0xc681940>, "RectangleTool", 21094, 0]
onToolStateChanged; [#<Sketchup;;Tools;0xc681940>, "RectangleTool", 21094, 0]
onActiveToolChanged; [#<Sketchup;;Tools;0xc681940>, "RectangleTool", 21094]
onActiveToolChanged; [#<Sketchup;;Tools;0xc681940>, "RectangleTool", 21094]
onActiveToolChanged; [#<Sketchup;;Tools;0xc681940>, "CameraOrbitTool", 10508]
onActiveToolChanged; [#<Sketchup;;Tools;0xc681940>, "CameraOrbitTool", 10508]
onActiveToolChanged; [#<Sketchup;;Tools;0xc681940>, "RectangleTool", 21094]
onActiveToolChanged; [#<Sketchup;;Tools;0xc681940>, "RectangleTool", 21094]
onActiveToolChanged; [#<Sketchup;;Tools;0xc681940>, "SelectionTool", 21022]
onActiveToolChanged; [#<Sketchup;;Tools;0xc681940>, "SelectionTool", 21022]
So the output is mostly right according to the docs. In some cases, there is a fourth argument passed to the methods. I can only guess what it is - perhaps a flag, or indicator of the tool state.