Recreate Move Tool

I’m new to making Tools, and I need a tool that essentially does what the move tool does, it would be easy to make a tool that moves from point A to point B, but where I am lost is actually showing the object you are moving while you are moving it.

Is it a new component instance ? There is an API interface that allows us to leverage the MoveTool in component placement mode, where the instance is stuck to the cursor until you click the insertion point.

Sketchup::Model#place_component()

But it is a bit weird as I do not think the Ruby waits for the method to return.

If it is not a component, you could temporarily wrap the object(s) in a component, use this method, then explode the instance when done, and remove the temporary definition.

But you need to use a temporary DefinitionObserver to watch when the user is done placing the instance (ie, the observer is only attached during the placement, and detached afterward.)

1 Like

you can use the Move Tool itself by calling it and a timer, when the timer is stopped your tool can continue…

that’s what happens in the selection tool gif I posted on your other thread…

In it, the user can use any of the builtin manipulation tools and then hit the spacebar to pop back into my tool…

john

Did you post the code with that gif? What’s the link?
I’ve been testing timers.
First problem is that
id = UI.start_timer(5, false) { UI.messagebox(‘hello’)}
repeats, but so does
id = UI.start_timer(5, true) { UI.messagebox(‘hello’)}
???
I want a timer that does not repeat.

avoid UI.messagebox when testing, it can fail silently…

id = UI.start_timer(5, false) { puts('hello')
UI.stop_timer(id)}

john

1 Like

I use two timers to pop my tool…

The first calls this method and watches for the attribute being true…

    # this allows you to manipulate using standard tools and then 'pops' you back into the plugin...
    def modify_selector
      Sketchup.send_action 'selectScaleTool:'
      model = Sketchup.active_model
      ents  = model.active_entities
      sel   = model.selection
      @tool = ''
      what_comes_next = UI.start_timer(1.0, true) do
        if Sketchup.active_model.tools.active_tool_name != 'SelectionTool'
          tool = Sketchup.active_model.tools.active_tool_name
          puts "SelectorClass waiting for #{tool}" unless tool == @tool
          @tool = tool
        else # this is the 'pop' back
          p 'SelectorClass done'
          UI.stop_timer(what_comes_next)
          model.set_attribute('JcB_SS', 'next', true)
        end # if tool
      end # what_comes_next
    end # modify_selector

john

1 Like

Very Helpful, would you mind elaborating on this? I haven’t used any observers yet, but it seems that I can only track when an instance is added or removed, but not moved. Is that correct?

You should read this to understand the quirks of the place_component() method:

With that observer yes. But you are the one initiating the move, so you do not need to know that a move occurs (you’re in the middle of a move.)

And just for info: component instances are a Entity subclass, so you can attach an Entities observer to them, and detect a callback to onChangeEntity(). But this will not help you, as you need to attach to the instance after you place it, and when you do, well you know the move is over.


See this example I just cobbled together:

And reference these other 13 topics mentioning place_component():

class CreateCopy

	def activate
		@mouse_ip = Sketchup::InputPoint.new
		draw_whatever
	end

      def deactivate(view)
        view.invalidate
		@car_group.erase!
      end
	  
	def onMouseMove flags, x, y, view
		@mouse_ip.pick(view, x, y)
		@car_group.move! @mouse_ip.position
		view.tooltip = @mouse_ip.tooltip if @mouse_ip.valid?
		view.invalidate
	end
	
	def onLButtonDown flags, x, y, view
		group2 = @car_group.copy
		group2.set_attribute "sklik_rab", "Pondasi 70x35x20 (P1)", "pondasi"
	end

	def draw view
		@mouse_ip.draw(view) if @mouse_ip.display?
	end

	def draw_whatever
		ents = Sketchup.active_model.entities
		@car_group = ents.add_group
		main_face = @car_group.entities.add_face [-0.1.m,-0.1.m,0], [0.1.m,-0.1.m,0], [0.1.m,0.1.m,0], [-0.1.m,0.1.m,0]
		main_face.reverse!
		main_face.pushpull 5
	end

end

# Create the Command object
CreateCopy_cmd = UI::Command.new("CreateCopy") {
Sketchup.active_model.select_tool CreateCopy.new
}
# Configure the Command's appearance
CreateCopy_cmd.small_icon = "s_small.gif"
CreateCopy_cmd.large_icon = "s_large.gif"
CreateCopy_cmd.tooltip = "Create and copy"
# Create and configure the Toolbar
CreateCopy_toolbar = UI::Toolbar.new "CreateCopy"
CreateCopy_toolbar.add_item CreateCopy_cmd
CreateCopy_toolbar.show