Change axes position of the selected group or component and pick new position form viewport

Hello,
Actually, I am creating a script which sets the ORIGIN of a group/component to pick a point from the view port.

Can anyone help me to solve it. I attach the script below.
Thanks

module ChangeAxesTool
  class Tool
    def initialize
      @model = Sketchup.active_model
      @selection = @model.selection
      @entity = @selection.first if @selection.size == 1 && (@selection.first.is_a?(Sketchup::Group) || @selection.first.is_a?(Sketchup::ComponentInstance))

      unless @entity
        UI.messagebox("Please select a Group or Component before activating the tool.")
        return
      end

      @pick_helper = Sketchup::PickHelper.new  # Removed incorrect argument
      @ip = Sketchup::InputPoint.new
    end

    def activate
      Sketchup.active_model.active_view.invalidate
    end

    def onMouseMove(flags, x, y, view)
      @ip.pick(view, x, y)
      view.invalidate
    end

    def draw(view)
      @ip.draw(view) if @ip.valid?
    end

    def onLButtonDown(flags, x, y, view)
      if @entity && @ip.valid?
        picked_point = @ip.position
        #transformation = Geom::Transformation.new(picked_point)
        #entity.transform!(transformation)
		tr = Geom::Transformation.axes(picked_point, X_AXIS, Y_AXIS, Z_AXIS)
		@entity.entities.transform_entities( tr, @entity.entities.to_a )
        Sketchup.active_model.active_view.invalidate
      end
    end
  end

  def self.activate_tool
    Sketchup.active_model.select_tool(Tool.new)
  end
end

ChangeAxesTool.activate_tool

Post colorized code correctly in the forum and please edit your previous post.


The task is not clear.
Do you want to move the selected instance of group/component so that its origin is where you clicked? (this physically moves the instance)

Or would you like to modify the definition of the selected instance of group/component so that its origin is where you clicked? (this only modifies the origin, the group/component entities location apparently will stay as they were originally)

__

You can not instantiate PickHelper object like this.
You can retrieve a PickHelper object using the View#pick_helper method.
You don’t use @pick_helper anywhere else anyway, so it’s useless…

You assigned Sketchup::ComponentInstance or Sketchup::Group to @entity, however there is no .entities method for Sketchup::ComponentInstance Only its definition have the .entities method.
(The group a special kind of componentInstance so you can retrieve its entities directly or from its definition…but I won’t explain this in more detail now)

onLButtonDown(flags, x, y, view) have already a view reference, so you can invalidate it as you did it in onMouseMove(flags, x, y, view) above…

I would rather do this check before activating the tool and send the entity as a parameter.

1 Like

Hello Dezmo,

I want to change the selected group or component axes position.
This is the current position of the axes.

Want to pick a position from viewport and set

But do not change the position of selected group or component entities.
Only change axes position of the selected group or component.

See this topic:

2 Likes

Hello Dezmo,

Thanks for the topic; it helped me to update my script.
See how it is working now.
link: https://youtube.com/shorts/lgBu2QwucrM?feature=share

and thanks to other topics I refer to for updating my script.
Dezmo, Thomas (TT), Dan Rathbun,

Thank you.