Problem with a VBA macro to export and open a STEP file with Bamboo Studio

Hi all

I'm currently working on a VBA macro in SolidWorks 2017 to export a part in STEP format and open it automatically with Bamboo Studio. However, despite my efforts, the macro doesn't work as expected and I have difficulties with the STEP recording function.

Here's the current version of my macro:

Sub ExportAndOpenWithBambooStudio()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim partDoc As SldWorks.partDoc
    Dim stepFileName As String
    Dim bambooStudioPath As String
    
    ' Chemin du fichier STEP exporté
    stepFileName = "mon chemin \Exporte.step"
    
    ' Chemin vers Bamboo Studio
    bambooStudioPath = "C:\Program Files\Bambu Studio.exe"
    
' Initialisation de l'application SolidWorks
    Set swApp = CreateObject("SldWorks.Application")
    
    ' Vérifier si SolidWorks est en cours d'exécution
    If swApp Is Nothing Then
        MsgBox "SolidWorks n'est pas en cours d'exécution. Veuillez démarrer SolidWorks et réessayer.", vbExclamation
        Exit Sub
    End If
    
    ' Vérifier si un document est ouvert dans SolidWorks
    If swApp.ActiveDoc Is Nothing Then
        MsgBox "Aucun document n'est ouvert dans SolidWorks. Veuillez ouvrir un document et réessayer.", vbExclamation
        Exit Sub
    End If
    
    ' Récupérer le modèle actif
    Set swModel = swApp.ActiveDoc
    
    ' Vérifier si le document est une pièce (part)
    If swModel.GetType <> swDocPART Then
        MsgBox "Le document actif n'est pas une pièce (part). Veuillez ouvrir une pièce et réessayer.", vbExclamation
        Exit Sub
    End If
    
    ' Convertir le document en objet de pièce
    Set partDoc = swModel
    
    ' Exporter le modèle au format STEP
    If Not SaveAsSTEP(partDoc, stepFileName) Then
        MsgBox "Erreur lors de l'enregistrement de la pièce au format STEP.", vbExclamation
        Exit Sub
    End If
    
    ' Fermer le modèle sans enregistrer les modifications
    swModel.CloseDoc
    
    ' Ouvrir le fichier STEP avec Bamboo Studio
    OpenWithBambooStudio bambooStudioPath, stepFileName
End Sub

Function SaveAsSTEP(ByVal partDoc As SldWorks.partDoc, ByVal stepFileName As String) As Boolean
    On Error Resume Next
    Dim docExt As SldWorks.ModelDocExtension
    Set docExt = partDoc.Extension
    docExt.SaveAs stepFileName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Empty, Empty, Empty
    SaveAsSTEP = (Err.Number = 0)
    On Error GoTo 0
End Function

Sub OpenWithBambooStudio(ByVal bambooStudioPath As String, ByVal stepFileName As String)
    On Error Resume Next
    Dim objShell As Object
    Set objShell = CreateObject("WScript.Shell")
    objShell.Run """" & bambooStudioPath & """ """ & stepFileName & """", vbNormalFocus
    Set objShell = Nothing
    On Error GoTo 0
End Sub

I've tried several approaches before, including using different methods for saving in STEP format, but without success. I keep getting the error message "Error saving the part in STEP format."

If anyone has experience with SolidWorks macros or has ideas on how to fix this problem, I'd be very grateful for any help.

PS: I just have some basic code, and the code was generated with the help of ChatGPT.

Thanks in advance!

Kind regards

Hello
The problem is in the recording path of the step file. The line below cannot work because it does not point to any path.

stepFileName = "mon chemin \Exporte.step"

Must replace with a C:....
If the files then have to be saved in step with the filename open or properties of it, you need a code that is a little more "complex" than the one offered by Chat GPT

2 Likes

Hello Cyril,

Thank you for your answer.
In my code I have put the right path that I have changed in the post here for confidentiality reasons.

Re
Try changing this line:

    docExt.SaveAs stepFileName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, 0, 0

1 Like

Hello

Happy New Year to you. I searched for how to resolve the error "Error recording the part in STEP format", and here's what I found... To test, and as always, without guarantee that it will work but test anyway :blush:

  • Check STEP file data: This error can be caused by problematic data in the STEP file. You can try disabling the "3D Interconnect" option in the SolidWorks system options to work around this problem
    Here is a link in English to help you: Invalid registration index error when importing STEP into SOLIDWORKS

  • When exporting to STEP format, make sure that the export options are set correctly. This can include settings such as output as volume/polygon geometry or exporting sketch entities
    STEP Export Options - 2022 - SOLIDWORKS Help

  • If the problem persists, you can use the VBA debugging tools to examine the macro's behavior. This can help you identify the source of the error and make any necessary corrections.
    VBA Debugger - The CAD Coder

