MACRO LIBRARY

Set selected object's appearance using IDisplayStateSetting

Free public access. No account or subscription required.

Files

Set selected object's appearance using IDisplayStateSetting

Code

Module: mMain

'Demonstrates how to change the color of any number of selected objects _
    in the current display state in SolidWorks 2012 and later using _
    IModelDocExtension::DisplayStateSpecMaterialPropertyValues.
    
'Preconditions: part or assembly is open and objects are selected.
'Results: color is changed in specified display states (see constants).
    
'Notes:
'If a component is selected in an assembly, the color is applied at the _
    part only if IDisplayStateSetting::PartLevel = True. Faces and bodies _
    selected in an assembly are always changed at the part level.
'If you select an object that can't be colored, such as an edge, then _
    none of the objects you selected will be colored properly.
'The parameter in IModelDocExtension::GetDisplayStateSetting and _
    IDisplayStateSetting::Option are redundant.
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Const RED_VAL As Integer = 0
Const GREEN_VAL As Integer = 0
Const BLUE_VAL As Integer = 255
Const DISPLAY_STATE_OPTION = swDisplayStateOpts_e.swThisDisplayState
Const APPLY_AT_PART_LEVEL = True
    
Sub main()
    If red > 255 Or GREEN > 255 Or Blue > 255 Or _
        red < 0 Or GREEN < 0 Or Blue < 0 Then
        MsgBox "Color values must be between 0 and 255."
        Exit Sub
    End If
    
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swObj() As Object
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    
    If swSelMgr.GetSelectedObjectCount2(-1) = 0 Then Exit Sub
    
    ReDim swObj(swSelMgr.GetSelectedObjectCount2(-1) - 1)
    
    For i = 1 To swSelMgr.GetSelectedObjectCount2(-1)
        Set swObj(i - 1) = swSelMgr.GetSelectedObject6(i, -1)
    Next i
    
    ChangeAppearance swModel, swObj, DISPLAY_STATE_OPTION, APPLY_AT_PART_LEVEL, _
        RED_VAL, GREEN_VAL, BLUE_VAL
End Sub
    
Function ChangeAppearance(swModel As SldWorks.ModelDoc2, swObj As Variant, _
    intDisplayStateOption As Integer, blnPartLevel As Boolean, _
    r As Long, g As Long, b As Long) As Boolean
    
    Dim swDSS As SldWorks.DisplayStateSetting
    Set swDSS = swModel.Extension.GetDisplayStateSetting(intDisplayStateOption)
    swDSS.Entities = swObj
    swDSS.PartLevel = blnPartLevel
    
    Dim swAS(0) As SldWorks.AppearanceSetting
    Set swAS(0) = swModel.Extension.GetAppearanceSetting
    
    'Calculate color value (see API Help for IAppearanceSetting::Color)
    'Overflow error will occur if parentheses are not used or if _
        any of the variables involved are not declared as Long.
    Dim lngColor As Long
    lngColor = r + (g * 16 * 16) + (b * 16 * 16 * 16 * 16)
    
    swAS(0).color = lngColor
    
    swModel.Extension.DisplayStateSpecMaterialPropertyValues(swDSS) = swAS
End Function