Friday, February 20, 2009

Visio from IronPython and Powershell, Parts II & III

Saveen Reddy posted recently on automating Visio to draw diagrams from the command line. He has followed this up with two more posts. Both posts use the Visio automation API from IronPython and Powershell, and have plenty of screenshots to show you the results of the interactive code examples he presents.
This post discusses using data to generate your diagrams; specifically tabular, hierarchical data, and directed graphs:
Draw some simple shapes

>>> vi.Draw.Rectangle( 0, 0, 1,1 )
>>> vi.Draw.Oval( 2, 2, 3,3 )
>>> vi.Draw.Line( 4, 4, 5,5 )

Loading data from a CSV file exported from Excel.

The interactive shell extensively uses System.Data.DataTable to store tabular data

>>> data = ( ('Hello',1) , ('World',2) )
>>> datatable = ToDataTable( data )
>>> vi.Draw.Table( datatable )

create a CSV file in Excel

And then load it as a DataTable and let Visio Draw it

>>> datatable = vi.Data.ImportCSV( r"D:\saveenr\data1.csv" )
>>> vi.Draw.Table( datatable )

Of course, you can load an XLSX file. In this case, you’ll have to identify the name of the worksheet also…

>>> datatable = vi.Data.ImportExcelWorksheet( r"d:\\data1.xlsx" , "Sheet1" )
>>> vi.Draw.Table( datatable )

Drawing Hierarchical Data

Let’s draw a tree from a set of directory paths

items.Add( r'c:' )
items.Add( r'c:\windows' )
items.Add( r'c:\windows\system' )
items.Add( r'c:\windows\system32' )
items.Add( r'c:\windows\tasks' )
items.Add( r'c:\program files' )
items.Add( r'c:\program files\office live' )
items.Add( r'c:\baz' )
dir = Isotope.Drawing.CardinalDirection.Down
tree = vi.Data.PathsToTree( items )
vi.Draw.Tree( tree.Root , dir )

Automatic Layout Directed Graphs

Technical Note: the AutoLayoutDrawing class uses Microsoft’s Automatic Graph Layout library

d= VisioDOM.AutoLayout.AutoLayoutDrawing()
s1= d.AddShape('A')
s2= d.AddShape('B')
s3= d.AddShape('C')
c1 = d.Connect('c1',s1,s2)
c2 = d.Connect('c2',s1,s3)
r = VisioDOM.AutoLayout.AutoLayoutRenderer()
r.RenderToVisio( d , vi.visapp , True)

This was a simple example, you can draw much more complicated diagrams using the autolayout feature.
This post demonstrates using Visio to draw some shapes, and then automatically generating Excel spreadsheets from the data. This data can also be exported as a CSV or XML file.

He also connects the shapes and then programmatically inspects the relationships between the objects:
In this diagram:
  • Sheet.1 is connected to Sheet.2
  • Sheet.1 is connected to Sheet.3
  • Sheet.2 is connected to Sheet.3
  • Sheet.2 is connected to Sheet.4
Let’s discover this programmatically

>>> pairs = vi.Connect.GetConnectedShapePairs()
>>> for pair in pairs:
>>> print pair.ConnectedShape0.Name, "is connected to", pair.ConnectedShape1.Name, “by way of”, pair.ConnectingShape.Name

This will print …

Sheet.1 is connected to Sheet.2 by way of Dynamic connector
Sheet.2 is connected to Sheet.3 by way of Dynamic connector.6
Sheet.4 is connected to Sheet.2 by way of Dynamic connector.7
Sheet.3 is connected to Sheet.1 by way of Dynamic connector.8

Using this information and looking up of the line endpoints for the connectors, you can identify create a directed graph. (QED)

No comments:

Post a Comment

Note: only a member of this blog may post a comment.