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!
    πŸ›£οΈ Road Profile Builder | Generate roads, curbs and pavements easily Download

    [Request] Name, Definition Name of Components

    Scheduled Pinned Locked Moved Plugins
    16 Posts 2 Posters 973 Views 2 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.
    • TIGT Offline
      TIG Moderator
      last edited by

      My quick example code works on all instances of all definitions...
      If you want to affect only a certain named collection of definitions then this version will work for you...
      You save it as renameInstances.rb in ../Plugins/ and run it by typing renameInstances 'Dice' in the Ruby Console - where 'Dice' [inside ' ' or " "] is the 'string' pattern we'll look for in the definitions' names. Note how 'Dice' matches that text anywhere in the name, but adding a leading '^' as in '^Dice' matches only if the string is at the very start, or with a trailing '$' as in 'Dice$' it matches the string only at the very end - AND combos inside '[]' as in '[Dd]ice' matches with both 'Dice' and 'dice', anywhere in the names etc; if you add no 'argument' then ALL are definitions/instances are processed.
      I've also removed groups/images from the processing, and I've added a one step undo...

      def renameInstances(str = '')
          str=str.to_s ### makes sure!
          model = Sketchup.active_model
          model.start_operation('renameInstances')
          model.definitions.each{|defn|
            next if defn.image? or defn.group?
            dname = defn.name
            if str != ''
              next if dname.gsub(/#{str}/,'') == dname
              ### NO match to 'str' so skip it...
            end#if
            puts "Defn Name = "+dname
            iname = dname.gsub(/[0-9]+$/, '')
            ### i.e. it strips ALL numerals OFF the end of 'name'
            iname = 'Doh!' if iname == ''
            ### i.e. it was ALL numerals and so it vanished !!
            defn.instances.each{|inst|
              inst.name = iname
              puts "Inst Name = "+iname
            }
          }
          model.commit_operation
      end#def
      
      

      ???

      TIG

      1 Reply Last reply Reply Quote 0
      • pilouP Offline
        pilou
        last edited by

        In V6
        I have 2 components no instancied
        with definition name: Component#1, Component#2 all without name
        Wishing
        definition name: Dice1, name: Dice
        definition name: Dice2, name: Dice

        I select the 2 component
        launched in ruby Console your last ruby script, type renameInstances 'Dice'
        result :
        True
        but nothing is changed for my 2 components selected 😳

        Frenchy Pilou
        Is beautiful that please without concept!
        My Little site :)

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

          @unknownuser said:

          In V6
          I have 2 components
          with definition name: Component#1, Component#2 all without name
          Wishing
          definition name: Dice1, name: Dice
          definition name: Dice2, name: Dice

          I select the 2 component
          launched in ruby Console your last ruby script : result
          True
          but nothing is changed for my 2 components selected 😳

          It doesn't work like that... AND you never asked for that, initially you said that you had a component named 'Dice1' and you wanted to name its instances as 'Dice' πŸ˜’ πŸ˜’ πŸ˜’
          Where in my description did I say it works on renaming a selected component ???
          You now say you want to rename 'Component#1' as 'Dice1' and rename its instances as 'Dice'.
          That's completely different!!
          First you need a renameDefinitions tool to change the name of 'Component#1' to be 'Dice1' and then use the current tool to rename 'Dice...' definitions' instances as 'Dice'.
          Save this code into a file in Plugins called 'renameDefinitions.rb'.
          Usage: in the Ruby Console type in
          renameDefinitions 'Component#', 'Dice'
          to rename all components containing that text, which are renamed with the new text 'Dice' - so
          'Component#1' >>> 'Dice1'
          'Component#2' >>> 'Dice2'
          but 'Component' stays as 'Component'
          If there is no second argument the matching text is simply removed from the name.
          To match only the first two 'Component#' definitions use a pattern like
          renameDefinitions 'Component#[12]$', 'Dice#1'
          So only
          'Component#1' >>> 'Dice#1'
          'Component#2' >>> 'Dice#2'
          but 'Component#3' stays as 'Component#3'
          To fix the '#' in the replacement 'Dice#', run again as
          renameDefinitions 'Dice#', 'Dice'
          so all of the 'Dice#' names >>> 'Dice' names...
          OR if bizzare is needed
          renameDefinitions 'Dice#', 'AfghanistanBananistan' πŸ˜’
          So

          def renameDefinitions(str = '', txt = '')
                  str = str.to_s ### makes sure!
                  txt = txt.to_s
                  model = Sketchup.active_model
                  model.start_operation('renameDefinitions')
                  model.definitions.each{|defn|
                    next if defn.image? or defn.group?
                    dname = defn.name
                    if str != ''
                      next if dname.gsub(/#{str}/,'') == dname
                      ### NO match to 'str' so skip it...
                    end#if
                    nname = dname.gsub(/#{str}/, txt)
                    ### i.e. it replaces str with txt
                    nname = 'Doh!' if nname == ''
                    ### i.e. it was an exact match and txt=''!!
                    defn.name = nname
                    puts "Defn Name; "+dname+" = "+nname
                  }
                  model.commit_operation
          end#def
          
          

          NOW use the renameInstances tool to change any instances of renamed definitions as a second step. πŸ€“

          TIG

          1 Reply Last reply Reply Quote 0
          • pilouP Offline
            pilou
            last edited by

            That what that: missunderstanding my first post πŸ˜‰
            If you re read sequentially my first post I never said that i had component named Dice
            I said "I wish" πŸ˜‰

            Frenchy Pilou
            Is beautiful that please without concept!
            My Little site :)

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

              @unknownuser said:

              I wish rename all this collection of component selected like this
              First component
              Definition name = Dice1
              Name = Dice

              IF you had said:

              I wish to rename all selected components like this...
              Definition name = 'Component#1'
              New Definition name = 'Dice1'
              New Instances name = 'Dice'
              That's what you would have got!
              Anyway, you now have something that will do it what you want - renaming the definitions by pattern matching and then a second tool renaming the matching definitions' instances by stripping off the numerical part of the defn name and using that...

              TIG

              1 Reply Last reply Reply Quote 0
              • pilouP Offline
                pilou
                last edited by

                Thx for your efforts β˜€
                No possibility to enter a joker ?
                because my selection of components can have any Definition names!
                So make a general renaming in one pass

                Frenchy Pilou
                Is beautiful that please without concept!
                My Little site :)

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

                  Of course all your components will have a name - even if it's 'Component#1' it's still a name ???
                  Therefore you'd use
                  renameDefinitions 'Component#', 'Dice'
                  which acts like a 'wildcard' and every component containing the text 'Component#' will have that replaced with 'Dice' ??????
                  So 'Component#1 >>> 'Dice1', 'Component#2 >>> 'Dice2' etc
                  As I then explained further you can constrain the renaming to a smaller set by using other pattern matching like [1238] to just process the first three and the eighth one
                  renameDefinitions 'Component#[1238]', 'Dice#1'
                  to rename them all first as as 'Dice#1', 'Dice#2' etc and then in a second step using
                  renameDefinitions 'Dice#', 'Dice'
                  to rename them all as as 'Dice1', 'Dice2' etc...

                  This does NOT work on a highlighted 'selection' !!
                  You make your selection using a pattern matching string in the name!!!! πŸ€“

                  TIG

                  1 Reply Last reply Reply Quote 0
                  • pilouP Offline
                    pilou
                    last edited by

                    So there is no possibility to change 2 objects components Definition named
                    Ball
                    Poll
                    to these Definition named Dice1, Dice2 in one pass?

                    Frenchy Pilou
                    Is beautiful that please without concept!
                    My Little site :)

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

                      No...

                      TIG

                      1 Reply Last reply Reply Quote 0
                      • pilouP Offline
                        pilou
                        last edited by

                        Finally i have found a trick with another Console Script of your! πŸ‘
                        I explode all components selected and call your tricky thing who tranform any selections in Components! 😎

                        m=Sketchup.active_model;  ### tranform any selections in Components By TIG
                        m.start_operation("Faces>Compos");
                        n=m.active_entities;m.selection.to_a.each{|e|(g=n.add_group(e.all_connected);
                        g.to_component.definition.name="FaceSet#1")if e.valid? and e.parent==n.parent and e.class==Sketchup;;Face};
                        m.commit_operation
                        

                        So obtain a generic Components' name: FaceSet#1, FaceSet#2 etc...
                        Call your renameInstances many posts above
                        So obtain an only one name for all the Components: FaceSet#
                        So I can use it inside the Plug Random Replace Component
                        and even now no kneed to change it in Dice! πŸ˜’

                        ...but I must change my tutorial! πŸ’š

                        Thanks again for your lessons! πŸ‘
                        I have seen many things about renaming but see also that I am definitly not made for ruby scripting! πŸ˜’

                        Frenchy Pilou
                        Is beautiful that please without concept!
                        My Little site :)

                        1 Reply Last reply Reply Quote 0
                        • pilouP Offline
                          pilou
                          last edited by

                          and...In fact that is some redundant with the use of the Dialog box Info for rename a group of components! 😳
                          I had an headache for a so simple solution πŸ˜„

                          Frenchy Pilou
                          Is beautiful that please without concept!
                          My Little site :)

                          1 Reply Last reply Reply Quote 0
                          • pilouP Offline
                            pilou
                            last edited by

                            My trick works only with components with no level of component inside πŸ˜’

                            and for rename a same component with definition name "Ball" existing in different positions
                            as new components with of course new definitions name Ball1, Ball2, Ball3 etc... ?
                            I suppose they must be "Make Unic" then renamed

                            Frenchy Pilou
                            Is beautiful that please without concept!
                            My Little site :)

                            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