|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- using Advantech.Motion;
- using Newtonsoft.Json;
- using ProductionControl.Device;
- using ProductionControl.Utils;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Web.UI.WebControls;
- using System.Windows.Forms;
- using System.Xml.Linq;
- using static ProductionControl.Device.ScannerDev;
-
- namespace ProductionControl.UI
- {
- public partial class UIScannerDev : UserControl
- {
- SynchronizationContext SyncContext = null;
-
- public Action<string> GetParamsEvent;
- public ScannerType devType { get; set; }
- private List<string> pathList = new List<string>();
- //
- private ScannerDevProp prop;
- private ScannerDev dev;
- public UIScannerDev()
- {
- InitializeComponent();
- propertyGrid1.PropertyValueChanged += propertyGrid1_PropertyValueChanged;
- refreshUIState();
- //获取UI线程同步上下文
- SyncContext = SynchronizationContext.Current;
- //init();
- }
- public string getParamsData()
- {
- return prop.serialize();
- }
- public void setParamsData(string json)
- {
- if (json == "") return;
- prop.deserialize(json);
- this.propertyGrid1.Refresh();
- }
- private void refreshUIState()
- {
- foreach (ToolStripItem item in this.toolStrip1.Items)
- {
- if (item.Text == "打开设备")
- item.Visible = (dev == null || !dev.IsInit);
- else
- this.propertyGrid1.Enabled = item.Enabled = !(dev == null || !dev.IsInit);
- }
- }
-
- public void init(ScannerType devType)
- {
- pathList.Clear();
- this.tbtnSave.Visible = tssSave.Visible = (GetParamsEvent != null);
- prop = new ScannerDevProp(devType);
- this.propertyGrid1.SelectedObject = prop;
- dev = new ScannerDev(prop.DeviceType);
- dev.WarningEvent = (level, info) =>
- {
- txtLog.Text = $"({level}){info}";
- };
- //DATA
- dev.ScanEventPath += new System.Action<int, string>((num, path) =>
- {
- //string path = Config.Scnaner_Bmp_SavePath + "\\" + DateTime.Now.Ticks + ".bmp";
- //bmp.Save(path, ImageFormat.Bmp);
-
- //MemoryStream ms = new MemoryStream();
- //bmp.Save(ms, ImageFormat.Bmp);
- //byte[] bs = ms.ToArray(); //GetBuffer();
- //ms.Close();
- //ms.Dispose();
- pathList.Add(path);
- prop.ImageList=pathList.ToArray();
- this.refreshUI();
-
- if (!dev.isContinuousMode)
- updateViewWin(read2Bmp(path));
- });
- dev.ScanEvent += new System.Action<int, Bitmap>((num, bmp) =>
- {
- string path;
- if(prop.DeviceType== ScannerType.GENTL)
- path = Config.appBasePath + "\\temp\\" + DateTime.Now.Ticks + ".bmp";
- else
- path = Config.appBasePath + "\\temp\\" + DateTime.Now.Ticks + ".bmp";
- bmp.Save(path, ImageFormat.Bmp);
-
- //MemoryStream ms = new MemoryStream();
- //bmp.Save(ms, ImageFormat.Bmp);
- //byte[] bs = ms.ToArray(); //GetBuffer();
- //ms.Close();
- //ms.Dispose();
-
- pathList.Add(path);
- prop.ImageList = pathList.ToArray();
- this.refreshUI();
-
- if (!dev.isContinuousMode)
- updateViewWin(bmp);
- });
-
- if (!dev.open())
- {
- this.closeDev();
- return;
- }
- if (!dev.start(IntPtr.Zero, Config.appBasePath + "\\temp\\"))
- {
- this.closeDev();
- return;
- }
- dev.getParam();
- prop.ExposureTime = dev.ExposureTime;
- prop.Gain = dev.Gain;
- prop.ResultingFrameRate = dev.ResultingFrameRate;
-
- refreshUIState();
- }
- private void updateViewWin(Bitmap bmp)
- {
- if(frm!=null && API.IsWindow(frm.Handle))
- frm.updateBmp(bmp);
- }
- private Bitmap read2Bmp(string path)
- {
- MemoryStream ms = new System.IO.MemoryStream(File.ReadAllBytes(path));
- Bitmap bmp = new Bitmap(ms);
- ms.Close();
- ms.Dispose();
-
- return bmp;
- }
- private void refreshUI()
- {
- SyncContext.Post(m =>
- {
- var result = m as string;
- propertyGrid1.Refresh();
- //tbtnJogOnOff.Text = (prop.AxState == AxisState.STA_AX_EXT_JOG) ? "关闭Jog" : "开启Jog";
- //tbtnLeft.Enabled = tbtnRight.Enabled = (prop.AxState == AxisState.STA_AX_EXT_JOG);
- }, "异步操作完成结果");
-
-
- }
-
- private void closeDev()
- {
- try
- {
- if (dev == null)
- return;
- dev.stop();
- dev.close();
- }
- catch (Exception ex)
- {
- refreshUIState();
- //MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
-
- protected override void OnHandleDestroyed(EventArgs e)
- {
- base.OnHandleDestroyed(e);
- // 在此添加需要手动释放资源的代码
- this.closeDev();
- }
-
- private void tbtnRun_Click(object sender, EventArgs e)
- {
- try
- {
- dev.setParam(prop.ExposureTime,prop.Gain,prop.ResultingFrameRate);
- dev.getParam();
- prop.ExposureTime = dev.ExposureTime;
- prop.Gain = dev.Gain;
- prop.ResultingFrameRate = dev.ResultingFrameRate;
- }
- catch(Exception ex)
- {
- MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
-
- private void tbtnExport_Click(object sender, EventArgs e)
- {
- string filePath = FileUtil.saveAsFile($"Scanner_config.json", "JSON|*.json");
- if (filePath != "")
- {
- string jsonText = prop.serialize();
- File.WriteAllText(filePath, jsonText);
- MessageBox.Show("保存成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- }
-
- private void tbtnImport_Click(object sender, EventArgs e)
- {
- string filePath = FileUtil.selectFile("JSON|*.json");
- if (filePath != "")
- {
- string jsonText = File.ReadAllText(filePath);
- prop.deserialize(jsonText);
- this.propertyGrid1.Refresh();
- //this.propertyGrid1.SelectedObject= prop;
- //MessageBox.Show("加载成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- }
- private void tbtnScan_Click(object sender, EventArgs e)
- {
- prop.ImageList = new string[dev.isContinuousMode ? 2 : 1];
- dev.scan(prop.ImageList.Length);
- }
- private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
- {
- //其中包含了两个重要的属性:OldValue和ChangeItem。
- //ChangeItem是“PropertyDescriptorGridEntry”类型。一般可以通过ChangeItem.PropertyDescriptor.ComponentType查找到属性的实际类型。
- //而通过ChangeItem的Label,则可以查看到当前在属性编辑输入框中显示的值。
-
- string className = e.ChangedItem.PropertyDescriptor.ComponentType.Name;
- string propertyName = e.ChangedItem.PropertyDescriptor.Name;
- var oldValue = e.OldValue;
- var newValue = e.ChangedItem.Value;
- switch (propertyName)
- {
- //case "ChannelIndex":
- // if((int)newValue <1 || (int)newValue >4)
- // {
- // prop.ChannelIndex = (int)oldValue;
- // this.refreshUI();
- // }
- // else
- // {
- // dev.getDigitalValue((int)newValue);
- // }
- // break;
- }
-
- }
-
- private void tbtnMode_Click(object sender, EventArgs e)
- {
- if (!dev.isContinuousMode)
- {
- dev.setMode(true);
- tbtnMode.Text = "软触发";
- updateViewWin(null);
- dev.setPreviewWin(frm.picHwnd);
- }
- else
- {
- dev.setMode(false);
- tbtnMode.Text = "连续";
- dev.setPreviewWin(IntPtr.Zero);
- }
- }
- FrmScannerShow frm;
- private void tbtnShow_Click(object sender, EventArgs e)
- {
- if(frm!=null && !frm.IsDisposed)
- frm.Close();
-
- frm = new FrmScannerShow(dev.size);
- frm.Show();
- if(dev.isContinuousMode)
- dev.setPreviewWin(frm.picHwnd);
- }
-
- private void tbtnSave_Click(object sender, EventArgs e)
- {
- GetParamsEvent?.Invoke(prop.serialize());
- }
-
- private void tsbtnOpenDev_Click(object sender, EventArgs e)
- {
- init(devType);
- }
- }
- }
|