123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using OTSCOMMONCLR;
- using OTSDataType;
- using OTSModelSharp.Measure.OTSInclution;
- using OTSModelSharp.ServiceCenter;
- using OTSModelSharp.ServiceInterface;
- using static OTSDataType.otsdataconst;
- namespace OTSModelSharp
- {
- class CSmplMeasureInclution : CSmplMeasure
- {
-
-
- public CSmplMeasureInclution(string a_strWorkingFolder, COTSSample a_pSample) : base(a_strWorkingFolder, a_pSample)
- {
- SetWorkingFolder(a_strWorkingFolder);
- SetSample(a_pSample);
- m_classifyEngine = new CClassifyEngine();
- }
- // field image process
- public override bool FieldImageProcess(Point fldCenter, CBSEImgClr a_pBSEImg)
- {
- int nNewFieldId;
- nNewFieldId = m_pSampleRstFile.GetIdForANewField();
- // create a field
- curFldData = new CFieldDataIncA(a_pBSEImg, m_Sample.CalculatePixelSize());
- curFldData.SetId(nNewFieldId);
- curFldData.SetPosition(fldCenter);
- //first step:remove background of the bse image and compound all the finded particles.
- COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
- loger.Info("Begin to process image and get all particles!");
- if (!GetOriginalParticles())
- {
- StartSaveFileThread(curFldData);
- loger.Error("ImageProcess: failed to find any particles.");
- return false;
- }
- // second step :filter the finded particles.
- loger.Info("Begin to filter particles!");
- if (!FilterParticles())
- {
- StartSaveFileThread(curFldData);
- loger.Info("ImageProcess: there is no particles to be analyzed.");
- return false;
- }
- if (pXRayParam.GetUsingXray() == (int)OTS_USING_X_RAY.Yes)
- {
- Thread.Sleep(1000);
- if (!CollectParticlesXrayData())
- {
- StartSaveFileThread(curFldData);
- loger.Error("ImageProcess: failed to collect xray data!");
- return false;
- }
- Thread.Sleep(1000);
- }
- loger.Info("Begin to Calculate the image property of every particle!");
- var analysisparts = curFldData.ListAnalysisParticles;
- curFldData.CalParticleImageProp(analysisparts);//calculate particle image property such as feret diameter, DMAX etc.
- loger.Info("Begin to classify particles! particle num:"+ curFldData.ListAnalysisParticles.Count);
- ClassifyParticles(curFldData.ListAnalysisParticles);
-
- // save field files
- m_Sample.GetMsrStatus().SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
- StartSaveFileThread(curFldData);
- return true; ;
- }
- // save field data
- protected void SaveFieldMgrData()
- {
- while (bSaveThreadWorking)
- {
- while (fieldQueue.Count() > 0)
- {
- COTSFieldData f = fieldQueue.Dequeue();
-
- //save to disk first ,then pop . if the size is 0,then we know all the saving work is done.
- SaveFieldFiles(f,m_pSampleRstFile.GetFieldFileSubFolderStr());
- }
- if (fieldQueue.Count() == 0)
- {
- bSaveThreadWorking = false; //must set this flag,so the main thread can know this thread has exited.
- return;
- }
- }
- return;
- }
-
- protected void StartSaveFileThread(COTSFieldData a_pFieldMgr)
- {
-
-
- fieldQueue.Enqueue(a_pFieldMgr);
- if (fieldQueue.Count() > 0) //if there's data in the queue and the previous thread has finished then start a new thread.
- {
- if (bSaveThreadWorking == false)
- {
- bSaveThreadWorking = true;
- m_thread_ptr = new System.Threading.Thread(this.SaveFieldMgrData);//m_thread_ptr = shared_ptr<thread>(new thread(&CSmplMeasureInc::SaveFieldMgrData, this));
- m_thread_ptr.Start();//m_thread_ptr->detach();
- }
- }
- }
- public bool FilterParticles()
- {
- // remove over sized particles, too small particles and get a analysis particles list
- // get pixel size
- double dPixelSize = m_Sample.CalculatePixelSize();
-
- CSampleParam pMsrParam = m_Sample.GetMsrParams();
- COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
- curFldData.FilterParticles(pImgProcessParam, dPixelSize );
- //calculate the real sem position of the particle
- CSEMStageData pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
- foreach (var p in curFldData.ListAnalysisParticles)
- {
- var r = (Rectangle)p.GetParticleRect();
- Point fldLeftTopOTSPos = new Point(curFldData.PoiPos.X - curFldData.Width / 2, curFldData.PoiPos.Y + curFldData.Height / 2);//OTS coordinate
- Point Partcenter = new Point(r.Top + r.Height / 2, r.Left + r.Width / 2);//screen coordinate
- Point otsPartCenter = new Point((int)(fldLeftTopOTSPos.X + Partcenter.X * dPixelSize), (int)(fldLeftTopOTSPos.Y + Partcenter.Y * dPixelSize));
- Point sempoint = new Point();
- pCSEMStageData.ConverOTSToSEMPoint(otsPartCenter, ref sempoint);
- p.SetAbsolutPos(sempoint);
- }
- // make sure the particles list is not empty
- if (curFldData.NoAnalysisParticle())
- {
- loger.Info("There's no particles to be analysed!");
- return false;
- }
- return true;
- }
- public bool CollectParticlesXrayData()
- {
- // get x-ray parameters
- COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
- var listAnalysisparts = curFldData.ListAnalysisParticles;
- var pStatus = m_Sample.GetMsrStatus();
-
- ////=============================================
- curFldData.CreateXrayList(listAnalysisparts);
-
- var xraymode = m_Sample.GetMsrParams().GetXRayParam().GetScanMode();
- // get x-ray list (analysis) by particle features
- uint nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
- if (xraymode == OTS_X_RAY_SCAN_MODE.FeatureMode)
- {
- loger.Info("Begin feature mode xray collection!AQTime:"+nXRayAQTime+" xrayNum:"+ listAnalysisparts.Count);
- if (!m_EDSHardwareMgr.GetXRayByFeatures(listAnalysisparts, nXRayAQTime, true))
- {
- loger.Error("ImageProcess: call GetXRayByFeatures method.");
- pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
- StartSaveFileThread(curFldData);
- return false;
- }
- }
- else
- {
- loger.Info("Begin point mode xray collection!AQTime:" + nXRayAQTime + " xrayNum:" + listAnalysisparts.Count);
- // get x-ray list (analysis) by points
- if (!m_EDSHardwareMgr.GetXRayByPoints(listAnalysisparts, nXRayAQTime, true))
- { // failed to call GetXRayByPoints method
- loger.Error("ImageProcess: call GetXRayByPoints method.");
- pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
- StartSaveFileThread(curFldData);
- return false;
- }
- }
-
- return true;
- }
-
- public override void ClassifyParticles(List<COTSParticleClr> parts)
- {
- try
- {
- var quantifyparts = parts;
- int nSize = quantifyparts.Count();
- // go through all analysis particles
- for (int i = 0; i < nSize; ++i)
- {
- string libname = m_Sample.GetMsrParams().GetSTDName();
- ClassifyIncAParticle(quantifyparts[i], libname);
- }
-
-
- }
- catch (Exception e)
- {
- loger.Info("calcu the particle image property or classify failed. " + e.Message);
- }
- }
- public bool ClassifyIncAParticle(COTSParticleClr particle, string libname)// classify particles
- {
- int steelTech = (int)m_Sample.GetMsrParams().GetSteelTechnology();
- particle.SetType((int)OTS_PARTCLE_TYPE.NOT_IDENTIFIED);
- if (m_Sample.IfUsingSysSTD())
- {
- if (libname != "NoSTDDB")
- {
- //var m_classifyEngine = new CClassifyEngine();
- IClassifyEngine engine = m_classifyEngine.GetParticleEngine(libname);
- engine.Classify(particle);
- }
- if (particle.GetType() ==(int) OTS_PARTCLE_TYPE.NOT_IDENTIFIED)
- {
- IClassifyEngine engine;
- engine = m_classifyEngine.GetIncClassifyEngine();
- engine.ClassifyIncA(particle, steelTech);
- }
- }
- else
- {
- if (libname != "NoSTDDB")
- {
- //var m_classifyEngine = new CClassifyEngine();
- IClassifyEngine engine = m_classifyEngine.GetParticleEngine(libname);
- engine.Classify(particle);
- }
- }
-
- return true;
- }
-
- public bool GetOriginalParticles()
- {
- // measure status
- CMsrSampleStatus pStatus = m_Sample.GetMsrStatus();
- // get image process parameter
- CSampleParam pMsrParam = m_Sample.GetMsrParams();
- COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
- // remove BES image background
- if (!curFldData.RemoveBSEImageBG(pImgProcessParam))
- { // failed to call RemoveBSEImageBG method
- loger.Error("ImageProcess: call RemoveBSEImageBG method.");
- pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
- return false;
- }
-
- // check if this is an empty image
- if (curFldData.NoParticle())
- { // empty fields
- loger.Info("ImageProcess: empty field.");
- return false;
- }
- return true;
- }
- public bool SaveFieldFiles(COTSFieldData fldData,string filedFileFoler)
- {
- //loger.Warn("begin bitmap saving...");
- string strFieldFileFolder = filedFileFoler;
- CBSEImageFileMgr pBSEImgFileMgr = new CBSEImageFileMgr();
- pBSEImgFileMgr.SetBSEImg(fldData.GetBSEImage());
- int nId = fldData.GetId();
- string sFieldId;
- sFieldId = nId.ToString();
-
- string strBSEFilePathname = strFieldFileFolder + "\\" + m_pSampleRstFile. SMPL_MSR_RESULT_FIELDS_BSE + sFieldId + pBSEImgFileMgr.BMP_IMG_FILE_EXT;
- if (!pBSEImgFileMgr.SaveIntoBitmap(strBSEFilePathname))
- {
- loger.Info("SaveFieldFiles: save BSE file failed.");
- return false;
- }
- // IncA Data list
- CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
-
- pDBFileMgr.SaveStatusDataToDB();
- if (!pDBFileMgr.SaveIncADataToDB(curFldData.ListAnalysisParticles, fldData.GetPosition()))
- {
- loger.Info("SaveFieldFiles: save inclusion file failed.");
- return false;
- }
-
- //save the xray data and element data.
- CPosXrayDBMgr PosXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
- var m_listAnalysisPosXray = new List<CPosXrayClr>();
- foreach (var p in curFldData.ListAnalysisParticles)
- {
- m_listAnalysisPosXray.Add(p.GetXray());
- }
-
- if (!PosXrayDBMgr.SaveXray(m_listAnalysisPosXray, true))
- {
- loger.Info("SaveFieldFiles: save analysis x-ray failed.");
- return false;
- }
- return true;
- }
- }
- }
|