AE二次開發之新建、打開、保存、另存爲地圖文檔

最近正好在做AE的項目的開發,做這些基本功能的時候發現網上的代碼只有簡單的實現過程,並沒有一個完整的邏輯。所以自己的重新寫了這個邏輯並將邏輯過程與實現代碼貼出來,以供有需要的小夥伴參考。

以上是整個地圖文檔操作的代碼的邏輯過程 (主要是個if判斷的過程,如果是打開的已有的mxd,就保存到該工作空間;如果是加載的shp文件,就存爲新的mxd。)

新建地圖文檔

public void NewMxd(ESRI.ArcGIS.Controls.AxMapControl basicControl)
        {
            try
            {

                if (basicControl.LayerCount != 0)
                {
                    MessageBox.Show("是否保存當前操作?");
                    string sMxdFileName = basicControl.DocumentFilename;
                    IMapDocument pMapDocument = new MapDocumentClass();
                    if (sMxdFileName != null && basicControl.CheckMxFile(sMxdFileName))
                    {
                        if (pMapDocument.get_IsReadOnly(sMxdFileName))
                        {
                            MessageBox.Show("本地圖文檔是隻讀的,不能保存!");
                            pMapDocument.Close();
                            return;
                        }
                    }
                    else
                    {
                        SaveFileDialog pSavedlg = new SaveFileDialog();
                        pSavedlg.Title = "請選擇保存路徑";
                        pSavedlg.OverwritePrompt = true;
                        pSavedlg.Filter = "ArcMap文檔(*.mxd)|*.mxd|ArcMap模板(*.mxt)|*.mxt";
                        pSavedlg.RestoreDirectory = true;
                        if (pSavedlg.ShowDialog() == DialogResult.OK)
                        {
                            sMxdFileName = pSavedlg.FileName;
                        }
                        else
                        {
                            return;
                        }
                    }
                    pMapDocument.New(sMxdFileName);
                    pMapDocument.ReplaceContents(basicControl.Map as IMxdContents);
                    pMapDocument.Save(pMapDocument.UsesRelativePaths, true);
                    pMapDocument.Close();
                    MessageBox.Show("保存地圖文檔成功!");

                }

                basicControl.ClearLayers();
              
                basicControl.Update();
                basicControl.ActiveView.Refresh();
                MessageBox.Show("新建地圖文檔成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show("新建地圖文檔失敗" + ex.Message);
            }
        }

打開地圖文檔

public void OpenMxd(ESRI.ArcGIS.Controls.AxMapControl basicControl )
        {
            try
            {
                if (basicControl.LayerCount != 0)
                {
                    MessageBox.Show("是否保存當前操作?");
                    string sMxdFileName = basicControl.DocumentFilename;
                    //IMapDocument pMapDocument = new MapDocumentClass();
                    if (sMxdFileName != null && basicControl.CheckMxFile(sMxdFileName))
                    {
                        if (pMapDocument.get_IsReadOnly(sMxdFileName))
                        {
                            MessageBox.Show("本地圖文檔是隻讀的,不能保存!");
                            pMapDocument.Close();
                            return;
                        }
                    }
                    else
                    {
                        SaveFileDialog pSavedlg = new SaveFileDialog();
                        pSavedlg.Title = "請選擇保存路徑";
                        pSavedlg.OverwritePrompt = true;
                        pSavedlg.Filter = "ArcMap文檔(*.mxd)|*.mxd|ArcMap模板(*.mxt)|*.mxt";
                        pSavedlg.RestoreDirectory = true;
                        if (pSavedlg.ShowDialog() == DialogResult.OK)
                        {
                            sMxdFileName = pSavedlg.FileName;
                        }
                        else
                        {
                            return;
                        }
                    }
                    pMapDocument.New(sMxdFileName);
                    pMapDocument.ReplaceContents(basicControl.Map as IMxdContents);
                    pMapDocument.Save(pMapDocument.UsesRelativePaths, true);
                    pMapDocument.Close();
                    MessageBox.Show("保存地圖文檔成功!");
                }


                //打開地圖文檔對話框,讀取數據源類型
                OpenFileDialog pOpenFileDialog = new OpenFileDialog();
                pOpenFileDialog.Filter = "ArcMap文檔(*.mxd)|*.mxd|All Files (*.*)|*.*||";
                pOpenFileDialog.Title = "打開地圖文檔";
                pOpenFileDialog.RestoreDirectory = true;


                if (pOpenFileDialog.ShowDialog() == DialogResult.OK)
                {
                    if (System.IO.Path.GetExtension(pOpenFileDialog.FileName) == ".mxd")
                    {
                        if (basicControl.CheckMxFile(pOpenFileDialog.FileName)) //檢查地圖文檔有效性
                        {
                            basicControl.LoadMxFile(pOpenFileDialog.FileName);
                        }
                        else
                        {
                            MessageBox.Show(pOpenFileDialog.FileName + "是無效的地圖文檔!", "信息提示");
                            return;
                        }
                    }
                }
                else
                {
                    return;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex + "在XXX位置", "信息提示");
            }
        }

保存地圖文檔

public void SaveMxd(ESRI.ArcGIS.Controls.AxMapControl basicControl)
        {
            try
            {
                if (basicControl.LayerCount == 0)
                {
                    MessageBox.Show("無可存地圖文檔!");
                    return;
                }
                string sMxdFileName = basicControl.DocumentFilename;
                string fvg = sMxdFileName;
               if(sMxdFileName == null)
               {
                   //string sMxdFileName = basicControl.DocumentFilename;
                   IMapDocument pMapDocument = new MapDocumentClass();
                   if (sMxdFileName != null && basicControl.CheckMxFile(sMxdFileName))
                   {
                       if (pMapDocument.get_IsReadOnly(sMxdFileName))
                           MessageBox.Show("本地圖文檔是隻讀的,不能保存!");
                       pMapDocument.Close();
                       return;
                   }
                   else
                   {
                       SaveFileDialog pSavedlg = new SaveFileDialog();
                       pSavedlg.Title = "請選擇保存路徑";
                       pSavedlg.OverwritePrompt = true;
                       pSavedlg.Filter = "ArcMap文檔(*.mxd)|*.mxd|ArcMap模板(*.mxt)|*.mxt";
                       pSavedlg.RestoreDirectory = true;
                       if (pSavedlg.ShowDialog() == DialogResult.OK)
                       {
                           sMxdFileName = pSavedlg.FileName;
                       }
                       else
                       {
                           return;
                       }
                   }
                   pMapDocument.New(sMxdFileName);
                   pMapDocument.ReplaceContents(basicControl.Map as IMxdContents);
                   pMapDocument.Save(pMapDocument.UsesRelativePaths, true);
                   pMapDocument.Close();
                   MessageBox.Show("另存爲地圖文檔成功!");

               }

               else 
            {
                IMxdContents pMxdC;

                pMxdC = basicControl.Map as IMxdContents;

                IMapDocument pMapDocument = new MapDocumentClass();

                pMapDocument.Open(basicControl.DocumentFilename, "");

                IActiveView pActiveView = basicControl.Map as IActiveView;

                pMapDocument.ReplaceContents(pMxdC);

                if (pMapDocument == null) return;


                //檢查地圖文檔是否是隻讀

                if (pMapDocument.get_IsReadOnly(pMapDocument.DocumentFilename) == true)
                {

                    MessageBox.Show("本地圖文檔是隻讀的,不能保存!");

                    return;

                }

                //根據相對的路徑保存地圖文檔

                pMapDocument.Save(pMapDocument.UsesRelativePaths, true);

                MessageBox.Show("地圖文檔保存成功!");
                
            
            }
            
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

另存爲地圖文檔

public void OtherSave(ESRI.ArcGIS.Controls.AxMapControl basicControl)
        {
            try
            {
                string sMxdFileName = basicControl.DocumentFilename;
                IMapDocument pMapDocument = new MapDocumentClass();
                if (sMxdFileName != null && basicControl.CheckMxFile(sMxdFileName))
                {
                    if (pMapDocument.get_IsReadOnly(sMxdFileName))
                    {
                        MessageBox.Show("本地圖文檔是隻讀的,不能保存!");
                        pMapDocument.Close();
                        return;
                    }
                    else
                    {
                        SaveFileDialog pSavedlg = new SaveFileDialog();
                        pSavedlg.Title = "請選擇保存路徑";
                        pSavedlg.OverwritePrompt = true;
                        pSavedlg.Filter = "ArcMap文檔(*.mxd)|*.mxd|ArcMap模板(*.mxt)|*.mxt";
                        pSavedlg.RestoreDirectory = true;
                        if (pSavedlg.ShowDialog() == DialogResult.OK)
                        {
                            sMxdFileName = pSavedlg.FileName;
                        }
                        else
                        {
                            return;
                        }
                    }
                }
                
                pMapDocument.New(sMxdFileName);
                pMapDocument.ReplaceContents(basicControl.Map as IMxdContents);
                pMapDocument.Save(pMapDocument.UsesRelativePaths, true);
                pMapDocument.Close();
                MessageBox.Show("另存爲地圖文檔成功!");
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章