Updated Outdated VBA Code

  Hi all

I have a Solidworks macro coded in VBA that is several years old.

Since then, I have seen that several functions have become obsolete and therefore need to be replaced.

I'd like to use this macro under version Sw2020 and above. 

Looking at the APi Solidworks help I can see the obsolete functions to replace with their new names.

On the other hand, I still can't get this code to work...

I am not at all comfortable with the VBA and that is why I am asking for your help.

It's a subject that has been dragging on for quite some time but I haven't taken the time to deal with it.

If a kind soul could take a look at it and tell me how to update the code.

I've already started updating the code with the new functions but I'm blocking...

To explain in broad terms, the macro is supposed to:

- Export the unfolded version of each configuration of a sheet metal part in dxf or dwg with a variable name defined in the code.

I'm attaching the macro, for now I have an error on the swModel.GetConfigurationNames

Thank you in advance for your help! =)

 


oldvba.txt

Hello;
Here is my proposal:
-Note: try to publish your code in the message rather than as an attachment, it's more reassuring to "See" a Code rather than Download it.
-Note 2: Avoid Accents in VB (Visual Basic) Code at all costs
 

Dim swApp As Object
Option Explicit

'Enumeration des Option Choisies pour les Exports en DXF (A plat)
' Voir https://help.solidworks.com/2020/english/api/sldworksapi/solidworks.interop.sldworks~solidworks.interop.sldworks.ipartdoc~exporttodwg2.html

Enum SheetMetalOptions_e
    ExportFlatPatternGeometry = 1
    IncludeHiddenEdges = 2
    ExportBendLines = 4
    IncludeSketches = 8
    MergeCoplanarFaces = 16
    ExportLibraryFeatures = 32
    ExportFormingTools = 64
    ExportBoundingBox = 2048
End Enum

Sub main()
' Declaration:
Dim swApp                   As SldWorks.SldWorks
Dim swModel                 As SldWorks.ModelDoc2
Dim config                  As SldWorks.Configuration
Dim vConfNameArr            As Variant
Dim sConfigName             As String
Dim i                       As Long
Dim bShowConfig             As Boolean
Dim bRebuild                As Boolean
Dim bRet                    As Boolean
Dim FilePath                As String
Dim PathSize                As Long
Dim PathNoExtension         As String
Dim NewFilePath             As String
Dim Value_T                 As String
Dim ResolvedValOut          As String
Dim cusPropMgr              As SldWorks.CustomPropertyManager
Dim wasResolved             As Boolean
Dim Error                   As Long


    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc

vConfNameArr = swModel.GetConfigurationNames 'Creation de la liste des configurations

For i = 0 To UBound(vConfNameArr) 'Boucle la liste : de l'element 0 jusqu'au nombre d'element dans la liste (Ubound)
    Set config = swModel.GetActiveConfiguration
    Set cusPropMgr = config.CustomPropertyManager
    sConfigName = vConfNameArr(i) 'Recupere l'elementt Numero i de la liste
    bShowConfig = swModel.ShowConfiguration2(sConfigName) 'Affiche la configuration
    
    Error = cusPropMgr.Get5("TYPE", True, Value_T, ResolvedValOut, wasResolved) 'Recupere la valeur de la proriete "T" dans la variable "Value_T"
    bRebuild = swModel.ForceRebuild3(False) 'Reconstruction du modèle
    
    FilePath = swModel.GetPathName 'Recupere le chemin du fichier SW
    PathSize = Strings.Len(FilePath) 'Compte le nombre de caracteres du chemin
    
        PathNoExtension = Strings.Left(FilePath, PathSize - 6) 'Recupere le nom de la piecece en enlevant .Sldrt
        NewFilePath = Left(FilePath, InStrRev(FilePath, "\")) & sConfigName & ".DXF" 'Remplace le nom par Type + Lg + Nom de la config (sans Flat pattern).dxf

    If False = swModel.ExportToDWG2(NewFilePath, FilePath,  swExportToDWG_e.swExportToDWG_ExportSheetMetal, True, Empty, False, False, SheetMetalOptions_e.ExportFlatPatternGeometry + SheetMetalOptions_e.ExportBendLines, Empty) Then
        Err.Raise vbError, "", "Failed to export flat pattern"
    End If
    
Next i 'Passe a la prochaine config

bShowConfig = swModel.ShowConfiguration2(vConfNameArr(0)) 'Retour sur la Configuration Principale
End Sub

 

Kind regards.

3 Likes

I just tested the macro!

She has no mistakes and that's cool!

All that remains is to remove the save at each config because I have to click on "save" each time and tell it not to export the bend lines.

So I guess I'm changing the line ExportBendLines=4 to ExportBendLines=0 or some other hint^^

Then to remove the recording, I don't see which line I need to change. Maybe just an option in Sw to modify.

 

In any case, a big thank you because the code is really great! =)