4 Replies Latest reply on Sep 14, 2017 12:03 PM by Patrick Harper

    Saving a drawing as PDF and DXF in the same directory in a different folder

    Samuel Bailey

      Hello,

       

      My company doesn't have a PDM system so I'm using a folder structure to manage drawings, every project gets the structure;

      /ProjectNo./

                       /01 - CAD

                       /02 - PDF

                       /03 - DXF_DWG

       

       

      I'd like an API that will save a pdf and dxf to their respective folders. I am a newby to making API's, though I found one from Deepak that would search for a folder, but not within the same directory location - I think, maybe it could and I just don't know how to make it work correctly - found here --> https://forum.solidworks.com/thread/95244#497864

      I didn't want to have to select the folder location every time as it takes a while to get to the ProjectNo. I've also managed to get an API that will save a PDF and DXF to the same drawing location, but if they could be separated it'd be brilliant.

       

      Thank you

        • Re: Saving a drawing as PDF and DXF in the same directory in a different folder
          Bruce Perry

          I wrote the following code to create a PDF version with a date extension.

          Two things with this: 1. If you previously save this and run it again it will overwrite the file. Without notice.

                                             2. It will open Adobe, and you will have to close it before continuing.

          If you want you files to g somewhere else, you will need to change the path.

           

          Option Explicit

           

           

          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim swModelDocExt As SldWorks.ModelDocExtension

          Dim swExportData As SldWorks.ExportPdfData

          Dim MyFileName As String 'Returns File name with Extension

          Dim boolstatus As Boolean

          Dim lErrors As Long

          Dim lWarnings As Long

          Dim MyMonth

          Dim MyDay

          Dim MyYear

          Dim MyDateFormat

           

           

          Sub main()

           

           

          MyMonth = Format(Date, "MM")

          MyDay = Format(Date, "dd")

          MyYear = Format(Date, "yyyy")

          MyDateFormat = "_" + MyMonth + "-" + MyDay + "-" + MyYear

           

           

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

           

           

          If swModel Is Nothing Then

                  MsgBox "Sorry! Needs an Active File to Function."

              End

              End If

           

           

          If swModel.GetType <> swDocDRAWING Then

                  MsgBox "This Program only works on Drawings"

              End

              End If

                 

                  Set swModelDocExt = swModel.Extension

                  Set swExportData = swApp.GetExportFileData(swExportPdfData)

                  MyFileName = swModel.GetPathName

           

           

          If MyFileName = "" Then

                  MsgBox "Please save the file first and try again"

              End

              End If

                 

                  MyFileName = Strings.Left(MyFileName, Len(MyFileName) - 7) & MyDateFormat & ".PDF"

           

           

              boolstatus = swExportData.SetSheets(swExportData_ExportAllSheets, 1)

             ' Change File name to include new path.................

              'Perhaps MyFileName = "C:\My new file name\PDF" + MyFileName

              boolstatus = swModelDocExt.SaveAs(MyFileName, 0, 0, swExportData, lErrors, lWarnings)

             

          If boolstatus Then

                  'MsgBox "Save as PDF successful" & vbNewLine & MyFileName

              Else

                  MsgBox "Save as PDF failed, Error code:" & lErrors

              End If

           

           

          End Sub

          • Re: Saving a drawing as PDF and DXF in the same directory in a different folder
            Patrick Harper

            Hello,

             

            Our directory structure looks like this, very similar to yours Sam,

            /ProjectNo./

                             /CAD_Files

                             /History

                             /Production

            We keep our Solidworks drawings in the CAD_Files folder and pdf's made from those drawings are kept in the History folder.

            I have 2 macros that do different things:

            1) Save a pdf of the open drawing in the CAD_Files folder with a name made from custom properties of the assembly in the drawing.

            2) Save a pdf of the open drawing into the History folder.

            Method 1 will not place the pdf in another directory and method 2 will not change the pdf name.

            When I try to combine them I get either errors or it simple does nothing.

            Method 1)

            Dim swApp       As SldWorks.SldWorks
            Dim swModel     As SldWorks.ModelDoc2
            Dim swDraw      As SldWorks.DrawingDoc
            Dim swCustProp  As CustomPropertyManager
            Dim valOut1      As String
            Dim valOut2      As String

            Dim resolvedValOut1  As String

            Dim resolvedValOut2  As String

            Dim Filepath    As String
            Dim FileName    As String

            Set swApp = Application.SldWorks

            Set swDraw = swApp.ActiveDoc

             

            ' Check to see if a drawing is loaded.

             

            If (swDraw Is Nothing) Or (swDraw.GetType <> swDocDRAWING) Then

             

            swApp.SendMsgToUser ("To be used for drawings only, Open a drawing first and then TRY!")

             

            ' If no model currently loaded, then exit

            Exit Sub

             

            End If

             

            Set swView = swDraw.GetFirstView

            Set swView = swView.GetNextView

            Set swModel = swView.ReferencedDocument

            Set Sheet = swApp.ActiveDoc.GetCurrentSheet

             

             

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

             

            swCustProp.Get2 "DWG REV", valOut1, resolvedValOut1

            swCustProp.Get2 "DWG No.", valOut2, resolvedValOut2

             

            Filepath = Left(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\"))

             

            FileName = Left(swDraw.GetTitle, Len(swDraw.GetTitle) - 9)

             

            swDraw.SaveAs (Filepath + resolvedValOut2 + "-" + resolvedValOut1 + ".PDF")

              

            End Sub

             

            Method 2)

            Dim swApp As Object

            Dim Part As Object

            Dim boolstatus As Boolean

            Dim longstatus As Long, longwarnings As Long

            Dim DWGREV As String

            Dim swModel         As SldWorks.ModelDoc2

            Dim cusPropMgr As SldWorks.CustomPropertyManager

             

             

            Sub main()

             

            Set swApp = Application.SldWorks

            Set Part = swApp.ActiveDoc

            'Part.PrintDirect

            boolstatus = Part.ActivateView("Drawing View1")

            boolstatus = Part.Extension.SelectByID2("Drawing View1", "DRAWINGVIEW", 0, 0, 0, False, 0, Nothing, 0)

            Part.ClearSelection2 True

            boolstatus = Part.EditRebuild3()

             

            Path = Left(Part.GetPathName, InStrRev(Part.GetPathName, "\"))

            vSplit = Split(Path, "\")

            Dim i As Integer

                For i = 0 To UBound(vSplit) - 2

                    Filepath = Filepath & vSplit(i) & "\"

                Next i

                  

            Filepath = Filepath & "\History\"

            sPathName = Mid(Part.GetPathName, InStrRev(Part.GetPathName, "\") + 1)

            sPathName = Left(sPathName, InStrRev(sPathName, ".") - 1)

             

            If DWGREV = "" Then

                    DWGREV = ""

            End If

             

            cfg = "default"

             

            'Save as PDF

            longstatus = Part.SaveAs3(Filepath & sPathName & "DWGREV" & ".pdf", 0, 0)

            End Sub