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

Iterate through nested groups/components to get edges

Scheduled Pinned Locked Moved Developers' Forum
8 Posts 2 Posters 6.4k 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.
  • P Offline
    Pixero
    last edited by 14 Mar 2019, 18:16

    I want to get all edges from a selection of one or several nested groups or components.
    How?

    1 Reply Last reply Reply Quote 0
    • P Offline
      Pixero
      last edited by 15 Mar 2019, 13:16

      A little further but still not working...
      I have attached a simple test scene but only the "free" edges in the first level gets selected.

      mod = Sketchup.active_model
      sel = mod.selection
      ent = mod.active_entities
      edges = [] 
              
      Sketchup.active_model.selection.each{|e|
          if e.is_a?(Sketchup;;Edge) #This works
            edges << e 
            
          elsif e.is_a?(Sketchup;;Group)
            e.entities.each{|edge|
              next unless edge.is_a?(Sketchup;;Edge)      
              edges << edge 
              }
            
           elsif e.is_a?(Sketchup;;ComponentInstance)
              e.definition.entities.each{|edge|
              next unless edge.is_a?(Sketchup;;Edge)      
              edges << edge       
              }        
           end
      }
      sel.clear
      sel.add(edges)
      

      Testscene

      1 Reply Last reply Reply Quote 0
      • T Offline
        TIG Moderator
        last edited by 15 Mar 2019, 13:47

        To process the selection you 'run' it...

        
        def mine_edges(ents)
          @edges << ents.grep(Sketchup;;Edge)
          ents.grep(Sketchup;;Group).each{|gp|
            @edges << mine_edges(gp.entities)
          }
          ents.grep(Sketchup;;ComponentInstance).each{|co|
            @edges << mine_edges(co.definition.entities)
          }
        end
        def run()
          mod = Sketchup.active_model
          sel = mod.selection
          @edges = []
          mine_edges(sel)
          @edges.flatten! ### combine sub-arrays into one array
          sel.clear
          sel.add(@edges)
        end
        
        

        Why do you want to 'select' all these nested edges ?
        you cannot do much to manipulate selections that span different entities without a BugSplat, however, if you change the last line to do something to the @edges array - e.g. remove their materials or layers, then than is acceptable...

        TIG

        1 Reply Last reply Reply Quote 0
        • P Offline
          Pixero
          last edited by 15 Mar 2019, 15:23

          This is just a start of a script and one thing I want to do is to hide all the edges in nested groups and components all the way down.
          Selecting is just to see if it works.

          I ran your code but it also selecting the sub groups and components.

          1 Reply Last reply Reply Quote 0
          • T Offline
            TIG Moderator
            last edited by 15 Mar 2019, 16:12

            I thought that's what you wanted, otherwise your earlier code should select all edges in the active_entities AND within the groups/instances in that context.
            My code collects ALL edges in the context and any edges within any groups/instances in that context AND nested deeper: it should not be selecting anything more than edges ?

            So instead of selecting the collected edges you just need to iterate @edges and set each edge.hidden=true ?

            TIG

            1 Reply Last reply Reply Quote 0
            • P Offline
              Pixero
              last edited by 15 Mar 2019, 16:21

              This is a screenshot after running your code.
              As you can see the inner components and groups are also selected.

              Skärmklipp.JPG

              1 Reply Last reply Reply Quote 0
              • T Offline
                TIG Moderator
                last edited by 15 Mar 2019, 16:37

                I do see it, and if you type @edges after it's run the list does include some component-instances.
                This is weird since the code specifically collects just Edges !
                BUT it's because the ' miner' was exiting without a specific 'empty' return statement, and therefore it also added the last thing it processed == the instances etc.
                By forcing it to return [] [an empty array], then that is lost by the @edges.flatten! command at the end - so it now works as expected - @edges is an array of only edges...
                Here's the fixed code...

                
                def mine_edges(ents)
                  @edges << ents.grep(Sketchup;;Edge)
                  ents.grep(Sketchup;;Group).each{|gp|
                    @edges << mine_edges(gp.entities)
                  }
                  ents.grep(Sketchup;;ComponentInstance).each{|co|
                    @edges << mine_edges(co.definition.entities)
                  }
                  return []
                end
                def run()
                  mod = Sketchup.active_model
                  sel = mod.selection
                  @edges = []
                  mine_edges(sel)
                  @edges.flatten! ### combine sub-arrays into one array
                  sel.clear
                  sel.add(@edges)
                end
                
                

                TIG

                1 Reply Last reply Reply Quote 0
                • P Offline
                  Pixero
                  last edited by 15 Mar 2019, 16:49

                  Thank you. That works as I wanted.

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

                  Advertisement