Analyze Part (C#)
This example shows how to create a static analysis of a part.
NOTE: To get persistent reference
identifiers (PIDs) for model selections, you can use
pidcollector.exe
or IModelDocExtension::GetPersistReference3.
//--------------------------------------------------------------------------
// 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
// (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. Modify
the path to solidworks materials.sldmat
as needed.
// 4. Open
the Immediate window.
// 5. Run
the macro.
//
// Postconditions:
// 1. Study
called Static-solid is created.
// 2. Material
is applied to the model.
// 3. Restraints
and pressure are applied to the selected faces.
// 4. Mesh
is created.
// 5. Analysis
is run.
// 6. Stress
and strain values written to Immediate window.
//
// NOTE: Because this part document is used
// by an online SolidWorks Simulation tutorial,
// do not save any changes when closing the
// the document.
//-------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using SolidWorks.Interop.cosworks;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace StartAnalysisPartCSharp.csproj
{
partial
class SolidWorksMacro
{
public
void Main()
{
CosmosWorks
COSMOSWORKS = default(CosmosWorks);
CwAddincallback
CWObject = default(CwAddincallback);
CWModelDoc
ActDoc = default(CWModelDoc);
CWStudyManager
StudyMngr = default(CWStudyManager);
CWStudy
Study = default(CWStudy);
CWSolidManager
SolidMgr = default(CWSolidManager);
CWSolidComponent
SolidComponent = default(CWSolidComponent);
CWSolidBody
SolidBody = default(CWSolidBody);
CWMesh
CwMesh = default(CWMesh);
CWResults
CWResult = default(CWResults);
object
stress = null;
ModelDoc2
Part = default(ModelDoc2);
CWLoadsAndRestraintsManager
LBCMgr = default(CWLoadsAndRestraintsManager);
CWPressure
CWPressure = default(CWPressure);
CWRestraint
CWRes1 = default(CWRestraint);
object
pointer1 = null;
object
pointer2 = null;
object
pointer3 = null;
CWMaterial
CWMat = default(CWMaterial);
byte[]
var1 = null;
byte[]
var2 = null;
byte[]
var3 = null;
int
returnValue = 0;
string
selection1 = null;
string
selection2 = null;
string
selection3 = null;
int
status = 0;
int
warnings = 0;
int
errCode = 0;
int
NSource = 0;
double
el = 0;
double
tl = 0;
//
Open SolidWorks part document
Part
= (ModelDoc2)swApp.OpenDoc6("C:\\Program
Files\\SolidWorks Corp\\SolidWorks\\Simulation\\Examples\\Tutor1.SLDPRT
", (int)swDocumentTypes_e.swDocPART, 1, "", ref status,
ref warnings);
if
(Part == null) ErrorMsg(swApp, "Failed to open C:\\Program Files\\SolidWorks
Corp\\SolidWorks\\Simulation\\Examples\\Tutor1.SLDPRT.", true);
//
Get SolidWorks Simulation object
CWObject
= (CwAddincallback)swApp.GetAddInObject("SldWorks.Simulation");
if
(CWObject == null) ErrorMsg(swApp, "CWObject object not found.",
true);
COSMOSWORKS
= (CosmosWorks)CWObject.CosmosWorks;
if
(COSMOSWORKS == null) ErrorMsg(swApp, "CWObject not found.",
true);
//
Get active document
ActDoc
= (CWModelDoc)COSMOSWORKS.ActiveDoc;
if
(ActDoc == null) ErrorMsg(swApp, "No active document.", true);
//
Create new static study
StudyMngr
= (CWStudyManager)ActDoc.StudyManager;
if
(StudyMngr == null) ErrorMsg(swApp, "StudyMngr object not there.",
true);
Study
= (CWStudy)StudyMngr.CreateNewStudy("Static_solid",
(int)swsAnalysisStudyType_e.swsAnalysisStudyTypeStatic, (int)swsMeshType_e.swsMeshTypeSolid,
out errCode);
if
(Study == null) ErrorMsg(swApp, "Study not created.", true);
//
Set material from the SolidWorks material library
SolidMgr
= (CWSolidManager)Study.SolidManager;
if
(SolidMgr == null) ErrorMsg(swApp, "SolidMgr object not there.",
true);
SolidComponent
= (CWSolidComponent)SolidMgr.GetComponentAt(0,
out errCode);
if
(errCode != 0) ErrorMsg(swApp, "No solid component.", true);
SolidBody
= (CWSolidBody)SolidComponent.GetSolidBodyAt(0,
out errCode);
if
(errCode != 0) ErrorMsg(swApp, "No solid body.", true);
returnValue
= SolidBody.SetLibraryMaterial("c:\\Program
Files\\SolidWorks Corp\\SolidWorks\\lang\\english\\sldmaterials\\solidworks
materials.sldmat", "Ductile Iron (SN)");
if
(returnValue == 0) ErrorMsg(swApp, "No material applied.", true);
CWMat
= (CWMaterial)SolidBody.GetDefaultMaterial();
NSource
= CWMat.Source;
//
Get the PIDs of the faces
//
First two selections are the faces for restraints
//
Third selection is the face where pressure is applied
selection1
= "216,14,0,0,3,0,0,0,255,254,255,0,0,0,0,0,255,255,1,0,11,0,109,111,70,97,99,101,82,101,102,95,99,1,0,0,0,0,0,0,0,5,0,0,0,0,3,0,0,0,0,0,0,125,195,148,37,173,73,178,84,125,195,148,37,173,73,178,84,0,0,255,255,1,0,21,0,109,111,76,80,97,116,116,101,114,110,83,117,114,102,73,100,82,101,112,95,99,0,0,255,255,1,0,6,0,109,111,70,82,95,99,255,255,1,0,13,0,109,111,69,120,116,79,98,106,101,99,116,95,99,255,255,1,0,14,0,109,111,79,98,106,70,105,108,101,68,101,102,95,99,255,255,1,0,17,0,109,111,67,83,116,114,105,110,103,72,97,110,100,108,101,95,99,255,254,255,6,84,0,117,0,116,0,111,0,114,0,49,0,123,228,186,50,11,128,255,254,255,62,67,0,58,0,92,0,80,0,114,0,111,0,103,0,114,0,97,0,109,0,32,0,70,0,105,0,108,0,101,0,115,0,92,0,83,0,111,0,108,0,105,0,100,0,87,0,111,0,114,0,107,0,115,0,92,0,67,0,79,0,83,0,77,0,79,0,83,0,87,0,111,0,114,0,107,0,115,0,92,0,69,0,120,0,97,0,109,0,112,0,108,0,101,0,115,0,92,0,84,0,117,0,116,0,111,0,114,0,49,0,46,0,83,0,76,0,68,0,80,0,82,0,84,0,97,23,28,65,0,0,0,0";
selection1
= selection1 + ",2,0,1,0,0,0,0,0,0,0,1,0,0,0,255,254,255,7,68,0,101,0,102,0,97,0,117,0,108,0,116,0,0,0,0,0,0,0,0,0,0,0,0,0,17,0,0,0,156,231,186,50,255,255,1,0,23,0,109,111,70,114,111,109,83,107,116,69,110,116,83,117,114,102,73,100,82,101,112,95,99,0,0,5,128,8,0,14,0,0,0,88,231,186,50,3,0,0,0,1,0,0,0,0,0,0,0,255,255,1,0,20,0,109,111,69,110,100,70,97,99,101,83,117,114,102,73,100,82,101,112,95,99,0,0,5,128,8,0,6,0,0,0,211,228,186,50,1,0,0,0,255,255,255,255,0,0,17,128,0,0,5,128,8,0,6,0,0,0,211,228,186,50,0,0,0,0,255,255,255,255,0,0,0,0,0,0";
StringtoArray(selection1,
ref var1);
pointer1
= Part.Extension.GetObjectByPersistReference3((var1),
out status);
selection2
= "216,14,0,0,3,0,0,0,255,254,255,0,0,0,0,0,255,255,1,0,11,0,109,111,70,97,99,101,82,101,102,95,99,1,0,0,0,0,0,0,0,5,0,0,0,0,3,0,0,0,0,0,0,125,195,148,37,173,73,178,84,125,195,148,37,173,73,178,84,0,0,255,255,1,0,23,0,109,111,70,114,111,109,83,107,116,69,110,116,83,117,114,102,73,100,82,101,112,95,99,0,0,255,255,1,0,6,0,109,111,70,82,95,99,255,255,1,0,13,0,109,111,69,120,116,79,98,106,101,99,116,95,99,255,255,1,0,14,0,109,111,79,98,106,70,105,108,101,68,101,102,95,99,255,255,1,0,17,0,109,111,67,83,116,114,105,110,103,72,97,110,100,108,101,95,99,255,254,255,6,84,0,117,0,116,0,111,0,114,0,49,0,123,228,186,50,11,128,255,254,255,62,67,0,58,0,92,0,80,0,114,0,111,0,103,0,114,0,97,0,109,0,32,0,70,0,105,0,108,0,101,0,115,0,92,0,83,0,111,0,108,0,105,0,100,0,87,0,111,0,114,0,107,0,115,0,92,0,67,0,79,0,83,0,77,0,79,0,83,0,87,0,111,0,114,0,107,0,115,0,92,0,69,0,120,0,97,0,109,0,112,0,108,0,101,0,115,0,92,0,84,0,117,0,116,0,111,0,114,0,49,0,46,0,83,0,76,0,68,0,80,0,82,0,84,0,97,23,28,";
selection2
= selection2 + "65,0,0,0,0,2,0,1,0,0,0,0,0,0,0,1,0,0,0,255,254,255,7,68,0,101,0,102,0,97,0,117,0,108,0,116,0,0,0,0,0,0,0,0,0,0,0,0,0,14,0,0,0,88,231,186,50,3,0,0,0,255,255,1,0,20,0,109,111,69,110,100,70,97,99,101,83,117,114,102,73,100,82,101,112,95,99,0,0,5,128,8,0,6,0,0,0,211,228,186,50,1,0,0,0,255,255,255,255,0,0,14,128,0,0,5,128,8,0,6,0,0,0,211,228,186,50,0,0,0,0,255,255,255,255,0,0,0,0,0,0";
StringtoArray(selection2,
ref var2);
pointer2
= Part.Extension.GetObjectByPersistReference3((var2),
out status);
selection3
= "216,14,0,0,3,0,0,0,255,254,255,0,0,0,0,0,255,255,1,0,11,0,109,111,70,97,99,101,82,101,102,95,99,1,0,0,0,0,0,0,0,8,0,0,0,0,3,0,0,0,0,0,0,125,195,148,37,173,73,178,84,125,195,148,37,173,73,178,84,0,0,255,255,1,0,20,0,109,111,69,110,100,70,97,99,101,83,117,114,102,73,100,82,101,112,95,99,0,0,255,255,1,0,6,0,109,111,70,82,95,99,255,255,1,0,13,0,109,111,69,120,116,79,98,106,101,99,116,95,99,255,255,1,0,14,0,109,111,79,98,106,70,105,108,101,68,101,102,95,99,255,255,1,0,17,0,109,111,67,83,116,114,105,110,103,72,97,110,100,108,101,95,99,255,254,255,6,84,0,117,0,116,0,111,0,114,0,49,0,123,228,186,50,11,128,255,254,255,62,67,0,58,0,92,0,80,0,114,0,111,0,103,0,114,0,97,0,109,0,32,0,70,0,105,0,108,0,101,0,115,0,92,0,83,0,111,0,108,0,105,0,100,0,87,0,111,0,114,0,107,0,115,0,92,0,67,0,79,0,83,0,77,0,79,0,83,0,87,0,111,0,114,0,107,0,115,0,92,0,69,0,120,0,97,0,109,0,112,0,108,0,101,0,115,0,92,0,84,0,117,0,116,0,111,0,114,0,49,0,46,0,83,0,76,0,68,0,80,0,82,0,84,0,97,23,28,65,0,0,0,0,2,0,";
selection3
= selection3 + "1,0,0,0,0,0,0,0,1,0,0,0,255,254,255,7,68,0,101,0,102,0,97,0,117,0,108,0,116,0,0,0,0,0,0,0,0,0,0,0,0,0,11,0,0,0,51,230,186,50,1,0,0,0,255,255,255,255,255,255,1,0,23,0,109,111,70,114,111,109,83,107,116,69,110,116,83,117,114,102,73,100,82,101,112,95,99,0,0,5,128,8,0,8,0,0,0,218,229,186,50,4,0,0,0,0,0,255,255,1,0,18,0,109,111,80,76,105,110,101,83,117,114,102,73,100,82,101,112,95,99,0,0,5,128,8,0,31,0,0,0,210,195,223,50,17,128,0,0,5,128,8,0,23,0,0,0,160,192,223,50,14,128,0,0,5,128,8,0,19,0,0,0,181,233,186,50,1,0,0,0,1,0,0,0,2,0,0,0,17,128,0,0,5,128,8,0,31,0,0,0,210,195,223,50,17,128,0,0,5,128,8,0,23,0,0,0,160,192,223,50,14,128,0,0,5,128,8,0,19,0,0,0,181,233,186,50,1,0,0,0,1,0,0,0,1,0,0,0,17,128,0,0,5,128,8,0,31,0,0,0,210,195,223,50,17,128,0,0,5,128,8,0,23,0,0,0,160,192,223,50,14,128,0,0,5,128,8,0,19,0,0,0,181,233,186,50,1,0,0,0,2,0,0,0,3,0,0,0,17,128,0,0,5,128,8,0,31,0,0,0,210,195,223,50,17,128,0,0,5,128,8,0,23,0,0,0,160,192,223,50,14,";
selection3
= selection3 + "128,0,0,5,128,8,0,19,0,0,0,181,233,186,50,1,0,0,0,2,0,0,0,4,0,0,0,0,0,0,0,0,0";
StringtoArray(selection3,
ref var3);
pointer3
= Part.Extension.GetObjectByPersistReference3((var3),
out status);
//
Create variant arrays
object[]
varArray1 = { pointer1, pointer2 };
object[]
varArray2 = { pointer3 };
//
Add fixed restraint
LBCMgr
= (CWLoadsAndRestraintsManager)Study.LoadsAndRestraintsManager;
if
(LBCMgr == null) ErrorMsg(swApp, "No loads and restraints manager.",
false);
CWRes1
= (CWRestraint)LBCMgr.AddRestraint(0,
(varArray1), null, out errCode);
if
(errCode != 0) ErrorMsg(swApp, "No fixed restraint created.",
true);
//
Apply pressure normal to selected face
CWPressure
= (CWPressure)LBCMgr.AddPressure(0,
(varArray2), null, out errCode);
if
(errCode != 0) ErrorMsg(swApp, "No normal pressure created.",
true);
CWPressure.PressureBeginEdit();
CWPressure.Unit = 1;
CWPressure.Value = 1000;
errCode
= CWPressure.PressureEndEdit();
if
(errCode != 0) ErrorMsg(swApp, "Nonuniform pressure distribution
not set.", true);
//
Create mesh
CwMesh
= (CWMesh)Study.Mesh;
if
(CwMesh == null) ErrorMsg(swApp, "No mesh object.", false);
CwMesh.Quality = 1;
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);
CWResult
= (CWResults)Study.Results;
if
(CWResult == null) ErrorMsg(swApp, "No result object.", false);
stress
= CWResult.GetMinMaxStress(0,
0, 1, null, 0, out errCode);
//
Get strain and stress values for
// first face selected for restraint
DispatchWrapper[]
dispWrapper = new DispatchWrapper[1];
dispWrapper[0]
= new DispatchWrapper((Entity)pointer1);
object[]
results = null;
string
resultsString = null;
int
i;
Debug.Print("
Strain
value by node:");
results
= (object[])CWResult.GetStrainForEntities2(true,
(int)swsStrainComponent_e.swsStrainComponentESTRN, 1, null, dispWrapper,
out status);
for
(i = 0; i < results.Length; i++)
{
resultsString
= Convert.ToString(results[i]);
Debug.Print("
"
+ resultsString);
}
Debug.Print("
");
Debug.Print("
Stress
value by node:");
results
= (object[])CWResult.GetStressForEntities2(true,
(int)swsStressComponent_e.swsStressComponentVON, 1, null, dispWrapper,
(int)swsStrengthUnit_e.swsStrengthUnitPascal, out status);
for
(i = 0; i < results.Length; i++)
{
resultsString
= Convert.ToString(results[i]);
Debug.Print("
"
+ resultsString);
}
}
private
void ErrorMsg(object SwApp, string Message, bool EndTest)
{
swApp.SendMsgToUser2(Message,
0, 0);
swApp.RecordLine("'***
WARNING - General");
swApp.RecordLine("'***
" + Message);
swApp.RecordLine("");
if
(EndTest)
{
}
}
private
void StringtoArray(string inputSTR, ref byte[] varEntity)
{
string[]
PIDArray = null;
byte[]
PID = null;
int
i;
//
Parse string into an array
PIDArray
= inputSTR.Split(new char[] { ',' });
//Convert
string array to byte array
int
sizeArray = PIDArray.Length;
PID
= new byte[sizeArray];
for
(i = 0; i < PIDArray.Length; i++)
{
PID[i]
= Convert.ToByte(PIDArray[i]);
}
varEntity
= PID;
}
///
<summary>
///
The SldWorks swApp variable is pre-assigned for you.
///
</summary>
public
SldWorks swApp;
}
}