public override void OnClick() { //保存路径 SaveFileDialog Sfd = new SaveFileDialog(); Sfd.Title = "另存为地图文档"; Sfd.Filter = "(*.mxd)|*.mxd"; if (Sfd.ShowDialog() != DialogResult.OK) { return; } ModMxd._MxdPath = Sfd.FileName; IMxdContents pMxdC; pMxdC = _AppHk.MapControl.Map as IMxdContents; IMapDocument pMapDocument = new MapDocumentClass(); //创建地图文档 pMapDocument.New(ModMxd._MxdPath); //保存信息 IActiveView pActiveView = _AppHk.MapControl.Map as IActiveView; pMapDocument.ReplaceContents(pMxdC); try//yjl20110817 防止保存时,其他程序也在打开这个文档而导致共享冲突从而使系统报错 { pMapDocument.Save(true, true); if (this.WriteLog) { Plugin.LogTable.Writelog("保存地图文档到" + ModMxd._MxdPath); } ErrorHandle.ShowFrmErrorHandle("提示", "保存成功!"); } catch (Exception ex) { ErrorHandle.ShowFrmErrorHandle("提示", "保存失败!请关掉其他正在打开该文档的程序,重新试一次。"); } pMapDocument.Close();//yjl20110817 }
//保存地图文档 private void barButtonItem26_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { try { string sMxdFileName = Variable.pMapFrm.mainMapControl.DocumentFilename; IMapDocument pMapDocument = new MapDocumentClass(); if (sMxdFileName != null && Variable.pMapFrm.mainMapControl.CheckMxFile(sMxdFileName)) { if (pMapDocument.get_IsReadOnly(sMxdFileName)) { MessageBox.Show("本地图文档是只读的,不能保存!"); pMapDocument.Close(); return; } } else { SaveFileDialog pSaveFileDialog = new SaveFileDialog(); pSaveFileDialog.Title = "请选择保存路径"; pSaveFileDialog.OverwritePrompt = true; pSaveFileDialog.Filter = "ArcMap文档(*.mxd)|*.mxd|ArcMap模板(*.mxt)|*.mxt"; pSaveFileDialog.RestoreDirectory = true; if (pSaveFileDialog.ShowDialog() == DialogResult.OK) { sMxdFileName = pSaveFileDialog.FileName; } else { return; } } pMapDocument.New(sMxdFileName); pMapDocument.ReplaceContents(Variable.pMapFrm.mainMapControl.Map as IMxdContents); pMapDocument.Save(pMapDocument.UsesRelativePaths, true); pMapDocument.Close(); MessageBox.Show("保存地图文档成功!"); } catch (Exception ex) { MessageBox.Show(ex.Message + "/n" + ex.ToString(), "异常"); } }
//地图文档另存为; private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e) { //保存地图文档.mxd try { string smxdfilename = axMapControl1.DocumentFilename; IMapDocument pmapdocument = new MapDocumentClass(); if (smxdfilename != null && axMapControl1.CheckMxFile(smxdfilename)) { if (pmapdocument.get_IsReadOnly(smxdfilename)) { MessageBox.Show("本地图为只读文档,不能修改保存!"); pmapdocument.Close(); return; } } else { saveFileDialog1.Title = "选择保存路径"; saveFileDialog1.OverwritePrompt = true; saveFileDialog1.Filter = "ArcMap文档(*.mxd)|*.mxd"; saveFileDialog1.RestoreDirectory = true; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { smxdfilename = saveFileDialog1.FileName; } else { return; } } pmapdocument.ReplaceContents(axMapControl1.Map as IMxdContents); pmapdocument.New(smxdfilename); pmapdocument.Save(true, true); pmapdocument.Close(); MessageBox.Show("地图保存成功!"); } catch (Exception arr) { MessageBox.Show("错误:" + arr.Message, "提示", MessageBoxButtons.OK); } }
/// <summary> /// 新建mxd文档 /// 参数path为创建mxd文件的路径+名称 /// 参数mapControl为调用该dll项目的地图控件 /// 日期2013-12-13 /// lntu_GISer1 /// </summary> public string NewMxdFile(string path, AxMapControl mapControl) { try { string filename = newfileDialog(path); if (filename != "") { MapDocument pMapDocument = new MapDocumentClass(); pMapDocument.New(filename); pMapDocument.Open(filename, ""); } return(filename); } catch (Exception e) { MessageBox.Show(e.Message); return(""); } }
/// <summary> /// 地图文档保存 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSaveMap_Click(object sender, EventArgs e) { try { string sMxdFileName = MainMapControl.DocumentFilename; IMapDocument pMapDocument = new MapDocumentClass(); if (sMxdFileName != null && MainMapControl.CheckMxFile(sMxdFileName)) { if (pMapDocument.get_IsReadOnly(sMxdFileName)) { MessageBox.Show("本地图文档是只读,不能保存!"); pMapDocument.Close(); return; } else { SaveFileDialog pSaveFileDialog = new SaveFileDialog(); pSaveFileDialog.Title = "请选择保存路径"; pSaveFileDialog.OverwritePrompt = true; pSaveFileDialog.Filter = "ArcMap文档(*.mxd)|*.mxd|ArcMap模板(*.mxt)|*.mxt"; pSaveFileDialog.RestoreDirectory = true; if (pSaveFileDialog.ShowDialog() == DialogResult.OK) { sMxdFileName = pSaveFileDialog.FileName; } else { return; } } pMapDocument.New(sMxdFileName); pMapDocument.ReplaceContents(MainMapControl.Map as IMxdContents); pMapDocument.Save(pMapDocument.UsesRelativePaths, true); pMapDocument.Close(); MessageBox.Show("保存地图文档成功"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void SaveItem_Click(object sender, EventArgs e) { try { string mxdpath = this.axMapcontrol.DocumentFilename; IMapDocument mapDoc = new MapDocumentClass(); if (!string.IsNullOrEmpty(mxdpath) && this.axMapcontrol.CheckMxFile(mxdpath)) { if (mapDoc.get_IsReadOnly(mxdpath)) { MessageBox.Show("该文档为只读不能保存"); return; } } else { //当没有默认路径时 SaveFileDialog dlg = new SaveFileDialog(); dlg.AddExtension = true; dlg.Filter = "地图文档(*.mxd)|*.mxd"; dlg.RestoreDirectory = true; dlg.OverwritePrompt = true; if (dlg.ShowDialog() == DialogResult.OK) { mxdpath = dlg.FileName; } else { return; } } mapDoc.New(mxdpath); mapDoc.ReplaceContents(this.axMapcontrol.Map as IMxdContents); mapDoc.Save(mapDoc.UsesRelativePaths, true); mapDoc.Close(); MessageBox.Show("保存地图文档成功"); } catch (Exception ex) { MessageBox.Show("保存失败" + ex.Message); } }
private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e) { IMapDocument doc = new MapDocumentClass(); SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.Title = "另存为"; saveDialog.OverwritePrompt = true; saveDialog.RestoreDirectory = true; saveDialog.Filter = "ArcMap文档(*.mxd)|*.mxd|ArcMap模板(*.mxt)|*.mxt"; if (saveDialog.ShowDialog() == DialogResult.OK) { String filename = saveDialog.FileName; doc.New(filename); doc.ReplaceContents(axMapControl1.Map as IMxdContents); doc.Save(doc.UsesRelativePaths, true); doc.Close(); MessageBox.Show("保存地图文档成功!"); } }
/// <summary> /// 保存工程 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void iSaveProject_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { //判断当前是否存在mxd文件,若存在则执行保存,否则执行另存为。 if (m_mapControl.DocumentFilename != null) { IMxdContents pMxdC = m_mapControl.Map as IMxdContents; IMapDocument pMapDocument = new MapDocumentClass(); pMapDocument.Open(m_mapControl.DocumentFilename, ""); IActiveView pActiveView = m_mapControl.Map as IActiveView; pMapDocument.ReplaceContents(pMxdC); IObjectCopy lip_ObjCopy = new ObjectCopyClass(); //使用Copy,避免共享引用 m_mapControl.Map = (IMap)lip_ObjCopy.Copy(pMapDocument.Map[0]); lip_ObjCopy = null; pMapDocument.Save(true, false); pMapDocument.Close(); MessageBox.Show("保存成功"); } else { IMapDocument pMapDocument = new MapDocumentClass(); SaveFileDialog opensavemxd = new SaveFileDialog(); opensavemxd.Filter = "地图文档(*.mxd)|*.mxd"; //对话框的过滤器 if (opensavemxd.ShowDialog() == DialogResult.OK) { string filePath = opensavemxd.FileName; //获取文件全路径 pMapDocument.New(filePath); IMxdContents pMxdC = m_mapControl.Map as IMxdContents; pMapDocument.ReplaceContents(pMxdC); pMapDocument.Save(true, false); m_mapControl.LoadMxFile(filePath, 0, Type.Missing); //循环遍历所有的地图 for (int i = 0; i < pMapDocument.MapCount; i++) { m_mapControl.Map = pMapDocument.get_Map(i); //绑定地图控件 } m_mapControl.Map.Name = "Layers"; m_tocControl.SetBuddyControl(m_mapControl.Object); pMapDocument.Close(); } } }
public static void NewMapDoc(Control.ControlCollection controls, ESRI.ArcGIS.Controls.AxMapControl mainMapControl) { try { //实例化AxMapControl控件,并加入Form的Controls ESRI.ArcGIS.Controls.AxMapControl axMapControl = new ESRI.ArcGIS.Controls.AxMapControl(); ((System.ComponentModel.ISupportInitialize)(axMapControl)).BeginInit(); controls.Add(axMapControl); ((System.ComponentModel.ISupportInitialize)(axMapControl)).EndInit(); //实例化SaveFileDialog控件 SaveFileDialog pSaveFileDialog = new SaveFileDialog(); pSaveFileDialog.Title = "输入需要新建地图文档的名称"; pSaveFileDialog.Filter = "地图文件(*.mxd)|*.mxd"; pSaveFileDialog.OverwritePrompt = true; pSaveFileDialog.RestoreDirectory = true; if (pSaveFileDialog.ShowDialog() == DialogResult.OK) {//保存地图文档 string filename = pSaveFileDialog.FileName; IMapDocument pMapDocument = new MapDocumentClass(); pMapDocument.New(filename); axMapControl.Map = pMapDocument.get_Map(0); axMapControl.DocumentFilename = pMapDocument.DocumentFilename; pMapDocument.Close(); mainMapControl.LoadMxFile(filename); mainMapControl.Refresh(); MessageBox.Show("新建地图文档成功!", "信息提示", MessageBoxButtons.OK); return; } else { return; } } catch (Exception exc) { MessageBox.Show("新建地图文档失败!" + exc.Message, "信息提示", MessageBoxButtons.OK); return; } }
//新建 public static void NewDoc(AxMapControl mapControl, AxPageLayoutControl pageLayoutControl = null) { SaveFileDialog NewDlg = new SaveFileDialog(); NewDlg.Title = "新建文档"; NewDlg.InitialDirectory = "C:\\Users\\Administrator\\Desktop"; NewDlg.FileName = "Default"; NewDlg.Filter = "mxd文件|*.mxd"; if (NewDlg.ShowDialog() == DialogResult.OK) { if (MessageBox.Show(" 是否保存当前文档?", "信息提示", MessageBoxButtons.YesNo) == DialogResult.OK) { OperateFile.SaveDoc(mapControl); } IMapDocument pMapDocument = new MapDocumentClass(); pMapDocument.New(NewDlg.FileName); pMapDocument.Open(NewDlg.FileName); mapControl.Map = pMapDocument.get_Map(0); } }
/// <summary> /// 保存地图文档 /// </summary> /// <param name="m_FilePath"></param> /// <param name="m_SaveMap"></param> /// <returns></returns> public bool SaveMap(string m_FilePath, IMap m_SaveMap) { try { IMapDocument pMapDoc = new MapDocumentClass(); IMxdContents pMxdC = m_SaveMap as IMxdContents; pMapDoc.New(m_FilePath); pMapDoc.ReplaceContents(pMxdC); if (pMapDoc.get_IsReadOnly(pMapDoc.DocumentFilename) == true) { MessageBox.Show("本地图文档是只读的,不能保存!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return(false); } pMapDoc.Save(pMapDoc.UsesRelativePaths, true); return(true); } catch (Exception ex) { return(false); } }
private void NewDoc_Click(object sender, EventArgs e) { string sMxdFileName = axMapControl1.DocumentFilename; IMapDocument pMapDocument = new MapDocumentClass(); DialogResult res = MessageBox.Show("是否保存当前地图文档?", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (res == DialogResult.Yes) { try { pMapDocument.New(sMxdFileName); pMapDocument.ReplaceContents((IMxdContents)axMapControl1.Map as IMxdContents); pMapDocument.Save(pMapDocument.UsesRelativePaths, true); pMapDocument.Close(); } catch (Exception ex) { MessageBox.Show("保存地图文档失败!" + ex.ToString()); } } }
public static void new_doc() { try { //实例化SaveFileDialog控件 SaveFileDialog pSaveFileDialog = new SaveFileDialog { Title = "输入需要新建地图文档的名称", Filter = "地图文件(*.mxd)|*.mxd", OverwritePrompt = true, RestoreDirectory = true }; if (pSaveFileDialog.ShowDialog() == DialogResult.OK) {//保存并打开地图文档 string filename = pSaveFileDialog.FileName; IMapDocument pMapDocument = new MapDocumentClass(); pMapDocument.New(filename); mainForm.mainform.mainMapControl.LoadMxFile(filename); //将图层名重设为“图层” mainForm.mainform.mainMapControl.Map.Name = "图层"; mainForm.mainform.axTOCControl1.Update(); //关闭地图文档,防止发生冲突 pMapDocument.Close(); MessageBox.Show("新建地图文档成功!", "信息提示", MessageBoxButtons.OK); return; } else { return; } } catch (Exception exc) { MessageBox.Show("新建地图文档失败!" + exc.Message, "信息提示", MessageBoxButtons.OK); return; } }
/// <summary> /// Occurs when this command is clicked /// </summary> public override void OnClick() { //保存路径 SaveFileDialog Sfd = new SaveFileDialog(); Sfd.Title = "另存为地图文档"; Sfd.Filter = "(*.mxd)|*.mxd"; if (Sfd.ShowDialog() != DialogResult.OK) { return; } ModMxd._MxdPath = Sfd.FileName; IMxdContents pMxdC; pMxdC = m_hookHelper.FocusMap as IMxdContents; IMapDocument pMapDocument = new MapDocumentClass(); //创建地图文档 pMapDocument.New(ModMxd._MxdPath); //保存信息 IActiveView pActiveView = m_hookHelper.ActiveView; pMapDocument.ReplaceContents(pMxdC); try//yjl20110817 防止保存时,其他程序也在打开这个文档而导致共享冲突从而使系统报错 { pMapDocument.Save(true, true); MessageBox.Show("保存成功", "提示!"); } catch { MessageBox.Show("保存失败,请关掉其他正在打开该文档的程序,重新试一次。", "提示!"); } pMapDocument.Close();//yjl20110817 }
/// <summary> /// Saves the map document. /// </summary> /// <param name="mxdContents">The MXD contents.</param> /// <param name="sFilePath">The file path.</param> /// <param name="bUseRelativePath">if set to <c>true</c> [b use relative path].</param> public static void SaveMapDocument(IMxdContents mxdContents, string sFilePath, bool bUseRelativePath = true) { IMapDocument mapDocument = new MapDocumentClass(); try { mapDocument.New(sFilePath); mapDocument.ReplaceContents(mxdContents); mapDocument.Save(bUseRelativePath, true); EnviVars.instance.MapControl.DocumentFilename = sFilePath; } catch (Exception ex) { //_logger.Log(LogLevel.Error, EventType.UserManagement, null, ex); Log.WriteLog(typeof(MapAPI), ex); } finally { EnviVars.instance.MainForm.Text = EnviVars.instance.MapControl.DocumentFilename; mapDocument.Close(); } }
/// <summary> /// 作用:保存mxd文件 /// 作者:汪建龙 /// 编写时间:2016年12月27日18:13:46 /// </summary> /// <param name="mapControl"></param> /// <param name="saveMXDFileName"></param> public static void Save(this IMapControl2 mapControl, string saveMXDFileName) { try { if (System.IO.File.Exists(saveMXDFileName)) { System.IO.File.Delete(saveMXDFileName); } if (System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(saveMXDFileName)) == false) { System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(saveMXDFileName)); } IMapDocument mapDocument = new MapDocumentClass(); mapDocument.New(saveMXDFileName); mapDocument.ReplaceContents(mapControl.Map as IMxdContents); mapDocument.Save(mapDocument.UsesRelativePaths, true); }catch (Exception ex) { System.Diagnostics.Trace.WriteLine(ex); } }
public void SaveMxd(object obj) { SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.InitialDirectory = Environment.SpecialFolder.Desktop.ToString(); saveDialog.Filter = "mxd files (*.mxd)|*.mxd|All files (*.*)|*.*"; saveDialog.FilterIndex = 1; saveDialog.RestoreDirectory = true; if (saveDialog.ShowDialog() == DialogResult.OK) { try { IMapDocument mapDoc = new MapDocumentClass(); mapDoc.New(saveDialog.FileName); mapDoc.ReplaceContents(obj as IMxdContents); mapDoc.Save(true, true); } catch (Exception ex) { MessageBox.Show("保存文件时出错,无法保存MXD!\n\n" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }
private void saveFileAs_Click(object sender, EventArgs e) { try { SaveFileDialog pSaveFileDialog = new SaveFileDialog(); pSaveFileDialog.Title = "另存为"; pSaveFileDialog.OverwritePrompt = true; pSaveFileDialog.Filter = "ArcMap文档(*.mxd)|*.mxd|ArcMap模板(*.mxt)|*.mxt"; pSaveFileDialog.RestoreDirectory = true; if (pSaveFileDialog.ShowDialog() == DialogResult.OK) { string filePath = pSaveFileDialog.FileName; IMapDocument pMapDocument = new MapDocumentClass(); pMapDocument.New(filePath); pMapDocument.ReplaceContents(mainMapControl.Map as IMxdContents); pMapDocument.Save(true, true); pMapDocument.Close(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
//保存工程 private void saveProject_Click(object sender, EventArgs e) { if (!m_isProjOpen) { MessageBox.Show("未打开工程!"); return; } try { m_prjMan.WriteProjectFile(); string mxdFileName = m_prjMan.m_projectPath + "\\" + m_prjMan.m_projectName + "\\" + m_prjMan.m_projectName + ".mxd"; IMxdContents pMxdC; pMxdC = m_mapControl.Map as IMxdContents; IMapDocument pMapDocument = new MapDocumentClass(); if (!File.Exists(mxdFileName)) { pMapDocument.New(mxdFileName); } else { pMapDocument.Open(mxdFileName); } //IActiveView pActiveView = m_mapControl.ActiveView; //pMapDocument.SetActiveView(pActiveView); pMapDocument.ReplaceContents(pMxdC); pMapDocument.Save(true, true); pMapDocument.Close(); MessageBox.Show("工程保存成功!"); } catch { MessageBox.Show("工程保存失败!"); } }
/// <summary> /// Occurs when this command is clicked /// </summary> public override void OnClick() { IPageLayoutControl3 pPageLayoutControl = null; if (m_hookHelper.Hook is IToolbarControl) { pPageLayoutControl = (IPageLayoutControl3)((IToolbarControl)m_hookHelper.Hook).Buddy; } //In case the container is MapControl else if (m_hookHelper.Hook is IPageLayoutControl3) { pPageLayoutControl = (IPageLayoutControl3)m_hookHelper.Hook; } else { MessageBox.Show("当前界面必须是PageLayoutControl界面!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } try { string strDocName = pPageLayoutControl.DocumentFilename; if (strDocName != null) { if (pPageLayoutControl.CheckMxFile(strDocName)) { //create a new instance of a MapDocument IMapDocument mapDoc = new MapDocumentClass(); mapDoc.Open(strDocName, string.Empty); //Make sure that the MapDocument is not readonly if (mapDoc.get_IsReadOnly(strDocName)) { MessageBox.Show("Map document is read only!"); mapDoc.Close(); return; } //Replace its contents with the current map mapDoc.ReplaceContents((IMxdContents)pPageLayoutControl.PageLayout); //save the MapDocument in order to persist it mapDoc.Save(true, true); //close the MapDocument mapDoc.Close(); } } else { IMapDocument mDoc = new MapDocumentClass(); //地图文档对象 SaveFileDialog dlgFile = new SaveFileDialog(); //以对话框选择文档路径 string sFilePath; dlgFile.Title = "另存地图文档"; dlgFile.Filter = "Map Documents(*.mxd)|*.mxd"; if (dlgFile.ShowDialog() == DialogResult.OK) { sFilePath = dlgFile.FileName; mDoc.New(sFilePath); mDoc.ReplaceContents((IMxdContents)pPageLayoutControl.PageLayout); mDoc.Save(true, true); pPageLayoutControl.DocumentFilename = sFilePath; mDoc.Close(); } } } catch (System.Exception ex) { MessageBox.Show(ex.Message); } }
public override void OnClick() { if (!ModMxd._MxdPath.Equals("")) { DialogResult pResult = MessageBox.Show("是否保存当前的地图文档?", "询问", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); switch (pResult) { case DialogResult.Cancel: return; break; case DialogResult.Yes: { IMxdContents pMxdC; pMxdC = _AppHk.MapControl.Map as IMxdContents; IMapDocument pMapDocument = new MapDocumentClass(); //打开地图文档 if (File.Exists(ModMxd._MxdPath)) { pMapDocument.Open(ModMxd._MxdPath, ""); } else { pMapDocument.New(ModMxd._MxdPath); } //保存信息 IActiveView pActiveView = _AppHk.MapControl.Map as IActiveView; pMapDocument.ReplaceContents(pMxdC); pMapDocument.Save(true, true); break; } case DialogResult.No: break; } } OpenFileDialog pOpendlg = new OpenFileDialog(); pOpendlg.Title = "打开地图文档"; pOpendlg.Filter = "(*.mxd)|*.mxd"; if (pOpendlg.ShowDialog() != DialogResult.OK) { return; } string strMxdName = pOpendlg.FileName; if (_AppHk.MapControl.CheckMxFile(strMxdName)) { _AppHk.MapControl.LoadMxFile(strMxdName, "", ""); } UserControlSMPD pUserControl = _hook.MainUserControl as UserControlSMPD; if (pUserControl != null) { pUserControl.OpenMxdDocDeal(); } ModMxd._MxdPath = strMxdName; }
public override bool SaveMap(string filePath) { IMapDocument mapDocument = new MapDocumentClass(); if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } mapDocument.New(filePath); mapDocument.ReplaceContents((IMxdContents)this._axMapCtrl.Map); mapDocument.Save(mapDocument.UsesRelativePaths, true); mapDocument.Close(); return true; }
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 void SaveInitState() { Plugin.Application.IAppDBIntegraRef _pDBIntegra = _hook as Plugin.Application.IAppDBIntegraRef; string strTmpPath = Application.StartupPath + "\\..\\Temp\\Tmpmxd" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".mxd"; string strTmpPath2 = Application.StartupPath + "\\..\\Temp\\Tmpmxd" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + "_.mxd"; if (!System.IO.Directory.Exists(Application.StartupPath + "\\..\\Temp")) { System.IO.Directory.CreateDirectory(Application.StartupPath + "\\..\\Temp"); } IMxdContents pMxdC; pMxdC = _pDBIntegra.MapControl.Map as IMxdContents; IMapDocument pMapDocument = new MapDocumentClass(); //创建地图文档 pMapDocument.New(strTmpPath); //保存信息 IActiveView pActiveView = _pDBIntegra.MapControl.Map as IActiveView; pMapDocument.ReplaceContents(pMxdC); try//yjl20110817 防止保存时,其他程序也在打开这个文档而导致共享冲突从而使系统报错 { pMapDocument.Save(true, true); System.Runtime.InteropServices.Marshal.ReleaseComObject(pMapDocument); pMapDocument = null; pMapDocument = new MapDocumentClass(); pMapDocument.Open(strTmpPath, ""); GeoLayerTreeLib.LayerManager.ModuleMap.SetLayersVisibleAttri(pMapDocument, true); ModPublicFun.WriteLog("pMapDocument.Save start"); pMapDocument.SaveAs(strTmpPath2, true, true); ModPublicFun.WriteLog("pMapDocument.Save false"); } catch (Exception ex) { if (ex != null) { ModPublicFun.WriteLog(ex.Message); } } pMapDocument.Close(); System.Runtime.InteropServices.Marshal.ReleaseComObject(pMapDocument); pMapDocument = null; try { System.IO.File.Delete(strTmpPath); } catch (Exception err2) { } if (System.IO.File.Exists(strTmpPath2)) { SysGisTable mSystable = new SysCommon.Gis.SysGisTable(Plugin.ModuleCommon.TmpWorkSpace); Dictionary <string, object> pDic = new Dictionary <string, object>(); //参数名 pDic.Add("SETTINGNAME", "初始加载地图文档"); //参数数据类型 pDic.Add("DATATYPE", "MxdFile"); //参数描述 //pDic.Add("DESCRIPTION", textBoxSettingDescription.Text); //参数值(分简单参数值和文件型参数值) IMemoryBlobStream pBlobStream = new MemoryBlobStreamClass(); pBlobStream.LoadFromFile(strTmpPath2); pDic.Add("SETTINGVALUE2", pBlobStream); Exception err = null; bool bRes = false; if (mSystable.ExistData("SYSSETTING", "SETTINGNAME='初始加载地图文档'")) { bRes = mSystable.UpdateRow("SYSSETTING", "SETTINGNAME='初始加载地图文档'", pDic, out err); } else { bRes = mSystable.NewRow("SYSSETTING", pDic, out err); } if (!bRes) { MessageBox.Show("保存出错:" + err.Message); } try { System.IO.File.Delete(strTmpPath2); } catch (Exception err2) { } } }
/// <summary> /// 新建mxd文档 /// 参数path为创建mxd文件的路径+名称 /// 参数mapControl为调用该dll项目的地图控件 /// 日期2013-12-13 /// lntu_GISer1 /// </summary> public string NewMxdFile(string path,AxMapControl mapControl) { try { string filename =newfileDialog(path); if (filename != "") { MapDocument pMapDocument = new MapDocumentClass(); pMapDocument.New(filename); pMapDocument.Open(filename, ""); } return filename; } catch(Exception e) { MessageBox.Show(e.Message); return ""; } }
public void ChangelayoutAndSave(string mxdPath, string templatePath, string savePath) { // IMapDocument pDoc = new MapDocumentClass(); IMxdContents pMxdC; IMapDocument pMapDocument = new MapDocumentClass(); pMapDocument.Open(mxdPath, ""); pMxdC = pMapDocument.PageLayout as IMxdContents; IMap pMap = pMxdC.ActiveView.FocusMap; //this.axPageLayoutControl1.ActiveView.FocusMap; IPageLayout pPageLayout = pMxdC.PageLayout; //this.axPageLayoutControl1.PageLayout; //读取新模板 IMapDocument pNewDoc = new MapDocumentClass(); pNewDoc.Open(templatePath, ""); IMap pTempMap; IPageLayout pTempPagelayout = pNewDoc.PageLayout; pTempMap = pNewDoc.get_Map(0); IPage pTempPage = pTempPagelayout.Page; IPage pCurPage = pPageLayout.Page; //替换单位 pCurPage.Units = pTempPage.Units; //exchange page orientation pCurPage.Orientation = pTempPage.Orientation; //替换页面尺寸 Double dWidth = 0; Double dHeight = 0; pTempPage.QuerySize(out dWidth, out dHeight); pCurPage.PutCustomSize(dWidth, dHeight); //删除当前Layout中除了mapframe外的所有elements IGraphicsContainer pGraphicsCont; IElement pElement; pGraphicsCont = pPageLayout as IGraphicsContainer; pGraphicsCont.Reset(); pElement = pGraphicsCont.Next(); IMapFrame pMapFrame = null; IElement pMapFrameElement = null; while (pElement != null) { if (pElement is IMapFrame) { // Console.WriteLine(pElement.Geometry.GeometryType.ToString() + '0'); pMapFrameElement = pElement; pMapFrame = pElement as IMapFrame; pMapFrame.Border = null; } else { pGraphicsCont.DeleteElement(pElement); pGraphicsCont.Reset(); } pElement = pGraphicsCont.Next(); } //遍历模板的PageLayout中的所有元素,并且替换当前PageLayout中的所有元素 IGraphicsContainer pTempGraphicsCont; pTempGraphicsCont = pTempPagelayout as IGraphicsContainer; pTempGraphicsCont.Reset(); pElement = pTempGraphicsCont.Next(); IArray pArray; pArray = new ESRI.ArcGIS.esriSystem.Array(); while (pElement != null) { if (pElement is IMapFrame) { pMapFrameElement.Geometry = pElement.Geometry; } else { if (pElement is IMapSurroundFrame) { IMapSurround pTempMapSurround; IMapSurroundFrame pTempMapSurroundFrame = pElement as IMapSurroundFrame; pTempMapSurroundFrame.MapFrame = pMapFrame; pTempMapSurround = pTempMapSurroundFrame.MapSurround; pMap.AddMapSurround(pTempMapSurround); } pArray.Add(pElement); } pElement = pTempGraphicsCont.Next(); } int pElementCount = pArray.Count; //将模板PageLayout中的其它元素(除了MapFrameElement和MapSurroundFrame外的元素)添加到当前PageLayout中去 for (int i = 0; i < pElementCount; i++) { pGraphicsCont.AddElement(pArray.get_Element(pElementCount - 1 - i) as IElement, 0); // this.axPageLayoutControl1.ActiveView.Refresh(); } // pMxdC.ActiveView.Refresh(); pNewDoc.Close(); pMapDocument = new MapDocumentClass(); pMapDocument.New(savePath); // IActiveView pActiveView = this.axPageLayoutControl1.ActiveView.FocusMap as IActiveView; pMapDocument.ReplaceContents(pMxdC); pMapDocument.Save(true, true); pMapDocument.Close(); }