Ik wil dat de SolidWorks-macro wordt ontworpen om het proces van het exporteren van PDF3D-, Parasolid (XT), STEP-bestanden en het exporteren van elke Solid Body van een model naar STL-formaat te automatiseren. Ik denk dat alles werkt, maar ik kan het probleem niet oplossen... Kun je me helpen?
Optie Expliciete
Dim swApp als SldWorks.SldWorks
Dim swModel als SldWorks.ModelDoc2
Dim swModelDocExt als SldWorks.ModelDocExtension
Dim swBodyFolder als SldWorks.BodyFolder
Dim swBody As SldWorks.Body2
Dim boolstatus als Booleaanse
Dim lErrors zo lang
Dim lWaarschuwingen zo lang mogelijk
Dim ActiveConfig als tekenreeks
Dim sModelFullPath als tekenreeks
Dim sFilePath als tekenreeks
Dim FolderNameDestination als tekenreeks
Dim Ext_PART als snaar
Dim Ext_STEP als snaar
Dim Ext_STL als snaar
Sub hoofd()
Stel swApp = Toepassing.SldWorks in
Stel swModel = swApp.ActiveDoc in
' 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
Einde Sub
Sub ExportBodyToSTL(swBody As SldWorks.Body2, destinationFolder As String)
' Construeer de STL-bestandsnaam met behulp van de naam van de vaste body
Dim bodyName als snaar
bodyName = swBody.Name
Als InStr(bodyName, "[") <> 0 dan
bodyName = Links(bodyName, InStr(bodyName, "[") - 1)
Einde als
Dim stlFileName als tekenreeks
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
Einde Sub