Пример #1
0
        private void MapItButtonClick(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 --> tag for the elements
            var elementColumnMap = new Dictionary <int, string>();
            int elementMaxColumnNr;
            int elementNameColumnNr;

            MmExcelUtil.BuildColumnMap(xlWorkBook, elementSheetName.Text, elementNameColumnName.Text, elementColumnMap, out elementMaxColumnNr, out elementNameColumnNr);


            // reference column number --> tag for the map
            var taxXColumnMap = new Dictionary <int, string>();
            int taxXMaxColumnNr;
            int taxXNameColumnNr;

            MmExcelUtil.BuildColumnMap(xlWorkBook, xTaxonomySheetName.Text, xTaxonomyNameColumnName.Text, taxXColumnMap, out taxXMaxColumnNr, out taxXNameColumnNr);

            // reference column number --> tag for the map
            var taxYColumnMap = new Dictionary <int, string>();
            int taxYMaxColumnNr;
            int taxYNameColumnNr;

            MmExcelUtil.BuildColumnMap(xlWorkBook, yTaxonomySheetName.Text, yTaxonomyNameColumnName.Text, taxYColumnMap, out taxYMaxColumnNr, out taxYNameColumnNr);

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

            MmExcelUtil.BuildList(xlWorkBook, elementSheetName.Text, elementNameColumnNr, elementMaxColumnNr, elementColumnMap, elementList, elementNameColumnName.Text);

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

            MmExcelUtil.BuildList(xlWorkBook, xTaxonomySheetName.Text, taxXNameColumnNr, taxXMaxColumnNr, taxXColumnMap, xTaxonomyList, xTaxonomyNameColumnName.Text);

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

            MmExcelUtil.BuildList(xlWorkBook, yTaxonomySheetName.Text, taxYNameColumnNr, taxYMaxColumnNr, taxYColumnMap, yTaxonomyList, yTaxonomyNameColumnName.Text);


            // xTaxonomyMapping
            var xTaxonomyMappingList = new List <Mapping>();

            MmExcelUtil.BuildMapping(xlWorkBook, xTaxonomyMappingList, xMapSheetName.Text, xMapElementNameColumnName.Text,
                                     xMapXTaxNameColumnName.Text);

            var yTaxonomyMappingList = new List <Mapping>();

            MmExcelUtil.BuildMapping(xlWorkBook, yTaxonomyMappingList, yMapSheetName.Text, yMapElementNameColumnName.Text,
                                     yMapYTaxNameColumnName.Text);

            // create the map

            int x      = 0;
            int y      = 0;
            var matrix = new List <String> [xTaxonomyList.Count, yTaxonomyList.Count];

            for (int xi = 0; xi < xTaxonomyList.Count(); xi++)
            {
                for (int yi = 0; yi < yTaxonomyList.Count(); yi++)
                {
                    matrix[xi, yi] = new List <String>();
                }
            }


            foreach (GenericElement yElement in yTaxonomyList)
            {
                x = 0;
                foreach (GenericElement xElement in xTaxonomyList)
                {
                    Trace.WriteLine("  checking for x " + xElement.Name + " and y " + yElement.Name);


                    foreach (GenericElement element in elementList)
                    {
                        // if there is a mapping element to x and y taxonomy then add the element to the matrix

                        Mapping yMap = yTaxonomyMappingList.Find(
                            delegate(Mapping me)
                        {
                            return
                            (me.ElementName == element.Name &&
                             me.MapName == yElement.Name);
                        }
                            );
                        Mapping xMap = xTaxonomyMappingList.Find(
                            delegate(Mapping me)
                        {
                            return
                            (me.ElementName == element.Name &&
                             me.MapName == xElement.Name);
                        }
                            );

                        if (xMap != null && yMap != null)
                        {
                            matrix[x, y].Add(element.Name);
                        }
                    }
                    x += 1;
                }
                y += 1;
            }

            // determine per row the number of different applications and the max number in a cell

            int[] maxNumApplications      = new int [yTaxonomyList.Count];
            int   totalMaxNumApplications = 0;

            int[] diffNumApplications      = new int[yTaxonomyList.Count];
            int   totalDiffNumApplications = 0;
            int   emptyRows = 0;

            for (int yi = 0; yi < yTaxonomyList.Count(); yi++)
            {
                maxNumApplications[yi] = 0;
                List <String> appNames = new List <string>();
                for (int xi = 0; xi < xTaxonomyList.Count(); xi++)
                {
                    maxNumApplications[yi] = Math.Max(maxNumApplications[yi], matrix[xi, yi].Count);
                    foreach (String s in matrix[xi, yi])
                    {
                        if (!appNames.Contains(s))
                        {
                            appNames.Add(s);
                        }
                    }
                }
                diffNumApplications[yi]   = appNames.Count;
                totalDiffNumApplications += diffNumApplications[yi];
                totalMaxNumApplications  += maxNumApplications[yi];
                if (maxNumApplications[yi] == 0)
                {
                    emptyRows++;
                }

                Trace.WriteLine(" Row " + yi + " max num application " + maxNumApplications[yi] + " diff num applications " + diffNumApplications[yi]);
            }

            Trace.WriteLine("Max total number applications " + totalMaxNumApplications);
            Trace.WriteLine("Total number of different applications " + totalDiffNumApplications);
            Trace.WriteLine("Number of empty rows " + emptyRows);

            // 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];
            Master taxonomyXStencil = stencil.Masters[xTaxonomyStencilName.Text];
            Master taxomomyYStencil = stencil.Masters[yTaxonomyStencilName.Text];

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

            Shape elementShape   = page.Drop(elementStencil, 0.0, 0.0);
            Shape taxonomyXShape = page.Drop(taxonomyXStencil, 0.0, 0.0);
            Shape taxonomyYShape = page.Drop(taxomomyYStencil, 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 taxonomyXHeight = taxonomyXShape.CellsU["Height"].ResultIU;
            double taxonomyXWidth  = taxonomyXShape.CellsU["Width"].ResultIU;
            double taxonomyYHeight = taxonomyYShape.CellsU["Height"].ResultIU;
            double taxonomyYWidth  = taxonomyYShape.CellsU["Width"].ResultIU;


            double space = double.Parse(spaceText.Text);

            elementShape.Delete();
            taxonomyXShape.Delete();
            taxonomyYShape.Delete();

            Trace.WriteLine("Size of an page    " + pageWidth + " " + pageHight);
            Trace.WriteLine("Size of an element " + elementWidth + " " + elementHeight);

            double yTaxonomyYRoot = pageHight - 2 * space - taxonomyXHeight;
            double xTaxonomyXRoot = 2 * space + taxonomyYWidth;

            double taxYHeight = 0.0;

            if (emptyRowSuppressionCheckBox.Checked)
            {
                taxYHeight = (yTaxonomyYRoot - 2 * space) / totalDiffNumApplications;
            }
            else
            {
                taxYHeight = (yTaxonomyYRoot - 2 * space) / (totalDiffNumApplications + emptyRows);
            }
            double taxXWidth = (pageWidth - xTaxonomyXRoot - 2 * space) / xTaxonomyList.Count;


            double yPos = yTaxonomyYRoot;


            for (int xi = 0; xi < xTaxonomyList.Count; xi++)
            {
                MmVisioUtil.PlaceGenericElement(page, taxonomyXStencil, xTaxonomyList[xi],
                                                xTaxonomyXRoot + (xi + 0.5) * taxXWidth,
                                                yTaxonomyYRoot + taxonomyXHeight * 0.5,
                                                taxXWidth,
                                                taxonomyXHeight,
                                                taxXColumnMap,
                                                false);
            }



            for (int yi = 0; yi < yTaxonomyList.Count(); yi++)
            {
                double xPos = space * 2.0 + taxonomyXWidth / 2.0;

                if (emptyRowSuppressionCheckBox.Checked && diffNumApplications[yi] == 0)
                {
                    // do noting
                }
                else
                {
                    double height = Math.Max(1, diffNumApplications[yi]) * taxYHeight;

                    MmVisioUtil.PlaceGenericElement(page, taxomomyYStencil, yTaxonomyList[yi],
                                                    xPos, yPos - height / 2,
                                                    taxonomyYWidth, height,
                                                    taxYColumnMap,
                                                    false);

                    // draw applications

                    List <String> appNames = new List <string>();

                    for (int xi = 0; xi < xTaxonomyList.Count; xi++)
                    {
                        foreach (String s in matrix[xi, yi])
                        {
                            if (!appNames.Contains(s))
                            {
                                appNames.Add(s);
                            }
                        }
                    }

                    int nApp = 0;
                    foreach (String Name in appNames)
                    {
                        GenericElement app = elementList.Find(
                            delegate(GenericElement ele)
                        {
                            return(ele.Name == Name);
                        }
                            );

                        if (app != null)
                        {
                            elementShape = null;

                            for (int xi = 0; xi < xTaxonomyList.Count; xi++)
                            {
                                if (matrix[xi, yi].Contains(Name))
                                {
                                    if (mergeApplicationsCheckBox.Checked && elementShape != null)
                                    {
                                        elementShape.CellsU["Width"].ResultIU += taxXWidth;
                                        elementShape.CellsU["PinX"].ResultIU  += taxXWidth / 2;
                                    }
                                    else
                                    {
                                        if (applySpaceCheckBox.Checked)
                                        {
                                            elementShape = MmVisioUtil.PlaceGenericElement(page, elementStencil, app,
                                                                                           xTaxonomyXRoot +
                                                                                           (xi + 0.5) * (taxXWidth),
                                                                                           yPos -
                                                                                           (nApp + 0.5) * taxYHeight,
                                                                                           taxXWidth - space, taxYHeight,
                                                                                           elementColumnMap,
                                                                                           false);
                                        }
                                        else
                                        {
                                            elementShape = MmVisioUtil.PlaceGenericElement(page, elementStencil, app,
                                                                                           xTaxonomyXRoot +
                                                                                           (xi + 0.5) * taxXWidth,
                                                                                           yPos -
                                                                                           (nApp + 0.5) * taxYHeight,
                                                                                           taxXWidth, taxYHeight,
                                                                                           elementColumnMap,
                                                                                           false);
                                        }
                                    }
                                }
                                else
                                {
                                    elementShape = null;
                                }
                            }
                            nApp++;
                        }
                        else
                        {
                            String error = "### unknown element " + elementShape.Name;
                            Trace.WriteLine(error);
                            errorListBox.Items.Add(error);
                        }
                    }


                    yPos -= height;
                }
            }


            Trace.Flush();
            Trace.Close();
        }
Пример #2
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();
        }
Пример #3
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();
        }
Пример #4
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();
        }
Пример #5
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();
        }