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);
}
}
方法SaveDocument3种获取路径部门,大家可以自己改成动态保存路径的方式。因为这种加载数据的方式截至到现在我们的程序还没有讲到,在后面我们会继续讲解。