private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)//保存mxd文档 { IMxdContents pMxdC = null; try { pMxdC = axMapControl1.Map as IMxdContents; IMapDocument pMapDocument = new MapDocumentClass(); if (axMapControl1.DocumentFilename == null) { XtraMessageBox.Show("地图文档不存在,需新建"); SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "地图文档(*.mxd)|*.mxd"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { pMapDocument.New(saveFileDialog.FileName); } else { return; } } else { pMapDocument.Open(axMapControl1.DocumentFilename, ""); } pMapDocument.ReplaceContents(pMxdC); pMapDocument.Save(true, true); XtraMessageBox.Show("保存成功"); } catch { } }
/// <summary> /// 工程另存为 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void iSaveProjectAs_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { //ESRI.ArcGIS.SystemUI.ICommand saveCommand = new ControlsSaveAsDocCommand(); //saveCommand.OnCreate(m_mapControl.Object); //saveCommand.OnClick(); 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); //绑定地图控件 } pMapDocument.Close(); } }
private void 保存SToolStripMenuItem_Click(object sender, EventArgs e) { if (axMapControl1.DocumentMap == null) { MessageBox.Show("地图文档不能为空", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { try { IMapDocument pMapDocument = new MapDocumentClass(); string saveFileName = axMapControl1.DocumentFilename; IMxdContents pMxdcontents = axMapControl1.Map as IMxdContents; pMapDocument.New(saveFileName); pMapDocument.ReplaceContents(pMxdcontents); if (pMapDocument.get_IsReadOnly(saveFileName)) { MessageBox.Show("本地图文档只读的,不能保存!"); } else { pMapDocument.Save(pMapDocument.UsesRelativePaths, true); MessageBox.Show("保存地图文档成功!"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
/// <summary> /// Called when the user clicks a command. /// </summary> /// <remarks>Note to inheritors: override OnClick and use this method to /// perform the actual work of the custom command.</remarks> public override void OnClick() { //Logger logger = new Logger(); try { EnviVars.instance.MainForm.UseWaitCursor = true; if (!String.IsNullOrEmpty(EnviVars.instance.MapControl.DocumentFilename) && EnviVars.instance.MapControl.CheckMxFile(EnviVars.instance.MapControl.DocumentFilename)) { IMapDocument mapDocument = new MapDocumentClass(); mapDocument.Open(EnviVars.instance.MapControl.DocumentFilename, ""); mapDocument.ReplaceContents((IMxdContents)EnviVars.instance.MapControl.Map); mapDocument.Save(mapDocument.UsesRelativePaths, true); mapDocument.Close(); } else { //ICommand command = new ControlsSaveAsDocCommandClass(); //command.OnCreate(EnviVars.instance.PageLayoutControl.Object); //command.OnClick(); System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog(); saveFileDialog.Title = "保存任务文档"; saveFileDialog.Filter = string.Format("{0} (*.mxd)|*.mxd", "任务文档"); if (saveFileDialog.ShowDialog(EnviVars.instance.MainForm) == System.Windows.Forms.DialogResult.OK) { IMxdContents mxdContents = this.m_hookHelper.FocusMap as IMxdContents; //if (EnviVars.instance.ActiveViewMode == ViewMode.PageLayout) //{ // mxdContents = (this.m_hookHelper.PageLayout as IMxdContents); //} MapAPI.SaveMapDocument(mxdContents, saveFileDialog.FileName, false); //logger.Log(LogLevel.Info, EventType.UserManagement, AppMessage.MSG0095, null); } } } catch (Exception ex) { //logger.Log(LogLevel.Error, EventType.UserManagement, AppMessage.MSG0095, ex); Log.WriteLog(typeof(CmdSaveFile), ex); } finally { EnviVars.instance.MainForm.Text = EnviVars.instance.MapControl.DocumentFilename; EnviVars.instance.MainForm.UseWaitCursor = false; } }
/// <summary> /// 保存地图文档 /// </summary> /// <param name="m_FilePath"></param> /// <param name="m_SaveMap"></param> /// <returns></returns> public void 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); } pMapDoc.Save(pMapDoc.UsesRelativePaths, true); } catch (Exception ex) { } }
private void btnOK_Click(object sender, EventArgs e) { //此处添加参数检查 //此处清空图层,新建地图文档,新建任务xml ICommand cmd = new CmdClearLayers(); cmd.OnCreate(EnviVars.instance.MapControl.Object); cmd.OnClick(); //保存空白地图文档 IMxdContents mxdContents = EnviVars.instance.MapControl.Map as IMxdContents; MapAPI.SaveMapDocument(mxdContents, taskFile, false); //新建任务xml NewTaskXML(); GFS.BLL.EnviVars.instance.history.AddTask(taskFile); this.Close(); }
/// <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(); } } }
/// <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(); } }
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); } }
/// <summary> /// Called when the user clicks a command. /// </summary> /// <remarks>Note to inheritors: override OnClick and use this method to /// perform the actual work of the custom command.</remarks> public override void OnClick() { //Logger logger = new Logger(); try { EnviVars.instance.MainForm.UseWaitCursor = true; //System.Windows.Forms.Cursor.Current=System.Windows.Forms.Cursors.WaitCursor; bool isCurrrentNew = (String.IsNullOrEmpty(EnviVars.instance.MapControl.DocumentFilename) && EnviVars.instance.MapControl.LayerCount > 0); if ((!String.IsNullOrEmpty(EnviVars.instance.MapControl.DocumentFilename) && EnviVars.instance.MapControl.CheckMxFile(EnviVars.instance.MapControl.DocumentFilename)) || isCurrrentNew) { DialogResult res = XtraMessageBox.Show("保存当前文档?", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (res == DialogResult.Yes) { //if yes, launch the Save command //ICommand command = new CmdSaveFile(); //command.OnCreate(EnviVars.instance.MapControl.Object); //command.OnClick(); IMxdContents mxdContents = EnviVars.instance.MapControl.Map as IMxdContents; MapAPI.SaveMapDocument(mxdContents, EnviVars.instance.MapControl.DocumentFilename.ToString(), false); } } //logger.Log(LogLevel.Info, EventType.UserManagement, AppMessage.MSG0090, null); //launch the OpenMapDoc command //ICommand OpenMapDoc = new ControlsOpenDocCommandClass(); //OpenMapDoc.OnCreate(this.m_hookHelper.Hook); //OpenMapDoc.OnClick(); OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "Map Documents (*.mxd)|*.mxd"; dlg.Multiselect = false; dlg.Title = "打开文档"; if (dlg.ShowDialog() == DialogResult.OK) { string docName = dlg.FileName; string taskXml = docName.Substring(0, docName.LastIndexOf(".")) + ".xml"; if (!File.Exists(taskXml)) { XtraMessageBox.Show("任务描述文件不存在!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } if (!Task.ParseTask(taskXml)) { return; } EnviVars.instance.CurrentTask = taskXml; IMapDocument mapDoc = new MapDocumentClass(); if (mapDoc.get_IsPresent(docName) && !mapDoc.get_IsPasswordProtected(docName)) { //mapDoc.Open(docName, string.Empty); // set the first map as the active view //IMap map = mapDoc.get_Map(0); //mapDoc.SetActiveView((IActiveView)map); //EnviVars.instance.PageLayoutControl.PageLayout = mapDoc.PageLayout; //EnviVars.instance.Synchronizer.ReplaceMap(map); //EnviVars.instance.MapControl.Map = map; //mapDoc.Close(); EnviVars.instance.MapControl.DocumentFilename = docName; EnviVars.instance.MapControl.LoadMxFile(docName); EnviVars.instance.history.AddTask(docName); //CommandAPI.AddRecentFile(docName, FileType.MXD); //CommandAPI.SaveRecentFilesInfo(); //logger.Log(LogLevel.Info, EventType.UserManagement, AppMessage.MSG0090, null); } } } catch (Exception ex) { //logger.Log(LogLevel.Error, EventType.UserManagement, AppMessage.MSG0090, ex); Log.WriteLog(typeof(CmdOpenMxd), ex); } finally { EnviVars.instance.MainForm.Text = EnviVars.instance.MapControl.DocumentFilename; EnviVars.instance.MainForm.UseWaitCursor = false; } }