Hide Table of Contents

Create Nonlinear Dynamic Plots Example (C#)

This example shows how to a create a plot of a specific solution step of a nonlinear dynamic 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. Activates Nonlinear1 study.
// 2. Meshes and analyzes the study.
// 3. Creates displacement, stress, and strain plots of solution step 72.
// 4. Gets the min/max values for the result plots.
// 5. 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 SolidWorks.Interop.cosworks;
using System.Runtime.InteropServices;
namespace CreatePlot_NL_Dynamic_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 = 11.26;   //nm
            const double URESMin = 0.0;     //nm
            const double VONMax = 0.00319;  //MPa
            const double VONMin = 8.95E-06; //MPa
 
            const double MaxStrn = 3.95E-08;
            const double MinStrn = 3.3E-11;
 
            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);
 
            //Get the Nonlinear1 study
            StudyMngr = ActDoc.StudyManager;
            if (StudyMngr == null)
                ErrorMsg(swApp, "No study manager object"true);
            StudyMngr.ActiveStudy = 1;
            Study = StudyMngr.GetStudy(1);
            if (Study == null)
                ErrorMsg(swApp, "No study object"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 resultant 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, "Failure to create plot"false);
 
            //Display plot of step 72
            errCode = CWCFf.SetPlotStepNumber(72);
            if (errCode != 0)
                ErrorMsg(swApp, "Plot is not activated for the given step"true);
 
            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);
 
            if ((Convert.ToDouble(Disp[1]) < 0.95 * URESMin) | (Convert.ToDouble(Disp[1]) > 1.05 * URESMin))
            {
                ErrorMsg(swApp, "URES minimum % error = " + (((Convert.ToDouble(Disp[1]) - URESMin) / URESMin) * 100), false);
            }
 
            if ((Convert.ToDouble(Disp[3]) < 0.95 * URESMax) | (Convert.ToDouble(Disp[3]) > 1.05 * URESMax))
            {
                ErrorMsg(swApp, "URES maximum % error = " + (((Convert.ToDouble(Disp[3]) - 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, "Failure to create plot"true);
 
            //Display plot of step 72
            errCode = CWCFf.SetPlotStepNumber(72);
            if (errCode != 0)
                ErrorMsg(swApp, "Plot is not activated for the given step"true);
 
            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);
 
            if ((Convert.ToDouble(Stress[1]) < 0.95 * VONMin) | (Convert.ToDouble(Stress[1]) > 1.05 * VONMin))
            {
                ErrorMsg(swApp, " Minimum von Mises stress % error = " + (((Convert.ToDouble(Stress[1]) - VONMin) / VONMin) * 100), false);
            }
 
            if ((Convert.ToDouble(Stress[3]) < 0.95 * VONMax) | (Convert.ToDouble(Stress[3]) > 1.05 * VONMax))
            {
                ErrorMsg(swApp, "Maximum von Mises stress % error = " + (((Convert.ToDouble(Stress[3]) - VONMax) / VONMax) * 100), false);
            }
 
            //Create equivalent elemental strain plot
            CWCFf = CWResult.CreatePlot((int)swsPlotResultTypes_e.swsResultStrain, (int)swsStaticResultElementalStrainComponentTypes_e.swsStaticElementalStrain_ESTRN, (int)swsUnit_e.swsUnitSI, trueout errCode);
            if (errCode != 0)
                ErrorMsg(swApp, "Failure to create plot"true);
 
            //Display plot of step 72
            errCode = CWCFf.SetPlotStepNumber(72);
            if (errCode != 0)
                ErrorMsg(swApp, "Plot is not activated for the given step"true);
 
            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);
 
            if ((Convert.ToDouble(Strn[1]) < 0.95 * MinStrn) | (Convert.ToDouble(Strn[1]) > 1.05 * MinStrn))
            {
                ErrorMsg(swApp, "Minimum strain % error = " + (((Convert.ToDouble(Strn[1]) - MinStrn) / MinStrn) * 100), false);
            }
 
            if ((Convert.ToDouble(Strn[3]) < 0.95 * MaxStrn) | (Convert.ToDouble(Strn[3]) > 1.05 * MaxStrn))
            {
                ErrorMsg(swApp, "Maximum strain % error = " + (((Convert.ToDouble(Strn[3]) - 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 Nonlinear Dynamic Plots 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) 2016 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.