StageTest.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. using NLog.Fluent;
  2. using OTSCLRINTERFACE;
  3. using OTSModelSharp.ServiceCenter;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.Drawing.Drawing2D;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15. using System.Xml;
  16. namespace OTSSysMgrApp
  17. {
  18. public partial class StageTest : Form
  19. {
  20. #region 全部变量声明
  21. RecommendedConfiguration recommendedConfiguration;
  22. //连接状态
  23. bool ConnectionState = false;
  24. static string xmlFilePath = System.Configuration.ConfigurationManager.ConnectionStrings["XMLFilePath"].ConnectionString;
  25. static string LogPath = System.Configuration.ConfigurationManager.ConnectionStrings["LogPath"].ConnectionString;
  26. static NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  27. ISemController m_SemHardwareMgr = null;
  28. IEDSController m_EDSHardwareMgr = null;
  29. //图片
  30. Bitmap bitmap = null;
  31. //国际化
  32. Language lan;
  33. //国际化存储信息
  34. Hashtable table;
  35. public enum connectionEnumType
  36. {
  37. EDSOnlyPointXRay = 0,
  38. EDSMultiPointXRay = 1,
  39. EDSAreaXRay = 2,
  40. ScanImage = 3
  41. }
  42. #endregion
  43. public StageTest()
  44. {
  45. InitializeComponent();
  46. m_EDSHardwareMgr = EDSController.GetEDSController(1024, 768, 5000, true, "");
  47. m_SemHardwareMgr = SemController.GetSEMController();
  48. lan = new Language(this);
  49. table = lan.GetNameTable(this.Name);
  50. if (!ConnectionState)
  51. {
  52. //和电镜建立通讯连接
  53. ConnectionState = m_SemHardwareMgr.Connect();
  54. ///获取当前电镜的ID号
  55. //SemType = cfun.GetSemType();
  56. if (!ConnectionState)
  57. {
  58. log.Error(table["message3"].ToString());
  59. }
  60. else
  61. {
  62. string str = table["str1"].ToString();
  63. log.Info("The energy spectrum is connected");
  64. }
  65. }
  66. else
  67. {
  68. string str = table["str2"].ToString();
  69. ConnectionState = false;
  70. }
  71. }
  72. private void btnGPositionXYR_Click(object sender, EventArgs e)
  73. {
  74. try
  75. {
  76. //赋值
  77. double PositionX = 0;
  78. double PositionY = 0;
  79. double PositionR = 0;
  80. //获取参数
  81. bool result = m_SemHardwareMgr.GetSemPositionXY(ref PositionX, ref PositionY, ref PositionR);
  82. if (result)
  83. {
  84. //赋值 显示
  85. tbPositionX.Text = Convert.ToDouble(PositionX).ToString();
  86. tbPositionY.Text = Convert.ToDouble(PositionY).ToString();
  87. tbPositionR.Text = Convert.ToDouble(PositionR).ToString();
  88. log.Info("X,Y-axis coordinates:(" + tbPositionX.Text + "," + tbPositionY.Text + ")");
  89. }
  90. else
  91. {
  92. //配置结果提示
  93. ShowMessage(6);
  94. log.Error("Failed to obtain the X and Y coordinates");
  95. }
  96. }
  97. catch (Exception ex)
  98. {
  99. //记录日志信息(异常日志)
  100. log.Error(ex.Message.ToString());
  101. }
  102. }
  103. private void btn_setpositionxyr_Click(object sender, EventArgs e)
  104. {
  105. try
  106. {
  107. if (!IsNull(tbSPositionX))
  108. {
  109. this.Focus();
  110. return;
  111. }
  112. if (!IsType(tbSPositionX.Text, 2))
  113. {
  114. this.Focus();
  115. return;
  116. }
  117. if (!IsNull(tbSPositionY))
  118. {
  119. this.Focus();
  120. return;
  121. }
  122. if (!IsType(tbSPositionY.Text, 2))
  123. {
  124. this.Focus();
  125. return;
  126. }
  127. if (!IsNull(tbSPositionR))
  128. {
  129. this.Focus();
  130. return;
  131. }
  132. if (!IsType(tbSPositionR.Text, 2))
  133. {
  134. this.Focus();
  135. return;
  136. }
  137. //赋值
  138. double PositionX = Convert.ToDouble(tbSPositionX.Text);
  139. double PositionY = Convert.ToDouble(tbSPositionY.Text);
  140. double PositionR = Convert.ToDouble(tbSPositionR.Text);
  141. bool result = m_SemHardwareMgr.MoveSEMToPoint(PositionX, PositionY, PositionR);
  142. if (result)
  143. {
  144. ShowMessage(3);
  145. log.Info("Set successfully, current X and Y coordinates should be:(" + PositionX.ToString() + "," + PositionY.ToString() + PositionR.ToString() + ")");
  146. }
  147. else
  148. {
  149. ShowMessage(4);
  150. log.Error("Failed to set X and Y coordinates!");
  151. }
  152. }
  153. catch (Exception ex)
  154. {
  155. //记录日志信息
  156. log.Error(ex.Message.ToString());
  157. }
  158. }
  159. private void btn_Recommendedconfiguration_Click(object sender, EventArgs e)
  160. {
  161. if (recommendedConfiguration == null || recommendedConfiguration.IsDisposed)
  162. {
  163. recommendedConfiguration = new RecommendedConfiguration(Language.ReadDefaultLanguage());
  164. recommendedConfiguration.Show();
  165. }
  166. else
  167. {
  168. recommendedConfiguration.Activate();
  169. }
  170. }
  171. private void btn_continuousshot_Click(object sender, EventArgs e)
  172. {
  173. btn_continuousshot.Enabled = false;
  174. btn_confirm.Enabled = false;
  175. this.Refresh();
  176. Graphics g = panel2.CreateGraphics();
  177. //g.SmoothingMode = SmoothingMode.HighSpeed;
  178. //g.CompositingQuality = CompositingQuality.HighSpeed;
  179. g.PixelOffsetMode = PixelOffsetMode.HighQuality;//高质量低速度呈现
  180. g.SmoothingMode = SmoothingMode.HighQuality;// 指定高质量、低速度呈现。
  181. g.InterpolationMode = InterpolationMode.NearestNeighbor;
  182. ISemController m_Sem = SemController.GetSEMController();
  183. bool flag = m_Sem.Connect();
  184. //电镜设置对象
  185. var cfun = ScanController.GetScanController();
  186. bool IsConnec = cfun.Init();
  187. double Magnification = 0;
  188. m_Sem.GetMagnification(ref Magnification);
  189. //double a_dScanFieldSizeX = 0;
  190. //double a_dScanFieldSizeY = 0;
  191. //m_Sem.GetScanFieldSize(ref a_dScanFieldSizeX,ref a_dScanFieldSizeY);
  192. double scanFieldSize100 = 0;
  193. double.TryParse(tB_scanFieldSize100.Text, out scanFieldSize100);
  194. double scanFieldSize = scanFieldSize100 * 100 / Magnification;
  195. double PositionX = 0;
  196. double PositionY = 0;
  197. double PositionR = 0;
  198. m_Sem.GetSemPositionXY(ref PositionX, ref PositionY, ref PositionR);
  199. double PositionXO = PositionX;
  200. double PositionYO = PositionY;
  201. //double PositionR0 = PositionR;
  202. int width = 1024;
  203. int height = 768;
  204. //设置图像分辨率
  205. if (cb_imageresolution.Text.Trim() == "")
  206. {
  207. MessageBox.Show("imageresolution cannot be null!");
  208. return;
  209. }
  210. else
  211. {
  212. width = Convert.ToInt32(cb_imageresolution.Text.Split('*')[0]);
  213. height = Convert.ToInt32(cb_imageresolution.Text.Split('*')[1]);
  214. }
  215. this.Refresh();
  216. double scanFieldSizeH = scanFieldSize * height / width;
  217. m_Sem.SetScanExternal(true);
  218. try
  219. {
  220. for (int i = 0; i < 4; i++)
  221. {
  222. if (i == 0)
  223. {
  224. }
  225. else if (i == 1)
  226. {
  227. switch (cb_Xdirection.SelectedItem.ToString())
  228. {
  229. case "RIGHT_TOWARD": PositionX += scanFieldSize; break;
  230. default: PositionX -= scanFieldSize; break;
  231. }
  232. m_Sem.MoveSEMToPoint(PositionX, PositionY);
  233. }
  234. else if (i == 2)
  235. {
  236. switch (cb_Ydirection.SelectedItem.ToString())
  237. {
  238. case "UP_TOWARD": PositionY -= scanFieldSizeH; break;
  239. default: PositionY += scanFieldSizeH; break;
  240. }
  241. m_Sem.MoveSEMToPoint(PositionX, PositionY);
  242. }
  243. else
  244. {
  245. switch (cb_Xdirection.SelectedItem.ToString())
  246. {
  247. case "RIGHT_TOWARD": PositionX -= scanFieldSize; break;
  248. default: PositionX += scanFieldSize; break;
  249. }
  250. m_Sem.MoveSEMToPoint(PositionX, PositionY);
  251. }
  252. //Thread.Sleep(50);
  253. byte[] ImageByte = new byte[width * height];
  254. bool resultValue = GetScanImage(width, height, "2", ref ImageByte);
  255. Size size = new Size(panel2.Width / 2, panel2.Height / 2);
  256. if (resultValue)
  257. {
  258. if (ImageByte != null)
  259. {
  260. var bitmap = CImageHandler.ToGrayBitmap(ImageByte, width, height);
  261. switch (i)
  262. {
  263. case 0:
  264. Point point0 = new Point(0, 0);
  265. Rectangle rect0 = new Rectangle(point0, size);
  266. g.DrawImage(bitmap, rect0);
  267. break;
  268. case 1:
  269. Point point1 = new Point(panel2.Width / 2, 0);
  270. Rectangle rect1 = new Rectangle(point1, size);
  271. g.DrawImage(bitmap, rect1);
  272. break;
  273. case 2:
  274. Point point2 = new Point(panel2.Width / 2, panel2.Height / 2);
  275. Rectangle rect2 = new Rectangle(point2, size);
  276. g.DrawImage(bitmap, rect2);
  277. break;
  278. case 3:
  279. Point point3 = new Point(0, panel2.Height / 2);
  280. Rectangle rect3 = new Rectangle(point3, size);
  281. g.DrawImage(bitmap, rect3);
  282. break;
  283. }
  284. //this.Refresh();
  285. }
  286. }
  287. }
  288. m_Sem.MoveSEMToPoint(PositionXO, PositionYO);
  289. }
  290. catch (Exception ex)
  291. {
  292. log.Error("continuousshot--错误信息:" + ex.ToString());
  293. }
  294. finally
  295. {
  296. m_Sem.SetScanExternal(false);
  297. btn_continuousshot.Enabled = true;
  298. btn_confirm.Enabled = true;
  299. btn_continuousshot.Refresh();
  300. g.Dispose();
  301. }
  302. }
  303. private bool GetScanImage(int iWidth, int iHeigh, string DwellTime, ref byte[] bImageData)
  304. {
  305. //电镜设置对象
  306. var scan = ScanController.GetScanController();
  307. int GetImgCount = 0;
  308. try
  309. {
  310. //连接电镜
  311. bool IsConnec = scan.Init();
  312. if (!IsConnec)
  313. {
  314. return false;
  315. }
  316. #region 设置图像分辨率
  317. //设置宽度
  318. if (!scan.SetImageSize(iWidth, iHeigh))
  319. {
  320. return false;
  321. }
  322. #endregion
  323. #region 采集时间
  324. //采集时间
  325. DwellTimeLevel nDwellTime = DwellTimeLevel.Low;
  326. switch (DwellTime)
  327. {
  328. case "Low":
  329. nDwellTime = DwellTimeLevel.Low;
  330. break;
  331. case "Medium":
  332. nDwellTime = DwellTimeLevel.Medium;
  333. break;
  334. case "High":
  335. nDwellTime = DwellTimeLevel.High;
  336. break;
  337. }
  338. //设置采集时间
  339. if (!scan.SetDwellTime(nDwellTime))
  340. {
  341. return false;
  342. }
  343. #endregion
  344. int resultCount = iWidth * iHeigh;
  345. var img = scan.AcquireBSEImage();
  346. bImageData = img.GetImageDataPtr();
  347. }
  348. catch (Exception ex)
  349. {
  350. NLog.LogManager.GetCurrentClassLogger().Error(ex.ToString());
  351. return false;
  352. }
  353. return true;
  354. }
  355. private void btn_confirm_Click(object sender, EventArgs e)
  356. {
  357. double dscanFieldSize100 = 0;
  358. if (cb_Xdirection.Text.Trim() != null && cb_imageresolution.Text.Trim() != null && cb_Ydirection.Text.Trim() != null && double.TryParse(tB_scanFieldSize100.Text, out dscanFieldSize100))
  359. {
  360. string xmlpath = Application.StartupPath + @"\Config\SysData\OTSProgMgrParam.pmf";
  361. string xAxisDir = "";
  362. string yAxisDir = "";
  363. switch (cb_Xdirection.SelectedItem.ToString())
  364. {
  365. case "LEFT_TOWARD":
  366. xAxisDir = "0:LEFT_TOWARD";
  367. break;
  368. default:
  369. xAxisDir = "1:RIGHT_TOWARD";
  370. break;
  371. }
  372. switch (cb_Ydirection.Text)
  373. {
  374. case "UP_TOWARD":
  375. yAxisDir = "0:" + cb_Ydirection.Text;
  376. break;
  377. default:
  378. yAxisDir = "1:DOWN_TOWARD";
  379. break;
  380. }
  381. SaveConfigXml(xmlpath, xAxisDir, yAxisDir, tB_scanFieldSize100.Text);
  382. }
  383. else
  384. {
  385. MessageBox.Show("Please check params!");
  386. }
  387. }
  388. bool ReadConfigXml(string xmlpath, ref string xAxisDir, ref string yAxisDir, ref string scanFieldSize)
  389. {
  390. try
  391. {
  392. XmlDocument xmlDocument = new XmlDocument();
  393. xmlDocument.Load(xmlpath);
  394. XmlNodeList nodeList = xmlDocument.SelectSingleNode("XMLData").ChildNodes;
  395. foreach (XmlNode xn in nodeList)
  396. {
  397. XmlElement xe = (XmlElement)xn;
  398. if (xe.GetAttribute("RegName") == "StageData")
  399. {
  400. scanFieldSize = xe.GetAttribute("scanFieldSize");
  401. xAxisDir = xe.GetAttribute("xAxisDir");
  402. yAxisDir = xe.GetAttribute("yAxisDir");
  403. break;
  404. }
  405. }
  406. }
  407. catch (Exception ex)
  408. {
  409. MessageBox.Show(ex.ToString());
  410. return false;
  411. }
  412. return true;
  413. }
  414. bool SaveConfigXml(string xmlpath, string xAxisDir, string yAxisDir, string scanFieldSize)
  415. {
  416. try
  417. {
  418. XmlDocument xmlDocument = new XmlDocument();
  419. xmlDocument.Load(xmlpath);
  420. XmlNodeList nodeList = xmlDocument.SelectSingleNode("XMLData").ChildNodes;
  421. foreach (XmlNode xn in nodeList)
  422. {
  423. XmlElement xe = (XmlElement)xn;
  424. if (xe.GetAttribute("RegName") == "StageData")
  425. {
  426. xe.SetAttribute("scanFieldSize", scanFieldSize);
  427. xe.SetAttribute("xAxisDir", xAxisDir);
  428. xe.SetAttribute("yAxisDir", yAxisDir);
  429. break;
  430. }
  431. }
  432. xmlDocument.Save(xmlpath);
  433. }
  434. catch (Exception ex)
  435. {
  436. MessageBox.Show(ex.ToString());
  437. return false;
  438. }
  439. return true;
  440. }
  441. public bool ConnectionSem(connectionEnumType connectionType)
  442. {
  443. //连接电镜标识
  444. bool DisConnResult = false;
  445. //判断连接状态
  446. if (!ConnectionState)
  447. {
  448. //连接电镜设置
  449. DisConnResult = m_EDSHardwareMgr.Connect();
  450. }
  451. if (DisConnResult)
  452. {
  453. ConnectionState = true;
  454. }
  455. else
  456. {
  457. ConnectionState = false;
  458. }
  459. return DisConnResult;
  460. }
  461. #region 用户信息提示
  462. /// <summary>
  463. /// 提示
  464. /// </summary>
  465. /// <param name="Message"></param>
  466. private void ShowMessage(int MessageType)
  467. {
  468. string message1 = table["message1"].ToString();
  469. string message2 = table["message2"].ToString();
  470. string message3 = table["message3"].ToString();
  471. string message4 = table["message4"].ToString();
  472. string message5 = table["message5"].ToString();
  473. string message6 = table["message6"].ToString();
  474. string message7 = table["message7"].ToString();
  475. string message8 = table["message8"].ToString();
  476. string message9 = table["message9"].ToString();
  477. string message10 = table["message10"].ToString();
  478. string MessageInfo = string.Empty;
  479. switch (MessageType)
  480. {
  481. case 0:
  482. MessageInfo = message1;
  483. break;
  484. case 1:
  485. MessageInfo = message2;
  486. break;
  487. case 2:
  488. MessageInfo = message3;
  489. break;
  490. case 3:
  491. MessageInfo = message4;
  492. break;
  493. case 4:
  494. MessageInfo = message5;
  495. break;
  496. case 5:
  497. MessageInfo = message6;
  498. break;
  499. case 6:
  500. MessageInfo = message7;
  501. break;
  502. case 7:
  503. MessageInfo = message8;
  504. break;
  505. case 8:
  506. MessageInfo = message9;
  507. break;
  508. case 9:
  509. MessageInfo = message10;
  510. break;
  511. }
  512. MessageBox.Show(MessageInfo, "Tip");
  513. }
  514. #endregion
  515. #region 判断控制内容是否为空 与 判断输入的格式是否正确
  516. /// <summary>
  517. /// 判断控制内容是否为空
  518. /// </summary>
  519. /// <param name="tbContent"></param>
  520. /// <returns></returns>
  521. public bool IsNull(TextBox tbContent)
  522. {
  523. if (tbContent.Text.Trim().Equals(""))
  524. {
  525. //为空提示
  526. ShowMessage(0);
  527. //获取焦点
  528. tbContent.Focus();
  529. return false;
  530. }
  531. return true;
  532. }
  533. /// <summary>
  534. ///
  535. /// </summary>
  536. /// <param name="objValue"></param>
  537. /// <param name="objType"></param>
  538. /// <returns></returns>
  539. public bool IsType(object ObjValue, int ObjType)
  540. {
  541. try
  542. {
  543. switch (ObjType)
  544. {
  545. case 1:
  546. int intValue = Convert.ToInt32(ObjValue);
  547. break;
  548. case 2:
  549. double douValue = Convert.ToDouble(ObjValue);
  550. break;
  551. case 3:
  552. float floValue = Convert.ToSingle(ObjValue);
  553. break;
  554. }
  555. return true;
  556. }
  557. catch (Exception)
  558. {
  559. //为空提示
  560. ShowMessage(7);
  561. return false;
  562. }
  563. }
  564. #endregion
  565. void InitEvent()
  566. {
  567. string edsType = FileHelper.GetXMLInformations("EDSName");
  568. string SemType = FileHelper.GetXMLInformations("SemControllerName");
  569. if (SemType == "FEI")
  570. {
  571. if (edsType == "FEI")
  572. {
  573. cb_imageresolution.SelectedIndex = 2;
  574. }
  575. else if (edsType == "Oxford")
  576. {
  577. cb_imageresolution.SelectedIndex = 0;
  578. }
  579. else if (edsType == "Bruker")
  580. {
  581. cb_imageresolution.SelectedIndex = 1;
  582. }
  583. else if (edsType == "OffLine")
  584. {
  585. cb_imageresolution.SelectedIndex = 1;
  586. }
  587. cb_Xdirection.SelectedIndex = 1;
  588. cb_Ydirection.SelectedIndex = 0;
  589. }
  590. }
  591. private void StageTest_Load(object sender, EventArgs e)
  592. {
  593. Control.CheckForIllegalCrossThreadCalls = false;
  594. //m_EDSHardwareMgr = EDSController.GetEDSController(width, height, 5000, true, "");
  595. cb_Xdirection.Items.Add("LEFT_TOWARD");
  596. cb_Xdirection.Items.Add("RIGHT_TOWARD");
  597. cb_Ydirection.Items.Add("UP_TOWARD");
  598. cb_Ydirection.Items.Add("DOWN_TOWARD");
  599. tB_scanFieldSize100.Text = "1270";
  600. cb_imageresolution.Items.Add("1024*768");
  601. cb_imageresolution.Items.Add("1024*704");
  602. cb_imageresolution.Items.Add("1536*1024");
  603. string xmlpath = Application.StartupPath + @"\Config\SysData\OTSProgMgrParam.pmf";
  604. string xAxisDir = "";
  605. string yAxisDir = "";
  606. string scanFieldSize = "";
  607. ReadConfigXml(xmlpath, ref xAxisDir, ref yAxisDir, ref scanFieldSize);
  608. if (xAxisDir.Split(':')[1] == "LEFT_TOWARD")
  609. {
  610. cb_Xdirection.SelectedIndex = 0;
  611. }
  612. else
  613. {
  614. cb_Xdirection.SelectedIndex = 1;
  615. }
  616. if (yAxisDir.Split(':')[1] == "UP_TOWARD")
  617. {
  618. cb_Ydirection.SelectedIndex = 0;
  619. }
  620. else
  621. {
  622. cb_Ydirection.SelectedIndex = 1;
  623. }
  624. tB_scanFieldSize100.Text = scanFieldSize;
  625. cb_imageresolution.SelectedIndex = 0;
  626. InitEvent();
  627. }
  628. }
  629. }