Solidworks Macro: Drawing to PDF and DXF

Hello

I want to make a macro for Solidworks that transforms drawing sheets into PDFs or DXFs.
PDF files will be named "filename+sheetname"
DXF files will be named in the same way.

The macro must differentiate between the sheets to be saved as PDF or DXF following the presence or absence of the word DXF in the name of the sheet.

Below is the macro created. Unfortunately I get the error "compilation error: User-defined type not defined".
With "swExportDXFData As SldWorks.ExportDxfData" highlighted in blue.
I think I understand that there is a piece of data missing in the references but I am lost.

I'm on SolidWorks 2022.

Could anyone help me?

Option Explicit

Sub ExportPDFandDXF()
    
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swDraw As SldWorks.DrawingDoc
    Dim swSheet As SldWorks.Sheet
    Dim swExportData As SldWorks.ExportPdfData
    Dim swExportDXFData As SldWorks.ExportDxfData
    Dim sheetName As String
    Dim fileName As String
    Dim filePath As String
    Dim numSheets As Integer
    Dim i As Integer
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    If swModel Is Nothing Then
        MsgBox "Aucun document ouvert dans SolidWorks."
        Exit Sub
    End If
    
    If Not swModel.GetType = swDocumentTypes_e.swDocDRAWING Then
        MsgBox "Ce n'est pas un document de mise en plan."
        Exit Sub
    End If
    
    Set swDraw = swModel
    
    numSheets = swDraw.GetSheetCount
    
    fileName = Left(swModel.GetPathName, InStrRev(swModel.GetPathName, ".") - 1)
    filePath = Left(swModel.GetPathName, InStrRev(swModel.GetPathName, "\"))

    For i = 1 To numSheets
        
        Set swSheet = swDraw.Sheet(i)
        
        sheetName = swSheet.GetName
        
        If InStr(sheetName, "DXF") > 0 Then
            
            Set swExportDXFData = swApp.GetExportFileData(1)
            
            swExportDXFData.fileName = fileName & sheetName & ".dxf"
            swExportDXFData.SetSheets swSheet.GetName
            
            swModel.Extension.ExportToDWGDXF swExportDXFData
            
        Else
            
            Set swExportData = swApp.GetExportFileData(swExportPdfData)
            
            swExportData.fileName = fileName & sheetName & ".pdf"
            swExportData.SetSheets swSheet.GetName
            
            swModel.Extension.SaveAs swExportData
            
        End If
        
    Next i
    
    MsgBox "Exportation terminée."
    
End Sub


Hello
Unless I'm mistaken, SldWorks.ExportDxfData doesn't exist, hence the compilation error.
Look at this topic which is identical

2 Likes

Thank you for your answer but I can't do it.
I tried another approach to the case and I have another problem.

I "only" want to save the sheets of a drawing in .pdf or .dxf depending on whether the word "DXF" is present in the tab name (name of the sheet) but it seems too complicated for me.

Dim swApp As Object
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim swSheet As SldWorks.Sheet
Dim nomFeuille As String
Dim nomFichier As String
Dim dossier As String

Sub EnregistrerFeuilles()

    Set swApp = Application.SldWorks
    
    ' Vérifier si un document est ouvert
    Set swModel = swApp.ActiveDoc
    If swModel Is Nothing Then
        MsgBox "Ouvrir un document SolidWorks", vbExclamation, "Erreur"
        Exit Sub
    End If
    
    ' Vérifier si le document est une mise en plan
    If Not swModel.GetType = swDocDRAWING Then
        MsgBox "Le document actif n'est pas une mise en plan", vbExclamation, "Erreur"
        Exit Sub
    End If
    
    ' Récupérer le dossier et le nom du fichier
    dossier = Left(swModel.GetPathName, InStrRev(swModel.GetPathName, "\"))
    
    If InStrRev(swModel.GetTitle, ".") > 0 Then
        nomFichier = Mid(swModel.GetTitle, 1, InStrRev(swModel.GetTitle, ".") - 1)
    Else
        nomFichier = swModel.GetTitle
    End If
    
    ' Enregistrer chaque feuille en tant que fichier séparé
    Set swDraw = swModel
    If swDraw Is Nothing Then
        MsgBox "Le document actif n'est pas une mise en plan", vbExclamation, "Erreur"
        Exit Sub
    End If
    
    For Each swSheet In swDraw.Sheets
        nomFeuille = swSheet.GetName
        
        If InStr(1, nomFeuille, "DXF", vbTextCompare) > 0 Then
            swSheet.SaveAs dossier & nomFichier & " " & nomFeuille & ".dxf"
        Else
            swSheet.SaveAs dossier & nomFichier & " " & nomFeuille & ".pdf"
        End If
    Next
    
    MsgBox "Les feuilles ont été enregistrées", vbInformation, "Terminé"
    
End Sub

With this code I have a new error:
row: For Each swSheet In swDraw.GetSheets()

Error Code: 438
Property or method not supported by this object

I'm trying to get some macro examples to help me but I'm going around in circles...

Hello

There are a lot of statements that don't exist in your code that require activation before you can use them. I don't have time to look at it now, maybe on Monday if no one else passes by on the subject.

1 Like

Hello @stev7833 ,

The compilation error is caused by the fact that GetSheets is not a method of a drawing document. The list of sheets should be traversed using their names by GetSheetNames.

Moreover, the sheet-by-sheet exports of a drawing in pdf and dxf do not follow the same logic: data structure for the first (ExportPdfData), setting of export options for the second (swDxfMultiSheetOption, swDxfActiveSheetOnly).

In principle, the macro join should do the job...

Kind regards.
ExportMEPtoPdfDxf.swp (59.5 KB)

1 Like

Thanks for the info and the macro,
It works very well!