Frontend Scripting
OpenTwin supports JavaScript scripting in the user interface.
A script can be provided during application launch via the command line argument --script <file path>.
The provided script will be evaluated just before the LogIn Dialog is shown.
Script usage
The script can access different Objects to react to signals, trigger slots and change properties. The examples showcase the basic usage.
Objects
Object Name |
Class Name |
Availability |
|---|---|---|
app |
Global |
|
views |
Global |
|
ttb |
After Login After a successful login the frontend will create the default UI. This will trigger the AppBase::toolBarAvailable(ToolBar) signal. If it is enusured that the code snipped is executed after the login, then the ToolBar object can directly be used. |
Examples
Automatic Login
The following example demonstrates how to automatically login with the last set user credentials.
1// React to the "logInDialogAvailable" signal of the global "AppBase" object.
2app.logInDialogAvailable.connect(function (dialog) {
3
4 // React to the "dialogShown" signal of the provided dialog object.
5 dialog.dialogShown.connect(function () {
6
7 // Call the "slotLogIn" slot of the dialog.
8 dialog.slotLogIn();
9 });
10});
Open Last Project
The following example demonstrates how to open the latest project.
1// Optionally log-in automatically ...
2
3// After login open the latest project
4app.loginSuccessful.connect(function () {
5 app.slotOpenProjectFromIndex(0);
6});
Switch Project 10 times
The following example demonstrates how to switch the project 10 times.
After the login the script will get the available project names and open the first one.
After project open completed the script will open the next project in the list.
1// Optionally log-in automatically ...
2
3// Declare variables
4var MAX_SWITCHES = 10;
5var projectNameList;
6var currentIndex = -1;
7var projectSwitchCount = 0;
8
9// After login
10app.loginSuccessful.connect(function () {
11 // Refresh the project information
12 app.slotRefreshProjectOverivew();
13
14 // Get the available project names
15 projectNameList = app.getAvailableProjectNames();
16
17 // Open the first project
18 openNextProject();
19});
20
21// After project open completed
22app.servicesUiSetupComplete.connect(function () {
23
24 // Check for project switch limit
25 if (projectSwitchCount >= MAX_SWITCHES) {
26 return;
27 }
28
29 // Increase switch counter and open next project
30 projectSwitchCount++;
31 openNextProject();
32});
33
34// Helper function to open the next project
35function openNextProject() {
36
37 // Ensure we have at least one project in the list
38 if (projectNameList.length==0) {
39 return false;
40 }
41
42 // Increase index and check for wrap around
43 currentIndex++;
44 if (currentIndex >= projectNameList.length) {
45 currentIndex = 0;
46 }
47
48 // Open the next project (name, version)
49 app.slotOpenSpecificProject(projectNameList[currentIndex], "");
50}
Send Message to Service
The following example demonstrates how to send a message to a service via name and via url.
1// After project open completed send ping message
2app.servicesUiSetupComplete.connect(function () {
3 app.appendInfoMessage("Script: Pinging Model service\n");
4
5 var message = "{ \"action\": \"Ping\" }";
6
7 // Send ping message to service by name
8 var result = app.slotSendExecuteMessageToService("Model", message);
9
10 // Display information in output window.
11 if (result.success===true) {
12 app.appendInfoMessage("Script: Ping send successful. Receiver: \"Model\". Response: " + result.response + "\n");
13 }
14 else {
15 app.appendInfoMessage("Script: Ping send failed to service \"Model\"\n");
16 }
17
18 // Send ping message to service by url
19 var result2 = app.slotSendExecuteMessageToUrl("127.0.0.1:8000", message);
20
21 // Display information in output window.
22 if (result2.success===true) {
23 app.appendInfoMessage("Script: Ping send successful. Receiver: 127.0.0.1:8000. Response: " + result2.response + "\n");
24 }
25 else {
26 app.appendInfoMessage("Script: Ping send failed to 127.0.0.1:8000\n");
27 }
28});
Create Material and Select the Item
The following example demonstrates how a ToolBar button can be clicked, a timer can be used to check for changes and select a desired navigation item.
1// Optionally log-in automatically ...
2
3// Optionally automatically open a project ...
4
5// Declare variables
6var TIMER_MATERIAL_SELECT = "Script_MaterialSelect";
7var MAX_SELECTION_ATTEMPTS = 30;
8var currentSelectionAttempt = 0;
9
10// After project open completed trigger the create material logic
11app.servicesUiSetupComplete.connect(function () {
12
13 // If the material does not exist we create a new one
14 if (selectMaterial()===false) {
15
16 // To create a new material we trigger the corresponding button
17 var result = ttb.triggerToolBarButton("Model:Material:Create Material");
18
19 // Check the result of the trigger command
20 if (result===true) {
21 app.appendInfoMessage("Script: Button triggered\n");
22
23 // Run the timer to refresh the selection
24 app.slotRunCustomTimer(TIMER_MATERIAL_SELECT, 100);
25 }
26 else {
27 app.appendInfoMessage("Script: Failed to trigger button\n");
28 }
29 }
30});
31
32// Custom timer timeout
33app.customTimerTimeout.connect(function (timerId) {
34 app.appendInfoMessage("Script: Handling timer timeout \"" + timerId + "\"\n");
35
36 // Check which timer has a timeout
37 if (timerId == TIMER_MATERIAL_SELECT) {
38
39 // Try to select material
40 if (selectMaterial()===false) {
41
42 // Material does not exist, check if we reached the attempts limit
43 if (currentSelectionAttempt < MAX_SELECTION_ATTEMPTS) {
44 currentSelectionAttempt++;
45
46 // Run the timer to refresh the selection
47 app.slotRunCustomTimer(TIMER_MATERIAL_SELECT, 100);
48 }
49 else {
50 // We reached the maximum number of attempts
51 app.appendInfoMessage("Script: Failed to select tree item after " + MAX_SELECTION_ATTEMPTS.toString() + " attempts\n");
52 }
53 }
54 else {
55 currentSelectionAttempt = MAX_SELECTION_ATTEMPTS;
56 }
57 }
58});
59
60// Helper function to select the material. Returns true on success, false otherwise
61function selectMaterial() {
62
63 // The the ID of the desired navigation tree item
64 var treeId = app.findNavigationTreeItemByName("Materials/material1");
65
66 // ID 0 is the invalid ID. In that case we cancel and return false
67 if (treeId===0) {
68 return false;
69 }
70 else {
71 // Clear the current selection
72 app.clearNavigationTreeSelection();
73
74 // Select the desired item by its ID
75 app.setSingleNavigationTreeItemSelected(treeId, true);
76
77 return true;
78 }
79}