SmplMeasureInclution.cs 13 KB

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