CSmplMeasure.cs 57 KB

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