ION Scripting Examples
These are examples of scripts demonstrating the use of the functionality in the scripting library:
Hello World!
: Sets the custom field 01 field to "Hello World!" for a list of batches.This example demonstrates processing a list of batches and sets the user field for a list of batches using the
CmdSetBatchUserField
command:""" 'Hello world!' test script. Sets custom field 01 of the selected batches to 'Hello world!' """ from ps_scripting import * model = Model(input_data) batches = model.get_selected_batches() commands = [] for b in batches: commands.append(CmdSetBatchUserField(batch_code=b.code, index=1, text_value="Hello world!")) output_data = create_output_data(commands)
Set Batch Color
: Sets a color for each batch based on the quantity of batches.This example demonstrates how the ‘’if-else’’ condition can be applied to set a batch property using the
CmdSetBatchProperty
command:from ps_scripting import * model = Model(input_data) commands = [] for b in model.get_selected_batches(): if b.quantity > 10000: command = CmdSetBatchProperty(batch_code=b.code, color=16) else: command = CmdSetBatchProperty(batch_code=b.code, color=64) commands.append(command) output_data = create_output_data(commands)
Copy PStep User Field
: Copies the value from the custom data 01 user field in the process step to the custom data 02 user field for selected batches.This example demonstrates retrieving user field values from related entities and updating batch user fields using the
CmdSetBatchUserField
command:from ps_scripting import * model = Model(input_data) commands = [] for b in model.get_selected_batches(): new_text_value = model.get_process_step_user_field(b.process_step_code, 1).text_value commands.append(CmdSetBatchUserField(batch_code=b.code, index=2, text_value=new_text_value)) output_data = create_output_data(commands)
Align batches with work area
: Aligns the start time of the selected batches with the start of the work area.This example demonstrates accessing the work area and updating the start time of a batch using the
CmdAlignBatch
command:from ps_scripting import * model = Model(input_data) commands = [] new_start_time = model.get_global().work_area_start for b in model.get_selected_batches(): commands.append(CmdAlignBatch(batch_code=b.code, start_time=new_start_time)) output_data = create_output_data(commands)
Distribute batch quantity
: Distributes batch quantities to ensure that all batches stop at the same time.This example demonstrates the use of list comprehension to generate a list from another list, calculate the sum of the list, and verify if all elements in the list are valid:
from ps_scripting import * model = Model(input_data) batches = model.get_selected_batches() total_rate = sum([b.outflow_per_minute for b in batches]) quantity_offset = sum([b.quantity + b.outflow_per_minute * b.start_outflow for b in batches]) new_end_time = quantity_offset / total_rate commands = [] if all([b.start_inflow < new_end_time for b in batches]): for b in batches: new_quantity = b.outflow_per_minute * (new_end_time - b.start_outflow) commands.append(CmdSetBatchProperty(batch_code=b.code, quantity=new_quantity)) commands.append(CmdAlignBatch(batch_code=b.code, start_time=b.start_inflow, stop_time=int(new_end_time))) output_data = create_output_data(commands)
Fill gaps over multiple resources
: Aligns the stop time of selected batches with the start time of the next selected batch on the same resource based on the setup and changeover duration.This example demonstrates collecting data in a set to remove duplicates, identifying and sorting all batches on a resource, receiving the setup and changeover duration for a batch, and receiving the index of an element while the list is processed:
from ps_scripting import * model = Model(input_data) batches = model.get_selected_batches() # Find all batches that are assigned to just one resource single_resource_batches = [b for b in batches if len(b.resource_codes) == 1] # Get the resource codes that are allocated by the selected batches. # "resource_codes" is a set instead of a list, to remove any duplicates. # Note that this could also be written as: # resource_codes = set([b.resource_codes[0] for b in single_resource_batches]) resource_codes = set() for b in single_resource_batches: resource_codes.add(b.resource_codes[0]) commands = [] # Loop over all the resource codes for resource_code in resource_codes: # Find all the batches on the current resource resource_batches = [b for b in single_resource_batches if b.resource_codes[0] == resource_code] resource_batches.sort(key=lambda b: b.start_inflow) for idx, b in enumerate(resource_batches): # check if we are not on the last batch if idx < (len(resource_batches) - 1): curr_batch = resource_batches[idx] next_batch = resource_batches[idx + 1] # Find the changeover of the current batch, and the setup of the next batch changeover = model.get_changeover_duration(curr_batch.code, resource_code) setup = model.get_setup_duration(next_batch.code, resource_code) # Calculate the new end time of the current batch, keeping room for the setup and changeover new_end_time = resource_batches[idx + 1].start_inflow - changeover - setup commands.append(CmdAlignBatch(batch_code=curr_batch.code, start_time=curr_batch.start_inflow, stop_time=new_end_time)) output_data = create_output_data(commands)
Set batch color with user messages
: Sets the batch color and generates an error, warning, or messages based on the batch quantity.This example demonstrates creating different user messages using format string:
from ps_scripting import * """ From PS version 2022.07 on, you can pass a list of messages to the create_output_data function. These messages will be displayed in the message log and the macro execution log. You can create three types of messages: InfoMessage, WarningMessage, and ErrorMessage. """ model = Model(input_data) commands = [] messages = [] for b in model.get_selected_batches(): if b.quantity > 100000: command = CmdSetBatchProperty(batch_code=b.code, color=16) """ When a batch has a quantity of more than 100000, then add an error message to the message list. We use a format string to create the error message. A format string starts with the letter 'f', followed by the text in double quotes. A format string can contain fields, calculations, etc. enclosed in curly braces. In this case, {b.code}. This will be replaced with the value of b.code -- the code of the current batch. """ messages.append(ErrorMessage(f"Batch {b.code} has a quantity > 100000")) elif b.quantity > 50000: command = CmdSetBatchProperty(batch_code=b.code, color=48) """ When a batch has a quantity of more than 50000, then add a warning message. """ messages.append(WarningMessage(f"Batch {b.code} has a quantity > 50000")) else: command = CmdSetBatchProperty(batch_code=b.code, color=64) """ Otherwise, add an info message. """ messages.append(InfoMessage(f"Batch {b.code} has a quantity 50000 or lower")) commands.append(command) """ Finally, pass the list of messages as second parameter to the create_outflow_data method. """ output_data = create_output_data(commands, messages)