#include "stdafx.h" #include "OTSControlFunExport.h" #include "Bruker\OTSSEMBruker.h" #include "Simulate\OTSSemSim.h" #include "Simulate\OTSEDSSim.h" #include "Simulate\OTSScanSim.h" #include "Bruker\OTSEDSBrucker.h" #include "Bruker\OTSScanBrucker.h" using namespace OTSController; namespace OTSCLRINTERFACE { typedef enum class EDSDevID { Invalid = -1, OFFLINE = 0, BRUKER = 1, OXFORD = 2, }EDS_DEV_ID; bool COTSControlFunExport::Init() { int iDevID = this->GetEDSControllerID(CString(m_DeviceType)); if (nullptr == m_pSem) { switch (iDevID) { case (int)EDS_DEV_ID::BRUKER: { m_pSem = new COTSSEMBruker(); break; } case (int)EDS_DEV_ID::OFFLINE: { m_pSem = new COTSSemSim(); break; } default: break; } } if (nullptr == m_pScan) { switch (iDevID) { case (int)EDS_DEV_ID::BRUKER: { m_pScan = new COTSScanBrucker(); break; } case (int)EDS_DEV_ID::OFFLINE: { m_pScan = new COTSScanSim(); break; } default: break; } } if (nullptr == m_pEDS) { switch (iDevID) { case (int)EDS_DEV_ID::BRUKER: { m_pEDS = new COTSEDSBrucker(); break; } case (int)EDS_DEV_ID::OFFLINE: { m_pEDS = new COTSEDSSim(); break; } default: break; } } if (m_pSem == nullptr || m_pScan == nullptr || m_pEDS == nullptr) { return false; } else { return true; } } bool COTSControlFunExport::ConncetSem() { if (m_pSem->IsConnected()) { return true; } BOOL bRev = m_pSem->Connect(); if (bRev) { if (ScanInit() && EDSInit()) { return true; } else { return false; } } return bRev; } bool COTSControlFunExport::DisconnectSem() { BOOL bRev = m_pSem->Disconnect(); return bRev; } bool COTSControlFunExport::IsConnected() { BOOL bRev = false; if (m_pSem != nullptr) { bRev = m_pSem->IsConnected(); } return bRev; } bool COTSControlFunExport::CollectSpectrum(unsigned long a_nMilliseconds, Point a_oPoint, cli::array^% a_XrayData) { CPoint pt; pt.x = a_oPoint.X; pt.y = a_oPoint.Y; bool bRet = m_pEDS->CollectSpectrum(a_nMilliseconds, pt); DWORD* xrd; xrd = m_pEDS->GetXRayData(); for (int i = 0; i < (int)EDSConst::XANA_CHANNELS; i++) { a_XrayData[i] = xrd[i]; } return bRet; } bool COTSControlFunExport::CollectSpectrum(unsigned long a_nMilliseconds, cli::array^% a_XrayData, unsigned long a_nBufferSize) { long* aData = new long[a_nBufferSize]; memset(aData, 0, a_nBufferSize); bool bRet = m_pEDS->CollectSpectrum(a_nMilliseconds, aData, a_nBufferSize); for (int i = a_nBufferSize; i < (int)a_nBufferSize; i++) { a_XrayData[i] = aData[i]; } delete[] aData; return bRet; } bool COTSControlFunExport::CollectSpectrum(unsigned long a_nMilliseconds, cli::array^% a_XrayData) { bool bRet = m_pEDS->CollectSpectrum(a_nMilliseconds); DWORD* xrd; xrd = m_pEDS->GetXRayData(); for (int i = 0; i < (int)EDSConst::XANA_CHANNELS; i++) { a_XrayData[i] = xrd[i]; } return bRet; } bool COTSControlFunExport::GetXRayByPoints(unsigned long a_nMilliseconds, cli::array^% a_XrayData, bool bQuant) { std::vector listXRayPoints; for (int i = 0; i < a_XrayData->Length; i++) { listXRayPoints.push_back(a_XrayData[i]->GetPosXrayPtr()); } // set get quantify info flag if (bQuant) { m_pEDS->SetQuantification(TRUE); } else { m_pEDS->SetQuantification(FALSE); } bool bRet = m_pEDS->GetXRayByPoints(listXRayPoints, a_nMilliseconds); return bRet; } bool COTSControlFunExport::GetXRayByPoints(unsigned long a_nMilliseconds, cli::array^ points, cli::array^ parts, bool bQuant) { std::vector listXRayPoints; for (int i = 0; i < points->Length; i++) { CPosXrayPtr pXRayPoint = parts[i]->GetXray()->GetPosXrayPtr(); pXRayPoint->SetPosition(CPoint(points[i].X, points[i].Y)); pXRayPoint->SetIndex(parts[i]->GetXray()->GetIndex()); listXRayPoints.push_back(pXRayPoint); } if (bQuant) { m_pEDS->SetQuantification(TRUE); } else { m_pEDS->SetQuantification(FALSE); } bool bRet = m_pEDS->GetXRayByPoints(listXRayPoints, a_nMilliseconds); for (int i = 0; i < points->Length; i++) { auto part = parts[i]->GetOTSParticlePtr(); part->SetXrayInfo(listXRayPoints[i]); } return bRet; } bool COTSControlFunExport::QuantifyXrayByParts(cli::array^ parts) { std::vector xrays; for (int i=0;iLength;i++) { xrays.push_back(parts[i]->GetXray()->GetPosXrayPtr()); } auto bRet = m_pEDS->QuantifyXrays(xrays); return bRet; } bool COTSControlFunExport::QuantifyXrayByPart(COTSParticleClr^ part) { auto xray = part->GetXray(); auto bRet = m_pEDS->QuantifyXray(xray->GetPosXrayPtr()); return bRet; } bool COTSControlFunExport::GetXRayByFeatures(unsigned long a_nMilliseconds, cli::array^ features, cli::array^>^% a_XrayData, cli::array^% a_strEleResult, bool bQuant) { std::vector listXRayPoints; long xraynum = features->Length; for (int i = 0; i < xraynum; i++) { CPosXrayPtr pXRayPoint = CPosXrayPtr(new CPosXray()); listXRayPoints.push_back(pXRayPoint); } std::vectorlistFeatures; for (int i = 0; i < xraynum; i++) { auto feature = features[i]->GetOTSFeaturePtr(); auto otsSegs = feature->GetSegmentsList(); BrukerSegment* segArray = new BrukerSegment[otsSegs.size()]; for (int j = 0; j < otsSegs.size(); j++) { auto seg = otsSegs[j]; BrukerSegment* oSegment = &segArray[j]; oSegment->XCount = seg->GetLength(); oSegment->XStart = seg->GetStart(); oSegment->Y = seg->GetHeight(); } BrukerFeature oFeature; oFeature.SegmentCount = otsSegs.size(); oFeature.pSegment = segArray; listFeatures.push_back(oFeature); } // set get quantify info flag if (bQuant) { m_pEDS->SetQuantification(TRUE); } else { m_pEDS->SetQuantification(FALSE); } bool bRet = m_pEDS->GetXRayByFeatures(listXRayPoints, listFeatures, a_nMilliseconds); DWORD* xrd; for (int i = 0; i < xraynum; i++) { xrd = listXRayPoints[i]->GetXrayData(); for (int j = 0; j < (int)EDSConst::XANA_CHANNELS; j++) { a_XrayData[i][j] = xrd[j]; } delete listFeatures[i].pSegment;// here using the pure pointer increase the understandability but bring another problem: you must not forget to free the pointer or you involve into memory leak! } if (bQuant) { for (int i = 0; i < features->Length; i++) { CElementChemistriesList& listElement = listXRayPoints[i]->GetElementQuantifyData(); int nElementNum = (int)listElement.size(); CString strElementResult = _T(""); for (auto pElement : listElement) { CString strResult; CString strName = pElement->GetName(); double dPercent = pElement->GetPercentage(); strResult.Format(_T("%s: %f\n"), strName, dPercent); strElementResult += strResult; } a_strEleResult[i] = gcnew String(strElementResult); } } return bRet; } bool COTSControlFunExport::GetXRayByFeatures(unsigned long a_nMilliseconds, cli::array^ parts, bool bQuant) { std::vector listXRayPoints; long xraynum = parts->Length; for (int i = 0; i < xraynum; i++) { CPosXrayPtr pXRayPoint = parts[i]->GetXray()->GetPosXrayPtr(); pXRayPoint->SetPosition(CPoint(parts[i]->GetXRayPos()->X, parts[i]->GetXRayPos()->Y)); listXRayPoints.push_back(pXRayPoint); } std::vectorlistFeatures; for (int i = 0; i < xraynum; i++) { auto feature = parts[i]->GetOTSParticlePtr()->GetFeature(); auto otsSegs = feature->GetSegmentsList(); BrukerSegment* segArray = new BrukerSegment[otsSegs.size()]; for (int j = 0; j < otsSegs.size(); j++) { auto seg = otsSegs[j]; BrukerSegment* oSegment = &segArray[j]; oSegment->XCount = seg->GetLength(); oSegment->XStart = seg->GetStart(); oSegment->Y = seg->GetHeight(); } BrukerFeature oFeature; oFeature.SegmentCount = otsSegs.size(); oFeature.pSegment = segArray; listFeatures.push_back(oFeature); } // set get quantify info flag if (bQuant) { m_pEDS->SetQuantification(TRUE); } else { m_pEDS->SetQuantification(FALSE); } bool bRet = m_pEDS->GetXRayByFeatures(listXRayPoints, listFeatures, a_nMilliseconds); //DWORD* xrd; for (int i = 0; i < xraynum; i++) { auto part = parts[i]->GetOTSParticlePtr(); part->SetXrayInfo(listXRayPoints[i]); delete listFeatures[i].pSegment;// using the pure pointer increase the understandability but bring another problem: you must not forget to free the pointer or you involve into memory leak! } return bRet; } bool COTSControlFunExport::GetXRayBySinglePoint(unsigned long a_nMilliseconds, Point point, cli::array^% a_XrayData, String^% a_strEleResult, bool bQuant) { std::vector listXRayPoints; CPosXrayPtr pXRayPoint = CPosXrayPtr(new CPosXray()); pXRayPoint->SetPosition(CPoint(point.X, point.Y)); listXRayPoints.push_back(pXRayPoint); // set get quantify info flag if (bQuant) { m_pEDS->SetQuantification(TRUE); } else { m_pEDS->SetQuantification(FALSE); } bool bRet = m_pEDS->GetXRayByPoints(listXRayPoints, a_nMilliseconds); DWORD* xrd; xrd = listXRayPoints[0]->GetXrayData(); for (int j = 0; j < (int)EDSConst::XANA_CHANNELS; j++) { a_XrayData[j] = xrd[j]; } if (bQuant) { CElementChemistriesList& listElement = listXRayPoints[0]->GetElementQuantifyData(); int nElementNum = (int)listElement.size(); CString strElementResult = _T(""); for (auto pElement : listElement) { CString strResult; CString strName = pElement->GetName(); double dPercent = pElement->GetPercentage(); strResult.Format(_T("%s: %f\n"), strName, dPercent); strElementResult += strResult; } a_strEleResult = gcnew String(strElementResult); } return bRet; } bool COTSControlFunExport::GetXRayBySingleFeature(unsigned long a_nMilliseconds, COTSFeatureClr^ feature, cli::array^% a_XrayData, String^% a_strEleResult, bool bQuant) { std::vector listXRayPoints; CPosXrayPtr pXRayPoint = CPosXrayPtr(new CPosXray()); listXRayPoints.push_back(pXRayPoint); std::vector listSegment; std::vectorlistFeatures; auto otsSegs = feature->GetSegmentsList(); for (int j = 0; j < otsSegs->Count; j++) { auto seg = otsSegs[j]; BrukerSegment oSegment; oSegment.XCount = seg->GetLength(); oSegment.XStart = seg->GetStart(); oSegment.Y = seg->GetHeight(); listSegment.push_back(oSegment); } BrukerFeature ofeature; ofeature.SegmentCount = listSegment.size(); ofeature.pSegment = &listSegment[0]; listFeatures.push_back(ofeature); bool bRet = FALSE; // set get quantify info flag if (bQuant) { m_pEDS->SetQuantification(TRUE); } else { m_pEDS->SetQuantification(FALSE); } bRet = m_pEDS->GetXRayByFeatures(listXRayPoints, listFeatures, a_nMilliseconds); DWORD* xrd; pXRayPoint = listXRayPoints[0]; xrd = pXRayPoint->GetXrayData(); for (int i = 0; i < (int)EDSConst::XANA_CHANNELS; i++) { a_XrayData[i] = xrd[i]; } if (bQuant) { CString strElementResult = listXRayPoints[0]->GetQuantifiedElementsStr(); a_strEleResult = gcnew String(strElementResult); } return bRet; } bool COTSControlFunExport::GetXRayAndElements(unsigned long a_nMilliseconds, int a_BSEX, int a_BSEY, cli::array^% a_XrayData, int^% a_nElementNum, String^% a_strResult) { std::vector listXRayPoints; CPosXrayPtr pXRayPoint = CPosXrayPtr(new CPosXray()); pXRayPoint->SetPosition(CPoint(a_BSEX, a_BSEY)); listXRayPoints.push_back(pXRayPoint); m_pEDS->SetQuantification(TRUE); bool bRet = m_pEDS->GetXRayByPoints(listXRayPoints, a_nMilliseconds); DWORD* xrd; pXRayPoint = listXRayPoints[0]; xrd = pXRayPoint->GetXrayData(); // element quantify data a_nElementNum = (int)listXRayPoints[0]->GetElementQuantifyData().size(); for (int i = 0; i < (int)EDSConst::XANA_CHANNELS; i++) { a_XrayData[i] = xrd[i]; } CString strElementResult = listXRayPoints[0]->GetQuantifiedElementsStr(); a_strResult = gcnew String(strElementResult); return bRet; } int COTSControlFunExport::AcquireBSEImage( cli::array^% a_ImgData) { int bRet = 0; CSize sz; int height, width; CBSEImgPtr pbseImg = nullptr; pbseImg = m_pScan->AcquireBSEImage(); if (pbseImg == nullptr) { return bRet; } sz = pbseImg->GetImageSize(); height = sz.cx; width = sz.cy; bRet = height * width; BYTE* lpData = pbseImg->GetImageDataPointer(); for (int i = 0; i < bRet; i++) { a_ImgData[i] = lpData[i]; } return bRet; } int COTSControlFunExport::GetEDSControllerID(const CString& EDSControllerName) { char* lpEdsName[] = { "OffLine","Bruker"}; int iLen = sizeof(lpEdsName) / sizeof(*lpEdsName); int i = 0; for (i = 0; i < iLen; i++) { if (0 == strcmp(lpEdsName[i], EDSControllerName)) { return i; } } return -1; } void COTSControlFunExport::SetExpectCount(int expectcount) { m_pEDS->SetExpectCount(expectcount); } int COTSControlFunExport::GetExpectCount() { return m_pEDS->GetExpectCount(); } void COTSControlFunExport::SetQuantificationParam(bool ifauto, String^ knownelements) { CString knownele = knownelements; m_pEDS->SetQuantificationParam(ifauto, knownele); } }