Hide Table of Contents

Create Nonlinear Study and Apply Materials Example (C#)

This example shows how to create a nonlinear study and apply user-defined and SolidWorks materials to assembly components.


// Preconditions:

// 1. Add the SolidWorks Simulation as an add-in

//    (in SolidWorks, click Tools > Add-ins > SolidWorks Simulation).

// 2. Add the SolidWorks Simulation primary interop assembly as

//    a reference (in the IDE's Project Explorer, right-click

//    the project name, select Add Reference, click the Browse tab,

//    navigate to the install_dir\api\redist\CLR2 folder and

//    select SolidWorks.Interop.cosworks.dll).

// 3. Modify path to solidworks materials.sldmat if needed.

// 4. Run the macro.


// Postconditions:

// 1. Assembly opened.

// 2. A nonlinear study created.

// 3. User-defined material applied to the first assembly component,  

//    and SolidWorks material applied to the remaining components.

// 4. To verify, expand the Parts folder in the Simulation Study tree,

//    which is located beneath the FeatureManager design tree, and

//    observe the names of the materials applied to Holder-1 and Pipe-1.


// NOTE: Because the assembly document is used with

// a SolidWorks Simulation online tutorial, do not save

// any changes when closing the document.


using SolidWorks.Interop.sldworks;

using SolidWorks.Interop.swconst;

using SolidWorks.Interop.cosworks;

using System;

using System.Diagnostics;

using System.Windows.Forms;

namespace CreateNonlinearStudyApplyMaterials.csproj


    partial class SolidWorksMacro


        public void Main()


            CosmosWorks COSMOSWORKS = null;

            CwAddincallback COSMOSObject = default(CwAddincallback);

            CWModelDoc ActDoc = default(CWModelDoc);

            CWStudyManager StudyMngr = default(CWStudyManager);

            CWStudy Study = default(CWStudy);

            CWSolidManager SolidMgr = default(CWSolidManager);

            CWSolidComponent SolidComponent = default(CWSolidComponent);

            CWSolidBody SolidBody = default(CWSolidBody);

            string SName = null;

            int errors = 0;

            int warnings = 0;

            int errorCode = 0;

            int errCode = 0;

            int CompCount = 0;

            int j = 0;

            CWMaterial CWMat = null;

            int iApp = 0;

            bool bApp = false;


            // Get the SolidWorks Simulation object

            COSMOSObject = (CwAddincallback)swApp.GetAddInObject("SldWorks.Simulation");

            if (COSMOSObject == null) ErrorMsg(swApp, "COSMOSObject object not found.", true);

            COSMOSWORKS = (CosmosWorks)COSMOSObject.CosmosWorks;

            if (COSMOSWORKS == null) ErrorMsg(swApp, "COSMOSWORKS object not found.", true);


            // Open and get the active document

            swApp.OpenDoc6("C:\\Program Files\\SolidWorks Corp\\SolidWorks\\Simulation\\Examples\\Nonlinear\\nl_pipe_holder.sldasm", (int)swDocumentTypes_e.swDocASSEMBLY, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);

            ActDoc = (CWModelDoc)COSMOSWORKS.ActiveDoc;

            if (ActDoc == null) ErrorMsg(swApp, "No active document.", true);


            // Create new nonlinear study

            StudyMngr = (CWStudyManager)ActDoc.StudyManager;

            if (StudyMngr == null) ErrorMsg(swApp, "StudyMngr object not there.", true);

            Study = (CWStudy)StudyMngr.CreateNewStudy("Nonlinear", (int)swsAnalysisStudyType_e.swsAnalysisStudyTypeNonlinear, (int)swsMeshType_e.swsMeshTypeSolid, out errCode);

            if (Study == null) ErrorMsg(swApp, "Study not created.", true);


            // Get number of solid components

            SolidMgr = (CWSolidManager)Study.SolidManager;

            if (SolidMgr == null) ErrorMsg(swApp, "SolidMgr object not created.", true);

            CompCount = SolidMgr.ComponentCount;

            SolidComponent = SolidMgr.GetComponentAt(0, out errorCode);

            if (SolidComponent == null) ErrorMsg(swApp, "Solid component not created.", true);


            // Get name of solid component

            SName = SolidComponent.ComponentName;


            // Apply user-defined material to the first component in the tree

            SolidBody = (CWSolidBody)SolidComponent.GetSolidBodyAt(0, out errCode);

            if (errCode != 0) ErrorMsg(swApp, "No solid body.", true);

            if (SolidBody == null) ErrorMsg(swApp, "SolidBody not created.", true);

            CWMat = (CWMaterial)SolidBody.GetDefaultMaterial();

            CWMat.MaterialUnits = 0;

            if (CWMat == null) ErrorMsg(swApp, "No default material object.", true);

            CWMat.MaterialName = "Alloy Steel";

            CWMat.SetPropertyByName("EX", 210000000000.0, 0);

            CWMat.SetPropertyByName("NUXY", 0.28, 0);

            CWMat.SetPropertyByName("GXY", 79000000000.0, 0);

            CWMat.SetPropertyByName("DENS", 7700, 0);

            CWMat.SetPropertyByName("SIGXT", 723825600, 0);

            CWMat.SetPropertyByName("SIGYLD", 620422000, 0);

            CWMat.SetPropertyByName("ALPX", 1.3E-05, 0);

            CWMat.SetPropertyByName("KX", 50, 0);

            CWMat.SetPropertyByName("C", 460, 0);

            errCode = SolidBody.SetSolidBodyMaterial(CWMat);

            if (errCode != 0) ErrorMsg(swApp, "Failed to apply material.", true);


            //Apply SolidWorks library material to rest of components

            SolidBody = null;

            SolidComponent = null;

            for (j = 1; j <= (CompCount - 1); j++)


                SolidComponent = (CWSolidComponent)SolidMgr.GetComponentAt(j, out errorCode);

                SName = SolidComponent.ComponentName;

                SolidBody = (CWSolidBody)SolidComponent.GetSolidBodyAt(0, out errCode);

                if (errCode != 0) ErrorMsg(swApp, "No solid body", true);

                iApp = (int)SolidBody.SetLibraryMaterial("C:\\Program Files\\SolidWorks Corp\\SolidWorks\\lang\\english\\sldmaterials\\solidworks materials.sldmat", "Gray Cast Iron (SN)");

                bApp = System.Convert.ToBoolean(iApp);

                if (bApp == false) ErrorMsg(swApp, "No material applied.", true);

                SolidBody = null;



        // Error routine

        public void ErrorMsg(object SwApp, string Message, bool EndTest)



            MessageBox.Show("'*** WARNING - General");

            MessageBox.Show("'*** " + Message);


            if (EndTest)




        /// <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

Subject:   Feedback on Help Topics
Page:   Create Nonlinear Study and Apply Materials Example (C#)
*   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:


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

Web Help Content Version: API Help (English only) 2014 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.