Macro: Adding Materials + Skins

Hello everyone,

I created a macro that automatically generates room configurations corresponding to paint RALs.

In solidworks, I created the materials corresponding to the RAL by putting the right associated appearances on them.

In a room, when I manually apply and change the material, the colors vary well in correspondence.

With macro, the configurations are well created, the materials are well applied and the appearances correspond well to the configurations.

But the colors of all the configurations are all like that of the first configuration, therefore of the first appearance

When I edit the material and click apply without changing anything, the appearance corrects itself and changes to the right color.

Do you have any idea of the problem?

Below is 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
    
    
'=> Création de la configuration ALUMINIUM Brut (non peint)
    boolstatus = swPart.AddConfiguration2("00", "", "", True, False, False, True, 256)


    boolstatus = swModel.DeleteConfiguration2("Défaut")
    boolstatus = swModel.DeleteConfiguration2("xx")
    boolstatus = swModel.DeleteConfiguration2("PC")
    
    '''-----------------------------------------------
    
    '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"
    
'=> Création des configuration ALUMINIUM Peint (RAL)
    
    boolstatus = swPart.AddConfiguration2("R1013", "", "", True, False, False, True, 256)
    swModel.SetMaterialPropertyName2 "", "X:\DONNEES BE\DAO\BUREAU ETUDES\BIBLI SOLIDWORKS\MATERIAUX\MATERIAUX QUALI-CITE/QUALI-CITE.sldmat", "ALUMINIUM 6060 - RAL 1013"
    
    boolstatus = swPart.AddConfiguration2("R1018", "", "", True, False, False, True, 256)
    swModel.SetMaterialPropertyName2 "", "X:\DONNEES BE\DAO\BUREAU ETUDES\BIBLI SOLIDWORKS\MATERIAUX\MATERIAUX QUALI-CITE/QUALI-CITE.sldmat", "ALUMINIUM 6060 - RAL 1018"
    
    boolstatus = swPart.AddConfiguration2("R2008", "", "", True, False, False, True, 256)
    swModel.SetMaterialPropertyName2 "", "X:\DONNEES BE\DAO\BUREAU ETUDES\BIBLI SOLIDWORKS\MATERIAUX\MATERIAUX QUALI-CITE/QUALI-CITE.sldmat", "ALUMINIUM 6060 - RAL 2008"
    
    boolstatus = swPart.AddConfiguration2("R3000", "", "", True, False, False, True, 256)
    swModel.SetMaterialPropertyName2 "", "X:\DONNEES BE\DAO\BUREAU ETUDES\BIBLI SOLIDWORKS\MATERIAUX\MATERIAUX QUALI-CITE/QUALI-CITE.sldmat", "ALUMINIUM 6060 - RAL 3000"
    
    boolstatus = swPart.AddConfiguration2("R3004", "", "", True, False, False, True, 256)
    swModel.SetMaterialPropertyName2 "", "X:\DONNEES BE\DAO\BUREAU ETUDES\BIBLI SOLIDWORKS\MATERIAUX\MATERIAUX QUALI-CITE/QUALI-CITE.sldmat", "ALUMINIUM 6060 - RAL 3004"
    
    boolstatus = swPart.AddConfiguration2("R5015", "", "", True, False, False, True, 256)
    swModel.SetMaterialPropertyName2 "", "X:\DONNEES BE\DAO\BUREAU ETUDES\BIBLI SOLIDWORKS\MATERIAUX\MATERIAUX QUALI-CITE/QUALI-CITE.sldmat", "ALUMINIUM 6060 - RAL 5015"
    
    boolstatus = swPart.AddConfiguration2("R6005", "", "", True, False, False, True, 256)
    swModel.SetMaterialPropertyName2 "", "X:\DONNEES BE\DAO\BUREAU ETUDES\BIBLI SOLIDWORKS\MATERIAUX\MATERIAUX QUALI-CITE/QUALI-CITE.sldmat", "ALUMINIUM 6060 - RAL 6005"
    
    boolstatus = swPart.AddConfiguration2("R6018", "", "", True, False, False, True, 256)
    swModel.SetMaterialPropertyName2 "", "X:\DONNEES BE\DAO\BUREAU ETUDES\BIBLI SOLIDWORKS\MATERIAUX\MATERIAUX QUALI-CITE/QUALI-CITE.sldmat", "ALUMINIUM 6060 - RAL 6018"
    
    boolstatus = swPart.AddConfiguration2("R6029", "", "", True, False, False, True, 256)
    swModel.SetMaterialPropertyName2 "", "X:\DONNEES BE\DAO\BUREAU ETUDES\BIBLI SOLIDWORKS\MATERIAUX\MATERIAUX QUALI-CITE/QUALI-CITE.sldmat", "ALUMINIUM 6060 - RAL 6029"
    
    boolstatus = swPart.AddConfiguration2("R7016", "", "", True, False, False, True, 256)
    swModel.SetMaterialPropertyName2 "", "X:\DONNEES BE\DAO\BUREAU ETUDES\BIBLI SOLIDWORKS\MATERIAUX\MATERIAUX QUALI-CITE/QUALI-CITE.sldmat", "ALUMINIUM 6060 - RAL 7016"
    
    boolstatus = swPart.AddConfiguration2("R7035", "", "", True, False, False, True, 256)
    swModel.SetMaterialPropertyName2 "", "X:\DONNEES BE\DAO\BUREAU ETUDES\BIBLI SOLIDWORKS\MATERIAUX\MATERIAUX QUALI-CITE/QUALI-CITE.sldmat", "ALUMINIUM 6060 - RAL 7035"
    
    boolstatus = swPart.AddConfiguration2("R7037", "", "", True, False, False, True, 256)
    swModel.SetMaterialPropertyName2 "", "X:\DONNEES BE\DAO\BUREAU ETUDES\BIBLI SOLIDWORKS\MATERIAUX\MATERIAUX QUALI-CITE/QUALI-CITE.sldmat", "ALUMINIUM 6060 - RAL 7037"
    
    boolstatus = swPart.AddConfiguration2("R9005", "", "", True, False, False, True, 256)
    swModel.SetMaterialPropertyName2 "", "X:\DONNEES BE\DAO\BUREAU ETUDES\BIBLI SOLIDWORKS\MATERIAUX\MATERIAUX QUALI-CITE/QUALI-CITE.sldmat", "ALUMINIUM 6060 - RAL 9005"
    
    
    '''----------------------------------------------------------------------------
    
        
    '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

