sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Observer question

    Scheduled Pinned Locked Moved Developers' Forum
    5 Posts 3 Posters 354 Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P Offline
      Pout
      last edited by

      must me the heat here but some how i'm puzzled.
      I want to remove an observer.

      It's added like this:
      Sketchup.active_model.selection.add_observer(MySelectionObserver.new)

      and here is the observer code:
      class MySelectionObserver < Sketchup::SelectionObserver
      def onSelectionBulkChange(selection)
      #code to execute when the selection changes
      end
      end

      Now how do i remove this observer?

      Thx!

      1 Reply Last reply Reply Quote 0
      • TIGT Offline
        TIG Moderator
        last edited by

        Make an enduring reference to the observer as you set it...

        @@myselectionobserver=Sketchup.active_model.selection.add_observer(MySelectionObserver.new)

        later on to remove it use...

        Sketchup.active_model.selection.remove_observer(@@myselectionobserver)

        OR if the parts of the code are not within the same class/module use a global $ BUT make sure it's a 'unique' name - like $this_is_pouts_extra_special_selection_observer so no one else's similar observer by the same name gets messed with!!!

        TIG

        1 Reply Last reply Reply Quote 0
        • Dan RathbunD Offline
          Dan Rathbun
          last edited by

          @tig said:

          Make an enduring reference to the observer as you set it...

          @@myselectionobserver=Sketchup.active_model.selection.add_observer(MySelectionObserver.new)

          The API states that Selection.add_observer returns a boolean true || false indicating sucess.

          perhaps:

          @@myselectionobserver = MySelectionObserver.new() Sketchup.active_model.selection.add_observer(@@myselectionobserver)

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • TIGT Offline
            TIG Moderator
            last edited by

            Dan you are right - I rushed that off without thinking 😒
            Your way is correct - make a new observer, apply it... remove it!
            ` @@myselectionobserver = MySelectionObserver.new()
            Sketchup.active_model.selection.add_observer(@@myselectionobserver)

            Sketchup.active_model.selection.remove_observer(@@myselectionobserver)`
            IS the correct way...

            TIG

            1 Reply Last reply Reply Quote 0
            • P Offline
              Pout
              last edited by

              suberb, and this on a sunday!!!
              Thx tig and dan, much appreciated!

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              Buy SketchPlus
              Buy SUbD
              Buy WrapR
              Buy eBook
              Buy Modelur
              Buy Vertex Tools
              Buy SketchCuisine
              Buy FormFonts

              Advertisement