Page 1 of 1

Vensim (DDE) + Python

Posted: Wed Mar 16, 2011 4:52 pm
by apix
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.

Re: Vensim (DDE) + Python

Posted: Wed Mar 16, 2011 6:29 pm
by Administrator
Can you post anything for us to take a look at?

Re: Vensim (DDE) + Python

Posted: Fri Mar 18, 2011 8:43 am
by apix
Of course, you're right, sorry.
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)
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!

Re: Vensim (DDE) + Python

Posted: Fri Mar 18, 2011 1:28 pm
by Administrator
And what error message are you getting on "c.Exec(comando)" line?

Re: Vensim (DDE) + Python

Posted: Fri Mar 18, 2011 1:51 pm
by apix
Actually I don't get any error message but Vensim doesn't show (load) any model, as indeed it does with excel+dde.

Re: Vensim (DDE) + Python

Posted: Fri Mar 18, 2011 2:06 pm
by Administrator
Does "c.Exec(comando)" return a value? If yes, what is the value of nResult below?

nResult = c.Exec(comando)

Re: Vensim (DDE) + Python

Posted: Fri Mar 18, 2011 2:21 pm
by apix
nResult = None :cry:

Re: Vensim (DDE) + Python

Posted: Fri Mar 18, 2011 2:43 pm
by Administrator
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)

Re: Vensim (DDE) + Python

Posted: Fri Mar 18, 2011 2:57 pm
by apix
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".

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

Posted: Fri Mar 18, 2011 4:55 pm
by tomfid
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').

Re: Vensim (DDE) + Python

Posted: Fri Mar 18, 2011 6:36 pm
by Administrator
Tom is right. It could be that Vensim is stopping at the space so is trying to run
"[SPECIAL>LOADMODEL|C:\Program]"

Re: Vensim (DDE) + Python

Posted: Fri Mar 18, 2011 6:51 pm
by tomfid
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

Re: Vensim (DDE) + Python

Posted: Mon Mar 21, 2011 10:49 am
by apix
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.