Ruby code for selecting Component Instances by name
-
Dear all,
I'm a complete beginner with Ruby language, could anybody help me to write the necessary code to select a component instance in a given model by its name ?
Many thanks in advance
milleb
-
Going by the exact wording of your request you need to iterate all active_entities and extract ComponentInstances, then compare the instance names with some preset string...
However, an instance might have no name set at all or share names with several siblings, or even have a name that spans across several definitions...
Assuming this is the case, then select those instances...str="SomeName";m=Sketchup.active_model;s=m.selection;s.clear;s.add(m.active_entities.grep(Sketchup;;ComponentInstance).select{|e|e.name==str})
BUT I suspect that you really want to select instances of a component by its definition name ?
str="SomeName";m=Sketchup.active_model;s=m.selection;s.clear;s.add(m.active_entities.grep(Sketchup;;ComponentInstance).select{|e|e.definition.name==str})
in the code the exact match == in
...name==str
could be changed to...name=~/#{str}/
to match only partially anywhere in the name, or even...name=~/^#{str}/
to match any name starting with that string, or...name=~/#{str}$/
to match any name ending with that string etc... -
Many thanks TIG, your first code line does perfectly the job.
In fact I have many instances of the same component, and each one is differentiated by a unique name.
And just by removing s.clear I can make multiple selections, which is exactly what I intended to do.You made my day !!
Advertisement