4 Replies Latest reply on Jul 20, 2018 3:08 PM by Dan Frey

    Tweaking a working "Save as PDF" macro to only look at active assembly file

    Dan Frey

      Hello,

       

      I have a macro to save drawings as PDFs. It is adapted from one that Deepak Gupta wrote and shared on another thread ( https://forum.solidworks.com/thread/83172 )   It has the following functionality:

       

      - If a part file is active, this macro will save a PDF of the corresponding drawing if one exists.

      - If a drawing file is active, this macro will save a PDF of the drawing.

      - If an assembly file is active, this macro will save a PDF of all drawings corresponding to models in the assembly tree that have drawings, including a PDF of the assembly drawing.

      - The macro also rebuilds each drawing prior to saving it.

      - The user has the option to save in the current working directory or select a different location.

       

      I am happy with all of this. The macro works well other than one important issue: If the file that is active is an assembly file, the macro cycles through the entire assembly tree and saves PDFs as I want it to, but THEN it moves on to other documents that are currently open. This is a problem because my company frequently makes changes to assemblies for entire machines that may have 100+ drawing files, and our typical workflow is to open the top level assembly then follow the tree down to the subassembly for the part of the machine that we need to modify or add to. We would typically use the macro to make all the PDFs for these subassemblies only, so I don't want it to go all the way through the top level assembly.

       

      I have only a little bit of experience with API. I know enough to understand that the macro is cycling through other open documents because I am using the EnumDocuments method, which is what Deepak used in his original code. However, I am not sure how to modify my code to only cycle through the assembly tree in the currently active assembly and ignore other files that are currently open.

       

      Is there a simple fix to this? I have pasted the relevant part of my code below and also attached the entire macro code to my message. Thanks in advance!

         
          'Create PDFs of all drawings corresponding to models in tree of currently open assembly (if applicable)
          If FirstDoc.GetType = swDocASSEMBLY Then
         
              'Cycle through assembly tree looking for parts or subassemblies that have drawings
              swAllDocs.Reset
              swAllDocs.Next 1, swDoc, NumDocsReturned ' go to first document
              While NumDocsReturned <> 0
                  bDocWasVisible = swDoc.Visible
                  'swApp.ActivateDoc swDoc.GetPathName'
                 
                  'Determine if a particular doc has a drawing
                  DwgPath = swDoc.GetPathName
                  If (LCase(Right(DwgPath, 3)) <> "drw") And (DwgPath <> "") Then
                      DwgPath = Left(DwgPath, Len(DwgPath) - 3) & "drw"
                      Set myDwgDoc = swApp.OpenDoc6(DwgPath, swDocDRAWING, swOpenDocOptions_Silent, "", OpenErrors, OpenWarnings)
                      If Not myDwgDoc Is Nothing Then 'drawing found
                         
                          'Activate drawing
                          swApp.ActivateDoc myDwgDoc.GetPathName
                          Set Part = swApp.ActiveDoc()

                          'Force rebuild of drawing
                          Part.Rebuild (swRebuildAll)
                         
                          'Save PDF of drawing
                          drwPathName = Part.GetPathName()
                          pdfPathName = fso.BuildPath(pdfFolderName, fso.GetBaseName(drwPathName))
                          Set swExportPDFData = swApp.GetExportFileData(1)
                          swExportPDFData.ViewPdfAfterSaving = False
                          Part.Extension.SaveAs pdfPathName + ".pdf", 0, 0, swExportPDFData, lErrors, lWarnings

                          'Increment PDF count and quit drawing document
                          DocCount = DocCount + 1
                          swApp.QuitDoc (Part.GetTitle)
                          Set myDwgDoc = Nothing
                      End If
                  End If
                  swAllDocs.Next 1, swDoc, NumDocsReturned 'go to next document
                  Wend
                 
                  swApp.ActivateDoc FirstDoc.GetPathName 'return to document that was originally open

              GoTo MainExit:
          End If