Hello
I haven't had time to look at this topic yet, there's already this example of the API working (basically the "problem" is related to the fact that by default it applies the color of the material to all configurations).
Get and Set Material Visual Properties Example (VBA) - 2022 - SOLIDWORKS API Help

1 Like

Yes, that's the "problem".
It applies materials well to each configuration, but it is the appearance of the first material that is applied to all configurations. And that's what I have a problem with because you have to go back to each configuration to reapply the corresponding material.

Hello;

Shouldn't we create a display state for each new configuration?
Or by leaving the option "Link display states to configurations" => checked.

of the genre: (To be adapted)

Dim swConfig As SldWorks.Configuration
Dim displayStateNames As Variant

Set swConfig = swModel.GetActiveConfiguration
status = swConfig.CreateDisplayState("THE MATERIAU+PAINT NO")

Kind regards.

1 Like

Hello @Maclane
Yes, it would be simpler, but we can't work with display states, because we have properties related to each configuration that are retrieved from our BOMs. So we have to work with configurations and therefore related appearances.

I have trouble understanding your reasoning, creating display states is not incompatible with using configurations.
This is the only method I have found to create my multi-material part libraries ...

1 Like

Hello
On my files it is set with the option "Link display states to configurations" by default.
The problem is not there, I tested the code I linked, it applies the right appearance but as explained I don't have time to "dissect" this example to integrate it into the original macro.

Thank you for the advice @Maclane ,
I checked and I have a display status by configuration. They have well related to the configurations.
What I meant is that we only work with one display state per configuration.
We need a configuration by color so that there is an article code per configuration.
In itself, the macro function because it assigns materials and appearances by configuration. The only problem is that the color of the first configuration applies to all configurations.
After that, I'm interested in a little more explanation about your method for multi-material parts. I don't understand how it works. Thank you

Thank you @Cyril.f ,
I checked and for me too the display states are linked to the configurations.
I'm going to dive into the code you sent me to try to find a solution.
I open my neural box and I get started...

Hello;

I just noticed that "swPart.AddConfiguration2" was already deprecated in 2017!???
Let's see if replacing it with AddConfiguration3, associated with "swConfigurationOptions2_e Enumeration" doesn't solve your problem.
https://help.solidworks.com/2017/english/api/sldworksapi/solidworks.interop.sldworks~solidworks.interop.sldworks.imodeldoc2~addconfiguration3.html
https://help.solidworks.com/2017/english/api/swconst/SOLIDWORKS.Interop.swconst~SOLIDWORKS.Interop.swconst.swConfigurationOptions2_e.html

Kind regards.

2 Likes

Other avenues to explore:

https://help.solidworks.com/2021/English/SolidWorks/sldworks/r_Materials_in_Configurations.htm
… sorry, I don't have time to test...

2 Likes

Thank you @Maclane ,
I adapted what I found on CodeStack and it works wonderfully :+1:
And thank you all for your answers which allowed me to move forward and understand a little more about VBA

Glad I was helpful...
What did it come from then?
A sharing of the corrected code would be welcome. :roll_eyes:

1 Like

Here is the part concerning the ARLs:

Type ConfigData
    MaterialFilePath    As String
    ConfigNameSuffix    As String
    colorName           As String
    
