CSmplMeasure.cs 51 KB

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