MACRO that runs too fast

Hello

I made a Macro that allows us to export to PDF3D and STEP
To manage the WWTP, we need to go through a PART on the outside.

Sometimes the PART does not open, the STEP is generated with the active document, so the assembly if the part is not opened.
If I restart the Macro a second time (sometimes I need +), the PART opens and everything goes as it should.

Can it come from the speed of execution of the Macro?
Can we check that the PART is open before saving in WWTP?
Or add a waiting time?

Have a nice day

You can retrieve the name of the room before to be sure that the room is open, and if the name does not match your request you loop back to ask for the name again and as long as it is not good you do not export.

Post the complete code will be easier to see.

It may also be the method to open your room which is not the right one.

I can't attach...
Check Point Infinity Next Blocking Message

And window where this message appears:
An HTTP 0 error has occurred. <br />/filefield/ahah/answer/field_attachement/0

EDIT:
Finally it's good... I struggled to put the PJ


macro.txt

FYI to put the code you have the icon here:

And in language you put VBscript

1 Like

Hello @SebJo,

Could the problem be related to a misunderstanding about the path for saving files?
The value of the DestinationFolderName variable is not used when backing up files. The default directory is the destination.

In my first test, the Essai.SLDASM_1.SLDPRT file was saved in a SW installation subdirectory and not in the base template folder. Oddly enough, it works well if you restart the macro.

Simply concatenate the path and file name in the three backups: DestinationFolderName & FileName*** at the end of the macro.

Kind regards

2 Likes

 

I just looked and same error as @m.blt  the line to create DestinationFolderName is too low and the value is requested before the variable is assigned .

In addition, the filename when you export already contains an extension, so you export something like this:

A.sldprt and the recording therefore does not work:

The code corrected hoping that your error came from there:

' ******************************************************************************
' macro du 19/04/22 by sj
' ******************************************************************************

Sub main()

Dim swApp                   As SldWorks.SldWorks
Dim swModel                 As SldWorks.ModelDoc2
Dim swModelDocExt           As SldWorks.ModelDocExtension
Dim swCustProp              As CustomPropertyManager
Dim val                     As String
Dim valout                  As String
Dim bool                    As Boolean
Dim INDICE                  As Boolean
Dim swExportData            As SldWorks.ExportPdfData
Dim boolstatus              As Boolean
Dim filename                As String
Dim FileNamePDF             As String
Dim FileNamePART            As String
Dim FileNameSTEP            As String
Dim lErrors                 As Long
Dim lWarnings               As Long
Dim ActiveConfig            As String
Dim sModelFullPath          As String
Dim sFilePath               As String
Dim NomDossierDestination   As String
Dim Ext_PART                As String
Dim Ext_STEP                As String
Dim errors                  As Long
Dim warnings                As Long
Dim Nomfichier              As String
Dim TestStr                 As String

Ext_PART = ".SLDPRT"
Ext_STEP = ".STEP"

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
'Controle 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 swExportData = swApp.GetExportFileData(1)
    
    swExportData.ExportAs3D = True
    
'Controle si le fichier ouvert a déjà été sauvegardé
    filename = swModel.GetPathName
    If filename = "" Then
        MsgBox "Sauvegarder d'abord le fichier et réessayez", vbCritical
        End
    End If

    ActiveConfig = swApp.GetActiveConfigurationName(filename)
    
' Recuperation de propriete (changer la valeur entre "" apres Get4 pour changer de propriete à  récupérer)
    Set swCustProp = swModelDocExt.CustomPropertyManager("")
    bool = swCustProp.Get4("DESIGNATION", False, val, valout)
    PropDESIGNATION = valout
    
'Controle si le fichier ouvert a déjà  été approuvé
   PropINDICE = InputBox("A quel indice souhaitez vous générer les fichiers ?")

   If PropINDICE = "" Then
       MsgBox "Merci de ne pas laisser le champs vide", vbCritical
       Exit Sub
    End If
        
                    
