• Login
sketchucation logo sketchucation
  • Login
⌛ Sale Ending | 30% Off Profile Builder 4 ends 30th September

Group components from array

Scheduled Pinned Locked Moved Developers' Forum
13 Posts 4 Posters 544 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.
  • H Offline
    HPW
    last edited by 5 May 2009, 10:38

    When I import a Autocad DWG my blocks get components in SU.
    The outliner show them in his list.
    Now I want to group some components together.
    (like manual selecting them and choose 'Make Group')
    I get it so far that I have an array of all wanted groups
    and an array for each group with the wanted components.
    With this code I create the empty groups:

    grouplist.each{|c|
       group = entities.add_group
       group.name = '<'+c.rjust(5)+'>'
    
    
      }
    
    

    The empty groups show up in the outliner at the expected positions.
    But how do I add the existing components from array to their groups?

    1 Reply Last reply Reply Quote 0
    • T Offline
      thomthom
      last edited by 5 May 2009, 10:50

      Use the array of entities as argument for .add_group.

      Error 404 (Not Found)!!1

      favicon

      (code.google.com)

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

      1 Reply Last reply Reply Quote 0
      • H Offline
        HPW
        last edited by 5 May 2009, 11:11

        But waht about the quote in the doc:

        @unknownuser said:

        NOTE: calling add_group with entities in its parameters has been known to crash SketchUp. It is preferable to create an empty group and then add things to its Entities collection.

        1 Reply Last reply Reply Quote 0
        • H Offline
          HPW
          last edited by 5 May 2009, 11:29

          Anyway I give it a try and it works nicely.

          
            grouplist.each{|c|
             str = '@my_group_list = GroupPrefix'+ c
             eval str
             group = entities.add_group(@my_group_list)
             group.name = '<['+c.rjust(5)+'>'
            }
          
          

          By the way: Is there a better way to create dynamicly array names and recall them?

          The Grouplist is an array of strings like [ "100" "200" ...]
          The Group-arrays are like 'GroupPrefix100' etc.

          1 Reply Last reply Reply Quote 0
          • J Offline
            Jim
            last edited by 5 May 2009, 11:48

            Ruby is kind of cool in that you can increment Strings.

            
            name = "Group100"
            name.next #(also .next! and .succ & .succ!)
            ==> "Group101"
            
            

            Maybe useful?

            Hi

            1 Reply Last reply Reply Quote 0
            • T Offline
              thomthom
              last edited by 5 May 2009, 12:22

              @hpw said:

              But waht about the quote in the doc:

              @unknownuser said:

              NOTE: calling add_group with entities in its parameters has been known to crash SketchUp. It is preferable to create an empty group and then add things to its Entities collection.

              Yea, I've asked about that myself, but I've yet to hear about any concrete examples where it fails. And I don't know how you would move existing geometry into a group without rebuilding it from scratch.

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

              1 Reply Last reply Reply Quote 0
              • T Offline
                TIG Moderator
                last edited by 5 May 2009, 18:29

                group=Sketchup.active_model.active_entities.add_group()
                ### now we must assume you have an array of instances...
                instances.each{|instance|
                  group.entities.add_instance(instance.definition,instance.transformation)
                  instance.erase!
                  ### remove the original instance - it's now been repeated in the group...
                }
                
                

                I think this is what you want ?

                TIG

                1 Reply Last reply Reply Quote 0
                • H Offline
                  HPW
                  last edited by 5 May 2009, 19:13

                  @unknownuser said:

                  I think this is what you want ?

                  Thanks TIG, works also nicely.
                  But what is now the better or safer solution?

                  Hans-Peter

                  1 Reply Last reply Reply Quote 0
                  • T Offline
                    TIG Moderator
                    last edited by 5 May 2009, 19:19

                    @unknownuser said:

                    But what is now the better or safer solution?
                    Why, mine of course ! 😉

                    TIG

                    1 Reply Last reply Reply Quote 0
                    • H Offline
                      HPW
                      last edited by 5 May 2009, 19:24

                      Very convincing reason!
                      😉
                      But is the warning in the doc correct of from an older version of SU.
                      Both shows me an identical result.
                      The first version is one code-line shorter.

                      1 Reply Last reply Reply Quote 0
                      • H Offline
                        HPW
                        last edited by 5 May 2009, 19:53

                        Another observation: The first solution seems much faster.
                        When I leave the outliner open and run the code, the outliner flickers as long the code run.
                        But the first is much faster than the second one.

                        1 Reply Last reply Reply Quote 0
                        • T Offline
                          TIG Moderator
                          last edited by 5 May 2009, 22:37

                          Having the Outliner open during script execution is risky as it can cause crashes when groups/instances are getting changed...

                          Adding stuff to groups immediately as they are made, in scripts can cause crashes, although I often do it - safest not to... Do whatever works for you best...

                          TIG

                          1 Reply Last reply Reply Quote 0
                          • H Offline
                            HPW
                            last edited by 6 May 2009, 05:54

                            @unknownuser said:

                            Ruby is kind of cool in that you can increment Strings.

                            Thanks Jim for the tip.

                            In this case I can not use it, since my block-numbering-steps comes from the imported autocad-blocknames.
                            But maybe it gets usefull in the future.

                            @unknownuser said:

                            Do whatever works for you best...

                            Thanks again TIG.

                            Hans-Peter

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

                            Advertisement