连接列表中的 visio 形状



我尝试使用以下代码连接几个形状:

Visio.Master ConnectionMaster = _masters.get_ItemU(connectorShapeName);
Visio.Shape Connector = _activePage.Drop(ConnectionMaster, 1, 1);
// get the start cell of connector
var b1 = (short) Visio.VisSectionIndices.visSectionObject;
var b2 = (short) Visio.VisRowIndices.visRowXForm1D;
var b3 = (short) Visio.VisCellIndices.vis1DBeginX;
Visio.Cell beginXCell = Connector.get_CellsSRC(b1, b2, b3);
// and the end one
var e1 = (short) Visio.VisSectionIndices.visSectionObject;
var e2 = (short) Visio.VisRowIndices.visRowXForm1D;
var e3 = (short) Visio.VisCellIndices.vis1DEndX;
Visio.Cell endXCell = Connector.get_CellsSRC(e1, e2, e3);
// get start point from first shape
var bt1 = (short) Visio.VisSectionIndices.visSectionObject;
var bt2 = (short) Visio.VisRowIndices.visRowXFormOut;
var bt3 = (short) Visio.VisCellIndices.visXFormPinX;
var toBegin = fromShape.get_CellsSRC(bt1, bt2, bt3);
// get start point of second shape
var et1 = (short) Visio.VisSectionIndices.visSectionObject;
var et2 = (short) Visio.VisRowIndices.visRowXFormOut;
var et3 = (short) Visio.VisCellIndices.visXFormPinX;
var toEnd = toShape.get_CellsSRC(et1, et2, et3);
// connect 
beginXCell.GlueTo(toBegin);
endXCell.GlueTo(toEnd);

此代码非常适合使用以下代码添加到 visio 文档的形状:

Visio.Master shapeToDrop = _masters.get_ItemU(name);
Visio.Shape shape = _activePage.Drop(shapeToDrop, x, y);

但是,如果将形状拖放到文档中然后添加到列表中,如下所示:

var shape = _activePage.DropIntoList(shapeToDrop, target, position);

我收到错误:"发生异常。 在此代码中:

var toBegin = fromShape.get_CellsSRC(bt1, bt2, bt3);

那么,连接列表中的形状的正确方法是什么?我做错了什么?

提前感谢!

我怀疑您的问题与连接无关,而是与确保将项目放入列表时类别匹配有关。 下面是演示问题的简短示例(从 Visio 中的空白绘图开始(:

void Main()
{
//Run this code against a blank drawing in Visio
var vApp = MyExtensions.GetRunningVisio();
var vDoc = vApp.ActiveDocument;
var stencilDoc = vDoc.Application.Documents.OpenEx("wfctrl_m.vssx",
(short)Visio.VisOpenSaveArgs.visOpenRO
+ (short)Visio.VisOpenSaveArgs.visOpenDocked);
var vPag = vApp.ActivePage;
var diagramServices = vDoc.DiagramServicesEnabled;
vDoc.DiagramServicesEnabled = (int)Visio.VisDiagramServices.visServiceVersion140 
+ (int)Visio.VisDiagramServices.visServiceVersion150;
var shpList = vPag.Drop(stencilDoc.Masters.ItemU["List box"], 2.25, 9.5);
var itemMaster = stencilDoc.Masters.ItemU["List box item"];
// Drop two items in - this works because the item
// shapes have the correct required categories ('Grid')
vPag.DropIntoList(itemMaster, shpList, 1);
vPag.DropIntoList(itemMaster, shpList, 1);
// Now set the list's required categories to someting else
shpList.CellsU["User.msvSDListRequiredCategories"].FormulaU = $""Bob"";
// Note an error is thrown here because the list item being
// inserted does not contain the category 'Bob'
try
{           
vPag.DropIntoList(itemMaster, shpList, 1);
}
catch (COMException ex) when (ex.ErrorCode == -2032465763)
{
//Inappropriate source object for this action.
Console.WriteLine($"{ex.Message} - check matching categories in List and ListItem shapes");
}
vDoc.DiagramServicesEnabled = diagramServices;
}

您可能应该编写一个快速检查,在读取项目形状中的User.msvShapeCategories单元格的位置,然后检查这些是否属于列表形状(User.msvSDListRequiredCategories(中所需的分类。 有关详细信息,请参阅本文的">控制容器成员身份"部分:Visio 2010 中的自定义容器、列表和标注。

补充一点,如果你想连接列表中的形状,那么你可以使用上面的方法或类似的东西,这取决于你所追求的控制级别:

var item = vPag.DropIntoList(itemMaster, shpList, 1);
var target = vPag.DrawRectangle(4, 9, 6, 7.5);
item.AutoConnect(target, Visio.VisAutoConnectDir.visAutoConnectDirNone);

最新更新