8 Replies Latest reply on Jun 20, 2017 4:54 AM by Deepak Gupta

    Help Batch Saving Drawings As PDF - Configuration Specific Properties in File Name

    Quentin Torgerson

      Hi Everyone,

       

      I am attempting to use a macro to batch save all of the drawings in a folder as PDFs.  The goal is to save the PDF using a file name built on configuration specific custom properties.  Right now I'm having trouble getting the file names to build properly in all cases.

       

      I started with code taken from this post, (Macro Help: Open All Drawings In Folder and Save As PDF) and slightly modified for my needs.  In Matt Jones' words, the macro is meant to

       

      "

      -Open a Folder Selection Box (where the user selects a folder)

      -Open all the drawing files in the selected folder (one by one, one after the other)

      -Check to see if there is a folder called "PDF" in the directory, if not then create one

      -Save the open drawing file as a pdf, building the save as name from custom properties in the referenced model

      -Close the drawing

      -Move on to next one

      "

      My file name string is supposed to look like this, where italics are custom properties, sometimes configuration specific custom properties.

      "Number - Description Rev-Revision.pdf"

       

      What ends up happening, is that for some of the files the custom properties are returned as blank, so I end up saving over one PDF named " - Rev-.PDF".

      Some of my files work though, and it ends up saving them properly like "3534-001 - MOTOR BRACKET BOTTOM PLATE REV-A.PDF".

      And even still some others save as "3534-001 - MOTOR BRACKET BOTTOM PLATE REV-.PDF" which works except for the empty revision.

       

      I think the problem is tied to files with or without custom configurations.  It seems that the ones with custom configurations work rather well except for the revision, but those that are weldments or without custom configurations don't work at all.

       

      Here is my code, and the macro file is also attached if that's useful

       

      Option Explicit
      
      
      Dim swApp        As SldWorks.SldWorks
      Dim swModel      As SldWorks.ModelDoc2
      Dim sFileName    As String
      Dim vFileName    As String
      Dim Path         As String
      Dim nPath        As String
      Dim nErrors      As Long
      Dim nWarnings    As Long
      
      
      Dim swDraw As SldWorks.DrawingDoc
      Dim swCustProp As CustomPropertyManager
      Dim swView As SldWorks.View
      
      Dim ConfigName As String
      Dim i As Long
      
      
      Dim valOut1 As String
      Dim valOut2 As String
      Dim valOut3 As String
      Dim resolvedValOut1 As String
      Dim resolvedValOut2 As String
      Dim resolvedValOut3 As String
      
      
      
      
      Dim PartNo As String
      Dim nFileName As String
      Dim swDocs As Variant
      Dim PDFpath As String
      Dim currpath As String
      Dim PartNoDes As String
      
      
      
      Sub main()
      
      
          Set swApp = Application.SldWorks
         
          'added by Deepak's suggestion
          Dim swExportPDFData     As SldWorks.ExportPdfData
              Set swExportPDFData = swApp.GetExportFileData(1)
              swExportPDFData.ViewPdfAfterSaving = False
            
          Path = BrowseFolder() '"Select a Path/Folder"
         
          Path = Path + "\"
         
          'moved from below
          PDFpath = Path & "PDF"
          If Dir(PDFpath, vbDirectory) = "" Then MkDir PDFpath
         
          sFileName = Dir(Path & "*.slddrw")
         
         
          Do Until sFileName = ""
      
      
              Set swModel = swApp.OpenDoc6(Path + sFileName, swDocDRAWING, swOpenDocOptions_Silent, "", nErrors, nWarnings)
      
      
              Set swModel = swApp.ActiveDoc
              Set swDraw = swApp.ActiveDoc
              Set swView = swDraw.GetFirstView
              Set swView = swView.GetNextView
              Set swModel = swView.ReferencedDocument
             
          
               
              If swModel.GetType = swDocPART Then
      '            PartNoDes = Mid(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\") + 1)
      '            PartNoDes = Right(PartNoDes, Len(PartNoDes) - 14)
      '            PartNoDes = Left(PartNoDes, Len(PartNoDes) - 7)
      '            PartNo = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)
      '            PartNo = Left(PartNo, Len(PartNo) - 7)
      
      
                  If ((swView.ReferencedConfiguration <> "Default") Or (swView.ReferencedConfiguration <> "Default<As Machined>") _
                          Or (swView.ReferencedConfiguration <> "Default<As Welded>")) Then
                      Set swCustProp = swModel.Extension.CustomPropertyManager(swView.ReferencedConfiguration)
                  Else
                      Set swCustProp = swModel.Extension.CustomPropertyManager("")
                  End If
                     
                  ConfigName = swView.ReferencedConfiguration
                 
                  swCustProp.Get2 "Number", valOut1, resolvedValOut1
                  swCustProp.Get2 "Description", valOut2, resolvedValOut2
                  swCustProp.Get2 "Revision", valOut3, resolvedValOut3
                  nFileName = PDFpath & "\" & resolvedValOut1 & " - " & resolvedValOut2 & " REV-" & resolvedValOut3
      
      
      
      
                  swDraw.SaveAs3 nFileName & ".PDF", 0, 0
                         
              ElseIf swModel.GetType = swDocASSEMBLY Then
      '            PartNoDes = Mid(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\") + 1)
      '            PartNoDes = Right(PartNoDes, Len(PartNoDes) - 11)
      '            PartNoDes = Left(PartNoDes, Len(PartNoDes) - 7)
      '            PartNo = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)
      '            PartNo = Left(PartNo, Len(PartNo) - 7)
                  If (swView.ReferencedConfiguration <> "Default") Then
                      Set swCustProp = swModel.Extension.CustomPropertyManager(swView.ReferencedConfiguration)
                  Else
                      Set swCustProp = swModel.Extension.CustomPropertyManager("")
                  End If
                  ConfigName = swView.ReferencedConfiguration
      '            swCustProp.Get2 "Number", valOut1, resolvedValOut1
      '            swCustProp.Get2 "Description", valOut2, resolvedValOut2
      '            swCustProp.Get2 "Revision", valOut3, resolvedValOut3
      '            nFileName = PDFpath & "\" & resolvedValOut1 & " - " & resolvedValOut2 & " REV-" & resolvedValOut3
      
      
      
      
                  swDraw.SaveAs3 nFileName & ".PDF", 0, 0
                      
              End If
         
              swApp.QuitDoc swDraw.GetPathName
         
              Set swDraw = Nothing
          
              Set swModel = Nothing
          
              sFileName = Dir
        
          Loop
      
      
      MsgBox "All Done"
      
      
      End Sub
      
      
      
      
      Function BrowseFolder(Optional Title As String, Optional TopFolder _
                               As String) As String
          Dim objShell As New Shell32.Shell
          Dim objFolder As Shell32.Folder
      
      
      'If you use 16384 instead of 1 on the next line,
      'files are also displayed
          Set objFolder = objShell.BrowseforFolder(0, Title, 1, TopFolder)
         
          If Not objFolder Is Nothing Then
              BrowseFolder = objFolder.Items.Item.Path
          End If
         
      
      
      End Function
      

       

      Message was edited by: Quentin Torgerson Tried to make code appear as VBA syntax, having trouble

        • Re: Help Batch Saving Drawings As PDF - Configuration Specific Properties in File Name
          Deepak Gupta

          You can set an if condition to check if the property exists or not. If not then flag that file.

            • Re: Help Batch Saving Drawings As PDF - Configuration Specific Properties in File Name
              Quentin Torgerson

              I know that the property exists in all of the documents, but for some reason the property isn't being properly pulled into variables resolvedValOut1, 2, and 3.

               

              I'm attaching an example folder with the files that I'm using.  When I run the macro, I select the "Mechanical Drawings" folder.  If you look in the PDF folder you will see the result of the macro

                • Re: Help Batch Saving Drawings As PDF - Configuration Specific Properties in File Name
                  Deepak Gupta

                  Macro won't pull information from the configuration properties if configuration has either of these names:

                   

                  "Default"

                  "Default<As Machined>"

                  "Default<As Welded>"

                   

                  as set in these lines

                  If ((swView.ReferencedConfiguration <> "Default") Or (swView.ReferencedConfiguration <> "Default<As Machined>") _ 

                                      Or (swView.ReferencedConfiguration <> "Default<As Welded>")) Then 

                    • Re: Help Batch Saving Drawings As PDF - Configuration Specific Properties in File Name
                      Quentin Torgerson

                      I added those If statements in because it was changing the results for the better in some cases, I'm not sure why.  Even with the If statement removed and the command  "Set swCustProp = swModel.Extension.CustomPropertyManager(swView.ReferencedConfiguration)" running every time, I get the same results on the parts I put in that "Test" folder

                        • Re: Help Batch Saving Drawings As PDF - Configuration Specific Properties in File Name
                          Deepak Gupta

                          Since there are no configuration properties, it is giving you correct results. I commented these lines and it is working as expected. You need to rather check the properties value and see if configuration properties are empty then look in the custom properties.

                           

                            • Re: Help Batch Saving Drawings As PDF - Configuration Specific Properties in File Name
                              Quentin Torgerson

                              That one went right over my head.  I see what you were saying now.

                               

                              My code now runs properly after making Deepak's suggested changes.  It checks for the configuration specific custom properties and the general custom properties.  If the configuration specific custom properties do not exist, then it defaults to using the general custom properties. 

                               

                              I'm attaching my code if anyone needs it. 

                               

                              Thanks a lot for your help, Deepak

                               

                              Option Explicit
                              
                              
                              Dim swApp        As SldWorks.SldWorks
                              Dim swModel      As SldWorks.ModelDoc2
                              Dim sFileName    As String
                              Dim vFileName    As String
                              Dim Path         As String
                              Dim nPath        As String
                              Dim nErrors      As Long
                              Dim nWarnings    As Long
                              
                              
                              Dim swDraw As SldWorks.DrawingDoc
                              Dim swCustProp1 As CustomPropertyManager
                              Dim swCustProp2 As CustomPropertyManager
                              Dim swView As SldWorks.View
                              
                              Dim ConfigName As String
                              Dim i As Long
                              
                              
                              Dim valOut1 As String
                              Dim valOut2 As String
                              Dim valOut3 As String
                              Dim resolvedValOut1 As String
                              Dim resolvedValOut2 As String
                              Dim resolvedValOut3 As String
                              
                              
                              
                              
                              Dim PartNo As String
                              Dim nFileName As String
                              Dim swDocs As Variant
                              Dim PDFpath As String
                              Dim currpath As String
                              Dim PartNoDes As String
                              
                              
                                
                              Sub main()
                              
                              
                                  Set swApp = Application.SldWorks
                                  
                                  Dim swExportPDFData     As SldWorks.ExportPdfData
                                      Set swExportPDFData = swApp.GetExportFileData(1)
                                      swExportPDFData.ViewPdfAfterSaving = False
                                     
                                  Path = BrowseFolder()
                                  
                                  Path = Path + "\"
                                  
                                  'create the PDF directory if it doesn't exist
                                  PDFpath = Path & "PDF"
                                  If Dir(PDFpath, vbDirectory) = "" Then MkDir PDFpath
                                  
                                  'initialize Dir with the starting path and all files with .slddrw extension
                                  sFileName = Dir(Path & "*.slddrw")
                                  
                                  
                                  Do Until sFileName = ""
                              
                              
                                      Set swModel = swApp.OpenDoc6(Path + sFileName, swDocDRAWING, swOpenDocOptions_Silent, "", nErrors, nWarnings)
                              
                              
                                      Set swModel = swApp.ActiveDoc
                                      Set swDraw = swApp.ActiveDoc
                                      Set swView = swDraw.GetFirstView
                                      Set swView = swView.GetNextView
                                      Set swModel = swView.ReferencedDocument
                                      
                                      'object associated to general custom properties
                                      Set swCustProp1 = swModel.Extension.CustomPropertyManager("")
                                      'object associated to selected configuration's custom property
                                      Set swCustProp2 = swModel.Extension.CustomPropertyManager(swView.ReferencedConfiguration)
                                      
                                      'get all configuration specific custom properties
                                      swCustProp2.Get2 "Number", valOut1, resolvedValOut1
                                      swCustProp2.Get2 "Description", valOut2, resolvedValOut2
                                      swCustProp2.Get2 "Revision", valOut3, resolvedValOut3
                                      
                                      'if a configuration specific property is returned, use the general custom property
                                      If (resolvedValOut1 = "") Then
                                          swCustProp1.Get2 "Number", valOut1, resolvedValOut1
                                      End If
                                      
                                      If (resolvedValOut2 = "") Then
                                          swCustProp1.Get2 "Description", valOut2, resolvedValOut2
                                      End If
                                      
                                      If (resolvedValOut3 = "") Then
                                          swCustProp1.Get2 "Revision", valOut3, resolvedValOut3
                                      End If
                                      
                                      'make file name out of concatenated custom properties
                                      nFileName = PDFpath & "\" & resolvedValOut1 & " - " & resolvedValOut2 & " REV-" & resolvedValOut3
                                      swDraw.SaveAs3 nFileName & ".PDF", 0, 0
                                      
                                      
                                      swApp.QuitDoc swDraw.GetPathName
                                      Set swDraw = Nothing
                                      Set swModel = Nothing
                                   
                                      'move on to the next file in the directory
                                      sFileName = Dir
                                 
                                  Loop
                              
                              
                              MsgBox "All Done"
                              
                              
                              End Sub
                              
                              
                              
                              
                              Function BrowseFolder(Optional Title As String, Optional TopFolder _
                                                       As String) As String
                                  Dim objShell As New Shell32.Shell
                                  Dim objFolder As Shell32.Folder
                              
                              
                              'If you use 16384 instead of 1 on the next line,
                              'files are also displayed
                                  Set objFolder = objShell.BrowseforFolder(0, Title, 1, TopFolder)
                                  
                                  If Not objFolder Is Nothing Then
                                      BrowseFolder = objFolder.Items.Item.Path
                                  End If
                                  
                                
                              End Function
                              
                    • Re: Help Batch Saving Drawings As PDF - Configuration Specific Properties in File Name
                      Graham Thornton

                      Can anyone please explain how I can change the macro to just save the pdf files with the same file name as the document being opened in solidworks while still using all the other features?

                       

                      Thanks

                       

                      Graham

                        • Re: Help Batch Saving Drawings As PDF - Configuration Specific Properties in File Name
                          Deepak Gupta

                          Replace these lines

                          'object associated to general custom properties 

                                  Set swCustProp1 = swModel.Extension.CustomPropertyManager("") 

                                  'object associated to selected configuration's custom property 

                                  Set swCustProp2 = swModel.Extension.CustomPropertyManager(swView.ReferencedConfiguration) 

                                   

                                  'get all configuration specific custom properties 

                                  swCustProp2.Get2 "Number", valOut1, resolvedValOut1 

                                  swCustProp2.Get2 "Description", valOut2, resolvedValOut2 

                                  swCustProp2.Get2 "Revision", valOut3, resolvedValOut3 

                                   

                                  'if a configuration specific property is returned, use the general custom property 

                                  If (resolvedValOut1 = "") Then 

                                      swCustProp1.Get2 "Number", valOut1, resolvedValOut1 

                                  End If 

                                   

                                  If (resolvedValOut2 = "") Then 

                                      swCustProp1.Get2 "Description", valOut2, resolvedValOut2 

                                  End If 

                                   

                                  If (resolvedValOut3 = "") Then 

                                      swCustProp1.Get2 "Revision", valOut3, resolvedValOut3 

                                  End If 

                                   

                                  'make file name out of concatenated custom properties 

                                  nFileName = PDFpath & "\" & resolvedValOut1 & " - " & resolvedValOut2 & " REV-" & resolvedValOut3 

                                 swDraw.SaveAs3 nFileName & ".PDF", 0, 0 

                           

                          With

                          nFileName  = = Mid(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\") + 1) 

                          nFileName  = Left(nFileName, InStrRev(nFileName, ".") ) 

                          swDraw.SaveAs3 PDFpath & "\" & nFileName & ".PDF", 0, 0