以编程方式从形状中获取所有出线连接器
Get all outging connectors from a shape programmatically
我想在删除形状后重命名连接器。
可以说我有一个 shape1,我删除了一个与 shape1 相连的 shape2。
我想要 shape1 和 shape2 之间的连接器形状,以便我可以重命名它。
我想这取决于你在哪个阶段拦截掉落。如果是立即,您可能会对可能涉及的连接器数量做出一些假设,但如果是在丢弃后的某个时间,那么您可能想要确定涉及的连接数量。
例如,具有以下形状:
...您可以通过多种方式解决此问题:
- 使用从 ShapeTwo 返回的 GluedShapes 方法
- 使用包括 'from' 形状的 GluedShapes 方法
- 循环访问 Page 的 Connects 集合
- 迭代目标形状 (ShapeOne) 中的连接对象
我肯定会尝试在 Connect 对象上使用 GluedShapes 方法(该方法于 2010 年引入 Visio),但我将它们添加到此处,因为它们可能非常有用,具体取决于您要实现的目标。
这是一个使用 LINQPad 的示例:
void Main()
{
var vApp = MyExtensions.GetRunningVisio();
var vPag = vApp.ActivePage;
//For demo purposes I'm assuming the following shape IDs
//but in reality you'd get a reference by other methods
//such as Window.Selection, Page index or ID
var shpOne = vPag.Shapes.ItemFromID[1];
var shpTwo = vPag.Shapes.ItemFromID[2];
Array gluedIds;
Console.WriteLine("1) using GluedShapes with the 'to' shape only");
gluedIds = shpTwo.GluedShapes(Visio.VisGluedShapesFlags.visGluedShapesIncoming1D,"");
IterateByIds(vPag, gluedIds);
Console.WriteLine("\n2) using GluedShapes with the 'to' and 'from' shapes");
gluedIds = shpTwo.GluedShapes(Visio.VisGluedShapesFlags.visGluedShapesIncoming1D, "", shpOne);
IterateByIds(vPag, gluedIds);
Console.WriteLine("\n3) using the Connects collection on Page");
var pageConns = from c in vPag.Connects.Cast<Visio.Connect>()
where c.FromSheet.OneD != 0
group c by c.FromSheet into connectPair
where connectPair.Any(p => p.ToSheet.ID == shpOne.ID) && connectPair.Any(p => p.ToSheet.ID == shpTwo.ID)
select connectPair.Key.Text;
pageConns.Dump();
Console.WriteLine("\n4) using FromConnects and Linq to navigate from shpOne to shpTwo finding the connector in the middle");
var shpConns = from c in shpOne.FromConnects.Cast<Visio.Connect>()
where c.FromSheet.OneD != 0
let targetConnector = c.FromSheet
from c2 in targetConnector.Connects.Cast<Visio.Connect>()
where c2.ToSheet.Equals(shpTwo)
select targetConnector.Text;
shpConns.Dump();
}
private void IterateByIds(Visio.Page hostPage, Array shpIds)
{
if (shpIds.Length > 0)
{
for (int i = 0; i < shpIds.Length; i++)
{
//Report on the shape text (or change it as required)
Console.WriteLine(hostPage.Shapes.ItemFromID[(int)shpIds.GetValue(i)].Text);
}
}
}
运行 以上将导致此输出:
值得注意的是,连接代码(3 和 4)假设连接器形状 (1D) 正在连接 到 流程图形状 (2D) 而不是反过来(这是可能的)。
您可以将连接对象想象成连接点,因此在图中,三个连接器形状生成六个连接对象:
无论如何,希望你能摆脱困境。
更新 - 为了清楚起见(并正确回答原始问题),从 ShapeOne 获取所有传出连接器的代码为:
Console.WriteLine("using GluedShapes to report outgoing connectors");
gluedIds = shpOne.GluedShapes(Visio.VisGluedShapesFlags.visGluedShapesOutgoing1D, "");
IterateByIds(vPag, gluedIds);
我想在删除形状后重命名连接器。 可以说我有一个 shape1,我删除了一个与 shape1 相连的 shape2。 我想要 shape1 和 shape2 之间的连接器形状,以便我可以重命名它。
我想这取决于你在哪个阶段拦截掉落。如果是立即,您可能会对可能涉及的连接器数量做出一些假设,但如果是在丢弃后的某个时间,那么您可能想要确定涉及的连接数量。
例如,具有以下形状:
...您可以通过多种方式解决此问题:
- 使用从 ShapeTwo 返回的 GluedShapes 方法
- 使用包括 'from' 形状的 GluedShapes 方法
- 循环访问 Page 的 Connects 集合
- 迭代目标形状 (ShapeOne) 中的连接对象
我肯定会尝试在 Connect 对象上使用 GluedShapes 方法(该方法于 2010 年引入 Visio),但我将它们添加到此处,因为它们可能非常有用,具体取决于您要实现的目标。
这是一个使用 LINQPad 的示例:
void Main()
{
var vApp = MyExtensions.GetRunningVisio();
var vPag = vApp.ActivePage;
//For demo purposes I'm assuming the following shape IDs
//but in reality you'd get a reference by other methods
//such as Window.Selection, Page index or ID
var shpOne = vPag.Shapes.ItemFromID[1];
var shpTwo = vPag.Shapes.ItemFromID[2];
Array gluedIds;
Console.WriteLine("1) using GluedShapes with the 'to' shape only");
gluedIds = shpTwo.GluedShapes(Visio.VisGluedShapesFlags.visGluedShapesIncoming1D,"");
IterateByIds(vPag, gluedIds);
Console.WriteLine("\n2) using GluedShapes with the 'to' and 'from' shapes");
gluedIds = shpTwo.GluedShapes(Visio.VisGluedShapesFlags.visGluedShapesIncoming1D, "", shpOne);
IterateByIds(vPag, gluedIds);
Console.WriteLine("\n3) using the Connects collection on Page");
var pageConns = from c in vPag.Connects.Cast<Visio.Connect>()
where c.FromSheet.OneD != 0
group c by c.FromSheet into connectPair
where connectPair.Any(p => p.ToSheet.ID == shpOne.ID) && connectPair.Any(p => p.ToSheet.ID == shpTwo.ID)
select connectPair.Key.Text;
pageConns.Dump();
Console.WriteLine("\n4) using FromConnects and Linq to navigate from shpOne to shpTwo finding the connector in the middle");
var shpConns = from c in shpOne.FromConnects.Cast<Visio.Connect>()
where c.FromSheet.OneD != 0
let targetConnector = c.FromSheet
from c2 in targetConnector.Connects.Cast<Visio.Connect>()
where c2.ToSheet.Equals(shpTwo)
select targetConnector.Text;
shpConns.Dump();
}
private void IterateByIds(Visio.Page hostPage, Array shpIds)
{
if (shpIds.Length > 0)
{
for (int i = 0; i < shpIds.Length; i++)
{
//Report on the shape text (or change it as required)
Console.WriteLine(hostPage.Shapes.ItemFromID[(int)shpIds.GetValue(i)].Text);
}
}
}
运行 以上将导致此输出:
值得注意的是,连接代码(3 和 4)假设连接器形状 (1D) 正在连接 到 流程图形状 (2D) 而不是反过来(这是可能的)。
您可以将连接对象想象成连接点,因此在图中,三个连接器形状生成六个连接对象:
无论如何,希望你能摆脱困境。
更新 - 为了清楚起见(并正确回答原始问题),从 ShapeOne 获取所有传出连接器的代码为:
Console.WriteLine("using GluedShapes to report outgoing connectors");
gluedIds = shpOne.GluedShapes(Visio.VisGluedShapesFlags.visGluedShapesOutgoing1D, "");
IterateByIds(vPag, gluedIds);