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.

302 lines
13 KiB
C#

using Rs.Controls;
using Rs.DataAccess;
using Rs.Framework;
using Rs.Motion;
using Rs.MotionPlat.Commom;
using Rs.MotionPlat.Flow;
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;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
namespace Rs.MotionPlat.Recipe
{
public partial class StockTrayLocationRecipe : BaseForm
{
private TraySlot selectedSlot;
private string m_trayName = "";
DataTable dt = new DataTable();
SqliteHelper db=new SqliteHelper();
public StockTrayLocationRecipe()
{
InitializeComponent();
}
public StockTrayLocationRecipe(string trayName)
{
InitializeComponent();
m_trayName = trayName;
cameraTemplate1.ItemName = m_trayName;
}
private void BindPoints()
{
string querySql = $"select * from TrayBasePoints where trayname='{m_trayName}' and recipename='{GlobalVar.CurRecipe}'";
DataTable dtBasePoints = db.GetDataTable(querySql);
if (ObjectHelper.IsNotNullorEmpty(dtBasePoints))
{
dgvBasePoints.DataSource = dtBasePoints;
}
querySql = $"select * from TrayRunPoints where trayname='{m_trayName}' and recipename='{GlobalVar.CurRecipe}'";
DataTable dtRunPoints = db.GetDataTable(querySql);
if (ObjectHelper.IsNotNullorEmpty(dtRunPoints))
{
dgvRunPoints.DataSource = dtRunPoints;
}
}
private void StockTrayLocationRecipe_Load(object sender, EventArgs e)
{
rsTray1.HeadText= m_trayName;
string querySql = $"select * from NormalTray where trayname='{m_trayName}' and recipename='{GlobalVar.CurRecipe}'";
dt = db.GetDataTable(querySql);
if (ObjectHelper.IsNotNullorEmpty(dt))
{
DataRow dr = dt.Rows[0];
foreach (DataColumn dc in dt.Columns)
{
string columname = dc.ColumnName;
Control txt = ControlManager.FindControl(groupBox1, "txt" + columname);
if (txt != null)
{
txt.Text = dr[columname].ToString();
rsTray1.ColumnNum = int.Parse(dr["ColumnNum"].ToString());
rsTray1.RowNum = int.Parse(dr["RowNum"].ToString());
}
}
}
dgvBasePoints.AutoGenerateColumns = false;
dgvRunPoints.AutoGenerateColumns = false;
BindPoints();
}
private void btnCreateMatrix_Click(object sender, EventArgs e)
{
if (ObjectHelper.IsNotNullorEmpty(dt))
{
string deleteSql = $"delete from TrayBasePoints where trayid='{dt.Rows[0]["ID"]}'";
int rowEffect = db.ExecuteNonQuery(deleteSql);
deleteSql = $"delete from TrayRunPoints where trayid='{dt.Rows[0]["ID"]}'";
rowEffect = db.ExecuteNonQuery(deleteSql);
if (rowEffect >= 0)
{
int rowNum = int.Parse(dt.Rows[0]["RowNum"].ToString());
int colNum = int.Parse(dt.Rows[0]["ColumnNum"].ToString());
int index = 1;
for (int i = 0; i < rowNum; i++)
{
for (int j = 0; j < colNum; j++)
{
double xBase = double.Parse(dt.Rows[0]["MarkX1"].ToString()) + double.Parse(dt.Rows[0]["Mark2CenterDistX"].ToString()) + (j * double.Parse(dt.Rows[0]["SlotOffsetDistX"].ToString()));
double yBase = double.Parse(dt.Rows[0]["MarkY1"].ToString()) + double.Parse(dt.Rows[0]["Mark2CenterDistY"].ToString()) + (i * double.Parse(dt.Rows[0]["SlotOffsetDistY"].ToString()));
string insertBaseSql = $"insert into TrayBasePoints(trayid,recipename,trayname,slotindex,x,y) values('{dt.Rows[0]["id"]}','{dt.Rows[0]["recipename"]}','{dt.Rows[0]["TrayName"]}',{index},{xBase},{yBase})";
double xRun = double.Parse(dt.Rows[0]["MarkX1"].ToString()) + double.Parse(dt.Rows[0]["Mark2CenterDistX"].ToString()) + (j * double.Parse(dt.Rows[0]["SlotOffsetDistX"].ToString()));
double yRun = double.Parse(dt.Rows[0]["MarkY1"].ToString()) + double.Parse(dt.Rows[0]["Mark2CenterDistY"].ToString()) + (i * double.Parse(dt.Rows[0]["SlotOffsetDistY"].ToString()));
string insertRunSql = $"insert into TrayRunPoints(trayid,recipename,trayname,slotindex,x,y) values('{dt.Rows[0]["id"]}','{dt.Rows[0]["recipename"]}','{dt.Rows[0]["TrayName"]}',{index},{xRun},{yRun})";
db.ExecuteNonQuery(insertBaseSql);
db.ExecuteNonQuery(insertRunSql);
index++;
}
}
BindPoints();
}
}
}
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 TrayBasePoints where trayid={dt.Rows[0]["id"]} 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;
}
if (subMenu.Text.IndexOf("TakeProduct") >= 0)
{
TakeProduct(selectedSlot.Index, nozzleIndex);
}
else if (subMenu.Text.IndexOf("DumpProduct") >= 0)
{
DumpProduct(selectedSlot.Index, nozzleIndex);
}
}
}
}
}
private void btnTeach_Click(object sender, EventArgs e)
{
Button btnTeach = (Button)sender;
string name = btnTeach.Name.Replace("btnTeach", "");
Control txt = ControlManager.FindControl(groupBox1, "txt" + name);
if (txt != null)
{
double pos = Ops.GetCurPosition(txt.Tag.ToString());
txt.Text = pos.ToString("0.000");
if (ObjectHelper.IsNotNullorEmpty(dt))
{
string updateSql = $"update NormalTray set {txt.Name.Replace("txt", "")}={pos} where recipename='{GlobalVar.CurRecipe}' and trayname='{dt.Rows[0]["trayname"]}'";
if (db.ExecuteNonQuery(updateSql) > 0)
{
Msg.ShowInfo("teach point ok!");
}
}
}
}
private void btnMove_Click(object sender, EventArgs e)
{
Button btnTeach = (Button)sender;
string name = btnTeach.Name.Replace("btnMove", "");
Control txt = ControlManager.FindControl(groupBox1, "txt" + name);
if (txt != null)
{
double targetPos = double.Parse(txt.Text);
DialogResult dr = Msg.ShowQuestion($"are you sure to move axis {txt.Tag} to postion at:{targetPos}?");
if (dr == DialogResult.OK)
{
Motion.ErrorCode errCode = AxisControl.GetAxis(txt.Tag.ToString()).MovePos(targetPos, 4);
if (errCode != Motion.ErrorCode.Ok)
{
Msg.ShowError($"axis {txt.Tag.ToString()} move fail {errCode}");
}
}
}
}
private void btnSavePoint_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
string name = button.Name.Replace("btnSave", "");
Control txt = ControlManager.FindControl(this, "txt" + name);
if (txt != null)
{
if(ObjectHelper.IsNotNullorEmpty(dt))
{
string updateSql = $"update NormalTray set {name}={txt.Text} where id={dt.Rows[0]["id"]}";
if(db.ExecuteNonQuery(updateSql)>0)
{
Msg.ShowInfo("save ok!");
}
}
}
}
/// <summary>
/// 取料
/// </summary>
private void TakeProduct(int slotIndex, int nozzleIndex)
{
ErrorCode errCode = ErrorCode.Ok;
Task.Run(() => {
while (true)
{
if (Ops.IsStop("LoadX") && Ops.IsStop("LoadY"))
break;
Thread.Sleep(10);
}
DialogResult dr = Msg.ShowQuestion("Are you sure to take product?", MessageBoxButtons.OKCancel);
if (dr == DialogResult.OK)
{
errCode = AxisControl.GetAxis($"NozzleZ{nozzleIndex}").MovePos(SysConfigParam.GetValue<double>($"TrayNozzle{nozzleIndex}TakeHeight"), GlobalVar.WholeSpeed);
if (errCode > ErrorCode.Ok) return;
while (!Ops.IsStop($"NozzleZ{nozzleIndex}"))
{
Thread.Sleep(10);
}
Ops.On($"{nozzleIndex}号吸嘴真空吸电磁阀");
Thread.Sleep(200);
errCode = AxisControl.GetAxis($"NozzleZ{nozzleIndex}").MovePos(0, GlobalVar.WholeSpeed);
if (errCode > ErrorCode.Ok) return;
while (!Ops.IsStop($"NozzleZ{nozzleIndex}"))
{
Thread.Sleep(10);
}
}
});
}
/// <summary>
/// 放料
/// </summary>
private void DumpProduct(int slotIndex, int nozzleIndex)
{
ErrorCode errCode = ErrorCode.Ok;
Task.Run(() => {
while (true)
{
if (Ops.IsStop("LoadX") && Ops.IsStop("LoadY"))
break;
Thread.Sleep(10);
}
DialogResult dr = Msg.ShowQuestion("Are you sure to dump product?", MessageBoxButtons.OKCancel);
if (dr == DialogResult.OK)
{
errCode = AxisControl.GetAxis($"NozzleZ{nozzleIndex}").MovePos(SysConfigParam.GetValue<double>($"TrayNozzle{nozzleIndex}TakeHeight")+1, GlobalVar.WholeSpeed);
if (errCode > ErrorCode.Ok) return;
while (!Ops.IsStop($"NozzleZ{nozzleIndex}"))
{
Thread.Sleep(10);
}
Ops.Off($"{nozzleIndex}号吸嘴真空吸电磁阀");
Thread.Sleep(50);
Ops.On($"{nozzleIndex}号吸嘴真空破电磁阀");
Thread.Sleep(100);
Ops.Off($"{nozzleIndex}号吸嘴真空破电磁阀");
errCode = AxisControl.GetAxis($"NozzleZ{nozzleIndex}").MovePos(0, GlobalVar.WholeSpeed);
if (errCode > ErrorCode.Ok) return;
while (!Ops.IsStop($"NozzleZ{nozzleIndex}"))
{
Thread.Sleep(10);
}
}
});
}
}
}