The name of the dictionary "JcB"
should have been read as an example. (It is John’s initials.)
You really should use your own unique dictionary name. Usually developers use a combination of their company namespace and the plugin name …
Ex:
return if Sketchup.active_model.get_attribute('YangSuoJin_ModelMover', 'move_done')
The return
statement in Ruby “returns” from a method to the place that called the method.
The first time run, your should see "new_file"
output to the Console.
You can change that to something more descriptive, if you wish. Or you could use a message box.
See: UI.messagebox
1 Like
Very good to know! Thanks Dan.
1 Like
@john_drivenupthewall
Final Code with Full Function:
require 'sketchup.rb'
module JohnDanYangshuojin
module CenteringModel
# Centering model on xy plane of the world axes but preserving the camera setting saved with all scenes.
def self.centering_model
# Centering the model on xy plane by the projection of its geometric center.
model = Sketchup.active_model
center = model.bounds.center
vec = Geom::Point3d.new(center.x,center.y,0).vector_to(ORIGIN)
model.entities.transform_entities(vec,model.entities.to_a)
stp_r, stp_g, stp_b = vec.to_a.entries
# Preserving the camera setting in each scenes.
pages = Sketchup.active_model.pages
pages.each do |page|
# camera
cam = page.camera
# camera target
target = cam.target
# camera eye
eye = cam.eye
# camera up
up = cam.up
# get arrays
e0 = eye.to_a
t0 = target.to_a
# use standard ruby on target position
t = t0.zip([stp_r, stp_g, stp_b]).map(&:sum)
# eye positions
e = e0.zip([stp_r, stp_g, stp_b]).map(&:sum)
# use the same up
cam.set(e, t, up)
end #pages.each
# Message about the job done.
UI.messagebox('This model is centered!')
end # centering_model
#Add menu item.
unless file_loaded?(__FILE__)
# Add to extension menu.
menu = UI.menu('Plugins')
#Menu name.
menu.add_item('Centering Model'){
self.centering_model
}
file_loaded(__FILE__)
end
end # module CenteringModel
end # module JohnDanYangshuojin
1 Like
Thanks Dan! I will definitely do clean up and repost. I do see your comments would benefit hugely, hugely in the long run.
2 Likes
Dan,
Code refined based on your instruction. I wonder how you guy’s code gets color and format?
See this pinned post at top of Developers category …
1 Like
A bit late to the convo,
But for anyone less technical, I put the code from post no.43 into an easily install-able RBZ for anyone looking to use this more than once.
JDY_CenterModel.rbz (843 Bytes)
I made one tiny change: the menu item is now named ‘center model’ instead of ‘centering model’ - made more sense to me as a menu item.
For anyone downloading: be aware that this is not an undoable command! If you execute this, you can undo the move of the model, but not the move of the scenes!
1 Like