Using C# to program CosmosWorks (Simulation)

28 Jul

Thanks go to Mahir Abrahim at General Atomics for his VB code, and I have converted it to equivalent C# code, with some additions of code from Simulation Manual.

private CwAddincallback pCWAddin = null;
private CosmosWorks pCWApp = null;
private CWModelDoc pCWDoc = null;
private CWStudyManager pStudyManager = null;
private CWStudy pStudy=null;
private CWMesh pMesh = null;

pCWAddin = (CwAddincallback)SwApp.GetAddInObject(“SldWorks.Simulation”);
Debug.Assert(pCWAddin != null);

pCWApp = pCWAddin.CosmosWorks;
pCWDoc = pCWApp.ActiveDoc;
pStudyManager = pCWDoc.StudyManager;
int nStudies = pStudyManager.StudyCount;
int errCode;
const int cwStaticAnalysis = 0; //const int cwSolidElementMesh = 0;
pStudy = pStudyManager.CreateNewStudy2(“Static_solid”, cwStaticAnalysis, out errCode);

pStudyManager.ActiveStudy = 0;
pStudy = pStudyManager.GetStudy(0);
CWSolidManager pSolidMgr = pStudy.SolidManager;
CWSolidComponent pSolidComponent=pSolidMgr.GetComponentAt(0, out errCode);
CWSolidBody pSolidBody = pSolidComponent.GetSolidBodyAt(0, out errCode);
pMesh = pStudy.Mesh;
const int cwMeshState_SUCCEEDED=1;

int rtn = -100;
if (pMesh.MeshState != cwMeshState_SUCCEEDED)
pMesh.Quality = 1;
double el, tl;
pMesh.GetDefaultElementSizeAndTolerance(0, out el, out tl);
rtn=pStudy.CreateMesh(0, el,tl);

string info=string.Format(“{0} elements created”, pMesh.ElementCount.ToString());

Leave a comment

Posted by on July 28, 2009 in CAD, Dotnet/C#


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: