Esempio n. 1
0
        private void decorate_Click(object sender, EventArgs e)
        {
            // open log if not yet open
            var tr2 = new TextWriterTraceListener(File.CreateText(selectedLogFileName.Text));

            Trace.Listeners.Add(tr2);

            // do it with Excel ....
            // all the excel stuff
            var connectionList = new List <ShapeConnection>();


            // Excel related aspects

            var xlApp = new Microsoft.Office.Interop.Excel.Application {
                Visible = true
            };

            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

            // open the excel file
            String   excelFileName = connectionsFileName.Text;
            Workbook xlWorkBook    = xlApp.Workbooks.Open(excelFileName,
                                                          _missing, _missing, _missing, _missing, _missing, _missing,
                                                          _missing, _missing,
                                                          _missing, _missing, _missing, _missing, _missing, _missing);



            if (excelFileName != "" && File.Exists(excelFileName))
            {
                xlWorkBook = xlApp.Workbooks.Open(excelFileName,
                                                  _missing, _missing, _missing, _missing, _missing, _missing, _missing,
                                                  _missing,
                                                  _missing, _missing, _missing, _missing, _missing, _missing);

                var xlWorkSheet = (Worksheet)xlWorkBook.Sheets[connectionsSheetName.Text];


                int sourceElementNameCol = MmExcelUtil.ColumnNumber(xlWorkSheet, sourceElementColumnName.Text);
                int targetElementNameCol = MmExcelUtil.ColumnNumber(xlWorkSheet, targetElementColumnName.Text);

                Trace.WriteLine("  source element name column " + sourceElementNameCol);
                Trace.WriteLine("  target element name column " + targetElementNameCol);


                int  row      = 2;
                bool finished = false;

                while (!finished)
                {
                    String sourceElement = MmExcelUtil.GetCell(xlWorkSheet, row, sourceElementNameCol);
                    String targetElement = MmExcelUtil.GetCell(xlWorkSheet, row, targetElementNameCol);


                    finished = sourceElement == "" && targetElement == "";

                    if (!finished)
                    {
                        connectionList.Add(
                            new ShapeConnection(sourceElement, targetElement));
                    }
                    row++;
                }

                Trace.WriteLine("  connections read from Excel " + connectionList.Count);
            }

            // Visio related aspects

            var visapp = new Microsoft.Office.Interop.Visio.Application();



            Document landscape = visapp.Documents.Open(visioFileName.Text);
            Page     page      = visapp.ActivePage;



            Trace.WriteLine("Landscape Masters " + landscape.Masters.Count);
            foreach (Master m in landscape.Masters)
            {
                Trace.WriteLine("VisioStencil Master " + m.Name);
            }



            Master connectorMaster = landscape.Masters[connectorStencilName.Text];



            var placedMapShapes = new Dictionary <String, ShapeRef>();

            MmVisioUtil.GetShapeList(shapeName.Text, page, elementTagName.Text, placedMapShapes);


            Trace.WriteLine("  shapes in Visio " + placedMapShapes.Count);


            // filter internal flows
            for (int con = connectionList.Count - 1; con >= 1; con--)
            {
                if (connectionList[con].SourceElement == connectionList[con].TargetElement)
                {
                    connectionList.RemoveAt(con);
                }
            }
            Trace.WriteLine("  number of cross system connections  " + connectionList.Count);


            if (summarizeCheckBox.Checked)
            {
                connectionList.Sort((c1, c2) => String.CompareOrdinal(c1.ConnectionName(), c2.ConnectionName()));

                for (int con = connectionList.Count - 1; con >= 1; con--)
                {
                    if (connectionList[con - 1].ConnectionName() == connectionList[con].ConnectionName())
                    {
                        connectionList[con - 1].Weight = (connectionList[con].Weight + 1);
                        connectionList.RemoveAt(con);
                    }
                    else
                    {
                        Trace.WriteLine(connectionList[con].ConnectionName() + " " + connectionList[con].Weight);
                    }
                }

                Trace.WriteLine("  summarized to  " + connectionList.Count);
            }



            int progressValue = 0;

            decorationProgressBar.Minimum = 0;
            decorationProgressBar.Maximum = connectionList.Count - 1;

            // tweak performance
            bool  pInhibitSelectChange = visapp.InhibitSelectChange;
            short pDeferRecalc         = visapp.DeferRecalc;
            bool  pShowChanges         = visapp.ShowChanges;
            short pScreenUpdating      = visapp.ScreenUpdating;
            int   pOnDataChangeDelay   = visapp.OnDataChangeDelay;

            visapp.InhibitSelectChange = true;
            visapp.DeferRecalc         = 1;
            visapp.ShowChanges         = false;
            visapp.ScreenUpdating      = 0;
            visapp.OnDataChangeDelay   = -2;


            foreach (ShapeConnection con in connectionList)
            {
                decorationProgressBar.Value = progressValue++;
                if (progressValue % 50 == 0)
                {
                    Trace.Flush();
                }

                ShapeRef source = null;
                ShapeRef target = null;

                if (placedMapShapes.ContainsKey(con.SourceElement))
                {
                    source = placedMapShapes[con.SourceElement];
                }

                if (placedMapShapes.ContainsKey(con.TargetElement))
                {
                    target = placedMapShapes[con.TargetElement];
                }

                if (source != null && target != null)
                {
                    Shape connector = page.Drop(connectorMaster, 4.50, 4.50);
                    MmVisioUtil.ConnectShapes(source.Shape, target.Shape, connector);
                    // connector.Cells["EndArrow"].Formula = "=5";
                }
                else
                {
                    Trace.WriteLine(" cannot add connector between " + con.SourceElement + " to " + con.TargetElement);
                }
            }



            Trace.WriteLine("  decorations read from Excel " + connectionList.Count);

            // Reset Visio properties
            visapp.InhibitSelectChange = pInhibitSelectChange;
            visapp.DeferRecalc         = pDeferRecalc;
            visapp.ShowChanges         = pShowChanges;
            visapp.ScreenUpdating      = pScreenUpdating;
            visapp.OnDataChangeDelay   = pOnDataChangeDelay;



            Trace.Flush();
            Trace.Close();
        }
