Why does the #place_component method in the SketchUp API return the SketchUp::Model instead of the entities placed as an array?
In my opinion, returning the placed entities directly would make it easier to perform actions on them, saving time and increasing efficiency by not having to search for them using less optimal ways.
Here is the URL for the method in SketchUp Ruby API Documentation:
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 …
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)
It’s the place to make feature request. That is monitored more actively than the forums and we have automation in place to log the issues into our internal trackers. Makes it a lot easier for us to track bugs/feature requests.