using ChoiceTech.Halcon.Control; using HalconDotNet; using Rs.Camera; using Rs.Controls; using Rs.DataAccess; using Rs.Framework; using Rs.MotionPlat.Commom; using Rs.MotionPlat.Flow; using Rs.MotionPlat.Flow.Camera; using Rs.MotionPlat.Flow.SubFlow; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Rs.MotionPlat.Recipe { public partial class TurnoverTrayHaveOrNot : BaseForm { private TraySlot selectedSlot; string m_trayName = "Turnover"; SqliteHelper db = new SqliteHelper(); /// /// 搜索区域 /// HObject searchRegion; HTuple winHandle; HObject showImage; string dirPth = string.Empty; public TurnoverTrayHaveOrNot() { InitializeComponent(); } private void BindPoints() { string querySql = $"select * from TrayPoints where trayname='{m_trayName}' and recipename='{GlobalVar.CurRecipe}' order by slotindex"; DataTable dtBasePoints = db.GetDataTable(querySql); if (ObjectHelper.IsNotNullorEmpty(dtBasePoints)) { dgvBasePoints.DataSource = dtBasePoints; } } private void TurnoverTrayHaveOrNot_Load(object sender, EventArgs e) { dirPth = Path.Combine("recipe", GlobalVar.CurRecipe, "周转盘产品有无"); winHandle = hWindow_Final1.hWindowControl.HalconWindow; dgvBasePoints.AutoGenerateColumns = false; BindPoints(); PositionHelper.BindPosition(groupBox4, "int"); string filePath = Path.Combine(dirPth, "default.bmp"); if(File.Exists(filePath)) { HOperatorSet.ReadImage(out showImage, filePath); hWindow_Final1.HobjectToHimage(showImage); if (File.Exists(Path.Combine(dirPth, "搜索区域.reg"))) { //把区域都出来 HOperatorSet.ReadRegion(out searchRegion, Path.Combine(dirPth, "搜索区域.reg")); //show region HOperatorSet.SetDraw(winHandle, "margin"); HOperatorSet.SetColor(winHandle, "green"); hWindow_Final1.DispObj(searchRegion, "green"); } } } private void rsTray1_SlotClickEvent(TraySlot arg1, MouseEventArgs arg2) { selectedSlot = arg1; contextMenuStrip1.Show(MousePosition.X, MousePosition.Y); } private void goToolStripMenuItem_Click(object sender, EventArgs e) { if (selectedSlot != null) { int nozzleIndex = 0; string querySql = $"select * from TrayPoints where trayname='{m_trayName}' and slotindex={selectedSlot.Index}"; DataTable dtGoPosition = db.GetDataTable(querySql); if (ObjectHelper.IsNotNullorEmpty(dtGoPosition)) { double targetX = double.Parse(dtGoPosition.Rows[0]["X"].ToString()); double targetY = double.Parse(dtGoPosition.Rows[0]["Y"].ToString()); ToolStripMenuItem subMenu = sender as ToolStripMenuItem; ToolStripMenuItem parentMenu = (ToolStripMenuItem)subMenu.OwnerItem; if (parentMenu.Text.IndexOf("Nozzle") >= 0) { nozzleIndex = int.Parse(parentMenu.Text.Replace("Nozzle", "").Replace("GO", "")); SlotPoint dist = TrayPointManager.GetDistToNozzle1(nozzleIndex); targetX += dist.X; targetY += dist.Y; } DialogResult dr = Msg.ShowQuestion($"Are you sure to move loadx to {targetX},loady to {targetY}?"); if (dr == DialogResult.OK) { Motion.ErrorCode errCode = AxisControl.GetAxis("LoadX").MovePos(targetX, 4); if (errCode > Motion.ErrorCode.Ok) { Msg.ShowError($"axis loadx move fail,ret={errCode}"); return; } errCode = AxisControl.GetAxis("LoadY").MovePos(targetY, 4); if (errCode > Motion.ErrorCode.Ok) { Msg.ShowError($"axis loady move fail,ret={errCode}"); return; } } } } } public void txtSysParam_KeyUp(object sender, KeyEventArgs e) { UpdateSysParam((TextBox)sender); } private void btnDrawRegion_Click(object sender, EventArgs e) { hWindow_Final1.Focus(); HalconTool.SetDisplayFont(winHandle, 20, "sans", new HTuple("true"), new HTuple("false")); HalconTool.DispMessage(winHandle, "请在图像窗口中绘制ROI区域,点击右键结束", 20, 20, "red", "false"); HOperatorSet.SetDraw(winHandle, "margin"); HOperatorSet.SetColor(winHandle, "green"); hWindow_Final1.DrawModel = true; HOperatorSet.GenEmptyObj(out searchRegion); HOperatorSet.DrawRectangle1(winHandle, out HTuple row1, out HTuple column1, out HTuple row2, out HTuple column2); HOperatorSet.GenRectangle1(out searchRegion, row1, column1, row2, column2); hWindow_Final1.DispObj(searchRegion, "green"); hWindow_Final1.DrawModel = false; } private void btnSaveRegion_Click(object sender, EventArgs e) { if (searchRegion != null && searchRegion.IsInitialized()) { if(!Directory.Exists(dirPth)) { Directory.CreateDirectory(dirPth); } HOperatorSet.WriteImage(showImage, "bmp", 0, Path.Combine(dirPth, "default.bmp")); HOperatorSet.WriteRegion(searchRegion, Path.Combine(dirPth, "搜索区域.reg")); Msg.ShowInfo("save success!!!"); } else { Msg.ShowInfo("please select region"); } } private void btnCheck_Click(object sender, EventArgs e) { if (searchRegion != null && searchRegion.IsInitialized()) { SlotProductHasOrNotResult result = UpCameraCheckFlow.Instance.CheckTurnoverTrayHasProduct(null, 1, false); return; hWindow_Final1.ClearWindow(); hWindow_Final1.HobjectToHimage(showImage); HOperatorSet.ReduceDomain(showImage, searchRegion, out HObject imageReduced); HOperatorSet.Threshold(imageReduced, out HObject resultRegion, GlobalVar.TurnoverTrayHaveOrNotGrayMin, GlobalVar.TurnoverTrayHaveOrNotGrayMax); HOperatorSet.AreaCenter(resultRegion, out HTuple area, out HTuple row, out HTuple col); HOperatorSet.SetDraw(winHandle, "fill"); hWindow_Final1.DispObj(resultRegion, "green"); lblCheckResult.Text = area.D.ToString("0.00"); if(area.D>GlobalVar.TurnoverTrayHaveOrNotGrayArea) { lblProductCheckResult.BackColor = Color.Green; lblProductCheckResult.Text = "有产品"; } else { lblProductCheckResult.BackColor = Color.Red; lblProductCheckResult.Text = "无产品"; } } } private void btnGrab_Click(object sender, EventArgs e) { string cameraName = "upCamera"; ImageProcess.ClearManualTrigger(); HikCamera.Instance.SetTrigger(cameraName, ETriggerMode.Manual); HikCamera.Instance.SetExposure(cameraName, GlobalVar.UpCameraExposureTime); HikCamera.Instance.SetGain(cameraName, GlobalVar.UpCameraGain); AxisControl.LoadX.ComparePulse(2, false); bool cameraOk = ImageProcess.manualTriggerEvent.WaitOne(3000); if (cameraOk) { HObject image = ImageProcess.GetManualImage(); if (cameraName == SysConfigParam.GetValue("UpCameraName")) { HOperatorSet.RotateImage(image, out showImage, SysConfigParam.GetValue("UpCameraRotate"), "constant"); } hWindow_Final1.HobjectToHimage(showImage); } } } }