Get and Set Material Visual Properties Example (VBA)
This method shows how to get and set the material properties of a part.
'-----------------------------------------------------
'
' Preconditions: A part document is open.
'
' Postconditions: The material of the default configuration
is:
' *
Material: "Beech"
' *
Advanced graphics - SolidWorks standard textures.
' *
SolidWorks standard texture scale = 7.15699315071106, Angle = 1
' *
Blend part color with SolidWorks standard texture.
' *
Apply material color to part.
' *
Apply material hatch to drawing section.
'
'------------------------------------------------------
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
propName As String, newPropName As String
Dim
orgBlend As Boolean, orgApply As Boolean
Dim
orgAngle As Double, newAngle As Double
Dim
orgScale As Double, newScale As Double
Dim
boolstatus As Boolean
Dim
longstatus As Long, longwarnings 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
myMatVisProps.AdvancedGraphics = False
longstatus
= myPart.SetMaterialVisualProperties(myMatVisProps,
swThisConfiguration, Nothing)
Call
dump_material_visual_properties(myMatVisProps, myPart)
'
Set the material visual properties to be RealView
myMatVisProps.AdvancedGraphics = True
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 set 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 set 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)
End
If
End Sub
Private Sub dump_material_visual_properties(myMatVisProps
As SldWorks.MaterialVisualPropertiesData, myPart As SldWorks.PartDoc)
Dim
boolstatus As Boolean
Dim
longstatus As Long, longwarnings As Long
Dim
configName As String, databaseName As String
Dim
propName As String, newPropName As String
Dim
bAdvanced As Boolean, bRealView As Boolean
Dim
dScale As Double, dAngle As Double
Dim
bBlendColor As Boolean, bApplyColor As Boolean, bApplyHatch 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
bAdvanced
= myMatVisProps.AdvancedGraphics
bRealView
= myMatVisProps.RealView
dScale
= myMatVisProps.Scale2
dAngle
= myMatVisProps.Angle
bBlendColor
= myMatVisProps.BlendColor
bApplyColor
= myMatVisProps.ApplyMaterialColorToPart
bApplyHatch
= myMatVisProps.ApplyMaterialHatchToSection
If
bAdvanced = 0 Then
Debug.Print
"No advanced graphics"
Else
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
Debug.Print
" Blend
part color with SolidWorks standard texture."
End
If
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
End
If
End Sub