/* Contain the entire script within a function because REPORTER only has a single JavaScript realm
* for the entire session. */
on_load_script();
/**
* #AAW REPORTER on load script
*
* Prompts the user to select a keyword file.
* This script deactivates itself so that:
* 1. When it triggers template generation, we don't end up in an infinite loop
* 2. If you generate the script again, this script is skipped and keyword file
* is checked in subsequent `#AAW REPORTER check job inputs` item.
*/
function on_load_script() {
let template = Template.GetCurrent();
/* Check if template is launched in batch mode so that we don't pop up any windows.
* Create a BATCH_MODE variable so that Oasys items can pick up this information.
*/
let batch_mode;
if (Batch()) {
batch_mode = new Variable(
template,
`BATCH_MODE`,
`Whether template was launched in batch mode (true/false)`,
`true`,
`General`,
true
);
} else {
batch_mode = new Variable(
template,
`BATCH_MODE`,
`Whether template was launched in batch mode (true/false)`,
`false`,
`General`,
true
);
}
/* Get keyword file and set %DEFAULT_DIR% and %DEFAULT_JOB% */
if (!get_keyword_file(template)) {
finish_script(template, false, `Keyword file not selected. Report will not be generated.`);
} else {
finish_script(template, true);
}
}
/**
* Gets the keyword file and sets %DEFAULT_DIR% and %DEFAULT_JOB%.
* Returns whether successful (true/false).
* @param {Template} template Current Template object
* @returns {Boolean}
* @example
* let success = get_keyword_file(template);
*/
function get_keyword_file(template) {
/* LS-DYNA keyword filename */
let f_key_name;
if (Batch()) {
LogError(`Batch mode not configured yet.`);
} else {
let ans = Window.Message(
`Select keyword file`,
`Select the LS-DYNA keyword file of the job you wish to post-process.`,
Window.OK | Window.CANCEL
);
if (ans == Window.CANCEL) {
return false;
}
f_key_name = Window.GetFile("*.k*");
}
if (!f_key_name) {
/* User clicked cancel */
return false;
}
/* Got a file, so set %KEYWORD_FILE%, %DEFAULT_DIR% and %DEFAULT_JOB% */
let path_index = Math.max(f_key_name.lastIndexOf("/"), f_key_name.lastIndexOf("\\"));
let default_dir = f_key_name.substring(0, path_index);
let default_job = f_key_name.substring(path_index + 1);
/* Assign to REPORTER variables. Constructor will overwrite existing variable.
* KEYWORD_FILE should be temporary; DEFAULT_DIR and DEFAULT_JOB are not temporary. */
let keyword_file = new Variable(
template,
`KEYWORD_FILE`,
`Keyword file`,
f_key_name,
`File(absolute)`,
false,
true
);
let default_dir_var = new Variable(
template,
`DEFAULT_DIR`,
`Reporter default directory`,
default_dir,
`Directory`,
false,
false
);
let default_job_var = new Variable(
template,
`DEFAULT_JOB`,
`Reporter default jobname`,
default_job,
`File(basename)`,
false,
false
);
return true;
}
/**
* Common function for finishing the script.
* Complete the generation of the template if we have all the required information.
* @param {Template} template Current template object
* @param {Boolean} can_generate Whether or not to generate the template
* @param {string} [msg] Error message (provide when not generating)
* @example
* finish_script(Template.GetCurrent(), false, `Keyword file not provided`);
*/
function finish_script(template, can_generate, msg) {
/* Before finishing, deactivate this script to avoid entering an infinite loop. */
/* We will search for items with names beginning with an identifiable string */
let aaw_on_load_script_str = `#AAW REPORTER on load script`;
/* Loop through all pages in the template, searching for relevant items */
let pages = template.GetAllPages();
let match = false;
for (let page of pages) {
let items = page.GetAllItems();
for (let item of items) {
/* Deactivate this script item */
if (item.name.substring(0, aaw_on_load_script_str.length) == aaw_on_load_script_str) {
item.active = false;
match = true;
break;
}
}
if (match) break;
}
if (!match) {
LogError(`Could not find item "${aaw_on_load_script_str}" to deactivate it.`);
Exit();
}
/* Complete the template generation if we have the required information,
* otherwise end with message */
if (can_generate) {
template.Generate();
} else {
if (Batch()) LogError(msg);
else Window.Message(`Automotive Assessments`, msg);
}
// TODO
// Reactivate buttons on modifiers page
//reactivate_modifier_buttons();
Exit();
}