STAAD.Pro Help

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.

Tip: If you followed the previous "Hello World!" example, you may simply delete that line and start from that point.

OS. Initiate OpenSTAAD in C#

Note: You must have a model open in STAAD.Pro for this example. Otherwise your code will return an error.
  1. 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.
  2. 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.
Your program at this point should look like:
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;
            }
        }
    }

OS. Use Geometry Methods

  1. Type OpenSTAADUI.OSGeometryUI geometry = os.Geometry; and then press <Return>.

    This defines the geometry variable as the Geometry group of methods in OpenSTAAD.

  2. 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.

  3. 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.

    Note:
Your program at this point should look like:
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

  1. 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.

  2. 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.

Your final program should look like:
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();
            }
       }
}

OS. Run Your Code

You should now have a working piece of C# code which will provide you information about the currently active STAAD.Pro model. You’ll now run the code to test it.

  1. Save your progress by either:

    selecting File > Save

    or

    pressing <Ctrl+S>

  2. Press <Ctrl+F5> to run your project. The terminal will open and display the results.

It is best practice to annotate your code with comments. This allows others to easily understand what your code is doing (or at least what your intention was). The full example here contains comments accordingly.

Microsoft Visual Studio 2019 running a C# example