Hi!
Im trying to figure out how to use the add_observer method on the entities in a group, and saw TTs list (url below)
http://www.thomthom.net/software/sketchup/observers/#ViewObserver
Here my worst nightmare came through... The Sketchup::EntitiesObserver - onElementModified(entities, entity) dont work. Just as I had experienced myself.
What I try to accomplish (see the code below), is to keep track of the area of a specific face, named yv_face_area (green color). Initially I have set an attribute to the rsgroup (which contains the yv_face_area) set_attribute("rsdict, "area", area). It seems to work fine.
But, as mentioned, the onElementModified method dont seems to work.
Anyone haves an idea how to work around this problem?
snippet:
mod = Sketchup.active_model # Open model
ent = mod.entities # All entities in model
sel = mod.selection # Current selection
class MyEntityObserver < Sketchup;;EntityObserver
def onEraseEntity(entity) #this work
UI.messagebox("onEraseEntity; " + entity.to_s)
end
end #class
class MyEntitiesObserver < Sketchup;;EntitiesObserver
def onElementModified(entities, entity) #dont seems to work
UI.messagebox("onElementModified; " + entity.to_s)
end
def onEraseEntities(entities) #this seems to work
UI.messagebox("onEraseEntities; " + entities.to_s)
end
def onElementRemoved(entities, entity_id) #dont seems to work
UI.messagebox("onElementRemoved; " + entity_id)
end
def onElementAdded(entities, entity) #this seems to work
UI.messagebox("onElementAdded; " + entity.to_s)
end
end #class
pts = []
pts[0] = [ 0, 0, 0]
pts[1] = [ 1000.mm, 0, 0]
pts[2] = [ 1000.mm, 1000.mm, 0]
pts[3] = [ 0, 1000.mm, 0]
pts[4] = [ 0, 0, 1000.mm]
pts[5] = [ 1000.mm, 0, 1000.mm]
pts[6] = [ 1000.mm, 1000.mm, 1000.mm]
pts[7] = [ 0, 1000.mm, 1000.mm]
5.times do |i|
rsgroup = ent.add_group
rsgroup.entities.add_face pts[0..3]
yv_face_height = rsgroup.entities.add_face pts[4..7]
rsgroup.entities.add_face pts[0], pts[3], pts[7], pts[4]
rsgroup.entities.add_face pts[3], pts[2], pts[6], pts[7]
rsgroup.entities.add_face pts[1], pts[2], pts[6], pts[5]
yv_face_area = rsgroup.entities.add_face pts[0], pts[1], pts[5], pts[4]
yv_face_height.pushpull rand(1000.mm)
yv_area = yv_face_area.area.to_m.to_m
yv_face_area.material = (0xAADEAA)
rsgroup.set_attribute "rsdict","area", yv_area
yv_face_area.set_attribute "rsdict", "area2", yv_area
point = Geom;;Point3d.new (1100.mm * i, 0, 0)
t = Geom;;Transformation.new point
rsgroup.transform! t
rsgroup.add_observer(MyEntityObserver.new)
rsgroup.entities.add_observer(MyEntitiesObserver.new)
end #times_do
ent.each do |o|
point = Geom;;Point3d.new (0, 1100.mm, 0)
t = Geom;;Transformation.new point
o.transform! t
end #each_do
i = 0
ent.each do |o|
attr = o.get_attribute "rsdict","area"
if attr
UI.messagebox "Attr - Area; " + attr.to_s + " mΒ² - n; " + i.to_s
i = i + 1
end
end