5 Replies Latest reply on Mar 18, 2013 5:54 PM by Ross Doyle

    Pack and Go - API Help

    Ross Doyle

      Hello,

       

      I am using VB.net to automate the pack and go process for an assembly.  The Solidworks API help actually has some code for how to do this.  My problem is that the code does not work as I think it was intended to.

       

      The code produces new assembly, part, and drawing files in the existing folder but does not add anything to the intended folder.  The solidworks files in the existing folder do get the prefix and suffix that are part of the code.

       

      Does anyone have suggestions on what needs to change so that the output files go to the new folder that I created (the intended path)?

       

      Code and output from the immediate window below:

       

      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

       

              Dim swApp As SldWorks.SldWorks

              'Opens Solidworks

              swApp = New SldWorks.SldWorks()

       

              ' Open assembly

              openFile = "C:\Workzones\Club House\Club House.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

       

      End Module

       

       

      Immediate Window

      Pack and Go

        Number of model documents: 5

        Include drawings: True

        Include simulation results: True

       

        Current path and filenames:

          The path and filename is: c:\workzones\club house\club house.sldasm

          The path and filename is: c:\workzones\club house\house.sldprt

          The path and filename is: c:\workzones\club house\outside stringer.sldprt

          The path and filename is: c:\workzones\club house\step.sldprt

          The path and filename is: c:\workzones\club house\inside stringer.sldprt

          The path and filename is: c:\workzones\club house\club house.slddrw

       

        Current default save-to filenames:

         The path and filename is: c:\workzones\club house\club house.sldasm

         The path and filename is: c:\workzones\club house\house.sldprt

         The path and filename is: c:\workzones\club house\outside stringer.sldprt

         The path and filename is: c:\workzones\club house\step.sldprt

         The path and filename is: c:\workzones\club house\inside stringer.sldprt

         The path and filename is: c:\workzones\club house\club house.slddrw

       

        My Pack and Go path and filenames before adding prefix and suffix:

          My path and filename is: C:\temp\0.sldasm

          My path and filename is: C:\temp\1.sldprt

          My path and filename is: C:\temp\2.sldprt

          My path and filename is: C:\temp\3.sldprt

          My path and filename is: C:\temp\4.sldprt

       

        My Pack and Go path and filenames after adding prefix and suffix:

          My path and filename is: c:\workzones\club house\SWclub housePackAndGo.sldasm

          My path and filename is: c:\workzones\club house\SWhousePackAndGo.sldprt

          My path and filename is: c:\workzones\club house\SWoutside stringerPackAndGo.sldprt

          My path and filename is: c:\workzones\club house\SWstepPackAndGo.sldprt

          My path and filename is: c:\workzones\club house\SWinside stringerPackAndGo.sldprt

        • Re: Pack and Go - API Help
          Michael Spens

          Hello Ross,

               Notice that the return "status" value is False here in your code just before setting the prefix and suffix.  SetDocumentSaveToNames is failing.

           

                    ' Set document paths and filenames for Pack and Go

                  status = swPackAndGo.SetDocumentSaveToNames(pgSetFileNames)

           

               SetDocumentSaveToNames requires an array the same size as GetDocumentNames returns - it has to match so it knows what to rename. 

               One solution would be to change the For block counter where you create your own file names.  Use pgFileNames.Length - 1 instead of namesCount - 1.  That will ensure the array length is the same.

               The API sample code was likely tested on an assembly without any parts used in both a top assembly and sub assembly.  Hope that helps!

           

          Mike

            • Re: Pack and Go - API Help
              Ross Doyle

              Mike,

               

              Excellent suggestion.  The new parts, assembly, and drawing were created in the intended folder. 

               

              After reviewing the new files, I still have 2 issues with my code. 

               

              First, the new file names have the prefix and suffix that are built into the code but the middle portion of the name is getting a number (from 0 to 5).  I think the code is intended to keep the names of the original files in the new folder.  Is there a straightforward way to change the code so that the "main" portion of the name is the original file name? 

               

              Secondly, the drawing in the new folder still has a reference to the original file.  This may be related to the naming issue by I am not sure.

               

               

              Here is the results from the Immediate Window:

               

              Pack and Go

                Number of model documents: 5

                Include drawings: True

                Include simulation results: True

               

                Current path and filenames:

                  The path and filename is: c:\workzones\club house\club house.sldasm

                  The path and filename is: c:\workzones\club house\house.sldprt

                  The path and filename is: c:\workzones\club house\outside stringer.sldprt

                  The path and filename is: c:\workzones\club house\step.sldprt

                  The path and filename is: c:\workzones\club house\inside stringer.sldprt

                  The path and filename is: c:\workzones\club house\club house.slddrw

               

                Current default save-to filenames:

                 The path and filename is: c:\workzones\club house\club house.sldasm

                 The path and filename is: c:\workzones\club house\house.sldprt

                 The path and filename is: c:\workzones\club house\outside stringer.sldprt

                 The path and filename is: c:\workzones\club house\step.sldprt

                 The path and filename is: c:\workzones\club house\inside stringer.sldprt

                 The path and filename is: c:\workzones\club house\club house.slddrw

               

                My Pack and Go path and filenames before adding prefix and suffix:

                  My path and filename is: C:\temp\0.sldasm

                  My path and filename is: C:\temp\1.sldprt

                  My path and filename is: C:\temp\2.sldprt

                  My path and filename is: C:\temp\3.sldprt

                  My path and filename is: C:\temp\4.sldprt

                  My path and filename is: C:\temp\5.slddrw

               

                My Pack and Go path and filenames after adding prefix and suffix:

                  My path and filename is: C:\temp\SW0PackAndGo.sldasm

                  My path and filename is: C:\temp\SW1PackAndGo.sldprt

                  My path and filename is: C:\temp\SW2PackAndGo.sldprt

                  My path and filename is: C:\temp\SW3PackAndGo.sldprt

                  My path and filename is: C:\temp\SW4PackAndGo.sldprt

                • Re: Pack and Go - API Help
                  Keith Rice

                  Ross,

                   

                  The IPackAndGo interface, unfortunately, is pretty buggy. I have documented quite a few of the bugs (and, when possible, their workarounds) in a macro called "Create pack and go with specified custom property as prefix", which is located in our Macro Library. (requires a free membership) It is written in VBA but you may be able to convert it over to VB.NET without too much trouble.

                   

                  More specifically, IPackAndGo::IncludeDrawings has a bug that is documented in SPR 634905: "IPackAndGo::IncludeDrawings set TRUE and IPackAndGo::AddPrefix or ::AddSuffix used does not copy the drawing with updated references". So basically you can get around it if you are not adding prefixes or suffixes.

                   

                  All is not lost, however. You can replace references using ISldWorks::ReplaceReferencedDocument. Note that the document with references that you wish to modify cannot be open when you use this method. Check out the API Help example for this method as well.

                   

                  As for your issue "the middle portion of the name is getting a number (from 0 to 5)": this is occurring because the counter "j" is being used as the new names for the documents. If you want the original names then you need to use IPackAndGo::GetDocumentNames which will return an array of the original document names. You can then parse out the document names using string manipulation functions, then append them to the new folder path, then take these strings (in the form of an array) and put it back into IPackAndGo::SetDocumentSaveToNames. You can still add prefixes and suffixes, of course.

                   

                  You might try using the macro at my web site as the basis for your macro. It will probably take you farther than the API Help example you are looking at.

                   

                  Keith

                  Video Tutorials for the SolidWorks API

                    • Re: Pack and Go - API Help
                      Ross Doyle

                      Thanks Keith,

                       

                      Your suggestion was very helpful and the website that you referred me to is a great resource.  I wish I knew about it 2 years ago when I first started tinkering with Solidworks API.

                       

                      Thanks again,

                      Ross

                        • Re: Pack and Go - API Help
                          Ross Doyle

                          Keith,

                           

                          Do you or anyone else know how to turn off the "Flatten to single folder" option that pack and go has?

                           

                          When I manually pack and go an assembly, it is automatically turned on.  I am finding when I run my code that it flattening all the files to a single folder and I assume it is possible to turn this feature off through the API.

                           

                          Thanks,

                          Ross