使用C#在Visio流程图中遍历所有可能的路径 [英] Traverse through every possible path in a Visio Flow-Chart with C#

查看:597
本文介绍了使用C#在Visio流程图中遍历所有可能的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用C#,并且我必须导入一个Visio文件,该文件包含具有不同路径的流程图.

I recently started to work with C# and I have to import a Visio file that is including a flow-chart with different path.

我使用此代码加载文件.

I load the file with this code.

public Container loadFile(string fileName)
{
    Microsoft.Office.Interop.Visio.Application app = new Microsoft.Office.Interop.Visio.Application();
    app.Visible = false;
    Documents docs = app.Documents;
    Document doc = docs.Open(fileName);
    Microsoft.Office.Interop.Visio.Page page = doc.Pages[1];
    Container container = printProperties(page.Shapes);
    return container; 
}

public Container printProperties(Microsoft.Office.Interop.Visio.Shapes shapes)
{
    Container container = new Container("Visio Import");
    container.setParent(null);

    // Look at each shape in the collection.
    foreach (Microsoft.Office.Interop.Visio.Shape shape in shapes)
    {
        // traverse
    }

    return container;
}

我想遍历流程图的每个可能的(!)路径并打印进程名称.例如

I want to traverse through every possible (!) path of the flow-chart and print the process names. E.g.

Path 1:
- Enter PIN
- Select Account
- Select Amount
- Print Receipt
- Take Money

Path 2:
- Enter PIN
- Select Account
- Check Money
- Abort

您能告诉我如何检查单个流程之间的连接并遍历吗?非常感谢您的帮助!

Can you tell me how to check the connections between the single processes and traverse it? Thank you very much for your help!

推荐答案

我有执行此操作的代码,但由于它是商业产品的一部分,所以我无法共享它.

I have code that does this, but I cannot share it, since it's part of a commercial product.

但是,我可以告诉你,我在Visio中解决此问题的方式是,我首先在VBA中编写了一组非常通用的有向图类:一个用于节点,一个用于边缘,一个用于图形作为所有的.我在图形类中内置了循环路径检查,以及用于在图形中查找所有路径的代码.

However, I can tell you that the way I coped with doing this within Visio was, I first wrote a set of very generic directed graph classes in VBA: one for nodes, one for edges, and one for the graph as a whole. I built circular path checks into the graph class, as well as the code for finding all paths in the graph.

然后,我有一些代码可以读取Visio页面并填充此简单的图形表示形式,并调用适当的代码.

Then I had some code that would read the Visio page and populate this simple graph representation, and call the appropriate code.

我认为这也可能对您也有好处,因为Visio方面不可避免地比简单的有向图实现更混乱.因为必须支持Visio 2003,所以我没有使用API​​的ConnectedShapes部分,因此实际上我查看了形状上的Connects和FromConnects对象,以查看将哪个OneD连接器附加到形状上,并确定形状是否正确.在箭头的头部或尾部.这是将图形部分与Visio部分分开的另一个原因,因为我们读取Visio页面的方式会随着时间而变化,但是图形理论将保持不变.

I think this would probably be good for you to do, too, since the Visio side of things will inevitably be messier than a simple directed graph implementation. I didn't use the ConnectedShapes part of the API since I have to support down to Visio 2003, so I actually look at the Connects and FromConnects objects on my shapes to see what OneD connectors are attached to a shape, and determine whether a shape is at the head or tail of an arrow. This is another reason to break the graph part away from the Visio part, since the way we read the Visio page is subject to change over time, but the graph theory will stay the same.

寻路算法的工作原理是首先找到图中的所有终端节点,我的意思是那些没有下游节点的节点.对于其中的每一个,我添加一个名为DownstreamPaths的列表,该列表为空,因为下游没有任何内容.然后,对于图中的每个节点,我调用一个递归函数,该函数填充当前节点的所有下游路径,并且基本上它所做的就是在每个节点上构建一个DownstreamPaths列表.该列表是列表的列表,因此您只需查看每个下游节点,然后将该节点附加在其自己的DownstreamPaths列表的头部,然后将其添加到当前节点的路径列表中即可. 完成这些操作后,您将找到所有起始节点,而上游没有任何内容,并整理这些节点上的所有下游路径列表,然后获得路径列表.

The path-finding algorithm works by first finding all the terminal nodes in the graph, and I mean those nodes with no Downstream nodes. For each of these I add a list called DownstreamPaths, which is just empty since there is nothing downstream. Then for each node in the graph I call a recursive function that populates all the downstream paths for the current node, and basically all it does is builds a DownstreamPaths list on each node. This list is a list of lists, so you just look at each downstream node, and append that node on the head of its own DownstreamPaths list, and add that into the current node's path list. When that's all done, you find all the starting nodes, with nothing upstream, and collate all the downstream paths lists on those, and you get your list of paths.

这篇关于使用C#在Visio流程图中遍历所有可能的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