Get Stresses for Selected Beams Example (C#)
This example shows how to get the stress values for the selected beams.
//--------------------------------------------------------------------------
// 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 <SolidWorks_install_dir>\api\redist folder and
// select
SolidWorks.Interop.cosworks.dll).
// 3. Open the Immediate window.
// 4. Open a SolidWorks model that has structural members
and a
// Simulation
static study with beams.
// 5. Select the Simulation study tab.
// 6. Click the Run button on the Simulation CommandManager
to
// refresh
the study results.
// 7. In the Simulation Study tree, select the beams for
which
// you
want to know their stresses.
// 8. Run the macro.
//
// Postconditions: The selected beams' stresses are printed
to the
// the Immediate window.
//-------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using SolidWorks.Interop.cosworks;
using System;
using System.Diagnostics;
namespace ListBeamStressesCSharp.csproj
{
partial
class SolidWorksMacro
{
public
void Main()
{
ModelDoc2
swModel = default(ModelDoc2);
SelectionMgr
swSelMgr = default(SelectionMgr);
CosmosWorks
COSMOSWORKS = default(CosmosWorks);
CwAddincallback
COSMOSObject = default(CwAddincallback);
CWModelDoc
ActDoc = default(CWModelDoc);
CWStudyManager
StudyMngr = default(CWStudyManager);
CWStudy
Study = default(CWStudy);
CWBeamManager
BeamMgr = default(CWBeamManager);
CWResults
Results = default(CWResults);
int
actStudy = 0;
int
beamStress = 0;
int
nbrSteps = 0;
int
unit = 0;
int
errCode = 0;
object[]
selBeams = null;
int
nbrSelectedBeams = 0;
int
i = 0;
int
k = 0;
//
Connect to SolidWorks
swModel
= (ModelDoc2)swApp.ActiveDoc;
swSelMgr
= (SelectionMgr)swModel.SelectionManager;
//
Get the SolidWorks Simulation object
COSMOSObject
= (CwAddincallback)swApp.GetAddInObject("Sldworks.Simulation");
if
(COSMOSObject == null) ErrorMsg("COSMOSObject object not found.",
true);
COSMOSWORKS
= (CosmosWorks)COSMOSObject.CosmosWorks;
if
(COSMOSWORKS == null) ErrorMsg("COSMOSWORKS object not found.",
true);
//
Get the active document
ActDoc
= (CWModelDoc)COSMOSWORKS.ActiveDoc;
if
(ActDoc == null) ErrorMsg("No active document.", true);
//
Get the active study
StudyMngr
= (CWStudyManager)ActDoc.StudyManager;
if
(StudyMngr == null) ErrorMsg("StudyMngr object not there.",
true);
actStudy
= StudyMngr.ActiveStudy;
Study
= (CWStudy)StudyMngr.GetStudy(actStudy);
if
(Study == null) ErrorMsg("Study not created.", true);
//
Get the results
Results
= (CWResults)Study.Results;
//
Get the selected beams' stresses
BeamMgr
= (CWBeamManager)Study.BeamManager;
beamStress
= (int)swsBeamStressType_e.swsBeamStressWorstCase;
nbrSteps
= 1;
unit
= (int)swsUnit_e.swsUnitSI;
nbrSelectedBeams
= swSelMgr.GetSelectedObjectCount2(-1);
selBeams
= new object[nbrSelectedBeams];
for
(k = 1; k <= nbrSelectedBeams; k++)
{
selBeams[k-1]
= (object)swSelMgr.GetSelectedObject6(k,
-1);
}
Object
arrBeamStresses = Results.GetBeamStressForEntities(beamStress,
nbrSteps, (selBeams), unit, out errCode);
Array
BeamStresses = (Array)arrBeamStresses;
for
(i = 0; i <= BeamStresses.GetUpperBound(0); i++)
{
string
beamStressElement = "";
beamStressElement
= Convert.ToString(BeamStresses.GetValue(i));
Debug.Print(beamStressElement);
}
}
//Error
routine
private
void ErrorMsg(string Message, bool EndTest)
{
swApp.SendMsgToUser2(Message, 0, 0);
swApp.RecordLine("'*** WARNING - General");
swApp.RecordLine("'*** " + Message);
swApp.RecordLine("");
if
(EndTest)
{
}
}
public
SldWorks swApp;
}
}