Pack and Go / Select certain file + rename

Hello
I'm currently at an impasse on this Pack And Go code.

Specifications:

  • Retrieve the pack and Go from an assembly /Ok for me/
  • List all files that contain " OF " /Ok for me/
  • Keep only that /Ok for me/
  • Rename them with a new number /Ok for me/
  • Create my new Pack And Go list /Ok for me/
  • Generate Pack And Go // Not working //

My filesArray table is well filled, I call it with SetDocumentSaveToNames.

Except that she doesn't take it into account.

The SavePackAndGo feature does not work.

Thank you in advance for your answers.

Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension

Dim swPackAndGo As PackAndGo
Dim statusArray As Variant
Dim status As Boolean
Dim statuses As Boolean

Dim arraySource() As String
Dim arrayResult() As String
Dim i As Long, j As Long
Dim searchString As String
Sun NumberSearch As String
Dim count As Long
Dim result As String
Dim counterOF As Collection
Dim numeroOF As Variant
Dim RsultatOF As String
Dim filesArray() As String
Dim ListeOF() As String

' Initialiser SolidWorks
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

' Vérifier si un document est ouvert
If swModel Is Nothing Then
    MsgBox "Veuillez ouvrir un document SolidWorks."
    Exit Sub
End If

Set swModelDocExt = swModel.Extension

' Créer l'objet Pack and Go
Set swPackAndGo = swModelDocExt.GetPackAndGo

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

swPackAndGo.IncludeDrawings = False
Debug.Print " Include drawings: " & swPackAndGo.IncludeDrawings
swPackAndGo.IncludeSimulationResults = False
Debug.Print " Include SOLIDWORKS Simulation results: " & swPackAndGo.IncludeSimulationResults
swPackAndGo.IncludeToolboxComponents = False
Debug.Print " Include SOLIDWORKS Toolbox components: " & swPackAndGo.IncludeToolboxComponents

' Obtenir les informations sur les fichiers
swPackAndGo.GetDocumentNames namesArray

' Structure répertoire
swPackAndGo.FlattenToSingleFolder = True

' Définir la chaîne de recherche
searchString = "OF "
origineOF = "30277"
nouvelOF = "45000"



' Utiliser Filter pour extraire les éléments contenant la chaîne de recherche
tableauResultat = Filter(namesArray, searchString, True, vbTextCompare)


'myPath = "C:\BE\1 Plans 2024\Mathieu"

    For i = LBound(tableauResultat) To UBound(tableauResultat)
                RsultatOF = tableauResultat(i)
                j = j + 1
        Debug.Print RsultatOF
    Next i
    
ReDim filesArray(0 To j - 1)
       
For i = 0 To UBound(tableauResultat)
        FileName = GetFileName(CStr(tableauResultat(i)))
        ' Remplacer la chaîne si spécifié
            If origineOF <> "" Then
                FileName = Replace(FileName, origineOF, nouvelOF)
                FileNames = FileName
            End If
        filesArray(k) = FileNames
        Debug.Print "Nouveau fichier " & filesArray(k)
        k = k + 1
        'Debug.Print FileNames
Next


myPath = "C:\BE\1 Plans 2024\Mathieu"
status = swPackAndGo.SetSaveToName(True, myPath)

statuses = swPackAndGo.SetDocumentSaveToNames(filesArray)

statusArray = swModelDocExt.SavePackAndGo(swPackAndGo)

End Sub

Function GetFileName(path As String) As String
GetFileName = Right(path, Len(path) - InStrRev(path, ""))
End Function

PackAndGo.zip (1.1 MB)
MTD-PackAndGo-teste.swp (40.5 KB)

Hello mathieu.devezeaud,

If you have " MyCadTool " (We have myCADtools 2024 SP1 with SW 2022 SP4), there is the " projectManager " macro which is fine to rename files in one go, otherwise to the mano-mano... But what was it possible with " PackAndGo "?
To be seen=>https://www.youtube.com/watch?v=oVhNJA6kHzY.
PackAndGo
https://www.youtube.com/watch?v=wmlU9e0Sdjs
@+.
AR.

Hello

I think there's just a backslash missing from this line: myPath = "C:\BE\1 Plans 2024\Mathieu", correct writing: myPath = "C:\BE\1 Plans 2024\Mathieu\"

1 Like

