How to get Model Entities change event once?
-
Hello all,
I'm trying to track if faces located in the model node had been modified (i.e. model.entities collection has been changed). I can track changes via EntitiesObserver but I'm receiving too many calls. It would be just great to get only one event telling that model itself or model.entities had changed. Is there a way to do this?
Regards,
Bro -
Keep yourself a flag variable linked to the model (use the model's object id as a hash key,) and the value would be boolean.
use the boolean value within your observer callbacks as a conditional expression:
@changed = {} def onElementAdded( model ) unless @changed[ model.object_id ] # DO SOMETHING HERE @changed[ model.object_id ]= true end end
-
oddly enough, I don't see a proper observer which corresponds to def onElementAdded(model). Which one should I use? EntitiesObserver? I've noticed that PagesObserved also has a onElementAdded callback, but like EntitiesObserver it throws 2 variables: entities and entity.
-
Sorry wrote that in a hurry... I'll simplify a bit:
module Fuzzy class EntsSpy < Sketchup;;EntitiesObserver attr_writer( ;changed ) # makes a changed=() method def changed?() @changed end def initialize( model ) @changed = false end def onElementAdded( entities, entity ) unless changed? # DO SOMETHING HERE @changed = true end end end # class class AppSpy < Sketchup;;AppObserver def onNewModel( model ) model.entities.add_observer( EntsSpy.new(model) ) end def onOpenModel( model ) model.entities.add_observer( EntsSpy.new(model) ) end end # class AppSpy Sketchup.add_observer( AppSpy.new ) end # Fuzzy namespace
-
Thanks for the detailed answer, but I'm afraid that won't work properly: once a model is opened the @changed flag will be set to 'false' which is good, but when one will change model.entities it will get to the "# DO SOMETHING HERE" section only once. To set @changed to 'true' again I'll have to open/create a new model, but I need to track if this very model is changed per event. I guess it wont be possible to track the creation of new faces/instances in the model.entities that way. Please correct me if I'm wrong.
-
The logic above is an example.
You are free to change the logic, add conditional statements, to reset the
@changed
variable tofalse
ortrue
, depending upon conditions, in whatever callback you need to.I am just trying to give you some ideas, ... not write the plugin for you.
-
BTW... the model has an internal modification flag, that you can access via
model.modified?
This flag can get reset by the user if they undo all operations in a session (on the undo stack.)
-
Thanks, Dan!
Your idea worked out very well using "onTransactionCommit" event of the Model Observer. On every commit event I simply reset the "changed" flag and then do the rest as you proposed.
Advertisement