Someone asked me today for a Python-only example illustrating how to automation the following in Visio: Drop two masters and connect them. I wasn’t sure if “Python-only” included IronPython, so I decided to refresh myself on how Python (specifically Python 2.6 with the Python Win32 Extensions) code would look to the IronPython 2.0 code.
Note: it doesn’t make use of helper libraries like VisioAutoExt)
# # Visio: Connecting Shapes - IronPython 2.0 Example # import sys import clr import System clr.AddReference("Microsoft.Office.Interop.Visio") import Microsoft.Office.Interop.Visio IVisio = Microsoft.Office.Interop.Visio visapp = IVisio.ApplicationClass() doc = visapp.Documents.Add("") page = visapp.ActivePage stencilname = "basic_u.vss" stencildocflags = IVisio.VisOpenSaveArgs.visOpenRO | IVisio.VisOpenSaveArgs.visOpenDocked stencildoc = visapp.Documents.OpenEx(stencilname , stencildocflags ) masterrect = stencildoc.Masters.ItemU["rectangle"] masteroctagon = stencildoc.Masters.ItemU["octagon"] masterconnector= stencildoc.Masters.ItemU["dynamic connector"] shape1 = page.Drop( masterrect, 1,1 ) shape2 = page.Drop( masteroctagon, 4,3 ) connector1 = page.Drop( masterconnector, -1,-1) connector1.CellsU( "BeginX" ).GlueTo(shape1.CellsSRC(1, 1, 0)) connector1.CellsU( "EndY" ).GlueTo(shape2.CellsSRC(1, 1, 0))
# # Visio: Connecting Shapes - IronPython 2.0 Example #
import sys import clr import System clr.AddReference("Microsoft.Office.Interop.Visio") import Microsoft.Office.Interop.Visio IVisio = Microsoft.Office.Interop.Visio visapp = IVisio.ApplicationClass()
doc = visapp.Documents.Add("") page = visapp.ActivePage
stencilname = "basic_u.vss" stencildocflags = IVisio.VisOpenSaveArgs.visOpenRO | IVisio.VisOpenSaveArgs.visOpenDocked stencildoc = visapp.Documents.OpenEx(stencilname , stencildocflags )
masterrect = stencildoc.Masters.ItemU["rectangle"] masteroctagon = stencildoc.Masters.ItemU["octagon"] masterconnector= stencildoc.Masters.ItemU["dynamic connector"]
shape1 = page.Drop( masterrect, 1,1 ) shape2 = page.Drop( masteroctagon, 4,3 ) connector1 = page.Drop( masterconnector, -1,-1)
connector1.CellsU( "BeginX" ).GlueTo(shape1.CellsSRC(1, 1, 0)) connector1.CellsU( "EndY" ).GlueTo(shape2.CellsSRC(1, 1, 0))
# # Visio: Connecting Shapes - Python 2.6 Example via COM # import sys import win32com.client win32com.client.gencache.EnsureDispatch("Visio.Application") visapp = win32com.client.Dispatch("Visio.Application") doc = visapp.Documents.Add("") page = visapp.ActivePage stencilname = "basic_u.vss" stencildocflags = win32com.client.constants.visOpenRO | win32com.client.constants.visOpenDocked stencildoc = visapp.Documents.OpenEx(stencilname , stencildocflags ) masterrect = stencildoc.Masters.ItemU("rectangle") masteroctagon = stencildoc.Masters.ItemU("octagon") masterconnector= stencildoc.Masters.ItemU("dynamic connector") shape1 = page.Drop( masterrect, 1,1 ) shape2 = page.Drop( masteroctagon, 4,3 ) connector1 = page.Drop( masterconnector, -1,-1) connector1.CellsU( "BeginX" ).GlueTo(shape1.CellsSRC(1, 1, 0)) connector1.CellsU( "EndY" ).GlueTo(shape2.CellsSRC(1, 1, 0))
# # Visio: Connecting Shapes - Python 2.6 Example via COM #
import sys import win32com.client win32com.client.gencache.EnsureDispatch("Visio.Application") visapp = win32com.client.Dispatch("Visio.Application") doc = visapp.Documents.Add("") page = visapp.ActivePage
stencilname = "basic_u.vss" stencildocflags = win32com.client.constants.visOpenRO | win32com.client.constants.visOpenDocked stencildoc = visapp.Documents.OpenEx(stencilname , stencildocflags )
masterrect = stencildoc.Masters.ItemU("rectangle") masteroctagon = stencildoc.Masters.ItemU("octagon") masterconnector= stencildoc.Masters.ItemU("dynamic connector")
And for all 3 of my regular readers, here is the IronPython code when using the VisioInteractive components of my VisioAutoExt library. Strictly speaking, it’s unfair to compare this to Sample 1 and Sample 2 because VisioInteractive is built to optimize such tasks.
# # Visio: Connecting Shapes - IronPython via VisioInteractive shell # from visiointeractive import * vi.Start() vi.Document.New() shape1 = vi.Drop.Master( "basic_u.vss", "rectangle" , 1,1 ) shape2 = vi.Drop.Master( "basic_u.vss", "octagon", 4,3 ) vi.Connect.Shapes( shape1, shape2 )
# # Visio: Connecting Shapes - IronPython via VisioInteractive shell #
from visiointeractive import *
vi.Start() vi.Document.New() shape1 = vi.Drop.Master( "basic_u.vss", "rectangle" , 1,1 ) shape2 = vi.Drop.Master( "basic_u.vss", "octagon", 4,3 ) vi.Connect.Shapes( shape1, shape2 )
NOTE: this only covers Sample 1 vs Sample 2. Sample 3 is not part of the discussion.
def foo(self) : print “the Shape’s ID is”, self.ID
>>> shape1.__class__.foo = foo Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: '__ComObject' object has no attribute 'foo'
>>> shape1.__class__.foo = foo >>> shape1.foo() >>> the Shape’s ID is 1