Page 1 of 1

Venapp and External function - passing variable

Posted: Thu Sep 29, 2011 10:52 am
by apix
Hi everybody!

I have to pass the name of a selected variable and a selected time step to an external application from a Venapp interface.
So I need to pass the name of the variable, not the value.

Venapp has the command "SPECIAL>VARSELECT" that let the user select the variable to build a graph. I would like to catch that choice and use it in an external function, in order to pass the name of the variable to my custom application.

Any suggestion is welcome!

Many thanks

(I can't program well with language C/C++.)

Re: Venapp and External function - passing variable

Posted: Thu Sep 29, 2011 11:27 am
by Administrator
You will need to program in C/C++ to do this.

In the external function library you will find a GLOB_VARS structure, this contains a function pointer to "get_info", you can use this to retrieve the active workbench variable which you have selected using VARSELECT.

Re: Venapp and External function - passing variable

Posted: Thu Sep 29, 2011 4:34 pm
by tomfid
It might help if you describe what you're trying to accomplish.

Re: Venapp and External function - passing variable

Posted: Fri Sep 30, 2011 1:26 pm
by apix
I have some variables binded to geographical zones (subscripts) and I want to create a thematic map with each of them.
My Venapp interface should let me choice a variable and a year, an external function should pass the name of the selected variable and the year to a python script.
The python script ask the vensim.dll the values of each subscripted variable and create the final bitmap image, that I can view in Venapp.
I miss the way to pass the name of the variable to the external function.

I'll learn more about the GLOB_VARS structure, but I have difficulties with C.

Thank you for the help. :)

Re: Venapp and External function - passing variable

Posted: Fri Sep 30, 2011 2:09 pm
by Administrator
What you are trying to do will not be possible with external functions. For a start, you cannot use the Vensim DLL to get values, you will need to use the GLOB_VARS structure, and external functions are only executed when you press the go button on a game/simulation (what you describe is creating a report for display).

I'd suggest writing the whole interface outside of Vensim.

Re: Venapp and External function - passing variable

Posted: Fri Sep 30, 2011 7:22 pm
by tomfid
One possibility - if there aren't too many variables - would be to dump out all the data at the end of a run, and have python generate bitmaps for every map. Then you could load the bitmaps as needed from within Venapp. For that to work, you'd have to be able to launch the python script, which might be possible with the SHELL command. Not sure, as I haven't tried anything like this.

Re: Venapp and External function - passing variable

Posted: Mon Oct 03, 2011 8:21 am
by apix
I got it almost!!

Thanks to GLOB_VARS and get_info (option 21), as you told me, I can pass the name of the selected variable to my python script (through a shell command).

I miss just one step (I hope).

If I assign my external function to a variable ( C=MYFUNCT() ) it works, but every time step.
As I want it to work just after the simulation, I thought to use the "SPECIAL>EXTERNAL|MYFUNCT()" command, but it doesn't work, nothing happens.
I tried many combination, with:
"SPCECIAL>LOADDLL|VENEXT.DLL"
and
"SETTING>EXTTERNALFUNCTION|VENEXT.DLL"
as button command or normal command, with the function name lower-case or upper-case, with/without round brackets.

I'm sure the dll is loaded (an error message appears when it fails), but Venapp can't read the function as a button command.

Is it possible?
Thanks a lot.

Re: Venapp and External function - passing variable

Posted: Mon Oct 03, 2011 9:41 am
by Administrator
There is also a function in the external function library called simulation_shutdown. This is called once simulation is complete, can you use that?

Tony.

Re: Venapp and External function - passing variable

Posted: Mon Oct 03, 2011 10:05 am
by apix
BUTTON,"shutdown",30,25,,,,,"SPECIAL>EXTERNAL|simulation_shutdown",
BUTTON,"shutdown",30,25,,,,,"SPECIAL>EXTERNAL|simulation_shutdown()",
BUTTON,"shutdown",30,25,,,,,"SPECIAL>EXTERNAL|simulation_shutdown(0)",
BUTTON,"shutdown",30,25,,,,,"SPECIAL>EXTERNAL|simulation_shutdown(1)",

I tried the four commands:
1. Venapp closes but with an error message (it asks me if i want to warn Microsoft)
2., 3., or 4. Nothing happens

Re: Venapp and External function - passing variable

Posted: Mon Oct 03, 2011 11:41 am
by Administrator
No this will not work. Have a look at the following help entry
http://www.vensim.com/documentation/ind ... ?25435.htm

"SPECIAL>EXTERNAL" is not related to external functions in any way.

Look in the code for your external function library, it will contain a function called simulation_shutdown. To test it's operating ok, add in a call to display a messagebox (the function is something like ::MsgBox). Now when you run the simulation (in Vensim or Venapp), it will call simulation_shutdown at the end and the messagebox should display.

You could also try creating a function in your DLL (eg called "DoStuff"), make sure you export it. Now execute the following command.
SPECIAL>LOADDLL|YourExternalFunctionLib.dll
Then try
SPECIAL>EXTERNAL|DoStufff

