global class

The global class is the main JavaScript class. More...

The PRIMER JavaScript API provides many class constants, properties and methods. For Arup to be able to extend and enhance the API in the future any constant, property or method names beginning with a lowercase or uppercase letter are reserved.
If you need to add your own properties or methods to one of the existing classes then to avoid any potential future conflict you should ensure that the name begins with either an underscore (_) or a dollar sign ($) or the name is prefixed with your own unique identifier.
For example if company 'ABC' need to add a property called 'example' then to avoid any potential future conflict use one of:

Class functions

Detailed Description

The global class declares the global object in JavaScript that contains the global properties and methods. As well as the core JavaScript methods, PRIMER also defines other additional ones. e.g. Message(), Print() etc. See the documentation below for more details.

Details of functions

AllocateFlag() [static]

Description

Allocate a flag for use in the script. See also ReturnFlag() and Model.PropagateFlag(). Once allocated the flag is automatically cleared for all the models currently in PRIMER.

Arguments

No arguments

Returns

Flag

Return type

Number

Example

To allocate a flag

var flag = AllocateFlag();


BatchMode() [static]

Description

Check if PRIMER is running in "batch mode" (i.e. menus are not active). Menus will not be active if PRIMER is started with the -d=tty command line argument. Note that this is different to starting PRIMER with the -batch command line argument. When using -batch, the menu system is still running, but the main PRIMER window is not shown.

Arguments

No arguments

Returns

true if in batch mode, false if not

Return type

Boolean

Example

To test if PRIMER is in batch mode

var batch_mode = BatchMode();


DialogueFunction(name[function]) [static]

Description

Set the function for dialogue callback. This function can be used to make PRIMER return any dialogue messages that are printed. This may be useful for you to know if a particular dialogue message has been printed or a particular event has taken place.
The function will be called with 1 argument which is a string containing the dialogue message. To remove the dialogue function use DialogueFunction(null).

