Get Material Properties Example (VBA)
This example shows how to iterate over the faces and features of a model
and determine the displayed colors.
'-----------------------------------------------
'
' Problem:
' When
in shaded mode, colors can be allocated,
' in
order of priority, at the model, feature, and
' face
level. Thus, to correctly determine the
' color
of a face, it is necessary to examine
' the
color of the face at all these levels.
'
' There
are several document-level preferences
' that
also control how color is used in a
' model.
'
' This
sample code shows how to iterate over the
' faces
and features of a model and correctly
' determine
the displayed color.
'
' Preconditions:
' (1)
Part or assembly is open.
' (2)
In an assembly, something is selected.
'
' Postconditions: None
'
'-----------------------------------------------
Option Explicit
Public Enum swDocumentTypes_e
swDocNONE
= 0 '
Used to
be TYPE_NONE
swDocPART
= 1 '
Used to
be TYPE_PART
swDocASSEMBLY
= 2 '
Used to
be TYPE_ASSEMBLY
swDocDRAWING
= 3 '
Used to
be TYPE_DRAWING
End Enum
Public Enum swUserPreferenceToggle_e
swIgnoreFeatureColors
= 3
End Enum
Public Enum swUserPreferenceIntegerValue_e
swDocumentColorShading
= 185
End Enum
Sub main()
Dim
swApp As
SldWorks.SldWorks
Dim
swModel As
SldWorks.ModelDoc2
Dim
swSelMgr As
SldWorks.SelectionMgr
Dim
swPart As
SldWorks.PartDoc
Dim
swComp As
SldWorks.Component2
Dim
swBody As
SldWorks.body2
Dim
swFace As
SldWorks.face2
Dim
swFeat As
SldWorks.feature
Dim
vMatProp As
Variant
Dim
vConfigNames As
Variant
Dim
i As
Long
Dim
bRet As
Boolean
Set
swApp = CreateObject("SldWorks.Application")
Set
swModel = swApp.ActiveDoc
Set
swSelMgr = swModel.SelectionManager
If
swModel.GetType = swDocPART Then
Set
swPart = swModel
Set
swBody = swPart.Body
Else
Set
swComp = swSelMgr.GetSelectedObjectsComponent2(1)
Set
swBody = swComp.GetBody
End
If
Set
swFace = swBody.GetFirstFace
'
Get document default colors and settings
Debug.Print
"Model Default [" & swModel.GetPathName
& "]"
Debug.Print
" IgnoreFeatureColors
= "
& swModel.GetUserPreferenceToggle(swIgnoreFeatureColors)
Debug.Print
" ColorRef
=
" & swModel.GetUserPreferenceIntegerValue(swDocumentColorShading)
Debug.Print
""
vMatProp
= swModel.MaterialPropertyValues
Debug.Print
" RGB
=
[" & _
vMatProp(0)
& ", " & _
vMatProp(1)
& ", " & _
vMatProp(2)
& "]"
Debug.Print
" Ambient
=
" & vMatProp(3)
Debug.Print
" Diffuse
=
" & vMatProp(4)
Debug.Print
" Specular
=
" & vMatProp(5)
Debug.Print
" Shininess
=
" & vMatProp(6)
Debug.Print
" Transparency
= " & vMatProp(7)
Debug.Print
" Emission
=
" & vMatProp(8)
Debug.Print
"--------------------------------------"
If
Not swComp Is Nothing Then
'
Component colors can override face colors
vMatProp
= swComp.MaterialPropertyValues
If
Not IsEmpty(vMatProp) Then
Debug.Print
"Component Default [" & swComp.GetPathName
& "]"
Debug.Print
" RGB
=
[" & _
vMatProp(0)
& ", " & _
vMatProp(1)
& ", " & _
vMatProp(2)
& "]"
Debug.Print
" Ambient
=
" & vMatProp(3)
Debug.Print
" Diffuse
=
" & vMatProp(4)
Debug.Print
" Specular
=
" & vMatProp(5)
Debug.Print
" Shininess
=
" & vMatProp(6)
Debug.Print
" Transparency
= " & vMatProp(7)
Debug.Print
" Emission
=
" & vMatProp(8)
Debug.Print
"--------------------------------------"
End
If
End
If
While
Not swFace Is Nothing
'
Face colors can override part colors
vMatProp
= swFace.MaterialPropertyValues
If
Not IsEmpty(vMatProp) Then
Debug.Print
"Face[" & i & "] overridden"
Debug.Print
" RGB
=
[" & _
vMatProp(0)
& ", " & _
vMatProp(1)
& ", " & _
vMatProp(2)
& "]"
Debug.Print
" Ambient
=
" & vMatProp(3)
Debug.Print
" Diffuse
=
" & vMatProp(4)
Debug.Print
" Specular
=
" & vMatProp(5)
Debug.Print
" Shininess
=
" & vMatProp(6)
Debug.Print
" Transparency
= " & vMatProp(7)
Debug.Print
" Emission
=
" & vMatProp(8)
Debug.Print
"--------------------------------------"
End
If
'
Get feature associated with this face
Set
swFeat = swFace.GetFeature
vConfigNames
= swModel.GetConfigurationNames
'
Face colors can override feature colors
vMatProp
= swFeat.GetMaterialPropertyValues2(swSpecifyConfiguration,
(vConfigNames))
If
Not IsEmpty(vMatProp) Then
Debug.Print
"Face[" & i & "] overridden by "; swFeat.Name
Debug.Print
" RGB
=
[" & _
vMatProp(0)
& ", " & _
vMatProp(1)
& ", " & _
vMatProp(2)
& "]"
Debug.Print
" Ambient
=
" & vMatProp(3)
Debug.Print
" Diffuse
=
" & vMatProp(4)
Debug.Print
" Specular
=
" & vMatProp(5)
Debug.Print
" Shininess
=
" & vMatProp(6)
Debug.Print
" Transparency
= " & vMatProp(7)
Debug.Print
" Emission
=
" & vMatProp(8)
Debug.Print
"--------------------------------------"
End
If
i
= i + 1
Set
swFace = swFace.GetNextFace
Wend
End Sub
'----------------------------------------------------