STDEditor.cs 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. using OTS.WinFormsUI.Docking;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Windows.Forms;
  7. using System.Collections;
  8. using System.Text.RegularExpressions;
  9. using System.Text;
  10. using System.IO;
  11. using System.Data;
  12. using OTSCLRINTERFACE;
  13. namespace SpectrumSTDEditor
  14. {
  15. public partial class STDEditor : DockContent
  16. {
  17. #region 全局变量
  18. public SubMidWindow m_SubMidWindow = null;
  19. public int STDId = -1;
  20. Hashtable table_STDEditor;
  21. //数据库操作对象
  22. public SqlLiteClass m_sc = null;
  23. // 电镜设置对象
  24. COTSControlFunExport m_cfun = null;
  25. //Xray图谱用户控件
  26. UXrayControl XrayControl = null;
  27. //全局Xray 用于存储当前选择行的Xray信息
  28. byte[] xrayByteData = null;
  29. // 连接状态
  30. bool m_bConnectionState = false;
  31. //标准Xray信息列表
  32. public List<STDXray> m_STDXrayList = null;
  33. #endregion
  34. public enum Result
  35. {
  36. NoMatched = 0,
  37. Success = 1,
  38. Fail = 2
  39. }
  40. public STDEditor(SubMidWindow SubMidWindow)
  41. {
  42. InitializeComponent();
  43. m_sc = new SqlLiteClass();
  44. m_SubMidWindow = SubMidWindow;
  45. X = this.Width;
  46. Y = this.Height;
  47. XrayControl=new UXrayControl();
  48. setTag(this);
  49. }
  50. public STDEditor(SubMidWindow SubMidWindow, string STDDBaddress)
  51. {
  52. InitializeComponent();
  53. m_sc = new SqlLiteClass(STDDBaddress);
  54. m_SubMidWindow = SubMidWindow;
  55. X = this.Width;
  56. Y = this.Height;
  57. setTag(this);
  58. }
  59. private void STDEditor_Load(object sender, EventArgs e)
  60. {
  61. m_SubMidWindow.m_MainForm.lan = new Language(this);
  62. //添加Xray用户控件
  63. XrayControl = new UXrayControl();
  64. plXray.Controls.Add(XrayControl);
  65. XrayControl.Dock = DockStyle.Fill;
  66. //绑定数据库中的STDXray
  67. m_STDXrayList = new List<STDXray>();
  68. BindSTDXray();
  69. }
  70. float AmplificationFactor = 1;
  71. //private void button_Extend_Click(object sender, EventArgs e)
  72. //{
  73. // if (button_Extend.BackColor == SystemColors.ControlDarkDark)
  74. // {
  75. // //groupBox_Data.Visible = true;
  76. // textbox_STDEditor.Height = (int)Math.Round(150 * AmplificationFactor);
  77. // button_Extend.BackColor = SystemColors.ControlDark;
  78. // }
  79. // else
  80. // {
  81. // button_Extend.BackColor = SystemColors.ControlDarkDark;
  82. // textbox_STDEditor.Height = (int)Math.Round(300* AmplificationFactor);
  83. // }
  84. //}
  85. #region 采集Xray 按钮事件
  86. private void btnCollectXray_Click(object sender, EventArgs e)
  87. {
  88. m_SubMidWindow.m_MainForm.IsModified = true;
  89. string strTime = txtCollectTime.Text.Trim();
  90. if (txtCollectTime.Equals(""))
  91. {
  92. showMessage("Please fill in the collection time name!");
  93. txtCollectTime.Focus();
  94. return;
  95. }
  96. if (!IsMatch(strTime, 1))
  97. {
  98. showMessage("Error collecting time content, the format should be integer!");
  99. txtCollectTime.Focus();
  100. return;
  101. }
  102. if (Convert.ToInt32(strTime) < 50)
  103. {
  104. showMessage("CollectTime must not be less than 100 milliseconds!");
  105. txtCollectTime.Focus();
  106. return;
  107. }
  108. //显示Xray
  109. xrayByteData = IntArrToByteArr(GetCollectXray(strTime));
  110. int color = Convert.ToInt32(m_SubMidWindow.m_MainForm.STDDictionary[STDId].Color);
  111. //根据树节点修改对应的Xray信息
  112. UpdateSTDXray(STDId, xrayByteData);
  113. ShowXrayAtlas(color, xrayByteData);
  114. }
  115. /// <summary>
  116. /// 获取采集当前的Xray信息
  117. /// </summary>
  118. /// <returns></returns>
  119. protected uint[] GetCollectXray(string strTime)
  120. {
  121. try
  122. {
  123. if (null == m_cfun)
  124. {
  125. //m_cfun = COTSControlFunExport.GetControllerInstance();
  126. }
  127. if (ConnectionSem(connectionEnumType.EDSOnlyPointXRay))
  128. {
  129. if (EDSInit())
  130. {
  131. int iSize = 2000;
  132. uint[] iXrayData = new uint[iSize];
  133. //采集XRay数据
  134. if (m_cfun.CollectSpectrum(uint.Parse(strTime), ref iXrayData))
  135. {
  136. return iXrayData;
  137. }
  138. }
  139. }
  140. return null;
  141. }
  142. catch /*(Exception ex)*/
  143. {
  144. return null;
  145. }
  146. finally
  147. {
  148. //EDS过程结束
  149. //m_cfun.EDSFinishedInstance();
  150. //关闭连接
  151. DisConnectSem(connectionEnumType.EDSOnlyPointXRay);
  152. }
  153. }
  154. private void btnReadXray_Click(object sender, EventArgs e)
  155. {
  156. OpenFileDialog fileSel = new OpenFileDialog();
  157. fileSel.Filter = "|*.txt";
  158. if (DialogResult.OK == fileSel.ShowDialog())
  159. {
  160. String str = "";
  161. //读取Xray文件
  162. using (StreamReader sr = new StreamReader(fileSel.FileName))
  163. {
  164. while (!sr.EndOfStream)
  165. {
  166. str += sr.ReadLine() + ",";
  167. }
  168. str = str.Substring(0, str.Length - 1);
  169. string[] xrayData = str.Split(',');
  170. if (xrayData.Length != 2000)
  171. {
  172. return;
  173. }
  174. uint[] data = new uint[xrayData.Length];
  175. for (int i = 0; i < xrayData.Length; i++)
  176. {
  177. data[i] = Convert.ToUInt32(xrayData[i]);
  178. }
  179. //Xray数据
  180. xrayByteData = IntArrToByteArr(data);
  181. int color = Convert.ToInt32(m_SubMidWindow.m_MainForm.STDDictionary[STDId].Color);
  182. //根据树节点修改对应的Xray信息
  183. UpdateSTDXray(STDId, xrayByteData);
  184. ShowXrayAtlas(color, xrayByteData);
  185. }
  186. }
  187. }
  188. /// <summary>
  189. /// 将int[] 转换为 byte[]
  190. /// </summary>
  191. /// <param name="intArr"></param>
  192. /// <returns></returns>
  193. public static byte[] IntArrToByteArr(uint[] intArr)
  194. {
  195. int intSize = sizeof(uint) * intArr.Length;
  196. byte[] bytArr = new byte[intSize];
  197. //申请一块非托管内存  
  198. //IntPtr ptr = Marshal.AllocHGlobal(intSize);
  199. ////复制int数组到该内存块  
  200. //Marshal.Copy(intArr, 0, ptr, intArr.Length);
  201. ////复制回byte数组  
  202. //Marshal.Copy(ptr, bytArr, 0, bytArr.Length);
  203. ////释放申请的非托管内存  
  204. //Marshal.FreeHGlobal(ptr);
  205. for (int i = 0; i < intArr.Length; i++)
  206. {
  207. byte[] c1 = BitConverter.GetBytes(intArr[i]);
  208. c1.CopyTo(bytArr, i * 4);
  209. }
  210. return bytArr;
  211. }
  212. /// 将颜色对象转换为uint
  213. /// </summary>
  214. /// <param name="color"></param>
  215. /// <returns></returns>
  216. public uint ParseRGB(Color color)
  217. {
  218. return (uint)(((uint)color.B << 16) | (ushort)(((ushort)color.G << 8) | color.R));
  219. }
  220. #region 显示Xray图谱
  221. /// <summary>
  222. /// 显示Xray图谱
  223. /// </summary>
  224. public void ShowXrayAtlas(int colorValue = 0, byte[] XrayData = null)
  225. {
  226. DataTable dt = new DataTable();
  227. dt.Columns.Add("Color", typeof(int));
  228. dt.Columns.Add("SPEC", typeof(byte[]));
  229. DataRow dRow = dt.NewRow();
  230. dRow["Color"] = colorValue;
  231. if (XrayData != null)
  232. {
  233. dRow["SPEC"] = XrayData;
  234. }
  235. else
  236. {
  237. dRow["SPEC"] = new byte[8000];
  238. }
  239. //将选择行的数据传递给Xray控件
  240. XrayControl.Dr = dRow;
  241. XrayControl.Refresh();
  242. XrayData = null;
  243. }
  244. #endregion
  245. #region 常用数据验证的封装,数字字符的验证
  246. /// <summary>
  247. /// 常用数据验证的封装,数字字符的验证
  248. /// </summary>
  249. /// <param name="inputVal">需要验证的数值【字符串,或者数字】</param>
  250. /// <param name="type">类型为哪一个验证</param>
  251. /// <returns>如果验证成功则返回True,否则返回false</returns>
  252. public bool IsMatch(string inputVal, int type)
  253. {
  254. switch (type)
  255. {
  256. case 0:
  257. return Regex.IsMatch(inputVal, @"^[1-9]d*$"); //匹配正整数
  258. case 1:
  259. return Regex.IsMatch(inputVal, @"^-?\d+$"); //匹配整数
  260. case 2:
  261. return Regex.IsMatch(inputVal, @"^[A-Za-z0-9]+$"); //匹配由数字和26个英文字母组成的字符串
  262. case 3:
  263. 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]*))$"); //匹配正浮点数
  264. case 4:
  265. return Regex.IsMatch(inputVal, @"^[\u4e00-\u9fa5]{0,}$"); //匹配汉字
  266. case 5:
  267. return Regex.IsMatch(inputVal, @"^[0-9]+(.[0-9]{1,3})?$"); //匹配1~3位小数的正实数
  268. case 6:
  269. return Regex.IsMatch(inputVal, @"^[A-Za-z]+$"); //匹配英文字符
  270. default:
  271. return true;
  272. }
  273. }
  274. #endregion
  275. #region 弹出提示
  276. /// <summary>
  277. /// 弹出提示
  278. /// </summary>
  279. /// <param name="strContent"></param>
  280. protected void showMessage(string strContent)
  281. {
  282. MessageBox.Show(strContent, "Tip");
  283. }
  284. #endregion
  285. #region 连接与关闭设备
  286. public bool ConnectionSem(connectionEnumType connectionType)
  287. {
  288. //获取连接电镜类型
  289. string connTypeStr = GetConnectionType(connectionType);
  290. //连接电镜标识
  291. bool bDisConnResult = false;
  292. //判断连接状态
  293. if (!m_bConnectionState)
  294. {
  295. //连接电镜设置
  296. bDisConnResult = m_cfun.ConncetSem();
  297. }
  298. if (bDisConnResult)
  299. {
  300. m_bConnectionState = true;
  301. }
  302. else
  303. {
  304. m_bConnectionState = false;
  305. }
  306. return bDisConnResult;
  307. }
  308. public bool DisConnectSem(connectionEnumType disConnectType)
  309. {
  310. //获取关闭电镜类型
  311. string connTypeStr = GetConnectionType(disConnectType);
  312. bool bDisConnResult = false;
  313. if (m_bConnectionState)
  314. {
  315. bDisConnResult = m_cfun.DisconnectSem();
  316. }
  317. if (bDisConnResult)
  318. {
  319. m_bConnectionState = false;
  320. }
  321. else
  322. {
  323. m_bConnectionState = true;
  324. }
  325. return bDisConnResult;
  326. }
  327. private string GetConnectionType(connectionEnumType connectionType)
  328. {
  329. string connString = string.Empty;
  330. switch (connectionType)
  331. {
  332. //设置单点采集文字内容
  333. case connectionEnumType.EDSOnlyPointXRay:
  334. connString = "OnlyPointXRay";
  335. break;
  336. //设置多点采集文字内容
  337. case connectionEnumType.EDSMultiPointXRay:
  338. connString = "MultiPointXRay";
  339. break;
  340. //设置面采集文字内容
  341. case connectionEnumType.EDSAreaXRay:
  342. connString = "AreaXRay";
  343. break;
  344. //设置图片
  345. case connectionEnumType.ScanImage:
  346. connString = "Image";
  347. break;
  348. default: break;
  349. }
  350. return connString;
  351. }
  352. /// <summary>
  353. /// EDS初始化
  354. /// </summary>
  355. public bool EDSInit()
  356. {
  357. bool bResult = false;
  358. //线程调用 加载
  359. bResult = m_cfun.EDSInit();
  360. return bResult;
  361. }
  362. public enum connectionEnumType
  363. {
  364. EDSOnlyPointXRay = 0,
  365. EDSMultiPointXRay = 1,
  366. EDSAreaXRay = 2,
  367. ScanImage = 3
  368. }
  369. /// <summary>
  370. /// 标准Xray状态
  371. /// </summary>
  372. public enum STDXrayInfoState
  373. {
  374. Add = 0,
  375. Modify = 1,
  376. Delete = 2
  377. }
  378. #endregion
  379. /// <summary>
  380. /// 记录修改后的标准中的Xray信息
  381. /// </summary>
  382. public class STDXray
  383. {
  384. /// <summary>
  385. /// 标准编号
  386. /// </summary>
  387. private string stdID;
  388. /// <summary>
  389. /// Xray数据
  390. /// </summary>
  391. private byte[] xrayData;
  392. /// <summary>
  393. /// 状态
  394. /// </summary>
  395. private int infoState = -1;
  396. public string StdID { get => stdID; set => stdID = value; }
  397. public byte[] XrayData { get => xrayData; set => xrayData = value; }
  398. public int InfoState { get => infoState; set => infoState = value; }
  399. }
  400. #endregion
  401. #region Xray 与 STDDB数据操作
  402. /// <summary>
  403. /// 绑定数据库中已存在的Xray信息
  404. /// </summary>
  405. public void BindSTDXray()
  406. {
  407. //清空STDXray列表中的集合
  408. m_STDXrayList.Clear();
  409. //查询
  410. DataTable dt = m_sc.GetDTFormSysSTDBySQLString("select Id,SPEC from STDMinerals");
  411. if (dt != null)
  412. {
  413. if (dt.Rows.Count > 0)
  414. {
  415. foreach (DataRow item in dt.Rows)
  416. {
  417. STDXray sXray = new STDXray();
  418. sXray.StdID = item["Id"].ToString();
  419. sXray.XrayData = (byte[])item["SPEC"];
  420. sXray.InfoState = 1;
  421. m_STDXrayList.Add(sXray);
  422. }
  423. }
  424. }
  425. }
  426. bool SaveOtherDataToXray(string STDDBAddress, int id)
  427. {
  428. #region 数据库存贮方式一
  429. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + STDDBAddress + "'");
  430. System.Data.SQLite.SQLiteCommand cmm = m_dbConnection.CreateCommand();
  431. try
  432. {
  433. m_dbConnection.Open();
  434. //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 + ");");
  435. string insertstr = ("insert into STDMinerals(name) values(" + m_SubMidWindow.m_MainForm.STDDictionary[id].StrName + ");");
  436. cmm.CommandText = insertstr;
  437. cmm.ExecuteNonQuery();
  438. }
  439. catch (Exception ex)
  440. {
  441. MessageBox.Show(ex.ToString());
  442. return false;
  443. }
  444. m_dbConnection.Close();
  445. #endregion
  446. return true;
  447. }
  448. /// <summary>
  449. /// 保存STDXray至数据库
  450. /// </summary>
  451. public void SaveSTDXray()
  452. {
  453. ArrayList delArrayList = new ArrayList();
  454. if (m_STDXrayList != null)
  455. {
  456. //if (m_STDXrayList.Count > 0)
  457. //{
  458. foreach (STDXray item in m_STDXrayList)
  459. {
  460. int infoState = item.InfoState;
  461. //添加状态 根据Xray信息当前只能按照一条操作
  462. if (infoState == (int)STDXrayInfoState.Add)
  463. {
  464. //if (item.XrayData == null)
  465. //{
  466. // item.XrayData = new byte[8000];
  467. //}
  468. //string strvaluename = "@data";
  469. ////编辑添加语句
  470. //StringBuilder strSqlAdd = new StringBuilder();
  471. //strSqlAdd.Append("insert into STDMinerals(Id,SPEC) values(" + item.StdID + "," + strvaluename + ");");
  472. //bool addResult = m_sc.ExecuteBlob(strSqlAdd.ToString(), strvaluename, (byte[])item.XrayData, ((byte[])item.XrayData).Length);
  473. bool addResult = false;
  474. //判断STDID在数据库中是否存在
  475. bool selResult = SelSTDDBBySTDId(item.StdID);
  476. if (selResult)
  477. {
  478. //addResult = UpdateSTDDB(item.StdID, xrayByteData);
  479. addResult = UpdateSTDDB(item.StdID, item.XrayData);
  480. }
  481. else
  482. {
  483. addResult = AddSTDDB(item.StdID, item.XrayData);
  484. }
  485. if (!addResult)
  486. {
  487. continue;
  488. }
  489. }
  490. //修改状态
  491. else if (infoState == (int)STDXrayInfoState.Modify)
  492. {
  493. //判断STDID在数据库中是否存在
  494. bool selResult = SelSTDDBBySTDId(item.StdID);
  495. if (selResult)
  496. {
  497. UpdateSTDDB(item.StdID, item.XrayData);
  498. }
  499. else
  500. {
  501. AddSTDDB(item.StdID, item.XrayData);
  502. }
  503. }
  504. //删除状态 编辑批量删除语句
  505. else if (infoState == (int)STDXrayInfoState.Delete)
  506. {
  507. //编辑删除语句
  508. delArrayList.Add("delete from STDMinerals where id=" + item.StdID + ";");
  509. }
  510. }
  511. //批量删除
  512. if (delArrayList.Count > 0)
  513. {
  514. bool delResult = m_sc.DelSTDInfo(delArrayList);
  515. }
  516. //}
  517. }
  518. }
  519. /// <summary>
  520. /// 添加STD信息至数据库
  521. /// </summary>
  522. protected bool AddSTDDB(string stdID, byte[] XrayData)
  523. {
  524. if (XrayData == null)
  525. {
  526. XrayData = new byte[8000];
  527. }
  528. string strvaluename = "@data";
  529. StringBuilder sqlStrAdd = new StringBuilder();
  530. sqlStrAdd.Append("insert into STDMinerals(Id,SPEC) values(" + stdID + "," + strvaluename + ");");
  531. bool addResult = m_sc.ExecuteBlob(sqlStrAdd.ToString(), strvaluename, (byte[])XrayData, ((byte[])XrayData).Length);
  532. return addResult;
  533. }
  534. /// <summary>
  535. /// 修改STD信息至数据库
  536. /// </summary>
  537. protected bool UpdateSTDDB(string stdID, byte[] XrayData)
  538. {
  539. if (XrayData == null)
  540. {
  541. XrayData = new byte[8000];
  542. }
  543. string strValueName = "@data";
  544. StringBuilder sqlStrUpdate = new StringBuilder();
  545. sqlStrUpdate.Append("update STDMinerals set SPEC=" + strValueName +" where id=" + stdID + ";");
  546. bool updateResult = m_sc.ExecuteBlob(sqlStrUpdate.ToString(), strValueName, (byte[])XrayData, ((byte[])XrayData).Length);
  547. return updateResult;
  548. }
  549. /// <summary>
  550. /// 查询是否存在标准编号
  551. /// </summary>
  552. /// <param name="stdID"></param>
  553. protected bool SelSTDDBBySTDId(string stdID)
  554. {
  555. bool selResult = false;
  556. //编辑查询语句
  557. StringBuilder sqlStr = new StringBuilder();
  558. sqlStr.Append("select * from STDMinerals where id=" + stdID + "");
  559. DataTable dt = m_sc.GetDTFormSysSTDBySQLString(sqlStr.ToString());
  560. if (dt != null)
  561. {
  562. if (dt.Rows.Count > 0)
  563. {
  564. //已存在状态
  565. selResult = true;
  566. }
  567. }
  568. return selResult;
  569. }
  570. /// <summary>
  571. /// 根据选择树节点显示对应的Xray信息
  572. /// </summary>
  573. public void SelSTDXray(int Key, STDdata sT)
  574. {
  575. if (sT == null)
  576. {
  577. return;
  578. }
  579. if (m_STDXrayList != null)
  580. {
  581. //if (m_STDXrayList.Count > 0)
  582. //{
  583. bool isExists = false;
  584. foreach (STDXray item in m_STDXrayList)
  585. {
  586. //修改XrayData信息
  587. if (item.StdID == Key.ToString())
  588. {
  589. string selColor = sT.Color;
  590. int colorValue = Convert.ToInt32(selColor);
  591. //显示Xray信息
  592. if (item.XrayData != null)
  593. {
  594. isExists = true;
  595. ShowXrayAtlas(colorValue, item.XrayData);
  596. }
  597. break;
  598. }
  599. }
  600. if (!isExists)
  601. {
  602. ShowXrayAtlas();
  603. }
  604. //}
  605. }
  606. }
  607. public void ShowEditContent(string stdID)
  608. {
  609. if (m_STDXrayList != null)
  610. {
  611. for (int i = 0; i < m_STDXrayList.Count; i++)
  612. {
  613. if (m_STDXrayList[i].StdID == stdID)
  614. {
  615. //获取修改行的信息
  616. xrayByteData = (byte[])m_STDXrayList[i].XrayData;
  617. }
  618. }
  619. }
  620. }
  621. /// <summary>
  622. /// 根据STDId修改对应的Xray信息
  623. /// </summary>
  624. /// <param name="STDId"></param>
  625. protected void UpdateSTDXray(int STDId, byte[] xrayData)
  626. {
  627. if (m_STDXrayList != null)
  628. {
  629. //if (m_STDXrayList.Count > 0)
  630. //{
  631. bool isExists = false;
  632. foreach (STDXray item in m_STDXrayList)
  633. {
  634. //修改XrayData信息
  635. if (item.StdID == STDId.ToString())
  636. {
  637. item.XrayData = xrayData;
  638. item.InfoState = (int)STDXrayInfoState.Modify;
  639. isExists = true;
  640. break;
  641. }
  642. }
  643. if (!isExists)
  644. {
  645. if (!STDId.ToString().Equals(""))
  646. {
  647. //添加STD XrayData信息
  648. STDXray stdXray = new STDXray();
  649. stdXray.StdID = STDId.ToString();
  650. stdXray.XrayData = xrayData;
  651. stdXray.InfoState = (int)STDXrayInfoState.Add;
  652. m_STDXrayList.Add(stdXray);
  653. }
  654. }
  655. //}
  656. }
  657. }
  658. /// <summary>
  659. /// 添加STDXray信息
  660. /// </summary>
  661. /// <param name="STDid">STDid</param>
  662. /// <param name="xrayData">Xray信息</param>
  663. /// <returns></returns>
  664. protected bool AddSTDXray(int STDId, byte[] xrayData)
  665. {
  666. bool addResult = false;
  667. if (m_STDXrayList != null)
  668. {
  669. //if (m_STDXrayList.Count > 0)
  670. //{
  671. if (!STDId.ToString().Equals(""))
  672. {
  673. //添加STD XrayData信息
  674. STDXray stdXray = new STDXray();
  675. stdXray.StdID = STDId.ToString();
  676. stdXray.XrayData = xrayData;
  677. stdXray.InfoState = (int)STDXrayInfoState.Add;
  678. m_STDXrayList.Add(stdXray);
  679. addResult = true;
  680. }
  681. //}
  682. }
  683. return addResult;
  684. }
  685. /// <summary>
  686. /// 删除STDXray信息
  687. /// </summary>
  688. /// <param name="STDId">所选要删除的STDId</param>
  689. /// <returns></returns>
  690. public bool DelSTDXray(int STDId)
  691. {
  692. bool delResult = false;
  693. if (m_STDXrayList != null)
  694. {
  695. //if (m_STDXrayList.Count > 0)
  696. //{
  697. if (!STDId.ToString().Equals(""))
  698. {
  699. foreach (STDXray item in m_STDXrayList)
  700. {
  701. //修改XrayData信息
  702. if (item.StdID == STDId.ToString())
  703. {
  704. item.InfoState = (int)STDXrayInfoState.Delete;
  705. delResult = true;
  706. break;
  707. }
  708. }
  709. }
  710. //}
  711. }
  712. return delResult;
  713. }
  714. #endregion
  715. #region 控制BSE、化学式、元素本文框中不能输入下划线
  716. /// <summary>
  717. /// 控件不能输入下划线
  718. /// </summary>
  719. /// <param name="sender"></param>
  720. /// <param name="e"></param>
  721. private void textBox_KeyPress(object sender, KeyPressEventArgs e)
  722. {
  723. if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar >= 'A' && e.KeyChar <= 'Z') ||
  724. (e.KeyChar >= 'a' && e.KeyChar <= 'z') || (e.KeyChar == 8) || !(e.KeyChar == '_'))
  725. {
  726. e.Handled = false;
  727. }
  728. else
  729. {
  730. e.Handled = true;
  731. }
  732. }
  733. #endregion
  734. private void btnTest_Click(object sender, EventArgs e)
  735. {
  736. XrayContrastForm xrayContrastForm = new XrayContrastForm();
  737. xrayContrastForm.STDDictionary = m_SubMidWindow.m_MainForm.STDDictionary;
  738. xrayContrastForm.ShowDialog();
  739. }
  740. private float X = 1;
  741. private float Y = 1;
  742. private void STDEditor_Resize(object sender, EventArgs e)
  743. {
  744. //float newx = (this.Width) / X;
  745. //float newy = this.Height / Y;
  746. //AmplificationFactor = newy;
  747. //setControls(newx, newy, this.tabSTDStandrad.TabPages[0]);
  748. ////button_Extend.BackColor = SystemColors.ControlDarkDark;
  749. }
  750. private void setTag(Control cons)
  751. {
  752. foreach (Control con in cons.Controls)
  753. {
  754. con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;
  755. if (con.Controls.Count > 0)
  756. setTag(con);
  757. }
  758. }
  759. private void setControls(float newx, float newy, Control cons)
  760. {
  761. foreach (Control con in cons.Controls)
  762. {
  763. if (false)
  764. {
  765. }
  766. else
  767. {
  768. string[] mytag = con.Tag.ToString().Split(':');
  769. float a = Convert.ToSingle(mytag[0]) * newx;
  770. con.Width = (int)a;
  771. a = Convert.ToSingle(mytag[1]) * newy;
  772. con.Height = (int)(a);
  773. a = Convert.ToSingle(mytag[2]) * newx;
  774. con.Left = (int)(a);
  775. a = Convert.ToSingle(mytag[3]) * newy;
  776. con.Top = (int)(a);
  777. Single currentSize;
  778. if (con.Name == "button_Extend")
  779. {
  780. currentSize = 6f;
  781. }
  782. else
  783. {
  784. if (Math.Max(newx, newy) < 1)
  785. {
  786. currentSize = Convert.ToSingle(mytag[4]) * 1;
  787. }
  788. else
  789. {
  790. currentSize = Convert.ToSingle(mytag[4]) * Math.Max(newx, newy);
  791. }
  792. }
  793. con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
  794. if (con.Controls.Count > 0)
  795. {
  796. setControls(newx, newy, con);
  797. }
  798. }
  799. }
  800. }
  801. Form_PeriodicTable form_PeriodicTable;
  802. private void button_PeriodicTableSwitch_Click(object sender, EventArgs e)
  803. {
  804. if (form_PeriodicTable == null) //如果子窗体为空则创造实例 并显示
  805. {
  806. form_PeriodicTable = new Form_PeriodicTable();
  807. if (table_STDEditor["language"].ToString() == "EN")
  808. {
  809. form_PeriodicTable.BackgroundImage = global::SpectrumSTDEditor.Properties.Resources.PeriodicTable_EN;
  810. }
  811. else
  812. {
  813. form_PeriodicTable.BackgroundImage = global::SpectrumSTDEditor.Properties.Resources.PeriodicTable_ZH;
  814. }
  815. form_PeriodicTable.Show();
  816. }
  817. else
  818. {
  819. if (form_PeriodicTable.IsDisposed) //若子窗体关闭 则打开新子窗体 并显示
  820. {
  821. form_PeriodicTable = new Form_PeriodicTable();
  822. if (table_STDEditor["language"].ToString() == "EN")
  823. {
  824. form_PeriodicTable.BackgroundImage = global::SpectrumSTDEditor.Properties.Resources.PeriodicTable_EN;
  825. }
  826. else
  827. {
  828. form_PeriodicTable.BackgroundImage = global::SpectrumSTDEditor.Properties.Resources.PeriodicTable_ZH;
  829. }
  830. form_PeriodicTable.Show();
  831. }
  832. else
  833. {
  834. form_PeriodicTable.Activate(); //使子窗体获得焦点
  835. }
  836. }
  837. }
  838. private void button_ImportFromResult_Click(object sender, EventArgs e)
  839. {
  840. OpenFileDialog openFileDialog = new OpenFileDialog();
  841. openFileDialog.Filter = "(*.db)|*.db";
  842. openFileDialog.RestoreDirectory = true;
  843. openFileDialog.FilterIndex = 1;
  844. if (openFileDialog.ShowDialog() == DialogResult.OK)
  845. {
  846. try
  847. {
  848. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + openFileDialog.FileName + "'");
  849. m_dbConnection.Open();
  850. 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();
  851. Result ret=ImportFromResult(STDId, m_dbConnection);
  852. if(ret== Result.NoMatched)
  853. {
  854. MessageBox.Show("No matching energy spectrum data was found", "Tip");
  855. }
  856. m_dbConnection.Close();
  857. int color = Convert.ToInt32(m_SubMidWindow.m_MainForm.STDDictionary[int.Parse(STDId)].Color);
  858. for(int i=0;i< m_STDXrayList.Count;i++)
  859. {
  860. if(m_STDXrayList[i].StdID== STDId)
  861. {
  862. ShowXrayAtlas(color, m_STDXrayList[i].XrayData);
  863. break;
  864. }
  865. }
  866. }
  867. catch /*(Exception ex)*/
  868. {
  869. MessageBox.Show("Failed to load Xray data!", "Tip");
  870. }
  871. }
  872. }
  873. public Result ImportFromResult(string STDId, System.Data.SQLite.SQLiteConnection m_dbConnection)
  874. {
  875. try
  876. {
  877. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select FieldId ,XrayId from IncAData where TypeId ="+ STDId , m_dbConnection);
  878. DataSet ds = new DataSet();
  879. m_dataAdapter.Fill(ds);
  880. DataTable dt = ds.Tables[0];
  881. if (dt != null)
  882. {
  883. if (dt.Rows.Count > 0)
  884. {
  885. string sqlstr = "select XrayData from XRayData where FieldId = " + dt.Rows[0]["FieldId"] + " AND XrayIndex =" + dt.Rows[0]["XrayId"];
  886. m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter(sqlstr, m_dbConnection);
  887. ds = new DataSet();
  888. m_dataAdapter.Fill(ds);
  889. dt = ds.Tables[0];
  890. if (dt != null)
  891. {
  892. if (dt.Rows.Count > 0)
  893. {
  894. DialogResult dr = MessageBox.Show("ID Num:" + STDId+ ", this rule has found the first match object, is it loaded?", "Tip", MessageBoxButtons.OKCancel);
  895. if(dr == DialogResult.OK)
  896. {
  897. m_SubMidWindow.m_MainForm.IsModified = true;
  898. if (SelSTDDBBySTDId(STDId))
  899. {
  900. if (MessageBox.Show("ID Num:" + STDId + ", the database already contains records of this rule XRay curve, is it overwritten?", "Tip", MessageBoxButtons.OKCancel) == DialogResult.OK)
  901. {
  902. UpdateSTDXray(int.Parse(STDId), (byte[])dt.Rows[0]["XrayData"]);
  903. }
  904. }
  905. else
  906. {
  907. UpdateSTDXray(int.Parse(STDId), (byte[])dt.Rows[0]["XrayData"]);
  908. }
  909. }
  910. return Result.Success;
  911. }
  912. else
  913. {
  914. return Result.NoMatched;
  915. }
  916. }
  917. else
  918. {
  919. return Result.NoMatched;
  920. }
  921. }
  922. else
  923. {
  924. return Result.NoMatched;
  925. }
  926. }
  927. else
  928. {
  929. return Result.NoMatched;
  930. }
  931. }
  932. catch /*(Exception ee)*/
  933. {
  934. MessageBox.Show("Failed to load Xray data!", "Tip");
  935. return Result.Fail;
  936. }
  937. }
  938. public void ShowTabXray()
  939. {
  940. tabSTDStandrad.SelectedIndex = 1;
  941. }
  942. public void ShowSTD()
  943. {
  944. tabSTDStandrad.SelectedIndex = 0;
  945. }
  946. }
  947. }