您的位置:首页 > 其它

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

2013-12-04 22:55 429 查看
一个简单的实例,AE地图文档的一些操作,包括新建,打开,保存,另存为地图文档等操作。

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

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

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

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

开发环境:ArcObject10.1,VS2010

代码如下:

private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
//如果当前没有图层,则不操作
if (axMapControl1.Map.LayerCount == 0)
{

}
else
{
ICommand command = new CreateNewDocument();
command.OnCreate(axMapControl1.Object);
command.OnClick();
}
}

private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
ICommand command = new ControlsOpenDocCommandClass();
command.OnCreate(axMapControl1.Object);
command.OnClick();
}

private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
string m_currentMapDocument = axMapControl1.DocumentFilename;

if (axMapControl1.Map.LayerCount == 0)
{
MessageBox.Show("没有地图文档需要保存!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return;
}

//execute Save Document command
if (axMapControl1.CheckMxFile(m_currentMapDocument))
{
//create a new instance of a MapDocument
IMapDocument mapDoc = new MapDocumentClass();
mapDoc.Open(m_currentMapDocument, string.Empty);

//Make sure that the MapDocument is not readonly
if (mapDoc.get_IsReadOnly(m_currentMapDocument))
{
MessageBox.Show("Map document is read only!");
mapDoc.Close();
return;
}

//Replace its contents with the current map
mapDoc.ReplaceContents((IMxdContents)axMapControl1.Map);

//save the MapDocument in order to persist it
mapDoc.Save(mapDoc.UsesRelativePaths, false);

//close the MapDocument
mapDoc.Close();

MessageBox.Show("保存地图文档成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else
{
MessageBox.Show(m_currentMapDocument + "不是有效的地图文档!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (axMapControl1.Map.LayerCount == 0)
{
MessageBox.Show("没有地图文档需要保存!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return;
}

try
{
ICommand command = new ControlsSaveAsDocCommandClass();
command.OnCreate(axMapControl1.Object);
command.OnClick();
MessageBox.Show("另存为地图文档成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}


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

/// <summary>
/// Summary description for CreateNewDocument.
/// </summary>
public class CreateNewDocument : BaseCommand
{
private IHookHelper m_hookHelper = null;

//constructor
public CreateNewDocument()
{
//update the base properties
base.m_category = ".NET Samples";
base.m_caption = "NewDocument";
base.m_message = "Create a new map";
base.m_toolTip = "Create a new map";
base.m_name = "DotNetTemplate_NewDocumentCommand";
}

#region Overridden Class Methods

/// <summary>
/// Occurs when this command is created
/// </summary>
/// <param name="hook">Instance of the application</param>
public override void OnCreate(object hook)
{
if (m_hookHelper == null)
m_hookHelper = new HookHelperClass();

m_hookHelper.Hook = hook;
}

/// <summary>
/// Occurs when this command is clicked
/// </summary>
public override void OnClick()
{
IMapControl3 mapControl = null;

//get the MapControl from the hook in case the container is a ToolbarControl
if (m_hookHelper.Hook is IToolbarControl)
{
mapControl = (IMapControl3)((IToolbarControl)m_hookHelper.Hook).Buddy;
}
//In case the container is MapControl
else if (m_hookHelper.Hook is IMapControl3)
{
mapControl = (IMapControl3)m_hookHelper.Hook;
}
else
{
//MessageBox.Show("Active control must be MapControl!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
MessageBox.Show("必须是MapControl控件!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}

//check to see if there is an active edit session and whether edits have been made
DialogResult result;
IEngineEditor engineEditor = new EngineEditorClass();

if ((engineEditor.EditState == esriEngineEditState.esriEngineStateEditing) && (engineEditor.HasEdits() == true))
{
//result = MessageBox.Show("Would you like to save your edits", "Save Edits", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
result = MessageBox.Show("是否保存编辑", "保存编辑", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

switch (result)
{

case DialogResult.Cancel:
return;

case DialogResult.No:
engineEditor.StopEditing(false);
break;

case DialogResult.Yes:
engineEditor.StopEditing(true);
break;

}
}

//allow the user to save the current document
//DialogResult res = MessageBox.Show("Would you like to save the current document?", "AoView", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
DialogResult res = MessageBox.Show("是否保存当前地图文档?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (res == DialogResult.Yes)
{
//launch the save command
ICommand command = new ControlsSaveAsDocCommandClass();
command.OnCreate(m_hookHelper.Hook);
command.OnClick();
}

//create a new Map
IMap map = new MapClass();
map.Name = "Map";

//assign the new map to the MapControl
mapControl.DocumentFilename = string.Empty;
mapControl.Map = map;
}

#endregion
}


源代码完整实例下载地址:MapDocumentOperation.rar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: