3 Replies Latest reply on Dec 17, 2015 3:53 PM by Amen Allah Jlili

    Rename part files from a folder without loosing Assembly reference

    JOHN GEORGE

      I have a 100s of files in an assembly (imported) with a long name something like..

      ABC_123_updated_mold_sldasm-Part-1.sldprt,

      ABC_123_updated_mold_sldasm-Part-2.sldprt, etc...

      Is it possible to rename the whole assembly folder with each part file name like XYZ-P1.sldprt, XYZ-P2.sldprt, etc..following the sequence?

      I tried using SW Explorer and it takes lots of time rename one by one.

      Any help appreciated!

        • Re: Rename part files from a folder without loosing Assembly reference
          Amen Allah Jlili

          This is possible through a macro using pack and go.

           

          Use:

          - Define a IPackAndGo object (Set IPackAndGo = ModelDocExtension.GetPackAndGo)

          - Use IPackAndGo::GetDocumentNames to get the names of documents (array)

          - Modify the names of the documents (the array) and save it to the IPackAndGo object using IPackAndGo::SetSaveToName

          - Use ModelDocExtension:SavePackandGo(IPackAndGo) to save
          - Admire your work

            • Re: Rename part files from a folder without loosing Assembly reference
              JOHN GEORGE

              Amen,

              Good to know that it is possible, but with my very limited knowledge of SW API and VBA, it's hard to write a workable code without an experienced help

              I will dig thru the API help section and see if I can find something over there.

              Thanks!

                • Re: Rename part files from a folder without loosing Assembly reference
                  Amen Allah Jlili

                  Alright, this seem to work on my end. Give it a try amigo. It will rename parts to part-1, part-2... and likewise for assemblies. It does not handle drawing though.

                   

                  Dim swApp As SldWorks.SldWorks
                  Dim swModel As ModelDoc2
                  Dim swModelDocExt As ModelDocExtension
                  Dim SavingPath As String
                  Sub main()
                  SavingPath = InputBox("Where do you want to pack and go? Enter path here:", "PackAndGo by jliliamen@gmail.com")
                  Dim FileSystemObject As Object
                  Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
                  If Not FileSystemObject.FolderExists(SavingPath) Then
                  MsgBox ("Folder does not exist.")
                  End If
                  Set swApp = Application.SldWorks
                  Set swModel = swApp.ActiveDoc
                  Set swModelDocExt = swModel.Extension
                  Dim PackAndGoObj As PackAndGo
                  Set PackAndGoObj = swModelDocExt.GetPackAndGo
                  Dim VDocs
                  Dim result As Boolean
                  PackAndGoObj.FlattenToSingleFolder = True
                  PackAndGoObj.IncludeToolboxComponents = True
                  result = PackAndGoObj.GetDocumentNames(VDocs)
                  Dim Partcounter As Long: Partcounter = 1
                  Dim AssemblyCounter As Long: AssemblyCounter = 1
                  For i = 0 To UBound(VDocs)
                  If Split(VDocs(i), ".")(1) = "sldprt" Then
                  VDocs(i) = "Part-" & Partcounter & ".sldprt"
                  Partcounter = Partcounter + 1
                  ElseIf Split(VDocs(i), ".")(1) = "sldasm" Then
                  VDocs(i) = "Assem-" & AssemblyCounter & ".sldasm"
                  AssemblyCounter = AssemblyCounter + 1
                  End If
                  Next i
                  result = PackAndGoObj.SetSaveToName(True, SavingPath)
                  result = PackAndGoObj.SetDocumentSaveToNames(VDocs)
                  Dim vResult
                  vResult = swModelDocExt.SavePackAndGo(PackAndGoObj)
                  End Sub