Esempio n. 2
0
        /// <summary>
        /// Utility function to help to prepare a map. It puts elements in a grid onto the visio
        /// and allows to tune the rough layout of the map background elements. Once the elements have been
        /// put onto the map, all map background elements are right sized and aligned.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PrepareClick(object sender, EventArgs e)
        {
            // open log if not yet open
            var tr2 = new TextWriterTraceListener(File.CreateText(selectedLogFileName.Text));

            Trace.Listeners.Add(tr2);

            // Excel related aspects

            var xlApp = new Microsoft.Office.Interop.Excel.Application {
                Visible = true
            };

            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

            // open the excel file
            String   excelFileName = mapFileName.Text;
            Workbook xlWorkBook    = xlApp.Workbooks.Open(excelFileName,
                                                          _missing, _missing, _missing, _missing, _missing, _missing, _missing, _missing,
                                                          _missing, _missing, _missing, _missing, _missing, _missing);


            // read the columns of the sheets containing the map and the elements

            // reference column number --> column name for the map
            var mapColumnMap = new Dictionary <int, string>();
            int mapMaxColumn;
            int mapNameColumn;

            MmExcelUtil.BuildColumnMap(xlWorkBook, mapSheetName.Text, mapNameColumnName.Text, mapColumnMap, out mapMaxColumn, out mapNameColumn);


            // reference column number --> tag for the elements
            var elementColumnMap = new Dictionary <int, string>();
            int elementMaxColumn;
            int elementNameColumn;

            MmExcelUtil.BuildColumnMap(xlWorkBook, elementSheetName.Text, elementNameColumnName.Text, elementColumnMap, out elementMaxColumn, out elementNameColumn);

            // list of all the constituents of the map
            var mapList = new List <GenericElement>();

            MmExcelUtil.BuildList(xlWorkBook, mapSheetName.Text, mapNameColumn, mapMaxColumn, mapColumnMap, mapList, mapNameColumnName.Text);

            // list of all the elements to be maped
            var elementList = new List <GenericElement>();

            MmExcelUtil.BuildList(xlWorkBook, elementSheetName.Text, elementNameColumn, elementMaxColumn, elementColumnMap, elementList, elementNameColumnName.Text);


            // Visio related aspects

            var visapp = new Microsoft.Office.Interop.Visio.Application();

            // Prepare Visio
            Document landscape = visapp.Documents.Open(visioFileName.Text);
            Document stencil   = visapp.Documents.OpenEx(stencilFileName.Text, (short)VisOpenSaveArgs.visOpenDocked);
            Page     page      = visapp.ActivePage;



            Master elementStencil = stencil.Masters[elementStencilName.Text];

            if (elementStencil == null)
            {
                throw new ArgumentNullException("elementStencil");
            }

            Shape elementShape = page.Drop(elementStencil, 0.0, 0.0);

            double pageWidth = page.PageSheet.CellsU["PageWidth"].ResultIU;
            double pageHight = page.PageSheet.CellsU["PageHeight"].ResultIU;

            double elementWidth  = elementShape.CellsU["Width"].ResultIU;
            double elementHeight = elementShape.CellsU["Height"].ResultIU;
            double space         = double.Parse(spaceText.Text);

            double numColumns = (pageWidth - space) / (elementWidth + space);
            double numRows    = (pageHight - space) / (elementHeight + space);


            Trace.WriteLine("Size of an page    " + pageWidth + " " + pageHight);
            Trace.WriteLine("Size of an element " + elementWidth + " " + elementHeight);
            Trace.WriteLine("Number " + numRows + " rows " + numColumns + " columns");

            for (int r = 1; r < numRows; r++)
            {
                for (int c = 1; c < numColumns; c++)
                {
                    if (r == 1 && c == 1)
                    {
                        elementShape.Cells["PinX"].ResultIU = MmVisioUtil.CalculateX(c, elementWidth, space);
                        elementShape.Cells["PinY"].ResultIU = MmVisioUtil.CalculateY(r, elementHeight, space);
                    }
                    else
                    {
                        elementShape = page.Drop(elementStencil, MmVisioUtil.CalculateX(c, elementWidth, space), MmVisioUtil.CalculateY(r, elementHeight, space));
                    }

                    double elementX    = elementShape.CellsU["PinX"].ResultIU;
                    double elementY    = elementShape.CellsU["PinY"].ResultIU;
                    String coordinates = "Element at " + String.Format("{0:0.00}", elementX) + "/" + String.Format("{0:0.00}", elementY);
                    elementShape.Text = coordinates;
                }
            }


            var placedMapShapes = new Dictionary <String, ShapeRef>();

            MmVisioUtil.GetShapeList(mapStencilName.Text, page, mapNameColumnName.Text, placedMapShapes);

            // Place missing map Elements on Landscape
            var missingMapShapes = new List <String>();

            DetermineMissingMapShapes(mapList, placedMapShapes, missingMapShapes);


            Master mapStencil = stencil.Masters[mapStencilName.Text];


            foreach (GenericElement ge in mapList)
            {
                if (missingMapShapes.Contains(ge.Name))
                {
                    MmVisioUtil.PlaceGenericElement(page, mapStencil, ge, 0, 0, space, mapColumnMap, false);
                }
            }


            Trace.Flush();
            Trace.Close();
        }
