"""
ChangeParametersSingleElement changes the values(s) of parameters(s) for a single element. 
In this example the PUVolt and the KVVolt for bus 2 are changed. The output of 
ChangeParametersSingleElement is any errors. 
"""

# Specify file path to test case
file_name = "C:\\Training Cases\\B7FLAT.pwb" # Set this to a path on your system

# Import file path tools
import os

# Import the win32com library to connect to SimAuto
import win32com.client
SimAuto = win32com.client.Dispatch("pwrworld.SimulatorAuto")

# Open the case using the file path
(err,) = SimAuto.OpenCase(file_name)
if err:
    raise Exception(f'Error: {err}')
else:
    print(f'Opened {os.path.basename(file_name)}')

# arguments for ChangeParametersSingleElement specified 
param_list = ['BusNum', 'BusKVVolt', 'BusPUVolt']
bus_number = input("Enter a bus number: ")
val_list = [bus_number, 0, 0]

print()
(err, bus_info) = SimAuto.GetParametersSingleElement('BUS', param_list, val_list)
if err:
    raise Exception(f'Error: {err}')
else:
    print('Current info: ')

for i, x in enumerate(param_list):
    print (f'{x}: {bus_info[i]}')
print()

# Setting new values. Note that the KV Voltage is passed back in unchanged
val_list = [bus_number, bus_info[1], 1]

(err,) = SimAuto.RunScriptCommand('EnterMode(EDIT);')
if err:
    raise Exception(f'Error: {err}')
else:
    print('Entered Edit Mode')

# values for parameters in param_list for bus 2 are changed to values in val_list
(err,) = SimAuto.ChangeParametersSingleElement('BUS', param_list, val_list)
if err:
    raise Exception(f'Error: {err}')
else:
    print('Applied change')

# Check that the changes are applied
print()
(err, bus_info) = SimAuto.GetParametersSingleElement('BUS', param_list, val_list)
if err:
    raise Exception(f'Error: {err}')
else:
    print('Updated info: ')

# Note that since the PU Voltage change was applied after the KV Voltage, it overwrites the KV Voltage
for i, x in enumerate(param_list):
    print(f'{x}: {bus_info[i]}')
print()

# Close case
(err,) = SimAuto.CloseCase()
if err:
    raise Exception(f'Error: {err}')
else:
    print("Closed Case")

# Disconnect
SimAuto = None
