AnsweredAssumed Answered

Change color of selected faces

Question asked by Slavomir Kubasky on Sep 29, 2011
Latest reply on Jun 13, 2016 by Vincent Li

Hi,

 

I'm trying to modify solidworks example macro "Change color of selected face" for situation when I have selected more than one face of part. It sounds quite

simple, but my knowledge of API poor. Can anybody help me with this?

Thank you

 

 

Sub main()

    Dim swApp                       As Object

    Dim swModel                     As Object

    Dim swSelMgr                    As Object
   
    Dim swSelFace()                 As Object
   
    Dim nSelCount                   As Long

    Dim vFaceProp                   As Variant

    Dim bRet                        As Boolean
   
    Dim i                           As Long
   
    Dim vFaceList                   As Variant

 

   

    Set swApp = Application.SldWorks

    Set swModel = swApp.ActiveDoc

    Set swSelMgr = swModel.SelectionManager

    nSelCount = swSelMgr.GetSelectedObjectCount

    ReDim swSelFace(nSelCount - 1)

    For i = 1 To nSelCount

        Set swSelFace(i - 1) = swSelMgr.GetSelectedObject5(i)

    Next
   

     vFaceProp = swSelFace(i).MaterialPropertyValues
   

    If IsEmpty(vFaceProp) Then

        ' Is empty if face-level colors were not specified, so get them from underlying model

        vFaceProp = swModel.MaterialPropertyValues: Debug.Assert Not IsEmpty(vFaceProp)

    End If

   

    'Current color

    Debug.Print "RGB            = [" & vFaceProp(0) * 255# & ", " & vFaceProp(1) * 255# & ", " & vFaceProp(2) * 255# & "]"

    Debug.Print "Ambient        = " & vFaceProp(3)

    Debug.Print "Diffuse        = " & vFaceProp(4)

    Debug.Print "Specular       = " & vFaceProp(5)

    Debug.Print "Shininess      = " & vFaceProp(6)

    Debug.Print "Transparency   = " & vFaceProp(7)

    Debug.Print "Emission       = " & vFaceProp(8)

   

    ' New color

         bRet = swModel.SelectedFaceProperties( _
            RGB(0, 0, 255), _
            vFaceProp(3), vFaceProp(4), vFaceProp(5), _
            vFaceProp(6), vFaceProp(7), vFaceProp(8), _
            False, ""): Debug.Assert bRet
  

    ' Deselect face to see new color

    swModel.ClearSelection2 True

End Sub

Outcomes