On the other hand, you can also try with the following solutions:

  • First, check if you have the necessary permissions to save files in the specified directory. Sometimes, security restrictions can prevent files from being saved.

  • ChatGPT uses On Error Resume Next in your SaveAsSTEP function, which means that if an error occurs, the code continues to execute the next line. This can make it difficult to determine exactly where the error occurs. You might consider using On Error GoTo to direct the execution flow to a specific label in case of an error, where you can view detailed information about the error.

  • La méthode SaveAs renvoie un booléen indiquant si l’opération a réussi ou non. Vous pouvez utiliser cette valeur de retour pour déterminer si l’opération a réussi et afficher un message d’erreur approprié.
    

Here's an example of how you might modify your SaveAsSTEP function to include some of these suggestions:
Function SaveAsSTEP(ByVal partDoc As SldWorks.partDoc, ByVal stepFileName As String) As Boolean
On Error GoTo ErrorHandler
Dim docExt As SldWorks.ModelDocExtension
Set docExt = partDoc.Extension
SaveAsSTEP = docExt.SaveAs(stepFileName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Empty, Empty, Empty)
Exit Function
ErrorHandler:
MsgBox "Error recording the part in STEP format. Error Details: " & Err.Description, vbExclamation
SaveAsSTEP = False
End Function

Hoping to have helped you move forward
Yours sincerely

Re

Thank you for your answers, there is better!

When I launch the macro, I no longer get an error message thanks to the code of @Cyril.f , and it creates a STEP file in the right place, but does not open it.

I don't have bambu studio so I can't help. Let's see if it's not the Shell command that's the problem.

Try it without opening a room to see if BombuStudio opens or not, to begin with.
FYI, in your code you write bamboo and the software is called bambuStudio (internet) for the variables it's not a big deal but in the file path it can be problematic.

Thank you very much for your feedback.

It does work. I had made a small mistake in the path of the software.

bambooStudioPath = "C:\Program Files\Bambu Studio.exe"

Instead of:
bambooStudioPath = "C:\Program Files\Bambu Studio\bambu-studio.exe"

Thank you all!

1 Like

I'll share the final code with you if there are any interested parties:


Sub ExportAndOpenWithBambooStudio()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim partDoc As SldWorks.partDoc
    Dim stepFileName As String
    Dim bambooStudioPath As String
    
    ' Chemin du fichier STEP exporté
    stepFileName = "C:\**CHEMIN ICI**\Exporte.step"
    
    ' Chemin vers Bamboo Studio
    bambooStudioPath = "C:\Program Files\Bambu Studio\bambu-studio.exe"
    
' Initialisation de l'application SolidWorks
    Set swApp = CreateObject("SldWorks.Application")
    
    ' Vérifier si SolidWorks est en cours d'exécution
    If swApp Is Nothing Then
        MsgBox "SolidWorks n'est pas en cours d'exécution. Veuillez démarrer SolidWorks et réessayer.", vbExclamation
        Exit Sub
    End If
    
    ' Vérifier si un document est ouvert dans SolidWorks
    If swApp.ActiveDoc Is Nothing Then
        MsgBox "Aucun document n'est ouvert dans SolidWorks. Veuillez ouvrir un document et réessayer.", vbExclamation
        Exit Sub
    End If
    
    ' Récupérer le modèle actif
    Set swModel = swApp.ActiveDoc
    
    ' Vérifier si le document est une pièce (part)
    If swModel.GetType <> swDocPART Then
        MsgBox "Le document actif n'est pas une pièce (part). Veuillez ouvrir une pièce et réessayer.", vbExclamation
        Exit Sub
    End If
    
    ' Convertir le document en objet de pièce
    Set partDoc = swModel
    
    ' Exporter le modèle au format STEP
    If Not SaveAsSTEP(partDoc, stepFileName) Then
        MsgBox "Erreur lors de l'enregistrement de la pièce au format STEP.", vbExclamation
        Exit Sub
    End If
    
    ' Ouvrir le fichier STEP avec Bamboo Studio
    OpenWithBambooStudio bambooStudioPath, stepFileName
End Sub

Function SaveAsSTEP(ByVal partDoc As SldWorks.partDoc, ByVal stepFileName As String) As Boolean
    On Error Resume Next
    Dim docExt As SldWorks.ModelDocExtension
    Set docExt = partDoc.Extension
    docExt.SaveAs stepFileName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, 0, 0
    SaveAsSTEP = (Err.Number = 0)
    On Error GoTo 0
End Function

Sub OpenWithBambooStudio(ByVal bambooStudioPath As String, ByVal stepFileName As String)
    On Error Resume Next
    Dim objShell As Object
    Set objShell = CreateObject("WScript.Shell")
    objShell.Run """" & bambooStudioPath & """ """ & stepFileName & """", vbNormalFocus
    Set objShell = Nothing
    On Error GoTo 0
End Sub
1 Like

Good evening
I hadn't made the remark but I had my doubts about it.