|
- using Advantech.Motion;
- using Newtonsoft.Json;
- using AssistClient.Device;
- using AssistClient.Utils;
- 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 System.Xml.Linq;
- using static AssistClient.Device.SizeLib;
-
- namespace AssistClient.UI
- {
- public partial class UISizeLib : UserControl
- {
- SynchronizationContext SyncContext = null;
-
- public Action<string> GetParamsEvent;
- //
- private SizeLibProp prop = new SizeLibProp();
- private SizeLib dev;
- public UISizeLib()
- {
- InitializeComponent();
- propertyGrid1.PropertyValueChanged += propertyGrid1_PropertyValueChanged;
- refreshUIState();
- //获取UI线程同步上下文
- SyncContext = SynchronizationContext.Current;
- //init();
- }
- private string szLog;
- 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()
- {
- this.tbtnSave.Visible = tssSave.Visible = (GetParamsEvent != null);
- this.propertyGrid1.SelectedObject = prop;
- dev = new SizeLib();
- dev.WarningEvent = (level, info) =>
- {
- szLog = $"({level}){info}";
- txtLog.Text = $"({level}){info}";
- };
- //DATA
- dev.finishEvent += new System.Action<SizeTask>((task) =>
- {
- prop.PT1 = task.PT1;
- prop.PT2 = task.PT2;
- prop.Shanxian = task.Shanxian;
- //prop.RowP= task.RowP;
- prop.Circle_Ymm = task.Circle_Ymm;
- prop.Circle_Xmm = task.Circle_Xmm;
- prop.offsetX = task.offsetX;
- prop.offsetY = task.offsetY;
- prop.MarkPointList=task.MarkPointList;
- prop.resultInfo = task.isSucceed ? "成功" : task.resultInfo;
- this.refreshUI();
- });
-
- if (!dev.start(Config.SizeEnginePath))
- {
- this.closeDev();
- return;
- }
- refreshUIState();
- }
- public string getParamsData()
- {
- return prop.serialize();
- }
- public void setParamsData(string json)
- {
- if (json == "") return;
- prop.deserialize(json);
- this.propertyGrid1.Refresh();
- }
- 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();
- }
- 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
- {
- prop.resultInfo = "";
- dev.add(new SizeTask()
- {
- engineName = prop.EngineName,
- file_path = prop.BmpPath,
- //bmp =read2Bmp(prop.BmpPath),
- index=prop.Index,
- //finishEvent = (res) =>
- //{
- //}
- });
- }
- catch(Exception ex)
- {
- MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
-
- private void tbtnExport_Click(object sender, EventArgs e)
- {
- string filePath = FileUtil.saveAsFile($"SizeLib 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.openFile("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 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 "BmpPath":
- // if(newValue)
- // {
- // prop.BmpPath = (int)oldValue;
- // this.refreshUI();
- // }
- // else
- // {
- // dev.getDigitalValue((int)newValue);
- // }
- // break;
- //case "DigitalValue":
- // if ((int)newValue < 0 || (int)newValue > 100)
- // {
- // prop.DigitalValue = (int)oldValue;
- // this.refreshUI();
- // }
- // break;
- }
-
- }
-
- private void tbtnSave_Click(object sender, EventArgs e)
- {
- GetParamsEvent?.Invoke(prop.serialize());
- }
-
- private void tsbtnOpenDev_Click(object sender, EventArgs e)
- {
- this.init();
- }
-
- private void tbtnDebug_Click(object sender, EventArgs e)
- {
- if (dev.IsInit)
- {
- if (!dev.setDebug)
- {
- dev.setDebug=true;
- }
- else
- {
- dev.setDebug = false ;
- }
-
- tbtnDebug.Text = dev.setDebug?"关闭调试" : "打开调试";
- }
- }
- private Bitmap read2Bmp(string path)
- {
- FileStream fs = new FileStream(path, FileMode.Open);
- byte[] bmpdata = new byte[fs.Length];
- fs.Read(bmpdata, 0, bmpdata.Length);
-
- Bitmap bmp = new Bitmap(fs);
- fs.Close();
-
- return bmp;
-
- }
- }
- }
|