sketchucation logo sketchucation
    • Login
    🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Getting entity information in onEraseEntity observer

    Scheduled Pinned Locked Moved Developers' Forum
    2 Posts 2 Posters 571 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.
    • S Offline
      smeyers
      last edited by

      For a project I'm working on, when the user deletes a component I would like for the face underneath that component to also be deleted.

      I am trying to use the onEraseEntity observer, which I have placed on the component, to look at the location of the entity being erased and then find the plane at that position. The issue is that the onEraseEntity observer returns a Deleted Entity rather than a Entity object. The Deleted Entity appears to not store any of the attributes of the entity, including its location.

      Any ideas on how to get around this issue? Is there a onBeforeEraseEntity observer somewhere out there?

      Thanks!

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

        @smeyers said:

        I am trying to use the onEraseEntity observer, ...

        To be precise, this is a callback method name, of the Sketchup::EntityObserver class (and subclasses.)

        @smeyers said:

        ... which I have placed on the component, to look at the location of the entity being erased ...

        Okay then, it follows that at the time you've attached the observer, you should have a reference to the component instance, ... which means you can get a reference to the instance's transformation, and store it as an instance attribute of YOUR observer instance (assuming each component instance you attach a separate new observer instance to.)

        And then, also add an onChangeEntity callback method, that compares the current instance's transformation with the one that was stored in the observer, and if they are different, then the instance was moved, rotated, etc., so update the stored transformation in observer instance.

        THEN,... later when the onEraseEntity callback is fired, you'll have a copy of the deleted instance's transform stored right there in the observer instance (that is being called.)

        You do this by passing the component instance's transform into your observer subclass' new() constructor, which passes it into the new instance's initialize() method, (after the object has been created,) where you assign it to an instance variable.

         module Author;;SomePlugin
        
          class NiftyEntitySpy < Sketchup;;EntityObserver
        
            attr_accessor(;transform)
        
            def initialize(trans_arg)
              @transform = trans_arg
            end
        
            def onChangeEntity(obj)
              return unless obj.respond_to?(;transformation)
              if obj.transformation.to_a != @transform.to_a
                transform=(obj.transformation)
              end
            end
        
            def onEraseEntity(obj)
              # obj is invalid here !
              #
              # Use transform() method or @transform reference directly, BUT
              # beware of making model changes inside observer callbacks !!!
              #
            end
        
          end # class
        
          # Somewhere in the code, attach a new observer instance,
          # to each component instance to be watched;
          inst.add_observer( NiftyEntitySpy;;new(inst.transformation) )
        
        end
        
        

        I'm not here much anymore.

        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