sketchucation logo sketchucation
    • Login
    โ„น๏ธ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Global Layer Name Change

    Scheduled Pinned Locked Moved Developers' Forum
    13 Posts 3 Posters 447 Views 3 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.
    • Bob JamesB Offline
      Bob James
      last edited by

      Is there a ruby to change the names of several layers at once. I have a main model into which I put different customer equipment. The customer equipment layer names begin with the customer name. When I'm dealing with a new customer and some of the equipment is basically the same, I want to change all of the names. For example,

      Jones - Tug
      Jones - Trailer
      Jones - Tractor
      Jones - 5th Wheel Dolly

      I want to be able to quickly change it to:
      Adamson - Tug
      Adamson - Trailer
      Adamson - Tractor
      Adamson - 5th Wheel Dolly

      Or if I've developed a new model and want to re-organize the layers with prefixes.

      i7-4930K 3.4Ghz, 2x GTX780 6GB, 32GB DDR3-1600 ECC, OCZ Vertex 4 500GB, WD Black 3TB, 32TB NAS, 4x 27" Monitors, SpaceMouse Pro, X-keys XK-60

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

        @bob james said:

        Is there a ruby to change the names of several layers at once. I have a main model into which I put different customer equipment. The customer equipment layer names begin with the customer name. When I'm dealing with a new customer and some of the equipment is basically the same, I want to change all of the names. For example,
        Jones - Tug
        Jones - Trailer
        Jones - Tractor
        Jones - 5th Wheel Dolly
        I want to be able to quickly change it to:
        Adamson - Tug
        Adamson - Trailer
        Adamson - Tractor
        Adamson - 5th Wheel Dolly
        Or if I've developed a new model and want to re-organize the layers with prefixes.

        There is now...

        ### This changes all matching layer names
        ### You simply change the values of txt1="" and txt2="" to suit...
        txt1="Jones";txt2="Adamson";Sketchup.active_model.layers.each{|layer|layer.name=layer.name.gsub(txt1,txt2)}
        ### paste the line of text "txt1=...txt2)}" into the Ruby console
        ### and then <enter> to make the changes...
        
        

        It's very simplistic - it's easily expanded to do more or make it into a proper plugin with a menu item etc - but it's occasional use perhaps favours this method...
        To strip off a prefix use something like "txt1="Jones - "; txt2="";...layer.name=layer.name.gsub(txt1,txt2)}"...
        To add an additional prefix use something like "txt_prefix="XXXX-"; ...layer.name=txt_prefix+layer.name}"...

        TIG

        1 Reply Last reply Reply Quote 0
        • Bob JamesB Offline
          Bob James
          last edited by

          Many thanks, TIG. I don't know anything about ruby language (that is, I don't know how to program in ruby), but I'll sure give it a try.

          The real layer names are more like:

          Aqua - Payload Vertical
          Aqua - Payload Horizontal
          Aqua - Tilt Fixture
          Aqua - Simple Stand
          etc......
          Walls
          Floor
          Ceiling
          etc......
          Those with a prefix are unique to the current scenario: those without a prefix are "standard" for all scenarios.

          So, if I understand your approach, I would first strip off the prefixes, then add the new one.

          "txt1 = "Aqua -"; "txt2=""; layer.name=layer.name.gsub(txt1,txt2)]"
          Would this strip the Aqua prefix and ignore those without that prefix?
          How would I then change the prefix (to only those that had the Aqua prefix before) to "Jason"?
          How would it deal with layers that did not have a prefix (or had some other prefix)?

          Are there some ruby commands that would do something like (with txt_oldprefix and txt_newprefix being inputs):
          txt_oldprefix = "Aqua"
          txt_newprefix = "Jason"
          LOld = Len(txt_oldprefix)
          LName = Length(layer.name)
          If (left(layer.name,LOld) = txt_oldprefix,then layer.name = txt_newprefix + right(layer_name,LName-LOld))

          Maybe I'm just too dense and should continue going through each layer name and change it by cut and add. It's just such a bummer when there are twenty-something affected layers ๐Ÿ˜’

          i7-4930K 3.4Ghz, 2x GTX780 6GB, 32GB DDR3-1600 ECC, OCZ Vertex 4 500GB, WD Black 3TB, 32TB NAS, 4x 27" Monitors, SpaceMouse Pro, X-keys XK-60

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

            ### This changes all matching layer names
            ### You simply change the values of txt1="" and txt2="" to suit...
            txt1="Aqua";txt2="Jason";Sketchup.active_model.layers.each{|layer|layer.name=layer.name.gsub(txt1,txt2)}
            ### paste the line of text "txt1=...txt2)}" into the Ruby console
            ### and then <enter> to make the changes...
            
            

            This replaces [gsub] any 'Aqua - ' text with 'Jason - ' - thus all of the prefixes are changed and other layers are left alone... You are most unlikely to have the txt1 inside the rest of a layer's name so that is safe...

            You Len and Left is 'basic' - ruby has other tools... if anything you are making things more complicated !

            IF you wanted ALL layers to have the 'Jason' prefix then I recommend doing it in two steps - a bit like you suggest...
            Step 1: ...txt1="Aqua - "; txt2=""... ###that will remove all of the prefixes.
            Step 2: ...prefix="Jason - "...layer.name=prefix+layer.name... ###that will add the prefix to all layers...

            TIG

            1 Reply Last reply Reply Quote 0
            • Bob JamesB Offline
              Bob James
              last edited by

              Thanks again, TIG.
              I wasn't trying to make it more complicated: I used to program in Basic, Fortran IV and Algol (guess that dates me ๐Ÿ˜Ž ๐Ÿ˜ฎ) and, more recently, making macros in Excel and Access. It was the only way I knew how to try to communicate what I needed.

              Again, thank you.

              One, hopefully last, dumb guy question: do I make what you gave me into a ruby (Rename_layers.rb) and then type the other into the ruby console?

              i7-4930K 3.4Ghz, 2x GTX780 6GB, 32GB DDR3-1600 ECC, OCZ Vertex 4 500GB, WD Black 3TB, 32TB NAS, 4x 27" Monitors, SpaceMouse Pro, X-keys XK-60

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

                If you make a ruby script you'll need to make a definition and give it two arguments txt1 and txt2 etc... Easier is the the single line that can be copy/pasted into the Ruby Console + <enter> [with the txt1=/txt2= adjusted as desired]...

                txt1="Aqua";txt2="Jason";Sketchup.active_model.layers.each{|layer|layer.name=layer.name.gsub(txt1,txt2)}
                

                copy all of this line of code...

                TIG

                1 Reply Last reply Reply Quote 0
                • Bob JamesB Offline
                  Bob James
                  last edited by

                  Gave it a try. This is what I got:

                  txt1="3 Studio";txt2="3 Wood Room";Sketchup.active_model.layers.each{|layer|layer.name=layer.name.gsub(txt1,txt2)}
                  #Sketchup::Layers:0x206dd8d0

                  I don't know what that second line means, but the layer names were changed
                  I'm amazed that that simple command did the trick!

                  Thank you Ruby Master TIG ๐Ÿ‘

                  i7-4930K 3.4Ghz, 2x GTX780 6GB, 32GB DDR3-1600 ECC, OCZ Vertex 4 500GB, WD Black 3TB, 32TB NAS, 4x 27" Monitors, SpaceMouse Pro, X-keys XK-60

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

                    The #Sketchup::Layers:0x206dd8d0 is simply what is returned at the end of processing the model's layers. It's a pointer to the list of them... You could use

                    layers=Sketchup.active_model.layers
                    

                    which sets 'layers' as a variable pointing at the model's layers list, you can then use 'layers' later on - e.g.

                    layers.each{|layer|puts layer.name}
                    

                    which would print out each of the layers' names in turn in the Ruby Console [puts 123] You could then use layers again to do something else - like change some layers' names...

                    TIG

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

                      Bob, you design SpacePorts?!?!??!?!!? ๐Ÿ˜ฎ ?!?!?!??!?!?!

                      Thats the coolest thing ever.

                      Chris

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

                      1 Reply Last reply Reply Quote 0
                      • Bob JamesB Offline
                        Bob James
                        last edited by

                        This is getting REALLY interesting. My large models normally have over 100 layers. Attached is a partial listing for one model. Note the "0 - Small Payload" names: these were changed with your original name changer command - ENORMOUSLY HANDY.

                        In my original post I said:

                        @bob james said:

                        Or if I've developed a new model and want to re-organize the layers with prefixes.

                        This is true even for older models that are used as a basic model to which different user equipment is added.

                        You said
                        @tig said:

                        you can then use 'layers' later on - e.g.

                        layers.each{|layer|puts layer.name}
                        

                        which would print out each of the layers' names in turn in the Ruby Console [puts 123] You could then use layers again to do something else - like change some layers' names...

                        I see what you said, but don't really know how to implement it.

                        Quite often it would be very handy to be able to re-organize the layer names into a better grouping (since there is no provision for sub-layers under a heading layer). In this case being able to have a list of the layers, change the names and then feed it back in. The "feed-back" would probably have to be in the same order as the original name list, but SU would then re-sort them into the new groupings. Is this kind of thing doable?

                        You can see some of the scenarios I've done at http://www.calspace.com at Processing Scenario - Small and Processing Scenario - Large


                        ![About one-fourth of the layers list. Note the "0 - Small Payload" names: these were changed with your original name changer command - ENORMOUSLY HANDY](/uploads/imported_attachments/XbPH_Picture12.png "About one-fourth of the layers list. Note the "0 - Small Payload" names: these were changed with your original name changer command - ENORMOUSLY HANDY")

                        i7-4930K 3.4Ghz, 2x GTX780 6GB, 32GB DDR3-1600 ECC, OCZ Vertex 4 500GB, WD Black 3TB, 32TB NAS, 4x 27" Monitors, SpaceMouse Pro, X-keys XK-60

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

                          The list is in the order the layers were made. It would be possible to make a sorted list and put that into a text file - it it were any use to you...

                          However, probably the easiest way to rename stuff as you want is to make step by step changes to how the layers are prefixed. You can change prefix1 to another1 and the prefix2 to another2 etc etc. Just do the rename steps a few times till they are as you want... You just need to do a little bit of thinking about how you might want to rename and check that no steps will interfer with a previous one...

                          TIG

                          1 Reply Last reply Reply Quote 0
                          • Bob JamesB Offline
                            Bob James
                            last edited by

                            Chris: Yeh, and SU makes it possible for an old duffer like me to do enormously interesting things ๐Ÿ˜„

                            TIG: I tried your new commands and, as advertised, I received a list of the layer names in the Ruby Console. Interestingly, the order is not the same: I assume they are in the order that they were created (or something like that)?

                            Now what can I do with this with regard to changing names in SU?


                            The top part of the Ruby Console list

                            i7-4930K 3.4Ghz, 2x GTX780 6GB, 32GB DDR3-1600 ECC, OCZ Vertex 4 500GB, WD Black 3TB, 32TB NAS, 4x 27" Monitors, SpaceMouse Pro, X-keys XK-60

                            1 Reply Last reply Reply Quote 0
                            • Bob JamesB Offline
                              Bob James
                              last edited by

                              Good to go.

                              Thanks for taking the time to mess with this small stuff, I certainly appreciate it.

                              i7-4930K 3.4Ghz, 2x GTX780 6GB, 32GB DDR3-1600 ECC, OCZ Vertex 4 500GB, WD Black 3TB, 32TB NAS, 4x 27" Monitors, SpaceMouse Pro, X-keys XK-60

                              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