首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#和Visio 2013 -连接两个形状的问题

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

Stack Overflow用户
提问于 2018-05-04 13:29:15
回答 1查看 394关注 0票数 1

我对C# (Visual Studio2017)和Visio2013有问题。我正在写一个创建器官图的程序。它似乎工作得很好,但是当我对形状使用不同的Master时,它们是不相连的。

我的主要问题是:树就像我想要的那样绘制--但是当我使用不同的主控形状时(例如:在标有许多“#”的行用masterShapes1代替0),我整个树的连接就会变得混乱。当我使用相等的形状时,它是有效的。我尝试了很多种方法来画我的树,但都是一样的。

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

这是我画树的方法:

代码语言:javascript
复制
    //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);

            }

        }
    }

下面是我创建形状的方法

代码语言:javascript
复制
    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;
    }
EN

回答 1

Stack Overflow用户

发布于 2018-05-26 17:20:46

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

这可能是由于母版的设置和您的代码创建了自动连接,而不是连接形状的特定点。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50167752

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档