sketchucation logo sketchucation
    • Login
    🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Ruby code for selecting Component Instances by name

    Scheduled Pinned Locked Moved Developers' Forum
    3 Posts 2 Posters 737 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.
    • M Offline
      milleb
      last edited by

      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

      1 Reply Last reply Reply Quote 0
      • TIGT Offline
        TIG Moderator
        last edited by

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

        TIG

        1 Reply Last reply Reply Quote 0
        • M Offline
          milleb
          last edited by

          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 !!

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

          Advertisement