CSmplMeasure.cs 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using OTSModelSharp.ServiceInterface;
  7. using static OTSDataType.otsdataconst;
  8. using OTSDataType;
  9. using System.Diagnostics;
  10. using System.Drawing;
  11. using System.Threading;
  12. using OTSModelSharp.DTLBase;
  13. using OTSCOMMONCLR;
  14. namespace OTSModelSharp
  15. {
  16. using CHoleBSEImgsList = List<CHoleBSEImg>;
  17. class CSmplMeasure
  18. {
  19. protected static NLog.Logger loger = NLog.LogManager.GetCurrentClassLogger();
  20. protected bool bSaveThreadWorking;
  21. protected System.Threading.Thread m_thread_ptr;
  22. protected COTSSample m_Sample;
  23. protected CMsrThread m_pMsrThread;
  24. string m_strWorkingFolder;
  25. protected CSmplMsrResult m_pSampleRstFile;
  26. CHoleBSEImgsList m_listHoleBSEImg;
  27. //CFunExportClass loger;
  28. protected SemController m_SemHardwareMgr;
  29. protected ScanController m_ScanHardwareMgr;
  30. protected EDSController m_EDSHardwareMgr;
  31. protected Queue<COTSFieldData> fieldQueue=new Queue<COTSFieldData>();
  32. protected COTSFieldData curFldData;
  33. protected IClassifyEngine m_classifyEngine;
  34. public CSmplMeasure()
  35. {
  36. if (loger == null)
  37. {
  38. loger = NLog.LogManager.GetCurrentClassLogger();
  39. }
  40. m_pSampleRstFile = new CSmplMsrResult();
  41. m_SemHardwareMgr = new SemController();
  42. m_ScanHardwareMgr = new ScanController();
  43. m_EDSHardwareMgr = new EDSController();
  44. m_listHoleBSEImg = new CHoleBSEImgsList();
  45. }
  46. public CSmplMeasure( string a_strWorkingFolder, COTSSample a_pSample)
  47. {
  48. if (loger == null)
  49. {
  50. loger = NLog.LogManager.GetCurrentClassLogger();
  51. }
  52. m_strWorkingFolder = a_strWorkingFolder;
  53. m_pSampleRstFile = new CSmplMsrResult( a_strWorkingFolder, a_pSample);// new CSmplMsrResultFile(m_strWorkingFolder,a_pSample);
  54. m_SemHardwareMgr = new SemController();
  55. m_ScanHardwareMgr = new ScanController();
  56. m_EDSHardwareMgr = new EDSController();
  57. m_listHoleBSEImg = new CHoleBSEImgsList();
  58. m_Sample = a_pSample;
  59. }
  60. public void SetSample(COTSSample a_pSample)
  61. {
  62. m_Sample = a_pSample;
  63. m_pSampleRstFile.SetSample(a_pSample);
  64. }
  65. public void SetMsrThread(CMsrThread mt)
  66. {
  67. m_pMsrThread = mt;
  68. }
  69. bool SetSEMDataMrs()
  70. {
  71. var pSEMDataMsr = m_Sample.GetSEMDataMsr();
  72. //ASSERT(pSEMDataMsr);
  73. double dMag = pSEMDataMsr.GetMagnification();
  74. double dWorkDis = pSEMDataMsr.GetWorkingDistance();
  75. //CSemBasePtr pSEMCtrlPtr = GetSEMControl();
  76. var pSEMCtrl = m_pMsrThread.GetSEMController();
  77. // get SEM controller
  78. if (!pSEMCtrl.IsConnected())
  79. {
  80. if (!pSEMCtrl.Connect())
  81. {
  82. loger.Error("SetSEMDataMrs: can't connect SEM.");
  83. return false;
  84. }
  85. }
  86. loger.Info("set magnification:" + dMag.ToString());
  87. loger.Info("set Working Distance:" + dWorkDis.ToString());
  88. pSEMCtrl.SetMagnification(dMag);
  89. pSEMCtrl.SetWorkingDistance(dWorkDis);
  90. return true;
  91. }
  92. bool SetSEMExteralOn()
  93. {
  94. var pSEMCtrl = m_SemHardwareMgr;
  95. // get SEM controller
  96. if (!pSEMCtrl.IsConnected())
  97. {
  98. if (!pSEMCtrl.Connect())
  99. {
  100. loger.Error("SetSEMExteralOn: can't connect SEM.");
  101. return false;
  102. }
  103. }
  104. loger.Warn("Set Scan Exteral on!");
  105. pSEMCtrl.SetScanExternal(true);
  106. return true;
  107. }
  108. // set SEM external off
  109. bool SetSEMExteralOff()
  110. {
  111. var pSEMCtrlPtr = m_SemHardwareMgr;
  112. // get SEM controller
  113. if (!pSEMCtrlPtr.IsConnected())
  114. {
  115. if (!pSEMCtrlPtr.Connect())
  116. {
  117. loger.Error("SetSEMExteralOff: can't connect SEM.");
  118. return false;
  119. }
  120. }
  121. loger.Warn("Set SEM Exteral Off!");
  122. pSEMCtrlPtr.SetScanExternal(false);
  123. return true;
  124. }
  125. bool SetBSEParam()
  126. {
  127. // get SEM controller
  128. var pSEMController = m_SemHardwareMgr;
  129. // get scan controller
  130. var pScanController = m_ScanHardwareMgr;
  131. // scan parameters
  132. var pMsrParam = m_Sample.GetMsrParams();
  133. var pImgScanParam = pMsrParam.GetImageScanParam();
  134. // get image size
  135. var nImageSizeId = pImgScanParam.GetImagePixelSize();
  136. int nResulotionId = RESOLUTION_ID_FIRST_TIE + (int)nImageSizeId;
  137. Size sizePixelImage = RESOLUTION_VALUE[nResulotionId];
  138. // get SEM controller
  139. if (!pSEMController.IsConnected())
  140. {
  141. if (!pSEMController.Connect())
  142. {
  143. loger.Error("SetBSEParam: can't connect SEM.");
  144. return false;
  145. }
  146. }
  147. // get scan field size
  148. double dScanFieldSizeX = sizePixelImage.Width;
  149. double dScanFieldSizeY = sizePixelImage.Height;
  150. if (!pSEMController.GetScanFieldSize(ref dScanFieldSizeX, ref dScanFieldSizeY))
  151. {
  152. loger.Error("SetBSEParam: failed to call GetScanFieldSize method.");
  153. return false;
  154. }
  155. // get dwell time
  156. OTS_THREE_TIES_OPTIONS nDwellTime = pImgScanParam.GetScanImageSpeed();
  157. // convert dwell time to bruker dwell time (6, 16, 32)
  158. const long DWELLTIME_BRUKER_ID_THREE_TIE_MIN = 3; // 8
  159. long nBrukerDwellTimeId = DWELLTIME_BRUKER_ID_THREE_TIE_MIN + (int)nDwellTime;
  160. int[] DWELLTIME_BRUKER_VALUES = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512 };
  161. long nBrukerDwellTime = DWELLTIME_BRUKER_VALUES[nBrukerDwellTimeId];
  162. if (!pScanController.Init())
  163. {
  164. loger.Error("SetBSEParam: failed to get scan control.");
  165. return false;
  166. }
  167. // set dwell time
  168. if (!pScanController.SetDwellTime(nBrukerDwellTime))
  169. {
  170. loger.Error("SetBSEParam: failed to set dwell time (%d) for bruker system.", nBrukerDwellTime);
  171. return false;
  172. }
  173. // set image size
  174. if (!pScanController.SetImageSize(sizePixelImage.Width,sizePixelImage.Height))
  175. {
  176. // failed to set dwell time
  177. loger.Error("SetBSEParam: failed to set dwell time (%d).", sizePixelImage.Height);
  178. return false;
  179. }
  180. return true;
  181. }
  182. // reset Scan
  183. //bool ResetScan()
  184. //{
  185. // // get scan controller
  186. // //var pScanController = m_ScanHardwareMgr;
  187. // var sem = m_SemHardwareMgr;
  188. // // reset Scanner
  189. // //if (!sem.DisConnect())
  190. // //{
  191. // // loger.Error("AcquireABSEImage: failed to call SetAndStartScan method.");
  192. // //}
  193. // loger.Warn("Reset Scan!");
  194. // return true;
  195. //}
  196. public void SetWorkingFolder(String a_strWorkingFolder)
  197. {
  198. // add "\\" at the string end if it is not "\\"
  199. if (a_strWorkingFolder.PadRight(1)!="\\")
  200. {
  201. a_strWorkingFolder += "\\";
  202. }
  203. m_strWorkingFolder = a_strWorkingFolder + m_Sample.GetName() + "\\";
  204. }
  205. bool CalculateUnMeasuredFieldsCenters(out List<System.Drawing.Point> a_listFieldCenter)
  206. {
  207. // sample measure parameters
  208. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  209. COTSImgScanPrm poImageScanParam = pMsrParam.GetImageScanParam();
  210. CSEMDataMsr poSEMDataMsr = m_Sample.GetSEMDataMsr();
  211. CMsrSampleStatus pStatus = m_Sample.GetMsrStatus();
  212. // measured field centers list
  213. List<System.Drawing.Point> listCompletedCenter = pStatus.GetCompletedFieldsCenter();
  214. // field centers list manager
  215. CFieldPositionMgr pFieldMgr = new CFieldPositionMgr();
  216. // init field centers list manager
  217. if (!pFieldMgr.Init(m_Sample.GetMsrArea(), poImageScanParam, poSEMDataMsr, listCompletedCenter))
  218. {
  219. loger.Error("CalculateFieldsCenters: failed to init field centres list manager.");
  220. a_listFieldCenter = new List<System.Drawing.Point>();
  221. return false;
  222. }
  223. // get field centers list
  224. a_listFieldCenter = pFieldMgr.GetUnmeasuredFieldCentrePoints();// GetFieldCentrePoints();
  225. // ok, return TRUE
  226. return true;
  227. }
  228. protected bool IsAborted()
  229. {
  230. //Debug.Assert(m_pMsrThread == null);
  231. return m_pMsrThread.IsMeasureStopped();
  232. }
  233. bool IsSampleOver(COTSImgScanPrm a_pScanParam, int a_nTotalFields)
  234. {
  235. int nStopMode = (int)a_pScanParam.GetStopMode();
  236. int nStopField = a_pScanParam.GetStopParamFields();
  237. // completed fields number
  238. CMsrSampleStatus pMsrSampleStatus = m_Sample.GetMsrStatus();
  239. int nCompeltedField = pMsrSampleStatus.GetCompletedFields();
  240. CMsrDisplayResults pMsrResults = m_Sample.GetMsrResults();
  241. List<CMsrResultItem> listMsrResult = pMsrResults.GetResultItems();
  242. int nNumParticle = 0;
  243. foreach (var pResult in listMsrResult)
  244. {
  245. if (pResult.GetTypeId() > (int)OTS_PARTCLE_TYPE.NO_ANALYSIS_X_RAY)//summarize the number of the identified particle in this condition
  246. {
  247. nNumParticle += (int)pResult.GetNumber();
  248. }
  249. }
  250. TimeSpan timeSpan = pMsrSampleStatus.GetUsedTime();
  251. int nDay = timeSpan.Days;
  252. int nHour = timeSpan.Hours;
  253. int nMin = timeSpan.Minutes;
  254. int nSec = timeSpan.Seconds;
  255. int nUsedTime = nSec + nMin * 60 + nHour * 3600 + nDay * 86400;
  256. int nParticlAim = a_pScanParam.GetStopParamParticles();
  257. int nMeasTimeAim = a_pScanParam.GetStopParamMeasTime();
  258. bool bRet = true;
  259. switch (nStopMode)
  260. {
  261. case (int)OTS_MEASURE_STOP_MODE.CoverMode:
  262. // completed fields number
  263. if (nCompeltedField < a_nTotalFields)
  264. {
  265. bRet = false;
  266. }
  267. break;
  268. case (int)OTS_MEASURE_STOP_MODE.FieldMode:
  269. if (nCompeltedField < nStopField)
  270. {
  271. bRet = false;
  272. }
  273. break;
  274. case (int)OTS_MEASURE_STOP_MODE.ParticleMode:
  275. if (nNumParticle < nParticlAim)
  276. {
  277. bRet = false;
  278. }
  279. break;
  280. case (int)OTS_MEASURE_STOP_MODE.TimeMode:
  281. if (nUsedTime < nMeasTimeAim)
  282. {
  283. bRet = false;
  284. }
  285. break;
  286. default:
  287. break;
  288. }
  289. return bRet;
  290. }
  291. // move SEM to the point
  292. bool MoveSEMToPoint(System.Drawing.Point a_poi)
  293. {
  294. // get SEM controller
  295. var pSEMController = m_SemHardwareMgr;
  296. Point a_SEMpt = new Point();
  297. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  298. if (!a_pCSEMStageData.ConverOTSToSEMPoint(a_poi, ref a_SEMpt))
  299. {
  300. return false;
  301. }
  302. if (!pSEMController.IsConnected())
  303. {
  304. if (!pSEMController.Connect())
  305. {
  306. loger.Error("MoveSEMToPoint: can't connect SEM.");
  307. return false;
  308. }
  309. }
  310. loger.Info("Begin to move SEM stage to " + a_SEMpt.X + "," + a_SEMpt.Y);
  311. Thread.Sleep(100);
  312. // move SEM to the position (rotation 0)
  313. if (!pSEMController.MoveSEMToPoint(a_SEMpt, 0))
  314. {
  315. loger.Error("MoveSEMToPoint: failed to call MoveSEMToPoint method.");
  316. return false;
  317. }
  318. return true;
  319. }
  320. // Acquire a BSE image
  321. CBSEImgClr AcquireABSEImage()
  322. {
  323. // BSE image
  324. CBSEImgClr pBSEImage = null;
  325. // get scan controller
  326. var pScanController = m_ScanHardwareMgr;
  327. pBSEImage = pScanController.AcquireBSEImage(0, 0, 2);
  328. return pBSEImage;
  329. }
  330. public virtual bool FieldImageProcess(Point fldCenter, CBSEImgClr imageData)
  331. {
  332. return true;
  333. }
  334. public virtual void ClassifyParticles(List<COTSParticleClr> parts)
  335. {
  336. return;
  337. }
  338. private class AutoResetSEMControl:IDisposable
  339. {
  340. CSmplMeasure sm;
  341. public AutoResetSEMControl(CSmplMeasure s)
  342. {
  343. sm = s;
  344. }
  345. public void Dispose()
  346. {
  347. sm.SetSEMExteralOff();
  348. //sm.ResetScan();
  349. }
  350. }
  351. public void DoMeasureForOneSample()
  352. {
  353. using (AutoResetSEMControl autoReset = new AutoResetSEMControl(this)) //when this method exit ,the SetSEMExternalOff and ResetScan will be called automatically.
  354. {
  355. // let the main thread to know that this sample measurement starts
  356. var pStatus = m_Sample.GetMsrStatus();
  357. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  358. // set current time to current time
  359. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.START);
  360. // let main App know that the sample begin to measure
  361. ST_MSTMsg MsgSmpStart = new ST_MSTMsg();
  362. MsgSmpStart.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  363. MsgSmpStart.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.INPROCESS;
  364. MsgSmpStart.STMSampleStu.cSampleName = m_Sample.GetName();
  365. m_pMsrThread.SendMessageToMeasureApp(MsgSmpStart);
  366. loger.Info(m_Sample.GetName() + " Measurement started!");
  367. // get SEM controller to set magnification and working distance
  368. if (!SetSEMDataMrs())
  369. {
  370. loger.Error("DoMeasure: fail to set SEM data.");
  371. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  372. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  373. return;
  374. }
  375. // get SEM external controll on
  376. if (!SetSEMExteralOn())
  377. {
  378. loger.Error("DoMeasure: fail to set SEM external.");
  379. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  380. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  381. return;
  382. }
  383. // set the BSE scan param
  384. if (!SetBSEParam())
  385. {
  386. loger.Error("DoMeasure: fail to set BSE param.");
  387. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  388. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  389. return;
  390. }
  391. var pSEMDataGnr = new CSEMDataGnr();
  392. loger.Info("Get Kv, Brightness and Contrast!");
  393. m_SemHardwareMgr.GetSEMDataGnrFromHw(ref pSEMDataGnr);
  394. m_pSampleRstFile.SetSEMGnr(pSEMDataGnr);
  395. // record SEM data
  396. COTSMsrPrjResultData pProjMgrFile = m_pMsrThread.GetProjResultData();
  397. CSEMStageData pSEMStageData = pProjMgrFile.GetSEMStageData();
  398. m_pSampleRstFile.SetSEMStageData(pSEMStageData);
  399. // record stage
  400. CStage pStage = pProjMgrFile.GetStage();
  401. m_pSampleRstFile.SetSEMStage(pStage);
  402. //-----save the static measure result file data into xml file and the dynamic data of every field will be saved into sqlite database
  403. loger.Info("Create result file!");
  404. if (!m_pSampleRstFile.CreateResultFiles())
  405. {// failed to call measure result file Save method
  406. loger.Error("DoMeasure: failed to call measure result file Save method.");
  407. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  408. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  409. return;
  410. }
  411. //------
  412. // calculate field centers
  413. List<System.Drawing.Point> umMeasuredlistFieldCenter;
  414. if (!CalculateUnMeasuredFieldsCenters(out umMeasuredlistFieldCenter))
  415. {// failed to calculate field centers
  416. loger.Error("DoMeasure: failed to calculate field centers.");
  417. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  418. // record end time
  419. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  420. return;
  421. }
  422. int nNewFieldId = 0;
  423. int numOfAllFields = pStatus.GetCompletedFields() + umMeasuredlistFieldCenter.Count;//
  424. int completedFields = pStatus.GetCompletedFields();
  425. loger.Info("Unmeasured fields:" + umMeasuredlistFieldCenter.Count);
  426. for (int i = 0; i < (int)umMeasuredlistFieldCenter.Count; ++i)
  427. {// check and break if stop button is clicked
  428. if (IsAborted())
  429. {// measure stopped
  430. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  431. // record end time
  432. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  433. //must wait for the saving data thread finished,or we'll get null pointer exception when we stop the measure process.
  434. while (fieldQueue.Count() > 0)
  435. {
  436. Thread.Sleep(100);
  437. }
  438. return;
  439. }
  440. // check if sample measurement completes
  441. COTSImgScanPrm pScanParam = m_Sample.GetMsrParams().GetImageScanParam();
  442. int nTotalFieldSize = (int)umMeasuredlistFieldCenter.Count;
  443. if (IsSampleOver(pScanParam, nTotalFieldSize))
  444. {
  445. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  446. break;
  447. }
  448. // get a field center
  449. System.Drawing.Point poiFieldCentre = umMeasuredlistFieldCenter[i];
  450. // update thread measure status class, let the main thread know that starts a new field
  451. ST_MSTMsg MsgFieldStart = new ST_MSTMsg();
  452. MsgFieldStart.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  453. MsgFieldStart.STMSampleRetData.iRetDataType = MSAMPLE_RET.START_MSR_FIELD;
  454. MsgFieldStart.STMSampleRetData.SMsrField.FieldPos = poiFieldCentre;
  455. m_pMsrThread.SendMessageToMeasureApp(MsgFieldStart);
  456. int fldNo = completedFields + i + 1;
  457. loger.Warn("Current field:" + fldNo.ToString());
  458. // move SEM to the field center
  459. if (!MoveSEMToPoint(poiFieldCentre))
  460. {// failed to move SEM to the position
  461. loger.Error("DoMeasure: failed to move SEM to the field centre point.");
  462. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  463. // record end time
  464. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  465. return;
  466. }
  467. loger.Info("Begin to Acquire BSE image!");
  468. // take BSE image for the fields
  469. CBSEImgClr pBSEImg = AcquireABSEImage();
  470. // let the main thread to know that image process is completed
  471. if (pBSEImg == null)
  472. {
  473. loger.Error("ImageProcess: can't get no background image.");
  474. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  475. return;
  476. }
  477. //BSEData
  478. ST_MSTMsg MsgFieldBSE = new ST_MSTMsg();
  479. MsgFieldBSE.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  480. MsgFieldBSE.STMSampleRetData.iRetDataType = MSAMPLE_RET.BSE_DATA;
  481. MsgFieldBSE.STMSampleRetData.BSEData.pos.X = poiFieldCentre.X;
  482. MsgFieldBSE.STMSampleRetData.BSEData.pos.Y = poiFieldCentre.Y;
  483. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataHeight = pBSEImg.GetHeight();
  484. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataWidth = pBSEImg.GetWidth();
  485. byte[] pImgData = pBSEImg.GetImageDataPtr();
  486. MsgFieldBSE.STMSampleRetData.BSEData.lpBSEData = pImgData;
  487. m_pMsrThread.SendMessageToMeasureApp(MsgFieldBSE);
  488. // get a new field id
  489. loger.Info("Acquire BSE image success! Processing image...");
  490. // image process
  491. var rst = FieldImageProcess(poiFieldCentre, pBSEImg);
  492. if (rst == true)
  493. {
  494. // is the field data empty
  495. if (!curFldData.IsEmpty())
  496. {
  497. // add the field into the field
  498. m_pSampleRstFile.AddAField(curFldData);
  499. }
  500. }
  501. ++nNewFieldId;
  502. uint measuredArea = 0; // this area should be the field area
  503. var a_pBSEImg = curFldData.GetBSEImage();
  504. double dPixelSize = m_Sample.CalculatePixelSize();
  505. measuredArea = (uint)(a_pBSEImg.GetHeight() * a_pBSEImg.GetWidth()* dPixelSize * dPixelSize + 0.5); //Get measured area
  506. if (!CumulateFieldData(curFldData.ListAnalysisParticles, measuredArea))
  507. { // failed to call SaveFieldFile method
  508. loger.Error("ImageProcess: call CumulateFieldData method.");
  509. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  510. return;
  511. }
  512. loger.Info("Send field data to screen!");
  513. // completed fields
  514. pStatus.SetCompletedFields(pStatus.GetCompletedFields() + 1);
  515. // completed fieldCenter
  516. List<System.Drawing.Point> listCpltedCenter = pStatus.GetCompletedFieldsCenter();
  517. listCpltedCenter.Add(poiFieldCentre);
  518. //Field Data
  519. // record end time
  520. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  521. ST_MSTMsg MsgFieldEnd = new ST_MSTMsg();
  522. MsgFieldEnd.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  523. MsgFieldEnd.STMSampleRetData.iRetDataType = MSAMPLE_RET.FIELD_DATA;
  524. MsgFieldEnd.STMSampleRetData.SFieldData.iCompleteFieldCount = pStatus.GetCompletedFields();
  525. MsgFieldEnd.STMSampleRetData.SFieldData.iMeasureFieldCount = numOfAllFields;
  526. MsgFieldEnd.STMSampleRetData.SFieldData.iSParticleCount = (int)curFldData.ListAnalysisParticles.Count;
  527. MsgFieldEnd.STMSampleRetData.SFieldData.FieldPos.X = Convert.ToInt32(poiFieldCentre.X);
  528. MsgFieldEnd.STMSampleRetData.SFieldData.FieldPos.Y = Convert.ToInt32(poiFieldCentre.Y);
  529. m_pMsrThread.SendMessageToMeasureApp(MsgFieldEnd);
  530. }
  531. while (fieldQueue.Count > 0)//wait untill all the field data has been saved.
  532. {
  533. Thread.Sleep(100);
  534. }
  535. //merging particles
  536. loger.Info("Merging big particles on the field edge!");
  537. CImageHandler imgpro = new CImageHandler();
  538. int scanfldsize = m_Sample.GetSEMDataMsr().GetScanFieldSize();
  539. List<COTSParticleClr> mergedParticles = new List<COTSParticleClr>();
  540. imgpro.MergeBigBoundaryParticles(m_pSampleRstFile.GetFieldData(), m_Sample.CalculatePixelSize(), scanfldsize, m_Sample.GetResolutionSize(), ref mergedParticles);
  541. ClassifyParticles(mergedParticles);
  542. loger.Warn("begin particle data db saving...");
  543. SaveMergedParticles(mergedParticles);
  544. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.COMPLT);
  545. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  546. // let main thread to know that this sample measurement completes
  547. ST_MSTMsg MsgSmplEnd = new ST_MSTMsg();
  548. MsgSmplEnd.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  549. MsgSmplEnd.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.SUCCESSED;
  550. MsgSmplEnd.STMSampleStu.cSampleName = m_Sample.GetName();
  551. m_pMsrThread.SendMessageToMeasureApp(MsgSmplEnd);
  552. }
  553. }
  554. public void DoHolePreview(int a_nHoleID)
  555. {
  556. using (AutoResetSEMControl autoReset = new AutoResetSEMControl(this))
  557. {
  558. // let the main thread to know that this sample measurement starts
  559. CMsrSampleStatus pStatus = m_Sample.GetMsrStatus();
  560. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  561. // set current time to current time
  562. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.START);
  563. // let main App know that the sample begin to measure
  564. ST_MSTMsg MsgSmpStart=new ST_MSTMsg();
  565. MsgSmpStart.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  566. MsgSmpStart.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.INPROCESS;
  567. MsgSmpStart.STMSampleStu.cSampleName=m_Sample.GetName();
  568. // get SEM controller to set magnification and working distance
  569. if (!SetSEMDataMrs())
  570. {
  571. loger.Error("DoHolePreview: fail to set SEM data.");
  572. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  573. // record end time
  574. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  575. return;
  576. }
  577. // get SEM external controll on
  578. if (!SetSEMExteralOn())
  579. {
  580. loger.Error("DoMeasure: fail to set SEM external.");
  581. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  582. // record end time
  583. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  584. return;
  585. }
  586. // set the BSE scan param
  587. if (!SetBSEParam())
  588. {
  589. loger.Error("DoMeasure: fail to set BSE param.");
  590. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  591. // record end time
  592. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  593. return;
  594. }
  595. // check if measurement is aborted
  596. if (IsAborted())
  597. {// measurement aborted
  598. loger.Trace("DoHolePreview: measurement aborted before get SEM condition.");
  599. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  600. // record end time
  601. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  602. return;
  603. }
  604. // calculate field centers
  605. List<System.Drawing.Point> listFieldCenter=new List<System.Drawing.Point>();
  606. // listFieldCenter.clear();
  607. if (!CalculateUnMeasuredFieldsCenters(out listFieldCenter))
  608. {// failed to calculate field centers
  609. loger.Error("DoHolePreview: failed to calculate field centers.");
  610. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  611. // record end time
  612. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  613. return;
  614. }
  615. // go through each field
  616. int nNewFieldId = 0;
  617. for ( int i = 0; i < listFieldCenter.Count; ++i)
  618. {// check and break if stop button is clicked
  619. if (IsAborted())
  620. {// measure stopped
  621. loger.Trace("DoHolePreview: measure thread is stopped.");
  622. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  623. // record end time
  624. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  625. return;
  626. }
  627. // check if sample measurement completes
  628. COTSImgScanPrm pScanParam = m_Sample.GetMsrParams().GetImageScanParam();
  629. int nTotalFieldSize = listFieldCenter.Count;
  630. if (IsSampleOver(pScanParam, nTotalFieldSize))
  631. {
  632. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  633. break;
  634. }
  635. // get a field center
  636. System.Drawing.Point poiFieldCentre = listFieldCenter[i];
  637. // update thread measure status class, let the main thread know that starts a new field
  638. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  639. ST_MSTMsg MsgFieldStart=new ST_MSTMsg();
  640. //memset(&MsgFieldStart, 0, sizeof(ST_MSTMsg));
  641. MsgFieldStart.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  642. MsgFieldStart.STMSampleRetData.iRetDataType = MSAMPLE_RET.START_MSR_FIELD;
  643. MsgFieldStart.STMSampleRetData.SMsrField.FieldPos = poiFieldCentre;
  644. m_pMsrThread.SendMessageToMeasureApp(MsgFieldStart);
  645. // move SEM to the field center
  646. if (!MoveSEMToPoint(poiFieldCentre))
  647. {// failed to move SEM to the position
  648. loger.Error("DoHolePreview: failed to move SEM to the field centre point.");
  649. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  650. // record end time
  651. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  652. return;
  653. }
  654. // take BSE image for the fields
  655. CBSEImgClr pBSEIamge = AcquireABSEImage();
  656. if (pBSEIamge==null)
  657. {
  658. // failed to acquire a BSE image
  659. loger.Error("DoHolePreview: failed to acquire a BSE image.");
  660. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  661. // record end time
  662. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  663. return;
  664. }
  665. //BSEData
  666. ST_MSTMsg MsgFieldBSE=new ST_MSTMsg();
  667. // memset(&MsgFieldBSE, 0, sizeof(ST_MSTMsg));
  668. MsgFieldBSE.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  669. MsgFieldBSE.STMSampleRetData.iRetDataType = MSAMPLE_RET.BSE_DATA;
  670. MsgFieldBSE.STMSampleRetData.BSEData.pos = poiFieldCentre;
  671. //MsgFieldBSE.STMSampleRetData.BSEData.pos.y = poiFieldCentre.y;
  672. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataHeight = pBSEIamge.GetHeight();
  673. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataWidth = pBSEIamge.GetWidth();
  674. byte[] pImgData = pBSEIamge.GetImageDataPtr();
  675. MsgFieldBSE.STMSampleRetData.BSEData.lpBSEData = pImgData;
  676. m_pMsrThread.SendMessageToMeasureApp(MsgFieldBSE);
  677. if (pStatus.GetStatus() != OTS_MSR_SAMPLE_STATUS.INPROCESS)
  678. {
  679. // measurement failed or stopped
  680. loger.Error("DoHolePreview: measurement failed or stopped.");
  681. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  682. // record end time
  683. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  684. return;
  685. }
  686. //save the result to project file
  687. CHoleBSEImg pHoleBSEImg =new CHoleBSEImg();
  688. pHoleBSEImg.SetHoleID(a_nHoleID);
  689. pHoleBSEImg.SetPosition(poiFieldCentre);
  690. Rectangle oImageRect =(Rectangle) pBSEIamge.GetImageRect();
  691. int nImageSize = pBSEIamge.GetHeight() * pBSEIamge.GetWidth();
  692. pHoleBSEImg.SetImageRect(oImageRect);
  693. pHoleBSEImg.SetImageData( pBSEIamge.GetImageDataPtr(),oImageRect.Width,oImageRect.Height);
  694. m_listHoleBSEImg.Add(pHoleBSEImg);
  695. COTSMsrPrjResultData pProjMgrFile = m_pMsrThread.GetProjResultData();
  696. pProjMgrFile.SetHoleBESImgList(m_listHoleBSEImg,true);
  697. // prepare for the next
  698. ++nNewFieldId;
  699. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  700. ST_MSTMsg MsgFieldEnd=new ST_MSTMsg();
  701. MsgFieldEnd.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  702. MsgFieldEnd.STMSampleRetData.iRetDataType = MSAMPLE_RET.FIELD_DATA;
  703. MsgFieldEnd.STMSampleRetData.SFieldData.iCompleteFieldCount = (i + 1);
  704. MsgFieldEnd.STMSampleRetData.SFieldData.iMeasureFieldCount = listFieldCenter.Count;
  705. MsgFieldEnd.STMSampleRetData.SFieldData.iSParticleCount = 0;
  706. MsgFieldEnd.STMSampleRetData.SFieldData.FieldPos = poiFieldCentre;
  707. m_pMsrThread.SendMessageToMeasureApp(MsgFieldEnd);
  708. //break;
  709. }
  710. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  711. //calculate measure time
  712. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  713. // let main thread to know that this sample measurement completes
  714. ST_MSTMsg MsgSmplEnd=new ST_MSTMsg();
  715. MsgSmplEnd.iMsgType = ENUM_MSG_TYPE.MTHREADSTATUS;
  716. MsgSmplEnd.STMThreadStu.iMsrStatu = OTS_MSR_THREAD_STATUS.COMPLETED;
  717. m_pMsrThread.SendMessageToMeasureApp(MsgSmplEnd);
  718. }
  719. }
  720. // Cumulate field data info
  721. public virtual bool CumulateFieldData( List<COTSParticleClr> listParticles, uint a_nMeasuredArea)
  722. {
  723. // get measure result items of the sample
  724. CMsrDisplayResults pMsrResults = m_Sample.GetMsrResults();
  725. // cumulate field data info
  726. //pMsrResults.CumulateMeasureResults(listResultItems);
  727. pMsrResults.CumulateMeasuredArea(a_nMeasuredArea);
  728. // cumulate ratio
  729. double dRatio = 10000 * pMsrResults.GetTotalParticleArea();
  730. dRatio = dRatio / pMsrResults.GetMeasuredArea();
  731. pMsrResults.SetRatio(dRatio);
  732. // go through the particles list
  733. foreach (COTSParticleClr pParticle in listParticles)
  734. {
  735. // create a measure result item
  736. pMsrResults.CumulateMeasureResult(pParticle);
  737. }
  738. // ok, return TRUE
  739. return true;
  740. }
  741. // hole BSE images list
  742. void SetHoleBESImgList(CHoleBSEImgsList a_listHoleBSEImg, bool a_bClear/* = TRUE*/)
  743. {// clear the hole BSE image list if necessary
  744. if (a_bClear)
  745. {
  746. m_listHoleBSEImg.Clear();
  747. }
  748. // go through the input list
  749. foreach (var pHoleBSEImg in a_listHoleBSEImg)
  750. {// add the new hole BSE image into HoleBSEImage list
  751. m_listHoleBSEImg.Add(pHoleBSEImg);
  752. }
  753. }
  754. public bool SaveMergedParticles(List<COTSParticleClr> mergedParts)
  755. {
  756. CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
  757. var mergedpartdb = pDBFileMgr.GetMergedParticleDB();
  758. foreach (COTSParticleClr part in mergedParts)
  759. {
  760. mergedpartdb.SaveAParticle(part, part.GetXray(), new System.Drawing.Point(0, 0));
  761. }
  762. CPosXrayDBMgr pXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
  763. CElementChemistryDB xraydb = pXrayDBMgr.GetElementChemistryDB();
  764. List<CPosXrayClr> ches = new List<CPosXrayClr>();
  765. foreach (COTSParticleClr part in mergedParts)
  766. {
  767. ches.Add(part.GetXray());
  768. }
  769. xraydb.SaveElementChemistriesList(ches);
  770. return true;
  771. }
  772. }
  773. }