sketchucation logo sketchucation
    • Login
    πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Selection error?? with Ruby API

    Scheduled Pinned Locked Moved Developers' Forum
    7 Posts 5 Posters 989 Views 5 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.
    • H Offline
      hanl000
      last edited by

      Hi.

      I was working on 'Selection' with Ruby API and have a problem with it.

      
      entities=Sketchup.active_model.active_entities
      selection = Sketchup.active_model.selection
      a = entities.grep(Sketchup;;Group).each{|e|selection.add e if e.name.include?('aaa')}
      
      

      This is the code selects groups of which name has 'aaa' and it looked like all 'aaa' were selected well.

      The problem was the array 'a' contained other groups together not only 'aaa'.

      Once I tried to do next work(ex : a.hidden = true), other unnecessary groups were worked together.

      The ruby code editor said
      Done. Ruby says: [#Sketchup::Group:0x00000009e7c468, #Sketchup::Group:0x00000009e762c0, #Sketchup::Group:0x00000009e65ab0, #Sketchup::Group:0x00000009e5f868, #Sketchup::Group:0x00000009e3f040, #Sketchup::Group:0x00000009e06920, #Sketchup::Group:0x00000009e05368]

      in the array, only three things should have been selected(I put 7 objects in space. the name of 3 of 7 groups was 'aaa' and others had name of 'bbb')

      How can I exactly select only what I want?

      1 Reply Last reply Reply Quote 0
      • D Offline
        dezmo
        last edited by

        I think you were selected already what you want. πŸ˜‰
        But the "a" array is contains all groups since you were grep-ed it. Inside the iteration you have a right selection aray. You have to look for that for example like this:

        entities=Sketchup.active_model.active_entities
        selection = Sketchup.active_model.selection
        a = entities.grep(Sketchup;;Group).each{|e|selection.add e if e.name.include?('aaa')}
        
        selection.each{|e| e.hidden=true}
        

        So the "selection" will be your array of selected groups.
        (Btw. I don't think you can apply s.hidden=true you heve to iterate through each element... )

        More simple:

        entities=Sketchup.active_model.active_entities
        
        entities.grep(Sketchup;;Group).each{|e| e.hidden=true if e.name.include?('aaa')}
        

        Man can do everything. Only a matter of time and money.

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

          To make minor adjustments...
          Typo in 'ents' referencing fixed, alternative [more flexible?] testing and clearing the selection before adding matches...

          ents = Sketchup.active_model.active_entities
          sel  = Sketchup.active_model.selection
          a = ents.grep(Sketchup;;Group).find_all {|e|
            e.name =~ /aaa/
          }
          sel.clear
          sel.add(a)
          

          TIG

          1 Reply Last reply Reply Quote 0
          • thomthomT Offline
            thomthom
            last edited by

            Note to add to selecting/deselecting - if you do multiple entities make sure you collected them in arrays and pass in the array to selection.add/remove. If you call .add/.remove on each entity it'll be many times slower.

            Thomas Thomassen β€” SketchUp Monkey & Coding addict
            List of my plugins and link to the CookieWare fund

            1 Reply Last reply Reply Quote 0
            • H Offline
              hanl000
              last edited by

              Thank you for all kind replies. All my problems are solved except one :

              is there a pattern which determines the order of elements in array? or are they designated randomly in array? If there is a pattern, It will be much easier to control objects

              1 Reply Last reply Reply Quote 0
              • Dan RathbunD Offline
                Dan Rathbun
                last edited by

                You need to use a filtering method. The each method always returns the entire collection object, regardless of what you do in the iteration block.

                ents = Sketchup.active_model.active_entities
                sel  = Sketchup.active_model.selection
                
                a = ents.grep(Sketchup;;Group).find_all {|e|
                  e.name.include?('aaa')
                }
                
                sel.add(a)
                

                (Fixed: " a = ents.grep" was: " a = entities.grep"

                Array and most array-like SketchUp API collections have Ruby's Enumerable library module mixed in. So you can use any of it's methods, like find() and find_all(), etc.

                I'm not here much anymore.

                1 Reply Last reply Reply Quote 0
                • Dan RathbunD Offline
                  Dan Rathbun
                  last edited by

                  No pattern. The various ways that the user can manipulate the selection set makes any order meaningless.

                  Ie: single pick, single pick + CTRL, single pick + SHIFT, Window Right-to-Left pick, Window Right-to-Left pick + CTRL, Window Right-to-Left pick + SHIFT, Window Left-to-Right pick, Window Left-to-Right pick + CTRL, and Window Left-to-Right pick + SHIFT.

                  Any of these, followed by any other number of them, in whatever order the user may use them, creates a selection set in which the pick order has no importance.

                  That said, ...

                  You question indicates you are perhaps trying to program a workflow that would better be implemented as a Ruby Tool, using a Sketchup::PickHelper.

                  Another alternative might be a Sketchup::SelectionObserver subclass.

                  I'm not here much anymore.

                  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