Hide Table of Contents

Change Material Properties Example (VBA)

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

 

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

'

' Preconditions:

'       (1) Assembly is open.

'       (2) A component is selected.

'

' Postconditions:

'       (1) Color of selected component is changed to blue.

'       (2) Selection set is cleared.

'

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

 

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

            Exit Sub

        End If

        

        vMatProp = swCompModel.MaterialPropertyValues

    End If

        

    Debug.Print "File = " & swModel.GetPathName

    Debug.Print "  Comp = " & 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 face so we can see new colour

    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) 2012 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.