I am using the sample code supplied in the API help section for pack and go for adding or removing a non solidworks file from pack and go. I am trying to modify the code to remove the solidworks file that i do not want to save with pack and go. Anu ideas how to change the code to allow this behvior? Or any alternatives?
'General code from API Help
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System
Imports System.Diagnostics
Partial Class SolidWorksMacro
Public Sub Main()
Dim swModel As ModelDoc2
Dim swModelDocExtAs ModelDocExtension
Dim swPackAndGoAs PackAndGo
Dim openFile As String
Dim errors As Long
Dim warnings As Long
Dim status As Boolean
Dim pgFileNames As Object
Dim i As Long
Dim renderReferences As Object
Dim otherFiles(1) As String
Dim delOtherFiles(0) As String
Dim myPath As String
Dim statuses As Object
' Open assembly document
openFile = "C:\Program Files\SolidWorks Corp\SolidWorks\samples\tutorial\EDraw\claw\claw-mechanism.sldasm"
swModel = swApp.OpenDoc6(openFile, swDocumentTypes_e.swDocASSEMBLY, swOpenDocOptions_e.swOpenDocOptions_Silent,"", errors, warnings)
swModelDocExt = swModel.Extension
' Get Pack and Go object
Debug.Print("Pack and Go")
swPackAndGo = swModelDocExt.GetPackAndGo
' Get current paths and filenames of the assembly's documents
status = swPackAndGo.GetDocumentNames(pgFileNames)
Debug.Print("")
Debug.Print(" Add SolidWorks files' paths and filenames: ")
If Not IsNothing(pgFileNames) Then
For i = 0 To UBound(pgFileNames)
Debug.Print(" The path and filename is: " & pgFileNames(i))
Next i
End If
' Set document paths and names for Pack and Go
status = swPackAndGo.SetDocumentSaveToNames(pgFileNames)
' Get the render stock references in this assembly
' and print them to the Immediate window
Debug.Print(" ")
renderReferences = swModelDocExt.GetRenderStockReferences
Debug.Print(" Add render references:")
For i = 0 To UBound(renderReferences)
Debug.Print(" The path and filename is: " & renderReferences(i))
Next i
' Add render stock files to Pack and Go
status = swPackAndGo.AddExternalDocuments(renderReferences)
' Add other non-SolidWorks files to Pack and Go
otherFiles(0) = "c:\program files\solidworks corp\solidworks\samples\tutorial\edraw\claw\claw-mechanism.easm"
otherFiles(1) = "c:\program files\solidworks corp\solidworks\samples\tutorial\edraw\claw\claw-mechanism.emodel_debugonly.xml"
Debug.Print(" ")
Debug.Print(" Add non-SolidWorks files:")
For i = 0 To UBound(otherFiles)
Debug.Print(" The path and filename is: " & otherFiles(i))
Next i
status = swPackAndGo.AddExternalDocuments(otherFiles)
' Remove one of the non-SolidWorks files from Pack and Go
Debug.Print(" ")
Debug.Print(" Remove non-SolidWorks file:")
delOtherFiles(0) = otherFiles(0)
Debug.Print(" The path and filename is: " & delOtherFiles(0))
status = swPackAndGo.RemoveExternalDocuments(delOtherFiles)
' Override path where to save documents
myPath = "c:\temp\"
status = swPackAndGo.SetSaveToName(True, myPath)
' Pack and Go both SolidWorks and non-SolidWorks files
statuses = swModelDocExt.SavePackAndGo(swPackAndGo)
End Sub
''' <summary>
''' The SldWorks swApp variable is pre-assigned for you.
''' </summary>
Public swApp As SldWorks
End Class
James,
It works for me. Please see code below. (For the record, using a list wasn't really necessary. You could dimension strFinalPaths immediately after using IPackAndGo::GetDocumentSaveToNames, and simply add the new paths to that.)
===========
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
'Preconditions: "C:\test" path exists
'Results: Pack and Go is created in C:\test, with any files containing "exclude" removed
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System
Imports System.Diagnostics
Imports System.Collections.Generic
Partial Class SolidWorksMacro
Dim swApp As SldWorks
Dim swModel As ModelDoc2
Dim swPackAndGo As PackAndGo
Dim oPaths As Object
Dim listPaths As New List(Of String)
Dim strCurrentPath As String
Dim strSaveToPath As String
Dim oResults As Object
Public Sub main()
strSaveToPath = "C:\test\"
swModel = swApp.ActiveDoc
swPackAndGo = swModel.Extension.GetPackAndGo
swPackAndGo.GetDocumentSaveToNames(oPaths, Nothing)
For i As Integer = 0 To UBound(oPaths)
strCurrentPath = Left(oPaths(i), InStrRev(oPaths(i), "\"))
strCurrentPath = Replace(oPaths(i), strCurrentPath, strSaveToPath)
If InStr(strCurrentPath, "exclude") <> 0 Then strCurrentPath = Nothing
listPaths.Add(strCurrentPath)
Debug.Print(listPaths.Item(i))
Next
Dim strFinalPaths(listPaths.Count - 1) As String
listPaths.CopyTo(strFinalPaths)
Dim retval As Boolean
retval = swPackAndGo.SetDocumentSaveToNames(strFinalPaths)
Debug.Print("Save-to names successfuly set in pack and go object: " & retval)
oResults = swModel.Extension.SavePackAndGo(swPackAndGo)
For i As Integer = 0 To UBound(oResults)
Debug.Print("Pack and go save status: " & oResults(i))
Next
End Sub
End Class
========
Keith
Video Tutorials for the SolidWorks API