CSmplMeasure.cs 38 KB

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