ds-blue-logo
Preview  |  SOLIDWORKS USER FORUM
Use your SOLIDWORKS ID or 3DEXPERIENCE ID to log in.
VMVlad Munteanu10/01/2013

Hello,

I am trying to use the macro that is provided in the VBA help library with slight modifications .

http://help.solidworks.com/2012/English/api/sldworksapi/Pack_and_Go_an_Assembly_Example_vb.htm

What I am trying to do is while an assembly is open, get all the references contained in that assembly and then based on some pre-defined criteria, remove from that list some references which I don’t want to save. So when the pack and go operation is completed the removed references will not be copied over.

I was successful in getting the pgSetFileNames to have empty entries for the removed references and finish the pack and go. It seems to work some times and other times it completely ignores that list (pgSetFileNames) and copies everything. So when it doesn’t work the list pgSetFileNames still contains empty references but it seems to get the full list of references from somewhere else. I don’t know if the failures I am getting are due to assemblies having routing assemblies or not. There seems to be a correlation between having routing assemblies and the macro ignoring the truncated list of references pgSetFileNames.

What I need it to do is:

pgSetFileNames – list of documents I want the Pack And Go operation to use

I want the following line to use the values in the pgSetFileNames array. Statuses should be a function of pgSetFileNames

statuses = swModelDocExt.SavePackAndGo(swPackAndGo)

Thank you all for your help!

Here is part of my code, which is almost identical to the SolidWorks Macro listed on their website if it will help troubleshoot:

Public Sub pack_and_go()

Dim swModelDoc As SldWorks.ModelDoc2

Dim swModelDocExt As SldWorks.ModelDocExtension

Dim swPackAndGo As SldWorks.PackAndGo

'Route Stuff

Dim swTopLevelAssembly As SldWorks.AssemblyDoc

Dim rtRouteManager As SWRoutingLib.RouteManager

Set swApp = Application.SldWorks

Set swModelDoc = swApp.ActiveDoc

If swModelDoc Is Nothing Then

    swApp.sendmsgtuser ("Please open a document")

Else

End If

Set swModelDocExt = swModelDoc.Extension ' Get Pack and Go object

Dim routemgr As Object

Set routemgr = swModelDoc.GetRouteManager()

If swModelDoc.GetType = swDocASSEMBLY Then

Set swTopLevelAssembly = swModelDoc

Set rtRouteManager = swTopLevelAssembly.GetRouteManager

If rtRouteManager Is Nothing Then

  1. Debug.Print "No RouteManager found in top-level document: " & swTopLevelAssembly.GetPathName

Exit Sub

End If

Else

End If

Set swPackAndGo = swModelDoc.Extension.GetPackAndGo

' Include any drawings and simulation results

  1. swPackAndGo.IncludeDrawings = True

' Get number of documents in assembly

namesCount = swPackAndGo.GetDocumentNamesCount

status = swPackAndGo.GetDocumentNames(pgFileNames) 'added set

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

  1. 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)

  1. Debug.Print ""
  2. 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

status = swPackAndGo.GetDocumentSaveToNames(pgFileNames, Nothing)

'Create your own filenames for the model's documents

ReDim pgSetFileNames(namesCount - 1)

'Debug.Print ""

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

j = 0

For i = 0 To (namesCount - 1)

         myFileName = pgFileNames(i)

         ' Determine type of SolidWorks file based on file extension

             If InStr(LCase(myFileName), "sldprt") > 0 Then

                 'myFileName = j & ".sldprt"

                 fullpath = myFileName

                 fileName_function (fullpath)

                 'myFileName = myPath & fname

                 Call ExcelSearch

             ElseIf InStr(LCase(myFileName), "sldasm") > 0 Then

                 'myFileName = j & ".sldasm"

                 fullpath = myFileName

                 fileName_function (fullpath)

                 'myFileName = myPath & fname

                 Call ExcelSearch

            ElseIf InStr(LCase(myFileName), "slddrw") > 0 Then

                 'myFileName = j & ".slddrw"

                 fullpath = myFileName

                fileName_function (fullpath)

                 'myFileName = myPath & fname

                 Call ExcelSearch

             Else

                 ' Only packing up SolidWorks files

                 Exit Sub

             End If

        pgSetFileNames(i) = mypath & fname2 'fname 'myPath & fname 'myFileName

        End If

         j = j + 1

                 

Next i

reDim pgSetFileNames(namesCount - 1)

status = swPackAndGo.SetDocumentSaveToNames(pgSetFileNames)

status = swPackAndGo.SetDocumentSaveToNames(pgSetFileNames)

'swPackAndGo.AddPrefix = "SW"

'swPackAndGo.AddSuffix = "PackAndGo"

' Verify document paths and filenames after adding prefix and suffix

ReDim pgGetFileNames(namesCount - 1)

ReDim pgDocumentStatus(namesCount - 1)

pgGetFileNames = pgSetFileNames

status = swPackAndGo.SetSaveToName(True, mypath)

status = swPackAndGo.GetDocumentSaveToNames(pgSetFileNames, pgDocumentStatus)

‘Debug.Print ""

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

For i = 0 To (namesCount - 1)

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

Next i

‘Pack and Go

statuses = swModelDocExt.SavePackAndGo(swPackAndGo)

End Sub