ArcEngine数据操作之地图文档数据保存

ArcGIS Engine提供了保存Mxd文件的类MapDoment。但在具体保存MXD文件过程中有下面三种情况:
  

下面我们针对这三种情况进行简单的说明。

(1)为“文件”菜单添加下拉菜单“保存文档”,name属性设置为“mnuFile_2”;

(2)为“保存文档”菜单添加Click事件处理程序。

1 直接使用IMapDocument接口的Open方法来打开MXD文件,编辑过后进行保存。

对于采用这种方法的可直接使用IMapDocument的save或者saveas的方法来进行保存就可以。

     为了实现本重办法,我们将4.2.1.2地图数据文档加载改为IMapDocument打开的方式(请参照OpenDocument方法),并将对pMapDocument的定义为全局变量。

/// <summary>

          /// 保存地图文档

          /// </summary>

          /// <param name="sender"></param>

          /// <param name="e"></param>

          private void mnuFile_2_Click(object sender, System.EventArgs e)

           {

               //方法一

               SaveDocument1();

 

          }

          /// <summary>

          /// 保存地图文档(针对采用IMapDocument接口打开地图文档方式)

          /// </summary>

          private void SaveDocument1()

          {

               try

               {

                    //判断pMapDocument是否为空,否则将取消保存

                    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);

               }

          }

2 使用Engine中带的OpenDocument方法来打开MXD文件,然后编辑过之后要进行保存。

/// <summary>

          /// 保存地图文档

          /// </summary>

          /// <param name="sender"></param>

          /// <param name="e"></param>

          private void mnuFile_2_Click(object sender, System.EventArgs e)

          {

               //方法一

               //SaveDocument1();

               //方法二

               SaveDocument2();

 

      }

/// <summary>

          /// 保存地图文档(针对使用Engine中带的OpenDocument方法来打开MXD文件方式)

          /// </summary>

          private void SaveDocument2()

          {

               try

               {

                    //判断pMapDocument是否为空,

                    //获取pMapDocument对象

                    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==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);

               }

          }

3 使用自己写的添加数据的工具直接添加数据,也就是说一开始没有MXD文件,在编辑完之后需要把当前的地图保存为一个MXD文件。

/// <summary>

          /// 保存地图文档(针对使用自己写的添加数据的工具直接添加数据方式)

          /// </summary>

          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);

               }

          }

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值