using ESRI.ArcGIS.Controls; using KGIS.Framework.Maps; using KGIS.Framework.ThreadManager; using KGIS.Framework.Utils; using KGIS.Framework.Utils.Helper; using Kingo.PluginServiceInterface; using Kingo.PluginServiceInterface.Model; using Microsoft.Win32; using System; using System.IO; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; namespace Kingo.Plugin.YJJK.View { /// /// 执行数据导出用户控件 /// public partial class UCExecuteExport : UserControl, IWizardFrameworkExeState { #region 私有成员 /// /// 数据导出 /// //private IDataOutput m_pDataPickUp; //private ThreadProgressCoreClass m_pThreadEventCoreClass = null; #endregion private IHookHelper _hookHelper { get; set; } #region 构造函数 public UCExecuteExport(IHookHelper hookHelper) { InitializeComponent(); _hookHelper = hookHelper; } #endregion #region 实现向导接口属性 /// /// 向导步骤间的参数传递 /// public object Parameter { get; set; } private string m_strDescription = ""; /// /// 描述 /// public string Description { get { return m_strDescription; } set { m_strDescription = value; } } private string m_strCaption = "数据导出"; /// /// 标题 /// public string Caption { get { return m_strCaption; } set { m_strCaption = value; } } private bool m_bIsFinalSuccess = true; /// /// 标识是否是最后一步 /// public bool IsFinalSuccess { get { return m_bIsFinalSuccess; } set { m_bIsFinalSuccess = value; } } public bool IsSkip { get; set; } public Action ExeStateCallBacK { get; set; } #endregion public void ExeGP(object obj) { IDGParameter parm = obj as IDGParameter; if (parm.ExeDLTB) { ProcesHelper.Instance.ExeGPForProces2(obj as IDGParameter); } parm.ExeDLTB = false; ProcesHelper.Instance.ExeGPForProces(obj as IDGParameter); } public void ExeGPComplat(object obj) { this.Dispatcher.Invoke(() => { IDGParameter parm = obj as IDGParameter; if (parm.ExtParam != null && parm.ExtParam.Count > 0) { TextRange text = new TextRange(txtProcessLog.Document.ContentStart, txtProcessLog.Document.ContentEnd); if (!text.Text.Contains("失败")) { foreach (var item in parm.ExtParam) { if (item.IsSkip) continue; if (item.Execute()) { txtProcessLog.AppendText($"\r\n {item.Caption} 执行完成。"); pgbProcess.Value++; } else { txtProcessLog.AppendText($"\r\n {item.Caption} 执行失败。"); break; } } } } if (pgbProcess.Value == pgbProcess.Maximum) { ExeStateCallBacK?.Invoke(ExeState.Success); MessageHelper.ShowTips($"执行完成。"); } else { ExeStateCallBacK?.Invoke(ExeState.Fail); MessageHelper.ShowTips($"执行失败。"); } }); } #region 实现向导接口方法 /// /// 执行函数 /// /// 成功返回true,失败返回false public bool Execute() { try { ExeStateCallBacK?.Invoke(ExeState.Start); txtProcessLog.Document.Blocks.Clear(); txtProcessLog.AppendText("开始执行:" + DateTime.Now.ToString()); if (Parameter is IDGParameter) { IDGParameter parm = Parameter as IDGParameter; var count = 0; if (parm.ExeDLTB) count++; if (parm.ExeCJDCQ || parm.ExeXZQ || parm.ExeGDDB) count++; if (parm.ExeCZC) count++; if (parm.ExeZLHZ) count++; if (count > 0) { pgbProcess.Maximum = parm.ExtParam.Count + count; ProcesHelper.Instance.ProgressHandle = (o) => { this.Dispatcher.Invoke(() => { //System.Windows.Forms.Application.DoEvents(); if (o.ToString().Contains("地类图斑数据提取完成")) { //(_hookHelper.Hook as IMapControlDefault).ActiveView.Refresh(); MapsManager.Instance.MapService.getAxMapControl().ActiveView.Refresh(); } if (o.ToString().Contains("提取完成")) pgbProcess.Value++; if (o.ToString().Contains("Log:") || o.ToString().Contains("Err:") || o.ToString().Contains("Msg:")) txtProcessLog.AppendText($"\r\n {o.ToString()}"); }); }; ThreadManager.Instance.QueueUserWorkItem(new System.Threading.WaitCallback(ExeGP), parm, new System.Threading.WaitCallback(ExeGPComplat)); } else { pgbProcess.Maximum = parm.ExtParam.Count; if (parm.ExtParam.Count > 0) ExeGPComplat(parm); else { MessageHelper.ShowTips($"执行失败,未选择执行步骤。"); return false; } } } //DataOutputClass pPickUp = m_pDataPickUp as DataOutputClass; //if (pPickUp != null) //{ // pPickUp.ObsImportDataLogEntity = new System.Collections.ObjectModel.ObservableCollection(); //} //if (m_pThreadEventCoreClass == null) //{ // m_pThreadEventCoreClass = new ThreadProgressCoreClass(m_pDataPickUp as IThreadProgress, pgbProcess, txtProcessLog); // string strLog = "DataOutputLog_" + DateTime.Now.ToString("yyyyMMddHHmmss"); // m_pThreadEventCoreClass.OutputTempLogName = strLog; //} //try //{ // IAoInitialize aoinitialize = new AoInitializeClass(); // aoinitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB); // m_pDataPickUp.OutputData(); return true; //} //catch (Exception ex) //{ // LogAPI.Debug(ex); // m_pThreadEventCoreClass.m_threadProgress_ProgressEnd("数据导出失败!" ); // return false; //} } catch (Exception ex) { ExeStateCallBacK?.Invoke(ExeState.Fail); throw; } } /// /// 初始化函数 /// public void Intializing() { try { //m_pDataPickUp = (IDataOutput)Parameter; } catch (Exception ex) { LogAPI.Debug(ex); } } /// /// 验证函数,验证参数输入参数是否正确 /// /// public bool Validating() { return true; } /// /// 上一步 /// public void Back() { } #endregion #region 控件事件 private void bbiSave_Click(object sender, RoutedEventArgs e) { CSaveLogToFile c = new CSaveLogToFile(); c.SaveToTxt(txtProcessLog); } private void bbiClear_Click(object sender, RoutedEventArgs e) { txtProcessLog.Document.Blocks.Clear(); } private void UserControl_Unloaded(object sender, RoutedEventArgs e) { try { //CGeometryOperator pCop = new CGeometryOperator(); //pCop.DeleteAllElement(m_pDataPickUp.MapControl.ActiveView); } catch { } } public void Intializing(bool IsSkip = false) { //throw new NotImplementedException(); } #endregion private void txtProcessLog_TextChanged(object sender, TextChangedEventArgs e) { //this.txtProcessLog.AppendText(""); //TextRange a = new TextRange(txtProcessLog.Document.ContentStart, txtProcessLog.Document.ContentEnd); } } public class CSaveLogToFile { /// /// 保存成文本文件 /// /// 日志文本框 public void SaveToTxt(RichTextBox txtLog) { TextRange a = new TextRange(txtLog.Document.ContentStart, txtLog.Document.ContentEnd); string strLog = a.Text; if (strLog.Length == 0) { MessageHelper.Show("文本框中的内容为空!"); return; } try { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "文本文件(*.txt)|*.txt"; if (saveFileDialog.ShowDialog() == true) { //使用“另存为”对话框中输入的文件名实例化StreamWriter对象 StreamWriter sw = new StreamWriter(saveFileDialog.FileName, true, Encoding.GetEncoding("GB2312")); //向创建的文件中写入内容 sw.WriteLine(strLog); //关闭当前文件写入流 sw.Close(); MessageHelper.Show("保存成功!"); } } catch (Exception ex) { MessageHelper.ShowError("保存失败!"); LogAPI.Debug(ex); } } /// /// 保存成文本文件 /// /// 日志文本框 public void SaveToTxt(TextBox txtLog) { string strLog = txtLog.Text; if (strLog.Length == 0) { MessageHelper.Show("文本框中的内容为空!"); return; } try { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "文本文件(*.txt)|*.txt"; if (saveFileDialog.ShowDialog() == true) { //使用“另存为”对话框中输入的文件名实例化StreamWriter对象 StreamWriter sw = new StreamWriter(saveFileDialog.FileName, true, Encoding.GetEncoding("GB2312")); //向创建的文件中写入内容 sw.WriteLine(strLog); //关闭当前文件写入流 sw.Close(); MessageHelper.Show("保存成功!"); } } catch (Exception ex) { MessageHelper.ShowError("保存失败!"); LogAPI.Debug(ex); } } } }