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. Create and add a manifest file to your C++ project:
    1. Create a new file and save it somewhere convenient with a .manifest file extension.
    2. Right-click on your project from within Visual Studio and then select Add > Existing Item from the pop-up menu.
    3. Browse to the manifest file and select it.
    4. Open the manifest file and add the following contents:
      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
      	<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
      		<security>
      			<requestedPrivileges>
      				<requestedExecutionLevel level="asInvoker" uiAccess="false"></requestedExecutionLevel>
      			</requestedPrivileges>
      		</security>
      	</trustInfo>
      	<comInterfaceExternalProxyStub name="IOSMemberSteelDgnParams" iid="{F40BDCDA-B3DE-495C-B84F-790F4456137F}" tlbid="{EDA9FA7F-EFC9-4264-9513-39CF6E72604D}" proxyStubClsid32="{00020420-0000-0000-C000-000000000046}"></comInterfaceExternalProxyStub>
      	<comInterfaceExternalProxyStub name="IOpenSTAADUI" iid="{3F5B8055-31C6-446E-8BED-FEE43E09D4CC}" tlbid="{EDA9FA7F-EFC9-4264-9513-39CF6E72604D}" proxyStubClsid32="{00020420-0000-0000-C000-000000000046}"></comInterfaceExternalProxyStub>
      	<comInterfaceExternalProxyStub name="IOSGeometryUI" iid="{C052FED9-A2D6-42E3-A271-2C6FB8461711}" tlbid="{EDA9FA7F-EFC9-4264-9513-39CF6E72604D}" proxyStubClsid32="{00020420-0000-0000-C000-000000000046}"></comInterfaceExternalProxyStub>
      	<comInterfaceExternalProxyStub name="IStaadProWindow" iid="{9EF2FF8C-E574-4A04-9462-2E4500C8EADB}" tlbid="{EDA9FA7F-EFC9-4264-9513-39CF6E72604D}" proxyStubClsid32="{00020420-0000-0000-C000-000000000046}"></comInterfaceExternalProxyStub>
      	<comInterfaceExternalProxyStub name="IOSViewUI" iid="{87B1975B-6031-487E-A0F9-FB8F69FA24E6}" tlbid="{EDA9FA7F-EFC9-4264-9513-39CF6E72604D}" proxyStubClsid32="{00020420-0000-0000-C000-000000000046}"></comInterfaceExternalProxyStub>
      	<comInterfaceExternalProxyStub name="IOSOutputUI" iid="{824F1FC0-DC86-4CC4-A4C6-83C77D7B0496}" tlbid="{EDA9FA7F-EFC9-4264-9513-39CF6E72604D}" proxyStubClsid32="{00020420-0000-0000-C000-000000000046}"></comInterfaceExternalProxyStub>
      	<comInterfaceExternalProxyStub name="IOSPropertyUI" iid="{F919EF7D-E1DD-48CB-B3C3-B3CAE9E3B5AB}" tlbid="{EDA9FA7F-EFC9-4264-9513-39CF6E72604D}" proxyStubClsid32="{00020420-0000-0000-C000-000000000046}"></comInterfaceExternalProxyStub>
      	<comInterfaceExternalProxyStub name="IOSLoadUI" iid="{DAA37D16-821F-4137-88EB-DA4EB7650E90}" tlbid="{EDA9FA7F-EFC9-4264-9513-39CF6E72604D}" proxyStubClsid32="{00020420-0000-0000-C000-000000000046}"></comInterfaceExternalProxyStub>
      	<comInterfaceExternalProxyStub name="IOSTableUI" iid="{CF1A7B89-A007-4844-A098-CBABAEBEF304}" tlbid="{EDA9FA7F-EFC9-4264-9513-39CF6E72604D}" proxyStubClsid32="{00020420-0000-0000-C000-000000000046}"></comInterfaceExternalProxyStub>
      	<comInterfaceExternalProxyStub name="IOSSupportUI" iid="{B076EC62-9A33-4E1C-B38C-3E45090B8531}" tlbid="{EDA9FA7F-EFC9-4264-9513-39CF6E72604D}" proxyStubClsid32="{00020420-0000-0000-C000-000000000046}"></comInterfaceExternalProxyStub>
      	<comInterfaceExternalProxyStub name="IOSCommandsUI" iid="{6B994A7C-122C-4828-B42B-1B0CADF91F9D}" tlbid="{EDA9FA7F-EFC9-4264-9513-39CF6E72604D}" proxyStubClsid32="{00020420-0000-0000-C000-000000000046}"></comInterfaceExternalProxyStub>
      	<comInterfaceExternalProxyStub name="IOSDesignUI" iid="{ED218322-40EE-411C-8552-209DB2C4F32B}" tlbid="{EDA9FA7F-EFC9-4264-9513-39CF6E72604D}" proxyStubClsid32="{00020420-0000-0000-C000-000000000046}"></comInterfaceExternalProxyStub>
      </assembly>
  2. Import the StaadPro.dll by typing: #import "C:\Program Files\Bentley\Engineering\STAAD.Pro CONNECT Edition\STAAD\StaadPro.dll" named_guids and then press <Return>.
  3. Set the namespace by typing namespace OSApp = OpenSTAADUI; and then press <Return>. This namespace provides a wide variety of members that support COM interop and platform invoke services.
  4. Place the cursor inside the int main().
  5. Add the following lines:
    OSApp::IOpenSTAADUIPtr OSAppPtr;
    CoInitialize(NULL);
    HRESULT hr = OSAppPtr.CreateInstance(__uuidof(OSApp::OpenSTAAD));
    if (hr != S_OK)
    	{
    	printf("Unable to connect to STAAD.Pro application!\n");
    	return 1;
    	}
    
    hr = OSAppPtr.GetActiveObject(__uuidof(OSApp::OpenSTAAD));
    This initiates OpenSTAAD and connects to the current STAAD.Pro model to your program.

    CreateInstance creates an uninitialized object and GetActiveObject retrieves a pointer to a running object that has been registered with OLE.

