Hi,
I've been experimenting on adding/removing EntitiesObservers when groups/components are opened/closed, and I've found difference in behavior in old and new Sketchup versions.
When I have few nested groups and I open/close them multiple times (without leaving top parent group):
Sketchup 8 and Sketchup 2013 - observers are added/removed as expected and each new entity is detected only once
Sketchup 2014 and Sketchup 2015 - it seems like observers aren't removed each time 'onActivePathChanged' is called (although in console is printed that they are removed). When I add new face inside some nested group it is detected multiple times. Number of times EntitiesObserver is called is increased each time I open/close some subgroup.
Below it the code which is independent from my plugin and can be simply loaded in Sketchup for testing purposes:
module DL;;Daylighting
#Attach observers to model, entities and materials
class DLAppObserver < Sketchup;;AppObserver
def initializeObservers(model)
model_observer = DL;;Daylighting;;DLModelObserver.new()
model_observer.initializeObservers(model)
model.add_observer(model_observer)
end
def onNewModel(model)
initializeObservers(model)
end
def onOpenModel(model)
initializeObservers(model)
end
end
class DLModelObserver < Sketchup;;ModelObserver
@@observers = {}
@@entitiesObserver = nil
def initializeObservers(model)
@@entitiesObserver = DL;;Daylighting;;DLEntitiesObserver.new()
model.entities.add_observer(@@entitiesObserver)
end
#When new group is opened remove old observers and add new observer for active_entities
def onActivePathChanged(model)
puts "active path changed"
#remove all previous observers
@@observers.each_pair{|entities,observer|
begin
puts "***entities #{entities} remove observer #{observer}; #{entities.remove_observer(observer)}"
rescue Exception => e
puts "Can't remove observer",e.message
end
}
@@observers={}
#if all groups are closed -> don't add observers
if !Sketchup.active_model.active_path
return
end
entitiesObserver = DL;;Daylighting;;DLEntitiesObserver.new()
entities = Sketchup.active_model.active_entities
puts "***entities #{entities} add observer #{entitiesObserver}"
if entities.add_observer(entitiesObserver)
@@observers[entities] = entitiesObserver
end
end
end
class DLEntitiesObserver < Sketchup;;EntitiesObserver
def onElementAdded( ents, new_ent )
#detect only Sketchup;;Face entities
if new_ent.class == Sketchup;;Face
puts "onElementAdded #{ents}; #{new_ent}"
end
end
end
end #DL;;Daylighting
To attach these observers to the application just in call in Ruby console
app_observer = DL;;Daylighting;;DLAppObserver.new()
app_observer.initializeObservers(Sketchup.active_model)
Sketchup.add_observer(app_observer)
I don't understand what I'm doing wrong, because printed messages in ruby console show that old observer is always removed from entities when Active path is changed, but still new faces are detected multiple times.
Once I leave all groups and go back to model (active_path=nil), all observers are like reset and I have only one EntitiesObserver.
When I go inside groups and subgroups again, the number of observers increases again.
And to repeat, in Sketchup 8 and Sketchup 2013 - this problem doesn't exist.
Any suggestion is welcome, because I'm moving in circles with this observers.
Thanks in advance,
Marija