Vensim (DDE) + Python
Vensim (DDE) + Python
Hi everybody,
I need to connect a Python script to Vensim, using the last one as DDE server.
I looked at DDESERV.XLS file in the samples directory of Vensim and learned how VBA functions work.
I found a nice module for Python, able to connect, for example, Python to MS Access (that works as DDE Server). The python script works with Access, but not with Vensim:
The script get connected to Vensim but it can't load any model.
Can you help me?
Many thanks, bye bye.
I need to connect a Python script to Vensim, using the last one as DDE server.
I looked at DDESERV.XLS file in the samples directory of Vensim and learned how VBA functions work.
I found a nice module for Python, able to connect, for example, Python to MS Access (that works as DDE Server). The python script works with Access, but not with Vensim:
The script get connected to Vensim but it can't load any model.
Can you help me?
Many thanks, bye bye.
-
- Super Administrator
- Posts: 4589
- Joined: Wed Mar 05, 2003 3:10 am
Re: Vensim (DDE) + Python
Can you post anything for us to take a look at?
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Units are important!
http://www.bbc.co.uk/news/magazine-27509559
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Units are important!
http://www.bbc.co.uk/news/magazine-27509559
Re: Vensim (DDE) + Python
Of course, you're right, sorry.
Here's my script:
I'm working on a Win XP SP3 machine with Python 2.5.4 and pywin32-207.win32-py2.5.
First I open Vensim without loading any model, then I run the script and I get the message "Connection established".
Thanks for your help!
Here's my script:
Code: Select all
import win32ui
import dde
s = dde.CreateServer()
s.Create("TestClient")
c = dde.CreateConversation(s)
c.ConnectTo("VENSIM", "System")
if c.Connected() == 1:
print "Connection established"
comando = "[SPECIAL>LOADMODEL|C:\Program Files\Vensim\models\sample\EXTRA\ball.mdl]"
c.Exec(comando)
First I open Vensim without loading any model, then I run the script and I get the message "Connection established".
Thanks for your help!
-
- Super Administrator
- Posts: 4589
- Joined: Wed Mar 05, 2003 3:10 am
Re: Vensim (DDE) + Python
And what error message are you getting on "c.Exec(comando)" line?
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Units are important!
http://www.bbc.co.uk/news/magazine-27509559
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Units are important!
http://www.bbc.co.uk/news/magazine-27509559
Re: Vensim (DDE) + Python
Actually I don't get any error message but Vensim doesn't show (load) any model, as indeed it does with excel+dde.
-
- Super Administrator
- Posts: 4589
- Joined: Wed Mar 05, 2003 3:10 am
Re: Vensim (DDE) + Python
Does "c.Exec(comando)" return a value? If yes, what is the value of nResult below?
nResult = c.Exec(comando)
nResult = c.Exec(comando)
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Units are important!
http://www.bbc.co.uk/news/magazine-27509559
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Units are important!
http://www.bbc.co.uk/news/magazine-27509559
Re: Vensim (DDE) + Python
nResult = None
-
- Super Administrator
- Posts: 4589
- Joined: Wed Mar 05, 2003 3:10 am
Re: Vensim (DDE) + Python
Another question. Is Python similar to C where you need double \\?
What happens if you try
comando = "[SPECIAL>LOADMODEL|C:\\Program Files\\Vensim\\models\\sample\\EXTRA\\ball.mdl]"
c.Exec(comando)
What happens if you try
comando = "[SPECIAL>LOADMODEL|C:\\Program Files\\Vensim\\models\\sample\\EXTRA\\ball.mdl]"
c.Exec(comando)
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Units are important!
http://www.bbc.co.uk/news/magazine-27509559
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Units are important!
http://www.bbc.co.uk/news/magazine-27509559
Re: Vensim (DDE) + Python
Nothing happens, no error messages, no value for nResult.
Strange.
If I use the same code to connect the Python script to a MS Access database, it works:
I can see the database loaded, even if nResult gets value "None".
Strange.
If I use the same code to connect the Python script to a MS Access database, it works:
I can see the database loaded, even if nResult gets value "None".
Code: Select all
import win32ui
import dde
s = dde.CreateServer()
s.Create("TestClient")
c = dde.CreateConversation(s)
c.ConnectTo("MSAccess", "System")
if c.Connected() == 1:
print "Connection established"
comando = "[OpenDatabase 'C:\db1.mdb']"
nResult = c.Exec(comando)
print nResult
Re: Vensim (DDE) + Python
You might try moving the ball.mdl to a location with a simpler path, as with your mdb example, to see if the problem is an issue with the path (like the space in 'program files').
/*
Advice to posters (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Blog: http://blog.metasd.com
Model library: http://models.metasd.com
Bookmarks: http://delicious.com/tomfid/SystemDynamics
*/
Advice to posters (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Blog: http://blog.metasd.com
Model library: http://models.metasd.com
Bookmarks: http://delicious.com/tomfid/SystemDynamics
*/
-
- Super Administrator
- Posts: 4589
- Joined: Wed Mar 05, 2003 3:10 am
Re: Vensim (DDE) + Python
Tom is right. It could be that Vensim is stopping at the space so is trying to run
"[SPECIAL>LOADMODEL|C:\Program]"
"[SPECIAL>LOADMODEL|C:\Program]"
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Units are important!
http://www.bbc.co.uk/news/magazine-27509559
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Units are important!
http://www.bbc.co.uk/news/magazine-27509559
Re: Vensim (DDE) + Python
In any case, quoting the file path, as in
SPECIAL>LOADMODEL|"C:\Documents and Settings\All Users\Vensim\models\guide\CHAP03\wfinv.mdl"
shouldn't hurt.
I assume that in Python you'd have to escape the \ to \\ and the " to \" or something like that.
Tom
SPECIAL>LOADMODEL|"C:\Documents and Settings\All Users\Vensim\models\guide\CHAP03\wfinv.mdl"
shouldn't hurt.
I assume that in Python you'd have to escape the \ to \\ and the " to \" or something like that.
Tom
/*
Advice to posters (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Blog: http://blog.metasd.com
Model library: http://models.metasd.com
Bookmarks: http://delicious.com/tomfid/SystemDynamics
*/
Advice to posters (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Blog: http://blog.metasd.com
Model library: http://models.metasd.com
Bookmarks: http://delicious.com/tomfid/SystemDynamics
*/
Re: Vensim (DDE) + Python
Thank you for your advices, but quoting the path and/or simplifying it didn't solve the problems.
But, if I first run the excel sample file and then run the python script, I can get the results of the model.
That means that the scipt actually get connected to vensim and that the request command works fine.
There must be some problems with the execute command as you imagined.
I'll continue doing tests with it, thanks a lot.
But, if I first run the excel sample file and then run the python script, I can get the results of the model.
That means that the scipt actually get connected to vensim and that the request command works fine.
There must be some problems with the execute command as you imagined.
I'll continue doing tests with it, thanks a lot.