Hide Table of Contents

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

'----------------------------------------------------



Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Get Material Properties Example (VBA)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:

x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2015 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.