4 Replies Latest reply on Nov 9, 2016 9:06 AM by Marko Rebec

    Renaming parts in Assembly

    Marko Rebec

      Hi everyone,

       

      I am dealing with macro to rename parts filename in opened assembly.The name should be the existing name + some custom properties in each part.

      I think I am missing something because I get through the assembly and read the component names but can not rename documents.I get un error on RenameDocument witch is consequence of NOT selecting properly with SelectByID2 (I suppose).I am posting the code if anyone can help.

      Thank you in advance.

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

        

          Dim vChildComp As Variant

          Dim swChildComp As SldWorks.Component2

          Dim swCompConfig As SldWorks.Configuration

          Dim oldName As String

          Dim newName As String

          Dim swSelData As SldWorks.SelectData

          Dim bRet As Boolean

          Dim swConfigMgr As SldWorks.ConfigurationManager

          Dim cusPropMgr  As SldWorks.CustomPropertyManager

          Dim swModelDocExt As SldWorks.ModelDocExtension

        

          Dim ValOut                  As String

          Dim ResolvedValOut          As String

          Dim wasResolved             As Boolean

          Dim lRetVal                 As Long

          Dim status As Boolean

          Dim errorsRename As Long

       

          Dim sPadStr As String

          Dim i As Long

        

          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.GetModelDoc2

              Set swModelDocExt = swChildModel.Extension

       

              'swCompConfig = swChildComp.ReferencedConfiguration

            

              Debug.Print sPadStr & swChildComp.Name2

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

              ' Changing component name requires component to be selected

              bRet = swChildComp.Select4(False, swSelData, False)

              oldName = swChildComp.Name2

              'Debug.Print oldName

            

              Set swCompConfig = swChildModel.GetActiveConfiguration

              Set cusPropMgr = swCompConfig.CustomPropertyManager

              lRetVal = cusPropMgr.Get5("Pozicija", False, ValOut, ResolvedValOut, wasResolved)

              Debug.Print "Pozicija:          "; ResolvedValOut; lRetVal

            

              'swChild.Name2 = newName

              status = swModelDocExt.SelectByID2(oldName, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)

              Debug.Print "Staro ime: " & oldName

              newName = ResolvedValOut & "_" & oldName

              Debug.Print "Novo ime: " & newName

       

              errorsRename = swModelDocExt.RenameDocument(newName)

              Debug.Print "Rename document errors: " & status; errorsRename

       

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

              TraverseComponentandRename swChildComp, nLevel + 1

          Next i

       

      End Sub

        • Re: Renaming parts in Assembly
          Deepak Gupta

          Try these codes (modify as needed) else check the macro/comments in this post Rename files with custom properties

           

          Option Explicit

              Dim swApp           As SldWorks.SldWorks

              Dim swModel         As SldWorks.ModelDoc2

              Dim swRootComp      As SldWorks.Component2

              Dim Children        As Variant

              Dim swChild         As SldWorks.Component2

              Dim swSelData       As SldWorks.SelectData

              Dim ChildCount      As Long

              Dim oldName         As String

              Dim newName         As String

              Dim i               As Long

          Sub main()

            

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

              Set swRootComp = swModel.ConfigurationManager.ActiveConfiguration.GetRootComponent3(True)

              Children = swRootComp.GetChildren

             

              ChildCount = UBound(Children)

              Set swSelData = swModel.SelectionManager.CreateSelectData

             

              For i = 0 To ChildCount

                  Set swChild = Children(i)

                  oldName = swChild.GetPathName

                  oldName = Mid(oldName, InStrRev(oldName, "\") + 1)

                  oldName = Left(oldName, InStrRev(oldName, ".") - 1)

                  swChild.Select4 False, swSelData, False

                  newName = swChild.GetModelDoc2.CustomInfo("Description")

                  swModel.Extension.RenameDocument newName & "_" & oldName

              Next i

              swModel.ForceRebuild3 True

             

          End Sub