Direct DLL Example in Python


The following example present the implementation of the DirectDLL in Python without requiring any external library, the ctypes packages is available with python by default. 


In the example the OpenDSSDirect.dll is loaded first, then as a test case, the IEEE13 nodes, is compiled, 


import ctypes


dll_path = r"C:\Program Files\OpenDSS\x64\OpenDSSDirect.dll"


# Create DSS object

DSSObject = ctypes.cdll.LoadLibrary(dll_path)


'''

By default Ctypes functions returns C int type, the restype attribute permits convert this int into 

str of float, fot furhter information please see:  https://docs.python.org/3/library/ctypes.html

'''

DSSObject.CircuitF.restype = ctypes.c_double  # restype to assign float

DSSObject.CircuitS.restype = ctypes.c_char_p  # restype to assign string


dss_file_ex = r"C:\Program Files\OpenDSS\IEEETestCases\13Bus\IEEE13Nodeckt.dss"


# Here the text interface is implemented to compile the test case

argument = f"compile [{dss_file_ex}]"

ctypes.c_char_p(DSSObject.DSSPut_Command(argument.encode('ascii')))


# Then the Text interface is also used to solve the circuit.

argument = "solve"

ctypes.c_char_p(DSSObject.DSSPut_Command(argument.encode('ascii')))


# Finally with a similar procedure the tex interface is used to show the results

argument = "Show Powers MVA Elements"

ctypes.c_char_p(DSSObject.DSSPut_Command(argument.encode('ascii')))


# Next, the Integer subclass of a class in this case the Circuit Class is used to access the

# number of buses

num_bus = DSSObject.CircuitI(ctypes.c_int32(1), ctypes.c_int32(0))

print(num_bus)


# Also the Integer subclass is implemented to acces the number of PDElements

pd_elem = DSSObject.PDElementsI(ctypes.c_int32(0),  ctypes.c_int32(0))

print(pd_elem)


# The following two lines present an example of using a Float subclass, in this case the capacity

# Keep in mind that CircuitF need to be assigned converted to a float

capacity = DSSObject.CircuitF(ctypes.c_int32(0), ctypes.c_double(0), ctypes.c_double(0.1))

print(capacity)


# The following two lines present an example of using a String subclass, in this case the capacity

# Keep in mind that CircuitF need to be assigned converted to a str

circuit_name = ctypes.c_char_p(DSSObject.CircuitS(ctypes.c_int32(0), ctypes.c_int32(0))).value.decode('ascii')

print(circuit_name)


# The following line shows the powers of the circuit

argument = "Show Powers MVA Elements"

ctypes.c_char_p(DSSObject.DSSPut_Command(argument.encode('ascii')))