Hide Table of Contents

Get and Set Material Visual Properties Example (VBA)

This method shows how to get and set the material properties of a part.

'-----------------------------------------------------
' Preconditions: Open a part document.
'
' Postconditions: Inspect the Immediate window.
'------------------------------------------------------

Option Explicit

Sub main()

    Dim swApp As SldWorks.SldWorks
    Dim myModel As SldWorks.ModelDoc2
    Dim myPart As SldWorks.PartDoc
    Dim myMatVisProps As SldWorks.MaterialVisualPropertiesData
    Dim configName As String, databaseName As String
    Dim newPropName As String
    Dim orgBlend As Boolean, orgApply As Boolean
    Dim orgAngle As Double
    Dim orgScale As Double
    Dim longstatus As Long

    Set swApp = Application.SldWorks
    Set myModel = swApp.ActiveDoc
    Set myPart = myModel
    Set myMatVisProps = myPart.GetMaterialVisualProperties()

    Debug.Print "===== Material Visual Properties Example ====="

    If Not myMatVisProps Is Nothing Then
        Call dump_material_visual_properties(myMatVisProps, myPart)

    ' Set the material to something else, so that the display changes

        configName = "default"
        databaseName = "SolidWorks Materials"
        newPropName = "Beech"
        myPart.SetMaterialPropertyName2 configName, databaseName, newPropName
        Call dump_material_visual_properties(myMatVisProps, myPart)
    End If

    ' Set the material visual properties to be just color, no advanced graphics

    Set myMatVisProps = myPart.GetMaterialVisualProperties()

    If Not myMatVisProps Is Nothing Then
        longstatus = myPart.SetMaterialVisualProperties(myMatVisProps, swThisConfiguration, Nothing)
        Call dump_material_visual_properties(myMatVisProps, myPart)
       

    ' Set the material visual properties to be RealView
        myMatVisProps.RealView = True
        longstatus = myPart.SetMaterialVisualProperties(myMatVisProps, swThisConfiguration, Nothing)
        Call dump_material_visual_properties(myMatVisProps, myPart)

    ' Set the material visual properties to be SolidWorks standard textures

        myMatVisProps.RealView = False
        longstatus = myPart.SetMaterialVisualProperties(myMatVisProps, swThisConfiguration, Nothing)
        Call dump_material_visual_properties(myMatVisProps, myPart)
    End If

    Set myMatVisProps = myPart.GetMaterialVisualProperties()

    If Not myMatVisProps Is Nothing Then
        orgAngle = myMatVisProps.Angle
        myMatVisProps.Angle = orgAngle + 1#
        longstatus = myPart.SetMaterialVisualProperties(myMatVisProps, swThisConfiguration, Nothing)
        Call dump_material_visual_properties(myMatVisProps, myPart)
        orgScale = myMatVisProps.Scale2
        myMatVisProps.Scale2 = orgScale * 1.25
        longstatus = myPart.SetMaterialVisualProperties(myMatVisProps, swThisConfiguration, Nothing)
        Call dump_material_visual_properties(myMatVisProps, myPart)

    ' Toggle the standard texture to blend with the part color

        If myMatVisProps.BlendColor = 0 Then
            orgBlend = False
        Else
            orgBlend = True
        End If

        myMatVisProps.BlendColor = Not orgBlend
        longstatus = myPart.SetMaterialVisualProperties(myMatVisProps, swThisConfiguration, Nothing)
        Call dump_material_visual_properties(myMatVisProps, myPart)
        myMatVisProps.BlendColor = orgBlend
        longstatus = myPart.SetMaterialVisualProperties(myMatVisProps, swThisConfiguration, Nothing)
        Call dump_material_visual_properties(myMatVisProps, myPart)

    ' Toggle the apply material color to part flag

        If myMatVisProps.ApplyMaterialColorToPart = 0 Then
            orgApply = False
        Else
            orgApply = True
        End If

        myMatVisProps.ApplyMaterialColorToPart = Not orgApply
        longstatus = myPart.SetMaterialVisualProperties(myMatVisProps, swThisConfiguration, Nothing)
        Call dump_material_visual_properties(myMatVisProps, myPart)
        myMatVisProps.ApplyMaterialColorToPart = orgApply
        longstatus = myPart.SetMaterialVisualProperties(myMatVisProps, swThisConfiguration, Nothing)
        Call dump_material_visual_properties(myMatVisProps, myPart)

    ' Toggle the apply material hatch to drawing section view flag

        If myMatVisProps.ApplyMaterialHatchToSection = 0 Then
            orgApply = False
        Else
            orgApply = True
        End If

        myMatVisProps.ApplyMaterialHatchToSection = Not orgApply
        longstatus = myPart.SetMaterialVisualProperties(myMatVisProps, swThisConfiguration, Nothing)
        Call dump_material_visual_properties(myMatVisProps, myPart)
        myMatVisProps.ApplyMaterialHatchToSection = orgApply
        longstatus = myPart.SetMaterialVisualProperties(myMatVisProps, swThisConfiguration, Nothing)
        Call dump_material_visual_properties(myMatVisProps, myPart)
       

        ' Toggle the apply appearance flag
        If myMatVisProps.ApplyAppearance = 0 Then
            orgApply = False
        Else
            orgApply = True
        End If

        myMatVisProps.ApplyAppearance = Not orgApply
        longstatus = myPart.SetMaterialVisualProperties(myMatVisProps, swThisConfiguration, Nothing)
        Call dump_material_visual_properties(myMatVisProps, myPart)
        myMatVisProps.ApplyAppearance = orgApply
        longstatus = myPart.SetMaterialVisualProperties(myMatVisProps, swThisConfiguration, Nothing)
        Call dump_material_visual_properties(myMatVisProps, myPart)
    End If

