You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
10 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using HalconDotNet;
using NPOI.SS.Formula.PTG;
using NPOI.SS.UserModel;
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;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
namespace Rs.MotionPlat.Recipe
{
public partial class StockTrayHaveOrNot : BaseForm
{
private TraySlot selectedSlot;
private string m_trayName = ETrayType.Input1.ToString();
DataTable dt = new DataTable();
SqliteHelper db = new SqliteHelper();
/// <summary>
/// 搜索区域
/// </summary>
HObject searchRegion;
HTuple winHandle;
HObject showImage;
string dirPth = string.Empty;
public StockTrayHaveOrNot()
{
InitializeComponent();
//cameraTemplate1.ItemName = "料仓料盘产品有无";
//cameraTemplate1.CustomDefineGrabEvent += () => {
//AxisControl.LoadX.ComparePulse(2, false);
//};
}
private void BindPoints()
{
string sql = $"select * from normaltray where trayname='{m_trayName}' and recipename='{GlobalVar.CurRecipe}'";
DataTable dt=db.GetDataTable(sql);
if(dt!=null && dt.Rows.Count>0)
{
trayStock.RowNum = int.Parse(dt.Rows[0]["RowNum"].ToString());
trayStock.ColumnNum = int.Parse(dt.Rows[0]["ColumnNum"].ToString());
}
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 StockTrayHaveOrNot_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");
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;
}
}
}
}
}
private void btnCheck_Click(object sender, EventArgs e)
{
if(searchRegion!=null && searchRegion.IsInitialized())
{
SlotProductHasOrNotResult reslut = UpCameraCheckFlow.Instance.CheckStockTrayHasProduct(ETrayType.Input1);
return;
hWindow_Final1.ClearWindow();
hWindow_Final1.HobjectToHimage(showImage);
hWindow_Final1.DispObj(searchRegion);
HOperatorSet.ReduceDomain(showImage, searchRegion, out HObject imageReduced);
HOperatorSet.Threshold(imageReduced,out HObject resultRegion,GlobalVar.StockTrayHaveOrNotGrayMin,GlobalVar.StockTrayHaveOrNotGrayMax);
HOperatorSet.AreaCenter(resultRegion,out HTuple area,out HTuple row,out HTuple col);
HOperatorSet.SetDraw(winHandle, "fill");
hWindow_Final1.DispObj(resultRegion, "red");
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 = "无产品";
}
}
//Task.Run(() =>
//{
// Invoke(new Action(() => {
// btnCheck.Enabled = false;
// }));
// if (UpCameraCheckFlow.Instance.CheckStockTrayHasProduct((img) => { himgNozzle8.HobjectToHimage(img); }))
// {
// Invoke(new Action(() => {
// lblResult.BackColor = Color.Lime;
// lblResult.Text = "OK";
// }));
// }
// else
// {
// Invoke(new Action(() => {
// lblResult.BackColor = Color.Red;
// lblResult.Text = "NG";
// }));
// }
// Invoke(new Action(() => {
// btnCheck.Enabled = true;
// }));
//});
}
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())
{
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 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<string>("UpCameraName"))
{
HOperatorSet.RotateImage(image, out showImage, SysConfigParam.GetValue<double>("UpCameraRotate"), "constant");
}
hWindow_Final1.HobjectToHimage(showImage);
}
}
}
}