using Bro.Common.Base; using Bro.Common.Base.UI; using Bro.Common.Factory; using Bro.Common.Interface; using Bro.Common.Model; using Bro.UI.Model.Winform; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace Bro.Process { public partial class FrmCalibration_9P : Form { public FrmCalibration_9P() { InitializeComponent(); } public FrmCalibration_9P(CameraBase camera, IMotion motionDevice, CalibrationConfig config, Action singleResultAction, Func finalCalculation, Action resetHalconToolAction, string methodName) { InitializeComponent(); this.camera = camera; this.motionDevice = motionDevice; Config = config; this.singleResultAction = singleResultAction; this.finalCalculation = finalCalculation; this.resetHalconToolAction = resetHalconToolAction; this.methodName = methodName; this.Text += $" -- {methodName}"; } public CalibrationConfig Config { get; set; } private Action singleResultAction; private Func finalCalculation; private Action resetHalconToolAction; string methodName = ""; CtrlCameraRunBase runCtrl = null; CameraBase camera = null; IMotion motionDevice = null; AutoResetEvent _confirmHandle = new AutoResetEvent(false); public void LoadConfig() { lvResults.Items.Clear(); for (int i = 0; i < Config.Results.Count; i++) { ListViewItem item = new ListViewItem((i + 1).ToString()); item.SubItems.Add(Config.Results[i].GetDisplayText()); lvResults.Items.Add(item); Config.Results[i].PropertyChanged -= CtrlCalib9PDynamic_PropertyChanged; Config.Results[i].PropertyChanged += CtrlCalib9PDynamic_PropertyChanged; } if (lvResults.Items.Count > 0) { lvResults.Items[0].Selected = true; } propGridConfig.SelectedObject = Config.CameraOpConfig; } private void CtrlCalib9PDynamic_PropertyChanged(object sender, PropertyChangedEventArgs e) { OnCalibPropertyChanged(); } private void OnCalibPropertyChanged() { if (this.InvokeRequired) { this.Invoke(new Action(() => OnCalibPropertyChanged())); } else { for (int i = 0; i < Config.Results.Count; i++) { if (Config.Results[i].ImageMarkPoint.X != 0 || Config.Results[i].ImageMarkPoint.Y != 0) { lvResults.Items[i].BackColor = Color.GreenYellow; } else { lvResults.Items[i].BackColor = Color.Transparent; } } } } private void FrmCalib9PDynamic_Load(object sender, EventArgs e) { runCtrl = UIFactory.GetRunCtrl(camera) as CtrlCameraRunBase; if (runCtrl is UserControl uc) { uc.Dock = DockStyle.Fill; plRunCtrl.Controls.Add(uc); } LoadConfig(); } /// /// 标定结束时动作 /// /// /// public void OnCalibFinalDone(string msg, AutoWaitConfirm saveConfigConfirm) { tsslInfo.Text = msg; saveConfigConfirm.WaitResult = MessageBox.Show("选择Yes将标定结果写入配置,选择No不修改当前配置\r\n该操作不保存当前配置,需要保存配置请退出标定界面后手动保存", "写入配置提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes; saveConfigConfirm.WaitHandle.Set(); } public void OnCalibStepDone(int sequence) { tsslInfo.Text = $"步骤{sequence}完成,{Config.Results[sequence - 1].GetDisplayText()}"; ; if (chkManualConfirm.Checked) { this.Invoke(new Action(() => btnContinueCalib.Visible = true)); ; _confirmHandle.WaitOne(); } } int _selectedStepIndex = -1; private void lvResults_SelectedIndexChanged(object sender, EventArgs e) { if (lvResults.SelectedItems.Count <= 0) return; _selectedStepIndex = lvResults.SelectedIndices[0]; } private void lvResults_MouseDoubleClick(object sender, MouseEventArgs e) { if (lvResults.SelectedItems.Count <= 0) return; _selectedStepIndex = lvResults.SelectedIndices[0]; var result = Config.Results[_selectedStepIndex]; if (result.Image != null) { runCtrl.CvImage.LoadImage(result.Image); runCtrl.CvImage.Elements.Clear(); runCtrl.CvImage.Elements.Add(new PointIndicator() { Center = new Common.Model.CustomizedPoint(result.ImageMarkPoint) }); } } private void btnStartCalib_Click(object sender, EventArgs e) { //if (chkOfflineCalib.Checked) //{ // ProcessControl.Calibration_Pick_9P_Dynamic_Offline(Config.Configs); //} //else //{ // ProcessControl.SendCalibStartSignal(Config.TriggerAddress); //} } private void btnLoadOfflineImages_Click(object sender, EventArgs e) { Config.Results.ForEach(c => { if (!string.IsNullOrWhiteSpace(c.OfflineImagePath)) { c.Image = (Bitmap)Image.FromFile(c.OfflineImagePath); } }); tsslInfo.Text = "离线图片载入完成"; } private void btnStepRun_Click(object sender, EventArgs e) { //CalibrationConfig config = propGridConfig.SelectedObject as CalibrationConfig; //if (!chkOfflineRun.Checked) //{ // ProcessControl.CalibMarkPoint(Camera, config, _selectedStepIndex + 1); //} //else //{ // ProcessControl.CalibMarkPoint(_canvas.MAP, config, _selectedStepIndex + 1); //} //tsslInfo.Text = $"单步运算完成。标记点坐标:{config.ImageMarkPoint.X},{config.ImageMarkPoint.Y}"; try { if (singleResultAction != null) { singleResultAction(camera, Config, methodName, _selectedStepIndex + 1); OnCalibStepDone(_selectedStepIndex + 1); } } catch (Exception ex) { MessageBox.Show("标定过程异常\r\n" + ex.Message); } } private void btnCalcuMatrix_Click(object sender, EventArgs e) { try { if (finalCalculation != null) { var saveConfigConfirm = new AutoWaitConfirm() { WaitHandle = new AutoResetEvent(false), WaitResult = false }; OnCalibFinalDone(finalCalculation(camera, motionDevice, Config, saveConfigConfirm), saveConfigConfirm); } } catch (Exception ex) { MessageBox.Show("标定结果计算异常\r\n" + ex.Message); } } private void btnSnap_Click(object sender, EventArgs e) { camera.UploadOperationConfig(Config.CameraOpConfig); camera.Snapshot(); } //private void chkContinueMode_CheckedChanged(object sender, EventArgs e) //{ // if (chkContinueMode.Checked) // { // Task.Run(() => // { // HalconRelatedCameraOprerationConfigBase config = (propGridConfig.SelectedObject as CalibrationConfig).CameraOpConfig; // bool temp = config.IsSaveImage; // config.IsSaveImage = false; // while (chkContinueMode.Checked) // { // try // { // Camera.UploadOperationConfig(config); // Camera.Snapshot(); // _imageShowedHandle.WaitOne(3000); // } // catch (Exception) // { // } // } // config.IsSaveImage = temp; // }); // } //} private void btnContinueCalib_Click(object sender, EventArgs e) { _confirmHandle.Set(); btnContinueCalib.Visible = false; } private void btnReloadAlgorithem_Click(object sender, EventArgs e) { resetHalconToolAction(Config.CameraOpConfig); } } }