I answer myself. I searched and searched...
When I first launched my macro, it stuck at this line:
I did END the command and in the F8 properties, I could see that PROFILES/mass/WEIGHT and MATERIALS were well informed. It was therefore missing SURFACE PIECE
I restarted and the macro worked until the end by filling SURFACE PIECE well.
As if the first time the macro was launched, it was unable to retrieve the value of WEIGHT which is calculated just before.
So I added a part reconstruction between MATERIALS and mass property recovery and it works.
I tested my macro with a single configuration creation and the SURFACE PIECE was correct.
Then I tested my macro by integrating the creation of 3 configurations: 00 / PC / R1018.
SURFACE PIECE is created well in the 3 configurations, but I only get a good calculation result on one configuration.
Do you have an idea of the problem?
Here's the code:
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swConfigMgr As SldWorks.ConfigurationManager
Dim swConfig As SldWorks.Configuration
Dim vConfigName As Variant
Dim sSpecConfigNameArr(0) As String
Dim vSpecConfigNameArr As Variant
Dim dimValue As Variant
Dim stnameConfig As String
Dim boolstatus As Boolean
Dim i As Integer
Dim swErrors As Long
Dim swWarnings As Long
Dim swPart As SldWorks.PartDoc
Dim tConfig() As String
Dim swModelDocExt As ModelDocExtension
Dim swCustProp As CustomPropertyManager
Dim sMasse As String
Dim sValout As String
Dim sVal As String
Sub main()
'''-----------------------------------------------
Set swApp = Application.SldWorks
'on récupére le document actif
Set swModel = swApp.ActiveDoc
Set swConfigMgr = swModel.ConfigurationManager
'on récupére la configuration active
Set swConfig = swConfigMgr.ActiveConfiguration
stnameConfig = swConfig.Name
vConfigNameArr = swModel.GetConfigurationNames
Set swModelDocExt = swModel.Extension
'''-----------------------------------------------
'on boucle sur toutes les configurations
For Each vConfigName In vConfigNameArr
'on test si la configuration est différente de la configuration active et de la configuration Default
If vConfigName <> swConfig.Name And vConfigName <> "xx" Then
boolstatus = swModel.DeleteConfiguration2(vConfigName)
End If
If vConfigName <> swConfig.Name And vConfigName <> "Défaut" Then
boolstatus = swModel.DeleteConfiguration2(vConfigName)
End If
Next vConfigName
'on reconstruit la pièces
swModel.ForceRebuild3 False
Do
'récupère le document actif dans SW
Set swModel = swApp.ActiveDoc
If Not swModel Is Nothing Then
''' lignes déplacées dans la boucle, de manière à ce que l'operation s'effectue sur tous les fichiers et pas juste le premier.
Set swPart = swModel
boolstatus = swPart.AddConfiguration2("00", "", "", True, False, False, True, 256)
boolstatus = swPart.AddConfiguration2("PC", "", "", True, False, False, True, 256)
boolstatus = swPart.AddConfiguration2("R1018", "", "", True, False, False, True, 256)
boolstatus = swModel.DeleteConfiguration2("Défaut")
boolstatus = swModel.DeleteConfiguration2("xx")
'''-----------------------------------------------
'Mise en place de la matière
swModel.SetMaterialPropertyName2 "", "X:\DONNEES BE\DAO\BUREAU ETUDES\BIBLI SOLIDWORKS\MATERIAUX\MATERIAUX QUALI-CITE/QUALI-CITE.sldmat", "ALUMINIUM 6060"
'Boucle sur toutes les configurations
tConfig = swModel.GetConfigurationNames
For i = 0 To UBound(tConfig)
'ajoute une propriété personnalisée edition avec la valeur POTEAUXHEXA"
bRet = swModel.DeleteCustomInfo2(tConfig(i), "DESIGNATION 2")
bRet = swModel.DeleteCustomInfo2(tConfig(i), "PROFILS")
bRet = swModel.AddCustomInfo3(tConfig(i), "PROFILS", swCustomInfoText, "POTEAUXHEXA")
'ajoute un propriété personnalisée MASSE avec la valeur Masse
'Chr(34) permet d'ajouter le caractère "
bRet = swModel.DeleteCustomInfo2(tConfig(i), "masse")
bRet = swModel.AddCustomInfo3(tConfig(i), "masse", swCustomInfoText, Chr(34) & "SW-Mass" & Chr(34))
'ajoute un propriété personnalisée POIDS avec la valeur Masse
'Chr(34) permet d'ajouter le caractère "
bRet = swModel.DeleteCustomInfo2(tConfig(i), "POIDS")
bRet = swModel.AddCustomInfo3(tConfig(i), "POIDS", swCustomInfoText, Chr(34) & "SW-Mass" & Chr(34))
'ajoute un propriété personnalisée edition avec la valeur MATERIAUX
'Chr(34) permet d'ajouter le caractère "
bRet = swModel.DeleteCustomInfo2(tConfig(i), "MATERIAUX")
bRet = swModel.AddCustomInfo3(tConfig(i), "MATERIAUX", swCustomInfoText, Chr(34) & "SW-Material" & Chr(34))
'on reconstruit la pièces
swModel.ForceRebuild3 False
Set swCustProp = swModelDocExt.CustomPropertyManager(tConfig(i))
'Récupération de la propriété de masse, changer le nom de la propriété en fonction de votre modèle
bRet = swCustProp.Get6("POIDS", False, sValout, sMasse, False, False)
'Conversion du texte en valeur décimale, multiplication x RATIO et conversion en donné de type texte
sVal = CStr(Format(CDec(sMasse) * 0.0556, "0.000"))
'Renseignement de la propriété SURFACE PIECE avec la valeur "sVal"
bRet = swCustProp.Add3("SURFACE PIECE", 30, sVal, 1)
Next i
'Préférences SolidWorks
boolstatus = swModel.Extension.SetUserPreferenceInteger(swUserPreferenceIntegerValue_e.swUnitsLinearFractionDenominator, 0, 0)
boolstatus = swModel.Extension.SetUserPreferenceToggle(swUserPreferenceToggle_e.swUnitsLinearFeetAndInchesFormat, 0, False)
boolstatus = swModel.Extension.SetUserPreferenceInteger(swUserPreferenceIntegerValue_e.swUnitsDualLinear, 0, swLengthUnit_e.swMM)
boolstatus = swModel.Extension.SetUserPreferenceInteger(swUserPreferenceIntegerValue_e.swUnitsDualLinearFractionDenominator, 0, 0)
boolstatus = swModel.Extension.SetUserPreferenceToggle(swUserPreferenceToggle_e.swUnitsDualLinearFeetAndInchesFormat, 0, False)
boolstatus = swModel.Extension.SetUserPreferenceInteger(swUserPreferenceIntegerValue_e.swUnitsMassPropMass, 0, swUnitsMassPropMass_e.swUnitsMassPropMass_Kilograms)
boolstatus = swModel.Extension.SetUserPreferenceInteger(swUserPreferenceIntegerValue_e.swUnitsMassPropVolume, 0, swUnitsMassPropVolume_e.swUnitsMassPropVolume_Meters3)
boolstatus = swModel.Extension.SetUserPreferenceInteger(swUserPreferenceIntegerValue_e.swUnitsLinear, 0, swLengthUnit_e.swMM)
boolstatus = swModel.Extension.SetUserPreferenceInteger(swUserPreferenceIntegerValue_e.swUnitsMassPropLength, 0, swLengthUnit_e.swMM)
'Enregistre le docuement actif en mode silencieux
bRet = swModel.Save3(swSaveAsOptions_Silent, swErrors, swWarnings)
'Ferme le document actif
swApp.CloseDoc swModel.GetPathName
End If
'boucle jusqu'a ce qu'il n'y ai plus de fichier ouvert dans SW
Loop While Not swModel Is Nothing
End Sub