C# 和 Visio 2013 - 连接两个形状的问题



我在使用 C# (Visual Studio 2017( 和 Visio 2013 时遇到问题。我正在编写一个程序来创建组织图。它似乎工作得很好,但是当我对形状有不同的大师时,它们没有连接。

我的主要问题: 树是按照我想要的绘制的 - 但是当我使用不同的主形状时(例如:在标有许多"#"的行上使用 masterShapes[1] 而不是 0(,我的整个树的连接都搞砸了。当我使用相等的形状时,它有效。 我尝试了很多方法来画我的树,但它总是做同样的事情。

仅供参考:TreeNode是一个下载的树,运行良好。来自树节点的数据来自类图表数据,可以是员工或信息项。两者都必须由 Visio 在同一棵树中由 Visio 绘制,但它们必须使用不同的形状。在我的示例中,我使用的是 visio 附带的默认组织结构图模板。我也总是将新形状放在 0/0,因为这是最安全的方式,它不会自动连接随机形状。

这是我画树的方法:

//generates the diagram by using an existing tree
public void GenerateDiagramFromTree(TreeNode<ChartData> pTree, string pTemplatePath, string pSavePath, bool pVisible = false)
{
//creating the visio-application
Microsoft.Office.Interop.Visio.Application application = new Microsoft.Office.Interop.Visio.Application();
application.Visible = pVisible;
Microsoft.Office.Interop.Visio.Document doc = application.Documents.Add(pTemplatePath);
//add the page for the diagram
Microsoft.Office.Interop.Visio.Page page = application.Documents[1].Pages[1];
Microsoft.Office.Interop.Visio.Master[] masterShapes;
masterShapes = new Master[2];
masterShapes[0] = doc.Masters.get_ItemU(@"Manager Belt");
masterShapes[1] = doc.Masters.get_ItemU(@"Vacancy Belt");

Shape shape = GetOrganogramShape(pTree.Data, doc, page, masterShapes);
//draw each node of the tree
drawTree(pTree, doc, page, shape, masterShapes);
//finally: save the file
page.Layout();
string fileName = pSavePath + Guid.NewGuid() + ".vsdx";
doc.SaveAs(fileName);
doc.Close();

//recursive call for drawing all employees
public void drawTree(TreeNode<ChartData> pTree, Document pDoc, Page pPage, Shape pShape, Master[] pMasterShapes)
{
//Console.WriteLine(pTree.Data.ToString() + " - Parent: " + ((Employee)pTree.Data).ReportingLine);
//there should always be data!
if (pTree.Children.Count() > 0)
{
foreach (TreeNode<ChartData> currentChild in pTree.Children)
{
Shape childShape = GetOrganogramShape(currentChild.Data, pDoc, pPage, pMasterShapes);
//connectWithDynamicGlueAndConnector(pShape, childShape);
pShape.AutoConnect(childShape, VisAutoConnectDir.visAutoConnectDirNone);
//recursive call for each child giving the shape that we created at the moment
drawTree(currentChild, pDoc, pPage, childShape, pMasterShapes);
}
}
}

这是我创建形状的方法

private Shape GetOrganogramShape(ChartData pChartData, Document pDoc, Page pPage, Master[] pMasterShapes)
{
Shape returnShape;
if(pChartData is Employee)
{
//get the shape
returnShape = pPage.Drop(pMasterShapes[0], 0, 0);
//set the cells
Employee currentWorkingEmployee = (Employee)pChartData;
returnShape.Cells["Prop.Name"].FormulaU = """ + currentWorkingEmployee.Surname + """;
}
else if(pChartData is InformationItem)
{
//get the shape
//##############################################################
returnShape = pPage.Drop(pMasterShapes[1], 0, 0);
InformationItem currentWorkingInformationItem = (InformationItem)pChartData;
returnShape.Cells["Prop.Name"].FormulaU = """ + currentWorkingInformationItem.ToString() + """;
}
//this case should never happen
else
{
//get the shape
throw new Exception("Shape to generate was not for an Employee or InformationItem");
}
//set the shape width
returnShape.get_CellsSRC(
(short)Microsoft.Office.Interop.Visio.VisSectionIndices.
visSectionObject,
(short)Microsoft.Office.Interop.Visio.VisRowIndices.
visRowXFormIn,
(short)Microsoft.Office.Interop.Visio.VisCellIndices.
visXFormWidth).ResultIU = 2.5;
//set the shape height
returnShape.get_CellsSRC(
(short)Microsoft.Office.Interop.Visio.VisSectionIndices.
visSectionObject,
(short)Microsoft.Office.Interop.Visio.VisRowIndices.
visRowXFormIn,
(short)Microsoft.Office.Interop.Visio.VisCellIndices.
visXFormHeight).ResultIU = 1;
System.Drawing.Color BackColor = new System.Drawing.Color();
BackColor = System.Drawing.Color.Black;
//set the shape fore color
returnShape.Characters.set_CharProps(
(short)Microsoft.Office.Interop.Visio.
VisCellIndices.visCharacterColor,
(short)Utilities.GetVisioColor(Colors.Black));
//set the shape back color
returnShape.get_CellsSRC((short)VisSectionIndices.visSectionObject,
(short)VisRowIndices.visRowFill, (short)VisCellIndices.visFillForegnd).FormulaU = "RGB(" + BackColor.R.ToString() + "," + BackColor.G.ToString() + "," + BackColor.B.ToString() + ")";
return returnShape;
}

目前尚不清楚"搞砸了"是什么意思。我怀疑形状仍然正确连接,但连接器的布线不好?

这可能是由于设置了主控形状和代码进行自动连接,而不是连接形状的特定点。

您可能还想检查绘图的布线设置(相当多 - 可以在文档和页面的形状表中找到(。

最新更新