AE+地图文档基本操作

时间:2024-03-01 07:26:13

1.新建地图文档

        public IMapDocument pMapDoc;

        private void TSMenuItem_New_Click(object sender, EventArgs e)
        {
            pMapDoc = new MapDocumentClass();
            //设置默认路径
            string filePath = Application.StartupPath + "\\Temp\\Untitled.mxd";
            pMapDoc.New(filePath);
            pMapDoc.Open(filePath, "");
            //空白的地图文件只有一个地图
            axMapControl1.Map = pMapDoc.get_Map(0);
            MessageBox.Show("新建地图文档成功!");
        }

 

2.打开地图文档

        private void TSMenuItem_Open_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Map Documents(*.mxd)|*.mxd";
            if(openFileDialog.ShowDialog()==DialogResult.OK)
            {

                第一种方式
                string fileName = Path.GetFileName(filePath);
                if (axMapControl1.CheckMxFile(fileName))
                {
                    axMapControl1.MousePointer = esriControlsMousePointer.esriPointerHourglass;
                    axMapControl1.LoadMxFile(fileName, 0, Type.Missing);
                    axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;
                }
                else
                {
                    MessageBox.Show("所选文件不是地图文档文件!", "信息提示");
                    return;
                }

                第二种方式

                pMapDoc = new MapDocumentClass();
                pMapDoc.Open(filePath,"");
                for(int i=0;i<pMapDoc.MapCount;i++)
                {
                    axMapControl1.Map=pMapDoc.get_Map(i);
                }
                axMapControl1.ActiveView.Refresh();
            }
        }

 

3.保存地图文档

3.1 用于自定义IMapDocument接口打开地图文档时

        private void TSMenuItem_Save_Click(object sender, EventArgs e)
        {
            //如何pMapDoc为空,取消保存
            if (pMapDoc == null) return;

            if (pMapDoc.get_IsReadOnly(pMapDoc.DocumentFilename))
            {
                MessageBox.Show("地图文档是只读的,不能保存!");
                return;
            }

            //根据相对路径保存文档
            pMapDoc.Save(pMapDoc.UsesRelativePaths, true);
            MessageBox.Show("地图文档保存成功!");
        }

3.2 用于使用AE内置方式打开地图文档时(快捷方式打开)

        private void TSMenuItem_Save_Click(object sender, EventArgs e)
        {
            IMxdContents pMxdC = axMapControl1.Map as IMxdContents;
            IMapDocument pMapDoc= new MapDocumentClass();
            pMapDoc.Open(axMapControl1.DocumentFilename, "");
            pMapDoc.ReplaceContents(pMxdC);

            if (pMapDoc== null) return;

            if (pMapDoc.get_IsReadOnly(pMapDoc.DocumentFilename))
            {
                MessageBox.Show("地图文档是只读的,不能保存!");
                return;
            }

            pMapDoc.Save(pMapDoc.UsesRelativePaths, true);

            MessageBox.Show("地图文档保存成功!");
        }

3.3 用于使用AE内置方式添加数据时(快捷方式添加数据)

        private void TSMenuItem_Save_Click(object sender, EventArgs e)
        {
            IMxdContents pMxdC = axMapControl1.Map as IMxdContents;
            IMapDocument pMapDoc= new MapDocumentClass();
            //设置保存路径
            string filePath = Application.StartupPath + "\\Temp\\Untitled.mxd";
            pMapDoc.New(filePath);
            pMapDoc.ReplaceContents(pMxdC);
            pMapDoc.Save(true, true);
            MessageBox.Show("地图文档保存成功!");
        }

4.另存地图文档

        private void TSMenuItem_SaveAs_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Title = "另存为";
            saveFileDialog.FileName = "Untitled";
            saveFileDialog.DefaultExt = "mxd";
            saveFileDialog.Filter = "Map Documents(*.mxd)|*.mxd";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = saveFileDialog.FileName;
                pMapDoc = new MapDocumentClass();
                pMapDoc.New(filePath);
                pMapDoc.Open(filePath,"");
                pMapDoc.Save(pMapDoc.UsesRelativePaths, true);
                MessageBox.Show("地图文档另存成功!");
            }
        }