'Controle si le fichier ouvert a déjà  été smarté
    If PropDESIGNATION = "" Then
        MsgBox "SMARTER votre fichier avant d'exécuter la MACRO", vbCritical
        End
    End If

    sModelFullPath = swModel.GetPathName
    sFilePath = Left(sModelFullPath, InStrRev(sModelFullPath, "\"))

'variable remonté car déclaré en dessous la 1ère utilisation de la variable dans ta version
NomDossierDestination = sFilePath
 
    
'Vérifie que le fichier PDF n'existe pas à cet indice avant enregistrement
    FileNamePDF = NomDossierDestination & PropDESIGNATION & "_" & PropINDICE & ".PDF"
    TestStr = ""
    On Error Resume Next
    TestStr = Dir(FileNamePDF)
    On Error GoTo 0
    If TestStr = "" Then
    Else
        MsgBox "Le fichier PDF existe déjà  à  cet indice " & vbCrLf & "dans le dossier de l'assemblage : " & vbCrLf & vbCrLf & sFilePath, vbCritical
        Exit Sub
    End If
    
    
'Vérifie que le fichier PART n'existe pas à cet indice avant enregistrement
    FileNamePART = NomDossierDestination & (Left(swModel.GetTitle, Len(swModel.GetTitle) - 7)) & "_" & PropINDICE & Ext_PART
    TestStr = ""
    On Error Resume Next
    TestStr = Dir(FileNamePART)
    On Error GoTo 0
    If TestStr = "" Then
    Else
        MsgBox "Le fichier PART existe déjà à cet indice" & vbCrLf & "dans le dossier de l'assemblage : " & vbCrLf & vbCrLf & sFilePath, vbCritical
        Exit Sub
    End If
    
    
'Vérifie que le fichier STEP n'existe pas à cet indice avant enregistrement
    FileNameSTEP = NomDossierDestination & PropDESIGNATION & "_" & PropINDICE & Ext_STEP
    TestStr = ""
    On Error Resume Next
    TestStr = Dir(FileNameSTEP)
    On Error GoTo 0
    If TestStr = "" Then
    Else
        MsgBox "Le fichier STEP existe déjà  à  cet indice" & vbCrLf & "dans le dossier de l'assemblage : " & vbCrLf & vbCrLf & sFilePath, vbCritical
        Exit Sub
    End If
    
    
'Enregistrer le doc actif en PDF
    swModel.ForceRebuild3 True
    swModel.ShowNamedView2 "*Isometric", -1
    swModel.ViewZoomtofit2
    boolstatus = swModelDocExt.SaveAs(FileNamePDF, 0, 0, swExportData, lErrors, lWarnings)

'Enregistrer le doc actif en PART
    Set Part = swApp.ActiveDoc
    longstatus = Part.SaveAs3(FileNamePART, 0, 0)
    
'Ouvrir le doc précédent / Enregistrer le doc actif en STEP
    Set swPart = swApp.OpenDoc6(FileNamePART, 1, 0, "", errors, warnings)
    Set Part = swApp.ActiveDoc
    longstatus = Part.SaveAs3(FileNameSTEP, 0, 0)
           
'Message d'avertissement d'execution de la macro
    MsgBox "MACRO TERMINEE :" & vbCrLf & "Contrôler les fichiers PDF3D, PART, STEP", vbInformation

End Sub

 

1 Like

The record path is the folder of the current document
And not the default directory unless I'm wrong

__

It's surprising because at home, the swModel.GetTitle doesn't get the extension
So, for the PART, the macro gives this

 

I guess it's because the file extension is hidden in windows, which is not my case.

For the path of the folder I went up a few lines (see code and comment above) otherwise the

DestinationFolderName = sFilePath

was found below: of:
FileNamePDF = DestinationFolderName & PropDesignation & "_" & PropCLUE & ".PDF"
and of:
FileNamePART = DestinationFolderName & (Left(swModel.GetTitle, Len(swModel.GetTitle) - 7)) & "_" & PropHINT & Ext_PART

So on the 1st launch of the macro the path is = a "" or nothing.

On the other hand, every subsequent launch it takes the value it gets some next line.

Just close SW and restart your macro step by step and look at the values of your variable in the window and you'll see the problem.

Thank you for your help!
Everything seems to be going well