• 800 views | 6 messages Discussion: LEAP
    Topic: Problem with vbscript and LEAP API Subscribe | Previous | Next
  • Shoibal Chakravarty 1/6/2013

    1962 Views

    Hi,
    I am trying to extract data from a LEAP model. I encounter two problems:
    1. Variable.DataValue() is always 0.
    2. The API documentation says that either a variable either has ResultValue or DataValue property but I seems to get 0 for both without any error message. Here's my script for Freedonia. I am not sure what the source of error is.

    Thanks in advance!
    Shoibal.

    'VBScript for Freedonia'
    SET LEAP = CreateObject("LEAP.LEAPApplication")
    LEAP.Verbose = 4
    LEAP.Visible = false
    LEAP.ActiveArea = "Freedonia"
    WScript.Sleep 10000
    LEAP.ActiveView = "Results"
    LEAP.ActiveRegion = "All Regions"
    LEAP.ActiveBranch = "Demand"

    FOR EACH S IN LEAP.Scenarios
    S.Active = TRUE
    FOR EACH B IN LEAP.Branch("Demand").Children
    FOR EACH V in B.Variables
    FOR YR = LEAP.BaseYear to LEAP.EndYear
    Wscript.Echo S, B.FullName, V.Name, YR, V.DataValue(YR), V.ResultValue(YR), V.DataUnitText
    NEXT
    NEXT
    NEXT
    NEXT
    'End Script'
  • Shoibal Chakravarty 1/10/2013
      Best Response

    1936 Views

    Hi,

    Has anybody else faced this problem? I can use the API to read integers (LEAP.BaseYear), strings (Variable.Expression) etc from the model but I always get 0.0 when I am reading a float. I am not sure what the problem is. I am running LEAP on a Windows 7 64 bit installation.


    Any help will be welcome!
    Thanks,
    Shoibal.
  • Charlie Heaps 1/18/2013
      Best Response

    1 Like 1930 Views

    I'm reposting something here that I emailed earlier to you Shoibal - so that all can see it...

    Below is a script that does something similar to what you had. This one is designed to run INSIDE LEAP in the Advanced: Edit Scripts window. You can also save it as a VBS file...

    CLS ‘ This just clears the area of the screen used to receive print statements.
    Area = "Freedonia"
    PRINT "Export LEAP Data" ‘ The print statement causes text to be sent to the lower part of the script editing screen.

    LEAP.ActiveArea = AREA

    for each s in LEAP.Scenarios
    if (S.ResultsShown) and (S.ID <> 1) then 'Only show scenarios that have been marked for calculation and not Current Accounts
    s.Active = TRUE
    PRINT "Scenario: " & S.Name & ", Energy Demand Results"
    for each b in LEAP.Branch("Demand").Children
    SET V = B.Variable("Energy Demand Final Units") ‘I have noticed that VBScript is quite picky. Sometimes needs the SET statement
    for yr = LEAP.BaseYear to LEAP.EndYear
    if yr mod 5 = 0 then 'only do every 5 years
    PRINT b.FullName & ": " & yr & ", " & Round(v.Value(Yr,"TJ"),2) & " TJ"
    end if
    next ‘yr
    next ‘branch
    end if
    next ‘scenario

    My suggestion is to try prototyping these scripts directly within LEAP before running them elsewhere (eg in Excel). Note that when you create a script in LEAP you don’t have to explicitly create the LEAP object. That is already created implicitly. The advantage of prototyping in LEAP is that you can make use of the autocomplete and other editing features within the script window.

    You can run the above code by pasting it into the script editor (under menu option Advanced: Edit Scripts)

    One thing I did different is using the V.Value syntax. The V.ResultValue and V.DataValue functions are no longer functional. Note also that I am only printing out results for particular scenarios and also I am only doing one result variable (Energy Demand Final Units). Note also that some variables are data variables, while others are result variables.

  • Fran Lallana 1/23/2013
      Best Response

    1926 Views

    About writing scripts inside Leap..

    Charlie, I've been looking the script you posted long time ago, named "goal seek", cause I'm thinking use it to iterate over the reserve margin.

    The impact should be obtain (and verify) in a yearly base, but I think your script could be a fair starting point.

    I found it in a post of May of 2006 and you said it's attached, but it's no longer available. Could you paste it in this thread?

    Thanks!.
    Best, Fran.

    PS. I'm very grateful for the "Add" function you created, it's great.



  • Charlie Heaps 1/23/2013
      Best Response

    1925 Views

    Hi Fran,

    I will try and dig it out and get it working again. The API has evolved since I wrote that, so I may need to change the code a little to get it working. But first things first - I have to find it!

    Give me a few days...
  • Fran Lallana 1/23/2013
      Best Response

    1920 Views

    No Charlie,
    Don't do it. I thought It was only find it...

    I'm not sure it'll be useful (of course something would help, but I have to do somethig quite diffrent), so I try, and I let you know how it goes.

    Thanks,
    Fran.