pre_energy_check.js

// module: TRUE
// @ts-ignore
import { gui } from "./energy_check_gui.jsi";
import { WorkflowUnitsCombobox } from "../../modules/units.mjs";

/* Workflow definition filename is passed as an argument to this script from the Workflow menu */
let workflow_filename = Workflow.WorkflowDefinitionFilename();

set_up();

/* Show the first window */
if (gui) gui.wdwEnergyCheck.Show(false);

/**
 * Setup the GUI
 */
function set_up() {
    /* Ask the user which model to use */

    gui.model = Model.Select("Select the model to use");

    if (gui.model == null) {
        Window.Information("", "You need to select a model before you can use this workflow");
        Exit();
    }

    /* Add widget items to the units combobox */

    gui.wdwEnergyCheck.cbxUnitSystem = new WorkflowUnitsCombobox(gui.wdwEnergyCheck.cbxUnitSystem);

    /* Read in previously saved data */

    let num_models = Workflow.NumberOfModels();
    for (let i = 0; i < num_models; i++) {
        let model_id = Workflow.ModelIdFromIndex(i);
        if (model_id != gui.model.number) {
            continue;
        }

        /* Get the model unit system and set the combobox selected item */

        let unit_system = Workflow.ModelUnitSystemFromIndex(i);

        gui.wdwEnergyCheck.cbxUnitSystem.SetSelectedUnitSystem(unit_system);
    }

    /* Callbacks */

    gui.wdwEnergyCheck.helpBtn.onClick = workflow_manual;
    gui.wdwEnergyCheck.B1.onClick = save_to_file;
    gui.wdwEnergyCheck.B2.onClick = save_to_model;
}

/**
 * Saves the data to a file
 */
function save_to_file() {
    /* Blank data */

    let data = {};

    let extra = get_unit_system();

    /* Ask the user where to write it */

    let filename = Window.GetFile(".json", true);

    if (filename == null) return;

    /* API call to write workflow file */

    Workflow.WriteToFile(data, filename, workflow_filename, extra);

    Message("Written workflow file to " + filename);
}

/**
 * Saves the data to the model
 */
function save_to_model() {
    // Blank data
    let data = {};

    let extra = get_unit_system();

    // Ask the user which model to write it to

    let model = Model.Select("Select the model to write to");

    if (model == null) return;

    // API call to write workflow to a model

    Workflow.WriteToModel(data, model, workflow_filename, extra);

    Message("Workflow data added to post *END data. You need to write out the model from the main Model->Write menu to save the additions");
}

/**
 * Gets the unit system
 */
function get_unit_system() {
    return {
        model_unit_system: gui.wdwEnergyCheck.cbxUnitSystem.GetSelectedUnitSystem()
    };
}

/**
 * Opens up the clickhelp manual when the help button is pressed
 */
function workflow_manual() {
    OpenManual("primer", "energy-check.html");
}