sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    πŸ«› Lightbeans Update | Metallic and Roughness auto-applied in SketchUp 2025+ Download

    Quickly finding groups/comps in an Entity Collection?

    Scheduled Pinned Locked Moved Developers' Forum
    24 Posts 6 Posters 1.5k 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.
    • Chris FullmerC Offline
      Chris Fullmer
      last edited by

      Will that work on groups? Are they listed in the model definitions?

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

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

        Yes. In fact, Image definitions are also listed there. ComponentInstance, Groups and Images are all related.

        When you iterate model.definitions, check each definition with definition.image? and definition.group?. This is something that should be noted in the docs really. Because if you're not aware of that and process all definitions as components you may be unexpected results. You even get access to the entities of Image elements.

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

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

          Excellent, I'll look into this method for exploding internal groups and comps. I think this will be faster than parsing all the entities like I am currently doing. Thanks Thom!

          Chris

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

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

            I suspect that the .explode operation will be consuming most of the time.

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

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

              Write a def that iterates back on itself - something like this [untried]...

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

              TIG

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

                Looks good TIG. That code is significantly more compact than my code I am running. I need to spend some time and do some time tests on these various methods. On certain models I had felt that the process of finding internal groups and exploding them was the main thing slowing this script (shape bender) down. But now in my tests, I have not found a model that seems to freeze up on searching for explodeables. So now I'm sure it was such a HUGE problem as I thought it was. I did go throuh and clean out all my old typename == "Group" type comparisons for faster .class == Sketchup::Group. That alone made the script take 25% less time (from 10 seconds down to 7.5 for example). So that was a good change.

                Thanks Thom and TIG for the ideas. I'll respond here once I get around to doing some more time tests on this internal exploding business.

                Chris

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

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

                  @chris fullmer said:

                  I did go throuh and clean out all my old typename == "Group" type comparisons for faster .class == Sketchup::Group.

                  Yes - String comparisons are slooow!

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

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

                    @tig said:

                    
                    >     elsif e.class==Sketchup;;ComponentInstance
                    >       igroup=e.parent.entities.add_group(e)
                    >       e.explode
                    >       miner(igroup.entities)
                    >       igroup.explode
                    >     end#if
                    > 
                    

                    Why are you grouping the component and exploding both of them?

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

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

                      I grouped the instance so that I could then explode it keeping its entities in the group so I can find them easily - I can then look at those for further 'mining'... Once I have processed all of its entities back to raw geometry I can explode that 'igroup' group - Chris wants everything inside the 'entities set' exploded - we can't explode the definition's entities as it would affect other instances too so I explode the instance but keep its newly created entities inside that group for easier 'use'... πŸ˜•

                      TIG

                      1 Reply Last reply Reply Quote 0
                      • 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
                                            • 1
                                            • 2
                                            • 2 / 2
                                            • First post
                                              Last post
                                            Buy SketchPlus
                                            Buy SUbD
                                            Buy WrapR
                                            Buy eBook
                                            Buy Modelur
                                            Buy Vertex Tools
                                            Buy SketchCuisine
                                            Buy FormFonts

                                            Advertisement