How to know the type of removed element in onElementRemoved


#1

EntitiesObserver::onElementRemoved(entities, entityID)
How can one know what was entity type actually removed without holding a huge hash of entityID-type…
thanks


#2

You could attach EntityObserver to each entity in the entities collection.

class MyEntityObserver < Sketchup::EntityObserver
  def onEraseEntity(entity)
    puts "onEraseEntity: #{entity}\n  is a class: #{entity.class.to_s}"
  end
end

It will give output like:

onEraseEntity: #<Deleted Entity:0x9e73d68>
  is a class: Sketchup::Group

I believe the EntityObserver callbacks get called before those in EntitiesObserver.


#3

Pretty hardcore, I usually have huge scene as input, hundred/thousands Face-s… Sometimes just polysoups, not components


#4

This does NOT work, as Ruby object_id is different than entityID !

puts " entity is class: #{ObjectSpace._id2ref(entityID).class.name}"

I also tried this, but it just returns NilClass meaning the object has been set nil beforehand.

class MyEntitiesObserver < Sketchup::EntityObserver
  def onElementRemoved(entityset,entityID)
    # 
    entity = entityset.model.find_entity_by_id(entityID)
    #
    puts "onElementRemoved: #{entityset}"
    puts "     entityID is: #{entityID}"
    puts " entity is class: #{entity.class.name}"
  end
end

So, you find like we, that these observer removal callbacks are near worthless.


#5

Yeah, basically they said observers are for debug, but even that purpose is hindered by the limited info sent to the methods… especially these remove ones.
I really hope the developers are reading these API forums from time to time. They need to add some new methods or something to the observers with some proper args (just delete the entity AFTER the observer call, and let us know that the reference will be worthless after the call, but at least we can interrogate our soon-to-be-deleted entities).


#6

Also as a side note, if you modify an entity inside onElementAdded or Modified, the subsequent events are dropped for that operation you made in the UI. I dont know, maybe they do not queue the events when inside the obsever. So DONT CHANGE ANYTHING inside observers, deffer the operations to some timer.