Files
Pack and go top level parts only
Code
Module: mMain
'Demonstrates to how to pack and go top level parts only.
'Preconditions: assembly is open & correct save-to folder is specified below.
'Results: only top-level parts of assembly are added to pack and go; P&G _
is saved as a zip to folder specified below
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
Option Explicit
Option Compare Text 'case-insensitive text comparison
Const SAVE_TO_FOLDER As String = "C:\Temp\Output\" 'include closing backslash
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim swComp As SldWorks.Component2
Dim vComps As Variant
' Get top level document paths
Dim colTopLevelDocPaths As New Collection
Dim i As Integer
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swAssy = swModel
vComps = swAssy.GetComponents(True)
For i = 0 To UBound(vComps)
Set swComp = vComps(i)
If Right(swComp.GetPathName, 6) = "sldprt" Then _
colTopLevelDocPaths.Add vComps(i).GetPathName
Next i
Dim swPackAndGo As SldWorks.PackAndGo
Dim strFileNames As Variant
Dim strSetFileNames() As String
Dim strCurrentDir As String
Set swPackAndGo = swModel.Extension.GetPackAndGo
swPackAndGo.IncludeDrawings = True
' Get current paths and filenames of the assy's docs
swPackAndGo.GetDocumentNames strFileNames
' Get current save-to paths and filenames of the assy's docs
swPackAndGo.GetDocumentSaveToNames strFileNames, Empty
' Get current directory
strCurrentDir = Left(swModel.GetPathName, _
InStrRev(swModel.GetPathName, "\"))
' Create your own filenames for the model's documents
ReDim strSetFileNames(swPackAndGo.GetDocumentNamesCount - 1)
Dim strFileName As String
For i = 0 To swPackAndGo.GetDocumentNamesCount - 1
If i = 0 Then
'This will include the root assembly
strSetFileNames(i) = Replace(strFileNames(i), strCurrentDir, _
SAVE_TO_FOLDER)
Else
strFileName = strFileNames(i)
If IsTopLevelPart(strFileName, colTopLevelDocPaths) Then
strSetFileNames(i) = Replace(strFileNames(i), strCurrentDir, _
SAVE_TO_FOLDER)
Else
strSetFileNames(i) = Empty
End If
End If
Next i
' Set document paths and names for Pack and Go
swPackAndGo.SetDocumentSaveToNames strSetFileNames
swPackAndGo.GetDocumentNames strFileNames
' This line turns the pack and go into a zip file
swPackAndGo.SetSaveToName True, SAVE_TO_FOLDER & "test.zip"
' Pack and Go
swModel.Extension.SavePackAndGo swPackAndGo
End Sub
Function IsTopLevelPart(strFileName As String, _
ByRef colTopLevelDocPaths As Collection) As Boolean
Dim i As Integer
For i = 1 To colTopLevelDocPaths.Count
If colTopLevelDocPaths.Item(i) = strFileName Then
'This will save time on future searches. _
Requires passing collection as reference.
colTopLevelDocPaths.Remove i
IsTopLevelPart = True
Exit Function
End If
Next i
IsTopLevelPart = False
End Function