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.

Greetings

Joseph

Pil_1_piano.rb (1.1 KB)

You can use model.place_component for that:

Thanks for the answer, I’ve already tried watching " model.place.component",
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

Joseph

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.
http://ruby.sketchup.com/Sketchup/DefinitionList.html#add-instance_method
http://ruby.sketchup.com/Sketchup/ComponentDefinition.html#entities-instance_method

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

Joseph.

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
model.place_component(cdef,false)
# 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
    definition.remove_observer(self)
  end

  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
        UI.stop_timer(@tid)
        respond_to_instance(@instance)
      elsif @elapsed > @timeout
        UI.stop_timer(@tid)
        definition.remove_observer(self)
      end
    end
    # Attach this module as temporary observer:
    definition.add_observer(self)
    # Place the instance with the MoveTool:
    definition.model.place_component(definition,false)
  end

  def respond_to_instance()
    # Do something with @instance
  end

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?