For a single ID, the 1st step is finding the entity that matches your ID.
model = Sketchup.active_model
ents = model.entities
… then using Enumerable#find
…
found = ents.find {|e| e.entityID == 725 }
If the entity was found, then found
will be it’s reference, and nil
if not found.
If looking for multiple (Drawingelement
) Entity
objects, that are say hidden, you can use Enumerable#find_all
found = ents.find_all {|e| e.hidden? }
With this filter method the return is always an array, which might be empty if no entity matched the search block.
Okay? Now, for multiple IDs, you will need to define an array of the IDs to search for.
targets = [725, 726, 832, 901]
Then test if each entity’s id is included in the array …
found = ents.find_all {|e| targets.include?(e.entityID) }
(There might be another search algorithm that stops iterating once all targets are found, but for this tutorial we’ll use the simple brute force iterator method.)
Once you have the found
entity array, just pass it as an argument to the Selection#add
method (as long as it’s not nil
) …
model.selection.add(found) if found
But keep in mind that some API methods will return non-Drawingelement objects which the Selection
does not like.
REF: `Sketchup::Selection` control methods will not accept `#explode` arrays; `TypeError` message misleading · Issue #759 · SketchUp/api-issue-tracker · GitHub
The #add
method actually wants Sketchup::Drawingelement
subclass objects, so we must filter other types out …
stuff = instance.explode.grep(Sketchup::Drawingelement)
Sketchup.active_model.selection.add(stuff)