首页 > 解决方案 > C# 和 Visio 2013 - 连接两个形状的问题

问题描述

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

我的主要问题:树是按照我的意愿绘制的 - 但是当我使用不同的主形状时(例如:在标有许多“#”的行使用 masterShapes[1] 而不是 0)我的整个树的连接是弄乱。当我使用相同的形状时,它会起作用。我尝试了很多方法来绘制我的树,但它总是一样。

仅供参考:TreeNode 是一个下载的树,运行良好。树节点中的数据来自 thge 类 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;
    }

标签: c#.netvisio

解决方案


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

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

您可能还想检查绘图的路由设置(相当多 - 可以在文档和页面的 shapesheet 中找到)。


推荐阅读