In the Part 1 we explored creating a simple project, now we will work with Visio interactively using the IronPython Interactive shell that is part of Python Tools for Visio Studio 2010.

Launch Visual Studio and Under Tools > Python Tools click on IronPython 2.7 Interactive

image

The window now appears.

image

image

The full script we used in the first post in this series works here, of course

---

 

import clr 
import System 
clr.AddReference("Microsoft.Office.Interop.Visio") 
import Microsoft.Office.Interop.Visio 
IVisio = Microsoft.Office.Interop.Visio

print('Hello world')

visapp = IVisio.ApplicationClass() 
doc = visapp.Documents.Add("") 
page = visapp.ActivePage 
shape = page.DrawRectangle(1, 1, 5, 4) 
shape.Text = "Hello World" 

You should be aware though that if you simply copy the code above and paste it into the interactive shell window, you’ll get this behavior:

image

This means that this is all treated as one line and of course that isn’t cool with Python. If you hit RETURN to execute you’ll see this:

image

If we are going to play around with Visio this way we don’t want to have to enter all the import statements and use clr.AddReference everytime we want to do something quick in Visio.

There are two solutions: use the Send to Interactive feature or use a separate .py file. The Send to Interactive feature allows you to select from text from a .py file in Visual Studio and correctly pushes it to the Interactive shell.

Send to Interactive screenshot:

image

However, because it will be more convenient for future blog posts, I’ll use the alternate solution of keeping a separate python file. We’ll save this as a file called ironvisio.py and then import that as needed in the interactive shell.

(In the screenshot below I have not saved the file yet so it is still called “TextFile1.txt”)

image

But where to put the file so we can import it later?

Let’s clean up the Interactive shell …

image

And then see what paths it is looking at

import sys
print sys.path

image

In this example, I’m going to place the file in “C:\Program Files (x86)\IronPython 2.7\Lib\site-packages” so that I always have access to it.

(Obviously there isn’t much going on in site-packages on my machine)

image

Oops… (I knew this would happen but wanted to show it)

image

if I click yes, then it’s going to save in c:\users\saveenr – which isn’t where the interactive shell can find it.

What are my options?

I could try setting the path every time, but I don’t want to have to remember to do this.

import sys
sys.path.append('d:\ironvisio')

Fortunately, all you have to do is set the IRONPYTHONPATH environment variable correctly and restart Visual Studio and your path will include the new folder.

From Start menu >Computer, right-click properties

image

click on Advanced system settings

image

Under the Advanced tab, click on Environment Variables

image

Click New

image

And enter the path as described below and close the Control Panel.

image

Now restart Visual Studio and launch the interactive shell and look at sys.path.

image

Good. It’s there.

If you are into powershell, you can use the following commands to set it for the user of the system. Make sure to run with elevated privileges when making this change.

[Environment]::SetEnvironmentVariable("IRONPYTHONPATH","d:\ironvisio","Machine")

[Environment]::SetEnvironmentVariable("IRONPYTHONPATH","d:\ironvisio","User")

 

 

I’ll now save this script into a file called d:\ironvisio\ironvisio.py

import clr 
import System 
clr.AddReference("Microsoft.Office.Interop.Visio") 
import Microsoft.Office.Interop.Visio 
IVisio = Microsoft.Office.Interop.Visio

image

Now I’ll do the import.

image

And start creating the Visio application class.

image

NOTE: Currently I am seeing that Intellisense is not always working immediately when I import the script inthis wasof the fully-qualified “Microsoft.Office.Interop.Visio”

So for example, this works…

image

But the following seems to interfere with Intellisense at the start – several minutes later it seems to kick in.

image

I haven’t sorted out why there is a delay in this case, but it is worth knowing so that you can avoid frustration if you really want a good Intellisense experience.

And now you can use the rest of the script to draw something.

image

As you can see, putting the initial code into a separate file we can import makes it convenient to get started interactively controlling Visio.

WHAT NEXT

  • We’ve taken a first step at using the Interactive shell. In the next post we are going to do some basic data-driven drawing. The eventual goal will be to do some nice Infographics using Visio.