Select instances in the selection
-
**Hello everybody,
With the code below, it is possible to select an instance in SketchUp by ca definition.
m=Sketchup.active_model;s=m.selection;m.definitions.each{|d|s.add d.instances if d.name=~/#{"Handle"}/}
Is it possible to select the instances "Handle # 1", "Handle # 2", "Handle # 3" etc. which are only in the furniture being selected?
If so how do I do it?
Thank you in advance for your help.
David**
-
There are some code examples here:
Selecting Specific Entity, Group or Component (By Name)
Not sure if it is on the ruby end or if can be done in c. Is there a way to find a specific component in a model by its definition? Also this is from the ruby api. If [0] assumes the first entity, is there a way to get …
SketchUp Community (forums.sketchup.com)
These examples use the
==
but you can use regular expressions:
http://ruby-doc.org/core-2.0.0/Regexp.htmlbegins with:
=~ /\A#{name}/
Or the specific string "Handle" at start of string followed by: an optional whitespace character, underscore or dash; followed by a literal pound sign (which I escape just in case);
followed by an optional whitespace character; followed by one or more digit characters; just before the end of the string:
=~ /\AHandle(\s|-|_)?\#\s?\d+\z/
... and to make it non-case insensitive we can add a "i" switch at the end outside the slashes:
=~ /\AHandle(\s|-|_)?\#\s?\d+\z/i
If you are using Ruby 2.0 or higher you can also use the
String#start_with?()
method:
http://ruby-doc.org/core-2.0.0/String.html#method-i-start_with-3Finst.name.start_with?('Handle')
... or for case insensitive convert to a title:
inst.name.capitalize.start_with?('Handle')
-
**Thank you for all these Dan examples.
= ~ / # {"name"} /
It's perfect because it works for all instances.
Now, I only want to select the instances present in a selection of components.
The problem is that the code below, selects all the instances in the SketchUp file.
m=Sketchup.active_model;s=m.selection;m.definitions.each{|d|s.add d.instances if d.name=~/#{"Handle"}/}
Thank you**
-
@tntdavid said:
Now, I only want to select the instances present in a selection of components.
Then use the grep method that TIG showed you to collect all instances in the selection set.
http://sketchucation.com/forums/viewtopic.php?f=180&t=68172&p=626519#p626479Then build an array of definitions for those instances.
Then unique the array so that each definition reference appears only once in the array. (Or use a set.)
(And read the several examples in the SketchUp forum thread I pointed to above.)
@dan rathbun said:
There are some code examples here:
https://forums.sketchup.com/t/selecting-specific-entity-group-or-component-by-name/11469 -
Thank you Dan.
I will analyze all your information in detail.
Advertisement