AE地图文档的操作,新建打开保存另存为地图文档的实例

一个简单的实例,AE地图文档的一些操作,包括新建,打开,保存,另存为地图文档等操作。

 

功能介绍:新建地图文档,新建之前判断当前是否存在地图文档,如存在则提示是否保存,点击是保存当前文档并新建空白文档;点击否则直接新建地图文档。

     打开地图文档,打开一个新的地图文档。如果当前存在地图文档则直接重新加载新打开的地图文档。

     保存地图文档,保存当前的地图文档。

     另存为地图文档,将当前地图文档另存为一个新的地图文档。

开发环境:ArcObject10.1,VS2010

代码如下:

 1         private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
 2         {
 3             //如果当前没有图层,则不操作
 4             if (axMapControl1.Map.LayerCount == 0)
 5             {
 6 
 7             }
 8             else
 9             {
10                 ICommand command = new CreateNewDocument();
11                 command.OnCreate(axMapControl1.Object);
12                 command.OnClick();
13             }
14         }
15 
16         private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
17         {
18             ICommand command = new ControlsOpenDocCommandClass();
19             command.OnCreate(axMapControl1.Object);
20             command.OnClick();
21         }
22 
23         private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
24         {
25             string m_currentMapDocument = axMapControl1.DocumentFilename;
26 
27             if (axMapControl1.Map.LayerCount == 0)
28             {
29                 MessageBox.Show("没有地图文档需要保存!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
30                 return;
31             }
32 
33             //execute Save Document command
34             if (axMapControl1.CheckMxFile(m_currentMapDocument))
35             {
36                 //create a new instance of a MapDocument
37                 IMapDocument mapDoc = new MapDocumentClass();
38                 mapDoc.Open(m_currentMapDocument, string.Empty);
39 
40                 //Make sure that the MapDocument is not readonly
41                 if (mapDoc.get_IsReadOnly(m_currentMapDocument))
42                 {
43                     MessageBox.Show("Map document is read only!");
44                     mapDoc.Close();
45                     return;
46                 }
47 
48                 //Replace its contents with the current map
49                 mapDoc.ReplaceContents((IMxdContents)axMapControl1.Map);
50 
51                 //save the MapDocument in order to persist it
52                 mapDoc.Save(mapDoc.UsesRelativePaths, false);
53 
54                 //close the MapDocument
55                 mapDoc.Close();
56 
57                 MessageBox.Show("保存地图文档成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
58             }
59             else
60             {
61                 MessageBox.Show(m_currentMapDocument + "不是有效的地图文档!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
62             }
63         }
64 
65         private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
66         {
67             if (axMapControl1.Map.LayerCount == 0)
68             {
69                 MessageBox.Show("没有地图文档需要保存!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
70                 return;
71             }
72 
73             try
74             {
75                 ICommand command = new ControlsSaveAsDocCommandClass();
76                 command.OnCreate(axMapControl1.Object);
77                 command.OnClick();
78                 MessageBox.Show("另存为地图文档成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
79             }
80             catch (Exception ex)
81             {
82                 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
83             }
84         }
85 
86         private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
87         {
88             this.Close();
89         }

 

其中,新建地图文档CreateNewDocument类的代码:

  1 /// <summary>
  2     /// Summary description for CreateNewDocument.
  3     /// </summary>
  4     public class CreateNewDocument : BaseCommand
  5     {
  6         private IHookHelper m_hookHelper = null;
  7 
  8         //constructor
  9         public CreateNewDocument()
 10         {
 11             //update the base properties
 12             base.m_category = ".NET Samples";
 13             base.m_caption = "NewDocument";
 14             base.m_message = "Create a new map";
 15             base.m_toolTip = "Create a new map";
 16             base.m_name = "DotNetTemplate_NewDocumentCommand";
 17         }
 18 
 19         #region Overridden Class Methods
 20 
 21         /// <summary>
 22         /// Occurs when this command is created
 23         /// </summary>
 24         /// <param name="hook">Instance of the application</param>
 25         public override void OnCreate(object hook)
 26         {
 27             if (m_hookHelper == null)
 28                 m_hookHelper = new HookHelperClass();
 29 
 30             m_hookHelper.Hook = hook;
 31         }
 32 
 33         /// <summary>
 34         /// Occurs when this command is clicked
 35         /// </summary>
 36         public override void OnClick()
 37         {
 38             IMapControl3 mapControl = null;
 39 
 40             //get the MapControl from the hook in case the container is a ToolbarControl
 41             if (m_hookHelper.Hook is IToolbarControl)
 42             {
 43                 mapControl = (IMapControl3)((IToolbarControl)m_hookHelper.Hook).Buddy;
 44             }
 45             //In case the container is MapControl
 46             else if (m_hookHelper.Hook is IMapControl3)
 47             {
 48                 mapControl = (IMapControl3)m_hookHelper.Hook;
 49             }
 50             else
 51             {
 52                 //MessageBox.Show("Active control must be MapControl!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
 53                 MessageBox.Show("必须是MapControl控件!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
 54                 return;
 55             }
 56 
 57             //check to see if there is an active edit session and whether edits have been made
 58             DialogResult result;
 59             IEngineEditor engineEditor = new EngineEditorClass();
 60 
 61             if ((engineEditor.EditState == esriEngineEditState.esriEngineStateEditing) && (engineEditor.HasEdits() == true))
 62             {
 63                 //result = MessageBox.Show("Would you like to save your edits", "Save Edits", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
 64                 result = MessageBox.Show("是否保存编辑", "保存编辑", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
 65 
 66 
 67                 switch (result)
 68                 {
 69 
 70                     case DialogResult.Cancel:
 71                         return;
 72 
 73                     case DialogResult.No:
 74                         engineEditor.StopEditing(false);
 75                         break;
 76 
 77                     case DialogResult.Yes:
 78                         engineEditor.StopEditing(true);
 79                         break;
 80 
 81                 }
 82             }
 83 
 84             //allow the user to save the current document
 85             //DialogResult res = MessageBox.Show("Would you like to save the current document?", "AoView", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
 86             DialogResult res = MessageBox.Show("是否保存当前地图文档?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
 87             if (res == DialogResult.Yes)
 88             {
 89                 //launch the save command
 90                 ICommand command = new ControlsSaveAsDocCommandClass();
 91                 command.OnCreate(m_hookHelper.Hook);
 92                 command.OnClick();
 93             }
 94 
 95             //create a new Map
 96             IMap map = new MapClass();
 97             map.Name = "Map";
 98 
 99             //assign the new map to the MapControl
100             mapControl.DocumentFilename = string.Empty;
101             mapControl.Map = map;
102         }
103 
104         #endregion
105     }

 源代码完整实例下载地址:MapDocumentOperation.rar

 

posted @ 2013-12-04 22:55  zhzhx0318  阅读(1604)  评论(0编辑  收藏  举报