• 602 views | 5 messages Discussion: LEAP
    Topic: Linking LEAP with pythonSubscribe | Previous | Next
  • Yousef Al-abdullah 5/7/2018

    I was wondering if it is possible to link LEAP results through python to other models I have programmed that are solved using cplex?
  • Emily Ghosh 5/7/2018
      Best Response

    Hi Yousef,

    Yes, it is possible to link LEAP through python. LEAP can act as a standard "COM Automation Server" which allows other Windows programs to control LEAP directly (i.e. to calculate and export results). LEAP's built-in script editor supports multiple programming languages including python, and includes a library of classes, collections, objects. Refer to the following help page for more information on LEAP's API and the built-in script editor: https://www.energycommunity.org/Help/API/API.htm

    If you are using a python IDE, here is a sample script which prints the transformation module outputs from Freedonia:

    from win32com.client import Dispatch
    L = Dispatch('LEAP.LEAPApplication')

    L.ActiveArea = "Freedonia"
    L.ActiveView = "Results"

    setscenario = "Reference"
    typebranch = 3 #"Transformation Process"

    if L.Scenarios(setscenario):
    for b in L.branches:
    if b.branchtype == typebranch:
    for y in range(L.baseyear, L.endyear):
    print "year", y, " ", b.name, " ", b.Variable("Outputs by Output Fuel").Value(y, "Gigajoule"), "GJ"

    Hope this helps!
    Emily

  • Charles Li 4/30/2019
      Best Response

    Thanks for the detailed instruction! I'm using the 64-bit LEAP. When I ran "leap = win32com.client.Dispatch('LEAP.LEAPApplication')", I got the following error messages. Do you know if this is a 64-bit issue?

    Thanks,
    Charles

    Error messages:

    Traceback (most recent call last):
    File "C:\Users\charles\.conda\envs\LEAP\lib\site-packages\win32com\client\dynamic.py", line 89, in _GetGoodDispatch
    IDispatch = pythoncom.connect(IDispatch)
    pywintypes.com_error: (-2147221005, 'Invalid class string', None, None)

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File "", line 1, in
    File "C:\Users\charles\.conda\envs\LEAP\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch
    dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
    File "C:\Users\charles\.conda\envs\LEAP\lib\site-packages\win32com\client\dynamic.py", line 114, in _GetGoodDispatchAndUserName
    return (_GetGoodDispatch(IDispatch, clsctx), userName)
    File "C:\Users\charles\.conda\envs\LEAP\lib\site-packages\win32com\client\dynamic.py", line 91, in _GetGoodDispatch
    IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
    pywintypes.com_error: (-2147221005, 'Invalid class string', None, None)





  • Taylor Binnington 5/28/2019
      Best Response

    Hi Charles -

    As a quick check, can you try running your script using the 32-bit version of LEAP, and post the output to this thread?

    Thanks,

    Taylor

  • Charles Li 5/28/2019
      Best Response

    Hi Taylor,

    Thanks for the reply! I reinstalled the 64 version and it is running with python now. Not sure what the problem was earlier, but potentially related to Windows registry.

    Thanks,
    Charles