That might work.

Re: Venapp and External function - passing variable

Posted: Tue Oct 04, 2011 8:23 am
by apix
I see the light...

As I told, I'm newbie in programming, but I found how to export my function:

At the end of VENEXT.C I wrote the function:

Code: Select all

int VEFCC testfunc()
 {
  MessageBox(GetActiveWindow(),"it works","Apix",
               MB_ICONSTOP | MB_OK) ;
  return(1) ;
  }
and in VENEXT.DEF, under EXPORTS, I added the line

Code: Select all

testfunc @2


In the Venapp file I added:

Code: Select all

COMMAND,"",,,,,,,"SPECIAL>LOADDLL|venext.dll",
BUTTON,"prova funzione esterna",15,19,,,,,"SPECIAL>EXTERNAL|testfunc",
and tried it. All work fine!

Then I tried to insert the code of yesterday in my function (including some libraries: #include "stdlib.h", #include "stdio.h", #include <string.h>):

Code: Select all

int VEFCC testfunc()
 {
  char comando[150];
  char variabile[100] ;
  (*VENGV->get_info)(21,variabile,100) ;
  sprintf(comando,"viewvariable.py %s",variabile) ;
  system(comando);
  MessageBox(GetActiveWindow(),"funge","Apix",
               MB_ICONSTOP | MB_OK) ;
  return(1) ;
  }
but when I click the button, Venapp crashes.

What's the problem?? My function can't work with GLOB_VARS ??

Thank you guys, you are very nice!!

Re: Venapp and External function - passing variable

Posted: Tue Oct 04, 2011 9:07 am
by Administrator
You will need to step through the code using the debugger. It may well be that VENGV is not set in your DLL which would cause a crash. But there is no way of knowing unless you step through the code.

Re: Venapp and External function - passing variable

Posted: Wed Oct 05, 2011 1:07 pm
by apix
I'm pretty lucky!

One day I'll understand what happened but in the meanwhile it works, this is the important thing.

Using both:

Code: Select all

COMMAND,"",,,,,,,"SETTING>EXTERNALFUNCTION|venext.dll",
and

Code: Select all

BUTTON,"prova funzione esterna",15,19,,,,,"SPECIAL>LOADDLL|venext.dll&SPECIAL>EXTERNAL|testfunc",
I can use my function testfunc that read the GLOB_VARS without having crashes of Venapp.

Thank you for your help!

Re: Venapp and External function - passing variable

Posted: Mon Oct 24, 2011 8:00 am
by apix
Hi again,

I would need your help for the final missing step in my Venapp interface.

Now,
1. I can select an existing variable with the command SPECIAL>VARSELECT,
2. I check it with WBVAR and SETWB
3. and I catch it through GLOB_VARS and get_info to pass it to a third application.

Through a SLIDEVAR command I'd like to choose a number (corrisponding to a time step, but it hasn't to do modfy model inputs) and catch it like the workbench variable.
Any suggestion about it? I tried in several ways but without success.

Many thanks!

Re: Venapp and External function - passing variable

Posted: Mon Oct 24, 2011 8:25 am
by Administrator
Just create a dummy constant, set that using the slider, and then you should be able to get it via GLOB_VARS.

If you are actively running a simulation/game, you will need to make the dummy constant a gaming variable.

Is this what you are trying?

Re: Venapp and External function - passing variable

Posted: Fri Oct 28, 2011 1:43 pm
by apix
I tried that ways, but without success.

If I set my dummy variable as a constant, I can use the SLIDEVAR command, but it takes effect only in the next simulation.
If the variable is a gaming auxiliar variable, I can't use the SLIDEVAR.

What I want to do should be independent from the model: I should be able to set and get the value after the simulation (without a new run).

Am I wrong about the use of gaming variables?

Re: Venapp and External function - passing variable

Posted: Sat Oct 29, 2011 1:59 pm
by Administrator
I'm not sure what you are doing, but it sounds like you have the order of things mixed up.

You can set constants before the simulation starts using a slidevar, and gaming vars during a game. If your changes require a 2nd simulation to take effect, maybe you have started the simulation before trying to set the var.

Re: Venapp and External function - passing variable

Posted: Fri Apr 26, 2013 10:58 am
by apix
Just a confirmation:

I loaded a model by a Venapp interface.
In parallel I ran a programm that uses the DLL.
Can the second programm, trhough the DLL, ask for info (vensim_get_info) on the model loaded by the Venapp interface? (For example, I'd like to get the names of the currently loaded runs, infowanted=10)

I don't think so, because Vensim is able to have multiple process running, isn't it?

Thanks.

Re: Venapp and External function - passing variable

Posted: Fri Apr 26, 2013 11:58 am
by Administrator
I don't think so, because Vensim is able to have multiple process running, isn't it?
Correct. They are separate processes so no communication between them.