Open thomthom opened 12 years ago
I am working on a Ruby script that uses SelectionObservers. Everything works fine if I make my selections with the Selection tool. But if I use the Outliner to make selections I get different results when executing from Ruby code in my script verses the same code in the Ruby Console, and the results with my script are wrong. I wonder if someone could look at my code and see if it's a programming issue, or a SketchUp bug as I suspect.
The code is simple. What I am wanting to do is use the Outliner to select components instead of the Select tool. When I select a component(s) or change the selection using the Outliner I want to refresh a display screen. So I am using a SelectionObserver (two actually) to detect selections and selection changes. When a SelectionObserver fires I am using Sketchup.active_model.selection.length to get the number of components selected. This all works correctly if you use the Select tool. But the Outliner gives erratic results. For example when you go from no selection to one component selected (using the Outliner) the SelectionObserver never fires. When you go from one selected component to another, it fires but reports zero selected components (instances). If you use the shift key to select two or more components the results are correct but if you use the Ctrl key to add components the Selection Observer never fires. In all these failing cases if you manually type Sketchup.active_model.selection.length into the Ruby Console it reports the correct results.
Could someone please install the attached script into the Plugins folder and give it a spin. The Ruby Console will open automatically. Try random selections with both the Select tool and the Outliner to see the problem. When the Outliner fails enter Sketchup.active_model.selection.length into the Console and verify the results are correct but differ from the Outliner.
If I am doing something wrong please break it to me gently. I am a sensitive, frail person ;<) File attached. Thanks,
Yep it's sort of a bug. Apparently the outliner did not get the word, when the "Team" deprecated the onSelectionAdded() and onSelectionRemoved() callabacks. (they forgot to change the code for the outliner to call the other 2 "good" callbacks.
Check out the modified test version, attached.
Since these callbacks are likely to not be called in future API editions. I would suggest making them an alias of the other two.
ie:
def onSelectionBulkChange( selection, *extra ) -- code here end alias_method :onSelectionAdded, onSelectionBulkChange
def onSelectionCleared( selection, *extra ) -- code here end alias_method :onSelectionRemoved, onSelectionCleared
That way when the outliner is fixed, you won't need to re-distro an update:
On Wednesday, June 6, 2012 2:47:48 PM UTC-4, Joe Zeh wrote:
Bug report submitted with both my script and Dan's modified script.
On Wednesday, June 6, 2012 10:58:13 AM UTC-4, Dan Rathbun wrote:
### EDIT ###
OK yes there IS a bug. When you unselect an item using CTRL in the Outliner, NONE of the 4 observer callbacks fire.
But any other combination of keys with an unselect, or selecting with CTRL, fires at least one of the callbacks.
UPDATE:
Joe you'll like this.
I drastically improved my Observer Test Utility, and because in the past, I caught the SketchUp C-side calling misnamed callbacks (I think it was the MaterialsObserver,) .. anyway I added in an override for the respond_to?() method, that outputs the callback name, which the C-side is checking for, just before it makes the callback call.
I suspected that a typo-error on the C-side was wishing to call a callback, that had a name noone knew about... and I was correct.
SO.. when you unselect an item using CTRL in the Outliner, the C-side wants to call a callback named "onSelectedRemoved" which is a boo-boo. The REAL (although deprecated,) name is "onSelectionRemoved"
I suggest you also alias the boo-boo names (in the same manner I said above,) to one of the "good" callbacks: (Your choice which, it's your subclass.)
Example: alias_method(:onSelectedRemoved,:onSelectionBulkChange)
So.. you do not really need to wait for the Trimble Team to fix things... you can monkey patch you code.
Cheers!
Original report by me.
https://groups.google.com/forum/?fromgroups#!topic/sketchupruby/OdvLO9Dwhdk