Hello Cyril,
indeed there was a lack of an Anti-slash which solved part of the problem.
The program does not take into account the filesArray() array.
it creates the pack and go in the right destination directory but doesn't take into account the filesArray.
Kind regards.

image

1 Like

Hello

So I just tested a little bit more deeply. I modified the code slightly and would have to test on a larger number of files to check the behavior.
Basically from what I understand, in the function SetDocumentSaveToNames you need the same number of " files " as the one identified at the beginning in the pack and go via GetDocumentNamesCount.
To be tested with the code below modified:

Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swPackAndGo As PackAndGo
Dim statusArray As Variant
Dim status As Boolean
Dim statuses As Boolean
Dim tableauSource() As String
Dim tableauResultat() As String
Dim i As Long, j As Long
Dim searchString As String
Dim numeroRecherche As String
Dim count As Long
Dim resultat As String
Dim compteurOF As Collection
Dim numeroOF As Variant
Dim RsultatOF As String
Dim filesArray() As String
Dim ListeOF() As String
    
    ' Initialiser SolidWorks
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    ' Vérifier si un document est ouvert
    If swModel Is Nothing Then
        MsgBox "Veuillez ouvrir un document SolidWorks."
        Exit Sub
    End If
    
    Set swModelDocExt = swModel.Extension

    ' Créer l'objet Pack and Go
    Set swPackAndGo = swModelDocExt.GetPackAndGo
    count = swPackAndGo.GetDocumentNamesCount
    ' Include any drawings, SOLIDWORKS Simulation results, and SOLIDWORKS Toolbox components
swPackAndGo.IncludeDrawings = False
Debug.Print "  Include drawings: " & swPackAndGo.IncludeDrawings
swPackAndGo.IncludeSimulationResults = False
Debug.Print "  Include SOLIDWORKS Simulation results: " & swPackAndGo.IncludeSimulationResults
swPackAndGo.IncludeToolboxComponents = False
Debug.Print "  Include SOLIDWORKS Toolbox components: " & swPackAndGo.IncludeToolboxComponents


    
    ' Obtenir les informations sur les fichiers
    swPackAndGo.GetDocumentNames namesArray
    
    ' Structure répertoire
    swPackAndGo.FlattenToSingleFolder = True

    ' Définir la chaîne de recherche
    searchString = "OF "
    origineOF = "30277"
    nouvelOF = "45000"
    
    
    
    ' Utiliser Filter pour extraire les éléments contenant la chaîne de recherche
    tableauResultat = Filter(namesArray, searchString, True, vbTextCompare)
    

    'myPath = "C:\BE\1 Plans 2024\Mathieu\"
    
        For i = LBound(tableauResultat) To UBound(tableauResultat)
                    RsultatOF = tableauResultat(i)
                    j = j + 1
            Debug.Print RsultatOF
        Next i
        
    ReDim filesArray(count - 1)
    myPath = "C:\temp\"
    For i = 0 To UBound(tableauResultat)
            FileName = GetFileName(CStr(tableauResultat(i)))
            ' Remplacer la chaîne si spécifié
                If origineOF <> "" Then
                    FileName = Replace(FileName, origineOF, nouvelOF)
                    FileNames = FileName
                End If
            filesArray(k) = myPath & FileNames
            Debug.Print "Nouveau fichier " & filesArray(k)
            k = k + 1
            'Debug.Print FileNames
    Next

    
    
    statuses = swPackAndGo.SetDocumentSaveToNames(filesArray)
    status = swPackAndGo.SetSaveToName(True, myPath)
    statusArray = swModelDocExt.SavePackAndGo(swPackAndGo)
    

End Sub

Function GetFileName(path As String) As String
    GetFileName = Right(path, Len(path) - InStrRev(path, "\"))
End Function

The new lines are:

count = swPackAndGo.GetDocumentNamesCount
ReDim filesArray(count - 1)
filesArray(k) = myPath & FileNames
3 Likes

Hello

I'll be interested to know if in your packandgo there is an external equation file.

Let me explain, I have an Excel macro that allows me to make a packandgo and rename my files according to the name of the project
Everything goes smoothly except the referenced links between the files.
It keeps the links of the original files.
When doing some testing, it turns out that this comes from the external equation file, which is a simple TXT file.

if anyone has already encountered the problem or has a trick for this problem.

Kind regards.

Bonjour,

Il faudrait regarder dans cet exemple de l’API : Pack and Go Part and Linked Equation Example (VBA) - 2023 - SOLIDWORKS API Help

1 Like