using OTS.WinFormsUI.Docking; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using OTSCLRINTERFACE; using System.Collections; using System.IO; using System.Runtime.InteropServices; using System.Text.RegularExpressions; namespace OTSPartA_STDEditor { public partial class STDEditor : DockContent { #region 全局变量 public SubMidWindow m_SubMidWindow = null; public int STDId = -1; Hashtable table_STDEditor; //数据库操作对象 public SqlLiteClass m_sc = null; // 电镜设置对象 COTSControlFunExport m_cfun = null; //Xray图谱用户控件 UXrayControl XrayControl = null; //全局Xray 用于存储当前选择行的Xray信息 byte[] xrayByteData = null; // 连接状态 bool m_bConnectionState = false; //标准Xray信息列表 public List m_STDXrayList = null; #endregion public enum Result { NoMatched=0, Success=1, Fail=2 } public STDEditor(SubMidWindow SubMidWindow) { InitializeComponent(); m_sc = new SqlLiteClass(); m_SubMidWindow = SubMidWindow; X = this.Width; Y = this.Height; setTag(this); } public STDEditor(SubMidWindow SubMidWindow,string STDDBaddress) { InitializeComponent(); m_sc = new SqlLiteClass(STDDBaddress); m_SubMidWindow = SubMidWindow; X = this.Width; Y = this.Height; setTag(this); } private void STDEditor_Load(object sender, EventArgs e) { m_SubMidWindow.m_MainForm.lan = new Language(this); table_STDEditor = m_SubMidWindow.m_MainForm.lan.GetNameTable("STDEditor"); //初始化comboBox this.comboBox_Elem1.Items.Add("first_elem"); this.comboBox_Elem1.Items.Add("second_elem"); this.comboBox_Elem1.Items.Add("third_elem"); this.comboBox_Elem1.Items.Add("forth_elem"); this.comboBox_Elem1.Items.Add("fifth_elem"); this.comboBox_Elem1.Items.Add("sixth_elem"); this.comboBox_Elem1.Items.Add("seventh_elem"); this.comboBox_Elem1.Items.Add("eighth_elem"); this.comboBox_Elem1.Items.Add("ninth_elem"); this.comboBox_Elem1.Items.Add("tenth_elem"); comboBox_Elem1.SelectedIndex = comboBox_Elem1.Items.IndexOf("first_elem"); this.comboBox_Elem.Items.Add("Element#1"); this.comboBox_Elem.Items.Add("Element#2"); this.comboBox_Elem.Items.Add("Element#3"); this.comboBox_Elem.Items.Add("Element#4"); this.comboBox_Elem.Items.Add("Element#5"); this.comboBox_Elem.Items.Add("Element#6"); this.comboBox_Elem.Items.Add("Element#7"); this.comboBox_Elem.Items.Add("Element#8"); this.comboBox_Elem.Items.Add("Element#9"); this.comboBox_Elem.Items.Add("Element#10"); comboBox_Elem.SelectedIndex = comboBox_Elem.Items.IndexOf("Element#1"); this.comboBox_ImgProperty.Items.Add("Dmax"); //颗粒最大直径; this.comboBox_ImgProperty.Items.Add("Dmin"); //颗粒最小直径; this.comboBox_ImgProperty.Items.Add("Aspect"); //长宽比; this.comboBox_ImgProperty.Items.Add("Dperp"); //与Dmax垂直直径; this.comboBox_ImgProperty.Items.Add("Dmean"); //平均内接圆直径; this.comboBox_ImgProperty.Items.Add("Area"); //面积; this.comboBox_ImgProperty.Items.Add("Dferet"); //费雷特直径; this.comboBox_ImgProperty.Items.Add("Width"); //宽度(直上直下那种); this.comboBox_ImgProperty.Items.Add("Height"); //高度(直上直下那种) this.comboBox_ImgProperty.Items.Add("Perimeter"); //周长 this.comboBox_ImgProperty.Items.Add("Dinscr"); //最大内接圆直径 this.comboBox_ImgProperty.Items.Add("Orientation"); //朝向 this.comboBox_ImgProperty.Items.Add("Delong"); //展开长度 this.comboBox_ImgProperty.Items.Add("Aspectelong"); //展开后长宽比 this.comboBox_ImgProperty.Items.Add("Dequalcircle"); //等效圆直径 this.comboBox_ImgProperty.Items.Add("Vedio"); //BSE灰度平均值 comboBox_ImgProperty.SelectedIndex = comboBox_ImgProperty.Items.IndexOf("Dmax"); string[] PeriodicTable = { "H", "He", "Li", "Be", "B", "C", "N", "O", "F", "Ne", "Na", "Mg", "Al", "Si", "P", "S", "Cl", "Ar", "K", "Ca", "Sc", "Ti", "V", "Cr", "Mn", "Fe", "Co", "Ni", "Cu", "Zn", "Ga", "Ge", "As", "Se", "Br", "Kr", "Rb", "Sr", "Y", "Zr", "Nb", "Mo", "Tc", "Ru", "Rh", "Pd", "Ag", "Cd", "In", "Sn", "Sb", "Te", "I", "Xe", "Cs", "Ba", "La", "Ce", "Pr", "Nd", "Pm", "Sm", "Eu", "Gd", "Tb", "Dy", "Ho", "Er", "Tm", "Yb", "Lu", "Hf", "Ta", "W", "Re", "Os", "Ir", "Pt", "Au", "Hq", "TI", "Pb", "Bi", "Po", "At", "Rn", "Fr", "Ra", "Ac", "Th", "Pa", "U", "Np", "Pu", "Am", "Cm", "Bk", "Cf", "Es", "Fm", "Mc", "No", "Lr", "Rf", "Db", "Sg", "Bh", "Hs", "Mt", "Ds", "Rg", "Unb" }; comboBox_PeriodicTable.Items.AddRange(PeriodicTable); AutoCompleteStringCollection sc = new AutoCompleteStringCollection(); sc.AddRange(PeriodicTable); this.comboBox_PeriodicTable.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend; this.comboBox_PeriodicTable.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource; this.comboBox_PeriodicTable.AutoCompleteCustomSource = sc; dataGridView_KeyElements.RowHeadersVisible = false; dataGridView_KeyElements.ColumnHeadersVisible = false; dataGridView_KeyElements.AllowUserToAddRows = false; dataGridView_KeyElements.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; dataGridView_SubElements.RowHeadersVisible = false; dataGridView_SubElements.ColumnHeadersVisible = false; dataGridView_SubElements.AllowUserToAddRows = false; dataGridView_SubElements.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; //添加Xray用户控件 XrayControl = new UXrayControl(); plXray.Controls.Add(XrayControl); XrayControl.Dock = DockStyle.Fill; //绑定数据库中的STDXray m_STDXrayList = new List(); BindSTDXray(); } /// /// [颜色:16进制转成RGB] /// /// 设置16进制颜色 [返回RGB] /// public static System.Drawing.Color colorHx16toRGB(string strHxColor) { try { if (strHxColor.Length == 0) {//如果为空 return System.Drawing.Color.FromArgb(255, 255, 204);//设为白色 } else {//转换颜色 return System.Drawing.Color.FromArgb(System.Int32.Parse(strHxColor.Substring(1, 2), System.Globalization.NumberStyles.AllowHexSpecifier), System.Int32.Parse(strHxColor.Substring(3, 2), System.Globalization.NumberStyles.AllowHexSpecifier), System.Int32.Parse(strHxColor.Substring(5, 2), System.Globalization.NumberStyles.AllowHexSpecifier)); } } catch {//设为白色 return System.Drawing.Color.FromArgb(255, 255, 204); } } float AmplificationFactor = 1; //private void button_Extend_Click(object sender, EventArgs e) //{ // if (button_Extend.BackColor == SystemColors.ControlDarkDark) // { // //groupBox_Data.Visible = true; // textbox_STDEditor.Height = (int)Math.Round(150 * AmplificationFactor); // button_Extend.BackColor = SystemColors.ControlDark; // } // else // { // button_Extend.BackColor = SystemColors.ControlDarkDark; // textbox_STDEditor.Height = (int)Math.Round(300* AmplificationFactor); // } //} private void comboBox_Elem1_SelectedIndexChanged(object sender, EventArgs e) { string newStr = comboBox_Elem1.SelectedItem.ToString(); int index = textbox_STDEditor.SelectionStart; textbox_STDEditor.Text = textbox_STDEditor.Text.Insert(index, newStr); textbox_STDEditor.Focus(); textbox_STDEditor.Select(index + newStr.Length, 0); textbox_STDEditor.ScrollToCaret(); } private void comboBox_Elem2_SelectedIndexChanged(object sender, EventArgs e) { string newStr = comboBox_Elem.SelectedItem.ToString(); int index = textbox_STDEditor.SelectionStart; textbox_STDEditor.Text = textbox_STDEditor.Text.Insert(index, newStr); textbox_STDEditor.Focus(); textbox_STDEditor.Select(index + newStr.Length, 0); textbox_STDEditor.ScrollToCaret(); } private void comboBox_ImgProperty_SelectedIndexChanged(object sender, EventArgs e) { string newStr = comboBox_ImgProperty.SelectedItem.ToString(); int index = textbox_STDEditor.SelectionStart; textbox_STDEditor.Text = textbox_STDEditor.Text.Insert(index, newStr); textbox_STDEditor.Focus(); textbox_STDEditor.Select(index + newStr.Length, 0); textbox_STDEditor.ScrollToCaret(); } private void button_Plus_Click(object sender, EventArgs e) { //textbox_STDEditor 中的光标 int index = textbox_STDEditor.SelectionStart; textbox_STDEditor.Text = textbox_STDEditor.Text.Insert(index, "+"); textbox_STDEditor.Focus(); textbox_STDEditor.Select(index + 1, 0); textbox_STDEditor.ScrollToCaret(); } private void button_Subtract_Click(object sender, EventArgs e) { //textbox_STDEditor 中的光标 int index = textbox_STDEditor.SelectionStart; textbox_STDEditor.Text = textbox_STDEditor.Text.Insert(index, "-"); textbox_STDEditor.Focus(); textbox_STDEditor.Select(index + 1, 0); textbox_STDEditor.ScrollToCaret(); } private void button_Multiply_Click(object sender, EventArgs e) { //textbox_STDEditor 中的光标 int index = textbox_STDEditor.SelectionStart; textbox_STDEditor.Text = textbox_STDEditor.Text.Insert(index, "*"); textbox_STDEditor.Focus(); textbox_STDEditor.Select(index + 1, 0); textbox_STDEditor.ScrollToCaret(); } private void button_Divide_Click(object sender, EventArgs e) { //textbox_STDEditor 中的光标 int index = textbox_STDEditor.SelectionStart; textbox_STDEditor.Text = textbox_STDEditor.Text.Insert(index, "/"); textbox_STDEditor.Focus(); textbox_STDEditor.Select(index + 1, 0); textbox_STDEditor.ScrollToCaret(); ; } private void button_More_Click(object sender, EventArgs e) { //textbox_STDEditor 中的光标 int index = textbox_STDEditor.SelectionStart; textbox_STDEditor.Text = textbox_STDEditor.Text.Insert(index, ">"); textbox_STDEditor.Focus(); textbox_STDEditor.Select(index + 1, 0); textbox_STDEditor.ScrollToCaret(); } private void button_Less_Click(object sender, EventArgs e) { //textbox_STDEditor 中的光标 int index = textbox_STDEditor.SelectionStart; textbox_STDEditor.Text = textbox_STDEditor.Text.Insert(index, "<"); textbox_STDEditor.Focus(); textbox_STDEditor.Select(index + 1, 0); textbox_STDEditor.ScrollToCaret(); } private void button_LeftParenthesis_Click(object sender, EventArgs e) { //textbox_STDEditor 中的光标 int index = textbox_STDEditor.SelectionStart; textbox_STDEditor.Text = textbox_STDEditor.Text.Insert(index, "("); textbox_STDEditor.Focus(); textbox_STDEditor.Select(index + 1, 0); textbox_STDEditor.ScrollToCaret(); } private void button_RightParenthesis_Click(object sender, EventArgs e) { //textbox_STDEditor 中的光标 int index = textbox_STDEditor.SelectionStart; textbox_STDEditor.Text = textbox_STDEditor.Text.Insert(index, ")"); textbox_STDEditor.Focus(); textbox_STDEditor.Select(index + 1, 0); textbox_STDEditor.ScrollToCaret(); } private void button_Equal_Click(object sender, EventArgs e) { //textbox_STDEditor 中的光标 int index = textbox_STDEditor.SelectionStart; textbox_STDEditor.Text = textbox_STDEditor.Text.Insert(index, "="); textbox_STDEditor.Focus(); textbox_STDEditor.Select(index + 1, 0); textbox_STDEditor.ScrollToCaret(); } //元素下拉选择输入 private void comboBox_PeriodicTable_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox_PeriodicTable.Text != null && comboBox_PeriodicTable.SelectedItem != null) { string newStr = comboBox_PeriodicTable.SelectedItem.ToString(); comboBox_PeriodicTable.Text = null; int index = textbox_STDEditor.SelectionStart; textbox_STDEditor.Text = textbox_STDEditor.Text.Insert(index, newStr); textbox_STDEditor.Focus(); textbox_STDEditor.Select(index + newStr.Length, 0); textbox_STDEditor.ScrollToCaret(); } } private void button_And_Click(object sender, EventArgs e) { //textbox_STDEditor 中的光标 int index = textbox_STDEditor.SelectionStart; textbox_STDEditor.Text = textbox_STDEditor.Text.Insert(index, "and"); textbox_STDEditor.Focus(); textbox_STDEditor.Select(index + 3, 0); textbox_STDEditor.ScrollToCaret(); } private void button_Or_Click(object sender, EventArgs e) { //textbox_STDEditor 中的光标 int index = textbox_STDEditor.SelectionStart; textbox_STDEditor.Text = textbox_STDEditor.Text.Insert(index, "or"); textbox_STDEditor.Focus(); textbox_STDEditor.Select(index + 2, 0); textbox_STDEditor.ScrollToCaret(); } private void comboBox_Constants_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox_Constants.SelectedItem != null) { string newStr = comboBox_Constants.SelectedItem.ToString().Split('=')[0]; int index = textbox_STDEditor.SelectionStart; textbox_STDEditor.Text = textbox_STDEditor.Text.Insert(index, newStr); textbox_STDEditor.Focus(); textbox_STDEditor.Select(index + newStr.Length, 0); textbox_STDEditor.ScrollToCaret(); } } private void button_1_Click(object sender, EventArgs e) { //textbox_STDEditor 中的光标 int index = textbox_STDEditor.SelectionStart; textbox_STDEditor.Text = textbox_STDEditor.Text.Insert(index, "1"); textbox_STDEditor.Focus(); textbox_STDEditor.Select(index + 1, 0); textbox_STDEditor.ScrollToCaret(); } private void button_2_Click(object sender, EventArgs e) { //textbox_STDEditor 中的光标 int index = textbox_STDEditor.SelectionStart; textbox_STDEditor.Text = textbox_STDEditor.Text.Insert(index, "2"); textbox_STDEditor.Focus(); textbox_STDEditor.Select(index + 1, 0); textbox_STDEditor.ScrollToCaret(); } private void button_3_Click(object sender, EventArgs e) { //textbox_STDEditor 中的光标 int index = textbox_STDEditor.SelectionStart; textbox_STDEditor.Text = textbox_STDEditor.Text.Insert(index, "3"); textbox_STDEditor.Focus(); textbox_STDEditor.Select(index + 1, 0); textbox_STDEditor.ScrollToCaret(); } private void button_4_Click(object sender, EventArgs e) { //textbox_STDEditor 中的光标 int index = textbox_STDEditor.SelectionStart; textbox_STDEditor.Text = textbox_STDEditor.Text.Insert(index, "4"); textbox_STDEditor.Focus(); textbox_STDEditor.Select(index + 1, 0); textbox_STDEditor.ScrollToCaret(); } private void button_5_Click(object sender, EventArgs e) { //textbox_STDEditor 中的光标 int index = textbox_STDEditor.SelectionStart; textbox_STDEditor.Text = textbox_STDEditor.Text.Insert(index, "5"); textbox_STDEditor.Focus(); textbox_STDEditor.Select(index + 1, 0); textbox_STDEditor.ScrollToCaret(); } private void button_6_Click(object sender, EventArgs e) { //textbox_STDEditor 中的光标 int index = textbox_STDEditor.SelectionStart; textbox_STDEditor.Text = textbox_STDEditor.Text.Insert(index, "6"); textbox_STDEditor.Focus(); textbox_STDEditor.Select(index + 1, 0); textbox_STDEditor.ScrollToCaret(); } private void button_7_Click(object sender, EventArgs e) { //textbox_STDEditor 中的光标 int index = textbox_STDEditor.SelectionStart; textbox_STDEditor.Text = textbox_STDEditor.Text.Insert(index, "7"); textbox_STDEditor.Focus(); textbox_STDEditor.Select(index + 1, 0); textbox_STDEditor.ScrollToCaret(); } private void button_8_Click(object sender, EventArgs e) { //textbox_STDEditor 中的光标 int index = textbox_STDEditor.SelectionStart; textbox_STDEditor.Text = textbox_STDEditor.Text.Insert(index, "8"); textbox_STDEditor.Focus(); textbox_STDEditor.Select(index + 1, 0); textbox_STDEditor.ScrollToCaret(); } private void button_9_Click(object sender, EventArgs e) { //textbox_STDEditor 中的光标 int index = textbox_STDEditor.SelectionStart; textbox_STDEditor.Text = textbox_STDEditor.Text.Insert(index, "9"); textbox_STDEditor.Focus(); textbox_STDEditor.Select(index + 1, 0); textbox_STDEditor.ScrollToCaret(); } private void button_0_Click(object sender, EventArgs e) { int index = textbox_STDEditor.SelectionStart; textbox_STDEditor.Text = textbox_STDEditor.Text.Insert(index, "0"); textbox_STDEditor.Focus(); textbox_STDEditor.Select(index + 1, 0); textbox_STDEditor.ScrollToCaret(); } //元素自身text按回车键输入 private void comboBox_PeriodicTable_KeyDown(object sender, KeyEventArgs e) { if (e.KeyValue == 13) { if (comboBox_PeriodicTable.Text != null && comboBox_PeriodicTable.Text != "") { this.textbox_STDEditor.Text += comboBox_PeriodicTable.Text.ToString(); comboBox_PeriodicTable.Text = null; } } } #region 采集Xray 按钮事件 private void btnCollectXray_Click(object sender, EventArgs e) { m_SubMidWindow.m_MainForm.IsModified = true; string strTime = txtCollectTime.Text.Trim(); if (txtCollectTime.Equals("")) { showMessage("Please fill in the collection time name!"); txtCollectTime.Focus(); return; } if (!IsMatch(strTime, 1)) { showMessage("Error collecting time content, the format should be integer!"); txtCollectTime.Focus(); return; } if (Convert.ToInt32(strTime) < 50) { showMessage("CollectTime must not be less than 100 milliseconds!"); txtCollectTime.Focus(); return; } //显示Xray xrayByteData = IntArrToByteArr(GetCollectXray(strTime)); int color = Convert.ToInt32(ParseRGB(colorHx16toRGB(m_SubMidWindow.m_MainForm.STDDictionary[STDId].Color)).ToString()); //根据树节点修改对应的Xray信息 UpdateSTDXray(STDId, xrayByteData); ShowXrayAtlas(color, xrayByteData); } /// /// 获取采集当前的Xray信息 /// /// protected uint[] GetCollectXray(string strTime) { try { if (null == m_cfun) { m_cfun = COTSControlFunExport.GetControllerInstance(); } if (ConnectionSem(connectionEnumType.EDSOnlyPointXRay)) { if (EDSInit()) { int iSize = 2000; uint[] iXrayData = new uint[iSize]; //采集XRay数据 if (m_cfun.CollectSpectrum(uint.Parse(strTime), ref iXrayData)) { return iXrayData; } } } return null; } catch /*(Exception ex)*/ { return null; } finally { //EDS过程结束 //m_cfun.EDSFinishedInstance(); //关闭连接 DisConnectSem(connectionEnumType.EDSOnlyPointXRay); } } private void btnReadXray_Click(object sender, EventArgs e) { OpenFileDialog fileSel = new OpenFileDialog(); fileSel.Filter = "|*.txt"; if (DialogResult.OK == fileSel.ShowDialog()) { String str = ""; //读取Xray文件 using (StreamReader sr = new StreamReader(fileSel.FileName)) { while (!sr.EndOfStream) { str += sr.ReadLine() + ","; } str = str.Substring(0, str.Length - 1); string[] xrayData = str.Split(','); if (xrayData.Length != 2000) { return; } uint[] data = new uint[xrayData.Length]; for (int i = 0; i < xrayData.Length; i++) { data[i] = Convert.ToUInt32(xrayData[i]); } //Xray数据 xrayByteData = IntArrToByteArr(data); int color = Convert.ToInt32(ParseRGB(colorHx16toRGB(m_SubMidWindow.m_MainForm.STDDictionary[STDId].Color)).ToString()); //根据树节点修改对应的Xray信息 UpdateSTDXray(STDId, xrayByteData); ShowXrayAtlas(color, xrayByteData); } } } /// /// 将int[] 转换为 byte[] /// /// /// public static byte[] IntArrToByteArr(uint[] intArr) { int intSize = sizeof(uint) * intArr.Length; byte[] bytArr = new byte[intSize]; //申请一块非托管内存   //IntPtr ptr = Marshal.AllocHGlobal(intSize); ////复制int数组到该内存块   //Marshal.Copy(intArr, 0, ptr, intArr.Length); ////复制回byte数组   //Marshal.Copy(ptr, bytArr, 0, bytArr.Length); ////释放申请的非托管内存   //Marshal.FreeHGlobal(ptr); for (int i = 0; i < intArr.Length; i++) { byte[] c1 = BitConverter.GetBytes(intArr[i]); c1.CopyTo(bytArr, i * 4); } return bytArr; } /// /// 将颜色对象转换为uint /// /// /// public uint ParseRGB(Color color) { return (uint)(((uint)color.B << 16) | (ushort)(((ushort)color.G << 8) | color.R)); } #region 显示Xray图谱 /// /// 显示Xray图谱 /// public void ShowXrayAtlas(int colorValue = 0, byte[] XrayData = null) { DataTable dt = new DataTable(); dt.Columns.Add("Color", typeof(int)); dt.Columns.Add("SPEC", typeof(byte[])); DataRow dRow = dt.NewRow(); dRow["Color"] = colorValue; if (XrayData != null) { dRow["SPEC"] = XrayData; } else { dRow["SPEC"] = new byte[8000]; } //将选择行的数据传递给Xray控件 XrayControl.Dr = dRow; XrayControl.Refresh(); XrayData = null; } #endregion #region 常用数据验证的封装,数字字符的验证 /// /// 常用数据验证的封装,数字字符的验证 /// /// 需要验证的数值【字符串,或者数字】 /// 类型为哪一个验证 /// 如果验证成功则返回True,否则返回false public bool IsMatch(string inputVal, int type) { switch (type) { case 0: return Regex.IsMatch(inputVal, @"^[1-9]d*$"); //匹配正整数 case 1: return Regex.IsMatch(inputVal, @"^-?\d+$"); //匹配整数 case 2: return Regex.IsMatch(inputVal, @"^[A-Za-z0-9]+$"); //匹配由数字和26个英文字母组成的字符串 case 3: return Regex.IsMatch(inputVal, @"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$"); //匹配正浮点数 case 4: return Regex.IsMatch(inputVal, @"^[\u4e00-\u9fa5]{0,}$"); //匹配汉字 case 5: return Regex.IsMatch(inputVal, @"^[0-9]+(.[0-9]{1,3})?$"); //匹配1~3位小数的正实数 case 6: return Regex.IsMatch(inputVal, @"^[A-Za-z]+$"); //匹配英文字符 default: return true; } } #endregion #region 弹出提示 /// /// 弹出提示 /// /// protected void showMessage(string strContent) { MessageBox.Show(strContent, "Tip"); } #endregion #region 连接与关闭设备 public bool ConnectionSem(connectionEnumType connectionType) { //获取连接电镜类型 string connTypeStr = GetConnectionType(connectionType); //连接电镜标识 bool bDisConnResult = false; //判断连接状态 if (!m_bConnectionState) { //连接电镜设置 bDisConnResult = m_cfun.ConncetSem(); } if (bDisConnResult) { m_bConnectionState = true; } else { m_bConnectionState = false; } return bDisConnResult; } public bool DisConnectSem(connectionEnumType disConnectType) { //获取关闭电镜类型 string connTypeStr = GetConnectionType(disConnectType); bool bDisConnResult = false; if (m_bConnectionState) { bDisConnResult = m_cfun.DisconnectSem(); } if (bDisConnResult) { m_bConnectionState = false; } else { m_bConnectionState = true; } return bDisConnResult; } private string GetConnectionType(connectionEnumType connectionType) { string connString = string.Empty; switch (connectionType) { //设置单点采集文字内容 case connectionEnumType.EDSOnlyPointXRay: connString = "OnlyPointXRay"; break; //设置多点采集文字内容 case connectionEnumType.EDSMultiPointXRay: connString = "MultiPointXRay"; break; //设置面采集文字内容 case connectionEnumType.EDSAreaXRay: connString = "AreaXRay"; break; //设置图片 case connectionEnumType.ScanImage: connString = "Image"; break; default: break; } return connString; } /// /// EDS初始化 /// public bool EDSInit() { bool bResult = false; //线程调用 加载 bResult = m_cfun.EDSInit(); return bResult; } public enum connectionEnumType { EDSOnlyPointXRay = 0, EDSMultiPointXRay = 1, EDSAreaXRay = 2, ScanImage = 3 } /// /// 标准Xray状态 /// public enum STDXrayInfoState { Add = 0, Modify = 1, Delete = 2 } #endregion /// /// 记录修改后的标准中的Xray信息 /// public class STDXray { /// /// 标准编号 /// private string stdID; /// /// Xray数据 /// private byte[] xrayData; /// /// 状态 /// private int infoState = -1; public string StdID { get => stdID; set => stdID = value; } public byte[] XrayData { get => xrayData; set => xrayData = value; } public int InfoState { get => infoState; set => infoState = value; } } #endregion #region Xray 与 STDDB数据操作 /// /// 绑定数据库中已存在的Xray信息 /// public void BindSTDXray() { //清空STDXray列表中的集合 m_STDXrayList.Clear(); //查询 DataTable dt = m_sc.GetDTFormSysSTDBySQLString("select Id,SPEC from STDMinerals"); if (dt != null) { if (dt.Rows.Count > 0) { foreach (DataRow item in dt.Rows) { STDXray sXray = new STDXray(); sXray.StdID = item["Id"].ToString(); sXray.XrayData = (byte[])item["SPEC"]; sXray.InfoState = 1; m_STDXrayList.Add(sXray); } } } } bool SaveOtherDataToXray(string STDDBAddress, int id) { #region 数据库存贮方式一 System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + STDDBAddress + "'"); System.Data.SQLite.SQLiteCommand cmm = m_dbConnection.CreateCommand(); try { m_dbConnection.Open(); //string insertstr = ("insert into STDMinerals(name,formula,density,Hardness,BSEValue,Electrical_conductivity,color,Element) values(" + m_SubMidWindow.m_MainForm.STDDictionary[id].StrName + "," + m_SubMidWindow.m_MainForm.STDDictionary[id].Formula + "," + m_SubMidWindow.m_MainForm.STDDictionary[id].Density + "," + m_SubMidWindow.m_MainForm.STDDictionary[id].Hardness + "," + m_SubMidWindow.m_MainForm.STDDictionary[id].BSE + "," + m_SubMidWindow.m_MainForm.STDDictionary[id].Electrical_conductivity + "," + m_SubMidWindow.m_MainForm.STDDictionary[id].Color + "," + m_SubMidWindow.m_MainForm.STDDictionary[id].Element + ");"); string insertstr = ("insert into STDMinerals(name) values(" + m_SubMidWindow.m_MainForm.STDDictionary[id].StrName + ");"); cmm.CommandText = insertstr; cmm.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); return false; } m_dbConnection.Close(); #endregion return true; } /// /// 保存STDXray至数据库 /// public void SaveSTDXray() { ArrayList delArrayList = new ArrayList(); if (m_STDXrayList != null) { //if (m_STDXrayList.Count > 0) //{ foreach (STDXray item in m_STDXrayList) { int infoState = item.InfoState; //添加状态 根据Xray信息当前只能按照一条操作 if (infoState == (int)STDXrayInfoState.Add) { //if (item.XrayData == null) //{ // item.XrayData = new byte[8000]; //} //string strvaluename = "@data"; ////编辑添加语句 //StringBuilder strSqlAdd = new StringBuilder(); //strSqlAdd.Append("insert into STDMinerals(Id,SPEC) values(" + item.StdID + "," + strvaluename + ");"); //bool addResult = m_sc.ExecuteBlob(strSqlAdd.ToString(), strvaluename, (byte[])item.XrayData, ((byte[])item.XrayData).Length); bool addResult = false; //判断STDID在数据库中是否存在 bool selResult = SelSTDDBBySTDId(item.StdID); if (selResult) { //addResult = UpdateSTDDB(item.StdID, xrayByteData); addResult = UpdateSTDDB(item.StdID, item.XrayData); } else { addResult = AddSTDDB(item.StdID, item.XrayData); } if (!addResult) { continue; } } //修改状态 else if (infoState == (int)STDXrayInfoState.Modify) { //判断STDID在数据库中是否存在 bool selResult = SelSTDDBBySTDId(item.StdID); if (selResult) { UpdateSTDDB(item.StdID, item.XrayData); } else { AddSTDDB(item.StdID, item.XrayData); } } //删除状态 编辑批量删除语句 else if (infoState == (int)STDXrayInfoState.Delete) { //编辑删除语句 delArrayList.Add("delete from STDMinerals where id=" + item.StdID + ";"); } } //批量删除 if (delArrayList.Count > 0) { bool delResult = m_sc.DelSTDInfo(delArrayList); } //} } } /// /// 添加STD信息至数据库 /// protected bool AddSTDDB(string stdID, byte[] XrayData) { if (XrayData == null) { XrayData = new byte[8000]; } string strvaluename = "@data"; StringBuilder sqlStrAdd = new StringBuilder(); sqlStrAdd.Append("insert into STDMinerals(Id,SPEC) values(" + stdID + "," + strvaluename + ");"); bool addResult = m_sc.ExecuteBlob(sqlStrAdd.ToString(), strvaluename, (byte[])XrayData, ((byte[])XrayData).Length); return addResult; } /// /// 修改STD信息至数据库 /// protected bool UpdateSTDDB(string stdID, byte[] XrayData) { if (XrayData == null) { XrayData = new byte[8000]; } string strValueName = "@data"; StringBuilder sqlStrUpdate = new StringBuilder(); sqlStrUpdate.Append("update STDMinerals set SPEC=" + strValueName +" where id=" + stdID + ";"); bool updateResult = m_sc.ExecuteBlob(sqlStrUpdate.ToString(), strValueName, (byte[])XrayData, ((byte[])XrayData).Length); return updateResult; } /// /// 查询是否存在标准编号 /// /// protected bool SelSTDDBBySTDId(string stdID) { bool selResult = false; //编辑查询语句 StringBuilder sqlStr = new StringBuilder(); sqlStr.Append("select * from STDMinerals where id=" + stdID + ""); DataTable dt = m_sc.GetDTFormSysSTDBySQLString(sqlStr.ToString()); if (dt != null) { if (dt.Rows.Count > 0) { //已存在状态 selResult = true; } } return selResult; } /// /// 根据选择树节点显示对应的Xray信息 /// public void SelSTDXray(int Key, STDdata sT) { if (sT == null) { return; } if (m_STDXrayList != null) { //if (m_STDXrayList.Count > 0) //{ bool isExists = false; foreach (STDXray item in m_STDXrayList) { //修改XrayData信息 if (item.StdID == Key.ToString()) { string selColor = ParseRGB(colorHx16toRGB(sT.Color)).ToString(); int colorValue = Convert.ToInt32(selColor); //显示Xray信息 if (item.XrayData != null) { isExists = true; ShowXrayAtlas(colorValue, item.XrayData); } break; } } if (!isExists) { ShowXrayAtlas(); } //} } } public void ShowEditContent(string stdID) { if (m_STDXrayList != null) { for (int i = 0; i < m_STDXrayList.Count; i++) { if (m_STDXrayList[i].StdID == stdID) { //获取修改行的信息 xrayByteData = (byte[])m_STDXrayList[i].XrayData; } } } } /// /// 根据STDId修改对应的Xray信息 /// /// protected void UpdateSTDXray(int STDId, byte[] xrayData) { if (m_STDXrayList != null) { //if (m_STDXrayList.Count > 0) //{ bool isExists = false; foreach (STDXray item in m_STDXrayList) { //修改XrayData信息 if (item.StdID == STDId.ToString()) { item.XrayData = xrayData; item.InfoState = (int)STDXrayInfoState.Modify; isExists = true; break; } } if (!isExists) { if (!STDId.ToString().Equals("")) { //添加STD XrayData信息 STDXray stdXray = new STDXray(); stdXray.StdID = STDId.ToString(); stdXray.XrayData = xrayData; stdXray.InfoState = (int)STDXrayInfoState.Add; m_STDXrayList.Add(stdXray); } } //} } } /// /// 添加STDXray信息 /// /// STDid /// Xray信息 /// protected bool AddSTDXray(int STDId, byte[] xrayData) { bool addResult = false; if (m_STDXrayList != null) { //if (m_STDXrayList.Count > 0) //{ if (!STDId.ToString().Equals("")) { //添加STD XrayData信息 STDXray stdXray = new STDXray(); stdXray.StdID = STDId.ToString(); stdXray.XrayData = xrayData; stdXray.InfoState = (int)STDXrayInfoState.Add; m_STDXrayList.Add(stdXray); addResult = true; } //} } return addResult; } /// /// 删除STDXray信息 /// /// 所选要删除的STDId /// public bool DelSTDXray(int STDId) { bool delResult = false; if (m_STDXrayList != null) { //if (m_STDXrayList.Count > 0) //{ if (!STDId.ToString().Equals("")) { foreach (STDXray item in m_STDXrayList) { //修改XrayData信息 if (item.StdID == STDId.ToString()) { item.InfoState = (int)STDXrayInfoState.Delete; delResult = true; break; } } } //} } return delResult; } #endregion #region 控制BSE、化学式、元素本文框中不能输入下划线 /// /// 控件不能输入下划线 /// /// /// private void textBox_KeyPress(object sender, KeyPressEventArgs e) { if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar >= 'A' && e.KeyChar <= 'Z') || (e.KeyChar >= 'a' && e.KeyChar <= 'z') || (e.KeyChar == 8) || !(e.KeyChar == '_')) { e.Handled = false; } else { e.Handled = true; } } #endregion private void btnTest_Click(object sender, EventArgs e) { XrayContrastForm xrayContrastForm = new XrayContrastForm(); xrayContrastForm.STDDictionary = m_SubMidWindow.m_MainForm.STDDictionary; xrayContrastForm.ShowDialog(); } string ElementKeyOrSub = ""; private void dataGridView_KeyElements_CellClick(object sender, DataGridViewCellEventArgs e) { ElementKeyOrSub = dataGridView_KeyElements.CurrentCell.Value.ToString(); dataGridView_KeyElements.Columns.RemoveAt(e.ColumnIndex); DataGridViewButtonColumn dataGridViewButtonColumn = new DataGridViewButtonColumn(); dataGridViewButtonColumn.Name = dataGridView_SubElements.Columns.Count.ToString(); dataGridView_SubElements.Columns.Add(dataGridViewButtonColumn); if (dataGridView_SubElements.Rows.Count == 0) { DataGridViewRow row = new DataGridViewRow(); dataGridView_SubElements.Rows.Add(row); } dataGridView_SubElements.Rows[0].Cells[dataGridView_SubElements.Columns.Count - 1].Value = ElementKeyOrSub; dataGridView_SubElements.ClearSelection(); dataGridView_KeyElements.ClearSelection(); } private void dataGridView_SubElements_CellClick(object sender, DataGridViewCellEventArgs e) { ElementKeyOrSub = dataGridView_SubElements.CurrentCell.Value.ToString(); dataGridView_SubElements.Columns.RemoveAt(e.ColumnIndex); DataGridViewButtonColumn dataGridViewButtonColumn = new DataGridViewButtonColumn(); dataGridViewButtonColumn.Name = dataGridView_SubElements.Columns.Count.ToString(); dataGridView_KeyElements.Columns.Add(dataGridViewButtonColumn); if (dataGridView_KeyElements.Rows.Count == 0) { DataGridViewRow row = new DataGridViewRow(); dataGridView_KeyElements.Rows.Add(row); } dataGridView_KeyElements.Rows[0].Cells[dataGridView_KeyElements.Columns.Count - 1].Value = ElementKeyOrSub; dataGridView_KeyElements.ClearSelection(); dataGridView_SubElements.ClearSelection(); } private float X=1; private float Y=1; private void STDEditor_Resize(object sender, EventArgs e) { float newx = (this.Width) / X; float newy = this.Height/ Y; AmplificationFactor = newy; setControls(newx, newy, this.tabSTDStandrad.TabPages[0]); //button_Extend.BackColor = SystemColors.ControlDarkDark; } private void setTag(Control cons) { foreach (Control con in cons.Controls) { con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size; if (con.Controls.Count > 0) setTag(con); } } private void setControls(float newx, float newy, Control cons) { foreach (Control con in cons.Controls) { if (false) { } else { string[] mytag = con.Tag.ToString().Split(':'); float a = Convert.ToSingle(mytag[0]) * newx; con.Width = (int)a; a = Convert.ToSingle(mytag[1]) * newy; con.Height = (int)(a); a = Convert.ToSingle(mytag[2]) * newx; con.Left = (int)(a); a = Convert.ToSingle(mytag[3]) * newy; con.Top = (int)(a); Single currentSize ; if (con.Name == "button_Extend") { currentSize = 6f; } else { if (Math.Max(newx, newy) < 1) { currentSize = Convert.ToSingle(mytag[4]) * 1; } else { currentSize = Convert.ToSingle(mytag[4]) * Math.Max(newx, newy); } } con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit); if (con.Controls.Count > 0) { setControls(newx, newy, con); } } } } Form_PeriodicTable form_PeriodicTable; private void button_PeriodicTableSwitch_Click(object sender, EventArgs e) { if (form_PeriodicTable == null) //如果子窗体为空则创造实例 并显示 { form_PeriodicTable = new Form_PeriodicTable(); if (table_STDEditor["language"].ToString() == "EN") { form_PeriodicTable.BackgroundImage = global::OTSPartA_STDEditor.Properties.Resources.PeriodicTable_EN; } else { form_PeriodicTable.BackgroundImage = global::OTSPartA_STDEditor.Properties.Resources.PeriodicTable_ZH; } form_PeriodicTable.Show(); } else { if (form_PeriodicTable.IsDisposed) //若子窗体关闭 则打开新子窗体 并显示 { form_PeriodicTable = new Form_PeriodicTable(); if (table_STDEditor["language"].ToString() == "EN") { form_PeriodicTable.BackgroundImage = global::OTSPartA_STDEditor.Properties.Resources.PeriodicTable_EN; } else { form_PeriodicTable.BackgroundImage = global::OTSPartA_STDEditor.Properties.Resources.PeriodicTable_ZH; } form_PeriodicTable.Show(); } else { form_PeriodicTable.Activate(); //使子窗体获得焦点 } } } private void button_ImportFromResult_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "(*.db)|*.db"; openFileDialog.RestoreDirectory = true; openFileDialog.FilterIndex = 1; if (openFileDialog.ShowDialog() == DialogResult.OK) { try { System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + openFileDialog.FileName + "'"); m_dbConnection.Open(); string STDId = m_SubMidWindow.m_MainForm.m_STDRuleslist.Grid_Minerals[m_SubMidWindow.m_MainForm.m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Row, m_SubMidWindow.m_MainForm.m_STDRuleslist.Grid_Minerals.Selection.ActivePosition.Column].Tag.ToString(); Result ret=ImportFromResult(STDId, m_dbConnection); if(ret== Result.NoMatched) { MessageBox.Show("No matching energy spectrum data was found", "Tip"); } m_dbConnection.Close(); int color = Convert.ToInt32(ParseRGB(colorHx16toRGB(m_SubMidWindow.m_MainForm.STDDictionary[int.Parse(STDId)].Color)).ToString()); for(int i=0;i< m_STDXrayList.Count;i++) { if(m_STDXrayList[i].StdID== STDId) { ShowXrayAtlas(color, m_STDXrayList[i].XrayData); break; } } } catch /*(Exception ex)*/ { MessageBox.Show("Failed to load Xray data!", "Tip"); } } } public Result ImportFromResult(string STDId, System.Data.SQLite.SQLiteConnection m_dbConnection) { try { System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select FieldId ,XrayId from IncAData where TypeId ="+ STDId , m_dbConnection); DataSet ds = new DataSet(); m_dataAdapter.Fill(ds); DataTable dt = ds.Tables[0]; if (dt != null) { if (dt.Rows.Count > 0) { string sqlstr = "select XrayData from XRayData where FieldId = " + dt.Rows[0]["FieldId"] + " AND XrayIndex =" + dt.Rows[0]["XrayId"]; m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter(sqlstr, m_dbConnection); ds = new DataSet(); m_dataAdapter.Fill(ds); dt = ds.Tables[0]; if (dt != null) { if (dt.Rows.Count > 0) { DialogResult dr = MessageBox.Show("ID Num:" + STDId+ ", this rule has found the first match object, is it loaded?", "Tip", MessageBoxButtons.OKCancel); if(dr == DialogResult.OK) { m_SubMidWindow.m_MainForm.IsModified = true; if (SelSTDDBBySTDId(STDId)) { if (MessageBox.Show("ID Num:" + STDId + ", the database already contains records of this rule XRay curve, is it overwritten?", "Tip", MessageBoxButtons.OKCancel) == DialogResult.OK) { UpdateSTDXray(int.Parse(STDId), (byte[])dt.Rows[0]["XrayData"]); } } else { UpdateSTDXray(int.Parse(STDId), (byte[])dt.Rows[0]["XrayData"]); } } return Result.Success; } else { return Result.NoMatched; } } else { return Result.NoMatched; } } else { return Result.NoMatched; } } else { return Result.NoMatched; } } catch /*(Exception ee)*/ { MessageBox.Show("Failed to load Xray data!", "Tip"); return Result.Fail; } } private void textbox_STDEditor_MouseLeave(object sender, EventArgs e) { dataGridView_KeyElements.Rows.Clear(); dataGridView_KeyElements.Columns.Clear(); string str_RemoveBlank = textbox_STDEditor.Text.ToString().Replace(" ", ""); str_RemoveBlank = str_RemoveBlank.Replace("\r\n", ""); str_RemoveBlank = str_RemoveBlank.Replace("forth_elem", ""); string[] str_Removeand = System.Text.RegularExpressions.Regex.Split(str_RemoveBlank, "and", System.Text.RegularExpressions.RegexOptions.None); List str_Removeandor = new List(); for (int i = 0; i < str_Removeand.Length; i++) { str_Removeandor.AddRange(System.Text.RegularExpressions.Regex.Split(str_Removeand[i], "or", System.Text.RegularExpressions.RegexOptions.None)); } List list_all = new List(); for (int i = 0; i < str_Removeandor.Count; i++) { list_all.AddRange(str_Removeandor[i].Split(new char[] { '+', '-', '*', '/', '=', '>', '<', '(', ')' })); } List UsingElementL = new List(); for (int i = 0; i < list_all.Count; i++) { if (this.comboBox_PeriodicTable.Items.Contains(list_all[i])) { if (!UsingElementL.Contains(list_all[i])) { UsingElementL.Add(list_all[i]); } } } List SubElements = new List(); for (int i = 0; i < dataGridView_SubElements.ColumnCount; i++) { if (UsingElementL.Contains(dataGridView_SubElements.Rows[0].Cells[i].Value)) { SubElements.Add(dataGridView_SubElements.Rows[0].Cells[i].Value.ToString()); } } dataGridView_SubElements.Rows.Clear(); dataGridView_SubElements.Columns.Clear(); for (int i = 0; i < SubElements.Count; i++) { DataGridViewButtonColumn dataGridViewButtonColumn = new DataGridViewButtonColumn(); dataGridViewButtonColumn.Name = dataGridView_SubElements.Columns.Count.ToString(); dataGridView_SubElements.Columns.Add(dataGridViewButtonColumn); if (dataGridView_SubElements.Rows.Count == 0) { DataGridViewRow row = new DataGridViewRow(); dataGridView_SubElements.Rows.Add(row); } dataGridView_SubElements.Rows[0].Cells[dataGridView_SubElements.ColumnCount - 1].Value = SubElements[i]; } if (SubElements.Count > 0) { for (int i = 0; i < UsingElementL.Count; i++) { if (!SubElements.Contains(UsingElementL[i])) { DataGridViewButtonColumn dataGridViewButtonColumn = new DataGridViewButtonColumn(); dataGridViewButtonColumn.Name = dataGridView_KeyElements.Columns.Count.ToString(); dataGridView_KeyElements.Columns.Add(dataGridViewButtonColumn); if (dataGridView_KeyElements.Rows.Count == 0) { DataGridViewRow row = new DataGridViewRow(); dataGridView_KeyElements.Rows.Add(row); } dataGridView_KeyElements.Rows[0].Cells[dataGridView_KeyElements.ColumnCount - 1].Value = UsingElementL[i]; } } } else { for (int i = 0; i < UsingElementL.Count; i++) { DataGridViewButtonColumn dataGridViewButtonColumn = new DataGridViewButtonColumn(); dataGridViewButtonColumn.Name = dataGridView_KeyElements.Columns.Count.ToString(); dataGridView_KeyElements.Columns.Add(dataGridViewButtonColumn); if (dataGridView_KeyElements.Rows.Count == 0) { DataGridViewRow row = new DataGridViewRow(); dataGridView_KeyElements.Rows.Add(row); } dataGridView_KeyElements.Rows[0].Cells[i].Value = UsingElementL[i]; } } dataGridView_KeyElements.ClearSelection(); dataGridView_SubElements.ClearSelection(); } public void ShowTabXray() { tabSTDStandrad.SelectedIndex = 1; } public void ShowSTD() { tabSTDStandrad.SelectedIndex = 0; } } }