Fun with IronPython & GLEE

I started this exercise thinking about visualizing the methods tree in a diagram. There are several ways to do that. I was planning to use the GraphViz project to render a dot file showing the connections between the methods. But then I found that pydot (python library for wrapping GraphViz) is not yet supported on IronPython and QuickGraph doesn't look like it is updated for some time.

Luckily, from a comment on the QuickGraph CodeProject article, I found the Microsoft Research Project called GLEE [http://research.microsoft.com/~levnach/GLEEWebPage.htm] which certainly looked a better choice. The limitation is that you can only use in non-commercial applications.

Before continuing on our Method Visualizer, let's do a small sample with GLEE. 

First, download and extract the GLEE assemblies

 

Now fire up IronPython shell from the same directory and add the necessary references.

>>> import clr
>>> clr.AddReference("Microsoft.GLEE")
>>> clr.AddReference("Microsoft.GLEE.Drawing")
>>> clr.AddReference("Microsoft.GLEE.GraphViewerGDI")

>>> clr.AddReference("System.Drawing")

>>> import Microsoft

As expected, the object model has a graph model which is the central point of reference. 

>>> graph = Microsoft.Glee.Drawing.Graph("Sample Graph")

Once you have the graph object which is like a surface, you can start adding edges in the surface.

>>> graph.AddEdge.__doc__
'IEdge AddEdge(self, str source, str edgeLabel, str target)\r\nIEdge AddEdge(self, str source, str target)'
>>> graph.AddEdge("PointA", "PointB")
<Microsoft.Glee.Drawing.Edge object at 0x000000000000002B ["PointA" -> "PointB"[color="#000000ff",fontcolor="#000000ff",,]]>
>>> graph.AddEdge("PointA", "PointC")
<Microsoft.Glee.Drawing.Edge object at 0x000000000000002C ["PointA" -> "PointC"[color="#000000ff",fontcolor="#000000ff",,]]>

We have 3 points on the graph now. Point A is connected to both PointB and PointC. Now, we'll render it and generate a diagram of the graph.

>>> renderer = Microsoft.Glee.GraphViewerGdi.GraphRenderer(graph)
>>> renderer.CalculateLayout()

After this, we can either attach it with a control that comes with the download. See this in action in the sample. Or we can render it as Image and save as file. Here, I use the second method.

To use the bitmap object, we need to import the System.Drawing namespace

>>> from System.Drawing import *
>>> from System.Drawing.Imaging import *
>>> bmp = Bitmap(graph.Width, graph.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
>>> renderer.Render(bmp)
>>> bmp.Save("IronGlee.png")

To add color, we need to find the node ( by the name we used in AddEdge) and set the color

>>> graph.FindNode("PointA").NodeAttribute.Fillcolor = Microsoft.Glee.Drawing.Color.LightGreen
>>> renderer = Microsoft.Glee.GraphViewerGdi.GraphRenderer(graph)
>>> renderer.Render(bmp)
>>> bmp.Save("IronGleeColored.png")

GLEE looked like an interesting project which apart from its license, doesn't seem to have any issues. Next, we'll use GLEE to generate a method tree.

No Comments