我正在创建一个visio图,但需要检查现有的形状是否已连接。我用3种不同的方法编写了一种方法来确定这一点。我找不到任何形状方法直接这样做。这就是我想出来的。我更喜欢第三种方法,因为它不涉及迭代。还有其他建议吗?
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; }
}
答案 0 :(得分:1)
如果您使用的是Visio 2010,请查看Connectivity API
来自Visio MSDN博客的链接 http://blogs.msdn.com/b/visio/archive/2009/09/22/the-visio-2010-connectivity-api.aspx
作为参考,StackOverflow上还有一个相关的问题 Shape connectors in Visio