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

  • 2
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值