CSmplMeasure.cs 57 KB

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