using CaeJob; using System; using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Linq; using System.Windows.Forms; namespace CPSO.Forms { public partial class FrmRBFMonitor : Form { private Process _process; private Controller _controller; public FrmRBFMonitor() { InitializeComponent(); btnKill.Enabled = false; btnResult.Enabled = false; } private void Job_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { autoScrollTextBox1.AutoScrollAppendText(Environment.NewLine); autoScrollTextBox1.AutoScrollAppendText("<======================================================>" + Environment.NewLine); autoScrollTextBox1.AutoScrollAppendText("计算完毕!" + Environment.NewLine); if (!btnResult.Enabled) { btnResult.Enabled = true; } if (btnKill.Enabled) { btnKill.Enabled = false; } if (!btnSummit.Enabled) { btnSummit.Enabled = true; } } private void Job_DoWork(object sender, DoWorkEventArgs e) { // activate.bat文件所在目录 string condaPath = @"J:\Users\Luke\miniconda3\Scripts\activate.bat"; // Python虚拟环境 string env = null; // Python脚本文件 string script = @"YourScript.py"; if(propertyGrid1.SelectedObject is ViewRBFMorphing config) { condaPath = config.CondaActivateBat; if(!File.Exists(condaPath)) return; env = config.Environment; script = config.Script; if(!File.Exists(condaPath)) return; } else { return; } // 1. Set working directory and create process var workingDirectory = Path.GetFullPath("Scripts"); var psi = new ProcessStartInfo { FileName = "cmd.exe", RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, WorkingDirectory = workingDirectory, CreateNoWindow = true }; var setting = _controller.Settings; setting.CheckWorkingDirectory(); var ev = new EnvironmentVariable("WG_WorkingDirectory", setting.GetWorkDirectory()); if (psi.EnvironmentVariables.ContainsKey(ev.Name)) { psi.EnvironmentVariables.Remove(ev.Name); } psi.EnvironmentVariables.Add(ev.Name, ev.Value); if (!psi.EnvironmentVariables.ContainsKey(ev.Name)) { throw new Exception(); } _process = new Process { StartInfo = psi }; _process.OutputDataReceived += (o, args) => { if (args.Data != null) { autoScrollTextBox1.AutoScrollAppendText(args.Data); autoScrollTextBox1.AutoScrollAppendText(Environment.NewLine); } }; _process.ErrorDataReceived += (o, args) => { if (args.Data != null) { autoScrollTextBox1.AutoScrollAppendText(args.Data); autoScrollTextBox1.AutoScrollAppendText(Environment.NewLine); } }; _process.Start(); // 2. Pass multiple commands to cmd.exe using (var sw = _process.StandardInput) { if (sw.BaseStream.CanWrite) { // Vital to activate Anaconda sw.WriteLine(condaPath); // Activate your environment if (string.IsNullOrEmpty(env)) { sw.WriteLine($"activate {env}"); } // run your script. You can also pass in arguments sw.WriteLine($"python {script}"); } } // 3. Read multiple output lines _process.BeginOutputReadLine(); _process.BeginErrorReadLine(); _process.WaitForExit(); } private void FrmRBFMonitor_FormClosing(object sender, FormClosingEventArgs e) { if (e.CloseReason == CloseReason.UserClosing) { if (propertyGrid1.SelectedObject is ViewRBFMorphing rbf && rbf.CondaActivateBat != _controller.Settings.Python.CondaPath) { var settings = _controller.Settings.ToDictionary() .Where(kvp => !(kvp.Value is PythonSettings)) .ToDictionary(kvp => kvp.Key, kvp => kvp.Value); settings.Add(Globals.PythonSettingNames, rbf.GetBase()); foreach (var entry in settings) { entry.Value.CheckValues(); } _controller.Settings = new SettingsContainer(settings); } autoScrollTextBox1.Clear(); if (_process != null) { ExecutableJob.KillAllProcessesSpawnedBy((uint)_process.Id); } e.Cancel = true; Hide(); } } public bool PrepareForm(Controller ctx) { _controller = ctx; propertyGrid1.SelectedObject = new ViewRBFMorphing(_controller.Settings.Python); return true; } private void FrmRBFMonitor_Load(object sender, EventArgs e) { // 本地化 Text = @"RBF Morph运行监控"; tabPage1.Text = @"运行..."; tabPage2.Text = @"信息"; tabPage3.Text = @"设置"; // 定义后台程序 job.WorkerReportsProgress = true; job.WorkerSupportsCancellation = true; job.DoWork += Job_DoWork; job.RunWorkerCompleted += Job_RunWorkerCompleted; } private void btnSummit_Click(object sender, EventArgs e) { if (!job.IsBusy) { job.RunWorkerAsync(); } if (btnSummit.Enabled) { btnSummit.Enabled = false; } if (!btnKill.Enabled) { btnKill.Enabled = true; btnKill.Focus(); } } private void btnKill_Click(object sender, EventArgs e) { autoScrollTextBox1.Clear(); if (_process != null) { ExecutableJob.KillAllProcessesSpawnedBy((uint)_process.Id); } if (!btnSummit.Enabled) { btnSummit.Enabled = true; } if (btnKill.Enabled) { btnKill.Enabled = false; } } private void btnResult_Click(object sender, EventArgs e) { if (_controller.MorphManager != null && _controller.Model.Mesh != null) { var workDirectory = _controller.Settings.GetWorkDirectory(); var mesh = _controller.Model.Mesh; _controller.MorphManager.DeformedMesh(workDirectory, mesh); _controller.UpdateAfterMeshMorping(); } Close(); } } }