Get current display state in VBA

I’m trying to build a macro to cycle through all the display states of the active document, and I would like to get the name of active display state of the active document.

I thought of using the Names Property of IDisplayStateSetting like this:

' Display state permutation
'
' A macro to cycle through all the display state of the active file.

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2

Sub main()

    Set swApp = Application.SldWorks

try_:
    ' Comment the line below to avoid error to exit the macro, for debug only
    On Error GoTo catch_
    Set swModel = swApp.ActiveDoc

    ' Check if the open file is an assembly and it is saved
    If swModel Is Nothing Then
        Err.Raise vbError, "", "Open a file to run the macro"
    End If
    
    If swModel.GetPathName() = "" Then
        Err.Raise vbError, "", "Save the document to run the macro"
    End If
    
    If swModel.GetType() = swDocumentTypes_e.swDocDRAWING Then
        Err.Raise vbError, "", "Open an assembly or a part file to run the macro"
    End If

    ' Get file configurations and the active display state of the file
    Dim swConfigMgr As SldWorks.ConfigurationManager
    Set swConfigMgr = swModel.ConfigurationManager

    Dim vConfs As Variant
    vConfs = swModel.GetConfigurationNames

    Dim activeConfig As Configuration
    Set activeConfig = swConfigMgr.ActiveConfiguration

    Dim confName As String
    confName = activeConfig.Name

    Dim swConf As SldWorks.Configuration
    Set swConf = swModel.GetConfigurationByName(confName)

    Dim vDispStates As Variant
    vDispStates = swConf.GetDisplayStates

    Dim swComponent As SldWorks.Component2
    Set swComponent = swConf.GetRootComponent()

    Dim swDSS As SldWorks.DisplayStateSetting
    Set swDSS = swModel.Extension.GetDisplayStateSetting(swDisplayStateOpts_e.swThisDisplayState)

    Dim activeDispState() As String
    activeDispState = swDSS.Names

    GoTo finally_

catch_:
    MsgBox Err.Description, vbCritical, "Display state permutation"

finally_:

End Sub

but the activeDispState returns empty.

Where am I wrong? Is the a better way to get the current display state with VBA?

Thanks in advance.

1 Like

Add these lines

Dim nbrDisplayStates As Long
nbrDisplayStates = swConf.GetDisplayStatesCount

Dim i As Long

Debug.Print "This configuration's display states ="
For i = 0 To (nbrDisplayStates - 1)
    Debug.Print "  Display state name = " & vDispStates(i)
Next i

after

vDispStates = swConf.GetDisplayStates

3 Likes

:exploding_head:

How did you know that the first element of GetDisplayStates is the current one? Did I miss it somewhere in the documentation?

Thanks a lot by the way!

In case anyone is interested, here the code of a macro to change the active display state with the click of a button

' Display state permutation
'
' A macro to cycle through all the display state of the active file.

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2

Sub main()

    Set swApp = Application.SldWorks

try_:
    ' Comment the line below to avoid error to exit the macro, for debug only
    On Error GoTo catch_
    Set swModel = swApp.ActiveDoc

    ' Check if the open file is an assembly and it is saved
    If swModel Is Nothing Then
        Err.Raise vbError, "", "Open a file to run the macro"
    End If
    
    If swModel.GetPathName() = "" Then
        Err.Raise vbError, "", "Save the document to run the macro"
    End If
    
    If swModel.GetType() = swDocumentTypes_e.swDocDRAWING Then
        Err.Raise vbError, "", "Open an assembly or a part file to run the macro"
    End If

    ' Get file configurations and the active display state of the file
    Dim swConfigMgr As SldWorks.ConfigurationManager
    Set swConfigMgr = swModel.ConfigurationManager

    Dim vConfs As Variant
    vConfs = swModel.GetConfigurationNames

    Dim activeConfig As Configuration
    Set activeConfig = swConfigMgr.ActiveConfiguration

    Dim confName As String
    confName = activeConfig.Name

    Dim swConf As SldWorks.Configuration
    Set swConf = swModel.GetConfigurationByName(confName)

    Dim nbrDisplayStates As Long
    nbrDisplayStates = swConf.GetDisplayStatesCount

    Dim vDispStates As Variant
    vDispStates = swConf.GetDisplayStates

    Dim activeDispState As String
    activeDispState = vDispStates(0)

    ' Sort the array of display states
    vDispStates = SortArrayAtoZ(vDispStates)

    Dim i As Integer

    For i = 0 To UBound(vDispStates)
        
        If activeDispState = vDispStates(i) Then
            
            ' Get the name of the next configuration
            Dim nextDispState As String
            
            ' If this is the last element of the array, restart the counter
            If i = UBound(vDispStates) Then
                nextDispState = vDispStates(0)
            Else
                nextDispState = vDispStates(i + 1)
            End If

            Dim retValue As Boolean
            retValue = swConf.ApplyDisplayState(nextDispState)

            GoTo finally_

        End If

    Next

    GoTo finally_

catch_:
    MsgBox Err.Description, vbCritical, "Cambia stato di visualizzazione"

finally_:
    swModel.GraphicsRedraw2

End Sub

Function SortArrayAtoZ(myArray As Variant)

Dim i As Long
Dim j As Long
Dim Temp

'Sort the Array A-Z
For i = LBound(myArray) To UBound(myArray) - 1
    For j = i + 1 To UBound(myArray)
        If UCase(myArray(i)) > UCase(myArray(j)) Then
            Temp = myArray(j)
            myArray(j) = myArray(i)
            myArray(i) = Temp
        End If
    Next j
Next i

SortArrayAtoZ = myArray

End Function
2 Likes

Active display state is always the first element of GetDisplayStates. Check remarks in the help page here GetDisplayStates Method (IConfiguration) - 2022 - SOLIDWORKS API Help

3 Likes