Hide Table of Contents

Set Material Example (C#)

This example shows how to get the names of the material schema, material databases, and bodies in a part document. This example also shows how to apply a SOLIDWORKS Material to all of the bodies in a part document.

//----------------------------------------------------
// Preconditions:
// 1. Verify that the specified document exists.
// 2. Open the Immediate window.
//
// Postconditions:
// 1. Opens the specified part document.
// 2. Applies the material ABS PC (polycarbonate plastic)
//    from the SOLIDWORKS Material database to all
//    bodies in the part.
// 3. To verify, examine:
//    * DisplayManager tab
//    * graphics area
//    * Immediate window
//
// NOTE: Because the part document is used elsewhere,
// do not save changes.
//----------------------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Diagnostics;
namespace GetMaterialBodyCSharp.csproj
{
    public partial class SolidWorksMacro
    {
        public void Main()
        {
            ModelDoc2 swModel = default(ModelDoc2);
            PartDoc swPart = default(PartDoc);
            Body2 swBody = default(Body2);
            int errors = 0;
            int warnings = 0;
            object[] vMatDBarr = null;
            object[] vMatDB = null;
            object[] Bodies = null;
            int BodyMaterialError = 0;
            string sMatName = "";
            string sMatDB = "";
            int i = 0;
            int j = 0;

            // Open the document
            swModel = (ModelDoc2)swApp.OpenDoc6("C:\\Users\\Public\\Documents\\SOLIDWORKS\\SOLIDWORKS 2018\\samples\\tutorial\\multibody\\multi_inter.sldprt", (int)swDocumentTypes_e.swDocPART,
(int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);
            swPart = (PartDoc)swModel;
            // Get the material schema and names
            // of available materials databases
            vMatDBarr = (object[])swApp.GetMaterialDatabases();
            Debug.Print("Material schema pathname = " + swApp.GetMaterialSchemaPathName());
            for (i=0; i < vMatDBarr.Length; i++)
            {
                Debug.Print(" Material database: " + vMatDB);
            }
            Debug.Print("");
            Bodies = (object[])swPart.GetBodies2((int)swBodyType_e.swAllBodies, false);
            for (j = 0; j < Bodies.Length; j++)
            {
                swBody = (Body2)Bodies[j];
                // Get the name of the body
                Debug.Print(swBody.Name);


                swBody.Select2(false, null);


                // Set the SOLIDWORKS material for that body
                BodyMaterialError = swBody.SetMaterialProperty("Default", "solidworks
materials.sldmat", "ABS PC");
                // Comment out the previous statement and uncomment the following statement to use
custom material
                //BodyMaterialError = swBody.SetMaterialProperty("Default", "custom materials.sldmat",
"Custom Plastic");


                // Get the names of the body's material and the
                // database to which it belongs
                sMatName = swBody.GetMaterialPropertyName("", out sMatDB);
                if (string.IsNullOrEmpty(sMatName))
                {
                    Debug.Print("Body " + j + "'s material name: No material applied");
                }
                else
                {
                    Debug.Print("Body " + j + "'s material name: " + sMatName);
                    Debug.Print("Body " + j + "'s material database: " + sMatDB);
                    Debug.Print(" ");
                }
            }
        }
        /// <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:   Set Material Example (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) 2019 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.