Help with Observers
-
Hi all,
Anyone knows how to use observers, or have an example code file ?
There's quite NOTHING usable in the help pages(always the same content, no arguments given, no syntax, no return values...), and very few posts about that in the GGroups.
I wonder why Google provides new classes with such a lack of documentation -
Here's some example code:
### class FaceWatch < Sketchup;;EntityObserver def onChangeEntity(entity) UI.messagebox(entity.to_s+"; Face Changed!") end#def def onEraseEntity(entity) UI.messagebox(entity.to_s+"; Face Erased!") end#def end#class pts=[[0,0,0],[100,0,0],[0,100,0]] face=Sketchup.active_model.entities.add_face(pts) face.add_observer(FaceWatch.new) ###
Clearly the messages are only to show what it's doing - you can add other actions instead, such as affecting some other related objects ("attributed" together)... E.G. A window has an observer, you move the window and its associated reveal faces move too: the reveal faces each have an observer and if you move one the others move with it (like it's a group that's not a group !) AND their associated window moves too...
-
Thank TIG for the reply.
One more question : how to associate reveals faces to the component instance, since there is no persistent entityID which can be set as an attribute to the component instance ? -
Also have a look for the Sketchup::AppObserver - it's a good point to start with for understanding how they are working. You write your own class, which inherits from the Sketchup::AppObserver. Sketchup::AppObserver provides some empty methods, which will be called, when
- a new model gets opend
- a model gets loaded
- on program exit
These methods do nothing by default. But in your class you can overwrite (redefine) them. Because objects of your class also are Sketchup::AppObserver, they will be notified on changes. So i.e. a model gets opend, all Sketchup::AppObservers get the message "model opend" (their method for that is called by Sketchup).
You need to register your Sketchup::AppObserver object to Sketchup with the line
Sketchup.add_observer <MY_OBSERVERCLASS>.new # for simple constructors
That's all.
azuby
-
Thanks all for your help, but I have the feeling that what I want to achieve will not be easy...
-
@didier bur said:
Thank TIG for the reply.
One more question : how to associate reveals faces to the component instance, since there is no persistent entityID which can be set as an attribute to the component instance ?You can give the compo'instance an attribute and match this to the reveal's faces attributes, if you move it (observer kicks in)... it looks for any faces (reveals) with matching attributes and moves them too...
Conversely if one of the reveal faces moves it too has attributes linking it back to the other related reveal faces and the compo'instance, by their matching attributes, and it moves them to suit too...
Have an attribute for all related things... say it's called 'Cutter' and give it a random number key '1234567' when they are first connected by the tool.
The window has the attribute 'Cutter' set to '1234567' and when the observer does its stuff the faces in the model are scanned for attribute 'Cutter' and IF its set to '1234567' it's a hit and they are moved to suit. This way all reveal faces and compo'instance are connected across sessions.
You could also have a checksum in the key to check that there are still the original number of faces etc e.g. 4 faces >>> [4,1234567], when moving something IF the face count <4 then remove all of the face and compo'instance attributes so they're disconnected for good... OR try to make the missing face(s) and refix to suit ? - rather like erasing the wall face that the window is in... making a new window won't cut the face unless it's reglue'd (see my script fot this bit)...
The 'Cutter' attribute can also containe the base face linked with an attribute if this helps heal or reglue, move reveals later on etc... -
Aaaah TIG it's good to have your brain beside me...
Thanks (and merry Christmas) !
Advertisement