# Select component for placement

**URL:** <https://forums.sketchup.com/t/select-component-for-placement/52728>\
**Category:** Ruby API\
**Created:** [October 5, 2017, 10:38am UTC](https://forums.sketchup.com/t/select-component-for-placement/52728 "2017-10-05T10:38:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![SWeini](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@SWeini](https://forums.sketchup.com/u/SWeini)\
**Post date:** [October 5, 2017, 10:38am UTC](https://forums.sketchup.com/t/select-component-for-placement/52728/1 "2017-10-05T10:38:20Z")

</div>

Hello,

I’m creating a component via [Class: Sketchup::DefinitionList — SketchUp Ruby API Documentation](http://ruby.sketchup.com/Sketchup/DefinitionList.html#add-instance_method) (DefinitionList.add). Is there a way to select the created definition and start the component placement tool, so that the user can directly create an instance by clicking the mouse?

---

<div class="post-metadata">

**Author:** ![eneroth3](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/eneroth3/32/1222_2.png) [@eneroth3](https://forums.sketchup.com/u/eneroth3)\
**Post date:** [October 5, 2017, 11:02am UTC](https://forums.sketchup.com/t/select-component-for-placement/52728/2 "2017-10-05T11:02:17Z")

</div>

Model#place\_component does exactly this. [Class: Sketchup::Model — SketchUp Ruby API Documentation](http://ruby.sketchup.com/Sketchup/Model.html#place_component-instance_method)

---

<div class="post-metadata">

**Author:** ![SWeini](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@SWeini](https://forums.sketchup.com/u/SWeini)\
**Post date:** [October 5, 2017, 11:20am UTC](https://forums.sketchup.com/t/select-component-for-placement/52728/3 "2017-10-05T11:20:08Z")

</div>

Thanks a lot, this is exactly what I was looking for. Works perfectly. I must have searched in the wrong places (ComponentDefinition, DefinitionsList).

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [December 10, 2017, 7:53pm UTC](https://forums.sketchup.com/t/select-component-for-placement/52728/4 "2017-12-10T19:53:58Z")

</div>

> [@SWeini](#):
>
> I must have searched in the wrong places (ComponentDefinition, DefinitionsList).

Actually, it would make more sense look for it as an instance method of the [**`Sketchup::Entities`**](http://ruby.sketchup.com/Sketchup/Entities.html) class.

IF it were to be defined there, we would then be expected to need to only call it upon the active entities context thus:

```ruby
Sketchup::active_model.active_entities.place_component(def_ref)

```

But that would bring up the question, what should happen if it were to be called on an non-active entities context ?

Since it IS a user interactive command, it was thought best to just **force** it to act only within the active entities context, so the method was defined “up one level” as an instance method of the active model object.

Think about it this way, and it makes sense that it is defined where it is.

---

<div class="post-metadata">

**Author:** ![eneroth3](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/eneroth3/32/1222_2.png) [@eneroth3](https://forums.sketchup.com/u/eneroth3)\
**Post date:** [December 10, 2017, 8:06pm UTC](https://forums.sketchup.com/t/select-component-for-placement/52728/5 "2017-12-10T20:06:07Z")

</div>

Since this method activates a tool I think it makes sense to have it on the model class next to select\_tool. ComponentDefinition and DefinitionList would also make sense though.

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [December 10, 2017, 8:19pm UTC](https://forums.sketchup.com/t/select-component-for-placement/52728/6 "2017-12-10T20:19:08Z")

</div>

> [@eneroth3](#):
>
> Since this method activates a tool I think it makes sense to have it on the model class next to select\_tool.

I just said this with another more important reason …

> [@DanRathbun](#):
>
> Since it IS a user interactive command, it was thought best to just force it to act only within the active entities context, so the method was defined “up one level” **as an instance method of the active model object**.
> 
> Think about it this way, and it makes sense that it is defined where it is.

I will agree a **`Sketchup::ComponentDefintion#place_with_mouse()`** block form method would be COOL!  
Right now it’s a rigmarole dealing with the instance after placement. Usually we have to use a temporary definition observer with an **`onComponentInstanceAdded`** callback.  
Like in this example…  
[[code] undoable place\_component() "MoveTool" clone](https://forums.sketchup.com/t/code-undoable-place-component-movetool-clone/27193)

It would be nifty to do something like the following but without having to use an observer:

```ruby
this_defn.place_with_mouse(true) {|i|
  # code to perform on instance i
  # after each placement if repeat arg is true:
  i.add_attribute("MyDictionary","SomeProperty",some_value)
}

```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/sketchup/original/3X/f/2/f2a121500b085ed60806607a82720b092d800bb3.png) [@system](https://forums.sketchup.com/u/system)\
**Post date:** [March 12, 2018, 12:19pm UTC](https://forums.sketchup.com/t/select-component-for-placement/52728/7 "2018-03-12T12:19:18Z")

</div>

Closed on March 12, 2018, 12:19pm UTC

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.
