EntitiesObserver Weirdness
-
OK. I have repeatable steps for the Sketchup:EntitiesObserver that show some STRANGE behavior.
Use this code:
` class KW_EntitiesObserver_Weirdness < Sketchup::EntitiesObserver
def initialize(*args)
super
@m = nil
end
def onActiveSectionPlaneChanged(entities)
end
def onElementAdded(entities,entity)
end
def onElementModified(entities,entity)
puts "onElementModified: #{entity}"
if entity.is_a? Sketchup::Material
@m = entity
end
puts "material is #{@m}"
end
def onElementRemoved(entities,entity)
end
def onEraseEntities(entities)
enddef method_missing(meth,*args,&block) endend`
Materials Weirdness:
- In Sketchup, draw a few faces. Enclose them in a group
- Load the code above
- Select the group
- Do the following in the ruby console:
s = Sketchup.active_model.selection s[0].entities.add_observer KW_EntitiesObserver_Weirdness.new- Now change the material of one of the faces in the group. The observer will fire (at least) twice. In my case it sometimes fires three times - the first two times for the material. The console output should look like this:
onElementModified: #<Sketchup::Material:0x9c2328> material is #<Sketchup::Material:0x9c2328> onElementModified: #<Sketchup::Face:0xa77588> material isFor some reason, the material that was stored as @m after the first observer call has been lost. Even more strange, if you add the observer, to Sketchup.active_model.entities everything works fine. The steps for this are:
- In Sketchup, draw a few faces.
- Load the code above
- In the ruby console:
Sketchup.active_model.entities.add_observer KW_EntitiesObserver_Weirdness.new- Now change the material of one of the faces in the group. Again, the observer fires (at least) twice. In my case it sometimes fires three times - the first two times for the material. In any case @m maintains the reference to the material:
onElementModified: #<Sketchup::Material:0x98b3c8> material is #<Sketchup::Material:0x98b3c8> onElementModified: #<Sketchup::Material:0x98ae28> material is #<Sketchup::Material:0x98ae28> onElementModified: #<Sketchup::Face:0x98b680> material is #<Sketchup::Material:0x98ae28> -
What happens if you hold a reference to your observer instance?
-
Hi Dan,
Holding a reference does't make any difference. The only time the reference held by @m is actually stored is when the observer is assigned to Sketchup.active_model.entities.
Also, I noticed that in cases where I have a new file, and I first add the observer to a group's entities collection, the observer does not alert onElementModified for the material. It only alerts for the face.
Thanks,
--
Karen
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register LoginAdvertisement