Change Texture on Face in Specified Configuration Example (VBA)
This example shows how to change the texture on the selected face in
the specified configuration
'--------------------------------------------------
'
' Preconditions:
' (1)
Part or assembly is open.
' (2)
Specified configuration exists.
' (3)
Face is selected.
'
' Postconditions: Texture is applied to that face in that
configuration.
'
'----------------------------------------------------
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim face As SldWorks.face2
Dim texture As SldWorks.texture
Dim boolstatus As Boolean
Dim configName As String
Dim bAll As Boolean
Dim configVar As Variant
Dim namStr As String
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swSelMgr = swModel.SelectionManager
Set swModelDocExt = swModel.Extension
configName = "Default"
bAll = True
Set face = swSelMgr.GetSelectedObject5(1)
Dim configArr(0 To 0) As String
configArr(0) = configName
configVar = configArr
'Get existing texture on this face
Set texture = face.GetTexture(configName)
If Not texture Is Nothing Then
Debug.Print
"MATERIAL -> " & texture.MaterialName
Debug.Print
"SCALE -> " & texture.scaleFactor
Debug.Print
"Angle -> " & texture.angle
'Change
texture on this face
texture.angle = 340
texture.scaleFactor = 25
texture.MaterialName = "<SystemTexture>\images\textures\plastic\brushed\bblue.jpg"
boolstatus
= face.SetTexture(configName,
texture)
Else
'If
no texture exists on this face, then apply this texture to it
namStr
= "<SystemTexture>\images\textures\pattern\checker2.jpg"
Set
texture = swModelDocExt.CreateTexture(namStr,
5, 45, False)
boolstatus
= face.SetTexture(configName,
texture)
End If
namStr = texture.MaterialName
Debug.Print texture.GetSystemTextureName(namStr,
True)
End Sub