SmplMeasureCleanliness.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using OTSCLRINTERFACE;
  9. using OTSDataType;
  10. using OTSModelSharp.Measure.OTSCleanliness;
  11. using OTSModelSharp.ServiceCenter;
  12. using OTSModelSharp.ServiceInterface;
  13. using static OTSDataType.otsdataconst;
  14. namespace OTSModelSharp
  15. {
  16. class CSmplMeasureCleanliness : CSmplMeasure
  17. {
  18. public CSmplMeasureCleanliness(string a_strWorkingFolder, COTSSample a_pSample):base(a_strWorkingFolder,a_pSample)
  19. {
  20. SetWorkingFolder(a_strWorkingFolder);
  21. SetSample(a_pSample);
  22. m_classifyEngine = new CClassifyEngine();
  23. }
  24. // field image process
  25. public override bool FieldImageProcess(Point fldCenter, CBSEImgClr a_BSEImg)
  26. {
  27. int nNewFieldId;
  28. nNewFieldId = m_pSampleRstFile.GetIdForANewField();
  29. //first step:remove background of the bse image and compound all the finded particles.
  30. curFldData = new CFieldDataClean( a_BSEImg,m_Sample.CalculatePixelSize());
  31. CFieldDataClean curFldDataCln =( CFieldDataClean) curFldData;
  32. curFldData.SetId(nNewFieldId);
  33. curFldData.SetPosition(fldCenter);
  34. GetOriginalParticles();
  35. // second step :filter the finded particles.
  36. FilterParticles((CFieldDataClean)curFldData);
  37. CalculateParticlePos();
  38. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  39. //collect xray data.
  40. if (pXRayParam.GetUsingXray() == true)
  41. {
  42. Thread.Sleep(100);
  43. CollectParticlesXrayData((CFieldDataClean)curFldData);
  44. Thread.Sleep(100);
  45. }
  46. //special treatment.
  47. //ParticleSpecialTreatment();
  48. log.Info("Begin to Calculate the image property of every particle!");
  49. var analysisparts = curFldDataCln.ListBigParticles;
  50. curFldData.CalParticleImageProp(analysisparts);//calculate particle image property such as feret diameter, DMAX etc.
  51. ClassifyFieldParticles();
  52. // save field files
  53. m_Sample.GetMsrStatus() .SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  54. StartSaveFileThread(curFldData);
  55. return true;
  56. }
  57. public void CalculateParticlePos()
  58. {
  59. double dPixelSize = m_Sample.CalculatePixelSize();
  60. CSEMStageData pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  61. foreach (var p in curFldData.ListAnalysisParticles)
  62. {
  63. Point fldOtsPos = new Point(curFldData.PoiPos.X, curFldData.PoiPos.Y);
  64. Point semPos = new Point();
  65. pCSEMStageData.ConverOTSToSEMPoint(fldOtsPos, ref semPos);
  66. p.SetAbsolutPos(semPos);
  67. }
  68. }
  69. // save field data
  70. protected void SaveFieldMgrData()
  71. {
  72. while (bSaveThreadWorking)
  73. {
  74. while (fieldQueue.Count() > 0)
  75. {
  76. CFieldDataClean f = (CFieldDataClean) fieldQueue.Dequeue();
  77. double pixelSize = m_Sample.CalculatePixelSize();
  78. //save to disk first ,then pop . if the size is 0,then we know all the saving work is done.
  79. SaveFieldFiles(f,m_pSampleRstFile.GetFieldFileSubFolderStr());
  80. }
  81. if (fieldQueue.Count() == 0)
  82. {
  83. bSaveThreadWorking = false; //must set this flag,so the main thread can know this thread has exited.
  84. return;
  85. }
  86. }
  87. return;
  88. }
  89. protected void StartSaveFileThread(COTSFieldData a_pFieldMgr)
  90. {
  91. fieldQueue.Enqueue(a_pFieldMgr);
  92. if (fieldQueue.Count() > 0) //if there's data in the queue and the previous thread has finished then start a new thread.
  93. {
  94. if (bSaveThreadWorking == false)
  95. {
  96. bSaveThreadWorking = true;
  97. m_thread_ptr = new System.Threading.Thread(this.SaveFieldMgrData);//m_thread_ptr = shared_ptr<thread>(new thread(&CSmplMeasureInc::SaveFieldMgrData, this));
  98. m_thread_ptr.Start();//m_thread_ptr->detach();
  99. }
  100. }
  101. }
  102. public void FilterParticles(CFieldDataClean fld)
  103. {
  104. // 1)remove over sized particles, too small particles and get a analysis particles list
  105. double dPixelSize = m_Sample.CalculatePixelSize();
  106. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  107. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  108. curFldData.FilterParticles(pImgProcessParam, dPixelSize);
  109. if (curFldData.NoAnalysisParticle())
  110. {
  111. log.Warn("There's no analysis particles!");
  112. }
  113. //2) according to the quantify threshold size value saperate the analysis particles into two group :the bigparticles and the smallparticles.
  114. int quantifyThreshold = m_Sample.GetMsrParams().GetXRayParam().GetQuantifyMinSize();
  115. var smallparts = fld.ListSmallParticles;
  116. var bigparts = fld.ListBigParticles;
  117. foreach (var part in curFldData.ListAnalysisParticles)
  118. {
  119. double equalCircleDiameter = Math.Sqrt(part.GetArea() / 3.14159) * 2f;
  120. if (equalCircleDiameter < quantifyThreshold)
  121. {
  122. smallparts.Add(part);
  123. }
  124. else
  125. {
  126. bigparts.Add(part);
  127. }
  128. }
  129. fld.ListSmallParticles = smallparts;
  130. fld.ListBigParticles = bigparts;
  131. //fld.SmallParticlePercentage=percentage;
  132. log.Info("SmallQuantifyParts (<" + quantifyThreshold.ToString("f2") + "): " + smallparts.Count);
  133. log.Info("BigQuantifyParts (>=" + quantifyThreshold.ToString("f2")+ "): " + bigparts.Count);
  134. return ;
  135. }
  136. public void CollectParticlesXrayData(CFieldDataClean fld)
  137. {
  138. // get x-ray parameters
  139. COTSXRayParam pXRayParam = m_Sample.GetMsrParams ().GetXRayParam();
  140. // calculate search x-ray acquire time
  141. uint nXRayAQTime;
  142. var smallparts = fld.ListSmallParticles;
  143. var bigparts = fld.ListBigParticles;
  144. var pStatus = m_Sample.GetMsrStatus();
  145. //// particle x-ray analysis
  146. ////=============================================
  147. curFldData.CreateXrayList(bigparts); // big particle using the full xray strategy.
  148. curFldData.CreateXrayList(smallparts); //small particle using the fast xray strategy
  149. // get x-ray list (analysis) by particle features
  150. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  151. m_EDSHardwareMgr.GetXRayByFeatures(bigparts, nXRayAQTime, true);
  152. nXRayAQTime = (uint)pXRayParam.GetFastXrayTime();
  153. // get x-ray list (analysis) by points
  154. m_EDSHardwareMgr.GetXRayByPoints(smallparts, nXRayAQTime, true);
  155. return ;
  156. }
  157. public void ParticleSpecialTreatment(CFieldDataClean fld)
  158. {
  159. //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.
  160. Dictionary<COTSParticleClr, int> mapPartXray = new Dictionary<COTSParticleClr, int>(); //std.map<COTSParticlePtr, int> mapPartXray = new std.map<COTSParticlePtr, int>();
  161. double edsTime = 0; //to store the EDSTime returned from the engine.
  162. var bigparts = fld.ListBigParticles;
  163. var m_ClassifyEngine = new CClassifyEngine();
  164. for (int i = 0; i < bigparts.Count(); i++)
  165. {
  166. var engine = m_ClassifyEngine.GetParticleEngine(m_Sample.GetMsrParams().GetSTDName());
  167. var p = bigparts[i];
  168. // 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.
  169. //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.
  170. edsTime = engine.IfNeedMaxEDS(p);
  171. if (edsTime > 0)
  172. {
  173. mapPartXray[p] = i;
  174. }
  175. }
  176. List<COTSParticleClr> partsMax = new List<COTSParticleClr>();
  177. if (mapPartXray.Count() > 0)
  178. {
  179. foreach (var p in mapPartXray)
  180. {
  181. partsMax.Add(p.Key);
  182. }
  183. List<CPosXrayClr> maxEDSXrays = new List<CPosXrayClr>();
  184. m_EDSHardwareMgr.GetXRayByFeatures(partsMax, edsTime, true);
  185. }
  186. }
  187. public override void ClassifyFieldParticles()
  188. {
  189. try
  190. {
  191. CFieldDataClean curFldDataCln = (CFieldDataClean)curFldData;
  192. string libname = m_Sample.GetMsrParams().GetSTDName();
  193. if (libname != "NoSTDDB")
  194. {
  195. log.Info("Begin to classify big particles!Using " + libname);
  196. var bigparts = curFldDataCln.ListBigParticles;
  197. ClassifyQuantifyParticles(bigparts,libname);
  198. var smallParts = curFldDataCln.ListSmallParticles;
  199. ClassifySmallParticles(smallParts, libname);
  200. }
  201. }
  202. catch (Exception e)
  203. {
  204. log.Info("calcu the particle image property or classify failed. "+e.Message);
  205. }
  206. }
  207. public override void ClassifyMergedParticles(List<COTSParticleClr> mergedParts)
  208. {
  209. try
  210. {
  211. try
  212. {
  213. string libname = m_Sample.GetMsrParams().GetSTDName();
  214. if (libname != "NoSTDDB")
  215. {
  216. log.Info("Begin to classify big particles!Using " + libname);
  217. var bigparts = mergedParts;
  218. ClassifyQuantifyParticles(bigparts, libname);
  219. }
  220. }
  221. catch (Exception e)
  222. {
  223. log.Info("calcu the particle image property or classify failed. " + e.Message);
  224. }
  225. }
  226. catch (Exception e)
  227. {
  228. log.Info(" classify failed. " + e.Message);
  229. }
  230. }
  231. public void SaveFieldParticlesData()
  232. {
  233. StartSaveFileThread(curFldData);
  234. }
  235. public void GetOriginalParticles()
  236. {
  237. // measure status
  238. CMsrSampleStatus pStatus = m_Sample.GetMsrStatus();
  239. // get image process parameter
  240. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  241. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  242. // remove BES image background
  243. curFldData.RemoveBSEImageBG(pImgProcessParam);
  244. // check if this is an empty image
  245. if (curFldData.NoParticle())
  246. { // empty fields
  247. log.Info("ImageProcess: empty field.");
  248. //return false;
  249. }
  250. return ;
  251. }
  252. public bool ClassifyQuantifyParticles(List<COTSParticleClr> a_listAnalysisParticles, string libname)// classify particles
  253. {
  254. int nSize = (int)a_listAnalysisParticles.Count();
  255. // go through all analysis particles
  256. for (int i = 0; i < nSize; ++i)
  257. {
  258. COTSParticleClr pParticle = a_listAnalysisParticles[i];
  259. //CPosXrayClr pXray = a_listAnalysisParticles[i].GetXray();
  260. // CLEEngine engine = GetParticleEngine(libname.GetBuffer());
  261. IClassifyEngine engine = m_classifyEngine.GetParticleEngine(libname);
  262. if (!engine.Classify(pParticle))
  263. {
  264. // invalid x-ray pointer.
  265. log.Info("ClassifyParticle: can't identify the particle as any inclusion.");
  266. return false;
  267. }
  268. }
  269. // ok, return TRUE
  270. return true;
  271. }
  272. public bool ClassifySmallParticles(List<COTSParticleClr> smallparts, string libname)
  273. {
  274. List<COTSParticleClr> a_listAnalysisParticles = smallparts;
  275. int nSize = (int)a_listAnalysisParticles.Count();
  276. // go through all analysis particles
  277. for (int i = 0; i < nSize; ++i)
  278. {
  279. COTSParticleClr pParticle = a_listAnalysisParticles[i];
  280. IClassifyEngine engine = m_classifyEngine.GetCurveCompareEngine(libname);
  281. if (!engine.Classify(pParticle))
  282. {
  283. log.Info("ClassifyParticle: can't identify the particle as any inclusion.");
  284. return false;
  285. }
  286. }
  287. return true;
  288. }
  289. public bool SaveFieldFiles(CFieldDataClean fldData, string filedFileFoler)
  290. {
  291. string strFieldFileFolder = filedFileFoler;
  292. CBSEImageFileMgr pBSEImgFileMgr = new CBSEImageFileMgr();
  293. pBSEImgFileMgr.SetBSEImg(fldData.GetBSEImage());
  294. int nId = fldData.GetId();
  295. string sFieldId;
  296. sFieldId = nId.ToString();
  297. string strBSEFilePathname = strFieldFileFolder + "\\" + m_pSampleRstFile.SMPL_MSR_RESULT_FIELDS_BSE + sFieldId + pBSEImgFileMgr.BMP_IMG_FILE_EXT;
  298. if (!pBSEImgFileMgr.SaveIntoBitmap(strBSEFilePathname))
  299. {
  300. log.Info("SaveFieldFiles: save BSE file failed.");
  301. return false;
  302. }
  303. // IncA Data list
  304. CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
  305. //pDBFileMgr.BeginTransaction();
  306. pDBFileMgr.SaveStatusDataToDB();
  307. var fldDB = pDBFileMgr.GetFieldDB();
  308. fldDB.SaveAField(fldData.GetId(), fldData.GetPosition());
  309. if (!pDBFileMgr.SaveIncADataToDB(fldData.ListAnalysisParticles, fldData.GetPosition()))
  310. {
  311. log.Info("SaveFieldFiles: save inclusion file failed.");
  312. return false;
  313. }
  314. //save the xray data and element data.
  315. CPosXrayDBMgr PosXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
  316. var m_listAnalysisPosXray = new List<CPosXrayClr>();
  317. foreach (var p in fldData. ListAnalysisParticles)
  318. {
  319. m_listAnalysisPosXray.Add(p.GetXray());
  320. }
  321. if (!PosXrayDBMgr.SaveXray(m_listAnalysisPosXray, true))
  322. {
  323. log.Info("SaveFieldFiles: save analysis x-ray failed.");
  324. return false;
  325. }
  326. //save the small particle info
  327. CSmallParticleInfoDB smallPartDB = pDBFileMgr.GetSmallParticleInfoDB();
  328. Dictionary<int, List<COTSParticleClr>> mapSmallParts = new Dictionary<int, List<COTSParticleClr>>();
  329. foreach (COTSParticleClr smallP in fldData.ListSmallParticles)
  330. {
  331. if (mapSmallParts.ContainsKey((int)smallP.GetType()))
  332. {
  333. mapSmallParts[(int)smallP.GetType()].Add(smallP);
  334. }
  335. else
  336. {
  337. var plist = new List<COTSParticleClr>();
  338. plist.Add(smallP);
  339. mapSmallParts.Add((int)smallP.GetType(), plist);
  340. }
  341. }
  342. List<CSmallParticleInfo> smallparts = new List<CSmallParticleInfo>();
  343. foreach (KeyValuePair<int, List<COTSParticleClr>> particles in mapSmallParts)
  344. {
  345. CSmallParticleInfo partInfo = new CSmallParticleInfo();
  346. partInfo.FieldId = particles.Value[0].GetFieldId();
  347. partInfo.AveGray = particles.Value[0].GetAveGray();
  348. partInfo.Quantity = (int)(particles.Value.Count / fldData.SmallParticlePercentage);
  349. partInfo.TypeId = particles.Key;
  350. partInfo.TypeColor = particles.Value[0].GetTypeColor();
  351. partInfo.TypeName = particles.Value[0].GetTypeName();
  352. double area = 0;
  353. foreach (var p in particles.Value)
  354. {
  355. area += p.GetArea();
  356. }
  357. //partInfo.Area = Convert.ToInt32(area / f.SmallParticlePercentage);
  358. partInfo.Area =(int) area ;
  359. smallparts.Add(partInfo);
  360. }
  361. foreach (CSmallParticleInfo smallp in smallparts)
  362. {
  363. smallPartDB.SaveAKindOfSmallParticle(smallp, new CPosXrayClr(), new System.Drawing.Point(0, 0));
  364. }
  365. //pDBFileMgr.CommitTransaction();
  366. return true;
  367. }
  368. }
  369. }