使用visio互操作API创建序列图

时间:2017-01-21 15:40:10

标签: c# automation interop visio sequence-diagram

尝试以编程方式生成Visio序列图。以下是代码

的工作内容
namespace Training.Visio
{
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.Office.Interop.Visio;
    public class Program
    {
        static void Main(string[] args)
        {   
            var sequenceDrawer = new VisioSequenceDrawer();
            sequenceDrawer.MakeObjectLifelineShape("obj1", 1, 1);
            sequenceDrawer.MakeObjectLifelineShape("obj2", 2, 1);

            var shape1 = sequenceDrawer.GetShapeByName("obj1");
            var shape2 = sequenceDrawer.GetShapeByName("obj2");

            sequenceDrawer.ConnectShapes(shape1, shape2);

            //sequenceDrawer.ActiveDocument.Save();
            //sequenceDrawer.ActiveDocument.Close();
        }
    }
    public class VisioSequenceDrawer
    {
        private Application application;
        private Masters masters;
        private Page activePage;
        private Document activeDocument;
        public VisioSequenceDrawer()
        {
            this.application = new Application();
            this.application.Documents.Add(string.Empty);
            var documents = this.application.Documents;
            var document = documents.OpenEx("UMLSEQ_U.VSS", (short)VisOpenSaveArgs.visOpenDocked);
            this.masters = document.Masters;
            this.activeDocument = this.application.ActiveDocument;
            this.activePage = this.activeDocument.Pages.Add();
        }
        public Document ActiveDocument
        {
            get
            {
                return this.activeDocument;
            }
        }
        public void MakeObjectLifelineShape(string name, double x, double y)
        {
            var masterShape  = this.GetMaster("Object Lifeline");
            var lifelineShape = this.activePage.Drop(masterShape, x, y);
            lifelineShape.Name = name;
            lifelineShape.Text = name;
        }
        public void ConnectShapes(Shape frmShape, Shape endShape)
        {
            var masterArrow = this.GetMaster("Message");
            var arrowShape = this.activePage.Drop(masterArrow, 9, 9);

            var frmCell = arrowShape.get_CellsSRC((short)VisSectionIndices.visSectionObject, (short)VisRowIndices.visRowXForm1D, (short)VisCellIndices.vis1DBeginX);
            var endCell = arrowShape.get_CellsSRC((short)VisSectionIndices.visSectionObject, (short)VisRowIndices.visRowXForm1D, (short)VisCellIndices.vis1DEndX);
            // code under question
            // able to only connect to the end of the lifeline
            frmCell.GlueTo(frmShape.get_CellsSRC((short)VisSectionIndices.visSectionConnectionPts, (short)VisRowIndices.visRowXFormOut, (short)VisCellIndices.visXFormPinX));
            endCell.GlueTo(endShape.get_CellsSRC((short)VisSectionIndices.visSectionConnectionPts, (short)VisRowIndices.visRowXFormOut, (short)VisCellIndices.visXFormPinX));
        }
        public Shape GetShapeByName(string name)
        {
            IEnumerable<Shape> shapes = from Shape shape in this.activePage.Shapes where name == shape.Name select shape;
            return shapes.FirstOrDefault();
        }
        private Master GetMaster(string mastername)
        {
            return this.masters.ItemU[mastername];
        }
    }
}

我无法在不同的对象生命线之间连接消息箭头。 评论后面的代码

//只能连接到生命线的末尾

工作,但我无法 1.以编程方式延长该Shape对象中的垂直生命线 2.默认情况下,该对象至少有3个连接点,我们可以“粘合”消息箭头。但无法以编程方式选择连接点

有关如何解决这两个问题的任何意见?

0 个答案:

没有答案