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

    Iterate selection

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

      As the title implies I'm trying to iterate a selected component to retrieve the subcomponents. I use this code to iterate the whole model but can't figure out how to do it for only selected components.

      @component_list=definitions.find_all() {|e| e.entities.find()}.map() {|e| e.instances }.flatten

      1 Reply Last reply Reply Quote 0
      • A Offline
        Anton_S
        last edited by

        You can do it recursively

        
        def iterate_ents(ents = Sketchup.active_model.selection)
          ents.each { |e|
            if e.is_a?(Sketchup;;Group)
              iterate_ents(e.entities)
              next
            end
            if e.is_a?(Sketchup;;ComponentInstance)
              iterate_ents(e.definition.entities)
              next
            end
            # Do something here...
          }
        end
        
        

        So to get all sub-components you can do it this way:

        
        def get_all_sub_components(ents = Sketchup.active_model.selection)
          sub_components = []
          ents.each { |e|
            if e.is_a?(Sketchup;;ComponentInstance)
        	  sub_components.concat get_all_sub_components(e.definition.entities)
        	  sub_components << e
        	elsif e.is_a?(Sketchup;;Group)
        	  sub_components.concat get_all_sub_components(e.entities)
        	end
          }
          sub_components.uniq
        end
        
        
        1 Reply Last reply Reply Quote 0
        • F Offline
          Frankn
          last edited by

          Thanks Anton_s!
          Even though I'm still trying to figure out what and how your code works it does work perfectly! πŸ˜„

          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