Macro PDF3D,XT,STEP,STL unit

I want the SolidWorks macro to be designed to automate the process of exporting PDF3D, Parasolid (XT), STEP files and exporting each Solid Body of a model to STL format. I think everything works but I can't seem to solve the problem... Can you help me?

Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swBodyFolder As SldWorks.BodyFolder
Dim swBody As SldWorks.Body2
Dim boolstatus As Boolean
Dim lErrors As Long
Dim lWarnings As Long
Dim ActiveConfig As String
Dim sModelFullPath As String
Dim sFilePath As String
Dim FolderNameDestination As String
Dim Ext_PART As String
Dim Ext_STEP As String
Dim Ext_STL As String

Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

' Contrôle si un PART ou un ASM est ouvert
If swModel Is Nothing Then
    MsgBox "Aucun assemblage ou pièce en cours", vbCritical
    End
End If

If swModel.GetType <> swDocASSEMBLY And swModel.GetType <> swDocPART Then
    MsgBox "Cette Macro ne fonctionne que sur les assemblages ou les pièces", vbCritical
    End
End If

Set swModelDocExt = swModel.Extension

Set swModelDocExt = swModel.Extension
Set swExportData = swApp.GetExportData(swExportPDF)

' Paramètres d'export PDF3D
swExportData.ViewPdfAfterSaving = True ' Ouvrir le fichier PDF après l'enregistrement

' Paramètres d'export STL
Set swExportData = swApp.GetExportFileData(swExportSTL)
swExportData.ExportAsBinary = True ' Exporter en tant que fichier binaire
swExportData.Unit = 0 ' Unités en millimètres
swExportData.Quality = swSTLQuality_Fine ' Qualité fine
swExportData.IncludeInfo = True ' Afficher les informations avant l'enregistrement


' Controle si le fichier ouvert a déjà été sauvegardé
Dim filename As String
filename = swModel.GetPathName
If filename = "" Then
    MsgBox "Sauvegarder d'abord le fichier et réessayez", vbCritical
    End
End If

' Créer un objet BodyFolder pour accéder aux dossiers de corps
Set swBodyFolder = swModel.FirstFeature.GetSpecificFeature2
If Not swBodyFolder Is Nothing Then
    ' Récupérer le chemin du modèle pour déterminer le dossier de destination
    sModelFullPath = swModel.GetPathName
    sFilePath = Left(sModelFullPath, InStrRev(sModelFullPath, "\"))
    NomDossierDestination = sFilePath
    
    ' Boucler à travers chaque corps solide du dossier de corps
    Set swBody = swBodyFolder.GetFirstBody
    Do While Not swBody Is Nothing
        ' Exporter le corps solide au format STL
        ExportBodyToSTL swBody, NomDossierDestination
        ' Désélectionner le corps solide
        swModel.ClearSelection2 True
        ' Passer au corps solide suivant
        Set swBody = swBodyFolder.GetNextBody(swBody)
    Loop
End If

' Enregistrement en tant que Parasolid (XT)
NomDossierDestination = sFilePath
filename = filename & ".x_t"
boolstatus = swModelDocExt.SaveAs(filename, 0, 0, swExportData, lErrors, lWarnings)

' Enregistrement en tant que STEP
NomDossierDestination = sFilePath
filename = filename & ".stp"
boolstatus = swModelDocExt.SaveAs(filename, 0, 0, swExportData, lErrors, lWarnings)

' Enregistrement en tant que PDF3D
NomDossierDestination = sFilePath
sModelFullPath = swModel.GetPathName
sFilePath = Left(sModelFullPath, InStrRev(sModelFullPath, "\"))

Dim pdfOptions As Variant
pdfOptions = swApp.GetExportFileOptions(swExportPDF)
boolstatus = swModelDocExt.SaveAs3(sFilePath & swModel.GetTitle & "-PDF3D.PDF", 0, swSaveAsOptions_Silent, lErrors, lWarnings, pdfOptions)

' Message d'avertissement d'execution de la macro
MsgBox "MACRO TERMINEE :" & vbCrLf & "Contrôler les fichiers PDF3D, Parasolid, STEP et STL exportés", vbInformation

End Sub

Sub ExportBodyToSTL(swBody As SldWorks.Body2, destinationFolder As String)
' Construct the STL filename using the solid body name
Dim bodyName As String
bodyName = swBody.Name
If InStr(bodyName, "[") <> 0 Then
bodyName = Left(bodyName, InStr(bodyName, "[") - 1)
End If
Dim stlFileName As String
stlFileName = destinationFolder & bodyName & ".stl"

' Exporter le corps solide au format STL
boolstatus = swModelDocExt.SaveAs(stlFileName, 0, 0, swExportData, lErrors, lWarnings)

' Afficher le nom du fichier STL dans la console (peut être commenté/décommenté selon les besoins)
Debug.Print "Exported STL: " & stlFileName

End Sub

Hello

In order to help, what is not working? The generation of the PDF3D? The STL of each corps ...?

Thank you for your answer :slight_smile: Originally I have 2 macros, one to do the PDF3D XT STEP and another one that does the unit STL of each volume. I would like to merge the 2 to make one. the generation of PDF3D XT and STEP works but I can't integrate the unitary STL part

Hello

As it stands, I'm a little surprised that the macro works because there are some errors reported if I leave the code as it is.
In the function calls, the parameters for PDF export are poorly defined (at least for SW2022, which version are you on?), the registration name of the step is poorly realized (it is finally called .x_t.stp).
The end of the STL macro is really the one of the macro alone?
When testing it, SwBodyFolder remains empty since there is no tree analysis.
In relation to this topic: Macro Register STL 1 body select - 3D design / Other applications - myCAD Forum (visiativ.com), do you use the stl code you generated following this request?