Hide Table of Contents

Add and Delete Materials from Specific Display States (VBA)

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 State 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.
'
'-----------------------------------------------
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swConfig As SldWorks.Configuration
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swEntity As SldWorks.Entity
Dim swSelMgr As SldWorks.SelectionMgr
Dim swRenderMaterial As SldWorks.RenderMaterial
Dim displayStateNames As Variant
Dim status As Boolean
Dim modelName As String
Dim materialName As String
Dim errors As Long
Dim warnings As Long
Dim nbrDisplayStates As Long
Dim i As Long
Dim k As Long
Dim nbrMaterials As Long
Dim materialID1 As Long
Dim materialID2 As Long
Dim materialID1_ToDelete(0) As Long
Dim materialID2_ToDelete(0) As Long
Sub main()
modelName = "C:\Program Files\SolidWorks Corp\SolidWorks (2)\samples\tutorial\dimxpert\bracket_auto_manual.sldprt"
Set swApp = Application.SldWorks
Set swModel = swApp.OpenDoc6(modelName, swDocPART, swOpenDocOptions_Silent, "", errors, warnings)
Set swModelDocExt = swModel.Extension
' Get active configuration and create a new display 
' state for this configuration
Set swConfig = swModel.GetActiveConfiguration
status = swConfig.CreateDisplayState("Display State 2")
swModel.ForceRebuild3 True
' Get active configuration and create another new 
' display state for this configuration
Set swConfig = swModel.GetActiveConfiguration
status = swConfig.CreateDisplayState("Display State 3")
swModel.ForceRebuild3 True
' Create render material
materialName = "C:\Program Files\SolidWorks Corp\SolidWorks\data\graphics\materials\metal\steel\stainless steel treadplate.p2m"
Set 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)
Set swSelMgr = swModel.SelectionManager
Set 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, 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 display states of the active configuration
' (click the ConfigurationManager tab, and switch
' display states at bottom of the Configuration pane)
' to ensure that the specified material was applied to all
' display states
' Continue running the macro after your examination
Stop
' Delete the material from the part
materialID1_ToDelete(0) = materialID1
materialID2_ToDelete(0) = materialID2
swModelDocExt.DeleteDisplayStateSpecificRenderMaterial (materialID1_ToDelete), (materialID2_ToDelete)
swModel.ForceRebuild3 True
' Examine the display states of the active configuration
' (click the ConfigurationManager tab, and switch
' display states at bottom of the Configuration pane)
' to ensure that the specified material was deleted from all
' display states
' Continue running the macro after your examination
Stop
' Close the part without saving changes
modelName = swModel.GetTitle
swApp.QuitDoc modelName
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:   Add and Delete Materials from Specific Display States (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) 2011 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.