OS. Notes on Using C# with OpenSTAAD
While getting the OpenSTAAD object by doing:
OpenSTAADUI.OpenSTAAD os = Marshal.GetActiveObject("StaadPro.OpenSTAAD") as OpenSTAADUI.OpenSTAAD;
using 'Staadpro.Openstaad'
will give you access to the running instance of
STAAD.Pro which was first started.
If there are multiple instances of STAAD.Pro open, and you want access to an instance then use the following code:
public static OpenSTAADUI.OpenSTAAD GetStaadUiInterface (string staadFilename)
{
string lookUpCandidateName = RotMethods.GetStaadProCandidateNameToLookUpInRot();
if ( string.IsNullOrEmpty(lookUpCandidateName) )
{
return null;
}
if ( string.IsNullOrEmpty(staadFilename) )
{
return null;
}
string staadFileFullPath = Path.GetFullPath(staadFilename).ToUpper();
List<RotMethods.RotObjectInfo> runningObjects = RotMethods.GetRunningObjectList();
foreach ( RotMethods.RotObjectInfo item in runningObjects )
{
string candidateName = item.Name.ToUpper();
try
{
// if this call succeeds, then it is a valid path, else it will continue
string extStr = Path.GetExtension(candidateName).ToUpper();
{
OpenSTAADUI.OpenSTAAD osObject = null;
if ( !string.IsNullOrEmpty(extStr) )
{
// check if it’s a valid STAAD file
if ( extStr == ".STD" )
{
osObject = item.Value as OpenSTAADUI.OpenSTAAD;
if ( osObject != null )
{
// get staad filename
string openedStaadFile = GetOpenedStaadFilename(osObject).ToUpper();
if ( string.Compare(staadFileFullPath, openedStaadFile, StringComparison.OrdinalIgnoreCase) == 0 )
{
return osObject;
}
}
}
}
if ( candidateName.StartsWith(lookUpCandidateName) )
{
// we got the staad instance
osObject = item.Value as OpenSTAADUI.OpenSTAAD;
if ( osObject != null )
{
// get staad filename
string lookupStaadFilename = GetOpenedStaadFilename(osObject);
//
if ( string.Compare(staadFileFullPath, lookupStaadFilename, StringComparison.OrdinalIgnoreCase) == 0 )
{
return osObject;
}
}
}
}
}
catch ( Exception ex )
{
throw;
}
}
return null;
}
private static string GetOpenedStaadFilename (OpenSTAADUI.OpenSTAAD osObject)
{
// get staad filename
string staadFilename = "";
bool includeFullPath = true;
object oArg1 = staadFilename as object;
object oArg2 = includeFullPath as object;
osObject.GetSTAADFile(ref oArg1, oArg2);
staadFilename = oArg1 as string;
if ( staadFilename != null )
return Path.GetFullPath(staadFilename);
//
return null;
}
}
Call the GetStaadUiInterfaceInternal (string staadFilename)
method
as
OpenSTAADUI.OpenSTAAD os = GetStaadUiInterface (@"C:\Test.std");
The following helper methods should be added:
[DllImport("ole32.dll")]
private static extern int GetRunningObjectTable (int reserved, out IRunningObjectTable prot);
[DllImport("ole32.dll")]
private static extern int CreateBindCtx (int reserved, out IBindCtx ppbc);
public class RotObjectInfo
{
public string Name
{
get;
set;
}
public object Value
{
get;
set;
}
}
public static List<RotObjectInfo> GetRunningObjectList ()
{
try
{
var result = new List<RotObjectInfo>();
var numFetched = new IntPtr();
IRunningObjectTable runningObjectTable;
IEnumMoniker monikerEnumerator;
var monikers = new IMoniker[1];
GetRunningObjectTable(0, out runningObjectTable);
runningObjectTable.EnumRunning(out monikerEnumerator);
monikerEnumerator.Reset();
while ( monikerEnumerator.Next(1, monikers, numFetched) == 0 )
{
IBindCtx ctx;
CreateBindCtx(0, out ctx);
string runningObjectName;
monikers[0].GetDisplayName(ctx, null, out runningObjectName);
object runningObjectVal;
runningObjectTable.GetObject(monikers[0], out runningObjectVal);
var objInfo = new RotObjectInfo
{
Name = runningObjectName,
Value = runningObjectVal
};
result.Add(objInfo);
}
return result;
}
catch ( Exception )
{
throw;
}
}