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

    Quickly finding groups/comps in an Entity Collection?

    Scheduled Pinned Locked Moved Developers' Forum
    24 Posts 6 Posters 1.3k Views 6 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.
    • J Offline
      Jim
      last edited by

      If I understand what you want to do, you are making it harder than it is.

      1. Search the tree and build a list of things to explode.
      2. Then go through the list and explode them.
      
      def traverse_entities(entities, list)
          entities.each do |entity|
              if entity.class == Sketchup;;Group
                  traverse_entities(entity.entities, list)
                  list.push(entity)
              elsif enitity.class == Sketchup;;ComponentInstance
                  traverse_entities(entity.definition.entities, list)
                  list.push(entity)
              end
          end
      end
      
      def explode_selected
          list = []
          traverse_entities(Sketchup.active_model.selection, list)
          list.each { |entity| entity.explode }
      end
      
      

      I think TIG's would work like this if you don't need a list or general purpose traversal; which is essentially the same thing but more specialized.

      
          def exploder(ents)
            ents.to_a.each{|e|
              if e.class==Sketchup;;Group
                exploder(e.entities)
                e.explode
              elsif e.class==Sketchup;;ComponentInstance
                exploder(e.definition.entities)
                e.explode
              end
            }
          end
          ### usage...
          exploder(my_entities)
      
      

      The tendency is to perform the explode before the recursion. Go ahead and do the recursion first, and defer the explode until after the recursion returns.

      The result is that the most deeply nested objects get exploded first, and then works up the hierarchy. The last object exploded is then the first object found.

      Hi

      1 Reply Last reply Reply Quote 0
      • C Offline
        cjthompson
        last edited by

        @jim said:

        If I understand what you want to do, you are making it harder than it is.
        The result is that the most deeply nested objects get exploded first, and then works up the hierarchy. The last object exploded is then the first object found.

        wouldn't you want to explode top > down instead of bottom > up? that way, you don't explode definitions of instances in other Entities.

        or was Chris thinking of exploding the whole model?

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

          My version was to explode only groups and instances within a set of entities [which I understood to be the aim] and any groups/instances found within them - definition contents shouldn't change?]. To avoid confusing arrays of things to explode I think it's best to work from the lowest level up and explode these first, isn't it ?

          TIG

          1 Reply Last reply Reply Quote 0
          • C Offline
            cjthompson
            last edited by

            @tig said:

            My version was to explode only groups and instances within a set of entities [which I understood to be the aim] and any groups/instances found within them - definition contents shouldn't change?].

            Since you are exploding bottom to top, the top level definitions will change before they are exploded.

            here is a method that will explode them top to bottom:

            
            def miner(ents)
            	ents.each{|e|
            		if e.class==Sketchup;;Group
            			miner(e.explode)
            		elsif e.class==Sketchup;;ComponentInstance
            			miner(e.explode)
            		end#if
            		}#end each
            end#def
            
            

            EDIT: I just realized that's what the group is for. 😳

            1 Reply Last reply Reply Quote 0
            • J Offline
              Jim
              last edited by

              @jim said:

              If I understand what you want to do,

              Clearly, I do not. πŸ˜†

              Hi

              1 Reply Last reply Reply Quote 0
              • Chris FullmerC Offline
                Chris Fullmer
                last edited by

                Hehe, great thread. Yes, I want to explode everything inside a component. IT is possible that it might have componeonts inside it, that are used elsewhere and they should not have all their internals exploded. So I think if you work from deepest level to uppermost level, you have to do some trickery to not explode the stuff that exists in other instances outside mine.

                I would think more Jim and Chris on this and explode before recursing. And ther is no double exploding of any component. That way the component is exploded and there is no chance of affecting its siblings outside in the rest of the model.

                All in all, plenty of giid feedback and SIGNIFICANTLY simpler code than my silly piece I put togther 😳

                Chris

                Lately you've been tan, suspicious for the winter.
                All my Plugins I've written

                1 Reply Last reply Reply Quote 0
                • J Offline
                  Jim
                  last edited by

                  @cjthompson said:

                  Since you are exploding bottom to top, the top level definitions will change before they are exploded.

                  here is a method that will explode them top to bottom:

                  Yes, that's logical. It explodes Groups and Instances but does not decimate Definitions like I did. Thanks.

                  def miner(ents)
                      ents.each do |e|
                         if e.class==Sketchup;;Group or e.class==Sketchup;;ComponentInstance
                             miner(e.explode) 
                         end
                      end
                  end
                  
                  
                  

                  Hi

                  1 Reply Last reply Reply Quote 0
                  • M Offline
                    MartinRinehart
                    last edited by

                    Is it guaranteed that groups/instances are a tree structure?

                    One cycle (A is a member of B; B is, directly or indirectly, a member of A) turns a recursive function into an infinite loop.

                    Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

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

                      @martinrinehart said:

                      Is it guaranteed that groups/instances are a tree structure?

                      One cycle (A is a member of B; B is, directly or indirectly, a member of A) turns a recursive function into an infinite loop.

                      No - SU does not allow that.

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

                      1 Reply Last reply Reply Quote 0
                      • C Offline
                        cjthompson
                        last edited by

                        this thread reminds me of this:
                        http://refactormycode.com/codes/2-ruby-simple-loop
                        with each consecutive code sample improving on the last πŸ˜†

                        although the only improvement I could possibly add might be:

                        
                        def miner(ents)
                            for e in ents
                               if e.class==Sketchup;;Group or e.class==Sketchup;;ComponentInstance
                                   miner(e.explode) 
                               end
                            end
                        end
                        
                        1 Reply Last reply Reply Quote 0
                        • TIGT Offline
                          TIG Moderator
                          last edited by

                          Or to compact it as much as I can see, into a one-liner

                          
                          def xis(a);a.each{|e|xis(e.explode)if e.class==(Sketchup;;Group||Sketchup;;ComponentInstance)};end
                          

                          "xis" == e Xplode Instance S
                          Usage: xis(entities)
                          πŸ˜‰
                          PS: a shorter way to do their puts 1 to 10 is this
                          n=0;p n+=1while n<10
                          πŸ˜’ ❓

                          TIG

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

                            I take a few extra lines for readability any day.

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

                            1 Reply Last reply Reply Quote 0
                            • J Offline
                              Jim
                              last edited by

                              @thomthom said:

                              I take a few extra lines for readability any day.

                              True, but "squish-the-code" is a fun exercise and I often learn something new.

                              
                              def xis(a);a.map{|e|xis(e.explode)if e.respond_to?('move!')};end
                              
                              

                              (assuming no one has added move! to any of the built-in classes!)

                              Hi

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

                              Advertisement