//#define debug using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Globalization; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace mainProgram { public partial class SVDWindows : Form { private ProjectManager mProjectManager = null;//保存打开的/新建的工程对象 private string mSifAlgorithm; public SVDWindows(ProjectManager projectManager,string sifAlgorithm) { InitializeComponent(); mProjectManager = projectManager; mSifAlgorithm = sifAlgorithm; this.Text = mSifAlgorithm;//设置界面左上角的窗口名 //准备sif算法默认窗口 switch (mSifAlgorithm) { case "svd": Min_FraunhoferWindowTextBox.Text = "740"; Max_FraunhoferWindowTextBox.Text = "770"; FraunhoferPositionTextBox.Text = "760"; UseLeftShoulder(false); UseRightShoulder(false); UseFraunhoferPosition(true); break; case "sfm": Min_FraunhoferWindowTextBox.Text = "759"; Max_FraunhoferWindowTextBox.Text = "770"; FraunhoferPositionTextBox.Text = "760"; UseLeftShoulder(false); UseRightShoulder(false); UseFraunhoferPosition(true); break; case "sfm_gaussian": Min_FraunhoferWindowTextBox.Text = "759"; Max_FraunhoferWindowTextBox.Text = "770"; FraunhoferPositionTextBox.Text = "760"; UseLeftShoulder(false); UseRightShoulder(false); UseFraunhoferPosition(true); break; case "doas": Min_FraunhoferWindowTextBox.Text = "759"; Max_FraunhoferWindowTextBox.Text = "770"; FraunhoferPositionTextBox.Text = "760"; UseLeftShoulder(false); UseRightShoulder(false); UseFraunhoferPosition(true); break; case "sfld": Min_FraunhoferWindowTextBox.Text = "740"; Max_FraunhoferWindowTextBox.Text = "780"; LeftShoulder_leftTextBox.Text = "756"; LeftShoulder_rightTextBox.Text = "759"; UseLeftShoulder(true); UseRightShoulder(false); UseFraunhoferPosition(false); break; case "3fld": Min_FraunhoferWindowTextBox.Text = "740"; Max_FraunhoferWindowTextBox.Text = "780"; LeftShoulder_leftTextBox.Text = "756"; LeftShoulder_rightTextBox.Text = "759"; RightShoulder_leftTextBox.Text = "761"; RightShoulder_rightTextBox.Text = "762"; UseLeftShoulder(true); UseRightShoulder(true); UseFraunhoferPosition(false); break; /* 您可以有任意数量的 case 语句 */ default: /* 可选的 */ //statement(s); break; } } private void UseLeftShoulder(bool readyToUse) { LeftShoulder_leftTextBox.Enabled = readyToUse; LeftShoulder_rightTextBox.Enabled = readyToUse; } private void UseRightShoulder(bool readyToUse) { RightShoulder_leftTextBox.Enabled = readyToUse; RightShoulder_rightTextBox.Enabled = readyToUse; } private void UseFraunhoferPosition(bool readyToUse) { FraunhoferPositionTextBox.Enabled = readyToUse; } private void ProcessBtn_Click(object sender, EventArgs e) { //准备需要处理的文件 DateTime min = min_dateTimePicker.Value; DateTime max = max_dateTimePicker.Value; if (max < min) { MessageBox.Show(this, "选择的数据时间错误!", "提示"); return; } string minTime = min.Year.ToString() + "_" + min.Month.ToString() + "_" + min.Day.ToString(); string maxTime = max.Year.ToString() + "_" + max.Month.ToString() + "_" + max.Day.ToString(); ProcessMessageTextBox.Text = "Preparing files ..."; mProjectManager.CopyWantedFiles(minTime, maxTime); this.ProcessMessageTextBox.Text = this.ProcessMessageTextBox.Text + System.Environment.NewLine + "\tdone ..." + System.Environment.NewLine; try { //构建参数 string programDir = System.Environment.CurrentDirectory; #if debug//条件编译符号 定义在 此源文件的第一行 string param1 = "D:\\PycharmProjects\\sif\\standard_sif.csv"; #else string param1 = Path.Combine(programDir, "standard_sif.csv"); #endif string param2 = mProjectManager.mSifTmpPath;//输入数据文件夹 string outputFileName = mSifAlgorithm + "_" + Min_FraunhoferWindowTextBox.Text + "-" + Max_FraunhoferWindowTextBox.Text + "_" + FraunhoferPositionTextBox.Text + "_" + minTime + "-" + maxTime + ".csv"; string param3 = Path.Combine(mProjectManager.mSifPath, outputFileName);//输出文件名 string param4 = ""; switch (mSifAlgorithm) { case "svd": case "sfm": case "sfm_gaussian": case "doas": //string param4 = "[740,770],760"; param4 = "[" + Min_FraunhoferWindowTextBox.Text + "," + Max_FraunhoferWindowTextBox.Text + "]," + FraunhoferPositionTextBox.Text; break; case "sfld": param4 = "[" + Min_FraunhoferWindowTextBox.Text + "," + Max_FraunhoferWindowTextBox.Text + "]," + "[" + LeftShoulder_leftTextBox.Text + "," + LeftShoulder_rightTextBox.Text + "]"; break; case "3fld": param4 = "[" + Min_FraunhoferWindowTextBox.Text + "," + Max_FraunhoferWindowTextBox.Text + "]," + "[" + LeftShoulder_leftTextBox.Text + "," + LeftShoulder_rightTextBox.Text + "]," + "[" + RightShoulder_leftTextBox.Text + "," + RightShoulder_rightTextBox.Text + "]"; break; /* 您可以有任意数量的 case 语句 */ default: /* 可选的 */ //statement(s); break; } string param5 = "P1"; string param6 = mSifAlgorithm; int a = 1; int b = 2; //执行算法 StartTest(param1, param2, param3, param4, param5, param6); //this.DialogResult = DialogResult.OK;//这行代码会导致窗口直接关闭 } catch (Exception e1) { MessageBox.Show(e1.Message); } } private Process progressTest; private void log(string log, bool time) { //将参数写入到txt文件中 → 调试使用 string programDir = System.Environment.CurrentDirectory; string tmp = Path.Combine(programDir, "commandLine.txt");//保存的文件路径 //FileStream mystream = new FileStream(tmp, FileMode.OpenOrCreate); //StreamWriter sw = new StreamWriter(mystream); StreamWriter sw = new StreamWriter(tmp, true); if (time) { string tradeTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ", DateTimeFormatInfo.InvariantInfo); sw.WriteLine(tradeTime + log);//按行写 } else { sw.WriteLine(" " + log);//按行写 } sw.Close();//关闭 } public bool StartTest(string param1, string param2, string param3, string param4, string param5, string param6) { bool state = true; Console.WriteLine("开始执行sif 算法 python+++++++++++++++++++++++\n"); ProcessStartInfo start = new ProcessStartInfo(); string programDir = System.Environment.CurrentDirectory; #if debug//条件编译符号 定义在 此源文件的第一行 string sifPlgorithm_exe = Path.Combine(@"D:\csharp_vs2017\easySif\sifAlgorithm_Python_exe\dist", "sif_retrieval.exe"); #else string sifPlgorithm_exe = Path.Combine(programDir, "sif_retrieval.exe"); #endif start.FileName = sifPlgorithm_exe; string sArguments = param1 + " " + param2 + " " + param3 + " " + param4 + " " + param5 + " " + param6; log(sArguments, true); start.Arguments = sArguments; start.UseShellExecute = false; start.RedirectStandardOutput = true; start.RedirectStandardInput = true; start.RedirectStandardError = true; start.CreateNoWindow = true; using (progressTest = Process.Start(start)) { // 异步获取命令行内容 progressTest.BeginOutputReadLine(); // 为异步获取订阅事件 progressTest.OutputDataReceived += new DataReceivedEventHandler(outputDataReceived); progressTest.ErrorDataReceived += new DataReceivedEventHandler(outputDataReceived2); //progressTest.StandardError += new } Console.WriteLine("开始执行sif 算法 python---------------------\n"); //this.DialogResult = DialogResult.OK;//这行代码会导致窗口直接关闭 return state; } public void outputDataReceived(object sender, DataReceivedEventArgs e) { if (!string.IsNullOrEmpty(e.Data))//System.Environment.NewLine { this.Invoke( new Action(() => { this.ProcessMessageTextBox.AppendText(e.Data + System.Environment.NewLine); log(e.Data, false); }) ); if (e.Data.Contains("Sif compute Completed!"))//当转换完成后 { this.Invoke( new Action(() => { MessageBox.Show(this, "sif计算完成!", "提示"); this.DialogResult = DialogResult.OK; this.Close(); }) ); } if (e.Data.Contains("error"))//当转换完成后 { this.Invoke( new Action(() => { MessageBox.Show(this, "sif算法发生错误,请联系工程师解决!", "提示"); this.DialogResult = DialogResult.No; this.Close(); }) ); } } //if (this.InvokeRequired)//InvokeRequired属性为真时,说明一个创建它以以外的线程想访问它 //{ // if (e.Data.Contains("Completed"))//System.NullReferenceException:“未将对象引用设置到对象的实例。” // //System.Diagnostics.DataReceivedEventArgs.Data.get 返回 null。 // { // DataReceivedEventHandler Tmp = new DataReceivedEventHandler(outputDataReceived); // this.Invoke(Tmp, new object[] { sender, e }); // } //} //else //{ // MessageBox.Show(this, "sif转换完成!", "提示");//线程间操作无效: 从不是创建控件“SVDWindows”的线程访问它。 //} } public void outputDataReceived2(object sender, DataReceivedEventArgs e) { if (!string.IsNullOrEmpty(e.Data))//System.Environment.NewLine { string tmp = this.ProcessMessageTextBox.Text + System.Environment.NewLine + e.Data; this.Invoke( new Action(() => { this.ProcessMessageTextBox.Text = tmp; log(e.Data, false); }) ); if (e.Data.Contains("Completed"))//当转换完成后 { this.Invoke( new Action(() => { MessageBox.Show(this, "sif转换完成!", "提示"); this.DialogResult = DialogResult.OK; this.Close(); }) ); } } } private void CancelBtn_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; Close(); } private void SVDWindows_Load(object sender, EventArgs e) { //获取数据的时间范围 mProjectManager.FindTimespan(); string minDate = mProjectManager.GetMinDate(); string maxDate = mProjectManager.GetMaxDate(); string[] minDateList = minDate.Split('_'); string[] maxDateList = maxDate.Split('_'); //显示数据的时间范围 MinYearTextBox.Text = minDateList[0]; MinMonthTextBox.Text = minDateList[1]; MinDayTextBox.Text = minDateList[2]; MaxYearTextBox.Text = maxDateList[0]; MaxMonthTextBox.Text = maxDateList[1]; MaxDayTextBox.Text = maxDateList[2]; //设置默认需要处理的时间范围 DateTime min = new DateTime(int.Parse(minDateList[0]), int.Parse(minDateList[1]), int.Parse(minDateList[2])); DateTime max = new DateTime(int.Parse(maxDateList[0]), int.Parse(maxDateList[1]), int.Parse(maxDateList[2])); min_dateTimePicker.MinDate = min; min_dateTimePicker.MaxDate = max; min_dateTimePicker.Value = min; max_dateTimePicker.MinDate = min; max_dateTimePicker.MaxDate = max; max_dateTimePicker.Value = max; } } }