• Login
sketchucation logo sketchucation
  • Login
ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

Select all comps if contain name

Scheduled Pinned Locked Moved Plugins
5 Posts 2 Posters 222 Views 2 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    CadFather
    last edited by 23 Jan 2015, 11:06

    if this code gets all comps named 'arete'

    Sketchup.active_model.selection.add( Sketchup.active_model.definitions['Arete'].instances )
    

    how can i select ALL the comps that may contain it as well? (like Arete_1, Arete_2 etc)

    1 Reply Last reply Reply Quote 0
    • T Offline
      TIG Moderator
      last edited by 23 Jan 2015, 12:41

      m=Sketchup.active_model;e=m.active_entities;s=m.selection;s.clear;a=[]; m.definitions.to_a.select{|d|d.name=~/[Aa]rete/}.each{|d|d.instances.each{|i|a<<i if i.parent.entities==e}};s.add(a)if a[0]
      

      It clears the selection first, then because it could select instances across context boundaries, thereby causing splatting issues, I use the array ' a' to select only those instances in the model.active_entities !
      The pattern-matching =~/[Aa]rete/ matches upper and lower case A, to limit it to just what's at the start use =~/^[Aa]rete/, to the end =~/[Aa]rete$/, to allow for ê or one other letter, use =~/[Aa]r.te/, or perhaps =~/[Aa]r[eê]te/

      TIG

      1 Reply Last reply Reply Quote 0
      • C Offline
        CadFather
        last edited by 23 Jan 2015, 13:00

        Wow, super comprehensive, TIG - thank you.

        are you also saying that i should not be selecting comps inside other groups etc?

        suppose you wanted to swap an instance with another, across the whole model. are you risking bugsplats?

        1 Reply Last reply Reply Quote 0
        • T Offline
          TIG Moderator
          last edited by 23 Jan 2015, 13:18

          Don't select anything unless you want to do something like Move/Copy...
          You could make a transient selection to swap definitions etc, but it is a spalt-danger...
          BUT if you want to simply get a 'collection' of all instances - e.g. to swap them - use this:

          m=Sketchup.active_model;a=[];m.definitions.to_a.select{|d|d.name=~/[Aa]rete/}.each{|d|a<<d.instances};a.flatten!;a.compact!;a
          

          The variable ' a' is an array containing all of the matching instances, irrespective of their context.
          Note how you need to flatten to merge the instances arrays, and on the safe-side compact to remove any nil values...

          TIG

          1 Reply Last reply Reply Quote 0
          • C Offline
            CadFather
            last edited by 23 Jan 2015, 13:30

            brilliant, does more than i asked! need to study it a bit now (tax return can wait...)

            Thanks 👍

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            1 / 1
            • First post
              1/5
              Last post
            Buy SketchPlus
            Buy SUbD
            Buy WrapR
            Buy eBook
            Buy Modelur
            Buy Vertex Tools
            Buy SketchCuisine
            Buy FormFonts

            Advertisement