Hide Table of Contents

Access Load Case Manager Example (C#)

This example shows how to access the Load Case Manager to define load cases and load case combinations.

// Preconditions:
// 1. Add the SOLIDWORKS Simulation as an add-in (in SOLIDWORKS, click 
//    Tools > Add-ins > SOLIDWORKS Simulation > OK).

// 2. Add the SOLIDWORKS Simulation primary interop assembly as a reference
//    (in the IDE, click Project > Add Reference > .NET > 
//    SolidWorks.Interop.cosworks > OK).

// 3. Ensure that the specified model exists.
// Postconditions:
// 1. Gets the Ready study.
// 2. Opens the Load Case Manager.
// 3. Adds primary load cases and load case combinations.
// 4. Analyzes Ready using all primary load cases and load case combinations.
// 5. Creates a results displacement plot.

// 6. Click each primary load case and load case combination in the
//    Results View tab of the Load Case Manager. Inspect the result data
//    in the graphics area after each click.
// 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 LoadCaseManager_CSharp.csproj
    partial class SolidWorksMacro
        ModelDoc2 Part;
        CosmosWorks COSMOSWORKS;
        CwAddincallback CWAddinCallBack;
        CWModelDoc ActDoc;
        CWStudyManager StudyMngr;
        CWStudy Study;
        CWLoadCaseManager LoadCaseManager;
        CWResults CWResult;
        CWPlot CWCFf;
        int errCode;
        int longstatus;
        int longwarnings;
        bool bSuccess;
        object PrimaryCases;
        object SecondaryCases;
        object[] varBOOLVals = new object[1];
        object[] varLoadVals = new object[1];
        object[] varRetBOOLVals = new object[1];
        object[] varRetLoadVals = new object[1];
        string strCombination;
        string retStrCombination;
        public void Main()
            Part = swApp.OpenDoc6("C:\\Users\\Public\\Documents\\SOLIDWORKS\\SOLIDWORKS 2017\\Simulation Examples\\Tutor1.sldprt", (int)swDocumentTypes_e.swDocPART, 1, ""ref longstatus, ref longwarnings);
            if (Part == null)
                ErrorMsg(swApp, "Failed to open Tutor1.sldprt");
            CWAddinCallBack = (CwAddincallback)swApp.GetAddInObject("CosmosWorks.CosmosWorks");
            if (CWAddinCallBack == null)
                ErrorMsg(swApp, "Simulation add-in not loaded");
            COSMOSWORKS = CWAddinCallBack.CosmosWorks;
            if (COSMOSWORKS == null)
                ErrorMsg(swApp, "No COSMOSWORKS object");
            ActDoc = COSMOSWORKS.ActiveDoc;
            if (ActDoc == null)
                ErrorMsg(swApp, "No active document");
            StudyMngr = ActDoc.StudyManager;
            if (StudyMngr == null)
                ErrorMsg(swApp, "No CWStudyManager object");
            Study = StudyMngr.GetStudy(0);
            StudyMngr.ActiveStudy = 0;
            if (Study == null)
                ErrorMsg(swApp, "No study");
            LoadCaseManager = Study.LoadCaseManager;
            if (LoadCaseManager == null)
                ErrorMsg(swApp, "No Load Case Manager");
            bSuccess = LoadCaseManager.OpenLoadCaseManager();
            bSuccess = LoadCaseManager.AddNewPrimaryLoadCase("My Load Case1");
            if (bSuccess == false)
                ErrorMsg(swApp, "Primary load case not created");
            bSuccess = LoadCaseManager.AddNewPrimaryLoadCase("My Load Case2");
            if (bSuccess == false)
                ErrorMsg(swApp, "Primary load case not created");
            bSuccess = LoadCaseManager.AddNewPrimaryLoadCase("My Load Case3");
            if (bSuccess == false)
                ErrorMsg(swApp, "Primary load case not created");
            bSuccess = LoadCaseManager.DeletePrimaryLoadCase("My Load Case3");
            if (bSuccess == false)
                ErrorMsg(swApp, "Primary load case not deleted");
            bSuccess = LoadCaseManager.SuppressOrUnSuppressPrimaryLoadCase("My Load Case2"true);
            if (bSuccess == false)
                ErrorMsg(swApp, "Primary load case not suppressed");
            bSuccess = LoadCaseManager.SuppressOrUnSuppressPrimaryLoadCase("My Load Case2"false);
            if (bSuccess == false)
                ErrorMsg(swApp, "Primary load case not unsuppressed");
            bSuccess = LoadCaseManager.RenamePrimaryLoadCase("My Load Case2""Live Load2");
            if (bSuccess == false)
                ErrorMsg(swApp, "Primary load case not renamed");
            varBOOLVals[0] = false;
            varLoadVals[0] = 3.5;
            errCode = LoadCaseManager.SetLoadDataForPrimaryLoadCase("My Load Case1""Pressure-1", (varBOOLVals), (varLoadVals));
            if (errCode > 0)
                ErrorMsg(swApp, "Load data of Pressure-1 not applied to My Load Case1");
            varBOOLVals[0] = true;
            varLoadVals[0] = -1;
            errCode = LoadCaseManager.SetLoadDataForPrimaryLoadCase("My Load Case1""Restraint-1", (varBOOLVals), (varLoadVals));
            if (errCode > 0)
                ErrorMsg(swApp, "Load data of Restraint-1 not applied to My Load Case1");
            varBOOLVals[0] = false;
            varLoadVals[0] = -1;
            errCode = LoadCaseManager.SetLoadDataForPrimaryLoadCase("My Load Case1""Restraint-1", (varBOOLVals), (varLoadVals));
            if (errCode > 0)
                ErrorMsg(swApp, "Load data of Restraint-1 not applied to My Load Case1");
            varBOOLVals[0] = false;
            varLoadVals[0] = 6.5;
            errCode = LoadCaseManager.SetLoadDataForPrimaryLoadCase("Live Load2""Pressure-1", (varBOOLVals), (varLoadVals));
            if (errCode > 0)
                ErrorMsg(swApp, "Load data of Pressure-1 not applied to Live Load2");
            varRetLoadVals = (object[])LoadCaseManager.GetLoadDataForPrimaryLoadCase("Live Load2""Pressure-1"out varRetBOOLVals[0]);
            strCombination = "\"My Load Case1\" + \"Live Load2\"";
            errCode = LoadCaseManager.AddNewLoadCaseCombination("Load Case Combination1", strCombination);
            if (errCode > 0)
                ErrorMsg(swApp, "Load Case Combination1 not added");
            strCombination = "2*(sin(30)*\"My Load Case1\"*(1 + 1) + (4 - 2)*\"Live Load2\"*cos(60))";
            errCode = LoadCaseManager.AddNewLoadCaseCombination("Load Case Combination2", strCombination);
            if (errCode > 0)
                ErrorMsg(swApp, "Load Case Combination2 not added");
            strCombination = "\"My Load Case1\" + \"Live Load2\"";
            errCode = LoadCaseManager.AddNewLoadCaseCombination("Load Case Combination3", strCombination);
            if (errCode > 0)
                ErrorMsg(swApp, "Load Case Combination3 not added");
            strCombination = "\"My Load Case1\" + \"Live Load2\"";
            errCode = LoadCaseManager.AddNewLoadCaseCombination("Load Case Combination4", strCombination);
            if (errCode > 0)
                ErrorMsg(swApp, "Load Case Combination4 not added");
            bSuccess = LoadCaseManager.DeleteLoadCaseCombination("Load Case Combination3");
            if (bSuccess == false)
                ErrorMsg(swApp, "Load Case Combination3 not deleted");
            bSuccess = LoadCaseManager.DeleteLoadCaseCombination("Load Case Combination4");
            if (bSuccess == false)
                ErrorMsg(swApp, "Load Case Combination4 cannot be deleted");
            strCombination = "\"My Load Case1\" + \"Live Load2\"";
            errCode = LoadCaseManager.AddNewLoadCaseCombination("Load Case Combination3", strCombination);
            if (errCode > 0)
                ErrorMsg(swApp, "Load Case Combination3 not added");
            bSuccess = LoadCaseManager.SuppressOrUnSuppressLoadCaseCombination("Load Case Combination2"true);
            if (bSuccess == false)
                ErrorMsg(swApp, "Load Case Combination2 not suppressed");
            bSuccess = LoadCaseManager.SuppressOrUnSuppressLoadCaseCombination("Load Case Combination2"false);
            if (bSuccess == false)
                ErrorMsg(swApp, "Load Case Combination2 not unsuppressed");
            bSuccess = LoadCaseManager.SuppressOrUnSuppressLoadCaseCombination("Load Case Combination3"true);
            if (bSuccess == false)
                ErrorMsg(swApp, "Load Case Combination3 not suppressed");
            bSuccess = LoadCaseManager.RenameLoadCaseCombination("Load Case Combination2""My Combination2");
            if (bSuccess == false)
                ErrorMsg(swApp, "Load Case Combination2 not renamed");
            bSuccess = LoadCaseManager.RenameLoadCaseCombination("My Combination2""Load Case Combination2");
            if (bSuccess == false)
                ErrorMsg(swApp, "Load Case Combination2 not renamed");
            strCombination = "\"My Load Case1\" * \"Live Load2\"";
            errCode = LoadCaseManager.SetEquationForLoadCaseCombination("Load Case Combination1", strCombination);
            if (errCode == 0)
                ErrorMsg(swApp, "Load Case Combination1 equation not set");
            strCombination = "(\"My Load Case1\"*2 + \"Live Load2\"*2)/2";
            errCode = LoadCaseManager.SetEquationForLoadCaseCombination("Load Case Combination1", strCombination);
            if (errCode > 0)
                ErrorMsg(swApp, "Load Case Combination1 equation not set");
            retStrCombination = LoadCaseManager.GetEquationForLoadCaseCombination("Load Case Combination1");
            errCode = retStrCombination.CompareTo(strCombination);
            if (errCode < 0 | errCode > 0)
                ErrorMsg(swApp, "Load Case Combination1 equation not returned");
            PrimaryCases = LoadCaseManager.GetAllPrimaryLoadCaseNames();
            SecondaryCases = LoadCaseManager.GetAllLoadCaseCombinationNames();
            errCode = LoadCaseManager.RunLoadCases(truetrue);
            if (errCode > 0)
                ErrorMsg(swApp, "Load cases failed to run");
            bSuccess = LoadCaseManager.ShowResultsViewTab();
            if (bSuccess == false)
                ErrorMsg(swApp, "Cannot display Results View tab");
            bSuccess = LoadCaseManager.LoadResultsOfPrimaryLoadCase("My Load Case1");
            if (bSuccess == false)
                ErrorMsg(swApp, "My Load Case1 results not loaded");
            CWResult = Study.Results;
            if (CWResult == null)
                ErrorMsg(swApp, "No CWResults object");

            CWCFf = CWResult.CreatePlot((int)swsPlotResultTypes_e.swsResultDisplacementOrAmplitude, (int)swsStaticResultDisplacementComponentTypes_e.swsStaticDisplacement_URES, (int     swsUnit_e.swsUnitSI, falseout errCode);

            bSuccess = LoadCaseManager.LoadResultsOfPrimaryLoadCase("Live Load2");
            if (bSuccess == false)
                ErrorMsg(swApp, "Live Load2 results not loaded");
            bSuccess = LoadCaseManager.LoadResultsOfLoadCaseCombination("Load Case Combination1");
            if (bSuccess == false)
                ErrorMsg(swApp, "Load Case Combination1 results not loaded");
            bSuccess = LoadCaseManager.LoadResultsOfLoadCaseCombination("Load Case Combination2");
            if (bSuccess == false)
                ErrorMsg(swApp, "Load Case Combination2 results not loaded");
        public void ErrorMsg(SldWorks SwApp, string Message)
            SwApp.SendMsgToUser2(Message, 0, 0);
            SwApp.RecordLine("'*** WARNING - General");
            SwApp.RecordLine("'*** " + Message);
        /// <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:   Access Load Case Manager 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) 2017 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.