Exemplo n.º 1
0
        public void saveAsMWD(string openedFileName)
        {
            List<int> saveErr = new List<int>();
            saveErr.Clear();

            string schemaFile = config.ApplicationDirectory + "\\Misc\\schema.mwd";
            //if there are no Layer in layerArray, then do not save anything
            if (LayerCount == 0) { MessageBox.Show("im zu speichernden Projekt sind keine Layer vorhanden. Speichervorgang wird abgebrochen"); }
            else
            {
                if (File.Exists(schemaFile))
                {
                    StringWriter tempStream = null;
                    XmlTextWriter xwriter = null;
                    StreamReader s = null;
                    StreamWriter myFile = null;

                    try
                    {
                        tempStream = new StringWriter();

                        xwriter = new XmlTextWriter(tempStream);
                        xwriter.Formatting = Formatting.Indented;
                        xwriter.Indentation = 4;
                        xwriter.WriteStartElement("rs:data");
                        xwriter.WriteStartElement("rs:insert");

                        // Write current viewport corners into mwd to save zoom and position
                        Rectangle viewport = mainControler.MapPanel.Viewport;
                        int min = viewport.Top - viewport.Height;

                        xwriter.WriteStartElement("z:row");
                        xwriter.WriteAttributeString("Xmin", viewport.Left.ToString());
                        xwriter.WriteAttributeString("Ymin", min.ToString());
                        xwriter.WriteAttributeString("Xmax", viewport.Right.ToString());
                        xwriter.WriteAttributeString("Ymax", viewport.Top.ToString());
                        xwriter.WriteEndElement();

                        // Write ImageFiles

                        // Write out the layer list
                        for (int i = 0; i < LayerCount; i++)
                        {
                            ILayer layer = getLayerFromMapping(i);

                            if (layer != null)
                                switch (layer.Type)
                                {
                                    case LayerType.Shape:
                                        ShapeObject shp = layer as ShapeObject;
                                        xwriter.WriteStartElement("z:row");
                                        xwriter.WriteAttributeString("LayerName", shp.LayerInfo.FileName);
                                        xwriter.WriteAttributeString("LayerPfad", shp.LayerInfo.FilePath);
                                        xwriter.WriteAttributeString("LayerBeschriftung", shp.LayerName);
                                        xwriter.WriteAttributeString("Key", shp.LayerInfo.FilePath);
                                        xwriter.WriteAttributeString("Visible", shp.Visible.ToString());
                                        xwriter.WriteAttributeString("LayerType", "Shapefile"); // if we wanted to save also image layers - we have to use the LayerType property
                                        xwriter.WriteAttributeString("Fuellfarbe", (shp.VectorInfo.FillColor.ToArgb()).ToString());
                                        xwriter.WriteAttributeString("Linienfarbe", (shp.VectorInfo.LineColor.ToArgb()).ToString());
                                        xwriter.WriteAttributeString("Punktfarbe", (shp.VectorInfo.PointColor.ToArgb()).ToString());
                                        xwriter.WriteAttributeString("Stuetzpunkte", shp.BasePoints.ToString());
                                        xwriter.WriteAttributeString("Layergefuellt", shp.VectorInfo.Fill ? "True" : "False");
                                        xwriter.WriteAttributeString("Fuellmodus", "0");
                                        xwriter.WriteAttributeString("Linientyp", "0");
                                        xwriter.WriteAttributeString("Linienstaerke", shp.VectorInfo.LayerPen.Width.ToString());
                                        xwriter.WriteAttributeString("Punktgroesse", shp.VectorInfo.LayerPen.Width.ToString());
                                        xwriter.WriteAttributeString("Punkttyp", "0");
                                        xwriter.WriteAttributeString("Transparenz", "100");
                                        xwriter.WriteEndElement();
                                        break;
                                    case LayerType.Image:
                                        ImageLayer img = layer as ImageLayer;
                                        xwriter.WriteStartElement("z:row");
                                        xwriter.WriteAttributeString("LayerName", img.FileName);
                                        xwriter.WriteAttributeString("LayerPfad", img.FilePath);
                                        xwriter.WriteAttributeString("LayerBeschriftung", img.Description);
                                        xwriter.WriteAttributeString("Key", img.FilePath);

                                        xwriter.WriteAttributeString("Visible", img.Visible.ToString());
                                        xwriter.WriteAttributeString("LayerType", "Image");
                                        xwriter.WriteAttributeString("Transparenz", "0");
                                        xwriter.WriteEndElement();
                                        break;
                                    default:
                                        break;
                                }
                            else  saveErr.Add(getLayerIndexFromMapping(i));
                        }
                        xwriter.WriteEndElement();
                        xwriter.WriteEndElement();

                        s = new StreamReader(schemaFile);
                        myFile = new StreamWriter(openedFileName, false, Encoding.Unicode);

                        myFile.Write(s.ReadToEnd());
                        myFile.Write(tempStream.ToString());

                        myFile.Write("</xml>");
                        if (saveErr.Count > 0){
                            ErrorDialog errDialog = new ErrorDialog();
                            string err = "";
                            string n = ""; //grammatik von konnten
                            for (int j = 0; j < saveErr.Count - 2; j++)
                                err += saveErr[j] +1 + ", ";
                            if (saveErr.Count > 1)
                            {
                                n = "n";
                                err += saveErr[saveErr.Count - 2] +1 + " und ";
                            }
                            err += saveErr[saveErr.Count - 1]+1;
                                errDialog.ShowDialog("Fehler 0x3990: Layer " + err + " konnte" + n +" nicht gespeichert werden.");
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(
                            String.Format("Fehler 0x3991: Beim Speichern des Projekts ist ein Fehler aufgetreten:{0}{1}",
                            Environment.NewLine, ex.Message));
                    }
                    finally
                    {
                        if (xwriter != null) xwriter.Close();
                        if (myFile != null) myFile.Close();
                        if (s != null) s.Close();
                        if (tempStream != null) tempStream.Close();
                    }
                }
                else
                {
                    MessageBox.Show("Fehler 0x9012 - Es wurde kein XML-Schema gefunden.\nSchreiben der Datei abgebrochen!");
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName">gets openFileDialog.FileName as input, its path + fileName of an mwd File</param>
        public void importMWD(string fileName)
        {
            string[] pathnamearray = fileName.Split('\\');
            string pathName = ""; //its the path of the file + "\\"
            for (int i = 0; i <= pathnamearray.Length - 2; i++) pathName += pathnamearray[i] + '\\';

            bool pathFound = false;
            LayerInfo layerInfo = null;
            VectorInfo vectorInfo = null;
            bool isVisible = false;

            LayerType tempType = LayerType.Undefined;
            string tempName = "";
            string file = "";
            double[] Min = { 0, 0 };
            double[] Max = { 0, 0 };
            //check indicates wether all four bbox values are found
            int check = 0;
            bool oneLayerFound = false;
            string errFileNotFound = "";
            System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(fileName); ;

            while (reader.Read())
            {
                if (reader.NodeType == System.Xml.XmlNodeType.Element)
                    if (reader.Name == "z:row")
                    {
                            while (reader.MoveToNextAttribute())
                            {
                                #region im while

                                if (reader.Name == "Xmin") { Min[0] = Convert.ToDouble(reader.Value); check++; }
                                if (reader.Name == "Xmax") { Max[0] = Convert.ToDouble(reader.Value); check++; }
                                if (reader.Name == "Ymin") { Min[1] = Convert.ToDouble(reader.Value); check++; }
                                if (reader.Name == "Ymax") { Max[1] = Convert.ToDouble(reader.Value); check++; }

                                if (reader.Name == "LayerPfad") //LayerPfad muss vor anderen Attributen stehen
                                {
                                    file = reader.Value;
                                    if (File.Exists(file)) pathFound = true;
                                    else if (File.Exists(pathName + Path.GetFileName(file)))
                                    {
                                        file = pathName + Path.GetFileName(file);
                                        pathFound = true;
                                    }
                                    else
                                    {
                                        for (int i = 0; i < config.SearchDirList.Count; i++)
                                        {
                                            if (File.Exists(config.SearchDirList[i] + "\\" + Path.GetFileName(file)))
                                            {
                                                file = config.SearchDirList[i] + "\\" + Path.GetFileName(file);

                                                pathFound = true;
                                                break;
                                            }
                                        }

                                        if (!pathFound) //one of the layer in mwd file could not be found
                                        {
                                            errFileNotFound += file + "\r\n";
                                            //should look after other layerFiles
                                            break;
                                        }
                                    }
                                    if (pathFound)
                                    {
                                        oneLayerFound = true;
                                        layerInfo = new LayerInfo();
                                        vectorInfo = new VectorInfo();
                                        layerInfo.FilePath = file;
                                    }
                                }
                                if (pathFound)
                                {
                                    try
                                    {
                                        if (reader.Name == "Punktfarbe") vectorInfo.PointColor = Color.FromArgb(Int32.Parse(reader.Value));
                                        else if (reader.Name == "Linienfarbe") vectorInfo.LineColor = Color.FromArgb(Convert.ToInt32(reader.Value));
                                        else if (reader.Name == "Fuellfarbe") vectorInfo.FillColor = Color.FromArgb(Int32.Parse(reader.Value));
                                        else if (reader.Name == "Layergefuellt") vectorInfo.Fill = (reader.Value == "True");
                                        else if (reader.Name == "LayerBeschriftung") tempName = reader.Value;
                                        else if (reader.Name == "Visible") vectorInfo.IsVisible = Boolean.Parse(reader.Value);
                                        else if (reader.Name == "Linienstaerke") vectorInfo.LayerPen.Width = float.Parse(reader.Value);
                                        else if (reader.Name == "Punktgroesse") vectorInfo.LayerPen.Width = float.Parse(reader.Value);
                                        else if (reader.Name.ToLower() == "layertype")
                                                {
                                                    if (reader.Value.ToLower() == "image") tempType = LayerType.Image;
                                                    if (reader.Value.ToLower() == "shapefile") tempType = LayerType.Shape;
                                                }
                                    }
                                    catch (Exception)
                                    {
                                        MessageBox.Show("Fehler 0x6221: Die Projektdatei ist fehlerhaft - Layereinstellungen wird ignoriert\nGelesener Wert: "
                                            + reader.Value + "\nDatei: " + layerInfo.FilePath);
                                    }
                                }
                                #endregion
                            }
                        if(pathFound)
                            switch (tempType)
                            {
                                case LayerType.Shape:
                                    addShapefileLayer(file, layerInfo, vectorInfo);
                                    break;
                                case LayerType.Image:
                                    addImageLayer(file,layerInfo);
                                    break;

                                default:
                                    break;
                            }
                        pathFound = false;
                        tempType = LayerType.Undefined;
                        tempName = "";
                        isVisible = false;
                        layerInfo = null;
                        vectorInfo = null;
                    }
            }
            if ((check == 4) && (oneLayerFound)) // all bounding values are found + onelayer -> draw details of map
            {
                // scale und firstscale sind schon gesetzt durch das Laden der Layer

                MapPanel mp = mainControler.MapPanel;
                double zoom = mainControler.MapPanel.calculateZoomFactor(
                        (Max[0] - Min[0]) * scale, (Max[1] - Min[1]) * scale);

                double newAbsoluteZoom = mp.AbsolutZoom * zoom;

                PointD newD = new PointD(
                        Min[0] * firstScale * newAbsoluteZoom,
                        Max[1] * firstScale * newAbsoluteZoom);

                mainControler.PerformAction(
                            new ZoomAction(
                                mp.AbsolutZoom,
                                mainControler.MapPanel.D,
                                newD,
                                newAbsoluteZoom,
                                new PointD(Max[0], -Min[1]),
                                getMaxAbsoluteZoom(),
                                mainControler));
            }
            else {
                if(!oneLayerFound) MessageBox.Show("Die Standardprojektdateien konnten nicht gefunden werden.");
                //wenn die vier werte nicht gefunden werden, bleiben die Ansicht bei der BBox des 1. Layers
            }
            reader.Close();
            //ensure that not two errorMessages appear if no layer was found
            if (!errFileNotFound.Equals("") && oneLayerFound){
                ErrorDialog fileNotFoundDialog = new ErrorDialog();
                fileNotFoundDialog.ShowDialog("Fehler 0x0500 - Folgende Dateien konnten nicht gefunden werden:\r\n" + errFileNotFound + "Projekt wurde ohne diese Dateien geladen.");
            }
            mainControler.MapPanel.ScreenChanged = true;
            mainControler.MapPanel.Invalidate();
            mainControler.MapPanel.Update();
        }