End Type

Const PRP_NAME          As String = "DESIGNATION 2"

Dim swApp As SldWorks.SldWorks

Sub main()
    
    Dim CONFIGS_DATA(4) As ConfigData

    CONFIGS_DATA(0).colorName = "ALUMINIUM BRUT"
    CONFIGS_DATA(0).ConfigNameSuffix = "-00"
    CONFIGS_DATA(0).MaterialFilePath = "X:\DONNEES BE\DAO\BUREAU ETUDES\BIBLI SOLIDWORKS\APPARENCES\alu inox.p2m"

    CONFIGS_DATA(1).colorName = "RAL 1013"
    CONFIGS_DATA(1).ConfigNameSuffix = "-R1013"
    CONFIGS_DATA(1).MaterialFilePath = "X:\DONNEES BE\DAO\BUREAU ETUDES\BIBLI SOLIDWORKS\PEINTURE RAL\PE-TEXTURÉE\ral 1013 mat.p2m"

    CONFIGS_DATA(2).colorName = "RAL 1018"
    CONFIGS_DATA(2).ConfigNameSuffix = "-R1018"
    CONFIGS_DATA(2).MaterialFilePath = "X:\DONNEES BE\DAO\BUREAU ETUDES\BIBLI SOLIDWORKS\PEINTURE RAL\PE-TEXTURÉE\ral 1018 mat.p2m"

    CONFIGS_DATA(3).colorName = "RAL 2008"
    CONFIGS_DATA(3).ConfigNameSuffix = "-R2008"
    CONFIGS_DATA(3).MaterialFilePath = "X:\DONNEES BE\DAO\BUREAU ETUDES\BIBLI SOLIDWORKS\PEINTURE RAL\PE-TEXTURÉE\ral 2008 mat.p2m"

    CONFIGS_DATA(4).colorName = "RAL 3000"
    CONFIGS_DATA(4).ConfigNameSuffix = "-R3000"
    CONFIGS_DATA(4).MaterialFilePath = "X:\DONNEES BE\DAO\BUREAU ETUDES\BIBLI SOLIDWORKS\PEINTURE RAL\PE-TEXTURÉE\ral 3000 mat.p2m"

    Set swApp = Application.SldWorks
    
    Dim swModel As SldWorks.ModelDoc2
    
    Set swModel = swApp.ActiveDoc
    
    Dim i As Integer
    
    For i = 0 To UBound(CONFIGS_DATA)
        
        Dim confName As String
        
        confName = GetFileNameWithoutExtension(swModel.GetPathName())
        
        If CONFIGS_DATA(i).ConfigNameSuffix <> "" Then
            confName = confName & CONFIGS_DATA(i).ConfigNameSuffix
        End If
        
        If i <> 0 Then
            swModel.AddConfiguration3 confName, "", "", 0
        End If
        
        swModel.ConfigurationManager.ActiveConfiguration.Name = confName
        
        If CONFIGS_DATA(i).MaterialFilePath <> "" Then
            AddRenderMaterial swModel, CONFIGS_DATA(i).MaterialFilePath
        End If
        
        AddConfigProperty swModel, CONFIGS_DATA(i).colorName
        
    Next

End Sub

Sub AddRenderMaterial(model As SldWorks.ModelDoc2, path As String)
    
    Dim swRenderMaterial As SldWorks.RenderMaterial
    Set swRenderMaterial = model.Extension.CreateRenderMaterial(path)
    
    If False <> swRenderMaterial.AddEntity(model) Then
        If False = model.Extension.AddDisplayStateSpecificRenderMaterial(swRenderMaterial, swDisplayStateOpts_e.swThisDisplayState, Empty, -1, -1) Then
            Err.Raise vbError, "", "Failed to apply render material to display state"
        End If
    Else
        Err.Raise vbError, "", "Failed to add model as entity to render material"
    End If
   
End Sub

Sub AddConfigProperty(model As SldWorks.ModelDoc2, colorName As String)
    
    Dim swCustPrpMgr As SldWorks.CustomPropertyManager
    
    Set swCustPrpMgr = model.Extension.CustomPropertyManager("")
    
    Dim prpVal As String
    
    swCustPrpMgr.Get4 PRP_NAME, False, "", prpVal
    
    Set swCustPrpMgr = model.ConfigurationManager.ActiveConfiguration.CustomPropertyManager
    
    swCustPrpMgr.Add3 PRP_NAME, swCustomInfoType_e.swCustomInfoText, prpVal & colorName, swCustomPropertyAddOption_e.swCustomPropertyReplaceValue

End Sub

Function GetFileNameWithoutExtension(filePath As String) As String
    GetFileNameWithoutExtension = Mid(filePath, InStrRev(filePath, "\") + 1, InStrRev(filePath, ".") - InStrRev(filePath, "\") - 1)
    
End Function

:+1: