• Login
sketchucation logo sketchucation
  • Login
⚠️ Libfredo 15.4b | Minor release with bugfixes and improvements Update

How to get Model Entities change event once?

Scheduled Pinned Locked Moved Developers' Forum
8 Posts 2 Posters 463 Views 2 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.
  • F Offline
    fuzzybro
    last edited by 12 Sept 2013, 16:02

    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

    [OctaneRender for SketchUp Overview and Demo Version(http://sketchucation.com/click.php?url)]

    Latest updates, tutorials and artwork
    facebook.com/octane4sketchup

    1 Reply Last reply Reply Quote 0
    • D Offline
      Dan Rathbun
      last edited by 12 Sept 2013, 19:09

      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
      

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • F Offline
        fuzzybro
        last edited by 12 Sept 2013, 21:06

        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.

        [OctaneRender for SketchUp Overview and Demo Version(http://sketchucation.com/click.php?url)]

        Latest updates, tutorials and artwork
        facebook.com/octane4sketchup

        1 Reply Last reply Reply Quote 0
        • D Offline
          Dan Rathbun
          last edited by 13 Sept 2013, 02:53

          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
          

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • F Offline
            fuzzybro
            last edited by 17 Sept 2013, 15:05

            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.

            [OctaneRender for SketchUp Overview and Demo Version(http://sketchucation.com/click.php?url)]

            Latest updates, tutorials and artwork
            facebook.com/octane4sketchup

            1 Reply Last reply Reply Quote 0
            • D Offline
              Dan Rathbun
              last edited by 18 Sept 2013, 00:07

              The logic above is an example.

              You are free to change the logic, add conditional statements, to reset the @changed variable to false or true, depending upon conditions, in whatever callback you need to.

              I am just trying to give you some ideas, ... not write the plugin for you.

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • D Offline
                Dan Rathbun
                last edited by 18 Sept 2013, 00:09

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

                I'm not here much anymore.

                1 Reply Last reply Reply Quote 0
                • F Offline
                  fuzzybro
                  last edited by 18 Sept 2013, 08:03

                  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.

                  [OctaneRender for SketchUp Overview and Demo Version(http://sketchucation.com/click.php?url)]

                  Latest updates, tutorials and artwork
                  facebook.com/octane4sketchup

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

                  Advertisement