13 Replies Latest reply on Nov 28, 2015 10:46 AM by Franz Dahinden

    get current sheet as PDF

    Franz Dahinden

      someone know how to change it. I like to get only current sheet. when use the macro. Thanks a lot.

       

      Sub main()

      Dim swApp           As SldWorks.SldWorks
      Dim swModel         As SldWorks.ModelDoc2
      Dim swDrawModel     As SldWorks.ModelDoc2
      Dim swDraw          As SldWorks.DrawingDoc
      Dim swView          As SldWorks.View
      Dim nErrors         As Long
      Dim nWarnings       As Long
      Dim Revision As String

      Set swApp = Application.SldWorks
      Set swDrawModel = swApp.ActiveDoc

      If swDrawModel Is Nothing Then
              MsgBox "There is no active drawing document"
              Exit Sub
      End If
         
      If swDrawModel.GetType <> swDocDRAWING Then
              MsgBox "Open a drawing first and then TRY again!"
              Exit Sub
      End If

      Set swDraw = swDrawModel

      Set swView = swDraw.GetFirstView
      Set swView = swView.GetNextView
      Set swModel = swView.ReferencedDocument

      If swModel.GetPathName = "" Then
              MsgBox "Insert a View first and then TRY again!"
              Exit Sub
      End If

      Index = swModel.GetCustomInfoValue("", "Index") ' Change Revision property name here if different

      If Index = "" Then

              Index = ""

      Else

          Index = "_(" & Index & ")"

      End If

      swDraw.SaveAs3 Left(swDraw.GetPathName, InStrRev(swDraw.GetPathName, ".") - 1) & Prop & Index & ".PDF", 0, 0

      End Sub

        • Re: get current sheet as PDF
          Robert Kite

          Hi Franz

           

          You'll need to change:

          swDraw.SaveAs3 Left(swDraw.GetPathName, InStrRev(swDraw.GetPathName, ".") - 1) & Prop & Index & ".PDF", 0, 0

           

          to be...

          'Get PDF export Data

          Dim swExportPDFData as SldWorks.ExportPdfData

          Set swExportPDFData = swApp.GetExportFileData(1)

           

          'Set the PDF to only include current sheet

          Dim boolstatus As Boolean

          boolstatus = swExportPDFData.SetSheets(swExportDataSheetsToExport_e.swExportData_ExportCurrentSheet, Nothing)

           

          'Save out the PDF

          boolstatus = swDrawModel.Extension.SaveAs(swDraw.GetPathName, 0, 1, swExportPDFData, 0, 0)

           

          Hope that helps!

           

          Rob

            • Re: get current sheet as PDF
              Franz Dahinden

              I need also the Index from part, is lost

                • Re: get current sheet as PDF
                  Amen Allah Jlili

                  Cheers,

                   

                  Sub main()
                  Dim swApp          As SldWorks.SldWorks
                  Dim swModel        As SldWorks.ModelDoc2
                  Dim swDrawModel    As SldWorks.ModelDoc2
                  Dim swDraw          As SldWorks.DrawingDoc
                  Dim swView          As SldWorks.View
                  Dim nErrors        As Long
                  Dim nWarnings      As Long
                  Dim Revision As String
                  Set swApp = Application.SldWorks
                  Set swDrawModel = swApp.ActiveDoc
                  If swDrawModel Is Nothing Then
                          MsgBox "There is no active drawing document"
                          Exit Sub
                  End If
                      
                  If swDrawModel.GetType <> swDocDRAWING Then
                          MsgBox "Open a drawing first and then TRY again!"
                          Exit Sub
                  End If
                  Set swDraw = swDrawModel
                  Set swView = swDraw.GetFirstView
                  Set swView = swView.GetNextView
                  Set swModel = swView.ReferencedDocument
                  If swModel.GetPathName = "" Then
                          MsgBox "Insert a View first and then TRY again!"
                          Exit Sub
                  End If
                  Index = swModel.GetCustomInfoValue("", "Index") ' Change Revision property name here if different
                  If Index = "" Then
                          Index = ""
                  Else
                      Index = "_(" & Index & ")"
                  End If
                  'Get PDF export Data
                  Dim swExportPDFData as SldWorks.ExportPdfData
                  Set swExportPDFData = swApp.GetExportFileData(1)
                  
                  'Set the PDF to only include current sheet
                  Dim boolstatus As Boolean
                  boolstatus = swExportPDFData.SetSheets(swExportDataSheetsToExport_e.swExportData_ExportCurrentSheet, Nothing)
                  
                  'Save out the PDF
                  boolstatus = swDrawModel.Extension.SaveAs(Left(swDraw.GetPathName, InStrRev(swDraw.GetPathName, ".") - 1) & Prop & Index & ".PDF", 0, 1, swExportPDFData, 0, 0)
                  End Sub
                  
                  • Re: get current sheet as PDF
                    Robert Kite

                    You're using an obsolete call to obtain the custom property currently, but they generally still work (I've not tested in this case). My solution below is a bit lazy as I use Get (ICustomPropertyManager) rather than the superseding Get5, simply because it's less work in this case.

                     

                    Replace:

                    Index = swModel.GetCustomInfoValue("", "Index") ' Change Revision property name here if different

                    If Index = "" Then

                            Index = ""

                    Else

                        Index = "_(" & Index & ")"

                    End If

                     

                    With...

                    'Get the general custom property manager for the model

                    Dim cpm As CustomPropertyManager

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

                     

                    'Get the custom property called Index

                    Dim index As String

                    index = cpm.Get("Index")

                     

                    'Format the index string, assuming something was returned

                    If index <> "" Then

                         index = "_(" & index & ")"

                    End If

                      • Re: get current sheet as PDF
                        Franz Dahinden

                        Sorry my question, is it possible to add the sheet Name?

                          • Re: get current sheet as PDF
                            Amen Allah Jlili

                            Sure man, anything for my SW peep.

                             

                            Just make sure that the sheet name does not contain any file-naming illegal character.

                             

                            Sub main()
                            Dim swApp          As SldWorks.SldWorks
                            Dim swModel        As SldWorks.ModelDoc2
                            Dim swDrawModel    As SldWorks.ModelDoc2
                            Dim swDraw          As SldWorks.DrawingDoc
                            Dim swView          As SldWorks.View
                            Dim nErrors        As Long
                            Dim nWarnings      As Long
                            Dim Revision As String
                            Set swApp = Application.SldWorks
                            Set swDrawModel = swApp.ActiveDoc
                            If swDrawModel Is Nothing Then
                                    MsgBox "There is no active drawing document"
                                    Exit Sub
                            End If
                               
                            If swDrawModel.GetType <> swDocDRAWING Then
                                    MsgBox "Open a drawing first and then TRY again!"
                                    Exit Sub
                            End If
                            Set swDraw = swDrawModel
                            Set swView = swDraw.GetFirstView
                            Set swView = swView.GetNextView
                            Set swModel = swView.ReferencedDocument
                            If swModel.GetPathName = "" Then
                                    MsgBox "Insert a View first and then TRY again!"
                                    Exit Sub
                            End If
                            Index = swModel.GetCustomInfoValue("", "Index") ' Change Revision property name here if different
                            If Index = "" Then
                                    Index = ""
                            Else
                                Index = "_(" & Index & ")"
                            End If
                            'Get PDF export Data
                            Dim swExportPDFData As SldWorks.ExportPdfData
                            Set swExportPDFData = swApp.GetExportFileData(1)
                            
                            'Set the PDF to only include current sheet
                            Dim boolstatus As Boolean
                            boolstatus = swExportPDFData.SetSheets(swExportDataSheetsToExport_e.swExportData_ExportCurrentSheet, Nothing)
                            ' Get current sheet name
                            Dim swsheet As sheet
                            Set swsheet = swDraw.GetCurrentSheet
                            'Save out the PDF
                            boolstatus = swDrawModel.Extension.SaveAs(Left(swDraw.GetPathName, InStrRev(swDraw.GetPathName, ".") - 1) & Prop & Index & swsheet.GetName & ".PDF", 0, 1, swExportPDFData, 0, 0)
                            End Sub