Hide Table of Contents

Get Material Property Values Example (VBA)

This example shows how to get the color, a material property value, applied to each body in a part.

'-----------------------------------------------
' Preconditions:
' 1. Open a part to which color is applied
'    to one or more bodies.
' 2. Open the Immediate window.
'
' Postconditions:
' 1. Gets the color on each body in a part.
' 2. Examine the Immediate window.
'-----------------------------------------------
Option Explicit
Public Enum swBodyType_e
    swSolidBody = 0
    swSheetBody = 1
    swWireBody = 2
    swMinimumBody = 3
    swGeneralBody = 4
    swEmptyBody = 5
End Enum
 
Sub ProcessBodyArray(swApp As SldWorks.SldWorks, swModel As SldWorks.ModelDoc2, vBodyArr As Variant)
    Dim vBody As Variant
    Dim swBody As SldWorks.Body2
    Dim vMatProp As Variant
    For Each vBody In vBodyArr
        Set swBody = vBody
        vMatProp = swBody.MaterialPropertyValues2
        If IsEmpty(vMatProp) Then
            Debug.Print "    No color information."
        Else
            Debug.Print "    RGB            = [" & vMatProp(0) * 255# & ", " & vMatProp(1) * 255# & ", " & vMatProp(2) * 255# & "]"
            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
    Next
End Sub
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swPart As SldWorks.PartDoc
    Dim vBodyArr As Variant
    Dim i  As Long
    Dim bRet As Boolean
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swPart = swModel
    Debug.Print "File = " & swModel.GetPathName
    For i = 0 To 5
        vBodyArr = swPart.GetBodies2(i, False)
        If Not IsEmpty(vBodyArr) Then
            Debug.Print "  Body[" & i & "] = " & UBound(vBodyArr) + 1
            ProcessBodyArray swApp, swModel, vBodyArr
        End If
    Next i
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 Property Values 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) 2017 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.