C# Visio-检查形状是否已连接
我正在创建visio图表,但需要检查现有形状是否已连接。我写了一个方法,用3种不同的方法来确定这一点。我找不到任何直接执行此操作的方法。这是我想到的。我更喜欢第三种方法,因为它不涉及迭代。还有什么建议吗C# Visio-检查形状是否已连接,c#,visio,C#,Visio,我正在创建visio图表,但需要检查现有形状是否已连接。我写了一个方法,用3种不同的方法来确定这一点。我找不到任何直接执行此操作的方法。这是我想到的。我更喜欢第三种方法,因为它不涉及迭代。还有什么建议吗 private bool ShapesAreConnected(Visio.Shape shape1, Visio.Shape shape2) { // in Visio our 2 shapes will each be connected to a connector
private bool ShapesAreConnected(Visio.Shape shape1, Visio.Shape shape2)
{
// in Visio our 2 shapes will each be connected to a connector, not to each other
// so we need to see if theyare both connected to the same connector
bool Connected = false;
// since we are pinning the connector to each shape, we only need to check
// the fromshapes attribute on each shape
Visio.Connects shape1FromConnects = shape1.FromConnects;
Visio.Connects shape2FromConnects = shape2.FromConnects;
foreach (Visio.Shape connect in shape1FromConnects)
{
// first method
// for each shape shape 1 is connected to, see if shape2 is connected
var shape = from Visio.Shape cs in shape2FromConnects where cs == connect select cs;
if (shape.FirstOrDefault() != null) Connected = true;
// second method, convert shape2's connected shapes to an IEnumerable and
// see if it contains any shape1 shapes
IEnumerable<Visio.Shape> shapesasie = (IEnumerable<Visio.Shape>)shape2FromConnects;
if (shapesasie.Contains(connect))
{
return true;
}
}
return Connected;
//third method
//convert both to IEnumerable and check if they intersect
IEnumerable<Visio.Shape> shapes1asie = (IEnumerable<Visio.Shape>)shape1FromConnects;
IEnumerable<Visio.Shape> shapes2asie = (IEnumerable<Visio.Shape>)shape2FromConnects;
var shapes = shapes1asie.Intersect(shapes2asie);
if (shapes.Count() > 0) { return true; }
else { return false; }
}
private bool shapes重新连接(Visio.Shape shape1,Visio.Shape shape2)
{
//在Visio中,我们的两个形状将分别连接到一个连接器,而不是彼此连接
//所以我们需要看看它们是否都连接到同一个连接器上
布尔连接=假;
//因为我们要将连接器固定到每个形状上,所以只需进行检查
//每个形状上的fromshapes属性
Visio.Connects shape1 FromConnects=shape1.FromConnects;
Visio.Connects shape2FromConnects=shape2.FromConnects;
foreach(shape1中的Visio.Shape connect fromconnects)
{
//第一种方法
//对于形状1连接到的每个形状,请查看是否连接了形状2
var shape=来自Visio。shape2FromConnects中的shape cs,其中cs==连接选择cs;
如果(shape.FirstOrDefault()!=null)Connected=true;
//第二种方法,将shape2的连接形状转换为IEnumerable和
//查看它是否包含任何shape1形状
IEnumerable shapeSie=(IEnumerable)shape2 fromConnects;
if(shapesaie.Contains(connect))
{
返回true;
}
}
回流连接;
//第三种方法
//将两者转换为IEnumerable并检查它们是否相交
IEnumerable shapes1asie=(IEnumerable)shape1 fromConnections;
IEnumerable shape2asie=(IEnumerable)shape2fromConnections;
变量shapes=shapes1asie.Intersect(shapes2 sie);
如果(shapes.Count()>0){return true;}
else{return false;}
}
如果您使用的是Visio 2010,请查看连接API
来自Visio MSDN博客的链接
作为参考,还有一个关于StackOverflow的相关问题
我没有使用Visio 2010,但我想我会使用ugprade。这看起来比我现在做的容易多了。谢谢