• Login
sketchucation logo sketchucation
  • Login
πŸ”Œ Quick Selection | Try Didier Bur's reworked classic extension that supercharges selections in SketchUp Download

Explode group, retrive all entities and recreate group

Scheduled Pinned Locked Moved Developers' Forum
14 Posts 4 Posters 1.2k Views 4 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.
  • N Offline
    njeremy2
    last edited by 3 May 2012, 13:32

    Hello everyone, I need you πŸ˜„

    I have this code :

    def DefToName
    @tab = []
    @tabgroup = []

    #retrive all entities
    Sketchup.active_model.entities.each do |entities|
    # put all entities into TAB called @tab
    @tab << entities

    	@tab.each do |element|
    	%(#00FF00)[# If the entity is a group, explode the group]
    		if element.is_a?(Sketchup::Group)
    		element.explode
    

    %(#FF0000)[# I need to retrives all entities from this group HERE,
    # If I put this code : "Sketchup.active_model.entities.each do |entities|" it takes all entities, and I just need only entities from the group
    # after explosion, I'll do actions on this entities (ComponentInstance or Group)
    # After actions, I need to recreate the group with all the entities from this group.
    # For recreate the group, I have to pick the name of group before explosion into @name for example,
    # Then, when I need to recreate the group, I put that name into the new name of group
    ]

    		end %(#00FF00)[#is_a?]
    	end %(#00FF00)[#tab.each]
    

    end #|entities|
    end # end DefToName

    Thank you πŸ˜„

    1 Reply Last reply Reply Quote 0
    • T Offline
      TIG Moderator
      last edited by 3 May 2012, 13:44

      Use a lowercase letter for the start of a def method.

      You method will not be robust.
      If you get a list of the group's entities when you explode it, they might not all exist when you want to come to recreate it - coincident edges/faces can merge.

      Why do you need to explode and remake each group in the model ?
      Why not simply iterate the model.entities and for each group found you then do something to the group.entities, iterating that for other groups or whatever... no explode no remake needed πŸ˜•

      Sketchup.active_model.entities.each{|g|
        next unless g.is_a?(Sketchup;;Group)
        g.entities.each{|e|
          next unless e.is_a?(Sketchup;;Group)
          puts e
          # or whatever you want to do with 'e'
        }
      }
      

      TIG

      1 Reply Last reply Reply Quote 0
      • N Offline
        njeremy2
        last edited by 3 May 2012, 13:58

        Hmmm,
        It seems to be good, I will try to execute it

        I have a group which contains components. And with this component, I need to copy definition name into name (the copy works thanks to you. Thank to you TIG!!).
        So, to access to this component for doing modification, I tought I need to explode it.

        You just tell me it's possible to make modification without exploding groups.

        I will try this and I tell you if it's work or not. πŸ˜‰

        1 Reply Last reply Reply Quote 0
        • N Offline
          njeremy2
          last edited by 3 May 2012, 14:00

          How do you use "next unless" ?

          I search it into "Automatic_SketchUp.pdf", I found nothing

          1 Reply Last reply Reply Quote 0
          • K Offline
            kwalkerman
            last edited by 3 May 2012, 14:07

            "next unless" is ruby code - no Sketchup needed. TIG's code basically says "unless g is a Sketchup::Group, move on to the next item in each. It is functionally the same as the following, but reads a little cleaner:

            
            Sketchup.active_model.entities.each{|g|
              if g.is_a? Sketchup;;Group
                g.entities.each{|e|
                  if g.is_a? Sketchup;;Group
                    puts e
                    # or whatever you want to do with 'e'
                  end
                }
              end
            }
            
            
            1 Reply Last reply Reply Quote 0
            • T Offline
              TIG Moderator
              last edited by 3 May 2012, 14:30

              When you do a 'test' you can do it like this
              ` if e.is_a?(Sketchup::Group)

              do stuff

              end#ifif you want to negate the match use if not e.is_a?(Sketchup::Group)

              do stuff

              end#ifor if ! e.is_a?(Sketchup::Group)

              do stuff

              end#ifthere is the opposite of 'if' which is 'unless', so unless e.is_a?(Sketchup::Group)

              do stuff

              end#unless`
              where 'unless...' is the same as 'if not...'

              Now when you make a if...end or unless...end block and you nest other blocks of code within those it's easy to loose track of which 'end' ends what!
              I prefer to use a one line test to skip or jump out of a block.
              ` es.each{|e|
              next unless e.is_a?(Sketchup::Group)

              not a group so skip to the next item

              OR we do stuff to e because it IS a group!

              }the next unless e.is_a?(Sketchup::Group)is the same as next if not e.is_a?(Sketchup::Group)You don't need an 'end' to a single line test where it follows the action. The 'next' simply skips to the next item in the array/list. Using 'break' stops the iteration - useful if you are testing for ONE thing and you have found it... like g=nil
              es.each{|e|
              if e.is_a?(Sketchup::Group) and e.name=="FOO"
              g=e
              break
              end#if
              }
              puts g if g`
              which sets 'g' to nil, but then makes it a reference to ONE group named 'FOO' if it's found in the tested list, if found it breaks out of the {} and 'puts' it... Note how the 'g' is defined outside of the block {} so you can use it later, references initiated inside a {} are not visible to later processes.

              One tip more... you can iterate an 'entities' collection just as it it were an array, BUT if you intend to do anything to the items in the collection, like erase some, then you must convert it into a snapshot array as you start because otherwise your changes will change the entities collection as you iterate through it and give unexpected results...
              So use
              group.entities.to_a.each{|e| e.erase! unless e.is_a?(Sketchup::Edge) }
              to erase everything in a group that is not an Edge - i.e. all faces and groups etc.
              group.entities.each{|e| e.erase! unless e.is_a?(Sketchup::Edge) }
              will fail to erase everything as the list will change dynamically as you change it, and so some might get missed.

              TIG

              1 Reply Last reply Reply Quote 0
              • N Offline
                njeremy2
                last edited by 3 May 2012, 14:33

                Hmmm, I will meditate on this.
                It's a new notion for me, let me a little time to understand it

                now, I try the code

                [edit]
                WOOOOOW TIG !

                I don't have time enough to read all answer and you wrote that !! 😲

                1 Reply Last reply Reply Quote 0
                • N Offline
                  njeremy2
                  last edited by 3 May 2012, 14:45

                  lol !
                  wowowowo!
                  I have 2 bigs difficulties :
                  The first one is to understand correctly the English πŸ˜„ (I can manage that)
                  The second one is to understand Ruby and SketchUp language subtleties ---> AAAHHHH 🀒

                  Thanks

                  1 Reply Last reply Reply Quote 0
                  • N Offline
                    njeremy2
                    last edited by 3 May 2012, 15:09

                    I try this code

                    Maybe I don't know how to use that...

                    What I need to do is to retrive all entities, then I select what I need to do if it's a group or if it's a component.

                    If it's a component, I copy the DefinitionName into Name.
                    If it's a group, I need to copy DefinitionName into Name for all components into the group

                    The problem : I don't know how get into the group then copy the definition without exploding this

                    code for copying definition into name

                    if	element.definition.name[0] != 0 && element.name == ""
                    element.name = "#{element.definition.name}"
                    end # end if
                    
                    1 Reply Last reply Reply Quote 0
                    • T Offline
                      TIG Moderator
                      last edited by 3 May 2012, 19:40

                      You seem to be making this too complex...
                      Your last piece of code seems to make no sense πŸ˜•

                      Every definition has a unique name that is a 'string'.

                      Please try and explain [simply] what you want to do - in words, not code...
                      It IS straightforward to find definition/instance names, BUT what do you want and what will you ultimately do with them ???

                      TIG

                      1 Reply Last reply Reply Quote 0
                      • N Offline
                        njeremy2
                        last edited by 3 May 2012, 20:52

                        I'm a student in computer science into a small business. My boss use Sketchup Pro and he asks me do make a plugin which count all entities in a design.

                        It tells you how many components and groups are they in the design.
                        We have already use that plugin but there are some bugs.
                        If we don't put a name into the component, the plugin display nothing (that's why I ask you how to copy definition name into name).
                        The other problem is : when you have a group, into that group you can have some components and the plugin didn't count these components, that's why I ask how to get into a group without exploding this.

                        Voila πŸ˜„

                        1 Reply Last reply Reply Quote 0
                        • T Offline
                          TIG Moderator
                          last edited by 3 May 2012, 21:50

                          OK !
                          Let's start at the other end... πŸ˜’
                          Rather than try and mine into the model, shortcut it by examining definitions...

                          A model has definitions.
                          These refer to components, groups and images.
                          Definitions have instances methods, so you can count how many there are etc.
                          This example code iterates through all of the model's definitions, list their type, name, instances [with name and parent] etc...

                          model=Sketchup.active_model
                          collection=[]
                          model.definitions.each{|defn|
                            details=[]
                            if defn.image?
                              details << "IMAGE"
                            elsif defn.group?
                              details << "GROUP"
                            else #component
                              details << "COMPO"
                            end
                            if defn.image?
                              details << defn.path #images have no name
                            else
                              details << defn.name
                            end
                            insts=[]
                            defn.instances.each{|ins|
                              inst=[]
                              if defn.image?
                                inst << '' #image instances have no name
                              else
                                inst << ins.name
                              end
                              par=ins.parent
                              if par==model
                                inst << "MODEL"
                              else
                                inst << par.name
                              end
                              insts << inst
                            }
                            details << insts
                            collection << details
                          }
                          collection.sort!
                          puts collection
                          

                          OR write [puts] 'collection' into a file etc...
                          The format is
                          COMPO, Compo#1, , Group#1 COMPO, Compo#1, Cxxxxxx, MODEL GROUP, Group#1, MyGroup, MODEL GROUP, Group#2, , Compo#1 IMAGE, C:/Temp/My.png, , MODEL
                          etc
                          the empty fields ', ,' is where an instance or group has no name assigned.

                          TIG

                          1 Reply Last reply Reply Quote 0
                          • Dan RathbunD Offline
                            Dan Rathbun
                            last edited by 3 May 2012, 22:21

                            Does your boss ever use the
                            http://forums.sketchucation.com/download/file.php?id=87554
                            Model Info > Statistics panel ??

                            ModelInfo_Statistics.png


                            ModelInfo_button.png

                            I'm not here much anymore.

                            1 Reply Last reply Reply Quote 0
                            • N Offline
                              njeremy2
                              last edited by 4 May 2012, 07:02

                              @dan rathbun said:

                              Does your boss ever use the
                              http://forums.sketchucation.com/download/file.php?id=87554
                              Model Info > Statistics panel ??

                              [attachment=1:1ebrpxvl]<!-- ia1 -->ModelInfo_Statistics.png<!-- ia1 -->[/attachment:1ebrpxvl]

                              Yes he knows this tool, but it don't give you the texture (in m²) and of the components neither groups 😞

                              thanks to all everybody !!! I think I found the solution. πŸ‘

                              When I need to found each entities in a group I use that :

                              if g.is_a?(Sketchup;;Group)				  
                              	g.entities.each do |element|
                              		if element.definition.name[0] != 0 && element.name == ""
                              		element.name = "#{element.definition.name}"
                              		end # end if
                              	end #|element|
                              
                              

                              With that, I can change the name of component inside a group

                              The first code of TIG gave me an idea, I combine my code with a piece of code from him and it worked ! (it was only a sample of code used to test for me)
                              Now I have to copy that piece of code in the big one ! πŸ˜„

                              Before :

                              http://img205.imageshack.us/img205/1108/33890297.png

                              After launching the plugin :

                              http://img585.imageshack.us/img585/7154/18942418.png

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

                              Advertisement