Esempio n. 3
0
        /// <summary>
        /// The main routine which generates the map based on the parameters specified in the
        /// dialog on the user interface. The resolut is the final map with placed elements
        /// and right sized and adjusted map in the background.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GenerateLandscapeButtonClick(object sender, EventArgs e)
        {
            // open log if not yet open
            var tr2 = new TextWriterTraceListener(File.CreateText(selectedLogFileName.Text));

            Trace.Listeners.Add(tr2);

            var errorList = new List <String>();

            // Excel related aspects
            var xlApp = new Microsoft.Office.Interop.Excel.Application {
                Visible = true
            };

            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

            // open the excel file
            String   excelFileName = mapFileName.Text;
            Workbook xlWorkBook    = xlApp.Workbooks.Open(excelFileName,
                                                          _missing, _missing, _missing, _missing, _missing, _missing,
                                                          _missing, _missing,
                                                          _missing, _missing, _missing, _missing, _missing, _missing);


            // read the columns of the sheets containing the map and the elements

            // reference column number --> tag for the map
            var mapColumnMap = new Dictionary <int, string>();
            int mapMaxColumn;
            int mapNameColumn;

            MmExcelUtil.BuildColumnMap(xlWorkBook, mapSheetName.Text, mapNameColumnName.Text, mapColumnMap,
                                       out mapMaxColumn, out mapNameColumn);

            // reference column number --> tag for the elements
            var elementColumnMap = new Dictionary <int, string>();
            int elementMaxColumn;
            int elementNameColumn;

            MmExcelUtil.BuildColumnMap(xlWorkBook, elementSheetName.Text, elementNameColumnName.Text, elementColumnMap,
                                       out elementMaxColumn, out elementNameColumn);

            // list of all the constituents of the map
            var mapList = new List <GenericElement>();

            MmExcelUtil.BuildList(xlWorkBook, mapSheetName.Text, mapNameColumn, mapMaxColumn, mapColumnMap, mapList,
                                  mapNameColumnName.Text);

            // list of all the elements to be maped
            var elementList = new List <GenericElement>();

            MmExcelUtil.BuildList(xlWorkBook, elementSheetName.Text, elementNameColumn, elementMaxColumn,
                                  elementColumnMap, elementList, elementNameColumnName.Text);



            // Visio related aspects

            var visapp = new Microsoft.Office.Interop.Visio.Application();

            // Prepare Visio
            Document landscape = visapp.Documents.Open(visioFileName.Text);
            Document stencil   = visapp.Documents.OpenEx(stencilFileName.Text, (short)VisOpenSaveArgs.visOpenDocked);
            Page     page      = visapp.ActivePage;


            var placedMapShapes     = new Dictionary <String, ShapeRef>();
            var placedElementShapes = new Dictionary <String, ShapeRef>();

            MmVisioUtil.GetShapeList(mapStencilName.Text, page, mapNameColumnName.Text, placedMapShapes);
            MmVisioUtil.GetShapeList(elementStencilName.Text, page, elementNameColumnName.Text, placedElementShapes);


            var missingMapShapes = new List <String>();

            DetermineMissingMapShapes(mapList, placedMapShapes, missingMapShapes);


            // Read all the mappings

            var mappingList = new List <Mapping>();

            MmExcelUtil.BuildMapping(xlWorkBook, mappingList, mappingSheetName.Text, mappingElementNameColumnName.Text,
                                     mappingMapNameColumnName.Text);


            // Create the mapping layout data structure

            var mapLayout = new Dictionary <String, MapLayoutElement>();

            PopulateMapLayout(mapList, elementList, mappingList, mapLayout);


            Master elementStencil = stencil.Masters[elementStencilName.Text];

//            Master marker = stencil.Masters["Marker"];

            Shape elementShape = page.Drop(elementStencil, 0.0, 0.0);

            double pageWidth = page.PageSheet.CellsU["PageWidth"].ResultIU;
            double pageHight = page.PageSheet.CellsU["PageHeight"].ResultIU;

            double elementWidth  = elementShape.CellsU["Width"].ResultIU;
            double elementHeight = elementShape.CellsU["Height"].ResultIU;
            double space         = double.Parse(spaceText.Text);

            double numColumns = (pageWidth - space) / (elementWidth + space);
            double numRows    = (pageHight - space) / (elementHeight + space);


            Trace.WriteLine("Size of an page     w " + pageWidth + " " + pageHight);
            Trace.WriteLine("Size of an element  w " + elementWidth + " " + elementHeight);
            Trace.WriteLine("Space                 " + space);
            Trace.WriteLine("Number                " + numRows + " rows " + numColumns + " columns");

            elementShape.Delete();

            // determines size of map stencil on drawing in terms of elements which can be placed on it
            // the results are kept in the MapLayoutElement
            CaclulateMapLayout(mapLayout, placedMapShapes, elementHeight, elementWidth, space);

            // Move elements on map to lower left

            foreach (ShapeRef sr in placedElementShapes.Values)
            {
                sr.Shape.Cells["PinX"].ResultIU = 0;
                sr.Shape.Cells["PinY"].ResultIU = 0;
            }

            // Populate Map with Elements

            if (optimitzeElementLayoutCheckBox.Checked)
            {
                foreach (MapLayoutElement mle in mapLayout.Values)
                {
                    String mapElementName = mle.Name;
                    if (placedMapShapes.ContainsKey(mapElementName))
                    {
                        Shape mapShape = placedMapShapes[mapElementName].Shape;


                        double mapWidth  = mapShape.CellsU["Width"].ResultIU;
                        double mapHeight = mapShape.CellsU["Height"].ResultIU;
                        double mapX      = mapShape.CellsU["PinX"].ResultIU;
                        double mapY      = mapShape.CellsU["PinY"].ResultIU;

                        if (titleSpaceCheckBox.Checked)
                        {
                            mapHeight -= space * 1.5;
                            mapY      -= space * 0.75;
                        }

                        double offsetX = elementWidth / 2 + (mapWidth - (mle.MaxColumn - mle.MinColumn + 1) * elementWidth - (mle.MaxColumn - mle.MinColumn) * space) / 2;
                        double offsetY = mapHeight - (mle.MaxRow - mle.MinRow + 1) * elementHeight - (mle.MaxRow - mle.MinRow) * space;

                        int row = mle.MaxRow;
                        int col = mle.MinColumn;

                        if (titleRowCheckBox.Checked)
                        {
                            row--;
                        }

                        int topRow = row; // keep to use below in layout

                        mle.mapElements().Sort();

                        foreach (String meName in mle.mapElements())
                        {
                            GenericElement ge = elementList.Find(delegate(GenericElement x)
                            {
                                return(x.Name == meName);
                            });

                            double ex = mapX - mapWidth / 2 + (col - mle.MinColumn) * (elementWidth + space) + offsetX;
                            double ey = mapY - mapHeight / 2 + (row - mle.MinRow) * (elementHeight + space) + offsetY;
                            MmVisioUtil.PlaceGenericElement(page, elementStencil, ge, ex, ey, elementColumnMap, showDescriptionsCheckBox.Checked);


                            DetermineNextCell(mle, topRow, ref row, ref col);
                        }
                    }
                }
            }
            else
            {
                foreach (MapLayoutElement mle in mapLayout.Values)
                {
                    int row = mle.MaxRow;
                    int col = mle.MinColumn;

                    if (titleRowCheckBox.Checked)
                    {
                        row--;
                    }
                    int topRow = row; // keep to use below in layout

                    mle.mapElements().Sort();

                    foreach (String meName in mle.mapElements())
                    {
                        GenericElement ge = elementList.Find(delegate(GenericElement x)
                        {
                            return(x.Name == meName);
                        });


                        MmVisioUtil.PlaceGenericElement(page, elementStencil, ge, row, col, space, elementColumnMap, true);


                        DetermineNextCell(mle, topRow, ref row, ref col);
                    }
                }
            }

            // Beautify Map

            if (!freezeMapCheckBox.Checked)
            {
                foreach (MapLayoutElement mle in mapLayout.Values)
                {
                    String mapElementName = mle.Name;
                    if (placedMapShapes.ContainsKey(mapElementName))
                    {
                        Shape mapShape = placedMapShapes[mapElementName].Shape;



                        double x = MmVisioUtil.CalculateX(mle.MinColumn, elementWidth, space) +
                                   (MmVisioUtil.CalculateX(mle.MaxColumn, elementWidth, space) -
                                    MmVisioUtil.CalculateX(mle.MinColumn, elementWidth, space)) / 2;
                        double y = MmVisioUtil.CalculateY(mle.MinRow, elementHeight, space) + 0.2 * space +
                                   (MmVisioUtil.CalculateY(mle.MaxRow, elementHeight, space) -
                                    MmVisioUtil.CalculateY(mle.MinRow, elementHeight, space)) / 2;
                        double w = (mle.MaxColumn - mle.MinColumn + 1) * (elementWidth + space) - 0.2 * space;
                        double h = (mle.MaxRow - mle.MinRow + 1) * (elementHeight + space) - 0.2 * space;

                        Trace.WriteLine(mle.Name + " raw    " +
                                        " X= " + String.Format("{0:0.00}", x) + " Y= " + String.Format("{0:0.00}", y) +
                                        " W= " + String.Format("{0:0.00}", w) + " H= " + String.Format("{0:0.00}", h));

                        if (titleSpaceCheckBox.Checked)
                        {
                            h += space * 1.5;
                            y += space * 0.75;
                        }

                        Trace.WriteLine(mle.Name + " spaced " +
                                        " X= " + String.Format("{0:0.00}", x) + " Y= " + String.Format("{0:0.00}", y) +
                                        " W= " + String.Format("{0:0.00}", w) + " H= " + String.Format("{0:0.00}", h));


                        mapShape.Cells["PinX"].ResultIU   = x;
                        mapShape.Cells["PinY"].ResultIU   = y;
                        mapShape.Cells["Width"].ResultIU  = w;
                        mapShape.Cells["Height"].ResultIU = h;
                    }
                }
            }

            Trace.Flush();
            Trace.Close();
        }
