using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Bro.Common.Helper; using Bro.Common.Interface; using Bro.Common.Base; using Bro.Common.UI; using Bro.Common.PubSub; using static Bro.Common.Helper.EnumHelper; using Bro.Common.Model; using HalconDotNet; using System.Threading; using System.Collections.Specialized; namespace Bro.Common.Calibration { public partial class CalibrationDynamic9PointFrm : Form { //PubSubCenter PubSubCenter = PubSubCenter.GetInstance(); AutoResetEvent _confirmHandle = new AutoResetEvent(false); public CalibrationDynamic9PointFrm() { InitializeComponent(); } public CalibrationDynamic9PointFrm(CameraBase camera, PLCBase plc, CalibrationDynamic9PointConfig config) { InitializeComponent(); Camera = camera; PLC = plc; Config = config as CalibrationDynamic9PointConfig; } public CalibrationDynamic9PointConfig Config { get; set; } public CameraBase Camera { get; set; } public PLCBase PLC { get; set; } public CalibrationDynamic9PointConfig GetConfig() { return null; } public void LoadConfig(CalibrationDynamic9PointConfig config) { FillResults(); if (lvResults.Items.Count > 0) { lvResults.Items[0].Selected = true; } pgCameraConfig.SelectedObject = config.CameraOpConfig; } private void FillResults() { if (this.InvokeRequired) { this.Invoke(new Action(() => FillResults())); } else { lvResults.Items.Clear(); for (int i = 0; i < Config.Results.Count; i++) { ListViewItem item = new ListViewItem(Config.Results[i].StepNum.ToString()); item.SubItems.Add(Config.Results[i].GetDisplayText()); item.Tag = Config.Results[i]; item.BackColor = Config.Results[i].IsDone ? Color.GreenYellow : Color.Transparent; //if (Config.Results[i].Image != null) //{ // item.BackColor = Color.GreenYellow; //} //else //{ // item.BackColor = Color.Transparent; //} Config.Results[i].PropertyChanged -= CtrlCalib9PDynamic_PropertyChanged; Config.Results[i].PropertyChanged += CtrlCalib9PDynamic_PropertyChanged; lvResults.Items.Add(item); } } } private void CtrlCalib9PDynamic_PropertyChanged(object sender, PropertyChangedEventArgs e) { FillResults(); } //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].Image != null) // { // lvResults.Items[i].BackColor = Color.GreenYellow; // } // else // { // lvResults.Items[i].BackColor = Color.Transparent; // } // } // } //} Canvas _canvas = new Canvas(); private void CtrlCalib9PDynamic_Load(object sender, EventArgs e) { _canvas.IsShowElementList = true; _canvas.Dock = DockStyle.Fill; plImage.Controls.Add(_canvas); RemoveHandles(); if (Camera != null) { Camera.OnImageUpdated -= Camera_UpdateShowImage; Camera.OnImageUpdated += Camera_UpdateShowImage; } Config.Results.OnItemChanged = OnResultsListChanged; LoadConfig(Config); } private void OnResultsListChanged(NotifyCollectionChangedAction obj) { if (this.InvokeRequired) { this.Invoke(new Action(() => FillResults())); } else { FillResults(); } } public void RemoveHandles() { if (Camera != null) { Camera.OnImageUpdated -= Camera_UpdateShowImage; } //PubSubCenter.RemoveSubscriber(PubTag.CalibStepDone.ToString(), CalibStepDone); //PubSubCenter.RemoveSubscriber(PubTag.CalibAllDone.ToString(), CalibAllDone); chkContinueMode.Checked = false; } AutoResetEvent _imageShowedHandle = new AutoResetEvent(false); private void Camera_UpdateShowImage(CameraBase camera, Bitmap image) { if (this.InvokeRequired) { this.Invoke(new Action(Camera_UpdateShowImage), camera, image); } else { _canvas.LoadImage(image); _imageShowedHandle.Set(); } } //private object CalibAllDone(ISubscriber arg1, object arg2, object arg3) //{ // string msg = arg3.ToString(); // tsslInfo.Text = msg; // return null; //} //private object CalibStepDone(ISubscriber arg1, object arg2, object arg3) //{ // int index = Convert.ToInt32(arg2); // _canvas.LoadImage(Config.Results[index].Image); // _canvas.Elements.Clear(); // CrossHair ch = new CrossHair(Config.Results[index].PointResult); // _canvas.Elements.Add(ch); // tsslInfo.Text = $"步骤{index + 1}完成"; // if (chkManualConfirm.Checked) // { // this.Invoke(new Action(() => btnContinueCalib.Visible = true)); ; // _confirmHandle.WaitOne(); // } // return null; //} private void lvConfigs_SelectedIndexChanged(object sender, EventArgs e) { if (lvResults.SelectedItems.Count <= 0) return; var result = lvResults.SelectedItems[0].Tag as CalibrationResult; tsslStepHint.Text = $"{(result.StepNum)}/{Config.TotalSteps}"; if (result.Image != null) { _canvas.LoadImage(result.Image); _canvas.Elements.Clear(); _canvas.Elements.Add(new CrossHair(result.PointResult)); } } private void btnStartCalib_Click(object sender, EventArgs e) { if (OnStartCalibration == null) { throw new ProcessException("StartCalibration不能为空"); } OnStartCalibration(PLC, Config.TriggerAddress); } private void btnStepRun_Click(object sender, EventArgs e) { int stepNum = 1; if (lvResults.SelectedItems.Count > 0) { var step = lvResults.SelectedItems[0].Tag as CalibrationResult; stepNum = step.StepNum; } var config = pgCameraConfig.SelectedObject as CameraOprerationConfigBase; if (OnCalculateStep == null) { MessageBox.Show("单步运算函数为空"); return; } try { CalibrationResult result = OnCalculateStep(Camera, PLC, Config, stepNum); ShowCalibStepDone(result); } catch (Exception ex) { MessageBox.Show(ex.GetExceptionMessage()); } } private void btnCalcuMatrix_Click(object sender, EventArgs e) { if (OnCalculateAll == null) { MessageBox.Show("标定结果运算函数为空"); return; } try { string result = OnCalculateAll(Camera, Config); ShowCalibAllDone(result); } catch (Exception ex) { MessageBox.Show(ex.GetExceptionMessage()); } } private void btnSnap_Click(object sender, EventArgs e) { CameraOprerationConfigBase config = pgCameraConfig.SelectedObject as CameraOprerationConfigBase; Camera.UploadOperationConfig(config); Camera.Snapshot(config, out HObject hImage); hImage.Dispose(); } private bool _isContinueMode = false; CameraOprerationConfigBase _selectedCameraOpConfig = null; private void chkContinueMode_CheckedChanged(object sender, EventArgs e) { _selectedCameraOpConfig = pgCameraConfig.SelectedObject as CameraOprerationConfigBase; _isContinueMode = chkContinueMode.Checked; if (_isContinueMode) { bool temp = _selectedCameraOpConfig.IsSaveOriginImage; _selectedCameraOpConfig.IsSaveOriginImage = false; Task.Run(() => { while (_isContinueMode) { try { Camera.UploadOperationConfig(_selectedCameraOpConfig); Camera.Snapshot(); _imageShowedHandle.WaitOne(3000); } catch (Exception) { } } _selectedCameraOpConfig.IsSaveOriginImage = temp; }); } } private void btnContinueCalib_Click(object sender, EventArgs e) { _confirmHandle.Set(); btnContinueCalib.Visible = false; } private void CalibrationDynamic9PointFrm_FormClosing(object sender, FormClosingEventArgs e) { RemoveHandles(); } #region 匿名委托 //public virtual void StartCalibration() //{ //} //public virtual void CalibMarkPoint(CameraBase camera, CalibrationDynamic9PointConfig config, int stepIndex) //{ //} //public virtual void CalculationMatrix(List configs) //{ //} public Action OnStartCalibration { get; set; } public Func OnCalculateStep { get; set; } public Func OnCalculateAll { get; set; } #endregion #region UI显示 private void ShowCalibStepDone(CalibrationResult result) { if (this.InvokeRequired) { this.Invoke(new Action(ShowCalibStepDone), result); } else { //_canvas.LoadImage(result.Image); _canvas.Elements.Clear(); CrossHair ch = new CrossHair(result.PointResult); _canvas.Elements.Add(ch); tsslInfo.Text = $"步骤{result.StepNum}完成,{result.PointResult.GetDisplayText()}"; if (chkManualConfirm.Checked) { btnContinueCalib.Visible = true; } if (chkManualConfirm.Checked) { _confirmHandle.WaitOne(); } } } private void ShowCalibAllDone(string msg) { if (this.InvokeRequired) { this.Invoke(new Action(ShowCalibAllDone), msg); } else { tsslInfo.Text = msg; } } #endregion private void btnClearResult_Click(object sender, EventArgs e) { Config.Results.ForEach(r => { r.Image = null; r.PointResult = new CalibrationPoint(); r.IsDone = false; }); } } }