Пример #1
0
        private void addRectangle(object sender, EventArgs e)
        {
            var element = new LayoutRectangle
            {
                Size        = new SizeF(200, 100),
                IsFixedSize = true
            };

            LayoutControl.AddToLayout(element);
        }
Пример #2
0
        /// <summary>
        /// Loads the selected layoutfile
        /// </summary>
        /// <param name="fileName">The layout file to load</param>
        /// <param name="loadPaperSettings">Loads the paper settings (size, margins, orientation) from the layout</param>
        /// <param name="promptPaperMismatch">Warn the user if the paper size stored in the file doesn't exist in current printer and ask them if they want to load it anyways</param>
        public void LoadLayout(string fileName, bool loadPaperSettings, bool promptPaperMismatch)
        {
            //Open the model xml document
            XmlDocument layoutXmlDoc = new XmlDocument();
            layoutXmlDoc.Load(fileName);
            XmlElement root = layoutXmlDoc.DocumentElement;

            XmlDeserializer backDeserializer = new XmlDeserializer();

            //Temporarily stores all the elements and settings until the whole XML file is parsed
            List<LayoutElement> loadList = new List<LayoutElement>();
            bool paperSizeSupported = false;
            PaperSize savedPaperSize = null;
            bool savedLandscape = true;
            Margins savedMargins = null;

            //Makes sure we are really loading a DotSpatial layout file
            if (root != null && root.Name == "DotSpatialLayout")
            {
                //This creates instances of all the elements
                XmlNode child = root.LastChild;
                while (child != null)
                {
                    if (child.Name == "Element")
                    {
                        LayoutElement newLe = null;
                        switch (child.ChildNodes[0].Name)
                        {
                            case "Bitmap":
                                newLe = new LayoutBitmap();
                                break;
                            case "Legend":
                                newLe = CreateLegendElement();
                                break;
                            case "Map":
                                newLe = CreateMapElement();
                                break;
                            case "NorthArrow":
                                newLe = new LayoutNorthArrow();
                                break;
                            case "Rectangle":
                                newLe = new LayoutRectangle();
                                break;
                            case "ScaleBar":
                                newLe = CreateScaleBarElement();
                                break;
                            case "Text":
                                newLe = new LayoutText();
                                break;
                        }
                        if (newLe != null)
                        {
                            newLe.Name = child.Attributes["Name"].Value;
                            newLe.Invalidated += LeInvalidated;
                            newLe.Rectangle = new RectangleF(float.Parse(child.Attributes["RectangleX"].Value, CultureInfo.InvariantCulture), float.Parse(child.Attributes["RectangleY"].Value, CultureInfo.InvariantCulture), float.Parse(child.Attributes["RectangleWidth"].Value, CultureInfo.InvariantCulture), float.Parse(child.Attributes["RectangleHeight"].Value, CultureInfo.InvariantCulture));
                            newLe.ResizeStyle = (ResizeStyle)Enum.Parse(typeof(ResizeStyle), child.Attributes["ResizeStyle"].Value);
                            if (child.Attributes["Background"] != null)
                                newLe.Background = backDeserializer.Deserialize<PolygonSymbolizer>(child.Attributes["Background"].Value);
                            loadList.Insert(0, newLe);
                        }
                    }
                    else if (child.Name == "Paper" && loadPaperSettings)
                    {
                        //Loads printer paper size
                        //gets the name of the paper size
                        string paperName = child.Attributes["Name"].Value;

                        //Find out if it supports the paper size in the layout file
                        PrinterSettings.PaperSizeCollection availableSizes = _printerSettings.PaperSizes;
                        foreach (PaperSize testSize in availableSizes)
                        {
                            if (testSize.PaperName == paperName)
                            {
                                savedPaperSize = testSize;
                                paperSizeSupported = true;
                                break;
                            }
                        }

                        //If needed prompt the user due to a paper size mismatch
                        if (paperSizeSupported == false && promptPaperMismatch)
                        {
                            if (MessageBox.Show(this, "The currently selected printer \"" + _printerSettings.PrinterName + "\"\ndoes not support the paper size \"" + paperName + "\" used by the layout being loaded.\n\nLoad the layout with the printer's current paper settings?", "Paper size mismatch", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                                return;
                        }
                        else
                        {
                            savedLandscape = (bool)TypeDescriptor.GetConverter(typeof(bool)).ConvertFromInvariantString(child.Attributes["Landscape"].Value);
                            savedMargins = (Margins)TypeDescriptor.GetConverter(typeof(Margins)).ConvertFromInvariantString(child.Attributes["Margins"].Value);
                        }
                    }
                    child = child.PreviousSibling;
                }

                //Since some of the elements may be dependant on elements already being added we add their other properties after we add them all
                child = root.LastChild;
                for (int i = loadList.Count - 1; i >= 0; i--)
                {
                    if (child != null)
                    {
                        XmlNode innerChild = child.ChildNodes[0];
                        if (loadList[i] is LayoutBitmap)
                        {
                            LayoutBitmap lb = loadList[i] as LayoutBitmap;
                            if (lb != null)
                            {
                                lb.Filename = innerChild.Attributes["Filename"].Value;
                                lb.PreserveAspectRatio = Convert.ToBoolean(innerChild.Attributes["PreserveAspectRatio"].Value);
                                lb.Draft = Convert.ToBoolean(innerChild.Attributes["Draft"].Value);
                                if (innerChild.Attributes["Brightness"] != null)
                                    lb.Brightness = (int)TypeDescriptor.GetConverter(typeof(int)).ConvertFromInvariantString(innerChild.Attributes["Brightness"].Value);
                                if (innerChild.Attributes["Contrast"] != null)
                                    lb.Contrast = (int)TypeDescriptor.GetConverter(typeof(int)).ConvertFromInvariantString(innerChild.Attributes["Contrast"].Value);
                            }
                        }
                        else if (loadList[i] is LayoutLegend)
                        {
                            LayoutLegend ll = loadList[i] as LayoutLegend;
                            if (ll != null)
                            {
                                ll.LayoutControl = this;
                                ll.TextHint = (TextRenderingHint)Enum.Parse(typeof(TextRenderingHint), innerChild.Attributes["TextHint"].Value);
                                ll.Color = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromInvariantString(innerChild.Attributes["Color"].Value);
                                ll.Font = (Font)TypeDescriptor.GetConverter(typeof(Font)).ConvertFromInvariantString(innerChild.Attributes["Font"].Value);
                            }
                            int mapIndex = Convert.ToInt32(innerChild.Attributes["Map"].Value);
                            if (mapIndex >= 0)
                                if (ll != null) ll.Map = loadList[mapIndex] as LayoutMap;
                            string layStr = innerChild.Attributes["Layers"].Value;
                            List<int> layers = new List<int>();
                            while (layStr.EndsWith("|"))
                            {
                                layStr = layStr.TrimEnd("|".ToCharArray());
                                layers.Add((int)TypeDescriptor.GetConverter(typeof(int)).ConvertFromInvariantString(layStr.Substring(layStr.LastIndexOf("|") + 1)));
                                layStr = layStr.Substring(0, layStr.LastIndexOf("|") + 1);
                            }
                            if (ll != null)
                            {
                                ll.NumColumns = (int)TypeDescriptor.GetConverter(typeof(int)).ConvertFromInvariantString(innerChild.Attributes["NumColumns"].Value);
                                ll.Layers = layers;
                            }
                        }
                        else if (loadList[i] is LayoutMap)
                        {
                            LayoutMap lm = loadList[i] as LayoutMap;
                            Envelope env = new Envelope();
                            env.Minimum.X = (double)TypeDescriptor.GetConverter(typeof(double)).ConvertFromInvariantString(innerChild.Attributes["EnvelopeXmin"].Value);
                            env.Minimum.Y = (double)TypeDescriptor.GetConverter(typeof(double)).ConvertFromInvariantString(innerChild.Attributes["EnvelopeYmin"].Value);
                            env.Maximum.X = (double)TypeDescriptor.GetConverter(typeof(double)).ConvertFromInvariantString(innerChild.Attributes["EnvelopeXmax"].Value);
                            env.Maximum.Y = (double)TypeDescriptor.GetConverter(typeof(double)).ConvertFromInvariantString(innerChild.Attributes["EnvelopeYmax"].Value);
                            if (lm != null) lm.Envelope = env;
                        }
                        else if (loadList[i] is LayoutNorthArrow)
                        {
                            LayoutNorthArrow na = loadList[i] as LayoutNorthArrow;
                            if (na != null)
                            {
                                na.Color = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromInvariantString(innerChild.Attributes["Color"].Value);
                                na.NorthArrowStyle = (NorthArrowStyle)Enum.Parse(typeof(NorthArrowStyle), innerChild.Attributes["Style"].Value);
                                if (innerChild.Attributes["Rotation"] != null)
                                    na.Rotation = (float)TypeDescriptor.GetConverter(typeof(float)).ConvertFromInvariantString(innerChild.Attributes["Rotation"].Value);
                            }
                        }
                        else if (loadList[i] is LayoutRectangle)
                        {
                            LayoutRectangle lr = loadList[i] as LayoutRectangle;
                            if (lr != null)
                            {
                                //This code is to load legacy layouts that had properties for the color/outline of rectangles
                                if (innerChild.Attributes["Color"] != null && innerChild.Attributes["BackColor"] != null && innerChild.Attributes["OutlineWidth"] != null)
                                {
                                    Color tempOutlineColor = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromInvariantString(innerChild.Attributes["Color"].Value);
                                    Color tempBackColor = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromInvariantString(innerChild.Attributes["BackColor"].Value);
                                    int tempOutlineWidth = (int)TypeDescriptor.GetConverter(typeof(int)).ConvertFromInvariantString(innerChild.Attributes["OutlineWidth"].Value);
                                    lr.Background = new PolygonSymbolizer(tempBackColor, tempOutlineColor, tempOutlineWidth);
                                }
                            }
                        }
                        else if (loadList[i] is LayoutScaleBar)
                        {
                            LayoutScaleBar lsc = loadList[i] as LayoutScaleBar;
                            if (lsc != null)
                            {
                                lsc.LayoutControl = this;
                                lsc.TextHint = (TextRenderingHint)Enum.Parse(typeof(TextRenderingHint), innerChild.Attributes["TextHint"].Value);
                                lsc.Color = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromInvariantString(innerChild.Attributes["Color"].Value);
                                lsc.Font = (Font)TypeDescriptor.GetConverter(typeof(Font)).ConvertFromInvariantString(innerChild.Attributes["Font"].Value);
                                lsc.BreakBeforeZero = Convert.ToBoolean(innerChild.Attributes["BreakBeforeZero"].Value);
                                lsc.NumberOfBreaks = (int)TypeDescriptor.GetConverter(typeof(int)).ConvertFromInvariantString(innerChild.Attributes["NumberOfBreaks"].Value);
                                lsc.Unit = (ScaleBarUnit)Enum.Parse(typeof(ScaleBarUnit), innerChild.Attributes["Unit"].Value);
                                lsc.UnitText = innerChild.Attributes["UnitText"].Value;
                            }
                            int mapIndex = Convert.ToInt32(innerChild.Attributes["Map"].Value);
                            if (mapIndex >= 0)
                                if (lsc != null) lsc.Map = loadList[mapIndex] as LayoutMap;
                        }
                        else if (loadList[i] is LayoutText)
                        {
                            LayoutText lt = loadList[i] as LayoutText;
                            if (lt != null)
                            {
                                lt.TextHint = (TextRenderingHint)Enum.Parse(typeof(TextRenderingHint), innerChild.Attributes["TextHint"].Value);
                                lt.Color = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromInvariantString(innerChild.Attributes["Color"].Value);
                                lt.Font = (Font)TypeDescriptor.GetConverter(typeof(Font)).ConvertFromInvariantString(innerChild.Attributes["Font"].Value);
                                lt.ContentAlignment = (ContentAlignment)TypeDescriptor.GetConverter(typeof(ContentAlignment)).ConvertFromString(innerChild.Attributes["ContentAlignment"].Value);
                                lt.Text = innerChild.Attributes["Text"].Value;
                            }
                        }
                    }
                    if (child != null) child = child.PreviousSibling;
                }
                _layoutElements.Clear();
                _selectedLayoutElements.Clear();
                _layoutElements.InsertRange(0, loadList);
                //Loads the papersize if supported and needed
                if (paperSizeSupported)
                {
                    _printerSettings.DefaultPageSettings.PaperSize = savedPaperSize;
                    _printerSettings.DefaultPageSettings.Landscape = savedLandscape;
                    _printerSettings.DefaultPageSettings.Margins = savedMargins;
                }
                Filename = fileName;
                Invalidate();
                OnElementsChanged(null);
            }
        }