Vensim (DDE) + Python

Use this forum to post Vensim related questions.
Post Reply
apix
Senior Member
Posts: 64
Joined: Wed Mar 16, 2011 3:31 pm

Vensim (DDE) + Python

Post 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.
Administrator
Super Administrator
Posts: 4590
Joined: Wed Mar 05, 2003 3:10 am

Re: Vensim (DDE) + Python

Post by Administrator »

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
apix
Senior Member
Posts: 64
Joined: Wed Mar 16, 2011 3:31 pm

Re: Vensim (DDE) + Python

Post 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!
Administrator
Super Administrator
Posts: 4590
Joined: Wed Mar 05, 2003 3:10 am

Re: Vensim (DDE) + Python

Post by Administrator »

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
apix
Senior Member
Posts: 64
Joined: Wed Mar 16, 2011 3:31 pm

Re: Vensim (DDE) + Python

Post 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.
Administrator
Super Administrator
Posts: 4590
Joined: Wed Mar 05, 2003 3:10 am

Re: Vensim (DDE) + Python

Post by Administrator »

Does "c.Exec(comando)" return a value? If yes, what is the value of nResult below?

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
apix
Senior Member
Posts: 64
Joined: Wed Mar 16, 2011 3:31 pm

Re: Vensim (DDE) + Python

Post by apix »

nResult = None :cry:
Administrator
Super Administrator
Posts: 4590
Joined: Wed Mar 05, 2003 3:10 am

Re: Vensim (DDE) + Python

Post 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)
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
apix
Senior Member
Posts: 64
Joined: Wed Mar 16, 2011 3:31 pm

Re: Vensim (DDE) + Python

Post 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
tomfid
Administrator
Posts: 3811
Joined: Wed May 24, 2006 4:54 am

Re: Vensim (DDE) + Python

Post 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').
Administrator
Super Administrator
Posts: 4590
Joined: Wed Mar 05, 2003 3:10 am

Re: Vensim (DDE) + Python

Post by Administrator »

Tom is right. It could be that Vensim is stopping at the space so is trying to run
"[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
tomfid
Administrator
Posts: 3811
Joined: Wed May 24, 2006 4:54 am

Re: Vensim (DDE) + Python

Post 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
apix
Senior Member
Posts: 64
Joined: Wed Mar 16, 2011 3:31 pm

Re: Vensim (DDE) + Python

Post 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.
Post Reply