CSmplMeasure.cs 55 KB

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