OS. Write an OpenSTAAD Program in C#
This example will introduce you to several concepts necessary to writing OpenSTAAD applications in C#.
The example C# project for Visual Studio 2019 can be downloaded here.
OS. Initiate OpenSTAAD in C#
- Add a namespace by typing: using System.Runtime.InteropServices; This namespace provides a wide variety of members that support COM interop and platform invoke services.
- Type OpenSTAADUI.OpenSTAAD os = Marshal.GetActiveObject("StaadPro.OpenSTAAD") as OpenSTAADUI.OpenSTAAD; and then press <Return>. This initiates OpenSTAAD and connects to the current STAAD.Pro model to your program.
OS. Use Geometry Methods
-
Type OpenSTAADUI.OSGeometryUI geometry =
os.Geometry; and then press <Return>.
This defines the geometry variable as the Geometry group of methods in OpenSTAAD.
-
Type int nodeCount =
geometry.GetNodeCount(); and then press <Return>.
This uses the OpenSTAAD method GetNodeCount() to return the number of nodes in the active STAAD.Pro model into the specified variable.
-
Type int beamCount =
geometry.GetMemberCount(); and then press <Return>.
Similarly, this uses the OpenSTAAD method GetMemberCount() to return the number of members in the active STAAD.Pro model into the specified variable.
Tip: Reference the OpenSTAAD documentation for details on available methods and what values are accepted as input or returned from each.
using System;
using System.Runtime.InteropServices;
namespace OSEOMConsoleApp
{
class Program
{
static void Main (string[] args)
{
OpenSTAADUI.OpenSTAAD os = Marshal.GetActiveObject("StaadPro.OpenSTAAD") as OpenSTAADUI.OpenSTAAD;
OpenSTAADUI.OSGeometryUI geometry = os.Geometry;
int nodeCount = geometry.GetNodeCount();
int beamCount = geometry.GetMemberCount();
}
}
}
OS. Generate OpenSTAAD Output
-
Type Console.WriteLine("Node Count = " +
nodeCount); and then press <Return>.
The numerical nodeCount value is concatenated with some string to print in a readable format.
-
Type Console.WriteLine("Beam Count = " +
beamCount);.
Similarly, this is concatenating the beamCount value, converted to a string, with some text and displaying that result to the terminal.
using System;
using System.Runtime.InteropServices;
namespace OSEOMConsoleApp
{
class Program
{
static void Main (string[] args)
{
OpenSTAADUI.OpenSTAAD os = Marshal.GetActiveObject("StaadPro.OpenSTAAD") as OpenSTAADUI.OpenSTAAD;
OpenSTAADUI.OSGeometryUI geometry = os.Geometry;
int nodeCount = geometry.GetNodeCount();
int beamCount = geometry.GetMemberCount();
Console.WriteLine("Node Count = " + nodeCount);
Console.WriteLine("Beam Count = " + beamCount);
Console.ReadLine();
}
}
}