Pack and Go an Assembly (VB.NET)
This example shows how to get the names of the path and files of an assembly
document, assign them new names, add a prefix and suffix to the new names, and save the newly named files to a different path using the Pack and Go
interface.
'------------------------------------------
' Preconditions:
' 1. Specified assembly exists.
' 2. The folder, c:\temp, exists.
' 3. Open the Immediate window.
' 4. Run the macro.
'
' Postconditions:
' 1. Names of the current path and filenames
' of the assembly documents are printed to Immediate window.
' 2. Names of the default path and filenames to which to
' save assembly documents are printed to Immediate window.
' 3. User-specified path and user-named files for assembly documents
' created.
' 4. Prefix and suffix added to user-named filenames.
' 5. Names of user-specified path and user-named filenames printed to
' Immediate window.
' 6. User-named files created in user-specified path using Pack and Go.
' 7. Examine c:\temp to verify.
'-------------------------------------------
Imports
SolidWorks.Interop.sldworks
Imports
SolidWorks.Interop.swconst
Imports System
Imports System.Diagnostics
Partial
Class SolidWorksMacro
Public
Sub Main()
Dim swModelDoc
As ModelDoc2
Dim swModelDocExt
As ModelDocExtension
Dim swPackAndGo
As PackAndGo
Dim openFile
As
String
Dim
myFileName As
String
Dim
pgFileNames As
Object =
Nothing
Dim
pgFileStatus As
Object =
Nothing
Dim
pgSetFileNames() As
String
Dim
pgGetFileNames As
Object
Dim
pgDocumentStatus As
Object
Dim
status As
Boolean
Dim
warnings As
Integer
Dim
errors As
Integer
Dim
i As
Integer
Dim
j As
Integer
Dim
namesCount As
Integer
Dim
myPath As
String
Dim
statuses As
Object
' Open assembly
openFile =
"C:\Program Files\SolidWorks Corp\SolidWorks\samples\tutorial\advdrawings\handle.sldasm"
swModelDoc = swApp.OpenDoc6(openFile,
swDocumentTypes_e.swDocASSEMBLY, swOpenDocOptions_e.swOpenDocOptions_Silent,
"", errors, warnings)
swModelDocExt = swModelDoc.Extension
' Get Pack and Go object
Debug.Print("Pack
and Go")
swPackAndGo = swModelDocExt.GetPackAndGo
' Get number of documents in assembly
namesCount = swPackAndGo.GetDocumentNamesCount
Debug.Print(" Number of model documents: "
& namesCount)
' Include any drawings and
simulation results
swPackAndGo.IncludeDrawings =
True
Debug.Print(" Include
drawings: " & swPackAndGo.IncludeDrawings)
swPackAndGo.IncludeSimulationResults
= True
Debug.Print(" Include simulation results:
" & swPackAndGo.IncludeSimulationResults)
' Get current paths and filenames of the
assembly's documents
status = swPackAndGo.GetDocumentNames(pgFileNames)
Debug.Print("")
Debug.Print(" Current path and filenames:
")
If
Not pgFileNames
Is
Nothing 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)
Debug.Print("")
Debug.Print(" Current default save-to
filenames: ")
If
Not pgFileNames
Is
Nothing Then
For
i = 0 To UBound(pgFileNames)
Debug.Print(" The path and
filename is: " & pgFileNames(i))
Next i
End
If
' Folder where to
save the files
myPath =
"C:\temp\"
' 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"
ElseIf
InStr(LCase(myFileName), "sldasm")
> 0 Then
myFileName = j &
".sldasm"
ElseIf
InStr(LCase(myFileName), "slddrw")
> 0 Then
myFileName = j &
".slddrw"
Else
' Only
packing up SolidWorks files
Exit
Sub
End
If
pgSetFileNames(i) = myPath & myFileName
Debug.Print(" My path and filename
is: " & pgSetFileNames(i))
j = j + 1
Next i
' If a drawing document existed for the assembly or part document
' used in this example, then you have
to ensure that the
' drawing document copied by Pack and
Go references the assembly
' or part document copied by Pack and
Go and not the original
' assembly or part document
' Calling IPackAndGo::SetSaveToName
sets the target for drawings
' included in Pack and Go and
overrides a call to
' IPackAndGo::SetDocumentSaveToNames
' status = swPackAndGo.SetSaveToName(True,
myPath)
' Set document paths and filenames for Pack
and Go
status = swPackAndGo.SetDocumentSaveToNames(pgSetFileNames)
' Add a prefix and suffix to the filenames
swPackAndGo.AddPrefix =
"SW"
swPackAndGo.AddSuffix =
"PackAndGo"
' Verify document
paths and filenames after adding prefix and suffix
ReDim
pgGetFileNames(namesCount - 1)
ReDim
pgDocumentStatus(namesCount - 1)
status = swPackAndGo.GetDocumentSaveToNames(pgGetFileNames,
pgDocumentStatus)
Debug.Print("")
Debug.Print(" My Pack and Go path and
filenames after adding prefix and suffix: ")
For i = 0
To (namesCount - 1)
Debug.Print(" My path and filename
is: " & pgGetFileNames(i))
Next i
' Pack and Go
statuses = swModelDocExt.SavePackAndGo(swPackAndGo)
End
Sub
'''
<summary>
''' The SldWorks swApp
variable is pre-assigned for you.
'''
</summary>
Public
swApp As SldWorks
End
Class