geboshi_V1/LeatherProject/GeBoShi/Main.cs

961 lines
38 KiB
C#
Raw Permalink Normal View History

2024-04-01 15:23:50 +08:00
using GeBoShi.SysCtrl;
using GeBoShi.UI;
using GeBoShi.UI.Load;
2024-04-28 14:05:46 +08:00
using GeBoShi.UI.Product;
2024-04-01 15:23:50 +08:00
using GeBoShi.UI.SysSet;
2024-05-21 14:22:10 +08:00
using HZH_Controls;
using HZH_Controls.Controls;
2024-04-01 15:23:50 +08:00
using HZH_Controls.Forms;
2024-05-21 14:22:10 +08:00
using MaiMuControl.Device;
using MaiMuControl.Device.CamDev;
using Models;
using OpenCvSharp.Extensions;
2024-04-01 15:23:50 +08:00
using System;
2024-05-21 14:22:10 +08:00
using System.Collections;
2024-03-07 14:03:22 +08:00
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
2024-05-21 14:22:10 +08:00
using System.IO;
2024-03-07 14:03:22 +08:00
using System.Linq;
using System.Text;
2024-04-01 15:23:50 +08:00
using System.Threading;
2024-03-07 14:03:22 +08:00
using System.Threading.Tasks;
2024-05-21 14:22:10 +08:00
using System.Web.UI.WebControls.WebParts;
2024-03-07 14:03:22 +08:00
using System.Windows.Forms;
2024-05-21 14:22:10 +08:00
using System.Windows.Forms.DataVisualization.Charting;
2024-03-07 14:03:22 +08:00
namespace GeBoShi
{
public partial class Main : Form
{
2024-05-21 14:22:10 +08:00
object AddTextLock = new object();
bool backgroundWorkerDo;
/// <summary>
/// 显示颜色现在对应30色确保label在30个以内
/// </summary>
Color[] ChartColor = new Color[30] { Color.Red, Color.Blue, Color.Green, Color.YellowGreen, Color.Orange,
Color.Peru, Color.DarkRed,Color.AliceBlue, Color.Pink , Color.Brown,
Color.CadetBlue, Color.OrangeRed, Color.Cyan, Color.Lime, Color.Magenta, Color.Tan, Color.Sienna,
Color.AliceBlue, Color.SaddleBrown, Color.DarkBlue, Color.Firebrick,Color.Gainsboro,Color.Honeydew,Color.Khaki,Color.Lavender,
Color.LightGoldenrodYellow,Color.Navy, Color.Khaki, Color.IndianRed,Color.Lavender};
2024-03-07 14:03:22 +08:00
public Main()
{
InitializeComponent();
2024-04-01 15:23:50 +08:00
UIStyle.SetUIStyle(this);
this.uiTitel1.FatherForm = this;
this.uiTitel1.UserMenuStrip = this.menuStrip1;
this.uiTitel1.ShowContrlBox(true, true, true);
this.uiTitel1.ShowMenu();
this.DoubleBuffered = true;//设置本窗体
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
2024-05-21 14:22:10 +08:00
SysMgr.Instance.MainRuning += main_running;
SysMgr.Instance.AutoRuning += auto_runing;
#region dataGridView设置
skinDataGridView1.AllowUserToAddRows = skinDataGridView1.AllowUserToDeleteRows = false;//用户添加删除行
skinDataGridView1.AllowUserToResizeRows = false;//用户调整行大小 //显示行号与列宽度自动调整
skinDataGridView1.RowHeadersVisible = true;
skinDataGridView1.RowHeadersWidth = 50;
//dataGridView1.ColumnHeadersHeightSizeMode = dataGridView2.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
skinDataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;//数据量过百绑定太变
skinDataGridView1.RowTemplate.Height = 30;
//skinDataGridView1.RowPostPaint += (sender, e) =>//序号列头
//{
// showRowNum_onDataGrid_RowPostPaint(this.skinDataGridView1, sender, e);
//};
for (int i = 0; i < skinDataGridView1.Columns.Count; i++)//禁止点击列头排序
skinDataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
this.skinDataGridView1.CellClick += skinDataGridView1_CellClick; ;
#endregion
this.lblDefectLen.Top = toolStrip1.Top + 10;
this.lblDefectLen.Left = toolStrip1.Width - 300;
this.lblDefectSpeed.Top = toolStrip1.Top + 10 + lblDefectLen.Height + 5;
this.lblDefectSpeed.Left = toolStrip1.Width - 300;
2024-04-01 15:23:50 +08:00
}
#region
/// <summary>
/// 初始化
/// </summary>
/// <returns></returns>
private bool InitSystem()
{
//进入初始化,初始化失败则中断
InitFrm initFrm = new InitFrm();
initFrm.ShowDialog();
if (initFrm.DialogResult == DialogResult.OK)
{
//初始化成功
}
else
{
initFrm.Dispose();
return false;
}
initFrm.Dispose();
return true;
}
#endregion
#region
2024-05-21 14:22:10 +08:00
private void showRowNum_onDataGrid_RowPostPaint(DataGridView dgv, object sender, DataGridViewRowPostPaintEventArgs e)
{
Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgv.RowHeadersWidth - 4, e.RowBounds.Height);
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgv.RowHeadersDefaultCellStyle.Font, rectangle, dgv.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
}
2024-04-01 15:23:50 +08:00
private void ResetUI(bool clearSN)
{
//保持产品名称和料号
//txtProductName.Text = txtProductCode.Text = txtProductSN.Text = "";
if (clearSN)
cbPdtSN.Text = "";
this.lstLog.Items.Clear();
this.tsbtnImpStop.Enabled = false;
this.tsbtnStart.Enabled = true;
this.tsbtnStop.Enabled = false;
this.cbPdtSN.Enabled = true;
this.cbProductCode.Enabled = true;
this.cbProductBatch.Enabled = true;
this.cbProductReelNum.Enabled = true;
this.cbProductNum.Enabled = true;
2024-05-21 14:22:10 +08:00
this.pictureBox1.Image = null;
pictureBox1.Refresh();
this.pictureBox2.Image = null;
pictureBox2.Refresh();
this.pictureBox3.Image = null;
pictureBox3.Refresh();
this.pictureBox4.Image = null;
pictureBox4.Refresh();
//tslabelTime.Text = "实时测试:0.00s";
skinDataGridView1.DataSource = null;
this.skinDataGridView1.Rows.Clear();
chart1.Series.Clear();
chart1.Series.Add("SELECT");
chart1.Series["SELECT"].ChartType = SeriesChartType.Point;
chart1.Series["SELECT"].MarkerSize = 25;
chart1.Series["SELECT"].MarkerColor = Color.Black;
chart1.Series["SELECT"].MarkerStyle = MarkerStyle.Star6;
//chart1.Series["SELECT"].Enabled = false;
chart1.ChartAreas[0].AxisX.Title = "宽度(cm)";
chart1.ChartAreas[0].AxisY.Title = "长度(m)";
chart3.Series.Clear();
chart3.Series.Add("面宽");
chart3.ChartAreas[0].AxisX.Title = "长度(m)";
chart3.ChartAreas[0].AxisY.Title = "宽度(cm)";
chart3.Series["面宽"].ChartType = SeriesChartType.Line;
chart3.Series["面宽"].MarkerSize = 5;
chart3.Series["面宽"].MarkerColor = Color.Blue;
chart3.Series["面宽"].MarkerStyle = MarkerStyle.Circle;
lblDefectLen.Text = $"检测长度: 0 米";
lblDefectSpeed.Text = $"检测速度: 0 米/分钟";
2024-04-01 15:23:50 +08:00
this.Refresh();
}
#endregion
#region
private void UserRightCheck()
{
this.tsmPdtMgr.Enabled = SysMgr.Instance.UserMgr.RightControl(MaiMuControl.SysStatusMgr.UserMgr.UserRightEnum.Product);
this.tsmSysDataFind.Visible = SysMgr.Instance.UserMgr.RightControl(MaiMuControl.SysStatusMgr.UserMgr.UserRightEnum.Statistics);
this.tsmUserList.Enabled = SysMgr.Instance.UserMgr.RightControl(MaiMuControl.SysStatusMgr.UserMgr.UserRightEnum.User);
this.tsmUserRight.Enabled = SysMgr.Instance.UserMgr.RightControl(MaiMuControl.SysStatusMgr.UserMgr.UserRightEnum.Role);
if (this.tsmUserList.Enabled == false && this.tsmUserRight.Enabled == false)
this.tsmSysUserMgr.Visible = false;
else
this.tsmSysUserMgr.Visible = true;
this.tsmDebug.Enabled = SysMgr.Instance.UserMgr.RightControl(MaiMuControl.SysStatusMgr.UserMgr.UserRightEnum.Debug);
this.tsmSysSet.Enabled = SysMgr.Instance.UserMgr.RightControl(MaiMuControl.SysStatusMgr.UserMgr.UserRightEnum.ParamsSetting);
if (this.tsmDebug.Enabled == false && this.tsmSysSet.Enabled == false)
this.stmSysSet.Visible = false;
else
this.stmSysSet.Visible = true;
}
#endregion
#region
/// <summary>
/// 系统是否在运行
/// </summary>
/// <returns></returns>
private bool SystemIsRun()
{
if (SysMgr.Instance.IsRuning)
{
FrmDialog.ShowDialog(this, "设备正在运行中!", "警告", false);
//MessageBox.Show("设备正在运行中!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
return true;
}
#endregion
#region
private void Main_Load(object sender, EventArgs e)
{
//登入
if (SysMgr.Instance.LoginSystem())
{
//登入成功
if (InitSystem())
{
this.Cursor = Cursors.WaitCursor;
//this.WindowState = FormWindowState.Normal;
this.Top = 0;
this.Left = 0;
this.Width = SystemInformation.WorkingArea.Width;
this.Height = SystemInformation.WorkingArea.Height;
//this.WindowState = FormWindowState.Maximized;
//this.splitContainer1.SplitterDistance = this.Width / 3;
ResetUI(true);
this.Show();
this.tsslLoginInfo.Text = $"操作员:{SysMgr.Instance.UserMgr.LoginUser.Code}({SysMgr.Instance.UserMgr.LoginUser.Name})";
this.statusStrip1.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.HorizontalStackWithOverflow;
this.tsslLoginTime.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
this.tsslLoginInfo.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
this.tsAxisState.Text = "";
//更新数据库格式
Service.InitDB.initDB(ConfMgr.Instance.DBConStr);
//权限判断
UserRightCheck();
//开启后台
//timer1.Enabled = true;
//backgroundWorkerDo = true;
//backgroundWorker1.RunWorkerAsync();
//s_BackWorker = true;
//backgroundWorker.RunWorkerAsync();
2024-05-21 14:22:10 +08:00
2024-04-01 15:23:50 +08:00
//开启云端连接
//SysMgr.Instance.InitCloudConnect();
//开启产品服务
//SysMgr.Instance.InitPdtService();
//开启处理事件反馈
2024-05-21 14:22:10 +08:00
SysMgr.Instance.InitDefectEvent();
2024-04-01 15:23:50 +08:00
//加载产品料号
2024-05-21 14:22:10 +08:00
SysMgr.Instance.LoadProductCodeList();
2024-04-01 15:23:50 +08:00
//加载产品选择
2024-05-21 14:22:10 +08:00
LoadPdtList(SysMgr.Instance.ProductCodeList);
2024-04-01 15:23:50 +08:00
this.Cursor = Cursors.Default;
//运行前检测磁盘空间
SysMgr.CheckDisk(this);
}
else
{
Application.Exit();
}
}
else
{
Application.Exit();
}
}
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
//backgroundWorkerDo = false;
Thread.Sleep(300);
if (SystemIsRun())
{
DialogResult dr = DialogResult.Cancel;
if (SysMgr.Instance.IsInit)
{
dr = FrmDialog.ShowDialog(this, "确定要退出当前系统?", "警告", true);
}
else
dr = DialogResult.OK;
if (dr == DialogResult.Cancel)
{
e.Cancel = true;
}
else
{
Thread.Sleep(300);
if (SysMgr.Instance.IsInit)
SysMgr.Instance.Close();
//SysMgr.Instance.MainRuning -= main_running;
}
}
else
e.Cancel = true;
}
private void Main_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Maximized)
{
this.WindowState = FormWindowState.Normal;
this.Top = 0;
this.Left = 0;
this.Width = SystemInformation.WorkingArea.Width;
this.Height = SystemInformation.WorkingArea.Height;
}
2024-03-07 14:03:22 +08:00
}
2024-04-01 15:23:50 +08:00
#endregion
#region
#region 退
private void 退ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
#endregion
#region
private void tsmUserRight_Click(object sender, EventArgs e)
{
SysMgr.Instance.UserPermissiomMgr();
}
private void tsmUserList_Click(object sender, EventArgs e)
{
SysMgr.Instance.UserListMgr();
}
#endregion
#region
private void MainFrm_Paint(object sender, PaintEventArgs e)
{
this.tsbtnReset.Visible = ConfMgr.Instance.SysConfigParams.ShowResetBtn;
}
private void tsmSysSet_Click(object sender, EventArgs e)
{
if (SystemIsRun())
{
SysMgr.Instance.StatusMgr.GotoSetParams();
SysMgr.Instance.SendStatus();
SysSetFrm frm = new SysSetFrm();
frm.ShowDialog();
MainFrm_Paint(null, null);
SysMgr.Instance.StatusMgr.GotoNormalStandby();
SysMgr.Instance.SendStatus();
}
}
2024-04-28 14:05:46 +08:00
2024-05-21 14:22:10 +08:00
private void tsmDebug_Click(object sender, EventArgs e)
{
}
2024-04-01 15:23:50 +08:00
#endregion
#endregion
2024-04-28 14:05:46 +08:00
#region
private string[] pdtlistData;
private void LoadPdtList(List<string> list)
{
this.cbDefectCode.Items.Clear();
2024-05-21 14:22:10 +08:00
while (true)
{
bool bk = true;
for (int i = 0; i < list.Count; i++)
{
if (list[i] == null || string.IsNullOrEmpty(list[i]))
{
list.Remove(list[i]);
bk = false;
}
}
if (bk)
{
break;
}
}
2024-04-28 14:05:46 +08:00
this.pdtlistData = list.ToArray();
2024-05-21 14:22:10 +08:00
2024-04-28 14:05:46 +08:00
cbDefectCode.Items.AddRange(pdtlistData);//比使用DataSource速度要快一些
cbDefectCode.TextUpdate += cobList_TextUpdate;//重新绑定事件
cbDefectCode.KeyDown += CobList_KeyDown;
this.cbDefectCode.Text = "";
cbDefectCode.Focus();
cbDefectCode.SelectAll();
}
2024-04-01 15:23:50 +08:00
2024-04-28 14:05:46 +08:00
private void tsmPdtMgr_Click(object sender, EventArgs e)
{
if (SystemIsRun())
{
ProductListFrm frm = new ProductListFrm();
frm.ShowDialog();
//加载产品料号
SysMgr.Instance.LoadProductCodeList();
//加载产品选择
LoadPdtList(SysMgr.Instance.ProductCodeList);
}
}
private void CobList_KeyDown(object sender, KeyEventArgs e)
{
ComboBox ctrl = sender as ComboBox;
if (e.KeyCode == Keys.Enter)
{
if (ctrl.Items.Count == 1)
ctrl.Text = ctrl.Items[0].ToString();
}
}
private void cobList_TextUpdate(object sender, EventArgs e)
{
ComboBox ctrl = sender as ComboBox;
if (ctrl.Text != null)
{
string str = ctrl.Text; //获取cb_material控件输入内
//清空combobox
ctrl.DataSource = null;
ctrl.Items.Clear();
string[] workOrderFiltered;
workOrderFiltered = pdtlistData.Where(x => x.IndexOf(str, StringComparison.CurrentCultureIgnoreCase) != -1).ToArray();//忽略大小写
ctrl.Items.AddRange(workOrderFiltered);//比使用DataSource速度要快一些
// 不存在符合条件时
//设置光标位置,若不设置:光标位置始终保持在第一列,造成输入关键词的倒序排列
ctrl.Cursor = Cursors.Default; //保持鼠标指针原来状态,有时候鼠标指针会被下拉框覆盖,所以要进行一次设置
if (workOrderFiltered.Length > 0)
{
if (!ctrl.DroppedDown)
ctrl.DroppedDown = true; // 自动弹出下拉框
}
ctrl.SelectionStart = str.Length; // 设置光标位置,若不设置:光标位置始终保持在第一列,造成输入关键词的倒序排列
}
}
#endregion
2024-05-21 14:22:10 +08:00
#region
private void tsbtnStart_Click(object sender, EventArgs e)
{
if (!SysMgr.Instance.IsRuning)
{
//低报警自动复位
if (SysMgr.Instance.StatusMgr.Status == SystemStsEnum.Warning)
{
if (SysMgr.Instance.StatusMgr.Warning == WarningEnum.Low)
{
SysMgr.Instance.StatusMgr.ClearWarning();
SysMgr.Instance.StatusMgr.GotoNormalStandby();
}
}
//提示正在报警中
if (SysMgr.Instance.StatusMgr.Status == SystemStsEnum.Warning)
{
FrmDialog.ShowDialog(this, "系统报警中!无法启动运行!", "警告", false);
return;
}
this.Cursor = Cursors.WaitCursor;
System.Windows.Forms.Application.DoEvents();
if (SysMgr.Instance.StatusMgr.Status != SystemStsEnum.Pause)
{
ResetUI(false);
AddTextEvent("运行", $"启动自动检测流程");
this.lstLog.Refresh();
string name = this.cbProductCode.Text;
string batch = this.cbProductBatch.Text;
string reel = this.cbProductReelNum.Text;
string len = this.cbProductNum.Text;
string defname = this.cbDefectCode.Text;
if (SysMgr.Instance.StartRun(this, this.cbPdtSN.Text, ref defname, ref name, ref batch, ref reel, ref len))
{
this.tsbtnImpStop.Enabled = true;
this.tsbtnStop.Enabled = true;
this.tsbtnStart.Enabled = false;
//锁定信息
this.cbPdtSN.Enabled = false;
this.cbProductCode.Enabled = false;
this.cbProductBatch.Enabled = false;
this.cbProductReelNum.Enabled = false;
this.cbProductNum.Enabled = false;
this.cbDefectCode.Enabled = false;
//记录开始运行时间
//RunStartTime.Restart();
//重新显示
this.cbProductCode.Text = name;
this.cbProductBatch.Text = batch;
this.cbProductReelNum.Text = reel;
this.cbProductNum.Text = len;
this.cbDefectCode.Text = defname;
}
}
else //暂停状态开始
{
if (SysMgr.Instance.ReStartRun())
{
this.tsbtnStop.Enabled = true;
this.tsbtnStart.Enabled = false;
}
}
this.Cursor = Cursors.Default;
}
}
private void tsbtnReset_Click(object sender, EventArgs e)
{
}
private void tsbtnStop_Click(object sender, EventArgs e)
{
AddTextEvent("暂停", "手动暂停");
this.tsbtnStart.Enabled = true;
SysMgr.Instance.StatusMgr.GotoPause();
SysMgr.Instance.SendStatus();
SysMgr.Instance.LedPause();
}
private void tsbtnWarning_Click(object sender, EventArgs e)
{
if (SysMgr.Instance.StatusMgr.Warning != WarningEnum.Normal)
{
AddTextEvent("清除", "手动清除报警");
SysMgr.Instance.StatusMgr.ClearWarning();
SysMgr.Instance.LedReady();
if (SysMgr.Instance.IsAuto)
SysMgr.Instance.StatusMgr.GotoPause();
else
SysMgr.Instance.StatusMgr.GotoNormalStandby();
SysMgr.Instance.SendStatus();
}
}
/// <summary>
/// 结束验布
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsbtnImpStop_Click(object sender, EventArgs e)
{
AddTextEvent("结束验布", "手动结束验布");
SysMgr.Instance.DefectEnd(this);
}
int cnnnt = 10;
private void tsbGoHome_Click(object sender, EventArgs e)
{
#if false
//测试散点
chart1.Series.Clear();
chart1.ChartAreas[0].AxisX.Title = "宽度(cm)";
chart1.ChartAreas[0].AxisY.Title = "长度(m)";
chart1.Series.Add("皱印");
chart1.Series["皱印"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
chart1.Series["皱印"].MarkerSize = 10;
chart1.Series["皱印"].MarkerColor = Color.Red;
chart1.Series["皱印"].MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;
for (int i = 0; i < cnnnt; i++)
this.chart1.Series["皱印"].Points.AddXY(i%5,i);
chart1.Series.Add("划伤");
chart1.Series["划伤"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
chart1.Series["划伤"].MarkerSize = 10;
chart1.Series["划伤"].MarkerColor = Color.Blue;
chart1.Series["划伤"].MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;
for (int i = 0; i < cnnnt; i++)
this.chart1.Series["划伤"].Points.AddXY(i % 5 + 0.5, i + 0.3);
chart1.ChartAreas[0].AxisY.Maximum = ((int)(this.chart1.Series["划伤"].Points.FindMaxByValue().YValues.Max() /10) + 1) * 10 + 5;
chart1.ChartAreas[0].AxisY.ScaleView.Scroll(System.Windows.Forms.DataVisualization.Charting.ScrollType.Last);
cnnnt +=20;
MemoryStream ms = new MemoryStream();
chart1.SaveImage(ms, ChartImageFormat.Bmp);
Bitmap bmp = new Bitmap(ms);
bmp.Save("111.jpg");
ms.Close();
#endif
//测试散点
chart3.Series.Clear();
chart3.ChartAreas[0].AxisX.Title = "长度(m)";
chart3.ChartAreas[0].AxisY.Title = "宽度(cm)";
chart3.Series.Add("面宽");
string tt = "面宽";
if (chart3.Series.FindByName(tt) == null)
chart3.Series.Add("面宽");
else
{
chart3.Series["面宽"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
chart3.Series["面宽"].MarkerSize = 5;
chart3.Series["面宽"].MarkerColor = Color.Blue;
chart3.Series["面宽"].MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;
Random rm = new Random();
for (int i = 0; i < cnnnt; i++)
this.chart3.Series["面宽"].Points.AddXY(i, rm.Next(140, 160));
cnnnt += 20;
}
//chart3.Series[0].f
chart3.ChartAreas["ChartArea1"].AxisX.ScaleView.Scroll(ScrollType.Last);
}
#endregion
#region
private void lstLog_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
if (e.Index < 0) return;
try
{
string str = lstLog.Items[e.Index].ToString();
e.Graphics.DrawString(str.Substring(1), e.Font,
new SolidBrush(str[0] == 'R' ? Color.Red : (str[0] == 'Y' ? Color.Orange : Color.Black)),
e.Bounds);
}
catch (Exception ex)
{
e.Graphics.DrawString(ex.Message, e.Font,
new SolidBrush(Color.Red),
e.Bounds);
}
}
/// <summary>
/// 异步输出日志
/// </summary>
/// <param name="tag">模板标识</param>
/// <param name="msg">内容</param>
/// <param name="type"></param>
private void AddTextEvent(string tag, string msg, WarningEnum level = WarningEnum.Normal)
{
lock (AddTextLock)
{
if (tag != null && tag != "")
tag = $" - [{tag}]";
var now = DateTime.Now;
msg = now.ToString("HH:mm:ss fff") + tag + " - " + msg;
msg = (level == WarningEnum.Normal ? "B" : level == WarningEnum.Low ? "Y" : "R") + msg;
if (this.lstLog.Items.Count > 1000)
this.lstLog.Items.Clear();
lstLog.Items.Insert(0, msg);
}
}
private void main_running(object sender, MainEventArgs e)
{
ShowMessage(e);
}
private delegate void ShowMessageDelegate(MainEventArgs e);
private void ShowMessage(MainEventArgs e)
{
if (InvokeRequired)
{
ShowMessageDelegate smd = new ShowMessageDelegate(ShowMessage);
this.BeginInvoke(smd, e);
}
else
{
AddTextEvent(e.Tag, e.Message, e.Warning);
}
}
/// <summary>
/// 自动运行流程显示
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void auto_runing(object sender, RunEventArgs e)
{
ShowUIEvent(e);
}
private delegate void ShowUIEventDelegate(RunEventArgs e);
Bitmap bmp0;
Bitmap bmp1;
Bitmap bmp2;
private void ShowUIEvent(RunEventArgs e)
{
if (InvokeRequired)
{
ShowUIEventDelegate smd = new ShowUIEventDelegate(ShowUIEvent);
this.BeginInvoke(smd, e);
}
else
{
try
{
if (e.Cmd == 6) //显示主图
{
if (e.PicIndex == 1)
{
bmp1 = (Bitmap)CamDev.HImageToBitmap(e.Pic).Clone();
this.pictureBox2.Image = bmp1;
this.pictureBox2.Refresh();
}
else if (e.PicIndex == 2)
{
bmp2 = (Bitmap)CamDev.HImageToBitmap(e.Pic).Clone();
this.pictureBox3.Image = bmp2;
this.pictureBox3.Refresh();
}
else if(e.PicIndex == 0)
{
bmp0 = (Bitmap)CamDev.HImageToBitmap(e.Pic).Clone();
this.pictureBox1.Image = bmp0;
this.pictureBox1.Refresh();
}
}
else if (e.Cmd == 10) //测试结束
{
this.tsbtnStart.Enabled = true;
this.tsbtnStop.Enabled = false;
this.tsbtnImpStop.Enabled = false;
this.cbPdtSN.Enabled = true;
this.cbProductCode.Enabled = true;
this.cbProductBatch.Enabled = true;
this.cbProductReelNum.Enabled = true;
this.cbProductNum.Enabled = true;
this.cbDefectCode.Enabled = true;
}
else if (e.Cmd == 11) //显示缺陷列表
{
foreach (var rowItem in e.DataRowlist)
{
this.skinDataGridView1.Rows.Insert(0, rowItem);
if (chart1.Series.FindByName((string)rowItem[3]) == null)//rowItem[3] 为name
{
chart1.Series.Add((string)rowItem[3]);
var obj = SysMgr.Instance.GetDefectLabel((string)rowItem[3]);
int labelIndex = obj.Value<int>("id");
chart1.Series[(string)rowItem[3]].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
chart1.Series[(string)rowItem[3]].MarkerSize = 5;
chart1.Series[(string)rowItem[3]].MarkerColor = ChartColor[labelIndex];
chart1.Series[(string)rowItem[3]].MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;
}
chart1.Series[(string)rowItem[3]].Points.AddXY(rowItem[4], rowItem[5]);
}
//chart1.ChartAreas[0].AxisY.Maximum = ((int)(this.chart1.Series["划伤"].Points.FindMaxByValue().YValues.Max() / 10) + 1) * 10 + 5;
chart1.ChartAreas[0].AxisY.ScaleView.Scroll(System.Windows.Forms.DataVisualization.Charting.ScrollType.Last);
}
else if(e.Cmd == 12) //显示处理图
{
bmp0 = (Bitmap)e.Defectimg.Clone();
this.pictureBox1.Image = bmp0;
this.pictureBox1.Refresh();
}
else if (e.Cmd == 13) //显示门幅
{
//this.BeginInvoke(new System.Action(() =>
//{
List<float> ydata = new List<float>();
foreach (var Item in e.Points)
{
ydata.Add(Item[1]);
float x = Item[0] / 100; //cm -> m
this.chart3.Series["面宽"].Points.AddXY((float)Math.Round(x, 2), (float)Math.Round(Item[1], 2));
}
chart3.ChartAreas[0].AxisY.Maximum = Math.Round(ydata.Max() + 10, 0);
chart3.ChartAreas[0].AxisY.Minimum = Math.Round(ydata.Max() - 10, 0);
chart3.ChartAreas["ChartArea1"].AxisX.ScaleView.Scroll(ScrollType.Last);
//}));
}
else if (e.Cmd == 14) //显示速度
{
//this.BeginInvoke(new System.Action(() =>
//{
lblDefectLen.Text = $"检测长度: {e.Len} 米";
lblDefectSpeed.Text = $"检测速度: {e.Speed} 米/分钟";
//}));
}
else if (e.Cmd == 15) //显示处理图 new
{
bmp0 = (Bitmap)e.DefectMat.ToBitmap();
this.pictureBox1.Image = bmp0;
this.pictureBox1.Refresh();
}
Application.DoEvents();
}
catch { }
}
}
#endregion
#region
GetPN pn = new GetPN();
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
while (backgroundWorkerDo)
{
try
{
this.Invoke(new MethodInvoker(() =>
{
this.tsslLoginTime.Text = $" 当前时间:{DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss")}";
}));
//检测按钮和实时状态
if (SysMgr.Instance.IsInit)
{
bool sts = SysMgr.Instance.WarningShowLed(false);
if (sts)
{
this.Invoke(new MethodInvoker(() =>
{
this.tsbtnStart.Enabled = true;
this.tsbtnStop.Enabled = false;
}));
}
int stsno = 0;
stsno = SysMgr.Instance.ButtonIOTrg(false);
if (stsno == 1)
{
this.Invoke(new MethodInvoker(() =>
{
tsbtnStart_Click(null, null);
}));
}
else if (stsno == 2)
{
this.Invoke(new MethodInvoker(() =>
{
this.tsbtnStart.Enabled = true;
}));
SysMgr.Instance.LedPause();
}
else if (stsno == 3)
{
this.Invoke(new MethodInvoker(() =>
{
tsbtnReset_Click(null, null);
}));
}
}
//if (pn.P(SysMgr.Instance.IsRuning))
//{
// //继续计时
// RunStartTime.Start();
//}
//if (pn.N(SysMgr.Instance.IsRuning))
//{
// //停止计时
// RunStartTime.Stop();
//}
//if (SysMgr.Instance.IsRuning)
//{
// //实时显示
// RunStartTime.Stop();
// this.Invoke(new MethodInvoker(() =>
// {
// tslabelTime.Text = "实时测试:" + ((double)RunStartTime.ElapsedMilliseconds / 1000.0).ToString("0.000") + "s";
// //this.Refresh();
// }));
// RunStartTime.Start();
//}
Thread.Sleep(100);
Application.DoEvents();
}
catch { }
}
}
/// <summary>
/// 信号上升沿下降沿捕获
/// </summary>
class GetPN
{
bool _P = false;
bool _N = false;
/// <summary>
/// 判断上升沿
/// </summary>
/// <param name="Value"></param>
/// <returns></returns>
public bool P(bool Value)
{
if (Value && !_P)
{
_P = true;
return true;
}
if (!Value)
_P = false;
return false;
}
/// <summary>
/// 判断下降沿
/// </summary>
/// <param name="Value"></param>
/// <returns></returns>
public bool N(bool Value)
{
if (!Value && _N)
{
_N = false;
return true;
}
if (Value)
_N = true;
return false;
}
}
#endregion
#region
private void skinDataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0)
return;
var defectInfo = SysMgr.Instance.GetDefectInfo((long)this.skinDataGridView1.CurrentRow.Cells["colUid"].Value);
if (defectInfo == null)
{
DialogResult dr = DialogResult.Cancel;
dr = FrmDialog.ShowDialog(this, "此记录已不存在!", "警告", true);
return;
}
this.pictureBox4.Image = (defectInfo.image);
//选中
this.Invoke(new System.Action(() =>
{
if (chart1.Series != null && chart1.Series.FindByName("SELECT") != null)
{
chart1.Series["SELECT"].Points.Clear();
chart1.Series["SELECT"].Points.AddXY(defectInfo.CentreX, defectInfo.CentreY / 100);
chart1.Refresh();
}
}));
}
#endregion
2024-03-07 14:03:22 +08:00
}
}