MACRO LIBRARY

Working with display states

Free public access. No account or subscription required.

Files

Working with display states

Code

Module:

'Demonstrates how to work with display states. Non-indempotent.
    
'Preconditions: part is open.
'Results:
'1. Gets the active display state and renames it.
'2. Traverse all display states and print their names and associated configurations.
'3. Create a new display state in the current configuration. If "Link display states
'   to configurations" is ture then a display state is created in each configuration.
'   The display state will have the name <config name>-DisplayState.
'4. Rebuild the model to show the newly created display state.
    
'Notes: Not demonstrated here is how to change a display state, which is done using
'   IConfiguration.ApplyDisplayState.
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As ModelDoc2
    Dim swConfig As Configuration
    Dim vConfigNames As Variant
    Dim i As Integer
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swConfig = swModel.GetActiveConfiguration
    
    If swModel.Extension.LinkedDisplayState Then
        vConfigNames = swModel.GetConfigurationNames
        For i = 0 To UBound(vConfigNames)
            Debug.Print "Configuration: " & vConfigNames(i)
            Set swConfig = swModel.GetConfigurationByName(vConfigNames(i))
            DoTheWork swConfig
        Next i
    Else
        Set swConfig = swModel.GetActiveConfiguration
        Debug.Print "Configuration: " & swConfig.Name
        DoTheWork swConfig
    End If
    
    swModel.EditRebuild3
End Sub
    
Private Sub DoTheWork(swConfig As Configuration)
    Dim vDisplayStateNames As Variant
    Dim i As Integer
    
    'The first element is the active display state
    vDisplayStateNames = swConfig.GetDisplayStates
    
    swConfig.RenameDisplayState vDisplayStateNames(0), vDisplayStateNames(0) & "_new"
    
    For i = 0 To UBound(vDisplayStateNames)
        Debug.Print vbTab & vDisplayStateNames(i)
    Next i
    
    'Note: This will not return false if a display state with this name already exists.
    If swConfig.CreateDisplayState(swConfig.Name & "-DisplayState") Then
        Debug.Print "Failed to create display state."
    End If
End Sub