Get and Set Beams and Joints Example (C#)
This example shows how to get and set beams and joints.
//---------------------------------------------------------------------------
// 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. Open the Immediate window.
// 5. Run the macro.
//
// Postconditions:
// 1. A static study, named frame,
is created.
// 2. Beam information is printed to the Immediate window.
// 3. Plain Carbon Steel
material is applied to all beams.
// 4. Joints are calculated for all beams, and
// a
neutral axis is shown for each beam. Pinball tolerance
// value
and unit are printed to the Immediate window.
// 5. Mixed mesh is created, and type and state printed
to Immediate window.
//
// NOTES:
// * Beam elements are created by default for parts with
// structural
members.
// * Because the part 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;
namespace Macro1.csproj
{
partial
class SolidWorksMacro
{
public
void Main()
{
CosmosWorks
COSMOSWORKS = default(CosmosWorks);
CwAddincallback
COSMOSObject = default(CwAddincallback);
CWModelDoc
ActDoc = default(CWModelDoc);
CWStudyManager
StudyMngr = default(CWStudyManager);
CWStudy
Study = default(CWStudy);
CWBeamManager
BeamMgr = default(CWBeamManager);
CWBeamBody
BeamBody = default(CWBeamBody);
CWJoints
Joints = default(CWJoints);
CWMesh
Mesh = default(CWMesh);
int
nbrBeamBodies = 0;
int
beamBodyType = 0;
double
ElementSize = 0;
double
Tolerance = 0;
int
errors = 0;
int
warnings = 0;
int
errCode = 0;
int
j = 0;
bool keepJointUpdates = true;
//
Get the SOLIDWORKS Simulation object
COSMOSObject
= (CwAddincallback)swApp.GetAddInObject("SldWorks.Simulation");
if
(COSMOSObject == null) ErrorMsg("COSMOSObject object not found.",
true);
COSMOSWORKS
= COSMOSObject.CosmosWorks;
if
(COSMOSWORKS == null) ErrorMsg("COSMOSWORKS object not found.",
true);
//Open
and get the active document
swApp.OpenDoc6("c:\\Program Files\\SOLIDWORKS
Corp\\SOLIDWORKS\\samples\\tutorial\\weldments\\weldment_box2.sldprt",
(int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent,
"", ref errors, ref warnings);
ActDoc
= (CWModelDoc)COSMOSWORKS.ActiveDoc;
if
(ActDoc == null) ErrorMsg("No active document.", true);
//Create
new static study named frame
StudyMngr
= (CWStudyManager)ActDoc.StudyManager;
if
(StudyMngr == null) ErrorMsg("StudyMngr object not there.",
true);
Study
= (CWStudy)StudyMngr.CreateNewStudy("frame",
(int)swsAnalysisStudyType_e.swsAnalysisStudyTypeStatic, (int)swsMeshType_e.swsMeshTypeMixed,
out errCode);
if
(Study == null) ErrorMsg("Study not created.", true);
//
Get and set beam info
BeamMgr
= (CWBeamManager)Study.BeamManager;
nbrBeamBodies
= BeamMgr.BeamCount;
Debug.Print("Beams...");
Debug.Print("
Number of beams: " + nbrBeamBodies);
BeamBody
= null;
for
(j = 0; j <= (nbrBeamBodies - 1); j++)
{
BeamBody
= (CWBeamBody)BeamMgr.GetBeamBodyAt(j,
out errCode);
if
(errCode != 0) ErrorMsg("No beam body.", true);
Debug.Print("
Name of beam body: " + BeamBody.BeamBodyName);
beamBodyType
= BeamBody.BeamType;
if
(beamBodyType == 0)
{
Debug.Print("
Type of beam body: beam");
}
else
{
Debug.Print("
Type of beam body: truss");
}
errCode
= BeamBody.SetLibraryMaterial("C:\\Program
Files\\SOLIDWORKS Corp\\SOLIDWORKS\\lang\\english\\sldmaterials\\solidworks
materials.sldmat", "Plain Carbon Steel");
if
(errCode == 0) ErrorMsg("No material applied.", true);
BeamBody
= null;
}
//
Calculate joints
Joints
= (CWJoints)BeamMgr.GetJointGroup(out
errCode);
Debug.Print("
");
Debug.Print("Joints...");
if
(errCode != 0) ErrorMsg("No joint group.", true);
Joints.JointsBeginEdit();
Joints.IncludeAllSelectedBeam = true;
Joints.IncludeDisplayNeutralAxis = true;
Joints.CalculateJoints();
Joints.JointsEndEdit();
Debug.Print("
Pinball radius: " + Joints.PinBallRadius
* 0.001);
keepJointUpdates = Joints.IncludeKeepModifiedJointOnUpdate;
if (keepJointUpdates == true)
{
Debug.Print(" Keep joint updates: yes");
}
else
Debug.Print(" Keep joint udpates: no");
Debug.Print(" Overwrite pinball value: " + Joints.IncludeTreatAsJointForClearanceLessThan);
switch
(Joints.PinBallRadiusUnit)
{
case
0:
Debug.Print("
Pinball radius unit: mm");
break;
case
1:
Debug.Print("
Pinball radius unit: cm");
break;
case
2:
Debug.Print("
Pinball radius unit: m");
break;
case
3:
Debug.Print("
Pinball radius unit: in");
break;
case
4:
Debug.Print("
Pinball radius unit: ft");
break;
case
5:
Debug.Print("
Pinball radius unit: ft-in");
break;
case
6:
Debug.Print("
Pinball radius unit: am");
break;
case
7:
Debug.Print("
Pinball radius unit: nm");
break;
case
8:
Debug.Print("
Pinball radius unit: micron");
break;
case
9:
Debug.Print("
Pinball radius unit: mil");
break;
case
10:
Debug.Print("
Pinball radius unit: MicroIn");
break;
}
//
Mesh the part
Mesh
= Study.Mesh;
if
(Mesh == null) ErrorMsg("No mesh object.", false);
Mesh.Quality = (int)swsMeshQuality_e.swsMeshQualityHigh;
Mesh.GetDefaultElementSizeAndTolerance((int)swsLinearUnit_e.swsLinearUnitMillimeters,
out ElementSize, out Tolerance);
errCode
= Study.CreateMesh((int)swsLinearUnit_e.swsLinearUnitMillimeters,
ElementSize, Tolerance);
if
(errCode != 0) ErrorMsg("Mesh failed.", true);
Debug.Print("
");
Debug.Print("Mesh...");
Debug.Print("
Time to create mesh (hh:mm:ss): " + Mesh.TimeToCompleteMesh);
switch
(Mesh.MeshType)
{
case
0:
Debug.Print("
Mesh type: solid");
break;
case
1:
Debug.Print("
Mesh type: midsurface");
break;
case
2:
Debug.Print("
Mesh type: surface");
break;
case
3:
Debug.Print("
Mesh type: mixed");
break;
case
4:
Debug.Print("
Mesh type: beam");
break;
}
Debug.Print("
Number of mesh controls: " + Mesh.MeshControlCount);
switch
(Mesh.MeshState)
{
case
0:
Debug.Print("
Mesh state: no mesh");
break;
case
1:
Debug.Print("
Mesh state: exists and is current");
break;
case
2:
Debug.Print("
Mesh state: exists and is not current");
break;
case
3:
Debug.Print("
Mesh state: failed");
break;
case
4:
Debug.Print("
Mesh state: interrupted");
break;
}
}
//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)
{
}
}
///
<summary>
///
The SldWorks swApp variable is pre-assigned for you.
///
</summary>
public
SldWorks swApp;
}
}