public static int Set(
            IVisio.Shape shape,
            short row,
            ControlCells ctrl)
        {
            if (shape == null)
            {
                throw new ArgumentNullException(nameof(shape));
            }


            if (!ctrl.XDynamics.Formula.HasValue)
            {
                ctrl.XDynamics = $"Controls.Row_{row + 1}";
            }

            if (!ctrl.YDynamics.Formula.HasValue)
            {
                ctrl.YDynamics = $"Controls.Row_{row + 1}.Y";
            }

            var update = new ShapeSheet.Update();
            update.SetFormulas(ctrl, row);
            update.Execute(shape);

            return row;
        }
        public IVisio.Page CreatePage(FormPage formpage)
        {
            this.FormPage = formpage;

            this.page = this.VisioPages.Add();
            this.page.Name = formpage.Name;

            // Update the Page Cells
            var pagesheet = this.page.PageSheet;
            var pageupdate = new ShapeSheet.Update();

            var pagecells = new Pages.PageCells();
            pagecells.PageWidth = formpage.Size.Width;
            pagecells.PageHeight = formpage.Size.Height;
            pagecells.PageLeftMargin = formpage.Margin.Left;
            pagecells.PageRightMargin = formpage.Margin.Right;
            pagecells.PageTopMargin = formpage.Margin.Top;
            pagecells.PageBottomMargin = formpage.Margin.Bottom;
            pageupdate.SetFormulas(pagecells);
            pageupdate.Execute(pagesheet);


            this.Reset();
            return this.page;
        }
Exemplo n.º 3
0
        public short Render(IVisio.Shape shape)
        {
            short sec_index = GeometryHelper.AddSection(shape);
            short row_count = shape.RowCount[sec_index];

            var update = new ShapeSheet.Update();

            var src_nofill      = ShapeSheet.SRCConstants.Geometry_NoFill.ForSectionAndRow(sec_index, 0);
            var src_noline      = ShapeSheet.SRCConstants.Geometry_NoLine.ForSectionAndRow(sec_index, 0);
            var src_noshow      = ShapeSheet.SRCConstants.Geometry_NoShow.ForSectionAndRow(sec_index, 0);
            var src_nosnap      = ShapeSheet.SRCConstants.Geometry_NoSnap.ForSectionAndRow(sec_index, 0);
            var src_noquickdrag = ShapeSheet.SRCConstants.Geometry_NoQuickDrag.ForSectionAndRow(sec_index, 0);

            update.SetFormulaIgnoreNull(src_nofill, this.NoFill);
            update.SetFormulaIgnoreNull(src_noline, this.NoLine);
            update.SetFormulaIgnoreNull(src_noshow, this.NoShow);
            update.SetFormulaIgnoreNull(src_nosnap, this.NoSnap);
            update.SetFormulaIgnoreNull(src_noquickdrag, this.NoQuickDrag);

            foreach (var row in this.Rows)
            {
                row.AddTo(shape, update, row_count, sec_index);
                row_count++;
            }

            update.Execute(shape);
            return(0);
        }
Exemplo n.º 4
0
        public IVisio.Page CreatePage(FormPage formpage)
        {
            this.FormPage = formpage;

            this.page      = this.VisioPages.Add();
            this.page.Name = formpage.Name;

            // Update the Page Cells
            var pagesheet  = this.page.PageSheet;
            var pageupdate = new ShapeSheet.Update();

            var pagecells = new Pages.PageCells();

            pagecells.PageWidth        = formpage.Size.Width;
            pagecells.PageHeight       = formpage.Size.Height;
            pagecells.PageLeftMargin   = formpage.Margin.Left;
            pagecells.PageRightMargin  = formpage.Margin.Right;
            pagecells.PageTopMargin    = formpage.Margin.Top;
            pagecells.PageBottomMargin = formpage.Margin.Bottom;
            pageupdate.SetFormulas(pagecells);
            pageupdate.Execute(pagesheet);


            this.Reset();
            return(this.page);
        }
Exemplo n.º 5
0
        public void SetSize(IList <IVisio.Shape> target_shapes, double?w, double?h)
        {
            this.Client.Application.AssertApplicationAvailable();
            this.Client.Document.AssertDocumentAvailable();

            var shapes = this.GetTargetShapes(target_shapes);

            if (shapes.Count < 1)
            {
                return;
            }

            var shapeids = shapes.Select(s => s.ID).ToList();
            var update   = new ShapeSheet.Update();

            foreach (int shapeid in shapeids)
            {
                if (w.HasValue && w.Value >= 0)
                {
                    update.SetFormula((short)shapeid, ShapeSheet.SRCConstants.Width, w.Value);
                }
                if (h.HasValue && h.Value >= 0)
                {
                    update.SetFormula((short)shapeid, ShapeSheet.SRCConstants.Height, h.Value);
                }
            }

            var application = this.Client.Application.Get();

            using (var undoscope = this.Client.Application.NewUndoScope("Set Shape Size"))
            {
                var active_page = application.ActivePage;
                update.Execute(active_page);
            }
        }
