Select instances in the selection

Select instances in the selection

Postby TNTDAVID » Fri Oct 20, 2017 7:54 pm

Hello everybody, :enlight:

With the code below, it is possible to select an instance in SketchUp by ca definition.

Code: Select all
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
0
* Nouveau !!!
Découvrez notre nouveau Plugin Click-Cuisine 2, pour créer vos cuisines 3D !
User avatar
TNTDAVID 
PluginStore Author
PluginStore Author
 

Re: Select instances in the selection

Postby Dan Rathbun » Sat Oct 21, 2017 6:08 pm

There are some code examples here:

https://forums.sketchup.com/t/selecting-specific-entity-group-or-component-by-name/11469

These examples use the == but you can use regular expressions:
http://ruby-doc.org/core-2.0.0/Regexp.html

begins 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-3F

inst.name.start_with?('Handle')
... or for case insensitive convert to a title:
inst.name.capitalize.start_with?('Handle')
0
    I'm not here much anymore. But a PM will fire email notifications.
    User avatar
    Dan Rathbun 
    PluginStore Author
    PluginStore Author
     

    Re: Select instances in the selection

    Postby TNTDAVID » Sat Oct 21, 2017 6:33 pm

    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.

    Code: Select all
    m=Sketchup.active_model;s=m.selection;m.definitions.each{|d|s.add d.instances if d.name=~/#{"Handle"}/}


    Thank you
    0
    * Nouveau !!!
    Découvrez notre nouveau Plugin Click-Cuisine 2, pour créer vos cuisines 3D !
    User avatar
    TNTDAVID 
    PluginStore Author
    PluginStore Author
     

    Re: Select instances in the selection

    Postby Dan Rathbun » Sat Oct 21, 2017 8:03 pm

    TNTDAVID wrote: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.
    viewtopic.php?f=180&t=68172&p=626519#p626479

    Then 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.)

    0
      I'm not here much anymore. But a PM will fire email notifications.
      User avatar
      Dan Rathbun 
      PluginStore Author
      PluginStore Author
       

      Re: Select instances in the selection

      Postby TNTDAVID » Sun Oct 22, 2017 2:26 pm

      Thank you Dan.
      I will analyze all your information in detail. ;)
      0
      * Nouveau !!!
      Découvrez notre nouveau Plugin Click-Cuisine 2, pour créer vos cuisines 3D !
      User avatar
      TNTDAVID 
      PluginStore Author
      PluginStore Author
       

      SketchUcation One-Liner Adverts

      by Ad Machine » 5 minutes ago



      Ad Machine 
      Robot
       



       

      Return to Developers' Forum

      Who is online

      Users browsing this forum: Frankn and 7 guests

      Visit our sponsors: