Hide Table of Contents

Add and Delete Materials from Specific Display States (C#)

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.
//---------------------------------------------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace AddDeleteDisplayStateSpecificMaterialModelDocExtCSharp.csproj
{
    
partial class SolidWorksMacro
    {

        
public void Main()
        {

            ModelDoc2 swModel =
default(ModelDoc2);
            Configuration swConfig =
default(Configuration);
            ModelDocExtension swModelDocExt =
default(ModelDocExtension);
            Entity swEntity =
default(Entity);
            SelectionMgr swSelMgr =
default(SelectionMgr);
            RenderMaterial swRenderMaterial =
default(RenderMaterial);
            
object[] displayStateNames = null;
            
string displayStateName = "";
            
bool status = false;
            
string modelName = null;
            
string materialName = null;
            
int errors = 0;
            
int warnings = 0;
            
int nbrDisplayStates = 0;
            
int i = 0;
            
int k = 0;
            
int nbrMaterials = 0;
            
int materialID1 = 0;
            
int materialID2 = 0;
            
int[] materialID1_ToDelete = new int[1];
            
int[] materialID2_ToDelete = new int[1];

            modelName =
"C:\\Program Files\\SolidWorks Corp\\SolidWorks\\samples\\tutorial\\dimxpert\\bracket_auto_manual.sldprt";
            swModel = (ModelDoc2)swApp.OpenDoc6(modelName, (
int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);
            swModelDocExt = (ModelDocExtension)swModel.Extension;

            
// Get active configuration and create a new display
            // state for this configuration
            swConfig = (Configuration)swModel.GetActiveConfiguration();
            status = swConfig.CreateDisplayState(
"Display State 2");
            swModel.ForceRebuild3(
true);

            
// Get active configuration and create another
            // new display state for this configuration
            swConfig = (Configuration)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";
            swRenderMaterial = (RenderMaterial)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, null, 0);
            swSelMgr = (SelectionMgr)swModel.SelectionManager;
            swEntity = (Entity)swSelMgr.GetSelectedObject6(1, -1);
            status = swRenderMaterial.AddEntity(swEntity);

            
// Get the names of display states
            displayStateNames = (object[])swConfig.GetDisplayStates();
            nbrDisplayStates = swConfig.GetDisplayStatesCount();
            
Debug.Print("This configuration's display states:");
            
for (i = 0; i <= (nbrDisplayStates - 1); i++)
            {
                displayStateName = (
string)displayStateNames[i];
                
Debug.Print("  Display state name = " + displayStateName.ToString());
            }

            
// Add material to all of the display states
            status = swModelDocExt.AddDisplayStateSpecificRenderMaterial(swRenderMaterial, (int)swDisplayStateOpts_e.swAllDisplayState, (displayStateNames), out materialID1, out materialID2);


            
// Get the material's IDs and their names
            swRenderMaterial.GetMaterialIds(out materialID1, out materialID2);
            
Debug.Print("    Material IDs:");
            
Debug.Print("      materialID1 = " + materialID1.ToString());
            
Debug.Print("      materialID2 = " + materialID2.ToString());

            nbrMaterials = swModelDocExt.GetRenderMaterialsCount();
            
Debug.Print("    Number of materials: " + nbrMaterials.ToString());

            
for (k = 0; k <= (nbrMaterials - 1); k++)
            {
                
Debug.Print("      Name of material " + (k + 1) + ": " + swModel.MaterialIdName);
            }
 

            double xcoord = 0;
            
double ycoord = 0;
            
double zcoord = 0;
            swRenderMaterial.GetCenterPoint2(
out xcoord, out ycoord, out zcoord);
            
Debug.Print("");
            
Debug.Print("Texture-based appearance data:");
            
Debug.Print("X coordinate of center point: " + xcoord);
            
Debug.Print("Y coordinate of center point: " + ycoord);
            
Debug.Print("Z coordinate of center point: " + zcoord);

            swRenderMaterial.GetUDirection2(
out xcoord, out ycoord, out zcoord);
            
Debug.Print("X coordinate of U direction: " + xcoord);
            
Debug.Print("Y coordinate of U direction: " + ycoord);
            
Debug.Print("Z coordinate of U direction: " + zcoord);

            swRenderMaterial.GetVDirection2(
out xcoord, out ycoord, out zcoord);
            
Debug.Print("X coordinate of V direction: " + xcoord);
            
Debug.Print("Y coordinate of V direction: " + ycoord);
            
Debug.Print("Z coordinate of V direction: " + zcoord);
            
Debug.Print("");


            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
            System.Diagnostics.Debugger.Break();


            
// 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
            System.Diagnostics.Debugger.Break();

            
// Close the part without saving changes
            modelName = swModel.GetTitle();
            swApp.QuitDoc(modelName);
        }

        
/// <summary>
        /// The SldWorks swApp variable is pre-assigned for you.
        /// </summary>
        public
SldWorks swApp;


    }
}



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 (C#)
*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) 2013 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.