Thanks for the reply. I've updated the code but seeing the same results. I want to be able to remove observers, especially when a new model is opened. It does look like they're removed because the observer methods aren't called, but there is a slowdown each time they are added/removed which I don't get.
-
I did use a subclass of EntityObserver but removed it when creating the test. I've readded a trivial observer.
-
Ok. What causes this? Is it just calling methods on entities, or adding/removing entities? or any mutable operation on the model?
-
I wanted to show that it doesn't matter what entities the observers are added to, the effect is the same.
-
Yes, I meant to create an instance.
-
That sounds better but I can't seem to get removeObservers to work with it. When I tried using a single instance with add_observer/remove_observer (@@observer in the below code), the puts output looked correct, but only a single observer was removed. remove_observer returns false, and I see the observer output when I paint materials.
6, 10) I did this to simplify the example. It's the same when they are added and removed at separate times.
-
Ok.
-
Ok, I already use modules for larger scripts. What memory is used though? I thought each method used a constant amount of memory, no matter how many instances or subclasses there are.
-
Ok.
The results now are:
removeMaterials
(Completes in 0.05)
addObservers
(Observers are added. Painting a surface prints observer output.)
removeObservers
(Observers are removed. remove_observer returns true. Painting a surface gives no output.)
removeMaterials
(Completes in 0.4)
removeMaterials takes more time after calling addObservers/removeObservers again. What is taking the time if the observers aren't active?
module Dmactest
class MyEntityObserver < Sketchup;;EntityObserver
def onEraseEntity(e)
puts "Erase Entity; #{e}"
end
def onChangeEntity(e)
puts "Change Entity; #{e}"
end
end
class << self
@@watchedFaces = []
@@observer = MyEntityObserver.new
def timeOperation
startTime = Time.now
yield
time = Time.now - startTime
puts "Completed in; #{time.to_s}"
end
def addObservers
model = Sketchup.active_model
removeObservers
@@watchedFaces = model.entities.select { |e| e.instance_of? Sketchup;;Face }
@@watchedFaces.map! { |f| [f, MyEntityObserver.new] }
@@watchedFaces.each { |f, o|
# puts "Adding Observer; #{f} #{@@observer}"
# f.add_observer @@observer
puts "Adding Observer; #{f} #{o}"
f.add_observer o
}
return nil
end
def removeObservers
model = Sketchup.active_model
@@watchedFaces.each { |f, o|
# puts "Removing Observer; #{f} #{@@observer}"
# puts "Result; #{f.remove_observer @@observer}"
puts "Removing Observer; #{f} #{o}"
f.remove_observer o
}
@@watchedFaces = []
return nil
end
def removeMaterials
model = Sketchup.active_model
timeOperation {
model.start_operation 'removeMaterials', true
Sketchup.active_model.entities.to_a.each { |e|
if e.instance_of? Sketchup;;Face
e.material = nil
e.back_material = nil
end
}
model.commit_operation
}
end
end
end