MACRO LIBRARY

Remove appearance of selected object using IDisplayStateSetting

Free public access. No account or subscription required.

Files

Remove appearance of selected object using IDisplayStateSetting

Code

Module: mMain

'Remove appearance at assembly or part level for selected object.
    
'Preconditions: part or assembly is open and object is selected.
'Results: object's appearance is removed at specified level.
    
'Notes:
'IDisplayStateSetting is available starting in SolidWorks 2012. For earlier _
    versions, use IComponent2::RemoveMaterialProperty2 to remove an _
    appearance from the assembly level. Use IComponent2::GetModelDoc2 and then _
    IModelDocExtension::RemoveMaterialProperty to remove an appearance from _
    part level.
'The parameter in IModelDocExtension::GetDisplayStateSetting and _
    IDisplayStateSetting::Option are redundant.
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Const APPLY_AT_PART_LEVEL As Boolean = False
    
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swObj(0) As Object
    Dim swDSS As SldWorks.DisplayStateSetting
    Dim swAS(0) As SldWorks.AppearanceSetting
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    Set swDSS = swModel.Extension.GetDisplayStateSetting(swThisDisplayState)
    
    If swSelMgr.GetSelectedObjectCount2(-1) = 0 Then Exit Sub
    
    Set swObj(0) = swSelMgr.GetSelectedObject6(1, -1)
    
    swDSS.Entities = swObj
    swDSS.PartLevel = APPLY_AT_PART_LEVEL
    swDSS.RemoveAppearance = True
    
    Set swAS(0) = swModel.Extension.GetAppearanceSetting
    
    swModel.Extension.DisplayStateSpecMaterialPropertyValues(swDSS) = swAS
    
    swModel.EditRebuild3
End Sub