ArcGIS Engine 保存Mxd地图文档(转帖)

来源:互联网 发布:ubuntu vi 编辑命令 编辑:程序博客网 时间:2024/05/17 01:15

ArcGIS Engine提供了保存Mxd文件的类MapDoment。但在具体保存MXD文件过程中有下面三种情况:
  
下面我们针对这三种情况进行简单的说明。
1.直接使用IMapDocument接口的Open方法来打开MXD文件,编辑过后进行保存。
对于采用这种方法的可直接使用IMapDocument的save或者saveas的方法来进行保存就可以。
用IMapDocument打开的方式(请参照OpenDocument方法)打开一个Mxd,并将对pMapDocument的定义为全局变量。


  1. /// <summary> 
  2.          /// 保存地图文档 
  3.          /// </summary> 
  4.          /// <param name="sender"></param> 
  5.          /// <param name="e"></param> 
  6.          private void mnuFile_2_Click(object sender, System.EventArgs e)  
  7.           {  
  8.               //方法一 
  9.               SaveDocument1();  
  10.          }  
  11.          /// <summary> 
  12.          /// 保存地图文档(针对采用IMapDocument接口打开地图文档方式) 
  13.          /// </summary> 
  14.          private void SaveDocument1()  
  15.          {  
  16.               try 
  17.               {  
  18.                    //判断pMapDocument是否为空,否则将取消保存 
  19.                    if(pMapDocument==nullreturn ;  
  20.                      
  21.                    //检查地图文档是否是只读 
  22.                    if (pMapDocument.get_IsReadOnly(pMapDocument.DocumentFilename) == true)  
  23.                    {  
  24.                        MessageBox.Show("本地图文档是只读的,不能保存!");  
  25.                        return;  
  26.                    }  

//根据相对的路径保存地图文档

  •   pMapDocument.Save(pMapDocument.UsesRelativePaths,true);  
  •                    MessageBox.Show("地图文档保存成功!");  
  •               }  
  •               catch(Exception ex)  
  •               {  
  •                    MessageBox.Show(ex.Message);  
  •               }  
  •          }  

  • 2 使用Engine中带的OpenDocument方法来打开MXD文件,然后编辑过之后要进行保存。
    1. /// <summary> 
    2.          /// 保存地图文档 
    3.          /// </summary> 
    4.          /// <param name="sender"></param> 
    5.          /// <param name="e"></param> 
    6.          private void mnuFile_2_Click(object sender, System.EventArgs e)  
    7.          {  
    8.               //方法一 
    9.               //SaveDocument1(); 
    10.               //方法二 
    11.               SaveDocument2();  
    12.      }  
    13. /// <summary> 
    14.          /// 保存地图文档(针对使用Engine中带的OpenDocument方法来打开MXD文件方式) 
    15.          /// </summary> 
    16.          private void SaveDocument2()  
    17.          {  
    18.               try 
    19.               {  
    20.                    //判断pMapDocument是否为空, 
    21.                    //获取pMapDocument对象 
    22.                    IMxdContents pMxdC;  
  • pMxdC = axMapControl1.Map as IMxdContents ;  
  •                    IMapDocument pMapDocument = new MapDocumentClass();  
  •                    pMapDocument.Open (axMapControl1.DocumentFilename,"");  
  •                    IActiveView pActiveView = axMapControl1.Map as IActiveView ;  
  •                    pMapDocument.ReplaceContents (pMxdC);  
  •                    if(pMapDocument==nullreturn ;  
  •                      
  •                    //检查地图文档是否是只读  
  •                    if (pMapDocument.get_IsReadOnly(pMapDocument.DocumentFilename) == true)  
  •                    {  
  •                        MessageBox.Show("本地图文档是只读的,不能保存!");  
  •                        return;  
  •                    }  
  •                    //根据相对的路径保存地图文档  
  •                    pMapDocument.Save(pMapDocument.UsesRelativePaths,true);  
  •                    MessageBox.Show("地图文档保存成功!");  
  •               }  
  •               catch(Exception ex)  
  •               {  
  •                    MessageBox.Show(ex.Message);  
  •               }  
  •          }  3 使用自己写的添加数据的工具直接添加数据,也就是说一开始没有MXD文件,在编辑完之后需要把当前的地图保存为一个MXD文件。
  •  private void SaveDocument3()  
  •          {  
  •               try 
  •               {  
  •                    //判断pMapDocument是否为空,  
  •                    //获取pMapDocument对象  
  •                    IMxdContents pMxdC;  
  •                    pMxdC = axMapControl1.Map as IMxdContents ;  
  •                    IMapDocument pMapDocument = new MapDocumentClass();  
  •                    //获取保存路径  
  •                    pMapDocument.New ("d:\\aa3.mxd");  
  •                    //  
  •                    pMapDocument.ReplaceContents (pMxdC);  
  •                    //保存地图文档  
  •                    pMapDocument.Save(true,true);  
  •                    MessageBox.Show("地图文档保存成功!");  
  •                      
  •               }  
  •               catch(Exception ex)  
  •               {  
  •                    MessageBox.Show(ex.Message);  
  •               }  
  •          }  

    方法SaveDocument3种获取路径部门,大家可以自己改成动态保存路径的方式。因为这种加载数据的方式截至到现在我们的程序还没有讲到,在后面我们会继续讲解

  • 0 0
    原创粉丝点击