Hide Table of Contents

Change Material Properties Example (VBA)

This example shows how to change the color of the selected assembly component.

'----------------------------------------------------
' Preconditions:
' 1. Open install_dir\samples\tutorial\api\landing_gear.sldasm.
' 2. Click oleostrut<1> in the FeatureManager design tree.
' 3. Open the Immediate window.
'
' Postconditions:
' 1. Changes the color of the selected component to blue.
' 2. Examine the component in the graphics area and
'    the Immediate window.
'
' NOTE: Because this assembly is used elsewhere, do not save
' changes.
'----------------------------------------------------
Option Explicit
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swCompModel As SldWorks.ModelDoc2
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swComp As SldWorks.Component2
    Dim vMatProp As Variant
    Dim bRet As Boolean
    Set swApp = CreateObject("SldWorks.Application")
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    Set swComp = swSelMgr.GetSelectedObjectsComponent2(1)
    vMatProp = swComp.MaterialPropertyValues
    If IsEmpty(vMatProp) Then
        ' Empty if no component-level colors specified,
        ' so get from underlying model
        Set swCompModel = swComp.GetModelDoc
        If swCompModel Is Nothing Then
            ' Component is lightweight
            Debug.Print "Selected component is lightweight; exiting macro."
            Exit Sub
        End If
        vMatProp = swCompModel.MaterialPropertyValues
    End If
    Debug.Print "File = " & swModel.GetPathName
    Debug.Print "  Component = " & swComp.Name2 & " (" & swComp.ReferencedConfiguration & ")"
    Debug.Print "    State = " & swComp.GetSuppression
    Debug.Print "    Suppressed = " & swComp.IsSuppressed
    Debug.Print "    Hidden = " & swComp.IsHidden(False)
    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)
    ' Force component color to blue
    vMatProp(0) = 0#
    vMatProp(1) = 0#
    vMatProp(2) = 1#
    swComp.MaterialPropertyValues = vMatProp
    ' Deselect component to see new color
    swModel.ClearSelection2 True    
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:   Change 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.