Good day everyone,
I have a macro that toggles face transparency on and off. I know the right-click option exists, but it doesn’t allow me to record it, and I used transparency frequently, so I want to make it accessible easily. The macro works fine for making faces transparent, but when I try to revert them back to opaque, a new color appears instead of the original body color.
Does anyone know how to fix this so that the face returns to its original body color? Thanks in advance!
Option Explicit
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim swSelMgr As SelectionMgr
Dim swFace As Face2
Dim vMatProps As Variant
Dim currentTransparency As Double
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If swModel Is Nothing Then
MsgBox "No document open."
Exit Sub
End If
Set swSelMgr = swModel.SelectionManager
If swSelMgr.GetSelectedObjectType3(1, -1) <> swSelectType_e.swSelFACES Then
MsgBox "Please select a face."
Exit Sub
End If
Set swFace = swSelMgr.GetSelectedObject6(1, -1)
' Get current material properties
vMatProps = swFace.MaterialPropertyValues
If IsEmpty(vMatProps) Then
' Default material properties if none set
ReDim vMatProps(0 To 8)
vMatProps(0) = 1# ' Red
vMatProps(1) = 1# ' Green
vMatProps(2) = 1# ' Blue
vMatProps(3) = 0# ' Ambient
vMatProps(4) = 1# ' Diffuse
vMatProps(5) = 0# ' Specular
vMatProps(6) = 0# ' Shininess
vMatProps(7) = 0# ' Transparency (0 = opaque)
vMatProps(8) = 0# ' Emission
End If
' Get current transparency value
currentTransparency = vMatProps(7)
' Toggle transparency (if 0, make it transparent, if > 0, make it opaque)
If currentTransparency = 0 Then
vMatProps(7) = 0.7 ' Set transparency to 70% (adjust as needed)
Else
vMatProps(7) = 0 ' Set transparency to 0 (fully opaque)
End If
' Apply the updated material properties
swFace.MaterialPropertyValues = vMatProps
swModel.GraphicsRedraw2
End Sub