Your program at this point should look like:
#import "C:\Program Files\Bentley\Engineering\STAAD.Pro CONNECT Edition\STAAD\StaadPro.dll" named_guids
using namespace OpenSTAADUI;
using namespace std;
namespace OSApp = OpenSTAADUI;   
 
    int main()
{
	OSApp::IOpenSTAADUIPtr OSAppPtr;
		try
			{
			CoInitialize(NULL);
			HRESULT hr = OSAppPtr.CreateInstance(__uuidof(OSApp::OpenSTAAD));

			if (hr != S_OK)
				{
				printf("Unable to connect to STAAD.Pro application!\n");
				return 1;
				}

			hr = OSAppPtr.GetActiveObject(__uuidof(OSApp::OpenSTAAD));
			if (hr != S_OK)
			{
				printf("Unable to connect to an active instance of STAAD.Pro application!\n");
			return 1;
			}
}
catch (_com_error& e)
		{
		_bstr_t bstrDescription = e.Description();
		printf("COM Error! %s\n", bstrDescription);
		}
}

OS. Use Geometry Methods

  1. At the beginning of the main() - on a new line after OSApp::IOpenSTAADUIPtr OSAppPtr;, type OSApp::IOSGeometryUIPtr OSGeomPtr; and then press <Return>.
  2. Place your cursor just inside the closing curly bracket ("}") of the main() and then press <Return>.
  3. Type OSGeomPtr = OSAppPtr->GetGeometry(); and then press <Return>.

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

  4. Type int nNodes = OSGeomPtr->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.

  5. Type int nMembers = OSGeomPtr->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:
#include "pch.h"
#include <iostream>
#import "C:\Program Files\Bentley\Engineering\STAAD.Pro CONNECT Edition\STAAD\StaadPro.dll" named_guids 

using namespace OpenSTAADUI;
using namespace std;

namespace OSApp = OpenSTAADUI;
int main()
    {
    OSApp::IOpenSTAADUIPtr OSAppPtr;
    OSApp::IOSGeometryUIPtr OSGeomPtr;

    try
     {
 CoInitialize(NULL);	
        HRESULT hr = OSAppPtr.CreateInstance(__uuidof(OSApp::OpenSTAAD));

        if (hr != S_OK)
            {
            printf("Unable to connect to STAAD.Pro application!\n");
            return 1;
            }

        hr = OSAppPtr.GetActiveObject(__uuidof(OSApp::OpenSTAAD));
        if (hr != S_OK)
            {
            printf("Unable to connect to an active instance of STAAD.Pro application!\n");
            return 1;
            }

        OSGeomPtr = OSAppPtr->GetGeometry();

        int nNodes = OSGeomPtr->GetNodeCount();
        int nMembers = OSGeomPtr->GetMemberCount();
        }
    catch (_com_error& e)
        {
        _bstr_t bstrDescription = e.Description();
        printf("COM Error! %s\n", bstrDescription);
        }
    }

OS. Generate OpenSTAAD Output

  1. Type printf("No. of Nodes: %d\n", nNodes); and then press <Return>.

    We are using printf to print the formatted data to stdout. The number of nodes is printed in a readable format to the console..

  2. Type printf("No. of Members: %d\n", nMembers);.

    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:
#include "pch.h"
#include <iostream>
#import "C:\Program Files\Bentley\Engineering\STAAD.Pro CONNECT Edition\STAAD\StaadPro.dll" named_guids 

using namespace OpenSTAADUI;
using namespace std;

namespace OSApp = OpenSTAADUI;
int main()
   {
    OSApp::IOpenSTAADUIPtr OSAppPtr;
    OSApp::IOSGeometryUIPtr OSGeomPtr;

    try
     {
 CoInitialize(NULL);	
        HRESULT hr = OSAppPtr.CreateInstance(__uuidof(OSApp::OpenSTAAD));

        if (hr != S_OK)
            {
            printf("Unable to connect to STAAD.Pro application!\n");
            return 1;
            }

        hr = OSAppPtr.GetActiveObject(__uuidof(OSApp::OpenSTAAD));
        if (hr != S_OK)
            {
            printf("Unable to connect to an active instance of STAAD.Pro application!\n");
            return 1;
            }

        OSGeomPtr = OSAppPtr->GetGeometry();

        int nNodes = OSGeomPtr->GetNodeCount();
        int nMembers = OSGeomPtr->GetMemberCount();
	    printf("No. of Nodes: %d\n", nNodes);
	    printf("No. of Members: %d\n", nMembers);
        }
    catch (_com_error& e)
        {
        _bstr_t bstrDescription = e.Description();
        printf("COM Error! %s\n", bstrDescription);
        }
    }

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