Application Tasks
Of all possible externally generated tasks, this page addresses only general tasks pertaining to the Ansys Orbit Determination Tool Kit (ODTK®) application as a whole. It does not include any tasks concerning objects or attributes.
Use the following links for descriptions and code samples for these spcific tasks:
- Connecting to the ODTK application
- Disconnecting from the ODTK application
- Preference files
- Setting a custom location for the ODTK log file
- Using ODTKWriteMessages() function
- Closing child windows
- Custom scripts for event handling
- Perform date time and units conversion
Connecting to the ODTK application
There are three common modes for interacting with the ODTK application. The final result of each of them is that they provide a handle to the root object in the ODTK application. Standard automation server properties are also available either to make the application invisible or to disable user control while the script is running. "ODTK.Application" refers to the most recently installed version of the ODTK application. Alternatively, you can specify a version number, such as "ODTK13.Application".
1. To start a new instance of the ODTK application:
COM
set app = CreateObject("ODTK.Application")
set ODTK = app.Personality
app.visible = true
app.UserControl = true
var app = new ActiveXObject("ODTK.Application");
var ODTK = app.personality;
app.visible = true;
app.UserControl = true;
my $app = Win32::OLE->new("ODTK.Application", sub {$_[0]->Quit;})
my $ODTK = $app->personality;
$app->{Visible} = 1;
$app->{UserControl} = 1;
app = w32c.Dispatch("ODTK.Application")
ODTK = app.Personality
app.Visible = True
app.UserControl = True
app = actxserver( 'ODTK.Application' );
odtk = app.get('Personality');
app.visible = true;
app.usercontrol = true;
2. To attach to an already running instance of the ODTK application:
COM
3. To embed a script inside an HTML page and load that page into the ODTK Web Browser:
COM
Cross Platform API
Disconnecting from the ODTK application
To disconnect from the application, release all object handles in the order in which they were set. Then, unless you set UserControl=true, the application will shut down. If you want to immediately reopen the application, it may be necessary to "sleep" and allow the shutdown procedure to complete before opening another instance of the ODTK application, as shown in the following examples:
COM
set ODTK = Nothing ' Release ODTK root object
set app = Nothing ' Release application GUI handle
wscript.sleep(5000) ' Wait 5 seconds while the ODTK application terminates
ODTK = undefined;
app = undefined;
var WshShell = WScript.CreateObject("WScript.Shell");
WScript.Sleep(5000);
Preference files
Access to user Preference files is optional.
odtk.release;
ePrefFiles_NoLoad_NoSave = 0,
ePrefFiles_Load_NoSave = 1,
ePrefFiles_Load_And_Save = 2
These control load the Application and User Preferences when Personality is loaded/initialized and saves the User Preference files upon exiting the application. User Preference files are usually found in user home/Documents/STK_ODTK 13/Config.
Example:
uiApp = CreateObject("ODTK13.Automation") // or "ODTK13.Engine"
uiApp.Visible = false
uiApp.UserControl - false
uiApp.PrefFilesMode = AgEPrefFilesMode.ePrefFiles_Load_No_Save
ODTK = uiApp.LoadPersonality("ODTK")
The default value for ODTK13.Application and ODTK13.Automation is ePrefFiles_Load_And_Save, and the default mode for ODTK13.Engine is ePrefFile_Load_No_Save.
If you set the UserControl property to true, it enables saving of preference files, formats, and MRU file lists. A setting of "false" disables this, but still enables you interact with the application, though the ODTK application will not display any message boxes.
Setting a custom location for the ODTK log file
By default, the ODTK application will create its log in a user temp directory and delete it upon application exit. However, you may override this by using the following commands:
COM
ForWriting = 2 ' Create new or truncate existing file
ForAppending = 8 ' Create new or append to an existing file
MsgDebug = 0
MsgInfo = 1
MsgForceInfo = 2
MsgWarning = 3
MsgError = 4
set uiApp = WScript.CreateObject("ODTK.Automation")
uiApp.OpenLogFile "c:\temp\log1.txt", ForAppending
uiApp.LogMsg MsgWarning, "Warning: LogFile = " & uiApp.LogFile
uiApp.LoadPersonality "ODTK"
set ODTK = uiApp.Personality
uiApp.LogMsg MsgInfo, ODTK.NewDate(1,"JDate").Format("UTCG")
set ODTK = Nothing
set uiApp = Nothing
var ForWriting = 2; // Create new or truncate existing file
var ForAppending = 8; // Create new or append to an existing file
var MsgDebug = 0;
var MsgInfo = 1;
var MsgForceInfo = 2;
var MsgWarning = 3;
var MsgError = 4;
var uiApp = WScript.CreateObject("ODTK.Automation");
uiApp.OpenLogFile("c:\\temp\\log1.txt", ForAppending);
uiApp.LogMsg(MsgWarning, "Warning: LogFile = " + uiApp.LogFile);
uiApp.LoadPersonality("ODTK");
var ODTK = uiApp.Personality;
uiApp.LogMsg(MsgInfo, ODTK.NewDate(1,"JDate").Format("UTCG"));
ODTK = null;
uiApp = null;
ForWriting = 2 # Create new or truncate existing file
ForAppending = 8 # Create new or append to an existing file
MsgDebug = 0
MsgInfo = 1
MsgForceInfo = 2
MsgWarning = 3
MsgError = 4
uiApp = w32c.Dispatch("ODTK.Automation")
uiApp.OpenLogFile("c:\\Temp\\log1.txt", ForAppending)
uiApp.LogMsg(MsgWarning, "Warning: LogFile = " + uiApp.LogFile)
uiApp.LoadPersonality("ODTK")
ODTK = uiApp.Personality
uiApp.LogMsg(MsgInfo, ODTK.NewDate(1,"JDate").Format("UTCG"))
ODTK = None
Cross-Platform API
# from pathlib import Path
# Setting a custom location for the ODTK log file
# By default, the ODTK application will create its log in a user temp directory and delete it upon application exit.
# However, you may override this by using the following commands:
appendMode = True
log_file_path = Path(this_dir_path) / "log.txt"
odtk.Application.SetLogFile(str(log_file_path), appendMode)
log_level_debug = 0
log_level_info = 1
log_level_force_info = 2
log_level_warning = 3
log_level_error = 4
odtk.Application.LogMsg(log_level_info, f"LogFile = {odtk.Application.LogFile.eval()}")
odtk.Application.LogMsg(log_level_warning, "This is a warning message.")
odtk.Application.LogMsg(log_level_error, "This is an error message.")
% Setting a custom location for the ODTK log file
% By default, the ODTK application will create its log in a user temp directory and delete it upon application exit.
% However, you may override this by using the following commands:
appendMode = true;
logfilePath = [thisFolderPath, filesep, 'log.txt'];
odtk.Application.SetLogFile(logfilePath, appendMode);
logLevelDebug = 0;
logLevelInfo = 1;
logLevelForceInfo = 2;
logLevelWarning = 3;
logLevelError = 4;
odtk.Application.LogMsg(logLevelInfo, "LogFile = " + odtk.Application.LogFile);
odtk.Application.LogMsg(logLevelWarning, "This is a warning message.");
odtk.Application.LogMsg(logLevelError, "This is an error message.");
// Setting a custom location for the ODTK log file
// By default, the ODTK application will create its log in a user temp directory and delete it upon application exit.
// However, you may override this by using the following commands :
const auto appendMode = true;
const std::string logFilePath = thisFolderPath + s_pathSeparator + "log.txt";
odtk("Application")("SetLogFile").Invoke(logFilePath, appendMode);
const auto logLevelDebug = 0;
const auto logLevelInfo = 1;
const auto logLevelForceInfo = 2;
const auto logLevelWarning = 3;
const auto logLevelError = 4;
odtk("Application")("LogMsg").Invoke(logLevelInfo, "LogFile = " + odtk("Application")("LogFile").AsString());
odtk("Application")("LogMsg").Invoke(logLevelWarning, "This is a warning message.");
odtk("Application")("LogMsg").Invoke(logLevelError, "This is an error message.");
// Setting a custom location for the ODTK log file
// By default ODTK will create its log in a user temp directory and delete it upon application exit.
// However, you may override this by using the following commands :
public enum LogLevel { DEBUG, INFO, FORCEINFO, WARNING, ERROR }
boolean appendMode = true;
String logFilePath = thisFolderPath + Client.PATH_SEPARATOR + "log.txt";
odtk.get("Application.SetLogFile").invoke(logFilePath, appendMode);
odtk.get("Application.LogMsg").invoke(LogLevel.INFO.ordinal(), "LogFile = " + odtk.get("Application.LogFile").asString());
odtk.get("Application.LogMsg").invoke(LogLevel.WARNING.ordinal(), "This is a warning message.");
odtk.get("Application.LogMsg").invoke(LogLevel.ERROR.ordinal(), "This is an error message.");
Using ODTK.WriteMessage() function
In addition to the application level uiApp.LogMsg() function common to all AGI products, the ODTK application implements an additional ODTK.WriteMessage() function. This provides the ability to generate messages from ODTK application-specific event handler scripts described below as well as adding more debugging functionality for scripts.
COM
ODTK.WriteMessage "Example error message...", "error"
ODTK.WriteMessage "Example warning message...", "warn"
ODTK.WriteMessage "Example forced message...", "force"
ODTK.WriteMessage "Example info message...", "info"
ODTK.WriteMessage "Example debug message...", "debug"
ODTK.WriteMessage "Yawn", "sleep 1000" ' Causes system sleep for 1 sec
ODTK.WriteMessage("Example error message...", "error");
ODTK.WriteMessage("Example warning message...", "warn");
ODTK.WriteMessage("Example forced message...", "force");
ODTK.WriteMessage("Example info message...", "info");
ODTK.WriteMessage("Example debug message...", "debug");
ODTK.WriteMessage("Yawn", "sleep 1000"); // Causes system sleep for 1 sec
ODTK.WriteMessage("Example error message...", "error")
ODTK.WriteMessage("Example warning message...", "warn")
ODTK.WriteMessage("Example forced message...", "force")
ODTK.WriteMessage("Example info message...", "info")
ODTK.WriteMessage("Example debug message...", "debug")
ODTK.WriteMessage("Yawn", "sleep 1000") # Causes system sleep for 1 sec
Cross-Platform API
# Using ODTK.WriteMessage() function
# WriteMessage works for desktop only; the Runtime accepts the command and returns true but does not write a message.
odtk.WriteMessage("Example error message...", "error")
odtk.WriteMessage("Example warning message...", "warn")
odtk.WriteMessage("Example forced message...", "force")
odtk.WriteMessage("Example info message...", "info")
odtk.WriteMessage("Example debug message...", "debug")
odtk.WriteMessage("Yawn", "sleep 1000") # Causes system sleep for 1 sec
% Using ODTK.WriteMessage() function
% WriteMessage works for desktop only; the Runtime accepts the command and returns true but does not write a message.
odtk.WriteMessage("Example error message...", "error");
odtk.WriteMessage("Example warning message...", "warn");
odtk.WriteMessage("Example forced message...", "force");
odtk.WriteMessage("Example info message...", "info");
odtk.WriteMessage("Example debug message...", "debug");
odtk.WriteMessage("Yawn", "sleep 1000"); % Causes system sleep for 1 sec
// Using ODTK.WriteMessage() function
// WriteMessage works for desktop only; the Runtime accepts the command and returns true but does not write a message.
odtk("WriteMessage").Invoke("Example error message...", "error");
odtk("WriteMessage").Invoke("Example warning message...", "warn");
odtk("WriteMessage").Invoke("Example forced message...", "force");
odtk("WriteMessage").Invoke("Example info message...", "info");
odtk("WriteMessage").Invoke("Example debug message...", "debug");
odtk("WriteMessage").Invoke("Yawn", "sleep 1000"); // Causes system sleep for 1 sec
Closing child windows
You can access any of the windows in the ODTK application using the windows method. This will return a collection of all the open windows. The example below automatically closes all graph windows by checking the contents of their titles.
COM
for each w in uiApp.windows
if InStr(w.caption,"Graph Viewer") = 1 then
w.Close
end if
next
for(var i = 0; i < uiApp.windows.Count; i++)
{
if(uiApp.windows(i).Caption.indexOf("Graph Viewer") == 0)
{
uiApp.windows(i).Close();
}
}
Custom scripts for event handling
You can implement event handlers for the ODTK application in VBScript, Jscript, Python, and Perl. For example, Simulator.Events.OnComplete can be set to C:\Test\Test1.js file:
COM
function Test1(msg,simObj)
{
var odtk = simObj.Parent.Parent.Application;
odtk.WriteMessage("Test1 for " + simObj.name + "\n" +
"Message was: " + msg, "info");
}
function Test1(msg,simObj)
{
ODTK = simObj.Parent.Parent.Application;
ODTK.WriteMessage("Test1 for " + simObj.name + "\n" + "Message was: " + msg, "info");
}
def Test1(msg,simObj):
ODTK = simObj.Parent.Parent.Application
ODTK.WriteMessage("Test1 for " + simObj.name + "\n" + "Message was: " + msg, "info")
The file extension determines which scripting engine is used and the file name itself determines which function inside the file is called to handle the event. The function receives two arguments: An error message for this event and the object originating this event.
You can access additional plugin point and scripting examples shipped with the ODTK application at <Install_Dir>\ODTK\AppData\Scripts.
For additional help, please refer to the ODTK Plugin Programming Guide.
Using external scripts to perform Date Time and Unit conversion
The ODTK application ships with a DataPicker Control that allows external scripts to perform Date, Time, and Unit conversions using the same rules and leap second awareness that is built into the ODTK application. Here are examples of using it in Javascript, VB Script, and Perl.
COM
var unitPickerObj = null;
function convertDateTime(inpStr,inpUnit,retUnit)
{
if(unitPickerObj==null)
{
unitPickerObj = WScript.CreateObject("AgUiUnitPicker6.AgUiDatePickerCtrl");
//Optionally set alternative LeapSecond.dat file if it differs from the default location (below)
//unitPickerObj.LoadLeapSecondTable('C:/ProgramData/AGI/STK_ODTK <span class="mc-variable odtkVariables.ODTKWholeNumber variable">13</span>/DynamicEarthData/LeapSecond.dat');
unitPickerObj.ReadOnly = false;
}
unitPickerObj.unit = inpUnit;
unitPickerObj.returnUnit = inpUnit;
unitPickerObj.value = inpStr+"";
unitPickerObj.returnUnit = retUnit;
return unitPickerObj.value+"";
}
var x = convertDateTime("2012-05-16T11:12:13.456Z","ISO-YMD","JDate");
var y = parseFloat(x)+2.5;
var z = convertDateTime(y,"JDate","ISO-YMD");
WScript.Echo("z = " + z);
set unitPickerObj = WScript.CreateObject("AgUiUnitPicker6.AgUiDatePickerCtrl")
unitPickerObj.ReadOnly = false
Function convertDateTime(inpStr,inpUnit,retUnit)
unitPickerObj.unit = inpUnit
unitPickerObj.returnUnit = inpUnit
unitPickerObj.value = CStr(inpStr)
unitPickerObj.returnUnit = retUnit
convertDateTime = CStr(unitPickerObj.value)
End function
x = convertDateTime("2012-05-16T11:12:13.456Z","ISO-YMD","JDate")
y = CDbl(x)+2.5
z = convertDateTime(y,"JDate","ISO-YMD")
WScript.Echo("z = " + z)
use Win32::OLE;
$unitPickerObj = Win32::OLE->new('AgUiUnitPicker6.AgUiDatePickerCtrl') or die "oops\n";
$unitPickerObj->{ReadOnly} = false;
sub convertDateTime
{
my ($inpStr,$inpUnit,$retUnit) = @_;
$unitPickerObj->{unit} = $inpUnit;
$unitPickerObj->{returnUnit} = $inpUnit;
$unitPickerObj->{value} = $inpStr;
$unitPickerObj->{returnUnit} = $retUnit;
return $unitPickerObj->{value};
}
$x = convertDateTime("2012-05-16T11:12:13.456Z","ISO-YMD","JDate");
$y = $x+2.5;
$z = convertDateTime($y,"JDate","ISO-YMD");
print "z = $z";