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

    Collecting groups and components with certain material

    Scheduled Pinned Locked Moved Developers' Forum
    6 Posts 2 Posters 6.7k 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.
    • PixeroP Offline
      Pixero
      last edited by Pixero

      I’m looking for a way to make an array of all groups and components that have a certain material either on the group or the faces inside.
      If there are nested groups/components only the innermost groups containing that material should be collected.

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

        def collect_it()
          model=Sketchup.active_model
          ss=model.selection
          mats=model.materials
          @mat=mats['SomeName'] ### perhaps check if it exists ??
          @collection=[]
          ss.grep(Sketchup;;Group).each{|e|
            @collection << e if e.material==@mat
            do_group(e)
          }
          ss.grep(Sketchup;;ComponentInstance).each{|e|
            @collection << e if e.material==mat
            do_inst(e)
          }
          puts @collection
          return @collection
        end
        def do_group(e)
          e.entities.each{|e|
            @collection << e if e.respond_to?(;material) && e.material==@mat
            if e.is_a?(Sketchup;;Group)
              do_group(e)
            elsif e.is_a?(Sketchup;;ComponentInstance)
              do_inst(e)
            end
          }
        end
        def do_inst(e)
          e.definition.entities.each{|e|
            @collection << e if e.respond_to?(;material) && e.material==@mat
            if e.is_a?(Sketchup;;Group)
              do_group(e)
            elsif e.is_a?(Sketchup;;ComponentInstance)
              do_inst(e)
            end
          }
        end
        
        

        Now running collect_it() returns @collection, which includes references to all things within the selection [groups/instances AND nested-equivalents - including faces] using @mat from the name given...
        But, beware that they are in different entities collections, so some changes could BugSplat !

        TIG

        1 Reply Last reply Reply Quote 0
        • PixeroP Offline
          Pixero
          last edited by

          Wow, that was fast. Did you see my edited message about innermost groups? In this case I’m not going through a selection but looking at all groups in the model with that material.
          What I want to do is to set an attribute for all the groups and component instances ( not definition).

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

            Well !

            You need to replace ss=model.selection with ss=model.entities to iterate the whole model.

            Do you want to find only the most deeply nested things ?
            Or just those below the top-level ?

            Also I think you want to collect 'containers' as they get your attributes ??

            Seems like you need to collect the 'entities' and determine those.
            Then iterate through those and get the things using the specified material ??

            TIG

            1 Reply Last reply Reply Quote 0
            • PixeroP Offline
              Pixero
              last edited by

              Think of for example a window component which consists of the frame and other parts that can be nested groups/components. In that case I want to only collect the groups/components that have let’s say glass as material and set an attribute to them.
              Sorry I don’t understand what you mean by containers?

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

                'Containers' are groups or component-definitions, which 'contain' entities - they are usually referred to as 'entities-collections' - but 'containers' is synonymous...

                TIG

                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