Exemplo n.º 6
0
        public void SetLock(IList <IVisio.Shape> target_shapes, Shapes.LockCells lockcells)
        {
            this.Client.Application.AssertApplicationAvailable();
            this.Client.Document.AssertDocumentAvailable();

            var shapes = this.GetTargetShapes(target_shapes);

            if (shapes.Count < 1)
            {
                return;
            }

            var selection = this.Client.Selection.Get();
            var shapeids  = selection.GetIDs();
            var update    = new ShapeSheet.Update();

            foreach (int shapeid in shapeids)
            {
                update.SetFormulas((short)shapeid, lockcells);
            }

            var application = this.Client.Application.Get();

            using (var undoscope = this.Client.Application.NewUndoScope("Set Shape Lock Properties"))
            {
                var active_page = application.ActivePage;
                update.Execute(active_page);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Remove all tab stops on the shape
        /// </summary>
        /// <param name="shape"></param>
        private static void ClearTabStops(IVisio.Shape shape)
        {
            if (shape == null)
            {
                throw new System.ArgumentNullException(nameof(shape));
            }

            int num_existing_tabstops = TextFormat.GetTabStopCount(shape);

            if (num_existing_tabstops < 1)
            {
                return;
            }

            var cell_tabstopcount = shape.CellsSRC[TextFormat.src_tabstopcount.Section, TextFormat.src_tabstopcount.Row, TextFormat.src_tabstopcount.Cell];

            cell_tabstopcount.FormulaForce = "0";

            const string formula = "0";

            var update = new ShapeSheet.Update();

            for (int i = 1; i < num_existing_tabstops * 3; i++)
            {
                var src = new ShapeSheet.SRC(TextFormat.tab_section, (short)IVisio.VisRowIndices.visRowTab,
                                             (short)i);
                update.SetFormula(src, formula);
            }

            update.Execute(shape);
        }
        public static int Add(
            IVisio.Shape shape,
            ConnectionPointCells cp)
        {
            if (shape == null)
            {
                throw new System.ArgumentNullException(nameof(shape));
            }

            if (!cp.X.Formula.HasValue)
            {
                throw new System.ArgumentException("Must provide an X Formula");
            }

            if (!cp.Y.Formula.HasValue)
            {
                throw new System.ArgumentException("Must provide an Y Formula");
            }

            var n = shape.AddRow((short)IVisio.VisSectionIndices.visSectionConnectionPts,
                                 (short)IVisio.VisRowIndices.visRowLast,
                                 (short)IVisio.VisRowTags.visTagCnnctPt);

            var update = new ShapeSheet.Update();
            update.SetFormulas(cp,n);
            update.Execute(shape);

            return n;
        }
Exemplo n.º 9
0
        public static int Add(
            IVisio.Shape shape,
            ConnectionPointCells connection_point_cells)
        {
            if (shape == null)
            {
                throw new System.ArgumentNullException(nameof(shape));
            }

            if (!connection_point_cells.X.Formula.HasValue)
            {
                string msg = "Must provide an X Formula";
                throw new System.ArgumentException(msg, nameof(connection_point_cells));
            }

            if (!connection_point_cells.Y.Formula.HasValue)
            {
                string msg = "Must provide an Y Formula";
                throw new System.ArgumentException(msg, nameof(connection_point_cells));
            }

            var n = shape.AddRow((short)IVisio.VisSectionIndices.visSectionConnectionPts,
                                 (short)IVisio.VisRowIndices.visRowLast,
                                 (short)IVisio.VisRowTags.visTagCnnctPt);

            var update = new ShapeSheet.Update();

            update.SetFormulas(connection_point_cells, n);
            update.Execute(shape);

            return(n);
        }
Exemplo n.º 10
0
        public void PasteFormat(IVisio.Page page, IList <int> shapeids, FormatCategory category, bool applyformulas)
        {
            var update = new ShapeSheet.Update();

            foreach (var shape_id in shapeids)
            {
                foreach (var cellrec in this.Cells)
                {
                    if (!cellrec.MatchesCategory(category))
                    {
                        continue;
                    }

                    var sidsrc = new ShapeSheet.SIDSRC((short)shape_id, cellrec.SRC);

                    if (applyformulas)
                    {
                        update.SetFormula(sidsrc, cellrec.Formula);
                    }
                    else
                    {
                        if (cellrec.Result != null)
                        {
                            update.SetFormula(sidsrc, cellrec.Result);
                        }
                    }
                }
            }

            update.Execute(page);
        }
Exemplo n.º 11
0
        public static int Set(
            IVisio.Shape shape,
            short row,
            ControlCells ctrl)
        {
            if (shape == null)
            {
                throw new ArgumentNullException(nameof(shape));
            }


            if (!ctrl.XDynamics.Formula.HasValue)
            {
                ctrl.XDynamics = $"Controls.Row_{row + 1}";
            }

            if (!ctrl.YDynamics.Formula.HasValue)
            {
                ctrl.YDynamics = $"Controls.Row_{row + 1}.Y";
            }

            var update = new ShapeSheet.Update();
            update.SetFormulas(ctrl, row);
            update.Execute(shape);

            return row;
        }
Exemplo n.º 12
0
        public void Render(IVisio.Page page)
        {
            if (page == null)
            {
                throw new System.ArgumentNullException(nameof(page));
            }


            // First handle any page properties
            if (this.Name!=null)
            {
                page.NameU = this.Name;
            }

            this.VisioPage = page;

            var page_sheet = page.PageSheet;

            var app = page.Application;


            using (var perfscope = new Application.PerfScope(app, this.PerfSettings))
            {
                if (this.Size.HasValue)
                {
                    this.PageCells.PageHeight = this.Size.Value.Height;
                    this.PageCells.PageWidth = this.Size.Value.Width;
                }

                var update = new ShapeSheet.Update();
                update.SetFormulas((short)page_sheet.ID, this.PageCells);
                update.Execute(page);
                
                // Then render the shapes
                this.Shapes.Render(page);

                // Perform any additional layout
                if (this.Layout != null)
                {
                    this.Layout.Apply(page);
                }

                // Optionally, perform page resizing to fit contents
                if (this.ResizeToFit)
                {
                    if (this.ResizeToFitMargin.HasValue)
                    {
                        page.ResizeToFitContents(this.ResizeToFitMargin.Value);
                    }
                    else
                    {
                        page.ResizeToFitContents();
                    }
                }
            }

        }
Exemplo n.º 13
0
        public void ToogleCase(IList <IVisio.Shape> target_shapes)
        {
            this.Client.Application.AssertApplicationAvailable();
            this.Client.Document.AssertDocumentAvailable();

            var shapes = this.GetTargetShapes(target_shapes);

            if (shapes.Count < 1)
            {
                return;
            }

            var application = this.Client.Application.Get();

            using (var undoscope = this.Client.Application.NewUndoScope("Toggle Shape Text Case"))
            {
                var shapeids = shapes.Select(s => s.ID).ToList();

                var page = application.ActivePage;
                // Store all the formatting
                var formats = Text.TextFormat.GetFormat(page, shapeids);

                // Change the text - this will wipe out all the character and paragraph formatting
                foreach (var shape in shapes)
                {
                    string t = shape.Text;
                    if (t.Length < 1)
                    {
                        continue;
                    }
                    shape.Text = TextCommandsUtil.toggle_case(t);
                }

                // Now restore all the formatting - based on any initial formatting from the text

                var update = new ShapeSheet.Update();
                for (int i = 0; i < shapes.Count; i++)
                {
                    var format = formats[i];

                    if (format.CharacterFormats.Count > 0)
                    {
                        var fmt = format.CharacterFormats[0];
                        update.SetFormulas((short)shapeids[i], fmt, 0);
                    }

                    if (format.ParagraphFormats.Count > 0)
                    {
                        var fmt = format.ParagraphFormats[0];
                        update.SetFormulas((short)shapeids[i], fmt, 0);
                    }
                }

                update.Execute(page);
            }
        }
Exemplo n.º 14
0
        public void Render(IVisio.Page page)
        {
            if (page == null)
            {
                throw new System.ArgumentNullException(nameof(page));
            }


            // First handle any page properties
            if (this.Name != null)
            {
                page.NameU = this.Name;
            }

            this.VisioPage = page;

            var page_sheet = page.PageSheet;

            var app = page.Application;


            using (var perfscope = new Application.PerfScope(app, this.PerfSettings))
            {
                if (this.Size.HasValue)
                {
                    this.PageCells.PageHeight = this.Size.Value.Height;
                    this.PageCells.PageWidth  = this.Size.Value.Width;
                }

                var update = new ShapeSheet.Update();
                update.SetFormulas((short)page_sheet.ID, this.PageCells);
                update.Execute(page);

                // Then render the shapes
                this.Shapes.Render(page);

                // Perform any additional layout
                if (this.Layout != null)
                {
                    this.Layout.Apply(page);
                }

                // Optionally, perform page resizing to fit contents
                if (this.ResizeToFit)
                {
                    if (this.ResizeToFitMargin.HasValue)
                    {
                        page.ResizeToFitContents(this.ResizeToFitMargin.Value);
                    }
                    else
                    {
                        page.ResizeToFitContents();
                    }
                }
            }
        }
        public static void Set(IVisio.Shape shape, short row, CustomPropertyCells cp)
        {
            if (shape == null)
            {
                throw new ArgumentNullException(nameof(shape));
            }

            var update = new ShapeSheet.Update();
            update.SetFormulas(cp, row);
            update.Execute(shape);
        }
Exemplo n.º 16
0
        private static void SetSize(IVisio.Page page, Drawing.Size size)
        {
            var page_cells = new PageCells();

            page_cells.PageHeight = size.Height;
            page_cells.PageWidth  = size.Width;
            var pageupdate = new ShapeSheet.Update();

            pageupdate.SetFormulas(page_cells);
            pageupdate.Execute(page.PageSheet);
        }
Exemplo n.º 17
0
        public void Apply(IVisio.Page page)
        {
            var pagecells = new PageCells();
            this.SetPageCells(pagecells);

            var update = new ShapeSheet.Update();
            update.SetFormulas(pagecells);
            var pagesheet = page.PageSheet;
            update.Execute(pagesheet);
            page.Layout();
        }
Exemplo n.º 18
0
        private static void update_xfrms(IVisio.Page page, IList <int> shapeids, IList <Shapes.XFormCells> xfrms)
        {
            var update = new ShapeSheet.Update();

            for (int i = 0; i < shapeids.Count; i++)
            {
                var shape_id = shapeids[i];
                var xfrm     = xfrms[i];
                update.SetFormulas((short)shape_id, xfrm);
            }
            update.Execute(page);
        }
Exemplo n.º 19
0
        public static void Set(IVisio.Shape shape, short row, CustomPropertyCells cp)
        {
            if (shape == null)
            {
                throw new ArgumentNullException(nameof(shape));
            }

            var update = new ShapeSheet.Update();

            update.SetFormulas(cp, row);
            update.Execute(shape);
        }
Exemplo n.º 20
0
        public void Finish()
        {
            var update = new ShapeSheet.Update();

            foreach (var block in this.Blocks)
            {
                update.SetFormulas((short)block.VisioShapeID, block.FormatCells);
                update.SetFormulas((short)block.VisioShapeID, block.TextBlockCells);
                update.SetFormulas((short)block.VisioShapeID, block.ParagraphCells, 0);
                update.SetFormulas((short)block.VisioShapeID, block.CharacterCells, 0);
            }
            update.Execute(this.page);
        }
Exemplo n.º 21
0
        public static void SetTabStops(IVisio.Shape shape, IList <TabStop> stops)
        {
            if (shape == null)
            {
                throw new System.ArgumentNullException(nameof(shape));
            }

            if (stops == null)
            {
                throw new System.ArgumentNullException(nameof(stops));
            }

            TextFormat.ClearTabStops(shape);
            if (stops.Count < 1)
            {
                return;
            }

            const short row = 0;
            var         invariant_culture  = System.Globalization.CultureInfo.InvariantCulture;
            var         vis_tab_stop_count = (short)IVisio.VisCellIndices.visTabStopCount;
            var         tabstopcountcell   = shape.CellsSRC[TextFormat.tab_section, row, vis_tab_stop_count];

            tabstopcountcell.FormulaU = stops.Count.ToString(invariant_culture);

            // set the number of tab stobs allowed for the shape
            var tagtab = TextFormat.GetTabTagForStops(stops.Count);

            shape.RowType[TextFormat.tab_section, (short)IVisio.VisRowIndices.visRowTab] = (short)tagtab;

            // add tab properties for each stop
            var update = new ShapeSheet.Update();

            for (int stop_index = 0; stop_index < stops.Count; stop_index++)
            {
                int i = stop_index * 3;

                var alignment = ((int)stops[stop_index].Alignment).ToString(invariant_culture);
                var position  = ((int)stops[stop_index].Position).ToString(invariant_culture);

                var src_tabpos   = new ShapeSheet.SRC(TextFormat.tab_section, row, (short)(i + 1));
                var src_tabalign = new ShapeSheet.SRC(TextFormat.tab_section, row, (short)(i + 2));
                var src_tabother = new ShapeSheet.SRC(TextFormat.tab_section, row, (short)(i + 3));

                update.SetFormula(src_tabpos, position);    // tab position
                update.SetFormula(src_tabalign, alignment); // tab alignment
                update.SetFormula(src_tabother, "0");       // tab unknown
            }

            update.Execute(shape);
        }
Exemplo n.º 22
0
        public void Apply(IVisio.Page page)
        {
            var pagecells = new PageCells();

            this.SetPageCells(pagecells);

            var update = new ShapeSheet.Update();

            update.SetFormulas(pagecells);
            var pagesheet = page.PageSheet;

            update.Execute(pagesheet);
            page.Layout();
        }
        public static void Set(IVisio.Shape shape, string name, ShapeSheet.FormulaLiteral value, ShapeSheet.FormulaLiteral prompt)
        {
            if (shape == null)
            {
                throw new System.ArgumentNullException(nameof(shape));
            }

            UserDefinedCellsHelper.CheckValidName(name);

            if (UserDefinedCellsHelper.Contains(shape, name))
            {
                string full_prop_name = UserDefinedCellsHelper.GetRowName(name);

                if (value.HasValue)
                {
                    string value_cell_name = full_prop_name;
                    var    cell            = shape.CellsU[value_cell_name];
                    cell.FormulaU = value.Encode();
                }

                if (prompt.HasValue)
                {
                    string prompt_cell_name = full_prop_name + ".Prompt";
                    var    cell             = shape.CellsU[prompt_cell_name];
                    cell.FormulaU = prompt.Encode();
                }
                return;
            }

            short row = shape.AddNamedRow(
                UserDefinedCellsHelper._userdefinedcell_section,
                name,
                (short)IVisio.VisRowIndices.visRowUser);

            var update = new ShapeSheet.Update();

            if (value.HasValue)
            {
                var src = new ShapeSheet.SRC(UserDefinedCellsHelper._userdefinedcell_section, row, (short)IVisio.VisCellIndices.visUserValue);
                update.SetFormula(src, value.Encode());
            }

            if (prompt.HasValue)
            {
                var src = new ShapeSheet.SRC(UserDefinedCellsHelper._userdefinedcell_section, row, (short)IVisio.VisCellIndices.visUserPrompt);
                update.SetFormula(src, prompt.Encode());
            }

            update.Execute(shape);
        }
Exemplo n.º 24
0
        private void UpdateCells(RenderContext ctx)
        {
            this.UpdateCellsWithDropSizes(ctx);

            var update            = new ShapeSheet.Update();
            var shapes_with_cells = this.shapes.Where(s => s.Cells != null);

            foreach (var shape in shapes_with_cells)
            {
                var   fmt = shape.Cells;
                short id  = shape.VisioShapeID;
                fmt.Apply(update, id);
            }
            update.Execute(ctx.VisioPage);
        }
        public static void set_text_wrapping(IVisio.Page page,
                                               IList<int> shapeids,
                                               bool wrap)
        {
            const string formula_wrap = "WIDTH*1";
            const string formula_no_wrap = "TEXTWIDTH(TheText)";
            string formula = wrap ? formula_wrap : formula_no_wrap;
            var update = new ShapeSheet.Update();
            
            foreach (int shapeid in shapeids)
            {
                update.SetFormula((short)shapeid, ShapeSheet.SRCConstants.TxtWidth, formula);
            }

            update.Execute(page);
        }
Exemplo n.º 26
0
        public static void set_text_wrapping(IVisio.Page page,
                                             IList <int> shapeids,
                                             bool wrap)
        {
            const string formula_wrap    = "WIDTH*1";
            const string formula_no_wrap = "TEXTWIDTH(TheText)";
            string       formula         = wrap ? formula_wrap : formula_no_wrap;
            var          update          = new ShapeSheet.Update();

            foreach (int shapeid in shapeids)
            {
                update.SetFormula((short)shapeid, ShapeSheet.SRCConstants.TxtWidth, formula);
            }

            update.Execute(page);
        }
Exemplo n.º 27
0
        public static int Set(
            IVisio.Shape shape,
            short row,
            HyperlinkCells hyperlink)
        {
            if (shape == null)
            {
                throw new ArgumentNullException(nameof(shape));
            }

            var update = new ShapeSheet.Update();

            update.SetFormulas(hyperlink, row);
            update.Execute(shape);

            return(row);
        }
Exemplo n.º 28
0
        public void SetSize(Drawing.Size new_size)
        {
            this.Client.Application.AssertApplicationAvailable();
            this.Client.Document.AssertDocumentAvailable();

            var application = this.Client.Application.Get();

            using (var undoscope = this.Client.Application.NewUndoScope("Set Page Size"))
            {
                var active_page = application.ActivePage;
                var page_sheet  = active_page.PageSheet;
                var update      = new ShapeSheet.Update(2);
                update.SetFormula(ShapeSheet.SRCConstants.PageWidth, new_size.Width);
                update.SetFormula(ShapeSheet.SRCConstants.PageHeight, new_size.Height);
                update.Execute(page_sheet);
            }
        }
Exemplo n.º 29
0
        public void FitShapeToText(IList <IVisio.Shape> target_shapes)
        {
            this.Client.Application.AssertApplicationAvailable();
            this.Client.Document.AssertDocumentAvailable();

            var shapes = this.GetTargetShapes2D(target_shapes);

            if (shapes.Count < 1)
            {
                return;
            }

            var application = this.Client.Application.Get();
            var active_page = application.ActivePage;
            var shapeids    = shapes.Select(s => s.ID).ToList();

            using (var undoscope = this.Client.Application.NewUndoScope("FitShapeToText"))
            {
                // Calculate the new sizes for each shape
                var new_sizes = new List <Drawing.Size>(shapeids.Count);
                foreach (var shape in shapes)
                {
                    var text_bounding_box = shape.GetBoundingBox(Microsoft.Office.Interop.Visio.VisBoundingBoxArgs.visBBoxUprightText).Size;
                    var wh_bounding_box   = shape.GetBoundingBox(Microsoft.Office.Interop.Visio.VisBoundingBoxArgs.visBBoxUprightWH).Size;

                    double max_w    = System.Math.Max(text_bounding_box.Width, wh_bounding_box.Width);
                    double max_h    = System.Math.Max(text_bounding_box.Height, wh_bounding_box.Height);
                    var    max_size = new Drawing.Size(max_w, max_h);
                    new_sizes.Add(max_size);
                }

                var src_width  = ShapeSheet.SRCConstants.Width;
                var src_height = ShapeSheet.SRCConstants.Height;

                var update = new ShapeSheet.Update();
                for (int i = 0; i < new_sizes.Count; i++)
                {
                    var shapeid  = shapeids[i];
                    var new_size = new_sizes[i];
                    update.SetFormula((short)shapeid, src_width, new_size.Width);
                    update.SetFormula((short)shapeid, src_height, new_size.Height);
                }

                update.Execute(active_page);
            }
        }
Exemplo n.º 30
0
        private static short[] DropManyU(
            IVisio.Page page,
            IList <IVisio.Master> masters,
            IList <Drawing.Rectangle> rects)
        {
            var points   = rects.Select(r => r.Center).ToList();
            var shapeids = Pages.PageHelper.DropManyU(page, masters, points);

            var xfrm = new Shapes.XFormCells();

            var update = new ShapeSheet.Update(points.Count * 2);

            for (int i = 0; i < rects.Count(); i++)
            {
                xfrm.Width  = rects[i].Width;
                xfrm.Height = rects[i].Height;
                update.SetFormulas(shapeids[i], xfrm);
            }
            update.Execute(page);

            return(shapeids);
        }
Exemplo n.º 31
0
        public void SetOrientation(Pages.PrintPageOrientation orientation)
        {
            this.Client.Application.AssertApplicationAvailable();
            this.Client.Document.AssertDocumentAvailable();

            var app         = this.Client.Application.Get();
            var application = app;

            var active_page = application.ActivePage;

            if (orientation != Pages.PrintPageOrientation.Landscape && orientation != Pages.PrintPageOrientation.Portrait)
            {
                throw new System.ArgumentOutOfRangeException(nameof(orientation), "must be either Portrait or Landscape");
            }

            var old_orientation = PageCommands.GetOrientation(active_page);

            if (old_orientation == orientation)
            {
                // don't need to do anything
                return;
            }

            var old_size = this.GetSize();

            double new_height = old_size.Width;
            double new_width  = old_size.Height;

            var update = new ShapeSheet.Update(3);

            update.SetFormula(ShapeSheet.SRCConstants.PageWidth, new_width);
            update.SetFormula(ShapeSheet.SRCConstants.PageHeight, new_height);
            update.SetFormula(ShapeSheet.SRCConstants.PrintPageOrientation, (int)orientation);

            using (var undoscope = this.Client.Application.NewUndoScope("Set Page Orientation"))
            {
                update.Execute(active_page.PageSheet);
            }
        }
Exemplo n.º 32
0
        public void PasteSize(IList <IVisio.Shape> target_shapes, bool paste_width, bool paste_height)
        {
            this.Client.Application.AssertApplicationAvailable();
            this.Client.Document.AssertDocumentAvailable();

            var shapes = this.GetTargetShapes(target_shapes);

            if (shapes.Count < 1)
            {
                return;
            }

            if ((!this.cached_size_width.HasValue) && (!this.cached_size_height.HasValue))
            {
                return;
            }

            var update   = new ShapeSheet.Update();
            var shapeids = shapes.Select(s => s.ID).ToList();

            foreach (var shapeid in shapeids)
            {
                if (paste_width)
                {
                    update.SetFormula((short)shapeid, ShapeSheet.SRCConstants.Width, this.cached_size_width.Value);
                }

                if (paste_height)
                {
                    update.SetFormula((short)shapeid, ShapeSheet.SRCConstants.Height, this.cached_size_height.Value);
                }
            }

            var application = this.Client.Application.Get();
            var active_page = application.ActivePage;

            update.Execute(active_page);
        }
        public static void Set(
            IVisio.Shape shape,
            string name,
            CustomPropertyCells cp)
        {
            if (shape == null)
            {
                throw new ArgumentNullException(nameof(shape));
            }

            CustomPropertyHelper.CheckValidCustomPropertyName(name);

            if (CustomPropertyHelper.Contains(shape, name))
            {
                string full_prop_name = CustomPropertyHelper.GetRowName(name);
                var cell_propname = shape.CellsU[full_prop_name];

                if (cell_propname == null)
                {
                    string msg = $"Could not retrieve cell for custom property \"{full_prop_name}\"";
                    throw new AutomationException(msg);
                }

                var update = new ShapeSheet.Update();
                update.SetFormulas(cp, cell_propname.Row);
                update.Execute(shape);

                return;

            }

            short row = shape.AddNamedRow(
                (short)IVisio.VisSectionIndices.visSectionProp,
                name,
                (short)IVisio.VisRowIndices.visRowProp);

            CustomPropertyHelper.Set(shape, row, cp);
        }
Exemplo n.º 34
0
        public void MoveTextToBottom(IList <IVisio.Shape> target_shapes)
        {
            this.Client.Application.AssertApplicationAvailable();
            this.Client.Document.AssertDocumentAvailable();

            var shapes = this.GetTargetShapes(target_shapes);

            if (shapes.Count < 1)
            {
                return;
            }

            var update = new ShapeSheet.Update();

            foreach (var shape in shapes)
            {
                if (0 ==
                    shape.RowExists[
                        (short)IVisio.VisSectionIndices.visSectionObject, (short)IVisio.VisRowIndices.visRowTextXForm,
                        (short)IVisio.VisExistsFlags.visExistsAnywhere])
                {
                    shape.AddRow((short)IVisio.VisSectionIndices.visSectionObject, (short)IVisio.VisRowIndices.visRowTextXForm, (short)IVisio.VisRowTags.visTagDefault);
                }
            }

            var application = this.Client.Application.Get();
            var shapeids    = shapes.Select(s => s.ID);

            foreach (int shapeid in shapeids)
            {
                update.SetFormula((short)shapeid, ShapeSheet.SRCConstants.TxtHeight, "Height*0");
                update.SetFormula((short)shapeid, ShapeSheet.SRCConstants.TxtPinY, "Height*0");
                update.SetFormula((short)shapeid, ShapeSheet.SRCConstants.VerticalAlign, "0");
            }
            var active_page = application.ActivePage;

            update.Execute(active_page);
        }
        public void Set(IList<IVisio.Shape> target_shapes, Shapes.FormatCells format)
        {
            this.Client.Application.AssertApplicationAvailable();
            this.Client.Document.AssertDocumentAvailable();

            var shapes = this.GetTargetShapes(target_shapes);

            if (shapes.Count<1)
            {
                return;
            }

            var update = new ShapeSheet.Update();
            var shapeids = shapes.Select(s => s.ID).ToList();

            foreach (int shapeid in shapeids)
            {
                update.SetFormulas((short)shapeid, format);
            }

            var application = this.Client.Application.Get();
            update.Execute(application.ActivePage);            
        }
Exemplo n.º 36
0
        public static void Set(
            IVisio.Shape shape,
            string name,
            CustomPropertyCells cp)
        {
            if (shape == null)
            {
                throw new ArgumentNullException("shape");
            }

            CheckValidCustomPropertyName(name);

            if (Contains(shape, name))
            {
                string full_prop_name = GetRowName(name);
                var    cell_propname  = shape.CellsU[full_prop_name];

                if (cell_propname == null)
                {
                    string msg = String.Format("Could not retrieve cell for custom property \"{0}\"", full_prop_name);
                    throw new AutomationException(msg);
                }

                var update = new ShapeSheet.Update();
                update.SetFormulas(cp, cell_propname.Row);
                update.Execute(shape);

                return;
            }

            short row = shape.AddNamedRow(
                (short)IVisio.VisSectionIndices.visSectionProp,
                name,
                (short)IVisio.VisRowIndices.visRowProp);

            Set(shape, row, cp);
        }
Exemplo n.º 37
0
        public void ResetOrigin(IVisio.Page page)
        {
            this.Client.Application.AssertApplicationAvailable();
            this.Client.Document.AssertDocumentAvailable();

            var application = this.Client.Application.Get();

            if (page == null)
            {
                page = application.ActivePage;
            }

            var update = new ShapeSheet.Update();

            update.SetFormula(ShapeSheet.SRCConstants.XGridOrigin, "0.0");
            update.SetFormula(ShapeSheet.SRCConstants.YGridOrigin, "0.0");
            update.SetFormula(ShapeSheet.SRCConstants.XRulerOrigin, "0.0");
            update.SetFormula(ShapeSheet.SRCConstants.YRulerOrigin, "0.0");

            using (var undoscope = this.Client.Application.NewUndoScope("Reset Page Origin"))
            {
                update.Execute(page.PageSheet);
            }
        }
Exemplo n.º 38
0
        public void Set(IList <IVisio.Shape> target_shapes, Shapes.FormatCells format)
        {
            this.Client.Application.AssertApplicationAvailable();
            this.Client.Document.AssertDocumentAvailable();

            var shapes = this.GetTargetShapes(target_shapes);

            if (shapes.Count < 1)
            {
                return;
            }

            var update   = new ShapeSheet.Update();
            var shapeids = shapes.Select(s => s.ID).ToList();

            foreach (int shapeid in shapeids)
            {
                update.SetFormulas((short)shapeid, format);
            }

            var application = this.Client.Application.Get();

            update.Execute(application.ActivePage);
        }
 private static void SetSize(IVisio.Page page, Drawing.Size size)
 {
     var page_cells = new PageCells();
     page_cells.PageHeight = size.Height;
     page_cells.PageWidth = size.Width;
     var pageupdate = new ShapeSheet.Update();
     pageupdate.SetFormulas(page_cells);
     pageupdate.Execute(page.PageSheet);
 }
        public void SetOrientation(Pages.PrintPageOrientation orientation)
        {
            this.Client.Application.AssertApplicationAvailable();
            this.Client.Document.AssertDocumentAvailable();

            var app = this.Client.Application.Get();
            var application = app;

            var active_page = application.ActivePage;

            if (orientation != Pages.PrintPageOrientation.Landscape && orientation != Pages.PrintPageOrientation.Portrait)
            {
                throw new System.ArgumentOutOfRangeException(nameof(orientation), "must be either Portrait or Landscape");
            }

            var old_orientation = PageCommands.GetOrientation(active_page);

            if (old_orientation == orientation)
            {
                // don't need to do anything
                return;
            }

            var old_size = this.GetSize();

            double new_height = old_size.Width;
            double new_width = old_size.Height;

            var update = new ShapeSheet.Update(3);
            update.SetFormula(ShapeSheet.SRCConstants.PageWidth, new_width);
            update.SetFormula(ShapeSheet.SRCConstants.PageHeight, new_height);
            update.SetFormula(ShapeSheet.SRCConstants.PrintPageOrientation, (int)orientation);

            using (var undoscope = this.Client.Application.NewUndoScope("Set Page Orientation"))
            {
                update.Execute(active_page.PageSheet);
            }
        }
        public void SetSize(IList<IVisio.Shape> target_shapes, double? w, double? h)
        {
            this.Client.Application.AssertApplicationAvailable();
            this.Client.Document.AssertDocumentAvailable();

            var shapes = this.GetTargetShapes(target_shapes);
            if (shapes.Count < 1)
            {
                return;
            } 

            var shapeids = shapes.Select(s=>s.ID).ToList();
            var update = new ShapeSheet.Update();
            foreach (int shapeid in shapeids)
            {
                if (w.HasValue && w.Value>=0)
                {
                    update.SetFormula((short)shapeid, ShapeSheet.SRCConstants.Width, w.Value);
                }
                if (h.HasValue && h.Value >= 0)
                {
                    update.SetFormula((short)shapeid, ShapeSheet.SRCConstants.Height, h.Value);                    
                }
            }

            var application = this.Client.Application.Get();
            using (var undoscope = this.Client.Application.NewUndoScope("Set Shape Size"))
            {
                var active_page = application.ActivePage;
                update.Execute(active_page);
            }
        }
        public void PasteSize(IList<IVisio.Shape> target_shapes, bool paste_width, bool paste_height)
        {
            this.Client.Application.AssertApplicationAvailable();
            this.Client.Document.AssertDocumentAvailable();
            
            var shapes = this.GetTargetShapes(target_shapes);

            if (shapes.Count < 1)
            {
                return;
            }

            if ((!this.cached_size_width.HasValue) && (!this.cached_size_height.HasValue))
            {
                return;
            }

            var update = new ShapeSheet.Update();
            var shapeids = shapes.Select(s => s.ID).ToList();

            foreach (var shapeid in shapeids)
            {
                if (paste_width)
                {
                    update.SetFormula((short)shapeid, ShapeSheet.SRCConstants.Width, this.cached_size_width.Value);
                }

                if (paste_height)
                {
                    update.SetFormula((short)shapeid, ShapeSheet.SRCConstants.Height, this.cached_size_height.Value);
                }
            }

            var application = this.Client.Application.Get();
            var active_page = application.ActivePage;
            update.Execute(active_page);
        }
        public void MoveTextToBottom(IList<IVisio.Shape> target_shapes)
        {
            this.Client.Application.AssertApplicationAvailable();
            this.Client.Document.AssertDocumentAvailable();

            var shapes = this.GetTargetShapes(target_shapes);
            if (shapes.Count < 1)
            {
                return ;
            }

            var update = new ShapeSheet.Update();
            foreach (var shape in shapes)
            {
                if (0 ==
                    shape.RowExists[
                        (short) IVisio.VisSectionIndices.visSectionObject, (short) IVisio.VisRowIndices.visRowTextXForm,
                        (short) IVisio.VisExistsFlags.visExistsAnywhere])
                {
                    shape.AddRow((short)IVisio.VisSectionIndices.visSectionObject, (short)IVisio.VisRowIndices.visRowTextXForm, (short)IVisio.VisRowTags.visTagDefault); 
                    
                }
            }

            var application = this.Client.Application.Get();
            var shapeids = shapes.Select(s=>s.ID);
            foreach (int shapeid in shapeids)
            {
                update.SetFormula((short)shapeid, ShapeSheet.SRCConstants.TxtHeight, "Height*0"); 
                update.SetFormula((short)shapeid, ShapeSheet.SRCConstants.TxtPinY, "Height*0"); 
                update.SetFormula((short)shapeid, ShapeSheet.SRCConstants.VerticalAlign, "0");
            } 
            var active_page = application.ActivePage; 
            update.Execute(active_page);
        }
Exemplo n.º 44
0
        public void SetResult(
            IList <IVisio.Shape> target_shapes,
            IList <ShapeSheet.SRC> srcs,
            IList <string> results, IVisio.VisGetSetArgs flags)
        {
            this.Client.Application.AssertApplicationAvailable();
            this.Client.Document.AssertDocumentAvailable();

            var shapes = this.GetTargetShapes(target_shapes);

            if (shapes.Count < 1)
            {
                this.Client.WriteVerbose("SetResult: Zero Shapes. Not performing Operation");
                return;
            }

            if (srcs == null)
            {
                throw new System.ArgumentNullException(nameof(srcs));
            }

            if (results == null)
            {
                throw new System.ArgumentNullException(nameof(results));
            }

            if (results.Any(f => f == null))
            {
                this.Client.WriteVerbose("SetResult: One of the Input Results is a NULL value");
                throw new System.ArgumentException("results contains a null value", nameof(results));
            }

            this.Client.WriteVerbose("SetResult: src count= {0} and result count = {1}", srcs.Count, results.Count);

            if (results.Count != srcs.Count)
            {
                string msg = $"Must have the same number of srcs ({srcs.Count}) and results ({results.Count})";
                throw new System.ArgumentException(msg, nameof(results));
            }

            var shapeids = shapes.Select(s => s.ID).ToList();

            int num_results = results.Count;
            var update      = new ShapeSheet.Update(shapes.Count * num_results);

            update.BlastGuards  = ((short)flags & (short)IVisio.VisGetSetArgs.visSetBlastGuards) != 0;
            update.TestCircular = ((short)flags & (short)IVisio.VisGetSetArgs.visSetTestCircular) != 0;

            foreach (var shapeid in shapeids)
            {
                for (int i = 0; i < num_results; i++)
                {
                    var src    = srcs[i];
                    var result = results[i];
                    update.SetResult((short)shapeid, src, result, IVisio.VisUnitCodes.visNumber);
                }
            }

            var surface     = this.Client.ShapeSheet.GetShapeSheetSurface();
            var application = this.Client.Application.Get();

            using (var undoscope = this.Client.Application.NewUndoScope("Set ShapeSheet Result"))
            {
                update.Execute(surface);
            }
        }
        public static void SetTabStops(IVisio.Shape shape, IList<TabStop> stops)
        {
            if (shape == null)
            {
                throw new System.ArgumentNullException(nameof(shape));
            }

            if (stops == null)
            {
                throw new System.ArgumentNullException(nameof(stops));
            }

            TextFormat.ClearTabStops(shape);
            if (stops.Count < 1)
            {
                return;
            }

            const short row = 0;
            var invariant_culture = System.Globalization.CultureInfo.InvariantCulture;
            var vis_tab_stop_count = (short)IVisio.VisCellIndices.visTabStopCount;
            var tabstopcountcell = shape.CellsSRC[TextFormat.tab_section, row, vis_tab_stop_count];
            tabstopcountcell.FormulaU = stops.Count.ToString(invariant_culture);

            // set the number of tab stobs allowed for the shape
            var tagtab = TextFormat.GetTabTagForStops(stops.Count);
            shape.RowType[TextFormat.tab_section, (short)IVisio.VisRowIndices.visRowTab] = (short)tagtab;

            // add tab properties for each stop
            var update = new ShapeSheet.Update();
            for (int stop_index = 0; stop_index < stops.Count; stop_index++)
            {
                int i = stop_index * 3;

                var alignment = ((int)stops[stop_index].Alignment).ToString(invariant_culture);
                var position = ((int)stops[stop_index].Position).ToString(invariant_culture);

                var src_tabpos = new ShapeSheet.SRC(TextFormat.tab_section, row, (short)(i + 1));
                var src_tabalign = new ShapeSheet.SRC(TextFormat.tab_section, row, (short)(i + 2));
                var src_tabother = new ShapeSheet.SRC(TextFormat.tab_section, row, (short)(i + 3));

                update.SetFormula(src_tabpos, position); // tab position
                update.SetFormula(src_tabalign, alignment); // tab alignment
                update.SetFormula(src_tabother, "0"); // tab unknown
            }

            update.Execute(shape);
        }
        public static void Duplicate(
            IVisio.Page src_page,
            IVisio.Page dest_page)
        {
            var app = src_page.Application;
            short copy_paste_flags = (short)IVisio.VisCutCopyPasteCodes.visCopyPasteNoTranslate;

            // handle the source page
            if (src_page == null)
            {
                throw new System.ArgumentNullException(nameof(src_page));
            }

            if (dest_page == null)
            {
                throw new System.ArgumentNullException(nameof(dest_page));
            }

            if (dest_page == src_page)
            {
                throw new AutomationException("Destination Page cannot be Source Page");
            }


            if (src_page != app.ActivePage)
            {
                throw new AutomationException("Source page must be active page.");
            }

            var src_page_shapes = src_page.Shapes;
            int num_src_shapes=src_page_shapes.Count;

            if (num_src_shapes > 0)
            {
                var active_window = app.ActiveWindow;
                active_window.SelectAll();
                var selection = active_window.Selection;
                selection.Copy(copy_paste_flags);
                active_window.DeselectAll();
            }

            var src_pagesheet = src_page.PageSheet;
            var pagecells = PageCells.GetCells(src_pagesheet);


            // handle the dest page

            // first update all the page cells
            var dest_pagesheet = dest_page.PageSheet;
            var update = new ShapeSheet.Update();
            update.SetFormulas(pagecells);
            update.Execute(dest_pagesheet);

            // make sure the new page looks like the old page
            dest_page.Background = src_page.Background;
            
            // then paste any contents from the first page
            if (num_src_shapes>0)
            {
                dest_page.Paste(copy_paste_flags);                
            }
        }
        public void ResetOrigin(IVisio.Page page)
        {
            this.Client.Application.AssertApplicationAvailable();
            this.Client.Document.AssertDocumentAvailable();

            var application = this.Client.Application.Get();
            if (page == null)
            {
                page = application.ActivePage;
            }

            var update = new ShapeSheet.Update();

            update.SetFormula(ShapeSheet.SRCConstants.XGridOrigin, "0.0");
            update.SetFormula(ShapeSheet.SRCConstants.YGridOrigin, "0.0");
            update.SetFormula(ShapeSheet.SRCConstants.XRulerOrigin, "0.0");
            update.SetFormula(ShapeSheet.SRCConstants.YRulerOrigin, "0.0");

            using (var undoscope = this.Client.Application.NewUndoScope("Reset Page Origin"))
            {
                update.Execute(page.PageSheet);
            }
        }
        public static void Set(IVisio.Shape shape, string name, ShapeSheet.FormulaLiteral value, ShapeSheet.FormulaLiteral prompt)
        {
            if (shape == null)
            {
                throw new System.ArgumentNullException(nameof(shape));
            }

            UserDefinedCellsHelper.CheckValidName(name);

            if (UserDefinedCellsHelper.Contains(shape, name))
            {
                string full_prop_name = UserDefinedCellsHelper.GetRowName(name);

                if (value.HasValue)
                {
                    string value_cell_name = full_prop_name;
                    var cell = shape.CellsU[value_cell_name];
                    cell.FormulaU = value.Encode();                    
                }

                if (prompt.HasValue)
                {
                    string prompt_cell_name = full_prop_name+".Prompt";
                    var cell = shape.CellsU[prompt_cell_name];
                    cell.FormulaU = prompt.Encode();                                        
                }
                return;
            }

            short row = shape.AddNamedRow(
                UserDefinedCellsHelper._userdefinedcell_section,
                name,
                (short)IVisio.VisRowIndices.visRowUser);

            var update = new ShapeSheet.Update();

            if (value.HasValue)
            {
                var src = new ShapeSheet.SRC(UserDefinedCellsHelper._userdefinedcell_section, row, (short)IVisio.VisCellIndices.visUserValue);
                update.SetFormula(src, value.Encode());
            }

            if (prompt.HasValue)
            {
                var src = new ShapeSheet.SRC(UserDefinedCellsHelper._userdefinedcell_section, row, (short)IVisio.VisCellIndices.visUserPrompt);
                update.SetFormula(src, prompt.Encode());
            }

            update.Execute(shape);
        }
        public void ToogleCase(IList<IVisio.Shape> target_shapes)
        {
            this.Client.Application.AssertApplicationAvailable();
            this.Client.Document.AssertDocumentAvailable();
            
            var shapes = this.GetTargetShapes(target_shapes);

            if (shapes.Count < 1)
            {
                return;
            }

            var application = this.Client.Application.Get();
            using (var undoscope = this.Client.Application.NewUndoScope("Toggle Shape Text Case"))
            {
                var shapeids = shapes.Select(s => s.ID).ToList();

                var page = application.ActivePage;
                // Store all the formatting
                var formats = Text.TextFormat.GetFormat(page, shapeids);

                // Change the text - this will wipe out all the character and paragraph formatting
                foreach (var shape in shapes)
                {
                    string t = shape.Text;
                    if (t.Length < 1)
                    {
                        continue;
                    }
                    shape.Text = TextCommandsUtil.toggle_case(t);
                }

                // Now restore all the formatting - based on any initial formatting from the text

                var update = new ShapeSheet.Update();
                for (int i = 0; i < shapes.Count; i++)
                {
                    var format = formats[i];

                    if (format.CharacterFormats.Count>0)
                    {
                        var fmt = format.CharacterFormats[0];
                        update.SetFormulas((short) shapeids[i], fmt, 0);
                    }

                    if (format.ParagraphFormats.Count > 0)
                    {
                        var fmt = format.ParagraphFormats[0];
                        update.SetFormulas((short)shapeids[i], fmt, 0);
                    }
                }

                update.Execute(page);
            }
        }
        public void SetSize(Drawing.Size new_size)
        {
            this.Client.Application.AssertApplicationAvailable();
            this.Client.Document.AssertDocumentAvailable();

            var application = this.Client.Application.Get();
            using (var undoscope = this.Client.Application.NewUndoScope("Set Page Size"))
            {
                var active_page = application.ActivePage;
                var page_sheet = active_page.PageSheet;
                var update = new ShapeSheet.Update(2);
                update.SetFormula(ShapeSheet.SRCConstants.PageWidth, new_size.Width);
                update.SetFormula(ShapeSheet.SRCConstants.PageHeight, new_size.Height);
                update.Execute(page_sheet);
            }
        }
Exemplo n.º 51
0
        private static short[] DropManyU(
            IVisio.Page page,
            IList<IVisio.Master> masters,
            IList<Drawing.Rectangle> rects)
        {
            var points = rects.Select(r => r.Center).ToList();
            var shapeids = Pages.PageHelper.DropManyU(page, masters, points);

            var xfrm = new Shapes.XFormCells();

            var update = new ShapeSheet.Update(points.Count*2);
            for (int i = 0; i < rects.Count(); i++)
            {
                xfrm.Width = rects[i].Width;
                xfrm.Height = rects[i].Height;
                update.SetFormulas(shapeids[i], xfrm);
            }
            update.Execute(page);

            return shapeids;
        }
 public void Finish()
 {
     var update = new ShapeSheet.Update();
     foreach (var block in this.Blocks)
     {
         update.SetFormulas((short)block.VisioShapeID,block.FormatCells);
         update.SetFormulas((short)block.VisioShapeID,block.TextBlockCells);
         update.SetFormulas((short)block.VisioShapeID,block.ParagraphCells, 0);
         update.SetFormulas((short)block.VisioShapeID,block.CharacterCells, 0);
     }
     update.Execute(this.page);
 }
Exemplo n.º 53
0
        public IVisio.Page Render(IVisio.Document doc)
        {
            if (!this.IsLayedOut)
            {
                string msg =
                    $"{typeof (ContainerLayout).Name} usage error. Call PerformLayout() before calling Render().";
                throw new AutomationException(msg);
            }
            // create a new drawing
            var app = doc.Application;
            var docs = app.Documents;
            var pages = doc.Pages;
            var page = pages.Add();

            // load the stencil used to draw the items
            var item_stencil = docs.OpenStencil(this.LayoutOptions.ManualItemStencil);
            var item_stencil_masters = item_stencil.Masters;
            var item_master = item_stencil_masters[this.LayoutOptions.ManualItemMaster];
            var plain_container_master = item_stencil_masters[this.LayoutOptions.ManualContainerMaster];

            var page_shapes = page.Shapes;

            // Drop the container shapes
            var ct_items = this.Containers.ToList();
            var ct_rects = ct_items.Select(item => item.Rectangle).ToList();
            var masters = ct_items.Select(i => plain_container_master).ToList();
            short[] ct_shapeids = ContainerLayout.DropManyU(page, masters, ct_rects);

            // associate each container with the corresponding shape oject and shape id
            for (int i = 0; i < ct_items.Count; i++)
            {
                var ct_item = ct_items[i];
                var ct_shapeid = ct_shapeids[i];
                var shape = page_shapes[ct_shapeid];
                ct_item.VisioShape = shape;
                ct_item.ShapeID = ct_shapeid;
            }

            // Render the items
            var items = this.ContainerItems.ToList();
            var item_rects = items.Select(item => item.Rectangle).ToList();
            var item_masters = items.Select(i => item_master).ToList();
            short[] shapeids = ContainerLayout.DropManyU(page, item_masters, item_rects);

            // Associate each item with the corresponding shape object and shape id
            for (int i = 0; i < items.Count; i++)
            {
                var item = items[i];
                var shapeid = shapeids[i];
                var shape = page_shapes[shapeid];
                item.VisioShape = shape;
                item.ShapeID = shapeid;
            }

            // Often useful to show everthing because these diagrams can get large
            app.ActiveWindow.ViewFit = (short)IVisio.VisWindowFit.visFitPage;

            // Set the items
            foreach (var item in items.Where(i => i.Text != null))
            {
                item.VisioShape.Text = item.Text;
            }

            var update = new ShapeSheet.Update();

            // Format the containers and shapes

            foreach (var item in this.Containers)
            {
                this.LayoutOptions.ContainerFormatting.Apply(update, item.ShapeID,item.ShapeID);
            }

            foreach (var item in this.ContainerItems)
            {
                this.LayoutOptions.ContainerItemFormatting.Apply(update, item.ShapeID, item.ShapeID);
            }

            update.BlastGuards = true;
            update.Execute(page);

            // Set the Container Text
            foreach (var ct in this.Containers)
            {
                if (ct.Text != null)
                {
                    ct.Text.SetText(ct.VisioShape);
                }
            }

            page.ResizeToFitContents();
            app.ActiveWindow.ViewFit = (short)IVisio.VisWindowFit.visFitPage;

            return page;
        }
        /// <summary>
        /// Remove all tab stops on the shape
        /// </summary>
        /// <param name="shape"></param>
        private static void ClearTabStops(IVisio.Shape shape)
        {
            if (shape == null)
            {
                throw new System.ArgumentNullException(nameof(shape));
            }

            int num_existing_tabstops = TextFormat.GetTabStopCount(shape);

            if (num_existing_tabstops < 1)
            {
                return;
            }

            var cell_tabstopcount = shape.CellsSRC[TextFormat.src_tabstopcount.Section, TextFormat.src_tabstopcount.Row, TextFormat.src_tabstopcount.Cell];
            cell_tabstopcount.FormulaForce = "0";

            const string formula = "0";

            var update = new ShapeSheet.Update();
            for (int i = 1; i < num_existing_tabstops * 3; i++)
            {
                var src = new ShapeSheet.SRC(TextFormat.tab_section, (short)IVisio.VisRowIndices.visRowTab,
                                                (short)i);
                update.SetFormula(src, formula);
            }

            update.Execute(shape);
        }
        private static void update_xfrms(IVisio.Page page, IList<int> shapeids, IList<Shapes.XFormCells> xfrms)
        {

            var update = new ShapeSheet.Update();
            for (int i = 0; i < shapeids.Count; i++)
            {
                var shape_id = shapeids[i];
                var xfrm = xfrms[i];
                update.SetFormulas((short)shape_id, xfrm);
            }
            update.Execute(page);
        }
        public void SetLock(IList<IVisio.Shape> target_shapes, Shapes.LockCells lockcells)
        {
            this.Client.Application.AssertApplicationAvailable();
            this.Client.Document.AssertDocumentAvailable();

            var shapes = this.GetTargetShapes(target_shapes);
            if (shapes.Count < 1)
            {
                return;
            } 

            var selection = this.Client.Selection.Get();
            var shapeids = selection.GetIDs();
            var update = new ShapeSheet.Update();

            foreach (int shapeid in shapeids)
            {
                update.SetFormulas((short)shapeid, lockcells);
            }

            var application = this.Client.Application.Get();
            using (var undoscope = this.Client.Application.NewUndoScope("Set Shape Lock Properties"))
            {
                var active_page = application.ActivePage;
                update.Execute(active_page);
            }
        }
        public void SetText(IVisio.Shape shape)
        {
            if (shape == null)
            {
                throw new System.ArgumentNullException(nameof(shape));
            }

            // First just set all the text
            string full_doc_inner_text = this.GetInnerText();
            shape.Text = full_doc_inner_text;

            // Find all the regions needing formatting
            var markupinfo = this.GetMarkupInfo();
            var regions_to_format = markupinfo.FormatRegions.Where(region => region.Length >= 1).ToList();

            
            var default_chars_bias = IVisio.VisCharsBias.visBiasLeft;


            var update = new ShapeSheet.Update();

            foreach (var region in regions_to_format)
            {

                // Apply character formatting
                var charcells = region.Element.CharacterCells;
                if (charcells != null)
                {
                    var chars = shape.Characters;
                    chars.Begin = region.Start;
                    chars.End = region.End;
                    chars.CharProps[ShapeSheet.SRCConstants.CharColor.Cell] = 0;
                    short rownum = chars.CharPropsRow[(short) default_chars_bias];

                    if (rownum < 0)
                    {
                        throw new AutomationException("Could not create Character row");
                    }

                    update.Clear();
                    charcells.ApplyFormulas(update, rownum);
                    update.Execute(shape);
                }

                // Apply paragraph formatting
                var paracells = region.Element.ParagraphCells;
                if (paracells != null)
                {
                    var chars = shape.Characters;
                    chars.Begin = region.Start;
                    chars.End = region.End;
                    chars.ParaProps[ShapeSheet.SRCConstants.Para_Bullet.Cell] = 0;
                    short rownum = chars.ParaPropsRow[(short) default_chars_bias];

                    if (rownum < 0)
                    {
                        throw new AutomationException("Could not create Paragraph row");
                    }

                    update.Clear();
                    paracells.ApplyFormulas(update, rownum);
                    update.Execute(shape);
                }
            }

            // Insert the fields
            // note: Fields are added in reverse because it is simpler to keep track of the insertion positions
            foreach (var field_region in markupinfo.FieldRegions.Where(region => region.Length >= 1).Reverse())
            {
                var chars = shape.Characters;
                chars.Begin = field_region.Start;
                chars.End = field_region.End;
                chars.AddField((short) field_region.Field.Category, (short) field_region.Field.Code,
                               (short) field_region.Field.Format);
                var fr = field_region;
            }
        }
        public void PasteFormat(IVisio.Page page, IList<int> shapeids, FormatCategory category, bool applyformulas)
        {
            var update = new ShapeSheet.Update();

            foreach (var shape_id in shapeids)
            {
                foreach (var cellrec in this.Cells)
                {
                    if (!cellrec.MatchesCategory(category))
                    {
                        continue;
                    }

                    var sidsrc = new ShapeSheet.SIDSRC((short)shape_id, cellrec.SRC);

                    if (applyformulas)
                    {
                        update.SetFormula(sidsrc, cellrec.Formula);
                        
                    }
                    else
                    {
                        if (cellrec.Result != null)
                        {
                            update.SetFormula(sidsrc, cellrec.Result);
                        }
                    }
                }
            }

            update.Execute(page);
        }
        public void FitShapeToText(IList<IVisio.Shape> target_shapes)
        {
            this.Client.Application.AssertApplicationAvailable();
            this.Client.Document.AssertDocumentAvailable();

            var shapes = this.GetTargetShapes2D(target_shapes);
            if (shapes.Count < 1)
            {
                return;
            }

            var application = this.Client.Application.Get();
            var active_page = application.ActivePage;
            var shapeids = shapes.Select(s => s.ID).ToList();

            using (var undoscope = this.Client.Application.NewUndoScope("FitShapeToText"))
            {
                // Calculate the new sizes for each shape
                var new_sizes = new List<Drawing.Size>(shapeids.Count);
                foreach (var shape in shapes)
                {
                    var text_bounding_box = shape.GetBoundingBox(Microsoft.Office.Interop.Visio.VisBoundingBoxArgs.visBBoxUprightText).Size;
                    var wh_bounding_box = shape.GetBoundingBox(Microsoft.Office.Interop.Visio.VisBoundingBoxArgs.visBBoxUprightWH).Size;

                    double max_w = System.Math.Max(text_bounding_box.Width, wh_bounding_box.Width);
                    double max_h = System.Math.Max(text_bounding_box.Height, wh_bounding_box.Height);
                    var max_size = new Drawing.Size(max_w, max_h);
                    new_sizes.Add(max_size);
                }

                var src_width = ShapeSheet.SRCConstants.Width;
                var src_height = ShapeSheet.SRCConstants.Height;

                var update = new ShapeSheet.Update();
                for (int i = 0; i < new_sizes.Count; i++)
                {
                    var shapeid = shapeids[i];
                    var new_size = new_sizes[i];
                    update.SetFormula((short)shapeid, src_width, new_size.Width);
                    update.SetFormula((short)shapeid, src_height, new_size.Height);
                }

                update.Execute(active_page);
            }
        }