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++
-
Create and add a manifest file to your C++ project:
- Create a new file and save it somewhere convenient with a .manifest file extension.
- Right-click on your project from within Visual Studio and then select from the pop-up menu.
- Browse to the manifest file and select it.
-
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>
- Import the StaadPro.dll by typing: #import " C:\Program Files\Bentley\Engineering\STAAD.Pro 2023\ STAAD\StaadPro.dll" named_guids and then press <Return>.
- 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.
- Place the cursor inside the int main().
-
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.
#import "
C:\Program
Files\Bentley\Engineering\STAAD.Pro 2023\
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
- At the beginning of the main() - on a new line after OSApp::IOpenSTAADUIPtr OSAppPtr;, type OSApp::IOSGeometryUIPtr OSGeomPtr; and then press <Return>.
- Place your cursor just inside the closing curly bracket ("}") of the main() and then press <Return>.
-
Type OSGeomPtr = OSAppPtr->GetGeometry(); and then press
<Return>.
This defines the geometry variable as the Geometry group of methods in OpenSTAAD.
-
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.
-
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.
Tip: Reference the OpenSTAAD documentation for details on available methods and what values are accepted as input or returned from each.
#include "pch.h"
#include <iostream>
#import "C:\Program Files\Bentley\Engineering\STAAD.Pro 2023\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
-
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..
-
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.
#include "pch.h"
#include <iostream>
#import "
C:\Program
Files\Bentley\Engineering\STAAD.Pro 2023\
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);
}
}