Hide Table of Contents

Create Plots for a Static Study Example (C#)

This example shows how to create plots of displacement, stress, and strain results in a static study.

//----------------------------------------------------------------------------  
// 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, click Add Reference, click the Browse tab,
//    navigate to install_dir\api\redist\CLR2
//    click SolidWorks.Interop.cosworks.dll, and click OK).
// 3. Open install_dir\Simulation\Examples\tutor1.sldprt.  
// 
// Postconditions:  
// 1. Deletes all the default static study plots from the model document.
// 2. Activates the Ready static study.

// 3. Meshes and runs the study.
// 4. Creates displacement, stress, and strain plots.
// 5. Gets the min/max values for the result plots.
// 6. Click OK to close any error dialog boxes.
//
// NOTE: Because the model is used elsewhere, do not save changes. 
//---------------------------------------------------------------------------- 
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using SolidWorks.Interop.cosworks;
namespace CreatePlot_Static_CSharp.csproj
{
    partial class SolidWorksMacro
    {
 
        public void Main()
        {
            
            CosmosWorks COSMOSWORKS = default(CosmosWorks);
            CwAddincallback CWAddinCallBack = default(CwAddincallback);
            CWModelDoc ActDoc = default(CWModelDoc);
            CWStudyManager StudyMngr = default(CWStudyManager);
            CWStudy Study = default(CWStudy);
            CWMesh CWMesh = default(CWMesh);
            CWResults CWResult = default(CWResults);
            CWPlot CWCFf = default(CWPlot);
 
            int errCode = 0;
            double el = 0;
            double tl = 0;
           
            object[] Disp = null;
            object[] Stress = null;
            object[] Strn = null;
 
            const double URESMax = 1004928; //nm
            const double URESMin = 0.0;     //nm
            const double VONMax = 284;      //MPa
            const double VONMin = 0.797;    //MPa
            const double MaxStrn = 0.00352;
            const double MinStrn = 2.94E-06;

 
            CWAddinCallBack = (CwAddincallback)swApp.GetAddInObject("CosmosWorks.CosmosWorks");
            if (CWAddinCallBack == null)
                ErrorMsg(swApp, "No CWAddinCallBack object"true);
            COSMOSWORKS = CWAddinCallBack.CosmosWorks;
            if (COSMOSWORKS == null)
                ErrorMsg(swApp, "No CosmosWorks object"true);
 
            //Get active document
            ActDoc = COSMOSWORKS.ActiveDoc;
            if (ActDoc == null)
                ErrorMsg(swApp, "No active document"true);
 

            //Delete all default static study plots from this model
            ActDoc.DeleteAllDefaultStaticStudyPlots();


            //Get Ready study
            StudyMngr = ActDoc.StudyManager;
            if (StudyMngr == null)
                ErrorMsg(swApp, "No study manager object"true);
            StudyMngr.ActiveStudy = 0;
            Study = StudyMngr.GetStudy(0);
            if (Study == null)
                ErrorMsg(swApp, "Study not created"true);
 
            //Mesh
            CWMesh = Study.Mesh;
            if (CWMesh == null)
                ErrorMsg(swApp, "No mesh object"false);
            CWMesh.Quality = 0;
            CWMesh.GetDefaultElementSizeAndTolerance(0, out el, out tl);
            errCode = Study.CreateMesh(0, el, tl);
            if (errCode != 0)
                ErrorMsg(swApp, "Mesh failed"true);
 
            //Run analysis
            errCode = Study.RunAnalysis();
            if (errCode != 0)
                ErrorMsg(swApp, "Analysis failed"true);
 
            //Get results
            CWResult = Study.Results;
            if (CWResult == null)
                ErrorMsg(swApp, "No result object"false);
 
            //Create displacement plot
            CWCFf = CWResult.CreatePlot((int)swsPlotResultTypes_e.swsResultDisplacementOrAmplitude, (int)swsStaticResultDisplacementComponentTypes_e.swsStaticDisplacement_URES, (int)swsUnit_e.swsUnitSI, falseout errCode);
            if (CWCFf == null)
                ErrorMsg(swApp, "Failed to create plot"false);
 
            //Activate plot
            errCode = CWCFf.ActivatePlot();
            if (errCode != 0)
                ErrorMsg(swApp, "Plot is not activated"true);
 
            //Get min/max resultant displacements from plot
            Disp = (object[])CWCFf.GetMinMaxResultValues(out errCode);
            if (errCode != 0)
                ErrorMsg(swApp, "No displacement results"true);
 
            double Disp1 = Convert.ToDouble(Disp[1]);
            double Disp3 = Convert.ToDouble(Disp[3]);
 
            if ((Disp1 < 0.95 * URESMin) | (Disp1 > 1.05 * URESMin))
            {
                ErrorMsg(swApp, "URES minimum % error = " + (((Disp1 - URESMin) / URESMin) * 100), false);
            }
 
            if ((Disp3 < 0.95 * URESMax) | (Disp3 > 1.05 * URESMax))
            {
                ErrorMsg(swApp, "URES maximum % error = " + (((Disp3 - URESMax) / URESMax) * 100), false);
            }
 
            //Create stress plot
            CWCFf = CWResult.CreatePlot((int)swsPlotResultTypes_e.swsResultStress, (int)swsStaticResultNodalStressComponentTypes_e.swsStaticNodalStress_VON, (int)swsUnit_e.swsUnitSI, falseout errCode);
            if (errCode != 0)
                ErrorMsg(swApp, "Failed to create plot"true);
 
            //Activate plot
            errCode = CWCFf.ActivatePlot();
            if (errCode != 0)
                ErrorMsg(swApp, "Plot is not activated"true);
 
            //Get min/max von Mises stresses from plot
            Stress = (object[])CWCFf.GetMinMaxResultValues(out errCode);
            if (errCode != 0)
                ErrorMsg(swApp, "No stress results"true);
 
            double Stress1 = Convert.ToDouble(Stress[1]);
            double Stress3 = Convert.ToDouble(Stress[3]);
 
            if ((Stress1 < 0.95 * VONMin) | (Stress1 > 1.05 * VONMin))
            {
                ErrorMsg(swApp, " Minimum von Mises stress % error = " + (((Stress1 - VONMin) / VONMin) * 100), false);
            }
 
            if ((Stress3 < 0.95 * VONMax) | (Stress3 > 1.05 * VONMax))
            {
                ErrorMsg(swApp, "Maximum von Mises stress % error = " + (((Stress3 - VONMax) / VONMax) * 100), false);
            }
 
            //Create strain plot
            CWCFf = CWResult.CreatePlot((int)swsPlotResultTypes_e.swsResultStrain, (int)swsStaticResultElementalStrainComponentTypes_e.swsStaticElementalStrain_ESTRN, (int)swsUnit_e.swsUnitEnglish, trueout errCode);
            if (errCode != 0)
                ErrorMsg(swApp, "Failed to create plot"true);
 
            //Activate plot
            errCode = CWCFf.ActivatePlot();
            if (errCode != 0)
                ErrorMsg(swApp, "Plot is not activated"true);
 
            //Get min/max strains from plot
            Strn = (object[])CWCFf.GetMinMaxResultValues(out errCode);
            if (errCode != 0)
                ErrorMsg(swApp, "No strain results"true);
 
            double Strn1 = Convert.ToDouble(Strn[1]);
            double Strn3 = Convert.ToDouble(Strn[3]);
 
            if ((Strn1 < 0.95 * MinStrn) | (Strn1 > 1.05 * MinStrn))
            {
                ErrorMsg(swApp, "Minimum strain % error = " + (((Strn1 - MinStrn) / MinStrn) * 100), false);
            }
 
            if ((Strn3 < 0.95 * MaxStrn) | (Strn3 > 1.05 * MaxStrn))
            {
                ErrorMsg(swApp, "Maximum strain % error = " + (((Strn3 - MaxStrn) / MaxStrn) * 100), false);
            }
 
        }
 
        public void ErrorMsg(SldWorks SwApp, string Message, bool EndTest)
        {
            SwApp.SendMsgToUser2(Message, 0, 0);
            SwApp.RecordLine("'*** WARNING - General");
            SwApp.RecordLine("'*** " + Message);
            SwApp.RecordLine("");
        }
 
 
 
        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:   Create Plots for a Static Study 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) 2015 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.