10 Replies Latest reply on Mar 14, 2018 3:46 PM by Erik Rotolo

    rename configs and derived configs using find replace macro

    Erik Rotolo

      Hello all,

       

      I have a part file or asm file.

      This file has a huge amount of configs and derived configs  as the part numbers, and these part numbers need to change. 

      But these part numbers are associated in many asms, so deleting all the configs and changing the names in the excel design table is no good.

      I need a way instead of manually changing hundreds of configs via right click properties or slow double click all the names and manually type new names. 

      This is probably the kind of the situation i need a macro to change all the config names.


      A marco that works like find replace in excel, for example find replace in all config names "ANBXYZ" with "ABCXYZ". 

      Now, the part number code would really be ANBXYZ-48.  So the -48 must be untouched (so the result is ABCXYZ-48) .


      Is it even possible to do in an API?

      Is something like this exist already?

       

      Thanks,

      Erik

        • Re: rename configs and derived configs using find replace macro
          Erik Rotolo

          Deepak Gupta

          You have been the biggest help on the forums for me and many others when it comes to api/macro creation.

          Is my request to rename configs and derived configs using find replace macro possible?

          Would you be able to create this for community?

           

          Thank you very much for your knowledge, experience, and time. 

          • Re: rename configs and derived configs using find replace macro
            Alex Burnett

            You can build on this macro to do whatever you want with string manipulation to get the new name of the configuration where indicated. I'm not saying this is the best method but it does work. It has to activate each configuration to rename it though and I don't know if there's a way around that.

             

            Dim swApp As SldWorks.SldWorks
            Dim swModel As SldWorks.ModelDoc2
            Dim cfgNames() As String
            Dim cfgName As String
            Dim newcfgName As String
            Dim cfg As Configuration
            
            
            Sub main()
            Set swApp = Application.SldWorks
            Set swModel = swApp.ActiveDoc
            cfgNames = swModel.GetConfigurationNames
            
            
            For Each variable In cfgNames
                cfgName = variable
                
                Dim strArray() As String
                strArray = Split(cfgName, "-")
                If UBound(strArray) - LBound(strArray) > 0 Then
                    newcfgName = "CHANGED-" & strArray(1)
                End If
                
                swModel.ShowConfiguration2 cfgName
                Set cfg = swModel.GetActiveConfiguration
                cfg.Name = newcfgName
                Debug.Print "Name: " & cfgName
            Next variable
            
            
            End Sub
            

             

            Edit: added simple string manipulation. Use at your own risk

              • Re: rename configs and derived configs using find replace macro
                Erik Rotolo

                I asked a coworker "does the the macro need to activate every config to change the name?"

                he said "I would think it’s not necessary to activate the config prior to renaming. "

                he also said "I would check how referencing files react. You will likely get a message that the referenced configuration cannot be found. However, the correct config should still be found and referenced."

                and said "You would iterate through the configurations and use Configuration.Name = “New name” to do it."

                  • Re: rename configs and derived configs using find replace macro
                    Alex Burnett

                    Erik Rotolo wrote:

                     

                    I asked a coworker "does the the macro need to activate every config to change the name?"

                    he said "I would think it’s not necessary to activate the config prior to renaming. "

                    he also said "I would check how referencing files react. You will likely get a message that the referenced configuration cannot be found. However, the correct config should still be found and referenced."

                    and said "You would iterate through the configurations and use Configuration.Name = “New name” to do it."

                    I can take a look again tomorrow to see if there's a way to access configuration attributes without activating them. Perhaps the ConfigurationManager object has this functionality. Either way, the code I provided may get you by for the time being although I'd recommend testing it on several sample files.

                    • Re: rename configs and derived configs using find replace macro
                      Josh Brady

                      Just eliminate the line:

                      swModel.ShowConfiguration2 cfgName 

                       

                      and change

                       

                      Set cfg = swModel.GetActiveConfiguration

                       

                      to

                       

                      Set cfg = swModel.GetConfigurationByName(cfgName)

                        • Re: rename configs and derived configs using find replace macro
                          Alex Burnett

                          That makes sense. I knew there had to be a way but I haven't worked too much with configurations in this manner. Thanks for the tip!

                           

                          Edit: My final code (I know Deepak's works too but this is another approach)

                          Dim swApp As SldWorks.SldWorks
                          Dim swModel As SldWorks.ModelDoc2
                          Dim cfgNames() As String
                          Dim cfgName As String
                          Dim newcfgName As String
                          Dim cfg As Configuration
                          Const NewName As String = "CHANGED"
                            
                            
                          Sub main()
                          Set swApp = Application.SldWorks
                          Set swModel = swApp.ActiveDoc
                          cfgNames = swModel.GetConfigurationNames
                            
                            
                          For Each variable In cfgNames
                              cfgName = variable
                                
                              Dim strArray() As String
                              strArray = Split(cfgName, "-")
                              If UBound(strArray) - LBound(strArray) > 0 Then
                                  newcfgName = NewName & "-" & strArray(1)
                              End If
                                
                              Set cfg = swModel.GetConfigurationByName(cfgName)
                              cfg.Name = newcfgName
                              Debug.Print "Name: " & cfgName
                          Next variable
                            
                            
                          End Sub
                          
                    • Re: rename configs and derived configs using find replace macro
                      Deepak Gupta

                      Erik, try this macro. Change names/values as needed

                       

                      Option Explicit
                          Dim swApp As SldWorks.SldWorks
                          Dim swModel As SldWorks.ModelDoc2
                          Dim vConfNameArr As Variant
                          Dim i As Long
                          Dim swConfig As SldWorks.Configuration
                          Const OldName As String = "XXX" 'Change name as needed
                          Const NewName As String = "ABC" 'Change name as needed
                          
                      Sub main()
                         
                          Set swApp = Application.SldWorks
                          Set swModel = swApp.ActiveDoc
                          
                          vConfNameArr = swModel.GetConfigurationNames
                          For i = 0 To UBound(vConfNameArr)
                              Set swConfig = swModel.GetConfigurationByName(vConfNameArr(i))
                              swConfig.Name = Replace(vConfNameArr(i), OldName, NewName, Count:=1)
                          Next i
                          
                      End Sub
                      

                       

                      Edit: Updated code line to replace only one count in the name. For e.g. if you want to replace only XXX in the string XXXBBXXX, then without count defined, it would replace all XXX pairs. So without count value defined replaced name would be ABCBBABC instead of ABCBBXXX.

                      • Re: rename configs and derived configs using find replace macro
                        Erik Rotolo

                        This is the macro I am using to do a find replace of config names.

                        It works amazing, and I would like to thank everyone that helped me on this topic.

                         

                        Special thanks to Deepak, Alex, and CAD Micro.