0% found this document useful (0 votes)
193 views10 pages

Using OpenSTAAD with C# Code

This document describes how to connect to and interact with the STAAD application using the OpenSTAADUI object in C#. It provides code examples for getting a list of opened STAAD files, getting the STAAD interface associated with an opened file, getting the STAAD instance count, checking if any or multiple STAAD instances are running, and some helper methods.

Uploaded by

Bhavin Shah
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
193 views10 pages

Using OpenSTAAD with C# Code

This document describes how to connect to and interact with the STAAD application using the OpenSTAADUI object in C#. It provides code examples for getting a list of opened STAAD files, getting the STAAD interface associated with an opened file, getting the STAAD instance count, checking if any or multiple STAAD instances are running, and some helper methods.

Uploaded by

Bhavin Shah
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • Using OpenSTAAD application object (C#)
  • STAAD Pro application code interaction
  • Accessing Individual Objects

Using OpenSTAAD application object (C#)

Include OpenSTAADUI Library


1. Right click on ‘Reference’ node and click on ‘Add References’
2. This will bring up the ‘Add Reference’ dialog box. Select ‘COM’ tab and scroll down to find ‘OpenSTAADUI’ object. (Refer diagram below).

3. Click ‘OK’ to add ‘OpenSTAADUI’ to project.

4. Now, at the top of code file add the following line,


Connection
The following section will describe how to connect to the [Link] application using OpenSTAADUI object.

Get list of opened STAAD files,


public static List<string> GetOpenedStaadFiles()
{
try
{
List<string> fileList = null;
Hashtable addedFileNames = new Hashtable();

Type type = [Link]("[Link]");


string clsid = [Link]();
string lookUpCandidateName = [Link]("!{0}{1}{2}", "{", clsid, "}").ToUpper();

List<RunningObjectInfo> runningObjects = GetRunningObjectList();


foreach (RunningObjectInfo item in runningObjects)
{
string candidateName = [Link]();
// check if its a valid STAAD file
string extStr = [Link](candidateName);
if (extStr != null)
{
if (![Link](extStr))
{
if (extStr == ".STD")
{
// we have a std file opened, but this can be opened by other applications
also,
// we need to get the staadobject to see if it is opened by STAAD
OpenSTAAD osObject = [Link] as OpenSTAAD;
if (osObject != null)
{
if (fileList == null)
fileList = new List<string>();

if (![Link]([Link]()))
{
[Link]([Link]());
[Link]([Link](), "");
}
}
}
}

if ([Link](lookUpCandidateName))
{
// we got the staad instance
OpenSTAAD osObject = [Link] as OpenSTAAD;
if (osObject != null)
{
// get staad filename
string staadFilename = "";
bool includeFullPath = true;
Object oArg1 = staadFilename as object;
Object oArg2 = includeFullPath as object;
[Link](ref oArg1, oArg2);
staadFilename = oArg1 as string;
//

if (![Link](staadFilename))
{
if (fileList == null)
fileList = new List<string>();

if (![Link]([Link]()))
{
[Link]([Link]());
[Link]([Link](), "");
}
}
}
}
}
}
return fileList;
}
catch (COMException)
{
throw;
}
}

Get the [Link] application associated with opened STAAD file,


public static OpenSTAAD GetSTAADUIInterface(string staadFilename)
{
OpenSTAAD osObject = null;

try
{
Type type = [Link]("[Link]");
string clsid = [Link]();
string lookUpCandidateName = [Link]("!{0}{1}{2}", "{", clsid, "}").ToUpper();

List<RunningObjectInfo> runningObjects = GetRunningObjectList();


foreach (RunningObjectInfo item in runningObjects)
{
string candidateName = [Link]();
// check if its a valid STAAD file
string extStr = [Link](candidateName);
if (extStr != null)
{
if (![Link](extStr))
{
if (extStr == ".STD")
{
if ([Link]([Link](), [Link]()) == 0)
{
// we have a std file opened, but this can be opened by other
applications also,
// we need to get the staadobject to see if it is opened by STAAD
osObject = [Link] as OpenSTAAD;
if (osObject != null)
{
return osObject;
}
}
}
}

if ([Link](lookUpCandidateName))
{
// we got the staad instance
osObject = [Link] as OpenSTAAD;

if (osObject != null)
{
// get staad filename
string lookupStaadFilename = "";
bool includeFullPath = true;
Object oArg1 = lookupStaadFilename as object;
Object oArg2 = includeFullPath as object;
[Link](ref oArg1, oArg2);
lookupStaadFilename = oArg1 as string;

if ([Link]([Link](), [Link]())
== 0)
{
return osObject;
}
}
}
}
}
}
catch (COMException)
{
throw;
}

return null;
}
Get [Link] instance count
public static int GetSTAADInstanceCount()
{
int count = 0;
try
{
Type type = [Link]("[Link]");
string clsid = [Link]();
string lookUpCandidateName = [Link]("!{0}{1}{2}", "{", clsid, "}").ToUpper();

List<RunningObjectInfo> runningObjects = GetRunningObjectList();


foreach (RunningObjectInfo item in runningObjects)
{
string candidateName = [Link]();
if ([Link](lookUpCandidateName))
{
count++;
}
}
}
catch (COMException)
{
throw;
}
return count;
}

Any [Link] instance running?


public static bool AnySTAADInstanceRunning()
{
try
{
Type type = [Link]("[Link]");
string clsid = [Link]();
string lookUpCandidateName = [Link]("!{0}{1}{2}", "{", clsid, "}").ToUpper();

List<RunningObjectInfo> runningObjects = GetRunningObjectList();


foreach (RunningObjectInfo item in runningObjects)
{
string candidateName = [Link]();
if ([Link](lookUpCandidateName))
{
return true;
}
}
}
catch (COMException)
{
throw;
}
return false;
}

Are multiple [Link] instances running?


public static bool AreMultipleSTAADInstances()
{
int count = 0;
try
{
Type type = [Link]("[Link]");
string clsid = [Link]();
string lookUpCandidateName = [Link]("!{0}{1}{2}", "{", clsid, "}").ToUpper();

List<RunningObjectInfo> runningObjects = GetRunningObjectList();


foreach (RunningObjectInfo item in runningObjects)
{
string candidateName = [Link]();
if ([Link](lookUpCandidateName))
{
count++;
}
}
}
catch (COMException)
{
throw;
}
return (count > 1);
}

Helper Methods
[DllImport("[Link]")]
private static extern int GetRunningObjectTable(int reserved, out
[Link] prot);

[DllImport("[Link]")]
private static extern int CreateBindCtx(int reserved, out [Link]
ppbc);

private class RunningObjectInfo


{
public string Name
{
get;
set;
}

public object Value


{
get;
set;
}
}

private static List<RunningObjectInfo> GetRunningObjectList()


{
try
{
List<RunningObjectInfo> result = new List<RunningObjectInfo>();

IntPtr numFetched = new IntPtr();


[Link] runningObjectTable;
[Link] monikerEnumerator;
[Link][] monikers = new
[Link][1];

GetRunningObjectTable(0, out runningObjectTable);

[Link](out monikerEnumerator);
[Link]();

while ([Link](1, monikers, numFetched) == 0)


{
[Link] ctx;
CreateBindCtx(0, out ctx);

string runningObjectName;
monikers[0].GetDisplayName(ctx, null, out runningObjectName);
object runningObjectVal;
[Link](monikers[0], out runningObjectVal);

RunningObjectInfo objInfo = new RunningObjectInfo


{
Name = runningObjectName,
Value = runningObjectVal
};

[Link](objInfo);
}

return result;
}
catch (Exception)
{
throw;
}
}

Accessing Individual Objects


Example: Accessing Geometry Object

OSGeometryUI _osGeom = _os.Geometry as OSGeometryUI;

Accessing Methods of the Objects


Example: Accesing ‘GetNodeList’ method of Geometry object

public int GetNodeList(ref int[] nodeList)


{
nodeList = null;

try
{
if (_osGeom != null)
{
int count = GetNodeCount();
if (count > 0)
{
nodeList = new int[count];
Object o1 = nodeList as object;
_osGeom.GetNodeList(ref o1);
nodeList = o1 as int[];
}
return count;
}
}
catch (COMException ex)
{
return 0;
}
return 0;
}

Using OpenSTAAD application object (C#) 
Include OpenSTAADUI Library 
1. Right click on ‘Reference’ node and click on ‘Add Re
Connection 
The following section will describe how to connect to the STAAD.Pro application using OpenSTAADUI object. 
Ge
{ 
 
 
 
 
 
if (extStr == ".STD") 
 
 
 
 
 
{ 
 
 
 
 
 
 
// we have a std file opened, but this can be opened by
fileList.Add(staadFilename.ToUpper()); 
 
 
 
 
 
 
 
 
addedFileNames.Add(staadFilename.ToUpper(), "");
// we need to get the staadobject to see if it is opened by STAAD 
 
 
 
 
 
 
 
osObject = item.Value as OpenS
Get STAAD.Pro instance count 
public static int GetSTAADInstanceCount() 
{ 
 
int count = 0; 
 
try 
 
{ 
 
 
Type type = Typ
} 
 
 
} 
 
} 
 
catch (COMException) 
 
{ 
 
 
throw; 
 
} 
 
return false; 
} 
Are multiple STAAD.Pro instances runni
private static extern int CreateBindCtx(int reserved, out System.Runtime.InteropServices.ComTypes.IBindCtx 
ppbc); 
 
private
object runningObjectVal; 
 
 
 
runningObjectTable.GetObject(monikers[0], out runningObjectVal); 
 
 
 
 
RunningObject
nodeList = o1 as int[]; 
 
 
 
} 
 
 
 
return count; 
 
 
} 
 
} 
 
catch (COMException ex) 
 
{ 
 
 
return 0; 
 
}

You might also like