Change Color of Face Example (VBA)
This example shows how to change the color of the selected face to blue.
'-----------------------------------------------------
'
' Preconditions:
' (1)
Part or assembly is open.
' (2)
Face is selected.
'
' Postconditions: Color of selected face is changed to
blue.
'
'------------------------------------------------------
Option Explicit
Sub main()
Dim
swApp As
SldWorks.SldWorks
Dim
swModel As
SldWorks.ModelDoc2
Dim
swSelMgr As
SldWorks.SelectionMgr
Dim
swFace As
SldWorks.face2
Dim
vFaceProp As
Variant
Dim
bRet As
Boolean
Set
swApp = Application.SldWorks
Set
swModel = swApp.ActiveDoc
Set
swSelMgr = swModel.SelectionManager
Set
swFace = swSelMgr.GetSelectedObject5(1)
vFaceProp
= swFace.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
'-----------------------------------------------------