Scripting Environments
By convention, the extension of the filename determines the language to be used.
Languages and extensions
Extension | Language |
---|---|
.m | MATLAB |
.vbs | VB Script |
.py | Python |
The name of the function that is called by a plugin script is based on the filename. For example, the name of the function called when using the filename
C:\stk\user1\VB_ForceModel_Eval.vbs is VB_ForceModel_Eval.
This convention is the same as the MATLAB convention for dealing with user-defined functions (i.e., .m files). This convention was chosen to avoid name collisions, since MATLAB and VBScript do not understand the concept of namespaces.
MATLAB environment
Only one copy of MATLAB is opened by STK for use with plugin scripts. All MATLAB plugin scripts share the same workspace environment. STK does not connect to MATLAB until some MATLAB plugin script attempts to execute a script. This may occur during application startup or when loading scenario files, depending on the plugin point being used.
MATLAB plugin script locations
When the MATLAB Engine is opened, STK plugin script folders are added to the MATLAB path. However, if you add a new folder after the MATLAB engine has initialized, that folder will not be added to the MATLAB path. You can add the newly created folder to the MATLAB path in one of two ways:
- First use the MATLAB path command to determine the current scripting folder hierarchy, and then use the MATLAB addpath command to manually add the folder.
- Go to the STK Edit menu->Preferences->MATLAB window and use the Open MATLAB/Close MATLAB button to first close and then open the MATLAB Engine. The folder will automatically be added to the MATLAB path.
Sharing global variables
In MATLAB plugin scripts, you can share global variables, as shown in the following example:
global frequency;
Declaring a variable using the global keyword allows it to be used in other scripts.
VBScript environment
Only one copy of a VBScript engine is opened by STK for use with VBScript plugin scripts. All VBScript plugin scripts share the same workspace environment. STK does not create a VBScript environment until some VBScript plugin script attempts to execute a script. This may occur during application startup or when loading scenario files, depending on the plugin point being used.
As part of the creation process, all VBScript files (i.e., files with extension .vbs) within certain directories are automatically loaded into the workspace. You may add your own VBScript files to these directories and they will be automatically loaded as well. The directories are:
- <STK install folder>/STKData/Scripting/Init
- <STK user area>/Config/Scripting/Init
- <STK all users area>/STKData/Scripting/Init
where <STK install folder> refers to the STK installation directory, <STK user area> refers to the STK User's directory, and <STK all users area> refers to the directory that contains STK application data for all users. STK will send a success or failure message for each of the files found and loaded.
This mechanism enables you to create certain VBScript utilities that other VBScript scripts may utilize, since there is no internal mechanism in VBScript to load and execute another script.
Python Environment
STK creates a Python environment the first time that a Python plugin script is executed, which may not be when you first open the application. STK uses a single environment for all Python scripts that are executed during the application session.
Python Directories in STK
When STK creates a Python environment, all Python files (i.e., files with the extension, .py) in three of its directories are automatically loaded into the workspace. You can add your own Python files to these directories so that they are also automatically loaded whenever STK creates a Python environment. The directories are:
- <STK install folder>/STKData/Scripting/Init
- <STK user area>/Config/Scripting/Init
- <STK all users area>/STKData/Scripting/Init
<STK install folder> is the directory path where you installed STK. <STK user area> and <STK all users area> are your configuration directories. STK will send a success or failure message to the Message Viewer for each of the files that is found and loaded.