Arguments

  • name (function)

    The name of the function (or null to remove the function)

    Returns

    No return value

    Example

    To set function MyDialogueFunction as the dialogue function:

    DialogueFunction(MyDialogueFunction);


    DialogueInput(command[string]) [static]

    Description

    Execute one or more lines of command line dialogue input.

    Arguments

  • command (string)

    Command to execute (as if it had been typed into the dialogue box)

    This argument can be repeated if required
    Alternatively a single array argument containing the multiple values can be given

    Returns

    0: No errors/warnings.
    > 0: This number of errors occurred.
    < 0: Absolute number is the number of warnings that occurred.

    Return type

    Number

    Example

    To read two models:

    DialogueInput("/rea dk model_1.key 1", "/rea dk model_2.key 2");

    Note that each call to DialogueInput starts afresh at the top of the PRIMER command line "tree", so where multiple commands need to be given at sub-menu levels they need to be included in a single call. For example to restrain degrees of a mechanism assembly, and then move it by some amount:

    DialogueInput("/mech assy " + assy_number, "fix 123", "done", "point " + point_name, delta_x + " * *", "accept"); 

    NOT:

    DialogueInput("/mech assy " + assy_number); 

    DialogueInput("fix 123");

    etc


    DialogueInputNoEcho(command[string]) [static]

    Description

    Execute one or more lines of command line dialogue input with no echo of commands to dialogue box.

    Arguments

  • command (string)

    Command to execute (as if it had been typed into the dialogue box)

    This argument can be repeated if required
    Alternatively a single array argument containing the multiple values can be given

    Returns

    0: No errors/warnings.
    > 0: This number of errors occurred.
    < 0: Absolute number is the number of warnings that occurred.

    Return type

    Number

    Example

    To read two models:

    DialogueInputNoEcho("/re dk model_1.key 1", "/re dk model_2.key 2");

    As with DialogueInput above each call starts at the top of the PRIMER command tree structure, so any commands destined for sub-menus must all be arguments to a single call.


    ErrorMessage(string[Any valid javascript type]) [static]

    Description

    Print an error message to the dialogue box adding a carriage return.

    Arguments

  • string (Any valid javascript type)

    The string/item that you want to print

    Returns

    No return value

    Example

    To print the title of model object m as an error to the dialogue box

    ErrorMessage("The title is " + m.title);


    Execute(data[object]) [static]

    Description

    Execute a program or script outside PRIMER and get the standard output and error streams.

    Arguments

  • data (object)

    Execute data

    Object has the following properties:

    Name Type Description
    arguments (optional) Array of strings The arguments to pass to program
    program string The program you want to run. Note that on Linux this will consider PATH when resolving executable filenames without an absolute path. If you want to run something from the current directory and you do not have '.' in your PATH then you will need to write './something' as the program.

    Returns

    Object with the following properties:

    Name Type Description
    status integer The exit code from the program/script
    stderr string The standard error output from the program/script
    stdout string The standard output from the program/script

    Return type

    object

    Example

    To run script "example.bat" with arguments "foo" and "bar":

    var output = Execute( { program: 'example.bat', arguments: [ 'foo', 'bar' ]} );
    var text   = output.stdout;
    var errors = output.stderr;
    var ecode  = output.status;


    Exit(write hook interrupt (optional)[boolean]) [static]

    Description

    Exit script

    Arguments

  • write hook interrupt (optional) (boolean)

    If Exit() is called from a write_hook.js script, the first argument will be processed as in the following: If the argument is provided and set to "true", it is used to interrupt the write out of the model, so that the script exits without anything being written out. An argument value of "false" exits the script and allows the model to be written out as normal. An example of this function's use in a Write Hook script can be found at $OA_INSTALL/primer_library/scripts/hooks/example_write_hook.js.

    Returns

    No return value

    Example

    Exit with

    Exit();


    FlagsAvailable() [static]

    Description

    Number of flags available to be used for AllocateFlag()

    Arguments

    No arguments

    Returns

    Number of flags available

    Return type

    Number

    Example

    To get the number of flags available:

    var flags = FlagsAvailable();


    GetCurrentDirectory() [static]

    Description

    Get the current working directory

    Arguments

    No arguments

    Returns

    String containing current working directory

    Return type

    String

    Example

    To get the current directory:

    var cwd = GetCurrentDirectory();


    GetInstallDirectory() [static]

    Description

    Get the directory in which executables are installed. This is the OA_INSTALL environment variable, or if that is not set the directory in which the current executable is installed. Returns NULL if not found

    Arguments

    No arguments

    Returns

    string

    Return type

    String

    Example

    To get the install directory:

    var install_dir = GetInstallDirectory();


    GetPreferenceValue(program[string], name[string]) [static]

    Description

    Get the Preference value with the given string in the any of admin ("OA_ADMIN") or install ("OA_INSTALL") or home ("OA_HOME") directory oa_pref

    Arguments

  • program (string)

    The program name string : Valid values are 'All', 'D3PLOT', 'PRIMER', 'REPORTER', 'SHELL',
    'T/HIS'

  • name (string)

    The preference name string

    Returns

    : String containing preference value or null if preference string is not present in any oa_pref. Also if none of the above environment variables are not present, then API simply returns null. While returning preference value, locked preference value in admin and then install oa_pref takes precedence over home oa_pref. If preference is not locked in any of these oa_pref, preference in home directory oa_pref is returned.

    Return type

    String

    Example

    To get the preference value:

    var pref_list = GetPreferenceValue('All', "font_size");


    GetStartInDirectory() [static]

    Description

    Get the directory passed to PRIMER by the -start_in command line argument

    Arguments

    No arguments

    Returns

    String containing start_in directory or NULL if not set

    Return type

    String

    Example

    To get the start_in directory:

    var start_in = GetStartInDirectory();


    Getenv(name[string]) [static]

    Description

    Get the value of an environment variable

    Arguments

  • name (string)

    The environment variable name

    Returns

    String containing variable value or null if variable does not exist

    Return type

    String

    Example

    To get the value for environment variable HOME

    var home = Getenv("HOME");


    Labels(type[string], state (optional)[boolean]) [static]

    Description

    Set or get labelling of items in PRIMER

    Arguments

  • type (string)

    The type of the item (for a list of types see Appendix I of the PRIMER manual). Additionally, to change the visibility of attached or unattached nodes you can use the types "ATTACHED_NODE" and "UNATTACHED_NODE".

  • state (optional) (boolean)

    If it is provided it is used to set the labelling status of entity. "true" to make items labelled and "false" to make them not labelled.

    Returns

    Boolean

    Return type

    Boolean

    Example

    To turn on beam labels

    Labels("BEAM", true);

    To get the labelling status of beams

    var lab = Labels("BEAM");


    MacroFunction(name[function]) [static]

    Description

    Set the function for macro callback. This function can be used to make PRIMER return the macro command that would be recorded if macro recording was active for every button press etc. This may be useful for you to know if a particular action has been done by the user.
    The function will be called with 1 argument which is a string containing the macro command. To remove the macro function use MacroFunction(null).

    Arguments

  • name (function)

    The name of the function (or null to remove a function)

    Returns

    No return value

    Example

    To set function MyMacroFunction as the macro function:

    MacroFunction(MyMacroFunction);


    Message(string[Any valid javascript type]) [static]

    Description

    Print a message to the dialogue box adding a carriage return.

    Arguments

  • string (Any valid javascript type)

    The string/item that you want to print. If '\r' is added to the end of the string then instead of automatically adding a carriage return in the dialogue box, the next message will overwrite the current one. This may be useful for giving feedback to the dialogue box when doing an operation.

    Returns

    No return value

    Example

    To print the title of model object m as a message to the dialogue box

    Message("The title is " + m.title);


    MilliSleep(time[integer]) [static]

    Description

    Pause execution of the script for time milliseconds. See also Sleep()

    Arguments

  • time (integer)

    Number of milliseconds to pause for

    Returns

    No return value

    Example

    To pause for 500 milliseconds

    MilliSleep(500);


    NumberToString(number[integer/real], width[integer], pref_int (optional)[boolean]) [static]

    Description

    Formats a number to a string with the specified width.

    Arguments

  • number (integer/real)

    The number you want to format.

  • width (integer)

    The width of the string you want to format it to (must be less than 80).

  • pref_int (optional) (boolean)

    By default only integer values inside the single precision 32 bit signed integer limit of approximately +/-2e9 are formatted as integers, all other numeric values are formatted as floats. With this argument set to TRUE then integer values up to the mantissa precision of a 64 bit float, approximately +/-9e15, will also be formatted as integers.

    Returns

    String containing the number

    Return type

    String

    Example

    To write the number 1.2345e+6 to a string 10 characters wide

    var str = NumberToString(1.2345e+6, 10);


    OpenManual(program[string], page[string]) [static]

    Description

    Open the Oasys manuals at a requested page

    Arguments

  • program (string)

    The program manual to open. Can be "primer", "d3plot" or "this"

  • page (string)

    The page to open in the manual, e.g. "running-this.html"

    Returns

    true if successful, false if not

    Return type

    Boolean

    Example

    To open the T/HIS manual on the running-this.html page

    OpenManual("this", "running-this.html");


    PlayMacro(filename[string], options (optional)[object]) [static]

    Description

    Play a macro in PRIMER

    Arguments

  • filename (string)

    The name of the macro file to play

  • options (optional) (object)

    Options specifying how the macro file should be replayed. If omitted the default values below will be used.

    Object has the following properties:

    Name Type Description
    delay integer Delay in ms between commands when replaying. If omitted the current value from macro window will be used.
    pick boolean If picks/drags from the macro file should be replayed. If omitted the current value from macro window will be used.
    terminate boolean If the script should be terminated if an error occurs when playing the macro. If omitted the script will be terminated.
    utf8 boolean If the script is UTF-8 encoded. If omitted or false the script is assumed to be ASCII text.
    variables object Object containing names and values for variables in the macro. If null or omitted no variables are used.
    view boolean If views encoded in the macro file for picks/drags should be replayed. If omitted the current value from macro window will be used.

    Returns

    true if an error occured during playback, false otherwise.

    Return type

    Boolean

    Example

    To play a UTF-8 encoded macro file /data/test/example.prm using the default options for picking/dragging and a delay of 500ms:

    PlayMacro("/data/test/example.prm", { delay:500, utf8:true} );

    To play macro file /data/test/example.prm, defining values for variables A, B and C in the macro:

    PlayMacro("/data/test/example.prm", { variables: { A:10.0, B:0, C:"Example"} } );


    PlayMacro(filename[string], pick (optional)[boolean], view (optional)[boolean], delay (optional)[integer], variables (optional)[object], terminate (optional)[boolean]) [static]  [deprecated]

    This function is deprecated in version 15.0. It is only provided to keep old scripts working. We strongly advise against using it in new scripts. Support may be removed in future versions.

    Description

    Play a macro in PRIMER

    Arguments

  • filename (string)

    The name of the macro file to play

  • pick (optional) (boolean)

    If picks/drags from the macro file should be replayed. If omitted the current value from macro window will be used.

  • view (optional) (boolean)

    If views encoded in the macro file for picks/drags should be replayed. If omitted the current value from macro window will be used.

  • delay (optional) (integer)

    Delay in ms between commands when replaying. If omitted the current value from macro window will be used.

  • variables (optional) (object)

    Object containing names and values for variables in the macro. If null or omitted no variables are used.

  • terminate (optional) (boolean)

    If the script should be terminated if an error occurs when playing the macro. If omitted the script will be terminated.

    Returns

    true if an error occured during playback, false otherwise.

    Return type

    Boolean

    Example

    To play macro file /data/test/example.prm using the default options for picking/dragging and the default delay

    PlayMacro("/data/test/example.prm");

    To play macro file /data/test/example.prm, defining values for variables A, B and C in the macro

    var variables = new Object();
    variables.A = 10.0;
    variables.B = 0;
    variables.C = "Example";
    PlayMacro("/data/test/example.prm", true, true, 0, variables);


    Print(string[Any valid javascript type]) [static]

    Description

    Print a string to stdout. Note that a carriage return is not added.

    Arguments

  • string (Any valid javascript type)

    The string/item that you want to print

    Returns

    No return value

    Example

    To print string "Hello, world!"

    Print("Hello, world!");

    To print the title of model object m with a carriage return

    print("The title is " + m.title + "\n");


    Println(string[Any valid javascript type]) [static]

    Description

    Print a string to stdout adding a carriage return.

    Arguments

  • string (Any valid javascript type)

    The string/item that you want to print

    Returns

    No return value

    Example

    To print string "Hello, world!" automatically adding a carriage return

    Println("Hello, world!");

    To print the title of model object m, automatically adding a carriage return

    Println("The title is " + m.title);


    Requires(build[integer]) [static]

    Description

    Checks to see if the build number of PRIMER is high enough to run this script. If your script requires features that are only present in builds of PRIMER greater than a certain value Require can test this and only run the script if the build is high enough.

    Arguments

  • build (integer)

    The minimum build number that is required.

    Returns

    No return value (if the build is not high enough the script will terminate)

    Example

    To only allow a script to run if the build is >= 2000

    Requires(2000);


    ReturnFlag(flag[Flag]) [static]

    Description

    Return a flag used in the script. See also AllocateFlag() and Model.PropagateFlag().

    Arguments

  • flag (Flag)

    The flag to return.

    Returns

    No return value.

    Example

    To return flag f:

    ReturnFlag(f);


    RunScript(filename[string], separate (optional)[boolean]) [static]

    Description

    Run a script. Note: RunScript is intended to run a 'child' script that will finish before the calling script finishes. Terminating the calling script while child scripts are still running may give undefined behaviour.

    Arguments

  • filename (string)

    The name of the script file to run. If the filename is relative then the file will be searched for relative to this script. If not found then the script_directory preference will be used.

  • separate (optional) (boolean)

    If the script will use separate memory from the current script. If it uses separate memory (true) then the 'child' script is completely separated from this script and knows nothing about variables in this script. If it does not use separate memory (false) then the 'child' script will have access to all of the variables in the current script and hence variables must not clash. It is strongly recommended that you use namespaces to stop variable names from clashing. If omitted the script will use separate memory.

    Returns

    No return value

    Example

    To run script /data/test/child.js using separate memory for the child script

    RunScript("/data/test/child.js");


    SetCurrentDirectory(directory path[string]) [static]

    Description

    Sets the current working directory.

    Arguments

  • directory path (string)

    Path to the directory you would like to change into.

    Returns

    true if successful, false if not

    Return type

    Boolean

    Example

    To change into the directory "/data/test" exists

    SetCurrentDirectory("/data/test")


    SetPreferenceValue(program[string], name[string], value[string], refresh (optional)[boolean]) [static]

    Description

    Save the preference string and its value into oa_pref of home directory. If the preference is locked in admin ("OA_ADMIN") or install ("OA_INSTALL") oa_pref, then API is unsuccessful. Home directory is defined by environment variable OA_HOME. If OA_HOME is not defined then API is unsuccessful.

    Arguments

  • program (string)

    The program name string : Valid values are 'All', 'D3PLOT', 'PRIMER', 'REPORTER', 'SHELL',
    'T/HIS'

  • name (string)

    The preference name string

  • value (string)

    The preference value string. If "value" is of zero length, then the option is simply removed from the file if present, and no new entry is made.This argument cannot be null.

  • refresh (optional) (boolean)

    If the saved preference should be refreshed. If omitted, the preference will NOT be refreshed. This argument is currently only available in PRIMER JS API and ignored in D3PLOT and T/HIS.

    Returns

    An integer. Returns 0 if the preference is saved succesfully or 1 if unsuccessful

    Return type

    Number

    Example

    To save the preference value:

    var ierr = SetPreferenceValue( 'All', "font_size", 'Default');


    Sleep(time[integer]) [static]

    Description

    Pause execution of the script for time seconds. See also MilliSleep()

    Arguments

  • time (integer)

    Number of seconds to pause for

    Returns

    No return value

    Example

    To pause for 2 seconds

    Sleep(2);


    System(string[Any valid javascript type]) [static]

    Description

    Do a system command outside PRIMER. To run an external command and get the output then please use Execute() instead.

    Arguments

  • string (Any valid javascript type)

    The system command that you want to do

    Returns

    integer (probably zero if command successful but is implementation-dependant)

    Return type

    Number

    Example

    To make the directory "example"

    System("mkdir example");


    Unix() [static]

    Description

    Test whether script is running on a Unix/Linux operating system. See also Windows()

    Arguments

    No arguments

    Returns

    true if Unix/Linux, false if not

    Return type

    Boolean

    Example

    To test if the OS is Unix

    if ( Unix() )


    Use(filename[string]) [static]

    Description

    Use script from a separate file

    Arguments

  • filename (string)

    Use allows you to include a script from a separate file. This may be useful if your script is very large and you want to split it up to help with maintenance. Alternatively you may have a 'library' of common functions which you always want to include in your scripts. Including the 'library' with Use means that any changes only have to be done in one place. PRIMER will look for the file in the same directory as the main script. If that fails then it will look in $OA_INSTALL/primer_library/scripts directory and the script directory specified by the primer*script_directory preference.
    Note that the file is included when the script is compiled, NOT at runtime.

    Returns

    No return value

    Example

    To include script from file library.js

    Use("library.js");


    UuidCreate() [static]

    Description

    Create a UUID (Universally unique ID)

    Arguments

    No arguments

    Returns

    string

    Return type

    String

    Example

    To create a UUID:

    var uuid = UuidCreate();


    Visibility(type[string], state (optional)[boolean]) [static]

    Description

    Set or get visibility of items in PRIMER

    Arguments

  • type (string)

    The type of the item (for a list of types see Appendix I of the PRIMER manual). Additionally, to change the visibility of attached or unattached nodes you can use the types "ATTACHED_NODE" and "UNATTACHED_NODE".

  • state (optional) (boolean)

    If it is provided it is used to set the visibility. "true" to make items visible and "false" to make them not visible.

    Returns

    Boolean

    Return type

    Boolean

    Example

    To make beams visible

    Visibility("BEAM", true);

    To get the visibility status of beams

    var vis = Visibility("BEAM");


    WarningMessage(string[Any valid javascript type]) [static]

    Description

    Print a warning message to the dialogue box adding a carriage return.

    Arguments

  • string (Any valid javascript type)

    The string/item that you want to print

    Returns

    No return value

    Example

    To print the title of model object m as a warning to the dialogue box

    WarningMessage("The title is " + m.title);


    Windows() [static]

    Description

    Test whether script is running on a Windows operating system. See also Unix()

    Arguments

    No arguments

    Returns

    true if Windows, false if not

    Return type

    Boolean

    Example

    To test if the OS is Windows

    if ( Windows() )