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.

272 lines
11 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;
namespace Rs.MotionPlat.Recipe
{
public partial class ExceptionTrayConfig : BaseForm
{
SqliteHelper db = new SqliteHelper();
DataTable dt = new DataTable();
/// <summary>
/// 被选中的穴位
/// </summary>
private TraySlot selectedSlot;
public ExceptionTrayConfig()
{
InitializeComponent();
}
private void btnCreateMatrix_Click(object sender, EventArgs e)
{
string deleteSql = $"delete from traypoints where recipename='{GlobalVar.CurRecipe}' and trayname='{dt.Rows[0]["trayname"]}'";
db.ExecuteNonQuery(deleteSql);
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 xRun = double.Parse(dt.Rows[0]["MarkX1"].ToString()) + (j * double.Parse(dt.Rows[0]["SlotOffsetDistX"].ToString()));
double yRun = double.Parse(dt.Rows[0]["MarkY1"].ToString()) + (i * double.Parse(dt.Rows[0]["SlotOffsetDistY"].ToString()));
string insertRunSql = $"insert into TrayPoints(trayid,recipename,trayname,slotindex,x,y) values({dt.Rows[0]["id"]},'{GlobalVar.CurRecipe}', '{dt.Rows[0]["TrayName"]}',{index},{xRun},{yRun})";
db.ExecuteNonQuery(insertRunSql);
index++;
}
}
BindPoints();
}
private void BindPoints()
{
string querySql = $"select * from normaltray where recipename='{GlobalVar.CurRecipe}' and trayname='ExceptiontTray'";
dt = db.GetDataTable(querySql);
if (ObjectHelper.IsNotNullorEmpty(dt))
{
txtMarkX1.Text = dt.Rows[0]["MarkX1"].ToString();
txtMarkY1.Text = dt.Rows[0]["MarkY1"].ToString();
txtSlotOffsetDistX.Text = dt.Rows[0]["SlotOffsetDistX"].ToString();
txtSlotOffsetDistY.Text = dt.Rows[0]["SlotOffsetDistY"].ToString();
}
querySql = $"select * from TrayPoints where trayname='{dt.Rows[0]["TrayName"]}'";
DataTable dtRunPoints = db.GetDataTable(querySql);
if (ObjectHelper.IsNotNullorEmpty(dtRunPoints))
{
dgvRunPoints.DataSource = dtRunPoints;
}
}
private void ExceptionTrayConfig_Load(object sender, EventArgs e)
{
dgvRunPoints.AutoGenerateColumns = false;
BindPoints();
LoadSysParam(groupBox4);
}
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 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 turnoverx to {targetX},turnovery to {targetY}?");
if (dr == DialogResult.OK)
{
Motion.ErrorCode errCode = AxisControl.GetAxis("TurnoverX").MovePos(targetX, 4);
if (errCode > Motion.ErrorCode.Ok)
{
Msg.ShowError($"axis TurnoverX move fail,ret={errCode}");
return;
}
errCode = AxisControl.GetAxis("TurnoverY").MovePos(targetY, 4);
if (errCode > Motion.ErrorCode.Ok)
{
Msg.ShowError($"axis TurnoverY move fail,ret={errCode}");
return;
}
if (subMenu.Text.IndexOf("DumpProduct") >= 0)
{
DumpProduct(selectedSlot.Index, nozzleIndex);
}
}
}
}
}
/// <summary>
/// 放料
/// </summary>
private void DumpProduct(int slotIndex, int nozzleIndex)
{
ErrorCode errCode = ErrorCode.Ok;
Task.Run(() => {
//开吸嘴真空
//关闭周转盘真空吸
//打开周转盘真空破
//关闭真空破
while (true)
{
if (Ops.IsStop("TurnoverX") && Ops.IsStop("TurnoverY"))
break;
Thread.Sleep(10);
}
DialogResult dr = Msg.ShowQuestion("Are you sure to dump product?", MessageBoxButtons.OKCancel);
if (dr == DialogResult.OK)
{
errCode = AxisControl.GetAxis($"TurnoverZ").MovePos(SysConfigParam.GetValue<double>($"TurnoverNozzle{nozzleIndex}TakeHeight"), GlobalVar.WholeSpeed);
if (errCode > ErrorCode.Ok) return;
while (!Ops.IsStop($"TurnoverZ"))
{
Thread.Sleep(10);
}
Ops.On("独立吸头气缸");
while(!Ops.IsOn("独立吸头气缸动位"))
{
Thread.Sleep(10);
}
//关闭真空吸
Ops.Off($"独立吸头真空吸");
Thread.Sleep(100);
Ops.On($"独立吸头真空破");
Thread.Sleep(50);
Ops.Off("独立吸头气缸");
while (!Ops.IsOn("独立吸头气缸原位"))
{
Thread.Sleep(10);
}
errCode = AxisControl.GetAxis($"TurnoverZ").MovePos(0, GlobalVar.WholeSpeed);
if (errCode > ErrorCode.Ok) return;
while (!Ops.IsStop($"TurnoverZ"))
{
Thread.Sleep(10);
}
}
});
}
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)
{
BindPoints();
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!");
}
}
}
}
private void txtPoint_KeyUp(object sender, KeyEventArgs e)
{
TextBox txtKey= (TextBox)sender;
if(e.KeyCode== Keys.Enter)
{
string key = txtKey.Name.Replace("txt", "");
string value = txtKey.Text;
if(SysConfigParam.Update(key, value)>0)
{
Msg.ShowInfo("Save success!");
}
}
}
private void rsTray1_SlotClickEvent_1(TraySlot arg1, MouseEventArgs arg2)
{
selectedSlot = arg1;
contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
}
}
}