/// <summary> /// 作用:打开TemplateMapDocument /// 作者:汪建龙 /// 编写时间:2016年12月23日11:18:34 /// </summary> /// <returns></returns> private IMapDocument OpenTemplateDocument() { try { IMapDocument tempMapDocumnet = new MapDocumentClass(); if (!tempMapDocumnet.get_IsPresent(TemplateMxdFileName) || !tempMapDocumnet.get_IsMapDocument(TemplateMxdFileName) || tempMapDocumnet.get_IsPasswordProtected(TemplateMxdFileName) || tempMapDocumnet.get_IsRestricted(TemplateMxdFileName)) { var message = string.Format("路径:\"{0}\"下的地图文档不正确,请拷贝该文件到该路径下", _templateMxdFileName); System.Diagnostics.Trace.WriteLine(message); return(null); } tempMapDocumnet.Open(TemplateMxdFileName, null); if (tempMapDocumnet.DocumentVersion == esriMapDocumentVersionInfo.esriMapDocumentVersionInfoFail) { var message = string.Format("路径:\"{0}\"下的地图文档版本不正确,请拷贝该文件到该路径下", TemplateMxdFileName); return(null); } return(tempMapDocumnet); }catch (Exception ex) { System.Diagnostics.Trace.WriteLine(ex); return(null); } }
/// <summary> /// 作用:MapControl加载mxd文件 /// 作者:汪建龙 /// 编写时间:2016年12月26日09:49:25 /// </summary> /// <param name="mapControl"></param> /// <param name="mxdFile"></param> public static void Load(this IMapControl2 mapControl, string mxdFile) { try { if (System.IO.File.Exists(mxdFile) == false) { return; } IMapDocument mapDocument = new MapDocumentClass(); if (!mapDocument.get_IsPresent(mxdFile) || !mapDocument.get_IsMapDocument(mxdFile) || mapDocument.get_IsPasswordProtected(mxdFile) || mapDocument.get_IsRestricted(mxdFile)) { string message = string.Format("路径:\"{0}\"下的地图文档不正确,请拷贝文件到该路径下。", mxdFile); System.Diagnostics.Trace.WriteLine(message); } else { mapDocument.Open(mxdFile, null); if (mapDocument.DocumentVersion == esriMapDocumentVersionInfo.esriMapDocumentVersionInfoFail) { string message = string.Format("路径:\"{0}\"下的地图文档版本不正确,请拷贝该文件到该路径下。", mxdFile); System.Diagnostics.Trace.WriteLine(message); } else if (mapDocument.MapCount > 0) { IMap map = mapDocument.get_Map(0); mapControl.Map = map; } } }catch (Exception ex) { System.Diagnostics.Trace.WriteLine(ex); } }
public bool AddGxFileLayer(IBasicMap pBasicMap, IGroupLayer pParentLayer, string sFileName) { try { if (sFileName == null) { sFileName = ""; } if (pBasicMap == null) { return(false); } if (string.IsNullOrEmpty(sFileName)) { OpenFileDialog dialog = new OpenFileDialog { Filter = "图层文件 (*.lyr)|*.lyr", Multiselect = false, Title = "选择输入的图层文件" }; if (dialog.ShowDialog() != DialogResult.OK) { return(false); } sFileName = dialog.FileName; dialog = null; } if (string.IsNullOrEmpty(sFileName)) { return(false); } if (!File.Exists(sFileName)) { Interaction.MsgBox("地图图层加载失败,图层文件 " + sFileName + " 不存在。", MsgBoxStyle.Exclamation, "错误警告"); return(false); } IMapDocument document = null; document = new MapDocumentClass(); if (!document.get_IsMapDocument(sFileName)) { Interaction.MsgBox("文件 " + sFileName + " 无法加载。\r\n文件不是 MapDocument 文件。", MsgBoxStyle.Exclamation, "失败"); return(false); } if (document.get_IsRestricted(sFileName)) { Interaction.MsgBox("文件 " + sFileName + " 无法加载。\r\n文件受到限制,无权使用。", MsgBoxStyle.Exclamation, "失败"); return(false); } document.Open(sFileName, null); ILayer pLayer = null; if (document.DocumentType == esriMapDocumentType.esriMapDocumentTypeLyr) { pLayer = document.get_Layer(0, 0); if (pLayer == null) { Interaction.MsgBox("文件 " + sFileName + " 无法加载。\r\n图层文件或数据错误。", MsgBoxStyle.Exclamation, "加载失败"); } } else { Interaction.MsgBox("文件 " + sFileName + " 无法加载。\r\n文件不是地图图层文件。", MsgBoxStyle.Exclamation, "加载失败"); } document.Close(); document = null; if (pLayer == null) { return(false); } if (pParentLayer == null) { pBasicMap.AddLayer(pLayer); } else { pParentLayer.Add(pLayer); } IMapLayers layers = pBasicMap as IMapLayers; if (pParentLayer == null) { layers.MoveLayer(pLayer, 0); } else if (pBasicMap is IMap) { layers.MoveLayerEx(pParentLayer, pParentLayer, pLayer, 0); } return(true); } catch (Exception exception) { this.mErrOpt.ErrorOperate(this.mSubSysName, "FunFactory.LayerFun", "AddGxFileLayer", exception.GetHashCode().ToString(), exception.Source, exception.Message, "", "", ""); return(false); } }
public bool SaveMapDocument(IMxdContents pMxdContents, string sFileName, bool bOverwritePrompt, bool bUseRelativePaths, bool bCreateThumnbail, bool bSilent) { try { if (sFileName == null) { sFileName = ""; } if (pMxdContents == null) { MessageBox.Show("地图文档保存失败,Map 内容加载失败。", "错误警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return(false); } if (string.IsNullOrEmpty(sFileName)) { SaveFileDialog dialog = new SaveFileDialog { DefaultExt = "mxd", FileName = "无标题", Filter = "地图文档 (*.mxd)|*.mxd", OverwritePrompt = bOverwritePrompt, Title = "保存地图到文件" }; if (dialog.ShowDialog() != DialogResult.OK) { return(false); } sFileName = dialog.FileName; dialog = null; if (string.IsNullOrEmpty(sFileName)) { return(false); } } else if (((File.Exists(sFileName) & bOverwritePrompt) && !bSilent) && (Interaction.MsgBox("文件 " + sFileName + " 已存在。\r\n是否要替换?", MsgBoxStyle.YesNo, "确认替换") != MsgBoxResult.Yes)) { return(false); } if (File.Exists(sFileName)) { FileSystem.Kill(sFileName); } if (File.Exists(sFileName)) { MessageBox.Show("文件 " + sFileName + " 无法删除。\r\n共享冲突,文件正在使用。", "覆盖文件错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return(false); } IMapDocument document = null; document = new MapDocumentClass(); document.New(sFileName); document.ReplaceContents(pMxdContents); document.SetActiveView(pMxdContents.ActiveView); if (!document.get_IsMapDocument(sFileName)) { MessageBox.Show("文件 " + sFileName + " 无法保存。\r\n文件不是 MapDocument 文件。", "保存文件错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return(false); } if (document.get_IsRestricted(sFileName)) { Interaction.MsgBox("文件 " + sFileName + " 无法保存。\r\n文件受到限制,无权使用。", MsgBoxStyle.Exclamation, "失败"); return(false); } document.Save(bUseRelativePaths, bCreateThumnbail); document.Close(); document = null; return(true); } catch (Exception exception) { this.mErrOpt.ErrorOperate(this.mSubSysName, "FunFactory.CoreFun", "SaveMapDocument", exception.GetHashCode().ToString(), exception.Source, exception.Message, "", "", ""); return(false); } }
private IMapDocument OpenMapDocument(string sFileName, string sPassword) { try { if (sFileName == null) { sFileName = ""; } if (sPassword == null) { sPassword = ""; } if (string.IsNullOrEmpty(sFileName)) { OpenFileDialog dialog = new OpenFileDialog { Filter = "所有支持文件|*.mxd;*.mxt;*.pmf;*.lyr|地图文档 (*.mxd)|*.mxd|地图模板 (*.mxt)|*.mxt|发布地图 (*.pmf)|*.pmf|地图图层 (*.lyr)|*.lyr", Multiselect = false, Title = "选择的地图文件" }; if (dialog.ShowDialog() != DialogResult.OK) { return(null); } sFileName = dialog.FileName; dialog = null; } if (string.IsNullOrEmpty(sFileName)) { return(null); } if (!File.Exists(sFileName)) { Interaction.MsgBox("地图文档加载失败,文档文件 " + sFileName + " 不存在。", MsgBoxStyle.Exclamation, "错误警告"); return(null); } IMapDocument document = null; document = new MapDocumentClass(); if (!document.get_IsMapDocument(sFileName)) { Interaction.MsgBox("文件 " + sFileName + " 无法加载。\r\n文件不是 MapDocument 文件。", MsgBoxStyle.Exclamation, "失败"); return(null); } if (document.get_IsRestricted(sFileName)) { Interaction.MsgBox("文件 " + sFileName + " 无法加载。\r\n文件受到限制,无权使用。", MsgBoxStyle.Exclamation, "失败"); return(null); } if (document.get_IsPasswordProtected(sFileName)) { document.Open(sFileName, sPassword); } else { document.Open(sFileName, ""); } return(document); } catch (Exception exception) { this.mErrOpt.ErrorOperate(this.mSubSysName, "FunFactory.CoreFun", "OpenMapDocument", exception.GetHashCode().ToString(), exception.Source, exception.Message, "", "", ""); return(null); } }