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==strcould 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 !!
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register LoginAdvertisement