sketchucation logo sketchucation
    • Login
    πŸ›£οΈ Road Profile Builder | Generate roads, curbs and pavements easily Download

    Explode group, retrive all entities and recreate group

    Scheduled Pinned Locked Moved Developers' Forum
    14 Posts 4 Posters 1.3k 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

      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

        "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
        • TIGT Offline
          TIG Moderator
          last edited by

          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

            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

              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

                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
                • TIGT Offline
                  TIG Moderator
                  last edited by

                  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

                    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
                    • TIGT Offline
                      TIG Moderator
                      last edited by

                      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

                        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

                          @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
                          • First post
                            Last post
                          Buy SketchPlus
                          Buy SUbD
                          Buy WrapR
                          Buy eBook
                          Buy Modelur
                          Buy Vertex Tools
                          Buy SketchCuisine
                          Buy FormFonts

                          Advertisement