Move child component in dynamic component
-
I am attempting to move a child in a dynamic component through a Ruby script by setting the entity's transformation. This appears to work at first, but I get a really weird effect when I redraw (either through redraw_with_undo or using the hand icon). The objects seem to jump around, if I keep resetting the transformation and redrawing eventually it converges to the right place. Does anyone know why this is happening or a better way to move these sub components in a Ruby script(while still allowing the user to move them through the UI)? Here is my demo script and I have attached the problem component.
Thanks a lot,
DanOpenStudio_DaylightingControls.skp
import the component "OpenStudio_DaylightingControls.skp" and place at the origin
select the component as the only component then run this script
selection = Sketchup.active_model.selection
view = Sketchup.active_model.active_viewfor i in 0..10
puts selection[0].definition.entities[0].transformation.origin
selection[0].definition.entities[0].transformation = Geom::Transformation.translation([0.m, -1.m, 0.m])
puts selection[0].definition.entities[0].transformation.origin$dc_observers.get_latest_class.redraw_with_undo(selection[0])
view.refresh
sleep(1)
end -
Hello Dan! Sorry I missed this post. Here are two solutions to your problem. (I'm sure there are others, too.)
-
Instead of moving the "sub" DCs with Ruby and asking the parent to redraw, have the subDCs position themselves based off of some custom attributes in the parent. You script would set these attributes, like sensor_1_x to 5.9, sensor_2_y to 25.0, etc. Then when you fire a redraw, the children will puppet themselves properly. (But, if the user tries to manually reposition them, it will ignore them... maybe not what you want.)
-
Add a couple of more lines to you existing script...
selection = Sketchup.active_model.selection view = Sketchup.active_model.active_view for i in 0..10 puts selection[0].definition.entities[0].transformation.origin selection[0].definition.entities[0].transformation = Geom;;Transformation.translation([0.m, -1.m, 0.m]) puts selection[0].definition.entities[0].transformation.origin # DCs keep track of their "last size" in some attributes. # This allows them to infer whether they have been scaled # manually by the user. # # In this case, you are changing the bounding box of the DC # by moving its subparts, and so it thinks that the scale # tool has been applied, and is making some bad repositioning # decisions based on that. Doh! # # These are the methods that the DC plugins uses to tell the DC # to "recalculate" its bounding box. Do this after your translation # and things are happier. (I can explain more if you want...) lenx, leny, lenz = selection[0].unscaled_size selection[0].set_last_size(lenx, leny, lenz) $dc_observers.get_latest_class.redraw_with_undo(selection[0]) view.refresh sleep(1)
-
-
Works great, thanks so much! I guarantee I would not have gotten that on my own.
(PS I went with option 2 because the user can still move them on their own.)
Thanks a lot,
Dan
Advertisement