Remove Part in Pack and Go (VB.Net)

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

'''


''' The SldWorks swApp variable is pre-assigned for you.
'''

Public swApp As SldWorks

End Class

SolidworksApi macros
Answers 6
KR
KR 2012-03-06 Best Answer

James,

You need to use IPackAndGo::GetDocumentSaveToNames to get the array of strings of paths and file-names.  Specify an empty string for the element that you want removed.  Apply this new array to the Pack and Go using IPackAndGo::SetDocumentSaveToNames.

See the remarks for IPackAndGo::SetDocumentSaveToNames, which is where I got this information.

Hope this helps.

Keith

Training for the SolidWorks API

JG
JG 2012-03-06

Keith,

I have tried removing values from the array and making certain values in the array equal to nothing but even when I do that it still saves all the referenced documents to the specified folder.  Not sure if I am removing values from the array properly or what. Any ideas on the best way to recognize the values in the array and remove what you want?

Any help is greatly appreciated.

Thanks,

James

KR
KR 2012-03-07

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

JG
JG 2012-03-07

Keith,

Thanks, just what i needed.

James

JG
JG 2012-03-07

Using the API coding can you zip the created pack and go file? 

KR
KR 2012-03-08

James,

You have to use IPackAndGo::SetSaveToName to save as zip, which will override the values you specified in IPackAndGo::SetDocumentSaveToNames.  However when this is done, whatever modifications you made to exclude certain files are ignored.

So obviously this interface was poorly thought out.  You could probably get an enhancement request with API support (apisupport@solidworks.com) created if you wanted.  Nevertheless I can think of a workaround for your situation:

Save out the pack and go to a separate folder and then using System.IO.Packaging to zip up those files.  Then you could use functions from System.IO.File to delete the excess files.  I haven't tested this out, but here is some code that might be easy to implement to help you out: http://www.codeproject.com/Articles/28107/Zip-Files-Easy

Keith

Video Tutorials for the SolidWorks API