Add and Delete Materials from Specific Display States (VB.NET)
This example shows how to add material to and delete material from specific
display states.
'-----------------------------------------------
'
' Preconditions:
' 1. Specified model exists.
' 2. Specified material exists.
' 3. Open an Immediate window.
'
'
' Postconditions:
' 1. Display State 2 and Display 3 are created
' for the active configuration.
' 2. Specified material is applied to all display states
' of the active configuration.
' 3. Specified material is deleted from all display states
' of the active configuration.
'
'-----------------------------------------------
Imports
SolidWorks.Interop.sldworks
Imports
SolidWorks.Interop.swconst
Imports System
Imports System.Diagnostics
Imports
System.Runtime.InteropServices
Partial
Class SolidWorksMacro
Public
Sub Main()
Dim swModel
As ModelDoc2
Dim swConfig
As Configuration
Dim swModelDocExt
As ModelDocExtension
Dim swEntity
As Entity
Dim swSelMgr
As SelectionMgr
Dim
swRenderMaterial As
RenderMaterial
Dim
displayStateNames As
Object
Dim
status As
Boolean
Dim
modelName As
String
Dim
materialName As
String
Dim
errors As
Integer
Dim
warnings As
Integer
Dim
nbrDisplayStates As
Integer
Dim
i As
Integer
Dim
k As
Integer
Dim
nbrMaterials As
Integer
Dim
materialID1 As
Integer
Dim
materialID2 As
Integer
Dim
materialID1_ToDelete(0) As
Integer
Dim
materialID2_ToDelete(0) As
Integer
modelName =
"C:\Program Files\SolidWorks Corp\SolidWorks (2)\samples\tutorial\dimxpert\bracket_auto_manual.sldprt"
swModel = swApp.OpenDoc6(modelName,
swDocumentTypes_e.swDocPART, swOpenDocOptions_e.swOpenDocOptions_Silent,
"", errors, warnings)
swModelDocExt = swModel.Extension
' Get active configuration and create a new
' display state
for this configuration
swConfig = swModel.GetActiveConfiguration
status = swConfig.CreateDisplayState("Display
State 2")
swModel.ForceRebuild3(True)
' Get active configuration and create another
' new display
state for this configuration
swConfig = swModel.GetActiveConfiguration
status = swConfig.CreateDisplayState("Display
State 3")
swModel.ForceRebuild3(True)
' Create render material
materialName =
"C:\Program Files\SolidWorks Corp\SolidWorks
(2)\data\graphics\materials\metal\steel\stainless steel treadplate.p2m"
swRenderMaterial = swModelDocExt.CreateRenderMaterial(materialName)
' Select a face and add the material to that
face
status = swModelDocExt.SelectByID2("",
"FACE",
0.07151920610502, 0.0952597996959, 0.009524999999996,
False, 0, Nothing,
0)
swSelMgr = swModel.SelectionManager
swEntity = swSelMgr.GetSelectedObject6(1, -1)
status = swRenderMaterial.AddEntity(swEntity)
' Get the names of display states
displayStateNames = swConfig.GetDisplayStates
nbrDisplayStates = swConfig.GetDisplayStatesCount
Debug.Print("This configuration's display
states =")
For i = 0
To (nbrDisplayStates - 1)
Debug.Print(" Display state name = "
& displayStateNames(i))
Next i
' Add material to all of the display states
status = swModelDocExt.AddDisplayStateSpecificRenderMaterial(swRenderMaterial,
swDisplayStateOpts_e.swAllDisplayState, displayStateNames, materialID1,
materialID2)
' Get the material's IDs and their names
swRenderMaterial.GetMaterialIds(materialID1,
materialID2)
Debug.Print(" Material IDs:")
Debug.Print(" materialID1 = "
& materialID1)
Debug.Print(" materialID2 = "
& materialID2)
nbrMaterials = swModelDocExt.GetRenderMaterialsCount
Debug.Print(" Number of materials: "
& nbrMaterials)
For k = 0
To (nbrMaterials - 1)
Debug.Print(" Name of material "
& (k + 1) & ": " &
swModel.MaterialIdName)
Next k
swModel.ClearSelection2(True)
swModel.ForceRebuild3(True)
' Examine the active configuration to ensure
that
' the specified
material was applied
' Continue running
the macro after your examination
Stop
' Delete the
material from the part
materialID1_ToDelete(0) = materialID1
materialID2_ToDelete(0) = materialID2
status = swModelDocExt.DeleteDisplayStateSpecificRenderMaterial((materialID1_ToDelete),
(materialID2_ToDelete))
swModel.ForceRebuild3(True)
' Examine the active configuration to ensure
that
' the specified
material was deleted
' Continue running
the macro after your examination
Stop
' Close the part
without saving changes
modelName = swModel.GetTitle
swApp.QuitDoc(modelName)
End
Sub
'''
<summary>
''' The SldWorks swApp
variable is pre-assigned for you.
'''
</summary>
Public
swApp As SldWorks
End
Class