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; namespace A032.Process.Calibration { [Device("Calibration_9P_Dynamic", "动态9点标定", EnumHelper.DeviceAttributeType.OperationConfigCtrl)] public partial class CtrlCalib9PDynamic : UserControl//, IConfigCtrl { PubSubCenter PubSubCenter = PubSubCenter.GetInstance(); AutoResetEvent _confirmHandle = new AutoResetEvent(false); public CtrlCalib9PDynamic() { InitializeComponent(); } public CtrlCalib9PDynamic(ProcessControl process, CalibrationConfigCollection calibConfig, AGVBindUnit bind, PathPosition position, Action commuAction, Action finalCalculation) { InitializeComponent(); ProcessControl = process; Config = calibConfig as CalibrationConfigCollection; Camera = bind.Camera as CameraBase; Bind = bind; Position = position; CommuAction = commuAction; FinalCalculation = finalCalculation; } AGVBindUnit Bind { get; set; } PathPosition Position { get; set; } Action CommuAction; CalibrationConfigCollection Config { get; set; } CameraBase Camera { get; set; } ProcessControl ProcessControl { get; set; } Action FinalCalculation { get; set; } public CalibrationConfigCollection GetConfig() { return null; } public void LoadConfig(CalibrationConfigCollection config) { lvConfigs.Items.Clear(); for (int i = 0; i < Config.Configs.Count; i++) { ListViewItem item = new ListViewItem((i + 1).ToString()); item.SubItems.Add(Config.Configs[i].GetDisplayText()); lvConfigs.Items.Add(item); Config.Configs[i].PropertyChanged -= CtrlCalib9PDynamic_PropertyChanged; Config.Configs[i].PropertyChanged += CtrlCalib9PDynamic_PropertyChanged; } if (lvConfigs.Items.Count > 0) { lvConfigs.Items[0].Selected = true; } } 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.Configs.Count; i++) { if (Config.Configs[i].Image != null) { lvConfigs.Items[i].BackColor = Color.GreenYellow; } else { lvConfigs.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.UpdateShowImage -= Camera_UpdateShowImage; Camera.UpdateShowImage += Camera_UpdateShowImage; } LoadConfig(Config); PubSubCenter.Subscribe(PubTag.CalibStepDone.ToString(), CalibStepDone); PubSubCenter.Subscribe(PubTag.CalibAllDone.ToString(), CalibAllDone); } public void RemoveHandles() { if (Camera != null) { Camera.UpdateShowImage -= 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, string imagePath) { _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.Configs[index].Image); _canvas.Elements.Clear(); CrossHair ch = new CrossHair(new CalibrationPoint(Config.Configs[index].ImageMarkPoint, Config.Configs[index].CurrentPlatPoint)); _canvas.Elements.Add(ch); tsslInfo.Text = $"步骤{index + 1}完成"; if (chkManualConfirm.Checked) { this.Invoke(new Action(() => btnContinueCalib.Visible = true)); ; _confirmHandle.WaitOne(); } return null; } int _selectedStepIndex = -1; private void lvConfigs_SelectedIndexChanged(object sender, EventArgs e) { if (lvConfigs.SelectedItems.Count <= 0) return; _selectedStepIndex = lvConfigs.SelectedIndices[0]; var stepConfig = Config.Configs[_selectedStepIndex]; propGridConfig.SelectedObject = stepConfig; tsslStepHint.Text = $"{(_selectedStepIndex + 1).ToString()}/{Config.Configs.Count}"; if (stepConfig.Image != null) { _canvas.LoadImage(stepConfig.Image); _canvas.Elements.Clear(); _canvas.Elements.Add(new CrossHair(new CalibrationPoint(stepConfig.ImageMarkPoint, stepConfig.CurrentPlatPoint))); } } private void btnStartCalib_Click(object sender, EventArgs e) { //if (chkOfflineCalib.Checked) //{ // //ProcessControl.Calibration_Pick_9P_Dynamic_Offline(Config.Configs); //} //else //{ // //ProcessControl.SendCalibStartSignal(Config.TriggerAddress); //} ProcessControl.MultipleStepsProcess(Config, Bind, CommuAction); } private void btnLoadOfflineImages_Click(object sender, EventArgs e) { Config.Configs.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}"; ProcessControl.SingleStepProcess(config, CommuAction, Bind, Position.PositionNo, _selectedStepIndex); tsslInfo.Text = $"单步运算完成。标记点坐标:{config.ImageMarkPoint.X},{config.ImageMarkPoint.Y}"; } private void btnCalcuMatrix_Click(object sender, EventArgs e) { FinalCalculation.Invoke(Config, Bind, Position); } private void btnSnap_Click(object sender, EventArgs e) { CalibrationConfig config = propGridConfig.SelectedObject as CalibrationConfig; Camera.UploadOperationConfig(config.CameraOpConfig); Camera.Snapshot(config.CameraOpConfig, out HObject hImage); hImage.Dispose(); } 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; } } }