Files
Create pack and go with specified custom property as prefix
Code
'This macro will pack and go an assembly or drawing to a specified folder or as a ZIP file. _
Optionally, if a specified custom property is present in a file, then that custom _
property is appended onto the new file name in this format: "partname" "property".SLDPRT/ASM
'Preconditions:
' 1. Assembly or drawing is open - drawing must be saved
' 2. Output path is specified - either a directory or the path to the final .zip file
' 3. Custom property search string is specified
'Results: pack and go copy of the assy or drw is saved in output folder, with the specified custom _
property for each component used as a suffix in parentheses
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
Const strOutputPath As String = "C:\Temp\"
Const strCustProp As String = "Revision"
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swCompModel As SldWorks.ModelDoc2
Dim swCustPropMgr As SldWorks.CustomPropertyManager
Dim swPackAndGo As SldWorks.PackAndGo
Dim strValOut As String
Dim strFileExt As String
Dim intDocCount As Integer
Dim vPathNames As Variant
Dim strSetPathNames() As String
Dim i As Integer
Dim vStatus As Variant
Dim strRetVal As String
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
'Error handling
If swModel Is Nothing Then
swApp.SendMsgToUser "Please open an assembly or drawing."
Exit Sub
ElseIf swModel.GetType <> swDocASSEMBLY And swModel.GetType <> swDocDRAWING Then
swApp.SendMsgToUser "Please open an assembly or drawing."
Exit Sub
End If
'Get IPackAndGo object
Set swPackAndGo = swModel.Extension.GetPackAndGo
'Resize the set paths array
intDocCount = swPackAndGo.GetDocumentNamesCount
ReDim strSetPathNames(intDocCount - 1) As String
'Get the current document paths and append the Revision custom property
swPackAndGo.GetDocumentNames vPathNames
For i = 0 To UBound(vPathNames)
strSetPathNames(i) = vPathNames(i)
'The document should be open in memory, so get it
Set swCompModel = swApp.GetOpenDocumentByName(strSetPathNames(i))
If Not swCompModel Is Nothing Then
'Get the custom property value
Set swCustPropMgr = swCompModel.Extension.CustomPropertyManager(Empty)
swCustPropMgr.Get4 strCustProp, True, Empty, strValOut
Else 'if virtual component
strValOut = Empty
End If
'Change the save directory; assume that strOutputPath is either a folder path or a .zip file path
If UCase(Right(strOutputPath, 3)) = "ZIP" Then
strSetPathNames(i) = Right(strSetPathNames(i), Len(strSetPathNames(i)) - InStrRev(strSetPathNames(i), "\"))
Else
strSetPathNames(i) = strOutputPath & Right(strSetPathNames(i), Len(strSetPathNames(i)) - InStrRev(strSetPathNames(i), "\"))
End If
'Add in the custom property (note that IPackAndGo::AddSuffix could have also been used, _
though this would have been complicated since the suffix is different for each item
If strValOut <> Empty Then
strFileExt = Right(vPathNames(i), 7)
strSetPathNames(i) = Left(strSetPathNames(i), Len(strSetPathNames(i)) - 7)
strSetPathNames(i) = strSetPathNames(i) & " (" & strValOut & ")" & strFileExt
End If
Next i
'Set the save paths
swPackAndGo.SetDocumentSaveToNames strSetPathNames
'IPackAndGo::SetSaveToName is needed for two reasons. First, it is needed for _
IPackAndGo::IncludeDrawings to work. Second, it is needed if the pack and go _
is to result in a .zip file. Also note that in this example, at least, I am not _
able to see any difference between Override being True or False
swPackAndGo.SetSaveToName True, strOutputPath
'Include drawings
swPackAndGo.IncludeDrawings = True
'Save the pack and go
vStatus = swModel.Extension.SavePackAndGo(swPackAndGo)
'Determine whether pack and go was successful
For i = 0 To UBound(vStatus)
Select Case vStatus(i)
Case 0
strRetVal = "Pack and go succeeded"
Case 1
strRetVal = "User input not correct"
Case 2
strRetVal = "File already exists"
Case 3
strRetVal = "File to save is empty"
Case 4
strRetVal = "Save error"
End Select
Debug.Print strRetVal
Next i
End Sub
'Notes
'-To successfully pack and go the associated drawing files, those drawing files needs to be saved, _
it appears, otherwise Case 4 occurs.
'-As of 2013 SP0, IPackAndGo::IncludeDrawings does not work with IPackAndGo::SetDocumentSaveToNames _
alone, and IPackAndGo::SetSaveToName must also be used for it to work.
'-In the path for IPackAndGo::SetSaveToName, include an ending slash or you will get a _
"File already exists" result
'-As of 2013, drawings included with IPackAndGo::IncludesDrawings do not have their references _
updated to the new pack and go location.