Insert object on mouse click

Hello everyone, I wrote a small file Api Ruby, with which I create a pillar, setting the two
measurements at the base of the pillar, side along the x-axis and y and the height, all by entering the measurements
inputbox… in creating the face for the “pushpull”, the first point equals pt1 [0, 0, 0]
origin of the X, Y, Z axes … I would like the pillar object to remain momentarily attached
cursor and subsequently inserted on screen at a precise point at the click of the mouse insertion,
I hope I have explained myself… Possibly? What should I write to the Ruby file, for this insertion wait
pillar object… Thank you.



Pil_1_piano.rb (1.1 KB)

You can use model.place_component for that:

Thanks for the answer, I’ve already tried watching "",
but I understood very little, if he was kind enough to show me with practical example
using my ruby file, it would be great…Thank you


Currently your code adds a group and then creates geometry in it.

You could add a new definition into the model’s definitions, and then add your geometry into its entities context, much as you did with the group.entities.

Then use model.place_component(…) with the reference to this new component definition.

Also note that you should wrap your method inside your own module, so that it remains separate from everyone elses...
1 Like

I understand very little…Thanks all the same


Greetings Thomas. I feel like a fan talking to my idol here. :grinning:
Small follow up question to place_component method.
It returns Sketchup::Model instead of the ComponentInstance. Seems reasonable since repeat argument could be set to true and it would return something like an array OR ComponentInstance.

Therefore, how would you grab a handle to the ComponentInstance (assuming repeat set to false).
Should I go with Sketchup.active_model.entities[-1] or is there something more intuitive.

Thanks, Felipe.

There are several ways.

1. Since you must have a reference to the component definition, you can take an array snapshot of it’s instances collection before and then subtract it from the array afterward.

before = cdef.instances
# Wait some time because place_component returns immediately.
new_instances = cdef.instances - before

But the main problem is knowing when the placing has been completed (as place_component is non-blocking and returns immediately.)

This is the reason why it just returns the model object - ie the method call returns immediately and does not wait for the user to place any instances.

Changing how it works now is not possible. But adding a sister method that does wait and retrun an array of placed instances is an option.

A better approach is likely to use a temporary definition observer …

module Author::SomePlugin
  # inside your plugin module ...
  extend self

  def onComponentInstanceAdded(definition, instance)
    @instance = instance

  def place_component(definition)
    @instance = nil
    # Set up a timer to check for the new instance and remove observer
    # if the timeout is reached (ie, user canceled the placement.)
    @timeout = 10.0
    @elapsed = 0
    @tid = UI.start_timer(1.0,true) do
      @elapsed += 1.0
      if @instance
      elsif @elapsed > @timeout
    # Attach this module as temporary observer:
    # Place the instance with the MoveTool:

  def respond_to_instance()
    # Do something with @instance

end # plugin module

You might also watch the toolstack. The PlaceComponent tool will change to the MoveTool at some point.

There are a bunch of open API Issues regarding placing components with the API(s) …

1 Like

Perfect. It is working like a charm using the sister method. Thanks. Appreciate it.

Makes perfect sense for me now. And once again thanks for going further and explaining the main reason behind it returns Sketchup::Model.

sister method?