Hide Table of Contents

Make Part Transparent Example (VBA)

This example shows how to make a part transparent.

' Preconditions:
' 1. Open a part.
' 2. Open the Immediate window.
' Postconditions:
' 1. Makes the part transparent.
' 2. Examine the graphics area and Immediate window.
Option Explicit
Function GetRValue(MyColour As Long) As Long
    GetRValue = MyColour Mod 256
End Function
Function GetGValue(MyColour As Long) As Long
    GetGValue = (MyColour \ 256) Mod 256
End Function
Function GetBValue(MyColour As Long) As Long
    GetBValue = (MyColour \ 65536) Mod 256
End Function
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swPart As SldWorks.PartDoc
    Dim nDefaultColor As Long
    Dim vBodyArr As Variant
    Dim vBody As Variant
    Dim swBody As SldWorks.Body2
    Dim swFace As SldWorks.Face2
    Dim nMatProp(9) As Double
    Dim vMatProp As Variant
    Dim bRet As Boolean
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swPart = swModel
    nDefaultColor = swModel.GetUserPreferenceIntegerValue(swDocumentColorShading)
    Debug.Print "File = " & swModel.GetPathName
    Debug.Print "  Default Color = RGB(" & GetRValue(nDefaultColor) & ", " & GetGValue(nDefaultColor) & ", " & GetBValue(nDefaultColor) & ")"
    ' Set to sensible defaults
    ' Gray for default color
    nMatProp(0) = GetRValue(nDefaultColor) / 255#      ' red
    nMatProp(1) = GetRValue(nDefaultColor) / 255#      ' green
    nMatProp(2) = GetRValue(nDefaultColor) / 255#      ' blue
    nMatProp(3) = 1#                    ' Ambient
    nMatProp(4) = 1#                    ' Diffuse
    nMatProp(5) = 1#                    ' Specular
    nMatProp(6) = 0.31                  ' Shininess
    ' Increase transparency
    nMatProp(7) = 0.95                  ' Transparency
    nMatProp(8) = 0#                    ' Emmission
    vMatProp = nMatProp
    vBodyArr = swPart.GetBodies2(swAllBodies, True)
    For Each vBody In vBodyArr
        Set swBody = vBody
        Set swFace = swBody.GetFirstFace
        While Not swFace Is Nothing
            swFace.MaterialPropertyValues = vMatProp
            Set swFace = swFace.GetNextFace
    ' Redraw to see new transparency
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

Subject:   Feedback on Help Topics
Page:   Make Part Transparent Example (VBA)
*   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:


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

Web Help Content Version: API Help (English only) 2016 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.