End Sub

Private Sub dump_material_visual_properties(myMatVisProps As SldWorks.MaterialVisualPropertiesData, myPart As SldWorks.PartDoc)

   
    Dim configName As String, databaseName As String
    Dim propName As String
    Dim bRealView As Boolean
    Dim dScale As Double, dAngle As Double
    Dim bBlendColor As Boolean, bApplyColor As Boolean, bApplyHatch As Boolean, bApplyAppearance As Boolean
    configName = "default"
    propName = myPart.GetMaterialPropertyName2(configName, databaseName)
    Debug.Print ""
    Debug.Print "Config: """ & configName & """, Database: """ & databaseName & """, Material: """ & propName & """"

    If Not myMatVisProps Is Nothing Then
        bRealView = myMatVisProps.RealView
        dScale = myMatVisProps.Scale2
        dAngle = myMatVisProps.Angle
        bBlendColor = myMatVisProps.BlendColor
        bApplyColor = myMatVisProps.ApplyMaterialColorToPart
        bApplyHatch = myMatVisProps.ApplyMaterialHatchToSection
        bApplyAppearance = myMatVisProps.ApplyAppearance

       
        If bRealView = 0 Then
            Debug.Print "Advanced graphics - SolidWorks standard textures."
        Else
            Debug.Print "Advanced graphics - RealView textures."
        End If
        Debug.Print "   SolidWorks standard texture scale = " & dScale & ", Angle = " & dAngle
        If bBlendColor = 0 Then
            Debug.Print "   Do not blend part color with SolidWorks standard texture."
        Else
             ebug.Print "   Blend part color with SolidWorks standard texture."
        End If
 

        If bApplyColor = 0 Then
            Debug.Print "Do not apply material color to part."
        Else
            Debug.Print "Apply material color to part."
        End If

        If bApplyHatch = 0 Then
            Debug.Print "Do not apply material hatch to drawing section."
        Else
            Debug.Print "Apply material hatch to drawing section."

        End If
        If bApplyAppearance = 0 Then
            Debug.Print "Do not apply appearance."
        Else
            Debug.Print "Apply appearance."
        End If

    End If

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 and Set Material Visual 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) 2013 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.