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

    The component information inside the group extract

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

      Hello, I'm a beginner in Ruby's development.
      We are currently developing a Ruby to extract component information within the group.
      The component information inside the group is extracted, but the component information within the group and within the group cannot be extracted.
      Is there a way to continue searching for a child group by applying a repeat statement to get some component information?

      code :

      sel.each do |ss|
        if not ss.class == Sketchup;;Group
          UI.messagebox ('그룹을 선택해 주세요')
          return
        end
        #@group_names.push(ss.name)
        #@num_entities.push(ss.entities.length)
      
        #각 인스턴스 별 Definition 이름을 @name 배열에 넣는다.
        ss.entities.each do |e|
          if e.class == Sketchup;;ComponentInstance
            if e.definition.name.include? '-' or e.definition.name.include? '_'
              if e.definition.name.include? '~'
                ename = e.definition.name.gsub!('~','')
              else
                ename = e.definition.name
              end
              if ename.include? '#'
                @names.push(ename.split('#')[0].strip)
              else
                @names.push(ename)
              end
            end
          elsif e.class == Sketchup;;Group
            e.entities.each do |ee|
              if ee.definition.name.include? '-' or ee.definition.name.include? '_'
                if ee.definition.name.include? '~'
                  ename = ee.definition.name.gsub!('~','')
                else
                  ename = ee.definition.name
                end
                if ename.include? '#'
                  @names.push(ename.split('#')[0].strip)
                else
                  @names.push(ename)
                end
              end
            end
          end
        end #ss.each에 대한 END
        #@name의 배열 순서를 정렬한다.
        @names = @names.sort!
      
        #@name의 배열을 같은 내용을 묶어 해시로 통합한다.
        hash_overlap = @names.each_with_object(Hash.new(0)) { |word,counts| counts[word] += 1 }
      
      
        @comp_names = (hash_overlap.to_a)
      
      
          #comp_names의 내용을 h, b, t1, t2, length별로 분류한다.
          @comp_names.each do |str|
            num = str[0].split('-')[1].strip
            i = 0
            while i < num.count('X') + 1
                str.push(num.split('X')[i].strip)
                i = i + 1
            end
            #str.push(num.split('X')[1].strip)
            #str.push(num.split('X')[2].strip)
            #str.push(num.split('X')[3].strip)
            #str.push(num.split('X')[4].strip)
            end
      
        @hash_names.store(ss.name, @comp_names)
        @names = []
        hash_overlap = {}
      end #sel.each에 대한 end
      

      Group.png

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

        I reformatted your code using [ code ] rather than [ruby ] tags.
        Your Korean strings are also confusing 😉
        Here's the outline of a way... missing out the 'naming tests' you seemed to use...

        
        def start_process()
          @model=Sketchup.active_model
          @ss=@model.selection
          @hash={}
          @ss.grep(Sketchup;;Group).each{|group|
            do_group(group)
          }
          p @hash ### output or more processing ?
        end
        
        def do_group(group)
          name=group.name
          name=group.definition.name if name.empty?
          @hash[name]=[]
          group.entities.each{|e|
            if e.is_a?(Sketchup;;Group)
              e.entities.each[|ee| do_group(group) }
            elsif e.is_a?(Sketchup;;ComponentInstance)
              e.definition.entities.each{|ee|
                if ee.is_a?(Sketchup;;Group)
                  ee.entities.each{|eee| do_group(eee) }
                elsif ee.is_a?(Sketchup;;ComponentInstance)
                  @hash[name] << ee.definition.name
                end
              }
            end
          }
        end
        
        

        Run it using start_process()
        You should get @hash returned as a hash divided up by group name, with each value an array of nested component names...

        TIG

        1 Reply Last reply Reply Quote 0
        • T Offline
          themonty
          last edited by

          Thank you for your answer.
          As you execute the code you sent, Error: #SystemStackError: stack level too deep]
          There's a message.

          I will upload the sketchup file.

          I want to extract the name of the Component Instance Definition in each group.


          Bug_2.skp

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

            I haven't tested the code - I gave it as a simple example of iterative methods.
            There's probably a typo in it - the full error-message would point you to the faulty line...

            Tested: typo in line#16
            e.entities.each[|ee| do_group(group) }
            should be:
            e.entities.each{|ee| do_group(group) }

            There's still an error - I'm looking into it now...

            TIG

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

              I think this does what you want:

              def start_process()
                @model=Sketchup.active_model
                @ss=@model.selection
                @coll={}
                @ss.grep(Sketchup;;Group).each{|group|
                  name=group.name
                  name=group.definition.name if name.empty?
                  @coll[name]={}
                  do_group(group, name)
                }
                puts @coll ### output or more processing ?
              end
              
              def do_group(group, name)
                @coll[name]={} unless @coll[name]
                group.entities.each{|e|
                  if e.is_a?(Sketchup;;Group)
                    nname=e.name
                    nname=e.definition.name if nname.empty?
                    @coll[name][nname]={} unless @coll[name][nname]
                    e.entities.grep(Sketchup;;Group).each{|g|
                      dname=g.name
                      dname=g.definition.name if dname.empty?
                      @coll[name][nname][dname]={}  unless @coll[name][nname][dname]
                      do_group(g, dname)
                    }
                    e.entities.grep(Sketchup;;ComponentInstance).each{|c|
                      dname=c.definition.name
                      @coll[name][nname]['compos']=[] unless @coll[name][nname]['compos']
                      @coll[name][nname]['compos'] << dname
                    }
                  elsif e.is_a?(Sketchup;;ComponentInstance)
                    dname=e.definition.name
                    @coll[name]['compos']=[] unless @coll[name]['compos']
                    @coll[name]['compos'] << dname
                  end
                }
              end
              #start_process()
              

              It returns a hash of the (nested) groups, and a hash entry 'compos' which is an array of the nested component-instances...

              TIG

              1 Reply Last reply Reply Quote 0
              • T Offline
                themonty
                last edited by

                Thank you for your help.
                It works perfectly.
                Thank you again.

                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