我遇到了C#(Visual Studio 2017)和Visio 2013的问题。我正在编写一个程序来创建有机图。它似乎工作得很好,但是当我有不同的形状大师时,它们没有连接。
我的主要问题: Tree正如我想要的那样绘制 - 但是当我使用不同的Master-Shapes时(例如:使用masterShapes [1]而不是在标有许多&#34的行上使用0;#")我的整个连接树乱了。当我使用相同的形状,它的工作原理。 我尝试了很多绘制树的方法,但它总是这样做。
仅供参考:TreeNode是一个下载的树,效果很好。来自treenode的数据来自Thge Class ChartData,它可以是Employee或InformationItem。两者都必须由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;
}
答案 0 :(得分:0)
目前尚不清楚“混乱”是什么意思。我怀疑形状仍然正确连接,但连接器的路由不好?
这可能是由于设置了主人和您的代码进行自动连接,而不是连接形状的特定点。
您可能还想检查绘图的路由设置(相当多 - 可以在文档和页面的形状表中找到)。