VBA Custom Properties: Create and Populate

Hello

I'm working on a macro and would like to be able to easily create and write a custom property.

I figured out how to do it for configuration-specific properties but I couldn't find for custom properties.
Thank you for your help, I'm sure it's very simple for you!

If it can help someone with the rest...

Sub Valid()

    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim configNames()  As String
    Dim configName As String
    Dim swConfig As SldWorks.Configuration
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set FileSys = CreateObject("Scripting.FileSystemObject")

' Mettre la valeur de Ma_variable dans une propriété Ma_Variable_Value de chaque config
    configNames = swModel.GetConfigurationNames
    vConfigNameArr = swModel.GetConfigurationNames
    For Each vConfigName In vConfigNameArr
        vCustInfoNameArr = swModel.GetCustomInfoNames2(vConfigName)
        If Not IsEmpty(vCustInfoNameArr) Then
            'For Each vCustInfoName In vCustInfoNameArr
            
                swModel.CustomInfo2(vConfigName, "Ma_Variable_Value") = Ma_variable
                
            'Next
        End If
    Next
    
Exit Sub
    
End Sub

 

I found the following function allows you to put the value in the corresponding Custom Property...

"Ma_Variable_Value") = Ma_variable

... if this Custom Property exists

but how can we create it before, if it doesn't exist a basis

Hello

Below is a macro that creates custom properties on the file and on the configurations (pumped from SW and adapted by us given the comments):

' ----------------------------------------------

' Postconditions: The custom property Input1 is added to the file.

' NOTE: Because configurations are not supported in drawings, pass a blank string when the file is a drawing.

'------------------------------------------------

 

Dim swApp       As SldWorks.SldWorks

Dim swModel     As SldWorks.ModelDoc2

Dim retval      As String

   Dim vConfigNameArr              as Variant

    Dim vConfigName                 as Variant

    Dim swActiveConf                As SldWorks.Configuration

    Dim swConf                      As SldWorks.Configuration

    Dim swConfMgr                   As SldWorks.ConfigurationManager

    Dim swDerivConf                 As SldWorks.Configuration

    Dim bRet                        As Boolean

Sub main()
Set swApp = Application.SldWorks

Set swModel = swApp.ActiveDoc

    Set swConfMgr = swModel.ConfigurationManager

     Set swConfig = swConfMgr.ActiveConfiguration

        vConfigNameArr = swModel.GetConfigurationNames

retval = swModel.AddCustomInfo3("", "IsFastener", swCustomInfoYesOrNo, "No")
retval = swModel.AddCustomInfo3("", "Library", swCustomInfoYesOrNo, "No")
retval = swModel.AddCustomInfo3("", "Piece_standard", swCustomInfoYesOrNo, "No")
retval = swModel.AddCustomInfo3("", "Spare", swCustomInfoText, "-")
retval = swModel.AddCustomInfo3("", "Coordinate System", swCustomInfoText, "0")
retval = swModel.AddCustomInfo3("", "Repère_soudure", swCustomInfoText, "-")
retval = swModel.AddCustomInfo3("", "Revision", swCustomInfoText, "-")

    vConfigNameArr = swModel.GetConfigurationNames

 For Each vConfigName In vConfigNameArr

        Set swConf = swModel.GetConfigurationByName(vConfigName)

                ' Do not assert; will be NULL if (derived) configuration already exists

retval = swModel.AddCustomInfo3(swConf.Name, "Numero_plan", swCustomInfoText, "$PRPSHEET:""SW-File Name(File Name)""")
retval = swModel.AddCustomInfo3(swConf.Name, "Description_French", swCustomInfoText, "-")
retval = swModel.AddCustomInfo3(swConf.Name, "Description_French_2", swCustomInfoText, "-")
retval = swModel.AddCustomInfo3(swConf.Name, "Observation", swCustomInfoText, "-")
retval = swModel.AddCustomInfo3(swConf.Name, "Weight", swCustomInfoText, """SW-Mass@@Défaut@ModelDoc2.SLDPRT""")
retval = swModel.AddCustomInfo3(swConf.Name, "Material", swCustomInfoText, """SW-Material@@Défaut@ModelDoc2.SLDPRT""")
retval = swModel.AddCustomInfo3(swConf.Name, "CREATIONAUTO", swCustomInfoText, "NO")
     

    Next

MsgBox retval

End Sub

' ----------------------------------------------

 

 

1 Like

Great, thank you so much, it's interesting

it works, but I don't understand why you have to put the function create a custom property in a variable like Boolean.

Dim value As System.Boolean
 
value = instance. AddCustomInfo3(Configuration, FieldName, FieldType, FieldValue)