Move selected object to world origin

Hello, how would you go about writing a script that moves the selected object to the world origin?

https://sketchucation.com/pluginstore?pln=clf_center_on_origin

https://sketchucation.com/pluginstore?pln=TIG_originatecomponnetsaxes

Thanks a lot. I think I somehow made it work with the following code. It does the job but do you think there is any problem with it?

targetPoint = Geom::Point3d.new(0,0,0)
trans_move = Geom::Transformation.new(targetPoint)
Sketchup.active_model.selection[0].move!(trans_move)

The #move! method is used to set the transformation of this group instance, similarly to #transformation= but without recording to the undo stack.

This method is useful for moving entities inside of an animation or page transition.

If you are happy with it, for me is fine too.

I saved it as a .rb file and I am trying to -install(?)- it so I can use it faster, but I am getting the following error:

Error Loading File SendToOrigin.rb
Error: #<NoMethodError: undefined method `move!' for nil:NilClass>
C:/Users/MSi/AppData/Roaming/SketchUp/SketchUp 2019/SketchUp/Plugins/SendToOrigin.rb:5:in `<top (required)>'

What exactly is my .rb file missing?

Did you have an object selected before you loaded your Ruby?

Aha, then I think I am doing something wrong. I am having this error right after I open SketchUp. I thought I can copy paste the .rb file in Plugins folder, so that I can have faster access under the tools menu. So I should add more specifications in the .rb file to make it right?

Yes, you are doing it wrong. All .rb files in the Plugins folder are loaded as SketchUp starts. Your snippet is thus run once immediately but leaves nothing behind to be run again later. That is, it does not def any method, create any tool, or install anything in a menu. You should probably study some examples of extension files to learn how to do it right.

Sounds smart, thanks

See my recent posting …

1 Like

It assumes that the first member of the selection will be something and specifically a single something that responds to a move!() method call.

Also a global constant ORIGIN is already defined by SketchUp.

It would better that the code does nothing if nothing is selected, applies the transform to whatever is selected and is undoable just in case.

# Within you extension submodule ...

def move_to_origin
  model = Sketchup.active_model
  items = model.selection.to_a
  return "Nothing to move!" if items.empty?
  box = Geom::BoundingBox.new
  items.each {|item| box.add(item.bounds) if item.respond_to?(:bounds) }
  model.active_entities.transform_entities(
    Geom::Transformation.translation(box.corner(0).vector_to(ORIGIN)),
    items
  )
end

The bulk transform method Entities#transform_entities is natively undoable and it’s operation will appear in the Edit menu as “Undo Move”.

3 Likes

Thanks a lot for your reply, it was really super useful

1 Like

I’d recommend not using move! unless you are using it for animation purposes. Otherwise it omits the undo stack which is a UX issue.