Hy, I’am build a sketchup extension for a simulation of better router position. It’s a University project.
I need an observer to recognize when a user moves the object. With this I used if I remove the object the observer answers me but if I move it gives me no results … Help me please!
#this is my Observer
class Observer < Sketchup::EntityObserver
def onEraseEntity(entity)
puts("EntityObserver::onEraseEntity: #{entity.to_s}, is deleted? #{entity.deleted?}")
end
def onChangeEntity(entity)
puts("EntityObserver::onChangeEntity: #{entity.to_s} #{entity.entityID}")
end
end
#this is the code that add Observer
def self.create_Ap(x,y,z,x_min,y_min)
model = Sketchup.active_model
model.start_operation('Create Ap', true)
group = model.active_entities.add_group
entities = group.entities
points = [
Geom::Point3d.new(x+x_min, y+y_min, z.m),
Geom::Point3d.new((x+(0.15.m)+x_min), y+y_min, z.m),
Geom::Point3d.new((x+(0.15.m)+x_min), (y+(0.1.m)+y_min), z.m),
Geom::Point3d.new(x+x_min,(y+(0.1.m))+y_min, z.m)
]
face = entities.add_face(points)
face.pushpull(-0.02.m)
face.material = "Black"
face.back_material = "Black"
face.set_attribute "testdictionary", "test", 1
if $node == nil
$node = Array.new
end
$node.push(face)
face.add_observer(Observer.new)
model.commit_operation
end
Can you elaborate what the problem is? I ran the observer and didn’t see anything unexpected with it.
Also it’s quite hard to understand your code. Using 2 space indentation would help as we wouldn’t need to scroll sideways (or scroll away vertically from where we are just to get to the horizontal scroll bar). Using paranthesis around arguments makes it much easier to see what words are arguments to what method call. Lastly global variables should be avoided as they may clash with global variables defined by other plugins. You can wrap your code in a module and use a class variable instead.
It works for me.onChangeEntity() fires if you double-click into the group and move one of the BLACK face’s vertices with the MoveTool, or move the entire face with the PushPullTool.
Were you wanting to fire the observer if the user moved the entire group instance ?
If so, you’ll need to attach the observer to the group reference also.
I simply attached the observer myself to a selected entity from the console rather than figuring in out what the long method did and make up values for its arguments. I think you found the problem!
However, @d.trepiccione93, I would not create a new observer instance for every model object.
Instead I’d create only one per model, and reuse it for all the model’s object (if possible.)
But, I never use model level observers for more than one model. (That can “cross wires” and lead to problems and confuse the code.)
Thank you so much. I will modify the code taking into account all the advice you gave me.
Since we find ourselves, since these solids that I add to the model represent routers, if I wanted to import premade models instead of creating those ugly solids?