4 Replies Latest reply on Jul 27, 2018 8:12 AM by Matt Bieringer

    Macro for changing the component name in an assembly

    Riccardo Rebichini

      Hi,

      I found on this forum the following macro from Deepak Gupta and I modified it with my custom properties.

      My problem is that, when I have a sub-assembly I am not able to change the component name of the sub-assembly.

      As you can see in the following image all the parts, also the sub-assembliy parts, have been renamed (code-revision-description) but the sub-assembly remains with the original file name (code).

      Is there a way to modify the macro?

      Thanks in advance.

       

       

       

       

      ' Macro to Rename Part File (component name) with configuration properties.swp ------------- 07/19/13

      '

      'Description: Rename Part File (component name) based on specified configuration properties of the component in current active assembly file.

      '

      'Precondition: Any active assembly file with minimum one part file.

      '

      'Postconditions: Part Files (component name)are changed to new name based on specified properties.

      '

      ' Please back up your data before use and USE AT OWN RISK

      '

      ' This macro is provided as is.  No claims, support, refund, safety net, or

      ' warranties are expressed or implied.  By using this macro and/or its code in

      ' any way whatsoever, the user and any entities which the user represents,

      ' agree to hold the authors free of any and all liability.  Free distribution

      ' and use of this code in other free works is welcome.  If any portion of

      ' this code is used in other works, credit to the authors must be placed in

      ' that work within a user viewable location (e.g., macro header).  All other

      ' forms of distribution (i.e., not free, fee for delivery, etc) are prohibited

      ' without the expressed written consent by the authors.  Use at your own risk!

      ' ------------------------------------------------------------------------------

      ' Written by: Deepak Gupta (http://gupta9665.com/)

      ' -----------------------------------------------------------------------------

       

       

       

       

      Option Explicit

       

       

      Public Enum swUserPreferenceToggle_e

          swExtRefUpdateCompNames = 18

       

       

      End Enum

       

       

      Dim swApp            As SldWorks.SldWorks

      Dim swModel          As SldWorks.ModelDoc2

      Dim swAssy           As SldWorks.AssemblyDoc

      Dim swConf           As SldWorks.Configuration

      Dim swRootComp       As SldWorks.Component2

      Dim bRet             As Boolean

      Dim bOldSetting      As Boolean

       

       

      Sub TraverseComponent(swComp As SldWorks.Component2, nLevel As Long)

       

       

          Dim vChildComp              As Variant

          Dim swChildComp             As SldWorks.Component2

          Dim swCompConfig            As String

          Dim sPadStr                 As String

          Dim i                       As Long

          Dim swChildModel            As SldWorks.ModelDoc2

          Dim NewName                 As String

          Dim Descrizione             As String

          Dim Revisione               As String

          Dim Codice               As String

         

         

          

          bOldSetting = swApp.GetUserPreferenceToggle(swExtRefUpdateCompNames)

       

       

      swApp.SetUserPreferenceToggle swExtRefUpdateCompNames, False

       

       

       

       

         For i = 0 To nLevel - 1

              sPadStr = sPadStr + "  "

          Next i

         

          vChildComp = swComp.GetChildren

         

          For i = 0 To UBound(vChildComp)

       

       

              Set swChildComp = vChildComp(i)

             

              Set swChildModel = swChildComp.GetModelDoc

       

       

              If swChildModel.GetType = swDocPART Then

             

              swChildComp.Select2 False, 0

             

              'Get configuration name

              swCompConfig = swChildComp.ReferencedConfiguration

                   

              ' Get Configuration Properties Values

            

             

              Codice = swChildModel.CustomInfo2(swCompConfig, "CODICE")

              Descrizione = swChildModel.CustomInfo2(swCompConfig, "DESCRIZIONE")

              Revisione = swChildModel.CustomInfo2(swCompConfig, "REVISIONE")

              NewName = Codice + "-" + Revisione + "-" + Descrizione

         

             

              swChildComp.Name2 = NewName

             

              Debug.Print swChildComp.Name2

             

               End If

                    

              TraverseComponent swChildComp, nLevel + 1

                    

          Next i

         

          swApp.SetUserPreferenceToggle swExtRefUpdateCompNames, bOldSetting

       

       

      End Sub

       

       

      Sub main()

       

       

          Set swApp = CreateObject("SldWorks.Application")

          Set swModel = swApp.ActiveDoc

          Set swConf = swModel.GetActiveConfiguration

          Set swRootComp = swConf.GetRootComponent3(True)

         

          TraverseComponent swRootComp, 1

       

       

      End Sub

        • Re: Macro for changing the component name in an assembly
          Dariusz Karcz

          Hello Riccardo,

           

          as far i can see, in your macro there is only one decision making IF statement with "Part only" case, try this one, I've attached case, when doc type= assembly:

           

          ' Macro to Rename Part File (component name) with configuration properties.swp ------------- 07/19/13

          '

          'Description: Rename Part File (component name) based on specified configuration properties of the component in current active assembly file.

          '

          'Precondition: Any active assembly file with minimum one part file.

          '

          'Postconditions: Part Files (component name)are changed to new name based on specified properties.

          '

          ' Please back up your data before use and USE AT OWN RISK

          '

          ' This macro is provided as is.  No claims, support, refund, safety net, or

          ' warranties are expressed or implied.  By using this macro and/or its code in

          ' any way whatsoever, the user and any entities which the user represents,

          ' agree to hold the authors free of any and all liability.  Free distribution

          ' and use of this code in other free works is welcome.  If any portion of

          ' this code is used in other works, credit to the authors must be placed in

          ' that work within a user viewable location (e.g., macro header).  All other

          ' forms of distribution (i.e., not free, fee for delivery, etc) are prohibited

          ' without the expressed written consent by the authors.  Use at your own risk!

          ' ------------------------------------------------------------------------------

          ' Written by: Deepak Gupta (http://gupta9665.com/)

          ' -----------------------------------------------------------------------------

           

           

           

           

          Option Explicit

           

           

          Public Enum swUserPreferenceToggle_e

              swExtRefUpdateCompNames = 18

           

           

          End Enum

           

           

          Dim swApp            As SldWorks.SldWorks

          Dim swModel          As SldWorks.ModelDoc2

          Dim swAssy           As SldWorks.AssemblyDoc

          Dim swConf           As SldWorks.Configuration

          Dim swRootComp       As SldWorks.Component2

          Dim bRet             As Boolean

          Dim bOldSetting      As Boolean

           

           

          Sub TraverseComponent(swComp As SldWorks.Component2, nLevel As Long)

           

           

              Dim vChildComp              As Variant

              Dim swChildComp             As SldWorks.Component2

              Dim swCompConfig            As String

              Dim sPadStr                 As String

              Dim i                       As Long

              Dim swChildModel            As SldWorks.ModelDoc2

              Dim NewName                 As String

              Dim Descrizione             As String

              Dim Revisione               As String

              Dim Codice               As String

            

            

             

              bOldSetting = swApp.GetUserPreferenceToggle(swExtRefUpdateCompNames)

           

           

          swApp.SetUserPreferenceToggle swExtRefUpdateCompNames, False

           

           

           

           

             For i = 0 To nLevel - 1

                  sPadStr = sPadStr + "  "

              Next i

            

              vChildComp = swComp.GetChildren

            

              For i = 0 To UBound(vChildComp)

           

           

                  Set swChildComp = vChildComp(i)

                

                  Set swChildModel = swChildComp.GetModelDoc

           

           

                  If swChildModel.GetType = swDocPART Then

                

                  swChildComp.Select2 False, 0

                

                  'Get configuration name

                  swCompConfig = swChildComp.ReferencedConfiguration

                      

                  ' Get Configuration Properties Values

               

                

                  Codice = swChildModel.CustomInfo2(swCompConfig, "CODICE")

                  Descrizione = swChildModel.CustomInfo2(swCompConfig, "DESCRIZIONE")

                  Revisione = swChildModel.CustomInfo2(swCompConfig, "REVISIONE")

                  NewName = Codice + "-" + Revisione + "-" + Descrizione

            

                

                  swChildComp.Name2 = NewName

                

                  Debug.Print swChildComp.Name2

                

                 ElseIf swChildModel.GetType = swDocASSEMBLY Then

                

                 swChildComp.Select2 False, 0

                

                  'Get configuration name

                  swCompConfig = swChildComp.ReferencedConfiguration

                      

                  ' Get Configuration Properties Values

               

                

                  Codice = swChildModel.CustomInfo2(swCompConfig, "CODICE")

                  Descrizione = swChildModel.CustomInfo2(swCompConfig, "DESCRIZIONE")

                  Revisione = swChildModel.CustomInfo2(swCompConfig, "REVISIONE")

                  NewName = Codice + "-" + Revisione + "-" + Descrizione

            

                

                  swChildComp.Name2 = NewName

                

                  Debug.Print swChildComp.Name2

                

                   End If

                       

                  TraverseComponent swChildComp, nLevel + 1

                       

              Next i

            

              swApp.SetUserPreferenceToggle swExtRefUpdateCompNames, bOldSetting

           

           

          End Sub

           

           

          Sub main()

           

           

              Set swApp = CreateObject("SldWorks.Application")

              Set swModel = swApp.ActiveDoc

              Set swConf = swModel.GetActiveConfiguration

              Set swRootComp = swConf.GetRootComponent3(True)

            

              TraverseComponent swRootComp, 1

           

           

          End Sub

          • Re: Macro for changing the component name in an assembly
            Deepak Gupta

            Riccardo, just comment out these two lines and you should be good