SolidWorks API Tip #3: Export to DXF & PDF

Our API tips series will build on knowledge from previous posts, so be sure to check out our previous post.

Another common request I often receive is to save files in various formats… exporting sheet metal as DXF or opening drawings and saving as PDF. Today, we’ll cover how to accomplish both of these tasks with the SolidWorks API.

Say we have a sheet metal part file open, and we want to export it as a DXF, find it’s drawing and save that as a PDF (to keep things simple, let’s assume we KNOW the drawing file shares the same name as the part, and exists in the same folder). The main method for our macro will be as follows:

Dim swApp As SldWorks.SldWorks
Sub main()

    Set swApp = Application.SldWorks
    
    Dim swModel As ModelDoc2
    Set swModel = swApp.ActiveDoc
    
    Dim modelPath As String
    modelPath = LCase(swModel.GetPathName)

    SaveAsDXF swModel

    Dim swDwg As ModelDoc2
    Set swDwg = OpenDwg(modelPath)

    SaveAsPDF swDwg
    
End Sub

In the main sub above, have specified 3 methods that we need to define. First, the method to export as a DXF:

Private Sub SaveAsDXF(swModel As ModelDoc2)

    Dim exportPath As String
    exportPath = LCase(swModel.GetPathName)
    exportPath = Replace(exportPath, ".sldprt", ".dxf")
    
    Dim swPart As PartDoc
    Set swPart = swModel

    smOptions = 13   '0001101 - SheetMetalOptions bitmask: include flat pattern geometry, bend lines and sketches
    
    Debug.Print exportPath

    swPart.ExportToDWG2 exportPath, swPart.GetPathName, 1, True, Nothing, False, False, smOptions, Nothing

End Sub

There is one sneaky concept in the code above, SheetMetalOptions bitmask. A bitmask is a fancy word for a binary number used to encode different options by enabling or disabling the ‘bit’ (i.e. a 1 or 0) associated with each option. See the API documentation for ExportToDwg2 for more info.

Next, the method to open the drawing:

Private Function OpenDwg(modelPath As String) As ModelDoc2
    
    Dim dwgPath As String
    dwgPath = LCase(modelPath)
    dwgPath = Replace(dwgPath, ".sldprt", ".slddrw")
    
    Dim swDocSpec As SldWorks.DocumentSpecification
    Set swDocSpec = swApp.GetOpenDocSpec(dwgPath)
    
    Dim swDwg As ModelDoc2
    Set swDwg = swApp.OpenDoc7(swDocSpec)
    
    Set OpenDwg = swDwg

End Function

Now, to save the drawing as a PDF, we’ll create a SaveAsPDF method:

Private Sub SaveAsPDF(swDwg As ModelDoc2)

    Dim saveErrors As Long
    Dim saveWarnings As Long
    
    Dim exportPath As String
    exportPath = Replace(swDwg.GetPathName, ".slddrw", ".pdf")
    
    'get drawing sheets
    Dim vSheetNames As Variant
    vSheetNames = swDwg.GetSheetNames
    
    'get PDF interface
    Dim swExportPDFData As ExportPdfData
    Set swExportPDFData = swApp.GetExportFileData(swExportDataFileType_e.swExportPDFData)
    
    swExportPDFData.SetSheets swExportDataSheetsToExport_e.swExportData_ExportAllSheets, vSheetNames
    
    swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swPDFExportHighQuality, True 'set output line quality to high
    swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swPDFExportInColor, False 'set output colour to black & white

    swDwg.Extension.SaveAs exportPath, swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_UpdateInactiveViews, swExportPDFData, saveErrors, saveWarnings

    If saveErrors = 0 Then swApp.CloseDoc swDwg.GetPathName

End Sub

Drop everything above into a macro, and voila!

Here is the full macro:

Dim swApp As SldWorks.SldWorks
Sub main()

    Set swApp = Application.SldWorks
    
    Dim swModel As ModelDoc2
    Set swModel = swApp.ActiveDoc
    
    Dim modelPath As String
    modelPath = LCase(swModel.GetPathName)

    SaveAsDXF swModel

    Dim swDwg As ModelDoc2
    Set swDwg = OpenDwg(modelPath)

    SaveAsPDF swDwg
    
End Sub

Private Sub SaveAsDXF(swModel As ModelDoc2)

    Dim exportPath As String
    exportPath = LCase(swModel.GetPathName)
    exportPath = Replace(exportPath, ".sldprt", ".dxf")
    
    Dim swPart As PartDoc
    Set swPart = swModel

    smOptions = 13   '0001101 - SheetMetalOptions bitmask: include flat pattern geometry, bend lines and sketches
    
    Debug.Print exportPath

    swPart.ExportToDWG2 exportPath, swPart.GetPathName, 1, True, Nothing, False, False, smOptions, Nothing

End Sub

Private Function OpenDwg(modelPath As String) As ModelDoc2
    
    Dim dwgPath As String
    dwgPath = LCase(modelPath)
    dwgPath = Replace(dwgPath, ".sldprt", ".slddrw")
    
    Dim swDocSpec As SldWorks.DocumentSpecification
    Set swDocSpec = swApp.GetOpenDocSpec(dwgPath)
    
    Dim swDwg As ModelDoc2
    Set swDwg = swApp.OpenDoc7(swDocSpec)
    
    Set OpenDwg = swDwg

End Function

Private Sub SaveAsPDF(swDwg As ModelDoc2)

    Dim saveErrors As Long
    Dim saveWarnings As Long
    
    Dim exportPath As String
    exportPath = Replace(swDwg.GetPathName, ".slddrw", ".pdf")
    
    'get drawing sheets
    Dim vSheetNames As Variant
    vSheetNames = swDwg.GetSheetNames
    
    'get PDF interface
    Dim swExportPDFData As ExportPdfData
    Set swExportPDFData = swApp.GetExportFileData(swExportDataFileType_e.swExportPDFData)
    
    swExportPDFData.SetSheets swExportDataSheetsToExport_e.swExportData_ExportAllSheets, vSheetNames
    
    swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swPDFExportHighQuality, True 'set output line quality to high
    swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swPDFExportInColor, False 'set output colour to black & white

    swDwg.Extension.SaveAs exportPath, swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_UpdateInactiveViews, swExportPDFData, saveErrors, saveWarnings

    If saveErrors = 0 Then swApp.CloseDoc swDwg.GetPathName

End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *