CSmplMeasure.cs 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using OTSModelSharp.ServiceCenter;
  5. using static OTSDataType.otsdataconst;
  6. using OTSDataType;
  7. using System.Drawing;
  8. using System.Threading;
  9. using OTSModelSharp.DTLBase;
  10. using OTSCLRINTERFACE;
  11. using OTSMeasureApp._0_OTSModel.OTSDataType;
  12. using System.Windows.Forms;
  13. using System.Data;
  14. namespace OTSModelSharp
  15. {
  16. using CHoleBSEImgsList = List<CHoleBSEImg>;
  17. public class CSmplMeasure
  18. {
  19. protected static NLog.Logger log ;
  20. protected bool bSaveThreadWorking;
  21. protected System.Threading.Thread m_thread_ptr;
  22. protected COTSSample m_Sample;
  23. protected COTSSample m_HolePreviewSample;
  24. protected CMeasure m_pMsrThread;
  25. string m_strWorkingFolder;
  26. protected CSmplMsrResult m_pSampleRstFile;
  27. CHoleBSEImgsList m_listHoleBSEImg;
  28. protected ISemController m_SemHardwareMgr;
  29. protected IScanController m_ScanHardwareMgr;
  30. protected IEDSController m_EDSController;
  31. protected Queue<COTSFieldData> fieldQueue=new Queue<COTSFieldData>();
  32. protected IClassifyEngine m_classifyEngine;
  33. public CSmplMeasure( string a_strWorkingFolder, COTSSample a_pSample)
  34. {
  35. m_Sample = a_pSample;
  36. log = NLog.LogManager.GetCurrentClassLogger();
  37. var expC = m_Sample.GetMsrParams().GetXRayParam().GetAnalyExpCount();
  38. var imgwidth = m_Sample.GetMsrParams().GetImageScanParam().GetImageResolutionSize().cx;
  39. var imgheight = m_Sample.GetMsrParams().GetImageScanParam().GetImageResolutionSize().cy;
  40. m_strWorkingFolder = a_strWorkingFolder;
  41. m_pSampleRstFile = new CSmplMsrResult( a_strWorkingFolder, a_pSample);
  42. m_SemHardwareMgr = SemController.GetSEMController();
  43. m_ScanHardwareMgr = ScanController.GetScanController();
  44. m_EDSController = EDSController.GetEDSController(imgwidth,imgheight,expC);
  45. m_listHoleBSEImg = new CHoleBSEImgsList();
  46. m_Sample = a_pSample;
  47. }
  48. public void SetSample(COTSSample a_pSample)
  49. {
  50. m_Sample = a_pSample;
  51. m_pSampleRstFile.SetSample(a_pSample);
  52. }
  53. public COTSSample GetSample() { return m_Sample; }
  54. public void SetHolePreviewSample(COTSSample a_pSample)
  55. {
  56. m_HolePreviewSample = a_pSample;
  57. }
  58. public COTSSample GetHolePreviewSample() { return m_HolePreviewSample; }
  59. public void SetMsrThread(CMeasure mt)
  60. {
  61. m_pMsrThread = mt;
  62. }
  63. bool SetSEMDataMrs(COTSSample sample)
  64. {
  65. var pSEMDataMsr = sample.GetSEMDataMsr();
  66. double dMag = pSEMDataMsr.GetMagnification();
  67. double dWorkDis = pSEMDataMsr.GetWorkingDistance();
  68. var pSEMCtrl = m_pMsrThread.GetSEMController();
  69. pSEMCtrl.SetMagnification(dMag);
  70. pSEMCtrl.SetWorkingDistance(dWorkDis);
  71. return true;
  72. }
  73. bool SetSEMExteralOn()
  74. {
  75. var pSEMCtrl = m_SemHardwareMgr;
  76. log.Warn("Set Scan Exteral on!");
  77. pSEMCtrl.SetScanExternal(true);
  78. return true;
  79. }
  80. // set SEM external off
  81. bool SetSEMExteralOff()
  82. {
  83. var pSEMCtrlPtr = m_SemHardwareMgr;
  84. log.Warn("Set SEM Exteral Off!");
  85. pSEMCtrlPtr.SetScanExternal(false);
  86. return true;
  87. }
  88. bool SetBSEParam()
  89. {
  90. // get scan controller
  91. var pScanController = m_ScanHardwareMgr;
  92. // scan parameters
  93. var pMsrParam = m_Sample.GetMsrParams();
  94. var pImgScanParam = pMsrParam.GetImageScanParam();
  95. // get image size
  96. var nImageSizeId = pImgScanParam.GetImageResulotion();
  97. int nResulotionId = RESOLUTION_ID_FIRST_TIE + (int)nImageSizeId;
  98. Size sizePixelImage = RESOLUTION_VALUE[nResulotionId];
  99. // get dwell time
  100. OTS_IMAGE_SCANSPEED_OPTIONS nDwellTime = pImgScanParam.GetScanImageSpeed();
  101. // convert dwell time to bruker dwell time (8, 16, 32)
  102. int nBrukerDwellTimeId;
  103. switch (nDwellTime)
  104. {
  105. case OTS_IMAGE_SCANSPEED_OPTIONS.low:
  106. nBrukerDwellTimeId = 1;
  107. break;
  108. case OTS_IMAGE_SCANSPEED_OPTIONS.meddium:
  109. nBrukerDwellTimeId = 2;
  110. break;
  111. case OTS_IMAGE_SCANSPEED_OPTIONS.high:
  112. nBrukerDwellTimeId = 3;
  113. break;
  114. default:
  115. nBrukerDwellTimeId = 1;
  116. break;
  117. }
  118. long nBrukerDwellTime = OTSDataType.otsdataconst.DWELLTIME_BRUKER_VALUES[nBrukerDwellTimeId];
  119. if (!pScanController.Init())
  120. {
  121. log.Error("SetBSEParam: failed to get scan control.");
  122. return false;
  123. }
  124. // set dwell time
  125. if (!pScanController.SetDwellTime(nBrukerDwellTime))
  126. {
  127. log.Error("SetBSEParam: failed to set dwell time (%d) for bruker system.", nBrukerDwellTime);
  128. return false;
  129. }
  130. // set image size
  131. if (!pScanController.SetImageSize(sizePixelImage.Width,sizePixelImage.Height))
  132. {
  133. // failed to set dwell time
  134. log.Error("SetBSEParam: failed to set dwell time (%d).", sizePixelImage.Height);
  135. return false;
  136. }
  137. return true;
  138. }
  139. bool SetHoleBSEParam(COTSSample sample)
  140. {
  141. // get SEM controller
  142. //var pSEMController = m_SemHardwareMgr;
  143. // get scan controller
  144. var pScanController = m_ScanHardwareMgr;
  145. // scan parameters
  146. var pMsrParam = sample.GetMsrParams();
  147. var pImgScanParam = pMsrParam.GetImageScanParam();
  148. // get image size
  149. var nImageSizeId = pImgScanParam.GetImageResulotion();
  150. int nResulotionId = RESOLUTION_ID_FIRST_TIE + (int)nImageSizeId;
  151. Size sizePixelImage = RESOLUTION_VALUE[nResulotionId];
  152. // get dwell time
  153. OTS_IMAGE_SCANSPEED_OPTIONS nDwellTime = pImgScanParam.GetScanImageSpeed();
  154. // convert dwell time to bruker dwell time (6, 16, 32)
  155. long nBrukerDwellTime = DWELLTIME_BRUKER_VALUES[2];// choose the third option, so the dwell time will be 4 . there's no need to change here. it shoud be a const.
  156. if (!pScanController.Init())
  157. {
  158. log.Error("SetBSEParam: failed to get scan control.");
  159. return false;
  160. }
  161. // set dwell time
  162. if (!pScanController.SetDwellTime(nBrukerDwellTime))
  163. {
  164. log.Error("SetBSEParam: failed to set dwell time (%d) for bruker system.", nBrukerDwellTime);
  165. return false;
  166. }
  167. // set image size
  168. if (!pScanController.SetImageSize(sizePixelImage.Width/2, sizePixelImage.Height/2))
  169. {
  170. // failed to set dwell time
  171. log.Error("SetBSEParam: failed to set ImageSize");
  172. return false;
  173. }
  174. return true;
  175. }
  176. public void SetWorkingFolder(String a_strWorkingFolder)
  177. {
  178. // add "\\" at the string end if it is not "\\"
  179. if (a_strWorkingFolder.PadRight(1)!="\\")
  180. {
  181. a_strWorkingFolder += "\\";
  182. }
  183. m_strWorkingFolder = a_strWorkingFolder + m_Sample.GetName() + "\\";
  184. }
  185. bool CalculateUnMeasuredFieldsCenters(COTSSample sample, out List<System.Drawing.Point> a_allpieldcenter, out List<System.Drawing.Point> a_listUnMsrFieldCenter)
  186. {
  187. // sample measure parameters
  188. CSampleParam pMsrParam = sample.GetMsrParams();
  189. COTSImgScanPrm poImageScanParam = pMsrParam.GetImageScanParam();
  190. COTSImageProcParam pImgProcParam = pMsrParam.GetImageProcessParam();
  191. CSEMDataMsr poSEMDataMsr = sample.GetSEMDataMsr();
  192. CMsrSampleStatus pStatus = sample.GetMsrStatus();
  193. // measured field centers list
  194. List<System.Drawing.PointF> listCompletedCenter = pStatus.GetCompletedFieldsCenter();
  195. // field centers list manager
  196. CFieldPositionMgr pFieldMgr = new CFieldPositionMgr();
  197. // init field centers list manager
  198. if (!pFieldMgr.Init(sample.GetMsrDomain(), poImageScanParam, pImgProcParam, poSEMDataMsr, listCompletedCenter))
  199. {
  200. log.Error("CalculateFieldsCenters: failed to init field centres list manager.");
  201. a_listUnMsrFieldCenter = new List<System.Drawing.Point>();
  202. a_allpieldcenter = new List<Point>();
  203. return false;
  204. }
  205. // get field centers list
  206. a_listUnMsrFieldCenter = pFieldMgr.GetUnmeasuredFieldCentrePoints();// GetFieldCentrePoints();
  207. a_allpieldcenter = pFieldMgr.GetFieldCentrePoints();
  208. // ok, return TRUE
  209. return true;
  210. }
  211. protected bool IsAborted()
  212. {
  213. return m_pMsrThread.IsMeasureStopped();
  214. }
  215. protected bool IsPaused()
  216. {
  217. //Debug.Assert(m_pMsrThread == null);
  218. var statu = m_pMsrThread.GetMsrThreadStatus();
  219. if (statu.GetStatus() == OTS_MSR_THREAD_STATUS.PAUSED)
  220. {
  221. return true;
  222. }
  223. else
  224. {
  225. return false;
  226. }
  227. }
  228. bool IsSampleOver(COTSImgScanPrm a_pScanParam)
  229. {
  230. string sStopMode = a_pScanParam.GetStopMode();
  231. int nStopField = a_pScanParam.GetStopParamFields();
  232. // completed fields number
  233. CMsrSampleStatus pMsrSampleStatus = m_Sample.GetMsrStatus();
  234. int nCompeltedField = pMsrSampleStatus.GetCompletedFields();
  235. CMsrResultItems pMsrResults = m_Sample.GetMsrResults();
  236. List<CMsrResultItem> listMsrResult = pMsrResults.GetResultItems();
  237. int nNumParticle = 0;
  238. foreach (var pResult in listMsrResult)
  239. {
  240. if (pResult.GetTypeId() > (int)OTS_PARTICLE_TYPE.NOT_IDENTIFIED)//summarize the number of the identified particle in this condition
  241. {
  242. nNumParticle += (int)pResult.GetNumber();
  243. }
  244. }
  245. TimeSpan timeSpan = pMsrSampleStatus.GetUsedTime();
  246. int nDay = timeSpan.Days;
  247. int nHour = timeSpan.Hours;
  248. int nMin = timeSpan.Minutes;
  249. int nSec = timeSpan.Seconds;
  250. int nUsedTime = nSec + nMin * 60 + nHour * 3600 + nDay * 86400;
  251. int NMeasArea = (int)pMsrResults.GetMeasuredArea();
  252. int nParticlAim = a_pScanParam.GetStopParamParticles();
  253. int nMeasTimeAim = a_pScanParam.GetStopParamMeasTime();
  254. int NMeasAreaAim = a_pScanParam.GetStopParamArea()*1000000 ;
  255. bool bRet = false;
  256. string[] str = sStopMode.Replace(" ", "").Split('+');
  257. for(int i=0;i< str.Length;i++)
  258. {
  259. switch (int.Parse(str[i].Split(':')[0])-1)
  260. {
  261. case (int)OTS_MEASURE_STOP_MODE.CoverMode:
  262. // completed fields number
  263. if (nCompeltedField == m_Sample.GetFieldsData().Count)
  264. {
  265. bRet = true;
  266. }
  267. break;
  268. case (int)OTS_MEASURE_STOP_MODE.FieldMode:
  269. if (nCompeltedField >= nStopField)
  270. {
  271. bRet = true;
  272. }
  273. break;
  274. case (int)OTS_MEASURE_STOP_MODE.ParticleMode:
  275. if (nNumParticle >= nParticlAim)
  276. {
  277. bRet = true;
  278. }
  279. break;
  280. case (int)OTS_MEASURE_STOP_MODE.TimeMode:
  281. if (nUsedTime >= nMeasTimeAim)
  282. {
  283. bRet = true;
  284. }
  285. break;
  286. case (int)OTS_MEASURE_STOP_MODE.AreaMode:
  287. if (NMeasArea >= NMeasAreaAim)
  288. {
  289. bRet = true;
  290. }
  291. break;
  292. default:
  293. break;
  294. }
  295. }
  296. return bRet;
  297. }
  298. // move SEM to the point
  299. bool MoveSEMToPoint(System.Drawing.PointF a_poi)
  300. {
  301. // get SEM controller
  302. var pSEMController = m_SemHardwareMgr;
  303. PointF a_SEMpt = new Point();
  304. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  305. int hardWareDelay = a_pCSEMStageData.GetHardWareDelay();
  306. if (!a_pCSEMStageData.ConvertOTSToSEMCoord(a_poi, ref a_SEMpt))
  307. {
  308. return false;
  309. }
  310. log.Info("Begin to move SEM stage to OTScoord:" + a_poi.X + "," + a_poi.Y);
  311. log.Info("Begin to move SEM stage to " + a_SEMpt.X + "," + a_SEMpt.Y);
  312. // move SEM to the position (rotation 0)
  313. if (!pSEMController.MoveSEMToPoint(a_SEMpt.X, a_SEMpt.Y))
  314. {
  315. log.Error("MoveSEMToPoint: failed to call MoveSEMToPoint method.");
  316. return false;
  317. }
  318. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  319. if (pMsrParam.SlopParam.IsUsingSlopParam)
  320. {
  321. double wd = pMsrParam.SlopParam.GetWD(a_SEMpt);
  322. double originWd = 0;
  323. pSEMController.GetWorkingDistance(ref originWd);
  324. if ((wd - originWd) > 2)
  325. {
  326. log.Warn("Working Distance is invalid,outof the moving scope(2cm) wd=" + wd.ToString("F2"));
  327. }
  328. else
  329. {
  330. Thread.Sleep(hardWareDelay);
  331. pSEMController.SetWorkingDistance(wd);
  332. }
  333. }
  334. Thread.Sleep(hardWareDelay);
  335. return true;
  336. }
  337. // Acquire a BSE image
  338. CBSEImgClr AcquireABSEImage()
  339. {
  340. // BSE image
  341. CBSEImgClr pBSEImage = null;
  342. // get scan controller
  343. var pScanController = m_ScanHardwareMgr;
  344. pBSEImage = pScanController.AcquireBSEImage();
  345. return pBSEImage;
  346. }
  347. public bool IsLowCounts(COTSParticleClr particle)
  348. {
  349. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  350. if (pXRayParam.GetUsingXray() == true)
  351. {
  352. var thecount = particle.GetXray().GetTotalCount();
  353. var expect = pXRayParam.GetAnalyExpCount();
  354. if (thecount < expect)
  355. {
  356. particle.SetType((int)OTS_PARTICLE_TYPE.LOW_COUNT);
  357. particle.SetTypeColor("#000000");
  358. particle.SetTypeName("LowCounts");
  359. return true;
  360. }
  361. }
  362. return false;
  363. }
  364. public virtual void ClassifyFieldParticles(COTSFieldData curFldData)
  365. {
  366. return;
  367. }
  368. private class SEMStateObject
  369. {
  370. private PointF pos;
  371. private double workingDistance;
  372. private CSEMDataGnr semdata;
  373. private COTSSample originalSample;
  374. private double magnification;
  375. public PointF Pos { get => pos; set => pos = value; }
  376. public double WorkingDistance { get => workingDistance; set => workingDistance = value; }
  377. public CSEMDataGnr Semdata { get => semdata; set => semdata = value; }
  378. public double Magnification { get => magnification; set => magnification = value; }
  379. public COTSSample OriginalSample { get => originalSample; set => originalSample = value; }
  380. }
  381. private class AutoResetSEMControl:IDisposable
  382. {
  383. CSmplMeasure sm;
  384. private SEMStateObject semState=null;
  385. public AutoResetSEMControl(CSmplMeasure s)
  386. {
  387. sm = s;
  388. }
  389. public SEMStateObject SemState
  390. {
  391. get => semState;
  392. set => semState = value;
  393. }
  394. public void Dispose()
  395. {
  396. if (semState != null)
  397. {
  398. sm.SetBSEParam();
  399. sm.m_SemHardwareMgr.SetMagnification(semState.Magnification);
  400. Thread.Sleep(100);
  401. sm.MoveSEMToPoint(semState.Pos);
  402. Thread.Sleep(100);
  403. sm.m_SemHardwareMgr.SetWorkingDistance(semState.WorkingDistance);
  404. }
  405. sm.SetSEMExteralOff();
  406. }
  407. }
  408. private bool GetSEMDataGnrFromHw(ref CSEMDataGnr SemDataGnr)
  409. {
  410. double kv = 0, brightness = 0, contrast = 0;
  411. var hw = SemController.GetSEMController();
  412. hw.GetSemHighTension(ref kv);
  413. hw.GetSemBrightness(ref brightness);
  414. hw.GetSemContrast(ref contrast);
  415. SemDataGnr.SetValue(kv, brightness, contrast);
  416. return true;
  417. }
  418. public void DoMeasureForOneSample()
  419. {
  420. using (AutoResetSEMControl autoReset = new AutoResetSEMControl(this)) //when this method exit ,the SetSEMExternalOff and ResetScan will be called automatically.
  421. {
  422. // let the main thread to know that this sample measurement starts
  423. var pStatus = m_Sample.GetMsrStatus();
  424. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  425. // set current time to current time
  426. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.START);
  427. // let main App know that the sample begin to measure
  428. ST_MSTMsg MsgSmpStart = new ST_MSTMsg();
  429. MsgSmpStart.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  430. MsgSmpStart.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.INPROCESS;
  431. MsgSmpStart.STMSampleStu.cSampleName = m_Sample.GetName();
  432. m_pMsrThread.SendMessageToMeasureApp(MsgSmpStart);
  433. log.Info(m_Sample.GetName() + " Measurement started!");
  434. // get SEM controller to set magnification and working distance
  435. if (!SetSEMDataMrs(m_Sample))
  436. {
  437. log.Error("DoMeasure: fail to set SEM data.");
  438. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  439. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  440. return;
  441. }
  442. // set the BSE scan param
  443. if (!SetBSEParam())
  444. {
  445. log.Error("DoMeasure: fail to set BSE param.");
  446. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  447. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  448. return;
  449. }
  450. var pSEMDataGnr = new CSEMDataGnr();
  451. log.Info("Get Kv, Brightness and Contrast!");
  452. GetSEMDataGnrFromHw(ref pSEMDataGnr);
  453. m_pSampleRstFile.SetSEMGnr(pSEMDataGnr);
  454. // record SEM data
  455. COTSMsrPrjResultData pProjMgrFile = m_pMsrThread.GetProjResultData();
  456. CSEMStageData pSEMStageData = pProjMgrFile.GetSEMStageData();
  457. m_pSampleRstFile.SetSEMStageData(pSEMStageData);
  458. // record stage
  459. CStage pStage = pProjMgrFile.GetStage();
  460. m_pSampleRstFile.SetSEMStage(pStage);
  461. //-----save the static measure result file data into xml file and the dynamic data of every field will be saved into sqlite database
  462. log.Info("Create result file!");
  463. if (!m_pSampleRstFile.CreateResultFiles())
  464. {// failed to call measure result file Save method
  465. log.Error("DoMeasure: failed to call measure result file Save method.");
  466. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  467. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  468. return;
  469. }
  470. //------
  471. var FldDatas = m_Sample.GetFieldsData();
  472. if (m_EDSController.GetEDSType()==EDSTYPE.BRUKER && m_Sample.GetMsrParams().GetXRayParam().GetUseFilter())
  473. {
  474. List<string> KeyNameList = new List<string>();
  475. string DBAddress = Application.StartupPath + "\\Config\\SysData\\" + m_Sample.GetMsrParams().GetSTDName() + ".db";
  476. SQLiteHelper sQLiteHelper = new SQLiteHelper(DBAddress);//
  477. KeyNameList = sQLiteHelper.GetDBKeyElementList("ClassifySTD", "KeyElementList");
  478. m_EDSController.SetFilterKeyEleNames( KeyNameList);//will decide if do the quantification according to this keyelelist.
  479. }
  480. for (int i = 0; i < FldDatas.Count; ++i)
  481. {// check and break if stop button is clicked
  482. var curFld = FldDatas[i];
  483. if (curFld.GetIsMeasureComplete())
  484. {
  485. continue;
  486. }
  487. if (IsPaused())
  488. {// measure stopped
  489. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.PAUSED);
  490. // record end time
  491. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  492. //must wait for the saving data thread finished,or we'll get null pointer exception when we stop the measure process.
  493. while (fieldQueue.Count() > 0)
  494. {
  495. Thread.Sleep(100);
  496. }
  497. SetSEMExteralOff();
  498. // update thread measure status class, let the main thread know that this sample measurement stopped
  499. ST_MSTMsg MsgSmpStop = new ST_MSTMsg();
  500. MsgSmpStop.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  501. MsgSmpStop.STMThreadStu.iMsrStatu = OTS_MSR_THREAD_STATUS.PAUSED;
  502. MsgSmpStop.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.PAUSED;
  503. MsgSmpStop.STMThreadStu.csMsrEndTime = DateTime.Now.ToShortDateString();
  504. MsgSmpStop.STMThreadStu.iMsrStatu = OTS_MSR_THREAD_STATUS.PAUSED;
  505. m_pMsrThread.SendMessageToMeasureApp(MsgSmpStop);
  506. while (IsPaused())
  507. {
  508. Thread.Sleep(300);
  509. }
  510. }
  511. if (IsAborted())
  512. {// measure stopped
  513. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  514. // record end time
  515. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  516. //must wait for the saving data thread finished,or we'll get null pointer exception when we stop the measure process.
  517. while (fieldQueue.Count() > 0)
  518. {
  519. Thread.Sleep(100);
  520. }
  521. break;
  522. }
  523. // check if sample measurement completes
  524. COTSImgScanPrm pScanParam = m_Sample.GetMsrParams().GetImageScanParam();
  525. if (IsSampleOver(pScanParam))
  526. {
  527. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  528. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.COMPLT);
  529. break;
  530. }
  531. // get a field center
  532. System.Drawing.PointF poiFieldCentre = curFld.GetOTSPosition();
  533. // update thread measure status class, let the main thread know that starts a new field
  534. ST_MSTMsg MsgFieldStart = new ST_MSTMsg();
  535. MsgFieldStart.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  536. MsgFieldStart.STMSampleRetData.iRetDataType = MSAMPLE_RET.START_MSR_FIELD;
  537. MsgFieldStart.STMSampleRetData.SMsrField.FieldPos = new Point((int)poiFieldCentre.X,(int)poiFieldCentre.Y);
  538. m_pMsrThread.SendMessageToMeasureApp(MsgFieldStart);
  539. int fldNo = curFld.GetId();
  540. log.Warn("Current field:" + fldNo.ToString());
  541. // move SEM to the field center
  542. if (!MoveSEMToPoint(poiFieldCentre))
  543. {// failed to move SEM to the position
  544. log.Error("DoMeasure: failed to move SEM to the field centre point.");
  545. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  546. // record end time
  547. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  548. return;
  549. }
  550. log.Info("Begin to Acquire BSE image!");
  551. // take BSE image for the fields
  552. CBSEImgClr pBSEImg = AcquireABSEImage();
  553. // let the main thread to know that image process is completed
  554. if (pBSEImg == null)
  555. {
  556. log.Error("ImageProcess: can't get no background image.");
  557. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  558. return;
  559. }
  560. //BSEData
  561. ST_MSTMsg MsgFieldBSE = new ST_MSTMsg();
  562. MsgFieldBSE.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  563. MsgFieldBSE.STMSampleRetData.iRetDataType = MSAMPLE_RET.BSE_DATA;
  564. MsgFieldBSE.STMSampleRetData.BSEData.pos.X = (int)poiFieldCentre.X;
  565. MsgFieldBSE.STMSampleRetData.BSEData.pos.Y = (int)poiFieldCentre.Y;
  566. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataHeight = pBSEImg.GetHeight();
  567. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataWidth = pBSEImg.GetWidth();
  568. byte[] pImgData = pBSEImg.GetImageDataPtr();
  569. MsgFieldBSE.STMSampleRetData.BSEData.lpBSEData = pImgData;
  570. m_pMsrThread.SendMessageToMeasureApp(MsgFieldBSE);
  571. log.Info("Acquire BSE image success! Processing image...");
  572. // image process
  573. var rst = FieldImageProcess(curFld, pBSEImg);
  574. if (rst == true)
  575. {
  576. curFld.SetIsMeasureComplete(true);
  577. }
  578. else
  579. {
  580. return;
  581. }
  582. log.Info("Begin to classify particles! particle num:" + curFld.GetListAnalysisParticles().Count);
  583. ClassifyFieldParticles(curFld);
  584. //start db save
  585. StartSaveFileThread(ref curFld);
  586. SendFieldParticlesInfoToGUI(curFld, m_Sample.GetMsrStatus());
  587. }
  588. while (bSaveThreadWorking)//wait untill all the field data has been saved.
  589. {
  590. Thread.Sleep(1000);
  591. log.Warn("db saving!");
  592. }
  593. TheLastWorkOfSampleMeasure();
  594. }
  595. }
  596. public void TheLastWorkOfSampleMeasure()
  597. {
  598. COTSSample theSample = m_Sample;
  599. var pStatus = theSample.GetMsrStatus();
  600. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.COMPLT);
  601. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  602. // let main thread to know that this sample measurement completes
  603. ST_MSTMsg MsgSmplEnd = new ST_MSTMsg();
  604. MsgSmplEnd.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  605. MsgSmplEnd.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.SUCCESSED;
  606. MsgSmplEnd.STMSampleStu.cSampleName = theSample.GetName();
  607. m_pMsrThread.SendMessageToMeasureApp(MsgSmplEnd);
  608. }
  609. public bool DoMEasureForReMeasure()
  610. {
  611. return true;
  612. }
  613. public virtual bool FieldImageProcess(COTSFieldData curFldData, CBSEImgClr a_pBSEImg)
  614. {
  615. PointF fldCenter = curFldData.OTSPos;
  616. curFldData.SetBSEImage(a_pBSEImg);
  617. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  618. PointF semPos = new Point();
  619. a_pCSEMStageData.ConvertOTSToSEMCoord(fldCenter, ref semPos);
  620. curFldData.SetSemPos(semPos);
  621. //first step:remove background of the bse image and compound all the finded particles.
  622. log.Info("Begin to process image and get all particles!");
  623. GetOriginalParticles(ref curFldData);
  624. // second step :filter the finded particles.
  625. log.Info("Begin to filter particles!");
  626. FilterParticles(ref curFldData);
  627. CalculateParticleAbsolutPos(ref curFldData);
  628. log.Info("Begin to Calculate the image property of every particle!");
  629. var analysisparts = curFldData.GetListAnalysisParticles();
  630. curFldData.CalParticleImageProp(analysisparts);//calculate particle image property such as feret diameter, DMAX etc.
  631. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  632. if (pXRayParam.GetUsingXray() == true)
  633. {
  634. while (bSaveThreadWorking)
  635. {
  636. log.Info("last field has not finished yet! waiting!");
  637. Thread.Sleep(100);
  638. }
  639. CollectParticlesXrayData(curFldData);
  640. }
  641. m_SemHardwareMgr.SetScanExternal(false);
  642. m_Sample.GetMsrStatus().SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  643. return true;
  644. }
  645. private void FilterParticles(ref COTSFieldData curFldData)
  646. {
  647. var listXray = curFldData.GetAllParticles().OrderByDescending(x => x.GetActualArea()).ToList();
  648. var listXray1 = new List<COTSParticleClr>();
  649. var pXRayParam =m_Sample.GetMsrParams().GetXRayParam();
  650. if (listXray.Count > pXRayParam.GetXrayLimit())
  651. {
  652. for (var i = 0; i < pXRayParam.GetXrayLimit(); i++)
  653. {
  654. listXray1.Add(listXray[i]);
  655. }
  656. }
  657. else
  658. {
  659. listXray1 = listXray;
  660. }
  661. curFldData.SetListAnalysisParticles(listXray1);
  662. log.Info("Analysis particles:" + listXray1.Count);
  663. }
  664. public void GetOriginalParticles(ref COTSFieldData curFldData)
  665. {
  666. // get image process parameter
  667. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  668. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  669. var specialPartsparam = pMsrParam.GetSpecialGrayRangeParam();
  670. var pixelsize = m_Sample.CalculatePixelSize();
  671. if (specialPartsparam != null && specialPartsparam.GetIsToRun())
  672. {
  673. List<CSpecialGrayRange> ranges = pMsrParam.GetSpecialGrayRangeParam().GetSpecialGreyRanges();
  674. foreach (var grayRange in ranges)
  675. {
  676. CIntRangeClr range = new CIntRangeClr(grayRange.range.GetStart(), grayRange.range.GetEnd());
  677. CDoubleRangeClr diaRange = new CDoubleRangeClr(grayRange.diameterRange.GetStart(), grayRange.diameterRange.GetEnd());
  678. curFldData.GetPartsBySpecialGray(range, diaRange, pixelsize, grayRange.ifCollectXray);
  679. }
  680. }
  681. var ifCollectxray = pMsrParam.GetXRayParam().GetUsingXray();
  682. // remove BES image background
  683. curFldData.RemoveImgBGAndGetParticles(pImgProcessParam, pixelsize, ifCollectxray);
  684. // check if this is an empty image
  685. if (curFldData.NoParticle())
  686. { // empty fields
  687. log.Info("ImageProcess: empty field.");
  688. return;
  689. }
  690. double dPixelSize = m_Sample.CalculatePixelSize();
  691. curFldData.InitParticles(pImgProcessParam, dPixelSize);
  692. return;
  693. }
  694. public virtual void CollectParticlesXrayData(COTSFieldData curFldData)
  695. {
  696. List<COTSParticleClr> parts = new List<COTSParticleClr>();
  697. foreach (var p in curFldData.GetListXrayParticles())
  698. {
  699. if (p.IsXrayParticle())
  700. {
  701. parts.Add(p);
  702. }
  703. }
  704. curFldData.CreateXrayList(parts);
  705. // get x-ray parameters
  706. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  707. var workmode = pXRayParam.GetScanMode();
  708. log.Info("Begin to collect xraydata:"+parts.Count+" on "+workmode.ToString());
  709. if (workmode == OTS_X_RAY_SCAN_MODE.PointMode)
  710. {
  711. uint nXRayAQTime;
  712. if (parts.Count > 0)
  713. {
  714. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  715. m_EDSController.GetXRayByParts(parts, nXRayAQTime, true);
  716. }
  717. }
  718. else if (workmode == OTS_X_RAY_SCAN_MODE.FeatureMode)
  719. {
  720. uint nXRayAQTime;
  721. if (parts.Count > 0)
  722. {
  723. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  724. m_EDSController.GetXRayByFeatures(parts, nXRayAQTime, true);
  725. }
  726. }
  727. return;
  728. }
  729. public virtual void QuantifyParticlesXrayData(COTSFieldData curFldData)
  730. {
  731. var parts = curFldData.GetListXrayParticles();
  732. foreach (var p in parts)
  733. {
  734. m_EDSController.QuantifyXrayByPart(p);
  735. }
  736. }
  737. public void DoHolePreview()
  738. {
  739. using (AutoResetSEMControl autoReset = new AutoResetSEMControl(this))
  740. {
  741. //----------memorize the state of sem
  742. var semstate = new SEMStateObject();
  743. double dMagnification = m_Sample.GetSEMDataMsr().GetMagnification();
  744. double wd = m_Sample.GetSEMDataMsr().GetWorkingDistance();
  745. ISemController sem = m_SemHardwareMgr;
  746. double posX=0, posY=0,posR=0;
  747. sem.GetSemPositionXY(ref posX, ref posY, ref posR);
  748. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  749. Point pos = new Point((int)posX, (int)posY);
  750. PointF otsPos = new Point(0, 0);
  751. if (!a_pCSEMStageData.ConvertSEMToOTSCoord(pos, ref otsPos))
  752. {
  753. return;
  754. }
  755. semstate.Pos = otsPos;
  756. semstate.Magnification = dMagnification;
  757. semstate.WorkingDistance = wd;
  758. autoReset.SemState = semstate;
  759. //-------------------------------------
  760. // let the main thread to know that this sample measurement starts
  761. CMsrSampleStatus pStatus = m_HolePreviewSample.GetMsrStatus();
  762. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  763. // set current time to current time
  764. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.START);
  765. // let main App know that the sample begin to measure
  766. ST_MSTMsg MsgSmpStart=new ST_MSTMsg();
  767. MsgSmpStart.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  768. MsgSmpStart.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.INPROCESS;
  769. MsgSmpStart.STMSampleStu.cSampleName=m_Sample.GetName();
  770. // get SEM controller to set magnification and working distance
  771. if (!SetSEMDataMrs(m_HolePreviewSample))
  772. {
  773. log.Error("DoHolePreview: fail to set SEM data.");
  774. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  775. // record end time
  776. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  777. return;
  778. }
  779. // set the BSE scan param
  780. if (!SetHoleBSEParam(m_HolePreviewSample))
  781. {
  782. log.Error("DoHolePreview: fail to set BSE param.");
  783. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  784. // record end time
  785. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  786. return;
  787. }
  788. // calculate field centers
  789. List<System.Drawing.Point> listFieldCenter;
  790. List<System.Drawing.Point> alllistFieldCenter ;
  791. if (!CalculateUnMeasuredFieldsCenters(m_HolePreviewSample, out alllistFieldCenter,out listFieldCenter))
  792. {// failed to calculate field centers
  793. log.Error("DoHolePreview: failed to calculate field centers.");
  794. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  795. // record end time
  796. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  797. return;
  798. }
  799. // go through each field
  800. int nNewFieldId = 0;
  801. for ( int i = 0; i < listFieldCenter.Count; ++i)
  802. {// check and break if stop button is clicked
  803. if (IsAborted())
  804. {// measure stopped
  805. log.Trace("DoHolePreview: measure thread is stopped.");
  806. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  807. // record end time
  808. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  809. return;
  810. }
  811. // check if sample measurement completes
  812. COTSImgScanPrm pScanParam = m_Sample.GetMsrParams().GetImageScanParam();
  813. int nTotalFieldSize = listFieldCenter.Count;
  814. // get a field center
  815. System.Drawing.Point poiFieldCentre = listFieldCenter[i];
  816. // update thread measure status class, let the main thread know that starts a new field
  817. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  818. ST_MSTMsg MsgFieldStart=new ST_MSTMsg();
  819. MsgFieldStart.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  820. MsgFieldStart.STMSampleRetData.iRetDataType = MSAMPLE_RET.START_MSR_FIELD;
  821. MsgFieldStart.STMSampleRetData.SMsrField.FieldPos = poiFieldCentre;
  822. m_pMsrThread.SendMessageToMeasureApp(MsgFieldStart);
  823. // move SEM to the field center
  824. if (!MoveSEMToPoint(poiFieldCentre))
  825. {// failed to move SEM to the position
  826. log.Error("DoHolePreview: failed to move SEM to the field centre point.");
  827. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  828. // record end time
  829. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  830. return;
  831. }
  832. // take BSE image for the fields
  833. CBSEImgClr pBSEIamge = AcquireABSEImage();
  834. if (pBSEIamge==null)
  835. {
  836. // failed to acquire a BSE image
  837. log.Error("DoHolePreview: failed to acquire a BSE image.");
  838. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  839. // record end time
  840. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  841. return;
  842. }
  843. //BSEData
  844. ST_MSTMsg MsgFieldBSE=new ST_MSTMsg();
  845. MsgFieldBSE.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  846. MsgFieldBSE.STMSampleRetData.iRetDataType = MSAMPLE_RET.BSE_DATA;
  847. MsgFieldBSE.STMSampleRetData.BSEData.pos = poiFieldCentre;
  848. MsgFieldBSE.STMSampleRetData.BSEData.fieldId = i;
  849. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataHeight = pBSEIamge.GetHeight();
  850. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataWidth = pBSEIamge.GetWidth();
  851. byte[] pImgData = pBSEIamge.GetImageDataPtr();
  852. MsgFieldBSE.STMSampleRetData.BSEData.lpBSEData = pImgData;
  853. m_pMsrThread.SendMessageToMeasureApp(MsgFieldBSE);
  854. if (pStatus.GetStatus() != OTS_MSR_SAMPLE_STATUS.INPROCESS)
  855. {
  856. // measurement failed or stopped
  857. log.Error("DoHolePreview: measurement failed or stopped.");
  858. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  859. // record end time
  860. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  861. return;
  862. }
  863. //save the result to project file
  864. Rectangle oImageRect = (Rectangle)pBSEIamge.GetImageRect();
  865. CHoleBSEImg pHoleBSEImg =new CHoleBSEImg(oImageRect,poiFieldCentre);
  866. pHoleBSEImg.SetImageData( pBSEIamge.GetImageDataPtr(),oImageRect.Width,oImageRect.Height);
  867. m_listHoleBSEImg.Add(pHoleBSEImg);
  868. // prepare for the next
  869. ++nNewFieldId;
  870. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  871. ST_MSTMsg MsgFieldEnd=new ST_MSTMsg();
  872. MsgFieldEnd.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  873. MsgFieldEnd.STMSampleRetData.iRetDataType = MSAMPLE_RET.FIELD_DATA;
  874. MsgFieldEnd.STMSampleRetData.SFieldData.iCompleteFieldCount = (i + 1);
  875. MsgFieldEnd.STMSampleRetData.SFieldData.iMeasureFieldCount = listFieldCenter.Count;
  876. MsgFieldEnd.STMSampleRetData.SFieldData.iSParticleCount = 0;
  877. MsgFieldEnd.STMSampleRetData.SFieldData.FieldPos = poiFieldCentre;
  878. m_pMsrThread.SendMessageToMeasureApp(MsgFieldEnd);
  879. }
  880. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  881. //calculate measure time
  882. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  883. // let main thread to know that this sample measurement completes
  884. ST_MSTMsg MsgSmplEnd=new ST_MSTMsg();
  885. MsgSmplEnd.iMsgType = ENUM_MSG_TYPE.MTHREADSTATUS;
  886. MsgSmplEnd.STMThreadStu.iMsrStatu = OTS_MSR_THREAD_STATUS.COMPLETED;
  887. m_pMsrThread.SendMessageToMeasureApp(MsgSmplEnd);
  888. }
  889. }
  890. // Cumulate field data info
  891. public virtual bool CumulateFieldData( List<COTSParticleClr> listParticles, double a_nMeasuredArea)
  892. {
  893. // get measure result items of the sample
  894. CMsrResultItems pMsrResults = m_Sample.GetMsrResults();
  895. // cumulate field data info
  896. pMsrResults.CumulateMeasuredArea(a_nMeasuredArea);
  897. // cumulate ratio
  898. double dRatio = 10000 * pMsrResults.GetTotalParticleArea();
  899. dRatio = dRatio / pMsrResults.GetMeasuredArea();
  900. pMsrResults.SetRatio(dRatio);
  901. // go through the particles list
  902. foreach (COTSParticleClr pParticle in listParticles)
  903. {
  904. // create a measure result item
  905. pMsrResults.CumulateMeasureResult(pParticle);
  906. }
  907. m_Sample.SetMsrResults(pMsrResults);
  908. // ok, return TRUE
  909. return true;
  910. }
  911. public bool SaveFieldData(COTSFieldData fldData, string filedFileFoler)
  912. {
  913. string strFieldFileFolder = filedFileFoler;
  914. CBSEImageFileMgr pBSEImgFileMgr = new CBSEImageFileMgr();
  915. pBSEImgFileMgr.SetBSEImg(fldData.GetBSEImage());
  916. int nId = fldData.GetId();
  917. string sFieldId;
  918. sFieldId = nId.ToString();
  919. string strBSEFilePathname = strFieldFileFolder + "\\" + m_pSampleRstFile.SMPL_MSR_RESULT_FIELDS_BSE + sFieldId + pBSEImgFileMgr.BMP_IMG_FILE_EXT;
  920. if (!pBSEImgFileMgr.SaveIntoBitmap(strBSEFilePathname))
  921. {
  922. log.Error("SaveFieldFiles: save BSE file failed.");
  923. return false;
  924. }
  925. // IncA Data list
  926. CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
  927. pDBFileMgr.SaveStatusDataToDB();
  928. var fldDB = pDBFileMgr.GetFieldDB();
  929. log.Warn("Start saving particle data.");
  930. var fldcmd = fldDB.GetSavingAFieldcmdObj(fldData.GetId(), fldData.GetOTSPosition(), fldData.GetSemPos());
  931. List<KeyValuePair<string, System.Data.SQLite.SQLiteParameter[]>> fldcmds = new List<KeyValuePair<string, System.Data.SQLite.SQLiteParameter[]>>();
  932. fldcmds.Add(fldcmd);
  933. pDBFileMgr.ExecuteNonQueryBatch(ref fldcmds);
  934. //remomove the invalid particles
  935. var cmds = pDBFileMgr.GetSavingParticleDataToDBCmds(fldData.GetListAnalysisParticles(), fldData.GetOTSPosition());
  936. pDBFileMgr.ExecuteNonQueryBatch(ref cmds);
  937. CPosXrayDBMgr PosXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
  938. var listAnalysisPosXray = new List<CPosXrayClr>();
  939. foreach (var p in fldData.GetListXrayParticles())
  940. {
  941. listAnalysisPosXray.Add(p.GetXray());
  942. }
  943. var cmds1 = PosXrayDBMgr.GetSavingXrayCmds(listAnalysisPosXray, true);
  944. pDBFileMgr.ExecuteNonQueryBatch(ref cmds1);
  945. return true;
  946. }
  947. protected void SaveFieldMgrData()
  948. {
  949. while (bSaveThreadWorking)
  950. {
  951. while (fieldQueue.Count() > 0)
  952. {
  953. COTSFieldData f = fieldQueue.Dequeue();
  954. //save to disk first ,then pop . if the size is 0,then we know all the saving work is done.
  955. log.Info("Begin to save particles data! particle num:" + f.GetListAnalysisParticles().Count);
  956. SaveFieldData(f, m_pSampleRstFile.GetFieldFileSubFolderStr());
  957. }
  958. if (fieldQueue.Count() == 0)
  959. {
  960. bSaveThreadWorking = false; //must set this flag,so the main thread can know this thread has exited.
  961. log.Warn("finished batch saving");
  962. return;
  963. }
  964. }
  965. return;
  966. }
  967. private void SendFieldParticlesInfoToGUI(COTSFieldData curFld,CMsrSampleStatus pStatus)
  968. {
  969. double measuredArea = 0; // this area should be the field area
  970. var a_pBSEImg = curFld.GetBSEImage();
  971. double dPixelSize = curFld.GetPixelSize();
  972. measuredArea = a_pBSEImg.GetHeight() * a_pBSEImg.GetWidth() * dPixelSize * dPixelSize; //Get measured area
  973. CumulateFieldData(curFld.GetListAnalysisParticles(), measuredArea);
  974. log.Info("Send field data to screen!");
  975. pStatus.AddCompletedFieldCenter(curFld.OTSPos);
  976. //Field Data
  977. // record end time
  978. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  979. ST_MSTMsg MsgFieldEnd = new ST_MSTMsg();
  980. MsgFieldEnd.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  981. MsgFieldEnd.STMSampleRetData.iRetDataType = MSAMPLE_RET.FIELD_DATA;
  982. MsgFieldEnd.STMSampleRetData.SFieldData.iCompleteFieldCount = pStatus.GetCompletedFields();
  983. MsgFieldEnd.STMSampleRetData.SFieldData.iMeasureFieldCount = m_Sample.GetFieldsData().Count;
  984. MsgFieldEnd.STMSampleRetData.SFieldData.iSParticleCount = (int)curFld.GetListAnalysisParticles().Count;
  985. MsgFieldEnd.STMSampleRetData.SFieldData.FieldPos.X = Convert.ToInt32(curFld.OTSPos.X);
  986. MsgFieldEnd.STMSampleRetData.SFieldData.FieldPos.Y = Convert.ToInt32(curFld.OTSPos.Y);
  987. m_pMsrThread.SendMessageToMeasureApp(MsgFieldEnd);
  988. }
  989. protected void StartSaveFileThread(ref COTSFieldData a_pFieldMgr)
  990. {
  991. fieldQueue.Enqueue(a_pFieldMgr);
  992. if (fieldQueue.Count() > 0) //if there's data in the queue and the previous thread has finished then start a new thread.
  993. {
  994. if (bSaveThreadWorking == false)
  995. {
  996. bSaveThreadWorking = true;
  997. m_thread_ptr = new System.Threading.Thread(this.SaveFieldMgrData);//m_thread_ptr = shared_ptr<thread>(new thread(&CSmplMeasureInc::SaveFieldMgrData, this));
  998. m_thread_ptr.IsBackground = true;
  999. m_thread_ptr.Start();
  1000. }
  1001. }
  1002. }
  1003. public void ParticleSpecialTreatment(ref COTSFieldData fld)
  1004. {
  1005. //we adopt such a strategy here:if some particles satisfy the predefined condition then we go through the second collecting with max EDS time,so that we got a more acurate result.
  1006. Dictionary<COTSParticleClr, int> mapPartXray = new Dictionary<COTSParticleClr, int>();
  1007. double edsTime = 0; //to store the EDSTime returned from the engine.
  1008. var bigparts = fld.GetListXrayParticles();
  1009. var m_ClassifyEngine = new CClassifyEngine();
  1010. for (int i = 0; i < bigparts.Count(); i++)
  1011. {
  1012. var engine = m_ClassifyEngine.GetExpressionClassifyEngine(m_Sample.GetMsrParams().GetSTDName());
  1013. var p = bigparts[i];
  1014. // there will be very less particle satisfied the condition ,so we use only one MaxEDS time,though we set MaxEDSTime for every rule separately .Here we store the last one.
  1015. //if the particle satisfied the MaxEDS condition then we go through the second colleting.Here we record the particle and the EDSTime and the corresponding xray position.
  1016. edsTime = engine.IfNeedMaxEDS(p);
  1017. if (edsTime > 0)
  1018. {
  1019. mapPartXray[p] = i;
  1020. }
  1021. }
  1022. List<COTSParticleClr> partsMax = new List<COTSParticleClr>();
  1023. if (mapPartXray.Count() > 0)
  1024. {
  1025. foreach (var p in mapPartXray)
  1026. {
  1027. partsMax.Add(p.Key);
  1028. }
  1029. List<CPosXrayClr> maxEDSXrays = new List<CPosXrayClr>();
  1030. m_EDSController.GetXRayByFeatures(partsMax, edsTime, true);
  1031. }
  1032. }
  1033. public void CalculateParticleAbsolutPos(ref COTSFieldData curFldData)
  1034. {
  1035. double dPixelSize = m_Sample.CalculatePixelSize();
  1036. CSEMStageData pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  1037. foreach (var p in curFldData.GetListAnalysisParticles())
  1038. {
  1039. //Point fldOtsPos = new Point((curFldData.OTSPos.X, curFldData.OTSPos.Y);
  1040. PointF semP = new PointF(); ;
  1041. Point semPos = new Point();
  1042. pCSEMStageData.ConvertOTSToSEMCoord(curFldData.OTSPos, ref semP);
  1043. semPos.X = (int)semP.X;
  1044. semPos.Y = (int)semP.Y;
  1045. p.SetAbsolutPos(semPos);
  1046. }
  1047. }
  1048. }
  1049. }