sketchucation logo sketchucation
    • Login
    1. Home
    2. dmac
    3. Posts
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info
    D
    Offline
    • Profile
    • Following 0
    • Followers 0
    • Topics 1
    • Posts 2
    • Groups 1

    Posts

    Recent Best Controversial
    • RE: Removing entity observers

      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.

      1. I did use a subclass of EntityObserver but removed it when creating the test. I've readded a trivial observer.

      2. Ok. What causes this? Is it just calling methods on entities, or adding/removing entities? or any mutable operation on the model?

      3. I wanted to show that it doesn't matter what entities the observers are added to, the effect is the same.

      4. Yes, I meant to create an instance.

      5. 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.

      1. Ok.

      2. 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.

      3. 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
      
      
      posted in Developers' Forum
      D
      dmac
    • Removing entity observers

      I add entity observers to each face, and later remove them, but this causes a slowdown each time I do it. An example demonstrates this (run on a file with 1000 cubes):

      require 'sketchup'
      
      def timeOperation
        startTime = Time.now
      
        yield
      
        time = Time.now - startTime
        puts "Completed in; #{time.to_s}"
      end
      
      def addObservers
        Sketchup.active_model.entities.each { |e|
          if not e.instance_of? Sketchup;;Face
            o = Sketchup;;EntityObserver
      
            e.add_observer(o)
            e.remove_observer(o) # Returns true
          end
        }
      end
      
      def removeMaterials
        timeOperation {
          Sketchup.active_model.entities.each { |e|
            if e.instance_of? Sketchup;;Face
              e.material = nil
              e.back_material = nil
            end
          }
        }
      end
      

      Running the methods in the Ruby console:

      
      > removeMaterials
      Completed in; 0.172
      
      > addObservers
      > removeMaterials
      Completed in; 0.78
      
      > addObservers
      > addObservers
      > addObservers
      > addObservers
      
      > removeMaterials
      Completed in; 3.23
      
      

      Each time addObservers is called, the time that removeMaterials takes increases. Shouldn't addObservers be a no-op? An observer is added and then removed, resulting in no observers. Also, this persists after a File->Open.

      posted in Developers' Forum
      D
      dmac
    • 1 / 1