Request for Direct Access to Placed Entities in #place_component Method

Normally, when a non-getter method returns the receiver object, this is so that call chaining can be used. But the API has not really applied this to the API methods. Many of them that return nil (ie, are command methods that have no purposeful return value,) could have been made to return the receiver object or module to facilitate call chaining.

But yes, this particular method is one we’ve discussed in the past that would have been good to return an array of the instances placed.

Also, beware of this method as I think it will commit an operation. This causes us to be forced to create multiple operations stitched together.
I show this concept here …

[code] undoable place_component() "MoveTool" clone

Other than this, you will need to either:

  • use the “snapshot” pattern to get the array of added instances
    before = ents.grep(Sketchup::ComponentInstance)
    # do model.place_component here
    after = ents.grep(Sketchup::ComponentInstance)
    added = after - before
    

EDIT: No the snapshot will not work. The place_component() method is asynchronous. The after will be created before the GUI placement even begins. This means also that added would be empty.

So you must use the observer pattern …

  • Or, attach an observer to the definition before calling place_component and use the observer to collect references to the new instances as they are added. (Again, see the example)

See also:

2 Likes