38 Replies Latest reply on Jul 16, 2018 10:36 AM by Ian Farquharson

    Rename a part  file name

    Prabaharan Pichaiyan

      Is it any macro available to rename a part and its references?

       

      I found this example, but its for Assembly only.

      2016 SOLIDWORKS API Help - Rename Components and Save Assembly Example (VBA)  

       

      I need to rename a part with Macro.

       

      Thanks

        • Re: Rename a part  file name
          Abilash V.

          Hi Prabaharan,

           

          Can u explain your scenario?

          So you have the part files in a folder and wanted to rename it automatically ,is it?

           

          Abilash

          • Re: Rename a part  file name
            Deepak Gupta

            Have you tried using Pack and Go (both API and directly from File menu)?

              • Re: Rename a part  file name
                Prabaharan Pichaiyan

                Very often we are going to do rename a part. In this Scenario its really difficult to use Pack and Go

                 

                Thanks for your replay

                  • Re: Rename a part  file name
                    Abilash V.

                    Hi Prabhaharan

                     

                    The following macro will rename the file and ll delete the old file.

                    This works for the currently opened active file.

                     

                    Regards

                    Abilash

                      • Re: Rename a part  file name
                        Prabaharan Pichaiyan

                        Thanks for your reply. But this would remove the link between part and Drawing.

                        Is is possible to update the link?

                         

                        Thanks

                          • Re: Rename a part  file name
                            Abilash V.

                            Hi,

                            Pack and go API is a better way to maintain the references so that can be used to code your requirement in the way you desire.

                            2015 SOLIDWORKS API Help - Pack and Go an Assembly (VBA)

                             

                            Regards

                            Abilash

                            • Re: Rename a part  file name
                              Abilash V.

                               

                              For the opened file you could do as such in manual pack and go Method to rename the file name.

                              All the references are preserved.

                              Regards

                              Abilash

                              • Re: Rename a part  file name
                                Abilash V.

                                1.Open the drawing and select "SaveAs"

                                2.Select "Include all referenced documents" and click advanced

                                3.Now give away suitable name either for Part or Drawing.

                                Regards

                                Abilash

                                  • Re: Rename a part  file name
                                    Abilash V.

                                    Kindly check the following macro

                                     

                                    Regards

                                    Abilash

                                      • Re: Rename a part  file name
                                        Prabaharan Pichaiyan

                                        May I know which one?

                                          • Re: Rename a part  file name
                                            Abilash V.

                                            I have attached a macro.

                                            While running it will ask for Folder path.Copy/Paste the location path where the files must be saved.

                                            As well as enter the file name with which the files has been renamed with.

                                            If you have any clarification reach me.

                                             

                                            Regards

                                            Abilash

                                              • Re: Rename a part  file name
                                                Prabaharan Pichaiyan

                                                Thanks. Will check and let you know

                                                • Re: Rename a part  file name
                                                  Prabaharan Pichaiyan

                                                  Hi Abilash,

                                                  Really Thanks for your effort.

                                                  This Pack and go macro creating new files with new names.

                                                  But I want to replace the same files with new name in assembly and drawings.

                                                  I think I should use document manager api.

                                                  I do have a licence for that.

                                                  Is any one able to help me with Document manger API here?

                                                   

                                                  Thanks

                                                    • Re: Rename a part  file name
                                                      Deepak Gupta

                                                      Prabaharan Pichaiyan wrote:

                                                       

                                                      But I want to replace the same files with new name in assembly and drawings.

                                                       

                                                      You can also use ReplaceReferencedDocument method (which does not need SWDM)

                                                        • Re: Rename a part  file name
                                                          Prabaharan Pichaiyan

                                                          Thanks Deepak

                                                          I'm working on it. will let you the outcome.

                                                           

                                                          Thanks

                                                          • Re: Rename a part  file name
                                                            Prabaharan Pichaiyan

                                                            Hi

                                                            In Pack and go macro is Flatten folder option is not working. Is there any other mistake I'm doing?

                                                             

                                                            Option Explicit

                                                             

                                                            Dim swApp As SldWorks.SldWorks

                                                            Dim swModelDoc As SldWorks.ModelDoc2

                                                            Dim swModelDocExt As SldWorks.ModelDocExtension

                                                            Dim swPackAndGo As SldWorks.PackAndGo

                                                            Dim openFile As String

                                                            Dim pgFileNames As Variant

                                                            Dim pgFileStatus As Variant

                                                            Dim pgGetFileNames As Variant

                                                            Dim pgDocumentStatus As Variant

                                                            Dim status As Boolean

                                                            Dim warnings As Long

                                                            Dim errors As Long

                                                            Dim i As Long

                                                            Dim namesCount As Long

                                                            Dim myPath As String

                                                            Dim statuses As Variant

                                                            Sub main()

                                                             

                                                             

                                                            Set swApp = Application.SldWorks

                                                            Set swModelDoc = swApp.ActiveDoc

                                                            Set swModelDocExt = swModelDoc.Extension

                                                            Set swPackAndGo = swModelDocExt.GetPackAndGo

                                                            Set swModelDocExt = swModelDoc.Extension

                                                             

                                                             

                                                            ' Get Pack and Go object

                                                            Debug.Print "Pack and Go"

                                                            Set swPackAndGo = swModelDocExt.GetPackAndGo

                                                             

                                                             

                                                            ' Get number of documents in assembly

                                                            namesCount = swPackAndGo.GetDocumentNamesCount

                                                            Debug.Print "  Number of model documents: " & namesCount

                                                             

                                                             

                                                            ' Include any drawings, SOLIDWORKS Simulation results, and SOLIDWORKS Toolbox components

                                                            swPackAndGo.IncludeDrawings = True

                                                            Debug.Print "  Include drawings: " & swPackAndGo.IncludeDrawings

                                                            swPackAndGo.IncludeSimulationResults = False

                                                            Debug.Print "  Include SOLIDWORKS Simulation results: " & swPackAndGo.IncludeSimulationResults

                                                            swPackAndGo.IncludeToolboxComponents = True

                                                            Debug.Print "  Include SOLIDWORKS Toolbox components: " & swPackAndGo.IncludeToolboxComponents

                                                             

                                                             

                                                            ' Get current paths and filenames of the assembly's documents

                                                            status = swPackAndGo.GetDocumentNames(pgFileNames)

                                                            Debug.Print ""

                                                            Debug.Print "  Current path and filenames: "

                                                            If (Not (IsEmpty(pgFileNames))) Then

                                                                For i = 0 To UBound(pgFileNames)

                                                                    Debug.Print "    The path and filename is: " & pgFileNames(i)

                                                                Next i

                                                            End If

                                                             

                                                             

                                                            ' Get current save-to paths and filenames of the assembly's documents

                                                            status = swPackAndGo.GetDocumentSaveToNames(pgFileNames, pgFileStatus)

                                                            Debug.Print ""

                                                            Debug.Print "  Current default save-to filenames: "

                                                            If (Not (IsEmpty(pgFileNames))) Then

                                                                For i = 0 To UBound(pgFileNames)

                                                                    Debug.Print "   The path and filename is: " & pgFileNames(i)

                                                                Next i

                                                            End If

                                                             

                                                             

                                                            ' Set folder where to save the files

                                                            myPath = "C:\yahoo\"

                                                            status = swPackAndGo.SetSaveToName(True, myPath)

                                                            ' Maintain the Pack and Go folder structure; save all files to the root directory

                                                            swPackAndGo.FlattenToSingleFolder = False

                                                             

                                                             

                                                            ' Add a prefix and suffix to the new Pack and Go filenames

                                                            swPackAndGo.AddSuffix = "_R1"

                                                            ' Verify document paths and filenames after adding prefix and suffix

                                                            ReDim pgGetFileNames(namesCount - 1)

                                                            ReDim pgDocumentStatus(namesCount - 1)

                                                            status = swPackAndGo.GetDocumentSaveToNames(pgGetFileNames, pgDocumentStatus)

                                                            Debug.Print ""

                                                            Debug.Print "  My Pack and Go path and filenames after adding prefix and suffix: "

                                                            For i = 0 To (namesCount - 1)

                                                                 Debug.Print "    My path and filename is: " & pgGetFileNames(i)

                                                            Next i

                                                            ' Pack and Go

                                                            statuses = swModelDocExt.SavePackAndGo(swPackAndGo)

                                                            End Sub

                                                              • Re: Rename a part  file name
                                                                Prabaharan Pichaiyan

                                                                Found the solution for this ,

                                                                 

                                                                Just need to rearrange lines,

                                                                 

                                                                 

                                                                Option Explicit

                                                                 

                                                                Dim swApp As SldWorks.SldWorks

                                                                Dim swModelDoc As SldWorks.ModelDoc2

                                                                Dim swModelDocExt As SldWorks.ModelDocExtension

                                                                Dim swPackAndGo As SldWorks.PackAndGo

                                                                Dim openFile As String

                                                                Dim pgFileNames As Variant

                                                                Dim pgFileStatus As Variant

                                                                Dim pgGetFileNames As Variant

                                                                Dim pgDocumentStatus As Variant

                                                                Dim status As Boolean

                                                                Dim warnings As Long

                                                                Dim errors As Long

                                                                Dim i As Long

                                                                Dim namesCount As Long

                                                                Dim myPath As String

                                                                Dim statuses As Variant

                                                                Sub main()

                                                                 

                                                                 

                                                                Set swApp = Application.SldWorks

                                                                Set swModelDoc = swApp.ActiveDoc

                                                                Set swModelDocExt = swModelDoc.Extension

                                                                Set swPackAndGo = swModelDocExt.GetPackAndGo

                                                                Set swModelDocExt = swModelDoc.Extension

                                                                 

                                                                 

                                                                ' Get Pack and Go object

                                                                Debug.Print "Pack and Go"

                                                                Set swPackAndGo = swModelDocExt.GetPackAndGo

                                                                 

                                                                 

                                                                ' Get number of documents in assembly

                                                                namesCount = swPackAndGo.GetDocumentNamesCount

                                                                Debug.Print "  Number of model documents: " & namesCount

                                                                 

                                                                 

                                                                ' Include any drawings, SOLIDWORKS Simulation results, and SOLIDWORKS Toolbox components

                                                                swPackAndGo.IncludeDrawings = True

                                                                Debug.Print "  Include drawings: " & swPackAndGo.IncludeDrawings

                                                                swPackAndGo.IncludeSimulationResults = False

                                                                Debug.Print "  Include SOLIDWORKS Simulation results: " & swPackAndGo.IncludeSimulationResults

                                                                swPackAndGo.IncludeToolboxComponents = True

                                                                Debug.Print "  Include SOLIDWORKS Toolbox components: " & swPackAndGo.IncludeToolboxComponents

                                                                 

                                                                 

                                                                ' Get current paths and filenames of the assembly's documents

                                                                status = swPackAndGo.GetDocumentNames(pgFileNames)

                                                                Debug.Print ""

                                                                Debug.Print "  Current path and filenames: "

                                                                If (Not (IsEmpty(pgFileNames))) Then

                                                                    For i = 0 To UBound(pgFileNames)

                                                                        Debug.Print "    The path and filename is: " & pgFileNames(i)

                                                                    Next i

                                                                End If

                                                                 

                                                                 

                                                                ' Get current save-to paths and filenames of the assembly's documents

                                                                status = swPackAndGo.GetDocumentSaveToNames(pgFileNames, pgFileStatus)

                                                                Debug.Print ""

                                                                Debug.Print "  Current default save-to filenames: "

                                                                If (Not (IsEmpty(pgFileNames))) Then

                                                                    For i = 0 To UBound(pgFileNames)

                                                                        Debug.Print "   The path and filename is: " & pgFileNames(i)

                                                                    Next i

                                                                End If

                                                                 

                                                                ' Maintain the Pack and Go folder structure; save all files to the root directory

                                                                swPackAndGo.FlattenToSingleFolder = False

                                                                 

                                                                ' Set folder where to save the files

                                                                myPath = "C:\yahoo\"

                                                                status = swPackAndGo.SetSaveToName(True, myPath)

                                                                 

                                                                 

                                                                 

                                                                ' Add a prefix and suffix to the new Pack and Go filenames

                                                                swPackAndGo.AddSuffix = "_R1"

                                                                ' Verify document paths and filenames after adding prefix and suffix

                                                                ReDim pgGetFileNames(namesCount - 1)

                                                                ReDim pgDocumentStatus(namesCount - 1)

                                                                status = swPackAndGo.GetDocumentSaveToNames(pgGetFileNames, pgDocumentStatus)

                                                                Debug.Print ""

                                                                Debug.Print "  My Pack and Go path and filenames after adding prefix and suffix: "

                                                                For i = 0 To (namesCount - 1)

                                                                     Debug.Print "    My path and filename is: " & pgGetFileNames(i)

                                                                Next i

                                                                ' Pack and Go

                                                                statuses = swModelDocExt.SavePackAndGo(swPackAndGo)

                                                                End Sub

                                                                 

                                                                 

                                                                 

                                                                Thanks

                                                                • Re: Rename a part  file name
                                                                  Abilash V.

                                                                  Prabahran,

                                                                   

                                                                  But u will still have the original file right ?

                                                                   

                                                                  Abilash

                                                                      • Re: Rename a part  file name
                                                                        Abilash V.

                                                                        Prabaharn

                                                                        But u dont want multiple copies right ?

                                                                        Then u r ok with the Pack and Go   API.

                                                                         

                                                                        Abilash

                                                                          • Re: Rename a part  file name
                                                                            Prabaharan Pichaiyan

                                                                            Yes Abilash. But the worst case we could have a copy and we may delete by API or manually so.

                                                                             

                                                                            Thanks

                                                                            • Re: Rename a part  file name
                                                                              Prabaharan Pichaiyan

                                                                              Could you help me with the pack and go API which is giving the output name in CAPS?

                                                                              And that's enough for me to proceed further.

                                                                               

                                                                              Really Thanks for your help

                                                                                • Re: Rename a part  file name
                                                                                  Clark Honzik

                                                                                  I remember running into that issue with the CAPS on a piece of automation I wrote.  I believe there was an SPR on that issue.  As I recall I had to simply code up a work around and create my own function to get the windows API to retrieve the path back to me as is actually exists in the file system of windows.

                                                                                • Re: Rename a part  file name
                                                                                  Prabaharan Pichaiyan

                                                                                  Option Explicit

                                                                                   

                                                                                   

                                                                                  Dim swApp As SldWorks.SldWorks

                                                                                  Dim swModelDoc As SldWorks.ModelDoc2

                                                                                  Dim swModelDocExt As SldWorks.ModelDocExtension

                                                                                  Dim swPackAndGo As SldWorks.PackAndGo

                                                                                  Dim openFile As String

                                                                                  Dim pgFileNames As Variant

                                                                                  Dim pgFileStatus As Variant

                                                                                  Dim pgGetFileNames As Variant

                                                                                  Dim pgDocumentStatus As Variant

                                                                                  Dim status As Boolean

                                                                                  Dim warnings As Long

                                                                                  Dim errors As Long

                                                                                  Dim i As Long

                                                                                  Dim namesCount As Long

                                                                                  Dim myPath As String

                                                                                  Dim statuses As Variant

                                                                                   

                                                                                   

                                                                                  Sub main()

                                                                                   

                                                                                   

                                                                                  Set swApp = Application.SldWorks

                                                                                  Set swModelDoc = swApp.ActiveDoc

                                                                                  Set swModelDocExt = swModelDoc.Extension

                                                                                  Set swPackAndGo = swModelDocExt.GetPackAndGo

                                                                                  Set swModelDocExt = swModelDoc.Extension

                                                                                   

                                                                                   

                                                                                  'Get Pack and Go object

                                                                                  'Debug.Print "Pack and Go"

                                                                                  Set swPackAndGo = swModelDocExt.GetPackAndGo

                                                                                   

                                                                                   

                                                                                  ' Get number of documents in assembly

                                                                                  namesCount = swPackAndGo.GetDocumentNamesCount

                                                                                  Debug.Print "  Number of model documents: " & namesCount

                                                                                   

                                                                                   

                                                                                  ' Include any drawings, SOLIDWORKS Simulation results, and SOLIDWORKS Toolbox components

                                                                                  swPackAndGo.IncludeDrawings = True

                                                                                   

                                                                                   

                                                                                  'Debug.Print "  Include drawings: " & swPackAndGo.IncludeDrawings

                                                                                  swPackAndGo.IncludeSimulationResults = False

                                                                                   

                                                                                   

                                                                                  'Debug.Print "  Include SOLIDWORKS Simulation results: " & swPackAndGo.IncludeSimulationResults

                                                                                  swPackAndGo.IncludeToolboxComponents = True

                                                                                   

                                                                                   

                                                                                  ' Get current paths and filenames of the assembly's documents

                                                                                  status = swPackAndGo.GetDocumentNames(pgFileNames)

                                                                                  Debug.Print ""

                                                                                  Debug.Print "  Current path and filenames: "

                                                                                  If (Not (IsEmpty(pgFileNames))) Then

                                                                                      For i = 0 To UBound(pgFileNames)

                                                                                          Debug.Print "    The path and filename is: " & pgFileNames(i)

                                                                                      Next i

                                                                                  End If

                                                                                   

                                                                                   

                                                                                  ' Get current save-to paths and filenames of the assembly's documents

                                                                                  status = swPackAndGo.GetDocumentSaveToNames(pgFileNames, pgFileStatus)

                                                                                  'Debug.Print ""

                                                                                  'Debug.Print "  Current default save-to filenames: "

                                                                                   

                                                                                   

                                                                                  If (Not (IsEmpty(pgFileNames))) Then

                                                                                      For i = 0 To UBound(pgFileNames)

                                                                                          Debug.Print "   The path and filename is: " & pgFileNames(i)

                                                                                      Next i

                                                                                  End If

                                                                                   

                                                                                   

                                                                                  ' Maintain the Pack and Go folder structure; save all files to the root directory

                                                                                  swPackAndGo.FlattenToSingleFolder = False

                                                                                   

                                                                                   

                                                                                   

                                                                                   

                                                                                  ' Set folder where to save the files

                                                                                   

                                                                                   

                                                                                  myPath = "C:\Users\dwg18.AVANTEKSERVER\Desktop\yahoo\"

                                                                                  myPath = UCase(myPath)

                                                                                  status = swPackAndGo.SetSaveToName(True, myPath)

                                                                                   

                                                                                   

                                                                                  ' Add a prefix and suffix to the new Pack and Go filenames

                                                                                  swPackAndGo.AddSuffix = "_R2"

                                                                                  ' Verify document paths and filenames after adding prefix and suffix

                                                                                  ReDim pgGetFileNames(namesCount - 1)

                                                                                  ReDim pgDocumentStatus(namesCount - 1)

                                                                                  status = swPackAndGo.GetDocumentSaveToNames(pgGetFileNames, pgDocumentStatus)

                                                                                   

                                                                                   

                                                                                  'Debug.Print ""

                                                                                  'Debug.Print "  My Pack and Go path and filenames after adding prefix and suffix: "

                                                                                   

                                                                                   

                                                                                  For i = 0 To (namesCount - 1)

                                                                                       Debug.Print "    My path and filename is: " & pgGetFileNames(i)

                                                                                  Next i

                                                                                  ' Pack and Go

                                                                                  statuses = swModelDocExt.SavePackAndGo(swPackAndGo)

                                                                                  End Sub

                                                      • Re: Rename a part  file name
                                                        Voverrr Mironoff

                                                        Hello.

                                                        You can check this plugin:

                                                        Переименование деталей в сборке | StreamDivision

                                                        it's on the russsian there, but it's enough understandable in video.

                                                        It's allow to rename the parts in the opened assembly without closing and re-opening. Right in the tree.

                                                        • Re: Rename a part  file name
                                                          Jim Donnellon

                                                          Did you try the rename part from feature tree?  It will rename part, and update reference on the fly.

                                                          I did a test, and it updates the drawing also.  I love this feature. FYI I am on 2017 SP5.0

                                                          Rename.jpg

                                                          • Re: Rename a part  file name
                                                            Clark Honzik

                                                            I would suggest looking into SolidWorks PDM.  Even the standard addition will make this a simple process.

                                                            • Re: Rename a part  file name
                                                              Ian Farquharson

                                                              Prabaharan Pichaiyan,

                                                               

                                                              I have just cobbled together a macro that renames documents - assemblies and parts which will keep the references if your drawing is open. I can organize it to be not as specific for the properties that I have used to create my naming scheme.

                                                               

                                                              I don't have any training on this but I am willing to share what I have come up with if you would like me to.

                                                               

                                                              Ian