Skip to main content

STAAD.Pro

Introduction

This guide provides detailed instructions for setting up the VIKTOR worker with STAAD.Pro. The steps outlined are similar to those used for other structural analysis software. It is designed to offer the necessary context and file templates to facilitate the integration process.

The worker

A worker is a program that connects the VIKTOR platform to third-party software running outside the platform. You can install the worker on your local machine or a remote server, but STAAD.Pro must be installed where the worker is located. Your VIKTOR app will handle the communication between the web app and the worker as shown in the following diagram:

worker

Here is what happens during the integration:

  1. The user enters input parameters in the VIKTOR UI.
  2. The app.py in the VIKTOR app sends these parameters to the worker.
  3. The worker (running on your local machine or a virtual machine) creates the model using the input parameters in STAAD.Pro.
  4. The worker sends back the result of the calculation to your VIKTOR app.
  5. VIKTOR UI displays the result of the calculation.

Install a generic worker

Follow these steps to install the worker:

  1. Navigate to the "My Integrations" tab in your personal settings.

  2. Click "Add integration."

  3. Follow the steps provided in the modal.

    Integration

    3.1. Select Generic.

    3.2. Download the worker .msi (Microsoft Installer) and run it on the machine of choice.

    3.3. Copy the generated connection key and paste it when the installer asks for it. In the browser, you can now click Finish. Continue the installation in the installer wizard.

    CONNECTION KEY

    The generated connection key should be copied immediately as VIKTOR will not preserve this data for security reasons.

  4. In the installer wizard, select the Python executable of your choice. This can be your system Python or the python.exe in a dedicated virtual environment.

tip

Your default Python environment can usually be found in:
C:\Users\Username\AppData\Local\Programs\Python\Python31X\python.exe
If you cannot find it, try running the following command in your terminal:
where python
Do not use a Python path that contains venv anywhere in the path.

  1. Make sure to launch and test the worker once the installation is finished. If you closed the worker, you can restart it through the desktop shortcut you created during the installation.

Set up the worker

The worker will control STAAD.Pro through a Python environment where pywin32, comtypes, and openstaad need to be installed. This environment can be on your local Windows machine or a remote server where STAAD.Pro is installed with a valid license. Make sure to use Python version 3.9 or higher. For this integration, we will set up the worker in a separate folder from the VIKTOR app, simulating a remote setup.

Step 1: Create the worker directory

Create a new folder named StaadServer to store the worker files.

working directory

Installing the worker on the same machine as your app? Make this folder somewhere different from your app. We recommend navigating to 'C:' in your windows file explorer to experience similar behaviour as connecting to a server running STAAD.Pro.

Step 2: Set up Python and install dependencies

  1. Ensure that the correct Python environment is used to execute the code, and keep the path at hand, as the worker will need it to run Python code for the STAAD.Pro API.
  2. Now, install the necessary libraries ('pywin32, comtypes, and openstaad) using pip in the identified Python environment:
pip install pywin32 comtypes openstaad 

This Python environment will be used to execute all Python code that involves working with the STAAD.Pro API. Let’s proceed with the YAML configuration file.

Step 3: YAML file configuration

  1. Navigate to the worker installation directory. By default, this is located at:

    C:\Program Files\VIKTOR\

  2. Open the config.yaml file and update it with your actual Python path (the one you kept from step 1). Complete the rest of the YAML configuration for STAAD.Pro as shown below.

executables: 
run_staad:
path: 'C:\Users\<YourUsername>\AppData\Local\Programs\Python\PythonXX\'
arguments:
- 'run_staad_model.py'
workingDirectoryPath: 'C:\StaadServer'
maxParallelProcesses: 1

Note that in the arguments value, we add a Python file called run_staad_model.py. This file will be located in your VIKTOR app folder, and we will push it to the workingDirectoryPath to be executed.

  1. Save the config.yaml file.

  2. Optionally, restart the worker from the task scheduler.

Connecting your app to the worker

You can use the following app.py template to set up the communication with your worker. In the current state you will need to add some code to the 'Parametrization' class as well as functions to generate 'inputs.json'. The code to generate 'output.json' needs to be added to the 'run_staad_model.py' file (see next section). If you would like an example of these feel free to follow our STAAD.Pro tutorial.

from viktor.external.generic import GenericAnalysis 
from viktor.core import File
from pathlib import Path

class Parametrization(vkt.Parametrization):
# add your parameters here

class Controller(vkt.Controller):
...
# Add a function inside the Controller to create an `inputs.json` file.

def run_staad(self, params, **kwargs):
inputs_path = Path(__file__).parent / "inputs.json"
script_path = Path(__file__).parent / "run_staad_model.py"

files = [
("inputs.json", File.from_path(inputs_path)),
("run_staad_model.py", File.from_path(script_path))
]

# Note that executable_key matches the config.yaml of the worker (run_staad)
generic_analysis = GenericAnalysis(
files=files,
executable_key="run_staad",
output_filenames=["output.json"]
)

generic_analysis.execute(timeout=300)
output_file = generic_analysis.get_output_file("output.json", as_file=True)

# Further logic to handle the output.json data

The following template can be used to define the content of your run_staad_model.py to communicate with the worker and your VIKTOR app:

# run_staad_model.py 
import json
from pathlib import Path

def create_staad_model():
"""
1. Your code reads the inputs.json file and
uses the content for structural analysis in STAAD.Pro.
"""

input_json = Path.cwd() / "inputs.json"
with open(input_json) as jsonfile:
data = json.load(jsonfile)

"""
2. Add your logic here for the analysis
using the STAAD.Pro API, possibly through openstaad.
"""

outputs = {
"some_result": 123 # Example placeholder data
}

"""
3. Store the results of your analysis
in the output.json file to be sent to
your VIKTOR app.
"""

output_file = Path.cwd() / "output.json"
with open(output_file, "w") as jsonfile:
json.dump(outputs, jsonfile)

create_staad_model()

Conclusion

By following this guide, you have learned how to set up your worker for use with STAAD.Pro. With the provided templates, you are ready to integrate STAAD.Pro into your VIKTOR app and automate design and analysis tasks. Want to dive right in and build a complete STAAD.Pro automation? Follow our STAAD.Pro tutorial.