Esempio n. 1
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. 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();
        }