SmplMeasureInclution.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 OTSCOMMONCLR;
  9. using OTSDataType;
  10. using OTSModelSharp.Measure.OTSInclution;
  11. using OTSModelSharp.ServiceCenter;
  12. using OTSModelSharp.ServiceInterface;
  13. using static OTSDataType.otsdataconst;
  14. namespace OTSModelSharp
  15. {
  16. class CSmplMeasureInclution : CSmplMeasure
  17. {
  18. public CSmplMeasureInclution(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_pBSEImg)
  26. {
  27. int nNewFieldId;
  28. nNewFieldId = m_pSampleRstFile.GetIdForANewField();
  29. // create a field
  30. curFldData = new CFieldDataIncA(a_pBSEImg, m_Sample.CalculatePixelSize());
  31. curFldData.SetId(nNewFieldId);
  32. curFldData.SetPosition(fldCenter);
  33. //first step:remove background of the bse image and compound all the finded particles.
  34. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  35. log.Info("Begin to process image and get all particles!");
  36. GetOriginalParticles();
  37. // second step :filter the finded particles.
  38. log.Info("Begin to filter particles!");
  39. FilterParticles();
  40. CalculateParticlePos();
  41. if (pXRayParam.GetUsingXray() == (int)OTS_USING_X_RAY.Yes)
  42. {
  43. Thread.Sleep(100);
  44. CollectParticlesXrayData();
  45. Thread.Sleep(100);
  46. }
  47. log.Info("Begin to Calculate the image property of every particle!");
  48. var analysisparts = curFldData.ListAnalysisParticles;
  49. curFldData.CalParticleImageProp(analysisparts);//calculate particle image property such as feret diameter, DMAX etc.
  50. log.Info("Begin to classify particles! particle num:"+ curFldData.ListAnalysisParticles.Count);
  51. ClassifyParticles(curFldData.ListAnalysisParticles);
  52. // save field files
  53. m_Sample.GetMsrStatus().SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  54. StartSaveFileThread(curFldData);
  55. return true; ;
  56. }
  57. // save field data
  58. protected void SaveFieldMgrData()
  59. {
  60. while (bSaveThreadWorking)
  61. {
  62. while (fieldQueue.Count() > 0)
  63. {
  64. COTSFieldData f = fieldQueue.Dequeue();
  65. //save to disk first ,then pop . if the size is 0,then we know all the saving work is done.
  66. SaveFieldFiles(f,m_pSampleRstFile.GetFieldFileSubFolderStr());
  67. }
  68. if (fieldQueue.Count() == 0)
  69. {
  70. bSaveThreadWorking = false; //must set this flag,so the main thread can know this thread has exited.
  71. return;
  72. }
  73. }
  74. return;
  75. }
  76. protected void StartSaveFileThread(COTSFieldData a_pFieldMgr)
  77. {
  78. fieldQueue.Enqueue(a_pFieldMgr);
  79. if (fieldQueue.Count() > 0) //if there's data in the queue and the previous thread has finished then start a new thread.
  80. {
  81. if (bSaveThreadWorking == false)
  82. {
  83. bSaveThreadWorking = true;
  84. m_thread_ptr = new System.Threading.Thread(this.SaveFieldMgrData);//m_thread_ptr = shared_ptr<thread>(new thread(&CSmplMeasureInc::SaveFieldMgrData, this));
  85. m_thread_ptr.Start();//m_thread_ptr->detach();
  86. }
  87. }
  88. }
  89. public void FilterParticles()
  90. {
  91. // remove over sized particles, too small particles and get a analysis particles list
  92. // get pixel size
  93. double dPixelSize = m_Sample.CalculatePixelSize();
  94. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  95. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  96. curFldData.FilterParticles(pImgProcessParam, dPixelSize );
  97. // make sure the particles list is not empty
  98. if (curFldData.NoAnalysisParticle())
  99. {
  100. log.Info("There's no particles to be analysed!");
  101. //return false;
  102. }
  103. return ;
  104. }
  105. //calculate the real sem position of the particle
  106. public void CalculateParticlePos()
  107. {
  108. double dPixelSize = m_Sample.CalculatePixelSize();
  109. CSEMStageData pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  110. foreach (var p in curFldData.ListAnalysisParticles)
  111. {
  112. Point fldOtsPos = new Point(curFldData.PoiPos.X, curFldData.PoiPos.Y);
  113. Point semPos = new Point();
  114. pCSEMStageData.ConverOTSToSEMPoint(fldOtsPos, ref semPos);
  115. p.SetAbsolutPos(semPos);
  116. }
  117. }
  118. public void CollectParticlesXrayData()
  119. {
  120. // get x-ray parameters
  121. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  122. var listAnalysisparts = curFldData.ListAnalysisParticles;
  123. var pStatus = m_Sample.GetMsrStatus();
  124. ////=============================================
  125. curFldData.CreateXrayList(listAnalysisparts);
  126. var xraymode = m_Sample.GetMsrParams().GetXRayParam().GetScanMode();
  127. // get x-ray list (analysis) by particle features
  128. uint nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  129. if (xraymode == OTS_X_RAY_SCAN_MODE.FeatureMode)
  130. {
  131. log.Info("Begin feature mode xray collection!AQTime:"+nXRayAQTime+" xrayNum:"+ listAnalysisparts.Count);
  132. m_EDSHardwareMgr.GetXRayByFeatures(listAnalysisparts, nXRayAQTime, true);
  133. }
  134. else
  135. {
  136. log.Info("Begin point mode xray collection!AQTime:" + nXRayAQTime + " xrayNum:" + listAnalysisparts.Count);
  137. // get x-ray list (analysis) by points
  138. m_EDSHardwareMgr.GetXRayByPoints(listAnalysisparts, nXRayAQTime, true);
  139. }
  140. return ;
  141. }
  142. public override void ClassifyParticles(List<COTSParticleClr> parts)
  143. {
  144. try
  145. {
  146. var quantifyparts = parts;
  147. int nSize = quantifyparts.Count();
  148. // go through all analysis particles
  149. for (int i = 0; i < nSize; ++i)
  150. {
  151. string libname = m_Sample.GetMsrParams().GetSTDName();
  152. ClassifyIncAParticle(quantifyparts[i], libname);
  153. }
  154. }
  155. catch (Exception e)
  156. {
  157. log.Info(" classify failed. " + e.Message);
  158. }
  159. }
  160. public bool ClassifyIncAParticle(COTSParticleClr particle, string libname)// classify particles
  161. {
  162. int steelTech = (int)m_Sample.GetMsrParams().GetSteelTechnology();
  163. particle.SetType((int)OTS_PARTCLE_TYPE.NOT_IDENTIFIED);
  164. if (m_Sample.IfUsingSysSTD())
  165. {
  166. if (libname != "NoSTDDB")
  167. {
  168. //var m_classifyEngine = new CClassifyEngine();
  169. IClassifyEngine engine = m_classifyEngine.GetParticleEngine(libname);
  170. engine.Classify(particle);
  171. }
  172. if (particle.GetType() ==(int) OTS_PARTCLE_TYPE.NOT_IDENTIFIED)
  173. {
  174. IClassifyEngine engine;
  175. engine = m_classifyEngine.GetIncClassifyEngine();
  176. engine.ClassifyIncA(particle, steelTech);
  177. }
  178. }
  179. else
  180. {
  181. if (libname != "NoSTDDB")
  182. {
  183. //var m_classifyEngine = new CClassifyEngine();
  184. IClassifyEngine engine = m_classifyEngine.GetParticleEngine(libname);
  185. engine.Classify(particle);
  186. }
  187. }
  188. return true;
  189. }
  190. public void GetOriginalParticles()
  191. {
  192. // measure status
  193. CMsrSampleStatus pStatus = m_Sample.GetMsrStatus();
  194. // get image process parameter
  195. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  196. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  197. // remove BES image background
  198. curFldData.RemoveBSEImageBG(pImgProcessParam);
  199. // check if this is an empty image
  200. if (curFldData.NoParticle())
  201. { // empty fields
  202. log.Info("ImageProcess: empty field.");
  203. //return false;
  204. }
  205. return ;
  206. }
  207. public bool SaveFieldFiles(COTSFieldData fldData,string filedFileFoler)
  208. {
  209. //loger.Warn("begin bitmap saving...");
  210. string strFieldFileFolder = filedFileFoler;
  211. CBSEImageFileMgr pBSEImgFileMgr = new CBSEImageFileMgr();
  212. pBSEImgFileMgr.SetBSEImg(fldData.GetBSEImage());
  213. int nId = fldData.GetId();
  214. string sFieldId;
  215. sFieldId = nId.ToString();
  216. string strBSEFilePathname = strFieldFileFolder + "\\" + m_pSampleRstFile. SMPL_MSR_RESULT_FIELDS_BSE + sFieldId + pBSEImgFileMgr.BMP_IMG_FILE_EXT;
  217. if (!pBSEImgFileMgr.SaveIntoBitmap(strBSEFilePathname))
  218. {
  219. log.Info("SaveFieldFiles: save BSE file failed.");
  220. return false;
  221. }
  222. // IncA Data list
  223. CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
  224. pDBFileMgr.SaveStatusDataToDB();
  225. if (!pDBFileMgr.SaveIncADataToDB(curFldData.ListAnalysisParticles, fldData.GetPosition()))
  226. {
  227. log.Info("SaveFieldFiles: save inclusion file failed.");
  228. return false;
  229. }
  230. //save the xray data and element data.
  231. CPosXrayDBMgr PosXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
  232. var m_listAnalysisPosXray = new List<CPosXrayClr>();
  233. foreach (var p in curFldData.ListAnalysisParticles)
  234. {
  235. m_listAnalysisPosXray.Add(p.GetXray());
  236. }
  237. if (!PosXrayDBMgr.SaveXray(m_listAnalysisPosXray, true))
  238. {
  239. log.Info("SaveFieldFiles: save analysis x-ray failed.");
  240. return false;
  241. }
  242. return true;
  243. }
  244. }
  245. }