Observers. Can we use them!
-
Hello guys,
I've been reading some posts about Observers. It seems that some do not work right.
For example SelectionObserver.onSelectionBulkChange, it works in my tests, it identifies when something is selected.
I also realized that he is always working for alerting when I select something. How to disable Selectionobserver?
I tried using the remove, but I think did something wrong. It did not work.I think it is good to use in my script. It is short showed on next steps:
(1) I select a face with the right mouse button;
(2) Choose the action from the context menu and a dialog box, enter data that identify a set of openings (CJAB);
(3) After another dialog window to enter data that create an opening in the wall;
All this works already.However, for each opening of a set of openings is necessary to perform all the steps.
I would enter only once with the name of the set of openings. I think do this with SelectionObserver.onSelectionBulkChange.
I would like to do step 1,2 and 3 only for the first opening of the set.
After that I am asked if I want to make another opening for that same set.
If yes, Selectionobserver starts. It keeps the set name to select other face waiting to perform step 3.
And so is that answer until I do not want to make openings for that set. Then disconnect the Selectionobserver.
What do you think? Will that work?
Tell me if I'm on track or whether I should use another observer.
I've tried to do this but For and While, but there is always the need to monitor user actions to trigger the action of the script.
Thank you. -
Create a new bizelloSelectionObserver [
$bizelloSelectionObserver=bizelloSelectionObserver.new
] by referring to it with a 'global variable' like$bizelloSelectionObserver
- then useselection.add-observer($bizelloSelectionObserver)
to add it - then later on you can remove it usingselection.remove_observer($bizelloSelectionObserver)
[also set that $ variable to nil].
A $ global variable is remembered during the session even when the earlier code that has first started it has closed. To restart the SelectionObserver simply run your other method again toselection.add_observer($bizelloSelectionObserver)
using to the global variable again so you can 'remove' it later etc etc...
Global variables are usually best avoided BUT they are useful for observers that may require accessing outside of the original code that made them - just make sure you use a unique name - i.e. no one else is likely to want to use "$bizelloSelectionObserver" ??? whereas "$ob" would be fatal if anyone else used that tooDo not try to get an observer modifying itself - it will crash.
-
Observers are troublesome. They have different bugs in different SketchUp versions.
In general - only use observers when you really need to. And beware that modifying the model in events might very likely cause crashes.I've been trying to map out the state of observers in SU. If you have additional info or thing you found issues not documented, please post back with a reproducible case.
http://forums.sketchucation.com/viewtopic.php?f=180&t=30793
Advertisement