Esempio n. 4
0
        private void DecorGenerateClick(object sender, EventArgs e)
        {
            // open log if not yet open
            var tr2 = new TextWriterTraceListener(File.CreateText(selectedLogFileName.Text));

            Trace.Listeners.Add(tr2);

            // Excel related aspects

            var xlApp = new Microsoft.Office.Interop.Excel.Application {
                Visible = true
            };

            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

            // open the excel file
            String   excelFileName = decorExcelFileName.Text;
            Workbook xlWorkBook    = xlApp.Workbooks.Open(excelFileName,
                                                          _missing, _missing, _missing, _missing, _missing, _missing, _missing, _missing,
                                                          _missing, _missing, _missing, _missing, _missing, _missing);


            String decorationsTagName = decorElementCoilumnName.Text;

            // reference column number --> tag for the elements
            var decorationColumnMap = new Dictionary <int, string>();
            int decorationMaxColumn;
            int decorationTagColumn;

            MmExcelUtil.BuildColumnMap(xlWorkBook, decorSheetName.Text, decorationsTagName, decorationColumnMap, out decorationMaxColumn, out decorationTagColumn);

            // list of all the constituents of the map
            var decorationsList = new List <GenericElement>();

            MmExcelUtil.BuildList(xlWorkBook, decorSheetName.Text, decorationTagColumn, decorationMaxColumn, decorationColumnMap, decorationsList, decorElementCoilumnName.Text);


            // Visio related aspects

            var visapp = new Microsoft.Office.Interop.Visio.Application();

            // Prepare Visio
            Document landscape = visapp.Documents.Open(decorVisioFileName.Text);
            Document stencil   = visapp.Documents.OpenEx(decorStencilFileName.Text, (short)VisOpenSaveArgs.visOpenDocked);
            Page     page      = visapp.ActivePage;


            var    placedElementShapes = new Dictionary <String, ShapeRef>();
            String elementStencilName  = decorElementStencilName.Text;

            MmVisioUtil.GetShapeList(elementStencilName, page, decorationsTagName, placedElementShapes);

            int numUnplaceableDecorations = 0;
            int numPlaceableDecorations   = 0;

            Trace.Flush();

            Master decorationStencil = stencil.Masters[decorStencilName.Text];

            foreach (GenericElement genericElement in decorationsList)
            {
                String key = genericElement.Name;

                if (placedElementShapes.ContainsKey(key))
                {
                    Trace.WriteLine("  Decorating " + key);

                    MmVisioUtil.PlaceDecorationElement(page, placedElementShapes[key].Shape, decorationStencil, genericElement,
                                                       decorationColumnMap, false);

                    numPlaceableDecorations++;
                }
                else
                {
                    numUnplaceableDecorations++;
                }
            }
            Trace.WriteLine("  Decoration statistics " + numPlaceableDecorations + " placed and " + numUnplaceableDecorations + " ignored");



            Trace.Flush();
            Trace.Close();
        }