Getting entity information in onEraseEntity observer
-
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!
-
@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'sinitialize()
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
Advertisement