IMapDocument interface

本文详细介绍了如何通过MapDocument类实现地图文档的读写操作,包括打开文档、获取地图对象、判断文档是否只读、保存文档等核心功能。重点讨论了如何在不同情况下灵活运用这些API,确保地图数据的安全与高效管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  Provides access to members that control the reading and writing of map document files.(提供访问的成员,控制读写地图文档文件)
1:MapDocumentClass Class
The MapDocument coclass is used to read and write map document files.(地图文档组件类是用于读写地图文档文件。)实现了IMapDocument接口。
m_MapDocument =  new MapDocumentClass();
2:Open Method
[C#]public void Open (string sDocument,string bsPassword);
[C++]HRESULT Open( BSTR sDocument, BSTR bsPassword);
The MapDocument will be cached so no other users will be able to access the MapDocument until it has been closed.(MapDocument将会被缓存,所以没有其他用户可以访问MapDocument,除非它已被关闭。)Before using the Open methods check whether the specified document  IsPresent, IsRestricted, IsMapDocument and IsPasswordProtected. If the MapDocument is password protected, specify the password in the Open method.
3:MapCount Property
The number of Map objects contained within the map document

// 使用IMapDocument打开文档
IMapDocument  m_MapDocument;
private  void LoadMapDoc()
{
m_MapDocument =  new MapDocumentClass();
try
{
//打开文档对话框选择MXD文件
System.Windows.Forms.  OpenFileDialog openFileDialog2;
openFileDialog2 =  new OpenFileDialog();
openFileDialog2.Title =  "Open Map Document";
openFileDialog2.Filter =  "Map Documents (*.mxd)|*.mxd";
openFileDialog2.ShowDialog();
string  sFilePath = openFileDialog2.FileName;
//将数据加载到pMapDocument并与MapControl联系起来
m_MapDocument.Open(sFilePath,  "");
int  i;
int  sum = m_MapDocument.MapCount ;
for  (i = 0; i <sum; i++)
{
// 一个IMapDocument对象中可能包含很多Map对象,遍历map对象
axMapControl1.Map = m_MapDocument.get_Map(i);
}
// 刷新地图控件
axMapControl1.Refresh();
}
catch  ( Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
 



 
4:Map Property
[C#]public IMap get_Map (int  mapIndex);
[C++]HRESULT get_Map(long mapIndex,  IMap** ppMap);
 
5:IsReadOnly Property
[C#] public bool get_IsReadOnly (string  sDocument);
[C++] HRESULT get_IsReadOnly( BSTR sDocument, VARIANT_BOOL* IsReadOnly );
Determines whether the specified file is read only. The Save method cannot overwrite a read-only MapDocument, use the SaveAs to write to a new document.(保存方法不能覆盖只读的地图文件,使用另存为)
6:Save Method
[C#] public void Save (bool  bUseRelativePaths, bool bCreateThumnbail);
[C#] Optional Values
bUseRelativePaths  Supply true as a default value.(true代码默认值)
bCreateThumnbail  Supply true as a default value.
[C++] HRESULT Save(VARIANT_BOOL bUseRelativePaths, VARIANT_BOOL bCreateThumnbail );
7:UsesRelativePaths Property
[C#]public boolUsesRelativePaths {get;}
[C++]HRESULT get_UsesRelativePaths(VARIANT_BOOL* bUsesRelativePaths);文件是
Indicates if the data in the map document is referenced using relative paths.
(表明如果数据在地图文件是使用相对路径引用。)

 
// 使用IMapDocument保存文档
private  void SaveDocument()
{
// 判断文档是否为只读
if  (m_MapDocument.get_IsReadOnly(m_MapDocument.DocumentFilename))
{
 
MessageBox.Show(  "This map document is read only!");
return;
}
//用当前的文件路径设置保存文件
m_MapDocument.Save(m_MapDocument.UsesRelativePaths,  true);
MessageBox.Show(  "Changes saved successfully!");
}

8:SaveAs Method
Save the contents of the map document to the specified file name.
[C#]public void SaveAs (string sDocument, bool bUseRelativePaths, boolbCreateThumnbail);
Optional Values
bUseRelativePaths  Supply true as a default value.
bCreateThumnbail  Supply true as a default value.

// 地图文档另存为
private  void SaveAsDocument ()
{
//Open a file dialog for saving map documents
SaveFileDialog  saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Title =  "Save Map Document As";
saveFileDialog1.Filter =  "Map Documents (*.mxd)|*.mxd";
saveFileDialog1.ShowDialog();
//Exit if no map document is selected
string  sFilePath = saveFileDialog1.FileName;
if  (sFilePath == "")
{
return;
}
if  (sFilePath == m_MapDocument.DocumentFilename)
{
// 当选择文件和当前文件相同时,保存即可
SaveDocument();
}
else
{
//SaveAs a new document with relative paths
m_MapDocument.SaveAs(sFilePath,  true, true);
//Open document
 
MessageBox.Show(  "Document saved successfully!");
 
}
}

转载于:https://www.cnblogs.com/qiuzhongyang/p/3874124.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值