SmplMeasureCleanliness.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.SQLite;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using OTSCLRINTERFACE;
  10. using OTSDataType;
  11. using OTSModelSharp.Measure.OTSCleanliness;
  12. using OTSModelSharp.ServiceCenter;
  13. using OTSModelSharp.ServiceInterface;
  14. using static OTSDataType.otsdataconst;
  15. namespace OTSModelSharp
  16. {
  17. class CSmplMeasureCleanliness : CSmplMeasure
  18. {
  19. public CSmplMeasureCleanliness(string a_strWorkingFolder, COTSSample a_pSample):base(a_strWorkingFolder,a_pSample)
  20. {
  21. SetWorkingFolder(a_strWorkingFolder);
  22. SetSample(a_pSample);
  23. m_classifyEngine = new CClassifyEngine();
  24. }
  25. // field image process
  26. public override bool FieldImageProcess(Point fldCenter, CBSEImgClr a_BSEImg)
  27. {
  28. int nNewFieldId;
  29. nNewFieldId = m_pSampleRstFile.GetIdForANewField();
  30. //first step:remove background of the bse image and compound all the finded particles.
  31. curFldData = new CFieldDataClean( a_BSEImg,m_Sample.CalculatePixelSize());
  32. CFieldDataClean curFldDataCln =( CFieldDataClean) curFldData;
  33. curFldData.SetId(nNewFieldId);
  34. curFldData.SetOTSPosition(fldCenter);
  35. GetOriginalParticles();
  36. // second step :filter the finded particles.
  37. FilterParticles((CFieldDataClean)curFldData);
  38. CalculateParticlePos();
  39. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  40. //collect xray data.
  41. if (pXRayParam.GetUsingXray() == true)
  42. {
  43. Thread.Sleep(100);
  44. CollectParticlesXrayData((CFieldDataClean)curFldData);
  45. Thread.Sleep(100);
  46. }
  47. log.Info("Begin to Calculate the image property of every particle!");
  48. var analysisparts = curFldDataCln.ListBigParticles;
  49. curFldData.CalParticleImageProp(analysisparts);//calculate particle image property such as feret diameter, DMAX etc.
  50. ClassifyFieldParticles();
  51. // save field files
  52. m_Sample.GetMsrStatus() .SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  53. StartSaveFileThread(curFldData);
  54. return true;
  55. }
  56. public void CalculateParticlePos()
  57. {
  58. double dPixelSize = m_Sample.CalculatePixelSize();
  59. CSEMStageData pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  60. foreach (var p in curFldData.GetListAnalysisParticles())
  61. {
  62. Point fldOtsPos = new Point(curFldData.OTSPos.X, curFldData.OTSPos.Y);
  63. Point semPos = new Point();
  64. pCSEMStageData.ConverOTSToSEMPoint(fldOtsPos, ref semPos);
  65. p.SetAbsolutPos(semPos);
  66. }
  67. }
  68. // save field data
  69. protected void SaveFieldMgrData()
  70. {
  71. while (bSaveThreadWorking)
  72. {
  73. while (fieldQueue.Count() > 0)
  74. {
  75. CFieldDataClean f = (CFieldDataClean) fieldQueue.Dequeue();
  76. double pixelSize = m_Sample.CalculatePixelSize();
  77. //save to disk first ,then pop . if the size is 0,then we know all the saving work is done.
  78. SaveFieldFiles(f,m_pSampleRstFile.GetFieldFileSubFolderStr());
  79. }
  80. if (fieldQueue.Count() == 0)
  81. {
  82. bSaveThreadWorking = false; //must set this flag,so the main thread can know this thread has exited.
  83. return;
  84. }
  85. }
  86. return;
  87. }
  88. protected void StartSaveFileThread(COTSFieldData a_pFieldMgr)
  89. {
  90. fieldQueue.Enqueue(a_pFieldMgr);
  91. if (fieldQueue.Count() > 0) //if there's data in the queue and the previous thread has finished then start a new thread.
  92. {
  93. if (bSaveThreadWorking == false)
  94. {
  95. bSaveThreadWorking = true;
  96. m_thread_ptr = new System.Threading.Thread(this.SaveFieldMgrData);//m_thread_ptr = shared_ptr<thread>(new thread(&CSmplMeasureInc::SaveFieldMgrData, this));
  97. m_thread_ptr.Start();//m_thread_ptr->detach();
  98. }
  99. }
  100. }
  101. public void FilterParticles(CFieldDataClean fld)
  102. {
  103. // 1)remove over sized particles, too small particles and get a analysis particles list
  104. double dPixelSize = m_Sample.CalculatePixelSize();
  105. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  106. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  107. curFldData.InitParticles(pImgProcessParam, dPixelSize);
  108. if (curFldData.NoAnalysisParticle())
  109. {
  110. log.Warn("There's no analysis particles!");
  111. }
  112. //2) according to the quantify threshold size value saperate the analysis particles into two group :the bigparticles and the smallparticles.
  113. int quantifyThreshold = m_Sample.GetMsrParams().GetXRayParam().GetQuantifyMinSize();
  114. var smallparts = fld.ListSmallParticles;
  115. var bigparts = fld.ListBigParticles;
  116. foreach (var part in curFldData.GetListAnalysisParticles())
  117. {
  118. double equalCircleDiameter = Math.Sqrt(part.GetActualArea() / 3.14159) * 2f;
  119. if (equalCircleDiameter < quantifyThreshold)
  120. {
  121. smallparts.Add(part);
  122. }
  123. else
  124. {
  125. bigparts.Add(part);
  126. }
  127. }
  128. fld.ListSmallParticles = smallparts;
  129. fld.ListBigParticles = bigparts;
  130. //fld.SmallParticlePercentage=percentage;
  131. log.Info("SmallQuantifyParts (<" + quantifyThreshold.ToString("f2") + "): " + smallparts.Count);
  132. log.Info("BigQuantifyParts (>=" + quantifyThreshold.ToString("f2")+ "): " + bigparts.Count);
  133. return ;
  134. }
  135. public void CollectParticlesXrayData(CFieldDataClean fld)
  136. {
  137. // get x-ray parameters
  138. COTSXRayParam pXRayParam = m_Sample.GetMsrParams ().GetXRayParam();
  139. // calculate search x-ray acquire time
  140. uint nXRayAQTime;
  141. var smallparts = fld.ListSmallParticles;
  142. var bigparts = fld.ListBigParticles;
  143. var pStatus = m_Sample.GetMsrStatus();
  144. //// particle x-ray analysis
  145. ////=============================================
  146. curFldData.CreateXrayList(bigparts); // big particle using the full xray strategy.
  147. curFldData.CreateXrayList(smallparts); //small particle using the fast xray strategy
  148. // get x-ray list (analysis) by particle features
  149. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  150. m_EDSHardwareMgr.GetXRayByFeatures(bigparts, nXRayAQTime, true);
  151. nXRayAQTime = (uint)pXRayParam.GetFastXrayTime();
  152. // get x-ray list (analysis) by points
  153. m_EDSHardwareMgr.GetXRayByPoints(smallparts, nXRayAQTime, true);
  154. return ;
  155. }
  156. public void ParticleSpecialTreatment(CFieldDataClean fld)
  157. {
  158. //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.
  159. Dictionary<COTSParticleClr, int> mapPartXray = new Dictionary<COTSParticleClr, int>(); //std.map<COTSParticlePtr, int> mapPartXray = new std.map<COTSParticlePtr, int>();
  160. double edsTime = 0; //to store the EDSTime returned from the engine.
  161. var bigparts = fld.ListBigParticles;
  162. var m_ClassifyEngine = new CClassifyEngine();
  163. for (int i = 0; i < bigparts.Count(); i++)
  164. {
  165. var engine = m_ClassifyEngine.GetParticleEngine(m_Sample.GetMsrParams().GetSTDName());
  166. var p = bigparts[i];
  167. // 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.
  168. //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.
  169. edsTime = engine.IfNeedMaxEDS(p);
  170. if (edsTime > 0)
  171. {
  172. mapPartXray[p] = i;
  173. }
  174. }
  175. List<COTSParticleClr> partsMax = new List<COTSParticleClr>();
  176. if (mapPartXray.Count() > 0)
  177. {
  178. foreach (var p in mapPartXray)
  179. {
  180. partsMax.Add(p.Key);
  181. }
  182. List<CPosXrayClr> maxEDSXrays = new List<CPosXrayClr>();
  183. m_EDSHardwareMgr.GetXRayByFeatures(partsMax, edsTime, true);
  184. }
  185. }
  186. public override void ClassifyFieldParticles()
  187. {
  188. try
  189. {
  190. CFieldDataClean curFldDataCln = (CFieldDataClean)curFldData;
  191. string libname = m_Sample.GetMsrParams().GetSTDName();
  192. if (libname != "NoSTDDB")
  193. {
  194. log.Info("Begin to classify big particles!Using " + libname);
  195. var bigparts = curFldDataCln.ListBigParticles;
  196. ClassifyQuantifyParticles(bigparts,libname);
  197. var smallParts = curFldDataCln.ListSmallParticles;
  198. ClassifySmallParticles(smallParts, libname);
  199. }
  200. }
  201. catch (Exception e)
  202. {
  203. log.Info("calcu the particle image property or classify failed. "+e.Message);
  204. }
  205. }
  206. public override void ClassifyMergedParticles(List<COTSParticleClr> mergedParts)
  207. {
  208. try
  209. {
  210. try
  211. {
  212. string libname = m_Sample.GetMsrParams().GetSTDName();
  213. if (libname != "NoSTDDB")
  214. {
  215. log.Info("Begin to classify big particles!Using " + libname);
  216. var bigparts = mergedParts;
  217. ClassifyQuantifyParticles(bigparts, libname);
  218. }
  219. }
  220. catch (Exception e)
  221. {
  222. log.Info("calcu the particle image property or classify failed. " + e.Message);
  223. }
  224. }
  225. catch (Exception e)
  226. {
  227. log.Info(" classify failed. " + e.Message);
  228. }
  229. }
  230. public void SaveFieldParticlesData()
  231. {
  232. StartSaveFileThread(curFldData);
  233. }
  234. public void GetOriginalParticles()
  235. {
  236. // measure status
  237. CMsrSampleStatus pStatus = m_Sample.GetMsrStatus();
  238. // get image process parameter
  239. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  240. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  241. var dPixelsize = m_Sample.CalculatePixelSize();
  242. // remove BES image background
  243. curFldData.RemoveBSEImageBG(pImgProcessParam, dPixelsize);
  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.GetOTSPosition(),fldData.SemPos);
  309. if (!pDBFileMgr.SaveIncADataToDB(fldData.GetListAnalysisParticles(), fldData.GetOTSPosition()))
  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.GetListAnalysisParticles())
  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.GetActualArea();
  356. }
  357. //partInfo.Area = Convert.ToInt32(area / f.SmallParticlePercentage);
  358. partInfo.Area =(int) area ;
  359. smallparts.Add(partInfo);
  360. }
  361. List<KeyValuePair<string, SQLiteParameter[]>> cmds = new List<KeyValuePair<string, SQLiteParameter[]>>();
  362. foreach (CSmallParticleInfo smallp in smallparts)
  363. {
  364. var cmd= smallPartDB.SaveAKindOfSmallParticle(smallp, new CPosXrayClr(), new System.Drawing.Point(0, 0));
  365. cmds.Add(cmd);
  366. }
  367. pDBFileMgr.ExecuteNonQueryBatch(ref cmds);
  368. return true;
  369. }
  370. }
  371. }