CSmplMeasure.cs 56 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_EDSHardwareMgr;
  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_EDSHardwareMgr = 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_PARTCLE_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 virtual void ClassifyFieldParticles(COTSFieldData curFldData)
  351. {
  352. return;
  353. }
  354. public virtual void ClassifyMergedParticles(List<COTSParticleClr> parts)
  355. {
  356. return;
  357. }
  358. private class SEMStateObject
  359. {
  360. private PointF pos;
  361. private double workingDistance;
  362. private CSEMDataGnr semdata;
  363. private COTSSample originalSample;
  364. private double magnification;
  365. public PointF Pos { get => pos; set => pos = value; }
  366. public double WorkingDistance { get => workingDistance; set => workingDistance = value; }
  367. public CSEMDataGnr Semdata { get => semdata; set => semdata = value; }
  368. public double Magnification { get => magnification; set => magnification = value; }
  369. public COTSSample OriginalSample { get => originalSample; set => originalSample = value; }
  370. }
  371. private class AutoResetSEMControl:IDisposable
  372. {
  373. CSmplMeasure sm;
  374. private SEMStateObject semState=null;
  375. public AutoResetSEMControl(CSmplMeasure s)
  376. {
  377. sm = s;
  378. }
  379. public SEMStateObject SemState
  380. {
  381. get => semState;
  382. set => semState = value;
  383. }
  384. public void Dispose()
  385. {
  386. if (semState != null)
  387. {
  388. sm.SetBSEParam();
  389. sm.m_SemHardwareMgr.SetMagnification(semState.Magnification);
  390. Thread.Sleep(100);
  391. sm.MoveSEMToPoint(semState.Pos);
  392. Thread.Sleep(100);
  393. sm.m_SemHardwareMgr.SetWorkingDistance(semState.WorkingDistance);
  394. }
  395. sm.SetSEMExteralOff();
  396. }
  397. }
  398. private bool GetSEMDataGnrFromHw(ref CSEMDataGnr SemDataGnr)
  399. {
  400. double kv = 0, brightness = 0, contrast = 0;
  401. var hw = SemController.GetSEMController();
  402. hw.GetSemHighTension(ref kv);
  403. hw.GetSemBrightness(ref brightness);
  404. hw.GetSemContrast(ref contrast);
  405. SemDataGnr.SetValue(kv, brightness, contrast);
  406. return true;
  407. }
  408. public void DoMeasureForOneSample()
  409. {
  410. using (AutoResetSEMControl autoReset = new AutoResetSEMControl(this)) //when this method exit ,the SetSEMExternalOff and ResetScan will be called automatically.
  411. {
  412. // let the main thread to know that this sample measurement starts
  413. var pStatus = m_Sample.GetMsrStatus();
  414. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  415. // set current time to current time
  416. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.START);
  417. // let main App know that the sample begin to measure
  418. ST_MSTMsg MsgSmpStart = new ST_MSTMsg();
  419. MsgSmpStart.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  420. MsgSmpStart.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.INPROCESS;
  421. MsgSmpStart.STMSampleStu.cSampleName = m_Sample.GetName();
  422. m_pMsrThread.SendMessageToMeasureApp(MsgSmpStart);
  423. log.Info(m_Sample.GetName() + " Measurement started!");
  424. // get SEM controller to set magnification and working distance
  425. if (!SetSEMDataMrs(m_Sample))
  426. {
  427. log.Error("DoMeasure: fail to set SEM data.");
  428. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  429. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  430. return;
  431. }
  432. // set the BSE scan param
  433. if (!SetBSEParam())
  434. {
  435. log.Error("DoMeasure: fail to set BSE param.");
  436. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  437. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  438. return;
  439. }
  440. var pSEMDataGnr = new CSEMDataGnr();
  441. log.Info("Get Kv, Brightness and Contrast!");
  442. GetSEMDataGnrFromHw(ref pSEMDataGnr);
  443. m_pSampleRstFile.SetSEMGnr(pSEMDataGnr);
  444. // record SEM data
  445. COTSMsrPrjResultData pProjMgrFile = m_pMsrThread.GetProjResultData();
  446. CSEMStageData pSEMStageData = pProjMgrFile.GetSEMStageData();
  447. m_pSampleRstFile.SetSEMStageData(pSEMStageData);
  448. // record stage
  449. CStage pStage = pProjMgrFile.GetStage();
  450. m_pSampleRstFile.SetSEMStage(pStage);
  451. //-----save the static measure result file data into xml file and the dynamic data of every field will be saved into sqlite database
  452. log.Info("Create result file!");
  453. if (!m_pSampleRstFile.CreateResultFiles())
  454. {// failed to call measure result file Save method
  455. log.Error("DoMeasure: failed to call measure result file Save method.");
  456. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  457. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  458. return;
  459. }
  460. //------
  461. var FldDatas = m_Sample.GetFieldsData();
  462. if (m_EDSHardwareMgr.GetEDSType()==EDSTYPE.BRUKER && m_Sample.GetMsrParams().GetXRayParam().GetUseFilter())
  463. {
  464. List<string> KeyNameList = new List<string>();
  465. string DBAddress = Application.StartupPath + "\\Config\\SysData\\" + m_Sample.GetMsrParams().GetSTDName() + ".db";
  466. SQLiteHelper sQLiteHelper = new SQLiteHelper(DBAddress);//
  467. KeyNameList = sQLiteHelper.GetDBKeyElementList("ClassifySTD", "KeyElementList");
  468. m_EDSHardwareMgr.SetFilterKeyEleNames( KeyNameList);//will decide if do the quantification according to this keyelelist.
  469. }
  470. for (int i = 0; i < FldDatas.Count; ++i)
  471. {// check and break if stop button is clicked
  472. var curFld = FldDatas[i];
  473. if (curFld.GetIsMeasureComplete())
  474. {
  475. continue;
  476. }
  477. if (IsPaused())
  478. {// measure stopped
  479. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.PAUSED);
  480. // record end time
  481. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  482. //must wait for the saving data thread finished,or we'll get null pointer exception when we stop the measure process.
  483. while (fieldQueue.Count() > 0)
  484. {
  485. Thread.Sleep(100);
  486. }
  487. SetSEMExteralOff();
  488. // update thread measure status class, let the main thread know that this sample measurement stopped
  489. ST_MSTMsg MsgSmpStop = new ST_MSTMsg();
  490. MsgSmpStop.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  491. MsgSmpStop.STMThreadStu.iMsrStatu = OTS_MSR_THREAD_STATUS.PAUSED;
  492. MsgSmpStop.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.PAUSED;
  493. MsgSmpStop.STMThreadStu.csMsrEndTime = DateTime.Now.ToShortDateString();
  494. MsgSmpStop.STMThreadStu.iMsrStatu = OTS_MSR_THREAD_STATUS.PAUSED;
  495. m_pMsrThread.SendMessageToMeasureApp(MsgSmpStop);
  496. while (IsPaused())
  497. {
  498. Thread.Sleep(300);
  499. }
  500. }
  501. if (IsAborted())
  502. {// measure stopped
  503. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  504. // record end time
  505. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  506. //must wait for the saving data thread finished,or we'll get null pointer exception when we stop the measure process.
  507. while (fieldQueue.Count() > 0)
  508. {
  509. Thread.Sleep(100);
  510. }
  511. break;
  512. }
  513. // check if sample measurement completes
  514. COTSImgScanPrm pScanParam = m_Sample.GetMsrParams().GetImageScanParam();
  515. if (IsSampleOver(pScanParam))
  516. {
  517. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  518. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.COMPLT);
  519. break;
  520. }
  521. // get a field center
  522. System.Drawing.PointF poiFieldCentre = curFld.GetOTSPosition();
  523. // update thread measure status class, let the main thread know that starts a new field
  524. ST_MSTMsg MsgFieldStart = new ST_MSTMsg();
  525. MsgFieldStart.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  526. MsgFieldStart.STMSampleRetData.iRetDataType = MSAMPLE_RET.START_MSR_FIELD;
  527. MsgFieldStart.STMSampleRetData.SMsrField.FieldPos = new Point((int)poiFieldCentre.X,(int)poiFieldCentre.Y);
  528. m_pMsrThread.SendMessageToMeasureApp(MsgFieldStart);
  529. int fldNo = curFld.GetId();
  530. log.Warn("Current field:" + fldNo.ToString());
  531. // move SEM to the field center
  532. if (!MoveSEMToPoint(poiFieldCentre))
  533. {// failed to move SEM to the position
  534. log.Error("DoMeasure: failed to move SEM to the field centre point.");
  535. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  536. // record end time
  537. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  538. return;
  539. }
  540. log.Info("Begin to Acquire BSE image!");
  541. // take BSE image for the fields
  542. CBSEImgClr pBSEImg = AcquireABSEImage();
  543. // let the main thread to know that image process is completed
  544. if (pBSEImg == null)
  545. {
  546. log.Error("ImageProcess: can't get no background image.");
  547. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  548. return;
  549. }
  550. //BSEData
  551. ST_MSTMsg MsgFieldBSE = new ST_MSTMsg();
  552. MsgFieldBSE.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  553. MsgFieldBSE.STMSampleRetData.iRetDataType = MSAMPLE_RET.BSE_DATA;
  554. MsgFieldBSE.STMSampleRetData.BSEData.pos.X = (int)poiFieldCentre.X;
  555. MsgFieldBSE.STMSampleRetData.BSEData.pos.Y = (int)poiFieldCentre.Y;
  556. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataHeight = pBSEImg.GetHeight();
  557. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataWidth = pBSEImg.GetWidth();
  558. byte[] pImgData = pBSEImg.GetImageDataPtr();
  559. MsgFieldBSE.STMSampleRetData.BSEData.lpBSEData = pImgData;
  560. m_pMsrThread.SendMessageToMeasureApp(MsgFieldBSE);
  561. log.Info("Acquire BSE image success! Processing image...");
  562. // image process
  563. var rst = FieldImageProcess(curFld, pBSEImg);
  564. if (rst == true)
  565. {
  566. curFld.SetIsMeasureComplete(true);
  567. }
  568. else
  569. {
  570. return;
  571. }
  572. //start db save
  573. StartSaveFileThread(ref curFld);
  574. double measuredArea = 0; // this area should be the field area
  575. var a_pBSEImg = curFld.GetBSEImage();
  576. double dPixelSize = curFld.GetPixelSize();
  577. measuredArea = a_pBSEImg.GetHeight() * a_pBSEImg.GetWidth()* dPixelSize * dPixelSize + 0.5; //Get measured area
  578. CumulateFieldData(curFld.GetListAnalysisParticles(), measuredArea);
  579. log.Info("Send field data to screen!");
  580. pStatus.AddCompletedFieldCenter(poiFieldCentre);
  581. //Field Data
  582. // record end time
  583. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  584. ST_MSTMsg MsgFieldEnd = new ST_MSTMsg();
  585. MsgFieldEnd.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  586. MsgFieldEnd.STMSampleRetData.iRetDataType = MSAMPLE_RET.FIELD_DATA;
  587. MsgFieldEnd.STMSampleRetData.SFieldData.iCompleteFieldCount = pStatus.GetCompletedFields();
  588. MsgFieldEnd.STMSampleRetData.SFieldData.iMeasureFieldCount = m_Sample.GetFieldsData().Count;
  589. MsgFieldEnd.STMSampleRetData.SFieldData.iSParticleCount = (int)curFld.GetListAnalysisParticles().Count;
  590. MsgFieldEnd.STMSampleRetData.SFieldData.FieldPos.X = Convert.ToInt32(poiFieldCentre.X);
  591. MsgFieldEnd.STMSampleRetData.SFieldData.FieldPos.Y = Convert.ToInt32(poiFieldCentre.Y);
  592. m_pMsrThread.SendMessageToMeasureApp(MsgFieldEnd);
  593. }
  594. while (bSaveThreadWorking)//wait untill all the field data has been saved.
  595. {
  596. Thread.Sleep(1000);
  597. log.Warn("db saving!");
  598. }
  599. TheLastWorkOfSampleMeasure();
  600. }
  601. }
  602. public void TheLastWorkOfSampleMeasure()
  603. {
  604. COTSSample theSample = m_Sample;
  605. var pStatus = theSample.GetMsrStatus();
  606. //merging particles
  607. log.Info("Merging big particles which are crossing the field edge!");
  608. CImageHandler imgpro = new CImageHandler();
  609. int scanfldsize = theSample.GetSEMDataMsr().GetScanFieldSize();
  610. List<COTSParticleClr> mergedParticles = new List<COTSParticleClr>();
  611. double pixelSize = theSample.CalculatePixelSize();
  612. imgpro.MergeBigBoundaryParticles(theSample.GetFieldsData(), pixelSize, scanfldsize, theSample.GetResolutionSize(), ref mergedParticles);
  613. CalculateMergedPartProperty(mergedParticles, pixelSize);
  614. ClassifyMergedParticles(mergedParticles);
  615. log.Info("begin merged particle data db saving...");
  616. SaveMergedParticles(mergedParticles);
  617. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.COMPLT);
  618. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  619. // let main thread to know that this sample measurement completes
  620. ST_MSTMsg MsgSmplEnd = new ST_MSTMsg();
  621. MsgSmplEnd.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  622. MsgSmplEnd.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.SUCCESSED;
  623. MsgSmplEnd.STMSampleStu.cSampleName = theSample.GetName();
  624. m_pMsrThread.SendMessageToMeasureApp(MsgSmplEnd);
  625. }
  626. public bool DoMEasureForReMeasure()
  627. {
  628. return true;
  629. }
  630. private void CalculateMergedPartProperty(List<COTSParticleClr> mergedParticles,double pixelSize)
  631. {
  632. var m_ImagePro = new CImageHandler();
  633. foreach (COTSParticleClr part in mergedParticles)
  634. {
  635. m_ImagePro.CalParticleImageProp(part, pixelSize);
  636. }
  637. }
  638. public virtual bool FieldImageProcess(COTSFieldData curFldData, CBSEImgClr a_pBSEImg)
  639. {
  640. PointF fldCenter = curFldData.OTSPos;
  641. curFldData.SetBSEImage(a_pBSEImg);
  642. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  643. PointF semPos = new Point();
  644. a_pCSEMStageData.ConvertOTSToSEMCoord(fldCenter, ref semPos);
  645. curFldData.SetSemPos(semPos);
  646. //first step:remove background of the bse image and compound all the finded particles.
  647. log.Info("Begin to process image and get all particles!");
  648. GetOriginalParticles(ref curFldData);
  649. // second step :filter the finded particles.
  650. log.Info("Begin to filter particles!");
  651. FilterParticles(ref curFldData);
  652. CalculateParticleAbsolutPos(ref curFldData);
  653. log.Info("Begin to Calculate the image property of every particle!");
  654. var analysisparts = curFldData.GetListAnalysisParticles();
  655. curFldData.CalParticleImageProp(analysisparts);//calculate particle image property such as feret diameter, DMAX etc.
  656. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  657. if (pXRayParam.GetUsingXray() == true)
  658. {
  659. log.Info("Begin to collect particle's xray data!");
  660. //Thread.Sleep(100);
  661. CollectParticlesXrayData(curFldData);
  662. //Thread.Sleep(100);
  663. }
  664. log.Info("Begin to classify particles! particle num:" + curFldData.GetListAnalysisParticles().Count);
  665. ClassifyFieldParticles(curFldData);
  666. m_Sample.GetMsrStatus().SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  667. return true;
  668. }
  669. public void GetOriginalParticles(ref COTSFieldData curFldData)
  670. {
  671. // measure status
  672. //CMsrSampleStatus pStatus = m_Sample.GetMsrStatus();
  673. // get image process parameter
  674. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  675. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  676. var specialPartsparam = pMsrParam.GetSpecialGrayRangeParam();
  677. var pixelsize = m_Sample.CalculatePixelSize();
  678. if (specialPartsparam != null && specialPartsparam.GetIsToRun())
  679. {
  680. List<CSpecialGrayRange> ranges = pMsrParam.GetSpecialGrayRangeParam().GetSpecialGreyRanges();
  681. foreach (var grayRange in ranges)
  682. {
  683. CIntRangeClr range = new CIntRangeClr(grayRange.range.GetStart(), grayRange.range.GetEnd());
  684. CDoubleRangeClr diaRange = new CDoubleRangeClr(grayRange.diameterRange.GetStart(), grayRange.diameterRange.GetEnd());
  685. curFldData.GetPartsBySpecialGray(range, diaRange, pixelsize, grayRange.ifCollectXray);
  686. }
  687. }
  688. var ifCollectxray = pMsrParam.GetXRayParam().GetUsingXray();
  689. // remove BES image background
  690. curFldData.RemoveImgBGAndGetParticles(pImgProcessParam, pixelsize, ifCollectxray);
  691. // check if this is an empty image
  692. if (curFldData.NoParticle())
  693. { // empty fields
  694. log.Info("ImageProcess: empty field.");
  695. }
  696. return;
  697. }
  698. public void FilterParticles(ref COTSFieldData curFldData)
  699. {
  700. if (curFldData.NoAnalysisParticle())
  701. {
  702. log.Warn("There's no analysis particles!");
  703. return;
  704. }
  705. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  706. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  707. double dPixelSize = m_Sample.CalculatePixelSize();
  708. curFldData.InitParticles(pImgProcessParam, dPixelSize);
  709. var listXray = curFldData.GetAllParticles().OrderByDescending(x => x.GetActualArea()).ToList();
  710. var listXray1 = new List<COTSParticleClr>();
  711. var pXRayParam = pMsrParam.GetXRayParam();
  712. if (listXray.Count > pXRayParam.GetXrayLimit())
  713. {
  714. for (var i = 0; i < pXRayParam.GetXrayLimit(); i++)
  715. {
  716. listXray1.Add(listXray[i]);
  717. }
  718. }
  719. else
  720. {
  721. listXray1 = listXray;
  722. }
  723. curFldData.SetListAnalysisParticles(listXray1);
  724. log.Info("Analysis particles:"+ listXray1.Count);
  725. //2) according to the quantify threshold size value separate the analysis particles into two group :the bigparticles and the smallparticles.
  726. double quantifyThreshold = m_Sample.GetMsrParams().GetXRayParam().GetFeatureModeMinSize();
  727. var smallparts = curFldData.ListSmallParticles;
  728. var bigparts = curFldData.ListBigParticles;
  729. foreach (var part in listXray1)
  730. {
  731. double equalCircleDiameter = Math.Sqrt(part.GetActualArea() / 3.14159) * 2f;
  732. if (equalCircleDiameter < quantifyThreshold)
  733. {
  734. smallparts.Add(part);
  735. }
  736. else
  737. {
  738. bigparts.Add(part);
  739. }
  740. }
  741. curFldData.ListSmallParticles = smallparts;
  742. curFldData.ListBigParticles = bigparts;
  743. //fld.SmallParticlePercentage=percentage;
  744. log.Info("SmallQuantifyParts (<" + quantifyThreshold.ToString("f2") + "): " + smallparts.Count);
  745. log.Info("BigQuantifyParts (>=" + quantifyThreshold.ToString("f2") + "): " + bigparts.Count);
  746. return;
  747. }
  748. public virtual void CollectParticlesXrayData(COTSFieldData curFldData)
  749. {
  750. // get x-ray parameters
  751. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  752. // calculate search x-ray acquire time
  753. uint nXRayAQTime;
  754. List<COTSParticleClr> smallparts = new List<COTSParticleClr>();
  755. List<COTSParticleClr> bigparts = new List<COTSParticleClr>();
  756. foreach (var p in curFldData.ListSmallParticles)
  757. {
  758. if (p.IsXrayParticle())
  759. {
  760. smallparts.Add(p);
  761. }
  762. }
  763. foreach (var p in curFldData.ListBigParticles)
  764. {
  765. if (p.IsXrayParticle())
  766. {
  767. bigparts.Add(p);
  768. }
  769. }
  770. curFldData.CreateXrayList(bigparts); // big particle using the full xray strategy.
  771. curFldData.CreateXrayList(smallparts); //small particle using the fast xray strategy
  772. if (bigparts.Count > 0)
  773. {
  774. var workmode = pXRayParam.GetScanMode();
  775. // get x-ray list (analysis) by particle features
  776. if (workmode == OTS_X_RAY_SCAN_MODE.FeatureMode)
  777. {
  778. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  779. m_EDSHardwareMgr.GetXRayByFeatures(bigparts, nXRayAQTime, true);
  780. }
  781. else if(workmode==OTS_X_RAY_SCAN_MODE.PointMode)
  782. {
  783. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  784. m_EDSHardwareMgr.GetXRayByParts(bigparts, nXRayAQTime, true);
  785. }
  786. }
  787. if (smallparts.Count > 0)
  788. {
  789. nXRayAQTime = (uint)pXRayParam.GetFastXrayTime();
  790. // get x-ray list (analysis) by points
  791. m_EDSHardwareMgr.GetXRayByParts(smallparts, nXRayAQTime, true);
  792. }
  793. return;
  794. }
  795. public void DoHolePreview()
  796. {
  797. using (AutoResetSEMControl autoReset = new AutoResetSEMControl(this))
  798. {
  799. //----------memorize the state of sem
  800. var semstate = new SEMStateObject();
  801. double dMagnification = m_Sample.GetSEMDataMsr().GetMagnification();
  802. double wd = m_Sample.GetSEMDataMsr().GetWorkingDistance();
  803. ISemController sem = m_SemHardwareMgr;
  804. double posX=0, posY=0,posR=0;
  805. sem.GetSemPositionXY(ref posX, ref posY, ref posR);
  806. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  807. Point pos = new Point((int)posX, (int)posY);
  808. PointF otsPos = new Point(0, 0);
  809. if (!a_pCSEMStageData.ConvertSEMToOTSCoord(pos, ref otsPos))
  810. {
  811. return;
  812. }
  813. semstate.Pos = otsPos;
  814. semstate.Magnification = dMagnification;
  815. semstate.WorkingDistance = wd;
  816. autoReset.SemState = semstate;
  817. //-------------------------------------
  818. // let the main thread to know that this sample measurement starts
  819. CMsrSampleStatus pStatus = m_HolePreviewSample.GetMsrStatus();
  820. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  821. // set current time to current time
  822. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.START);
  823. // let main App know that the sample begin to measure
  824. ST_MSTMsg MsgSmpStart=new ST_MSTMsg();
  825. MsgSmpStart.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  826. MsgSmpStart.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.INPROCESS;
  827. MsgSmpStart.STMSampleStu.cSampleName=m_Sample.GetName();
  828. // get SEM controller to set magnification and working distance
  829. if (!SetSEMDataMrs(m_HolePreviewSample))
  830. {
  831. log.Error("DoHolePreview: fail to set SEM data.");
  832. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  833. // record end time
  834. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  835. return;
  836. }
  837. // set the BSE scan param
  838. if (!SetHoleBSEParam(m_HolePreviewSample))
  839. {
  840. log.Error("DoHolePreview: fail to set BSE param.");
  841. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  842. // record end time
  843. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  844. return;
  845. }
  846. // calculate field centers
  847. List<System.Drawing.Point> listFieldCenter;
  848. List<System.Drawing.Point> alllistFieldCenter ;
  849. if (!CalculateUnMeasuredFieldsCenters(m_HolePreviewSample, out alllistFieldCenter,out listFieldCenter))
  850. {// failed to calculate field centers
  851. log.Error("DoHolePreview: failed to calculate field centers.");
  852. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  853. // record end time
  854. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  855. return;
  856. }
  857. // go through each field
  858. int nNewFieldId = 0;
  859. for ( int i = 0; i < listFieldCenter.Count; ++i)
  860. {// check and break if stop button is clicked
  861. if (IsAborted())
  862. {// measure stopped
  863. log.Trace("DoHolePreview: measure thread is stopped.");
  864. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  865. // record end time
  866. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  867. return;
  868. }
  869. // check if sample measurement completes
  870. COTSImgScanPrm pScanParam = m_Sample.GetMsrParams().GetImageScanParam();
  871. int nTotalFieldSize = listFieldCenter.Count;
  872. // get a field center
  873. System.Drawing.Point poiFieldCentre = listFieldCenter[i];
  874. // update thread measure status class, let the main thread know that starts a new field
  875. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  876. ST_MSTMsg MsgFieldStart=new ST_MSTMsg();
  877. MsgFieldStart.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  878. MsgFieldStart.STMSampleRetData.iRetDataType = MSAMPLE_RET.START_MSR_FIELD;
  879. MsgFieldStart.STMSampleRetData.SMsrField.FieldPos = poiFieldCentre;
  880. m_pMsrThread.SendMessageToMeasureApp(MsgFieldStart);
  881. // move SEM to the field center
  882. if (!MoveSEMToPoint(poiFieldCentre))
  883. {// failed to move SEM to the position
  884. log.Error("DoHolePreview: failed to move SEM to the field centre point.");
  885. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  886. // record end time
  887. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  888. return;
  889. }
  890. // take BSE image for the fields
  891. CBSEImgClr pBSEIamge = AcquireABSEImage();
  892. if (pBSEIamge==null)
  893. {
  894. // failed to acquire a BSE image
  895. log.Error("DoHolePreview: failed to acquire a BSE image.");
  896. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  897. // record end time
  898. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  899. return;
  900. }
  901. //BSEData
  902. ST_MSTMsg MsgFieldBSE=new ST_MSTMsg();
  903. MsgFieldBSE.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  904. MsgFieldBSE.STMSampleRetData.iRetDataType = MSAMPLE_RET.BSE_DATA;
  905. MsgFieldBSE.STMSampleRetData.BSEData.pos = poiFieldCentre;
  906. MsgFieldBSE.STMSampleRetData.BSEData.fieldId = i;
  907. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataHeight = pBSEIamge.GetHeight();
  908. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataWidth = pBSEIamge.GetWidth();
  909. byte[] pImgData = pBSEIamge.GetImageDataPtr();
  910. MsgFieldBSE.STMSampleRetData.BSEData.lpBSEData = pImgData;
  911. m_pMsrThread.SendMessageToMeasureApp(MsgFieldBSE);
  912. if (pStatus.GetStatus() != OTS_MSR_SAMPLE_STATUS.INPROCESS)
  913. {
  914. // measurement failed or stopped
  915. log.Error("DoHolePreview: measurement failed or stopped.");
  916. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  917. // record end time
  918. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  919. return;
  920. }
  921. //save the result to project file
  922. Rectangle oImageRect = (Rectangle)pBSEIamge.GetImageRect();
  923. CHoleBSEImg pHoleBSEImg =new CHoleBSEImg(oImageRect,poiFieldCentre);
  924. pHoleBSEImg.SetImageData( pBSEIamge.GetImageDataPtr(),oImageRect.Width,oImageRect.Height);
  925. m_listHoleBSEImg.Add(pHoleBSEImg);
  926. // prepare for the next
  927. ++nNewFieldId;
  928. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  929. ST_MSTMsg MsgFieldEnd=new ST_MSTMsg();
  930. MsgFieldEnd.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  931. MsgFieldEnd.STMSampleRetData.iRetDataType = MSAMPLE_RET.FIELD_DATA;
  932. MsgFieldEnd.STMSampleRetData.SFieldData.iCompleteFieldCount = (i + 1);
  933. MsgFieldEnd.STMSampleRetData.SFieldData.iMeasureFieldCount = listFieldCenter.Count;
  934. MsgFieldEnd.STMSampleRetData.SFieldData.iSParticleCount = 0;
  935. MsgFieldEnd.STMSampleRetData.SFieldData.FieldPos = poiFieldCentre;
  936. m_pMsrThread.SendMessageToMeasureApp(MsgFieldEnd);
  937. }
  938. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  939. //calculate measure time
  940. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  941. // let main thread to know that this sample measurement completes
  942. ST_MSTMsg MsgSmplEnd=new ST_MSTMsg();
  943. MsgSmplEnd.iMsgType = ENUM_MSG_TYPE.MTHREADSTATUS;
  944. MsgSmplEnd.STMThreadStu.iMsrStatu = OTS_MSR_THREAD_STATUS.COMPLETED;
  945. m_pMsrThread.SendMessageToMeasureApp(MsgSmplEnd);
  946. }
  947. }
  948. // Cumulate field data info
  949. public virtual bool CumulateFieldData( List<COTSParticleClr> listParticles, double a_nMeasuredArea)
  950. {
  951. // get measure result items of the sample
  952. CMsrResultItems pMsrResults = m_Sample.GetMsrResults();
  953. // cumulate field data info
  954. pMsrResults.CumulateMeasuredArea(a_nMeasuredArea);
  955. // cumulate ratio
  956. double dRatio = 10000 * pMsrResults.GetTotalParticleArea();
  957. dRatio = dRatio / pMsrResults.GetMeasuredArea();
  958. pMsrResults.SetRatio(dRatio);
  959. // go through the particles list
  960. foreach (COTSParticleClr pParticle in listParticles)
  961. {
  962. // create a measure result item
  963. pMsrResults.CumulateMeasureResult(pParticle);
  964. }
  965. // ok, return TRUE
  966. return true;
  967. }
  968. public bool SaveMergedParticles(List<COTSParticleClr> mergedParts)
  969. {
  970. CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
  971. var mergedpartdb = pDBFileMgr.GetMergedParticleDB();
  972. foreach (COTSParticleClr part in mergedParts)
  973. {
  974. mergedpartdb.SaveAParticle(part, part.GetXray(), (Point)part.GetAbsolutPos());
  975. }
  976. CPosXrayDBMgr pXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
  977. CElementChemistryDB xraydb = pXrayDBMgr.GetElementChemistryDB();
  978. List<CPosXrayClr> ches = new List<CPosXrayClr>();
  979. foreach (COTSParticleClr part in mergedParts)
  980. {
  981. ches.Add(part.GetXray());
  982. }
  983. xraydb.SaveElementChemistriesList(ches);
  984. return true;
  985. }
  986. public bool SaveFieldData(COTSFieldData fldData, string filedFileFoler)
  987. {
  988. string strFieldFileFolder = filedFileFoler;
  989. CBSEImageFileMgr pBSEImgFileMgr = new CBSEImageFileMgr();
  990. pBSEImgFileMgr.SetBSEImg(fldData.GetBSEImage());
  991. int nId = fldData.GetId();
  992. string sFieldId;
  993. sFieldId = nId.ToString();
  994. string strBSEFilePathname = strFieldFileFolder + "\\" + m_pSampleRstFile.SMPL_MSR_RESULT_FIELDS_BSE + sFieldId + pBSEImgFileMgr.BMP_IMG_FILE_EXT;
  995. if (!pBSEImgFileMgr.SaveIntoBitmap(strBSEFilePathname))
  996. {
  997. log.Error("SaveFieldFiles: save BSE file failed.");
  998. return false;
  999. }
  1000. // IncA Data list
  1001. CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
  1002. pDBFileMgr.SaveStatusDataToDB();
  1003. var fldDB = pDBFileMgr.GetFieldDB();
  1004. log.Warn("Start saving particle data.");
  1005. var fldcmd = fldDB.GetSavingAFieldcmdObj(fldData.GetId(), fldData.GetOTSPosition(), fldData.GetSemPos());
  1006. List<KeyValuePair<string, System.Data.SQLite.SQLiteParameter[]>> fldcmds = new List<KeyValuePair<string, System.Data.SQLite.SQLiteParameter[]>>();
  1007. fldcmds.Add(fldcmd);
  1008. pDBFileMgr.ExecuteNonQueryBatch(ref fldcmds);
  1009. //remomove the invalid particles
  1010. var cmds = pDBFileMgr.GetSavingParticleDataToDBCmds(fldData.GetListAnalysisParticles(), fldData.GetOTSPosition());
  1011. pDBFileMgr.ExecuteNonQueryBatch(ref cmds);
  1012. CPosXrayDBMgr PosXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
  1013. var listAnalysisPosXray = new List<CPosXrayClr>();
  1014. foreach (var p in fldData.GetListXrayParticles())
  1015. {
  1016. listAnalysisPosXray.Add(p.GetXray());
  1017. }
  1018. var cmds1 = PosXrayDBMgr.GetSavingXrayCmds(listAnalysisPosXray, true);
  1019. pDBFileMgr.ExecuteNonQueryBatch(ref cmds1);
  1020. return true;
  1021. }
  1022. protected void SaveFieldMgrData()
  1023. {
  1024. while (bSaveThreadWorking)
  1025. {
  1026. while (fieldQueue.Count() > 0)
  1027. {
  1028. COTSFieldData f = fieldQueue.Dequeue();
  1029. //save to disk first ,then pop . if the size is 0,then we know all the saving work is done.
  1030. SaveFieldData(f, m_pSampleRstFile.GetFieldFileSubFolderStr());
  1031. }
  1032. if (fieldQueue.Count() == 0)
  1033. {
  1034. bSaveThreadWorking = false; //must set this flag,so the main thread can know this thread has exited.
  1035. log.Warn("finished batch saving");
  1036. return;
  1037. }
  1038. }
  1039. return;
  1040. }
  1041. protected void StartSaveFileThread(ref COTSFieldData a_pFieldMgr)
  1042. {
  1043. fieldQueue.Enqueue(a_pFieldMgr);
  1044. if (fieldQueue.Count() > 0) //if there's data in the queue and the previous thread has finished then start a new thread.
  1045. {
  1046. if (bSaveThreadWorking == false)
  1047. {
  1048. bSaveThreadWorking = true;
  1049. m_thread_ptr = new System.Threading.Thread(this.SaveFieldMgrData);//m_thread_ptr = shared_ptr<thread>(new thread(&CSmplMeasureInc::SaveFieldMgrData, this));
  1050. m_thread_ptr.IsBackground = true;
  1051. m_thread_ptr.Start();
  1052. }
  1053. }
  1054. }
  1055. public void ParticleSpecialTreatment(ref COTSFieldData fld)
  1056. {
  1057. //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.
  1058. Dictionary<COTSParticleClr, int> mapPartXray = new Dictionary<COTSParticleClr, int>();
  1059. double edsTime = 0; //to store the EDSTime returned from the engine.
  1060. var bigparts = fld.GetListXrayParticles();
  1061. var m_ClassifyEngine = new CClassifyEngine();
  1062. for (int i = 0; i < bigparts.Count(); i++)
  1063. {
  1064. var engine = m_ClassifyEngine.GetParticleEngine(m_Sample.GetMsrParams().GetSTDName());
  1065. var p = bigparts[i];
  1066. // 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.
  1067. //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.
  1068. edsTime = engine.IfNeedMaxEDS(p);
  1069. if (edsTime > 0)
  1070. {
  1071. mapPartXray[p] = i;
  1072. }
  1073. }
  1074. List<COTSParticleClr> partsMax = new List<COTSParticleClr>();
  1075. if (mapPartXray.Count() > 0)
  1076. {
  1077. foreach (var p in mapPartXray)
  1078. {
  1079. partsMax.Add(p.Key);
  1080. }
  1081. List<CPosXrayClr> maxEDSXrays = new List<CPosXrayClr>();
  1082. m_EDSHardwareMgr.GetXRayByFeatures(partsMax, edsTime, true);
  1083. }
  1084. }
  1085. public void CalculateParticleAbsolutPos(ref COTSFieldData curFldData)
  1086. {
  1087. double dPixelSize = m_Sample.CalculatePixelSize();
  1088. CSEMStageData pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  1089. foreach (var p in curFldData.GetListAnalysisParticles())
  1090. {
  1091. //Point fldOtsPos = new Point((curFldData.OTSPos.X, curFldData.OTSPos.Y);
  1092. PointF semP = new PointF(); ;
  1093. Point semPos = new Point();
  1094. pCSEMStageData.ConvertOTSToSEMCoord(curFldData.OTSPos, ref semP);
  1095. semPos.X = (int)semP.X;
  1096. semPos.Y = (int)semP.Y;
  1097. p.SetAbsolutPos(semPos);
  1098. }
  1099. }
  1100. }
  1101. }