"""
Example of how to use SimAuto methods in Python
Developed by PowerWorld 2019, created by Mayank Hirani

SimAuto data retrieval functions
Includes:
    OpenCase()
    ListOfDevices()
    GetSpecificFieldList()
    CloseCase()
"""

# 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)}')

# Lists all buses using the ListOfDevices() method
(err, buses) = SimAuto.ListOfDevices("Bus", '')
if err:
    raise Exception(f'Error: {err}')
else:
    buses = buses[0]
    print(f'Case contains {len(buses)} buses')
    

# Get information about the field "GenMW" using the GetSpecificFieldList() method
(err, field_info) = SimAuto.GetSpecificFieldList("Bus", [ "GenMW" ])
if err:
    raise Exception(f'Error: {err}')
else:
    field_info = field_info[0]
    print("  Variable Name: ", field_info[0])
    print("  Field Name:    ", field_info[1])
    print("  Col Header:    ", field_info[2])
    print("  Description:   ", field_info[3])

# Close the case
(err,) = SimAuto.CloseCase()
if err:
    raise Exception(f'Error: {err}')
else:
    print("Closed Case")
    
# Disconnect
SimAuto = None
