#include "stdafx.h" #include "OTSOxfordImpl.h" #include "../OTSControl/ControllerHelper.h" #include "COTSUtilityDllFunExport.h" #include "OxfordImplConst.h" #ifdef _DEBUG #define new DEBUG_NEW #endif namespace OTSController { OxfordImpl::OxfordImpl(void) : m_bInit(false) { } OxfordImpl::~OxfordImpl(void) { CloseClient(); m_oxfordPtr.reset(); } // Close Client // return true if success void OxfordImpl::CloseClient(void) { m_oxfordPtr->CloseClient(); } bool OxfordImpl::Connect() { if (!m_oxfordPtr) { m_oxfordPtr.reset(new OxfordController); } if (!m_bInit) { m_bInit = m_oxfordPtr->CreateController(); } return m_bInit; } bool OxfordImpl::DisConnect() { if (m_bInit) { CloseClient(); m_bInit = false; } return true; } bool OxfordImpl::IsConnected() { if (m_oxfordPtr && m_bInit) { return (bool)m_oxfordPtr->IsConnected(); } return false; } bool OxfordImpl::GetPositionXY(double& a_dPosX, double& a_dPosY) { if (m_oxfordPtr && m_bInit) { return m_oxfordPtr->GetPositionXY(a_dPosX, a_dPosY); } return false; } bool OxfordImpl::SetPositionXY(double a_dPosX, double a_dPosY) { if (m_oxfordPtr && m_bInit) { return m_oxfordPtr->SetPositionXY(a_dPosX, a_dPosY); } return false; } bool OxfordImpl::GetWorkingDistance(double& a_dWorkingDistance) { if (m_oxfordPtr && m_bInit) { return m_oxfordPtr->GetWorkingDistance(a_dWorkingDistance); } return false; } bool OxfordImpl::SetWorkingDistance(double a_dWorkingDistance) { if (m_oxfordPtr && m_bInit) { return m_oxfordPtr->SetWorkingDistance(a_dWorkingDistance); } return false; } bool OxfordImpl::GetMagnification(double& a_dMagnification) { if (m_oxfordPtr && m_bInit) { return m_oxfordPtr->GetMagnification(a_dMagnification); } return false; } bool OxfordImpl::SetMagnification(double a_dMagnification) { if (m_oxfordPtr && m_bInit) { return m_oxfordPtr->SetMagnification(a_dMagnification); } return false; } bool OxfordImpl::GetHighVoltage(double& a_dHighVoltage) { if (m_oxfordPtr && m_bInit) { return m_oxfordPtr->GetHighVoltage(a_dHighVoltage); } return false; } bool OxfordImpl::SetHighVoltage(double a_dHighVoltage) { if (m_oxfordPtr && m_bInit) { return m_oxfordPtr->SetHighVoltage(a_dHighVoltage); } return false; } bool OxfordImpl::GetBeamOn(bool& a_bBeamOn) { if (m_oxfordPtr && m_bInit) { return m_oxfordPtr->GetBeamOn(a_bBeamOn); } return false; } bool OxfordImpl::SetBeamOn(bool a_bBeamOn) { if (m_oxfordPtr && m_bInit) { return m_oxfordPtr->SetBeamOn(a_bBeamOn); } return false; } bool OxfordImpl::GetBeamBlank(bool& a_bBeamBlank) { if (m_oxfordPtr && m_bInit) { return m_oxfordPtr->GetBeamBlank(a_bBeamBlank); } return false; } bool OxfordImpl::SetBeamBlank(bool a_bBeamBlank) { if (m_oxfordPtr && m_bInit) { return m_oxfordPtr->SetBeamBlank(a_bBeamBlank); } return false; } bool OxfordImpl::GetExternal(bool& a_bExternal) { if (m_oxfordPtr && m_bInit) { return m_oxfordPtr->GetExternal(a_bExternal); } return false; } bool OxfordImpl::SetExternal(bool a_bExternal) { if (m_oxfordPtr && m_bInit) { return m_oxfordPtr->SetExternal(a_bExternal); } return false; } bool OxfordImpl::CollectXrayData(const long a_nAcTime, long* a_pnCounts, DWORD a_nBufferSize) { if (m_oxfordPtr && m_bInit) { return m_oxfordPtr->CollectXrayData(a_nAcTime, a_pnCounts, a_nBufferSize); } return false; } bool OxfordImpl::CollectXrayDataAtPos(const double a_dPosX, const double a_dPosY, const long a_nAcTime, long* a_pnCounts, DWORD a_nBufferSize) { if (m_oxfordPtr && m_bInit) { return m_oxfordPtr->CollectXrayDataAtPos(a_dPosX, a_dPosY, a_nAcTime, a_pnCounts, a_nBufferSize); } return false; } bool OxfordImpl::SetBeamPosition(const double a_dPosX, const double a_dPosY) { if (m_oxfordPtr && m_bInit) { return m_oxfordPtr->SetBeamPosition(a_dPosX, a_dPosY); } return false; } bool OxfordImpl::SetScanSpeed(const long a_nMilliseconds) { if (m_oxfordPtr && m_bInit) { return m_oxfordPtr->SetScanSpeed(a_nMilliseconds); } return false; } bool OxfordImpl::GetImageSize(long& a_nWidth, long& a_nHeight) { if (m_oxfordPtr && m_bInit) { return m_oxfordPtr->GetImageSize(a_nWidth, a_nHeight); } return false; } bool OxfordImpl::SetImageSize(const long a_nWidth, const long a_nHeight) { if (m_oxfordPtr && m_bInit) { return m_oxfordPtr->SetImageSize(a_nWidth, a_nHeight); } return false; } bool OxfordImpl::CollectImage(BYTE* a_pImageBits) { if (m_oxfordPtr && m_bInit) { int i= m_oxfordPtr->CollectImage(a_pImageBits); return i== (int)OxfordWrapperErrorCode::SUCCEED; } return false; } bool OxfordImpl::QuantifySpectrum(unsigned char* cResult) { if (m_oxfordPtr && m_bInit) { return m_oxfordPtr->QuantifySpectrum(cResult); } return false; } BOOL OxfordImpl::GetXRayByPoints(CPosXraysList& a_listXrayPois, DWORD a_nACTimeMS) { try { // oxford dll handle check ASSERT(m_oxfordPtr); if (!m_oxfordPtr) { // error, invalid m_oxfordDll LogErrorTrace(__FILE__, __LINE__, _T("COTSOxfordImpl::GetXRayByPoints: invalid m_oxfordDll.")); return FALSE; } // do nothing if points list is empty if (a_listXrayPois.empty()) { // points list is empty LogTrace(__FILE__, __LINE__, _T("COTSOxfordImpl::GetXRayByPoints: poits list is empty.")); return TRUE; } // create array of BrukerSegment long nCollectCount = (long)a_listXrayPois.size(); //boost::scoped_array segmentArray(new OxfordXrayData[nCollectCount]); OxfordXrayData* segmentArray(new OxfordXrayData[nCollectCount]); for (int i = 0; i < nCollectCount; ++i) { CPoint poi = a_listXrayPois[i]->GetPosition(); segmentArray[i].m_nPosX = poi.x; segmentArray[i].m_nPosY = poi.y; } if (!m_oxfordPtr->CollectXrayList(a_nACTimeMS, segmentArray, nCollectCount, GENERALXRAYCHANNELS)) { LogTrace(__FILE__, __LINE__, _T("COTSOxfordImpl::GetXRayByPoints: poits list is empty.")); return false; } // get the specs for a_vXPoints for (int i = 0; i < nCollectCount; ++i) { // set spectrum data for the x-ray point a_listXrayPois[i]->SetXrayData((DWORD*)(segmentArray[i].m_pXrayData)); if (m_bDoQuantification) { // quantify the spectrum CElementChemistriesList vElement = CElement::ExtractElementChemistrys(CControllerHelper::CharToString((const char*)segmentArray[i].m_strElementResult)); a_listXrayPois[i]->SetElementQuantifyData(vElement); } } for (int i = 0; i < nCollectCount; ++i) { // check spectrum DWORD nTatolXrayCount = a_listXrayPois[i]->GetTotalCount(); if (nTatolXrayCount < 20) { // captured an empty spectrum CPoint poi = a_listXrayPois[i]->GetPosition(); // try to redo x-ray collection at the position static DWORD nChannelData[GENERALXRAYCHANNELS]; memset(nChannelData, 0, sizeof(DWORD) * GENERALXRAYCHANNELS); if (!m_oxfordPtr->CollectXrayData(a_nACTimeMS, (long*)nChannelData, GENERALXRAYCHANNELS)) { // error CString s; s.Format(_T("Call CollectOneXRayPoint failed: index = %d(x:%d, y:%d))"), i, poi.x, poi.y); LogErrorTrace(__FILE__,__LINE__,s); return false; } // set spectrum with new spectrum a_listXrayPois[i]->SetXrayData(nChannelData); nTatolXrayCount = a_listXrayPois[i]->GetTotalCount(); if (nTatolXrayCount < 20) { CString s1; s1.Format(_T("Single point spectrum still low count (%d counts), index = %d(x:%d, y:%d.) This could be caused by charging."), nTatolXrayCount, i, poi.x, poi.y); LogTrace(__FILE__, __LINE__, s1); } else { CString s2; s2.Format(_T("Single point spectrum collected successfully (%d counts), index = %d(x:%d, y:%d.)"), nTatolXrayCount, i, poi.x, poi.y); LogTrace(__FILE__, __LINE__, s2); } } } delete segmentArray; // ok return TRUE return TRUE; } catch (const std::exception e) { LogErrorTrace(__FILE__, __LINE__, _T("COTSOxfordImpl::GetXRayByPoints: exception.")+ CString(e.what())); } // error, return false return FALSE; } BOOL OxfordImpl::GetXRayByFeatures(CPosXraysList&a_listXrayPois, std::vector& a_vFeatures, DWORD a_nXRayAQTime) { try { // oxford dll handle check ASSERT(m_oxfordPtr); if (!m_oxfordPtr) { // error, invalid m_oxfordDll LogErrorTrace(__FILE__, __LINE__, _T("COTSOxfordImpl::GetXRayByPoints: invalid m_oxfordDll.")); return FALSE; } // do nothing if points list is empty if (a_listXrayPois.empty()) { // points list is empty LogTrace(__FILE__, __LINE__, _T("COTSOxfordImpl::GetXRayByPoints: poits list is empty.")); return TRUE; } if (a_listXrayPois.size() != a_vFeatures.size()) { return FALSE; } // create array of BrukerSegment long nCollectCount = (long)a_listXrayPois.size(); //convert the brukerfeature to oxfordfeature OxfordXrayData* features(new OxfordXrayData[nCollectCount]); int nTotalNum = 0; for (size_t i = 0; i < a_listXrayPois.size(); i++) { long nSegmentCount = (long)a_vFeatures[i].SegmentCount; features[i].m_nChordNum = nSegmentCount; long nPixelCount = 0; if (nSegmentCount > 0) { BrukerSegment* segs = a_vFeatures[i].pSegment; for (int j = 0; j < nSegmentCount; j++) { features[i].m_ChordList[j].m_nX = segs[j].XStart; features[i].m_ChordList[j].m_nY = segs[j].Y; features[i].m_ChordList[j].m_nLength = segs[j].XCount; nPixelCount += segs[j].XCount; } features[i].m_nPixelNum = nPixelCount; } else { // will generate according to the x-ray position // this shouldn't happen } nTotalNum += nPixelCount; } int a_nACTimeMS = a_nXRayAQTime / nTotalNum; if (!m_oxfordPtr->CollectXrayArea(a_nXRayAQTime, features, nCollectCount, GENERALXRAYCHANNELS)) { LogTrace(__FILE__, __LINE__, _T("COTSOxfordImpl::GetXRayByPoints: poits list is empty.")); return false; } // get the specs for a_vXPoints for (int i = 0; i < nCollectCount; ++i) { // set spectrum data for the x-ray point a_listXrayPois[i]->SetXrayData((DWORD*)(features[i].m_pXrayData)); // set spectrum data for the x-ray point if (m_bDoQuantification) { // quantify the spectrum CElementChemistriesList vElement = CElement::ExtractElementChemistrys(CControllerHelper::CharToString((const char*)features[i].m_strElementResult)); a_listXrayPois[i]->SetElementQuantifyData(vElement); } } for (int i = 0; i < nCollectCount; ++i) { // check spectrum DWORD nTatolXrayCount = a_listXrayPois[i]->GetTotalCount(); if (nTatolXrayCount < 20) { // captured an empty spectrum CPoint poi = a_listXrayPois[i]->GetPosition(); // try to redo x-ray collection at the position static DWORD nChannelData[GENERALXRAYCHANNELS]; memset(nChannelData, 0, sizeof(DWORD) * GENERALXRAYCHANNELS); if (!m_oxfordPtr->CollectXrayData(a_nXRayAQTime, (long*)nChannelData, GENERALXRAYCHANNELS)) { return false; } // set spectrum with new spectrum a_listXrayPois[i]->SetXrayData(nChannelData); nTatolXrayCount = a_listXrayPois[i]->GetTotalCount(); if (nTatolXrayCount < 20) { /*LogWarn(_T("Single point spectrum still low count (%d counts), index = %d(x:%d, y:%d.) This could be caused by charging."), nTatolXrayCount, i, poi.x, poi.y);*/ } else { /*LogWarn(_T("Single point spectrum collected successfully (%d counts), index = %d(x:%d, y:%d.)"), nTatolXrayCount, i, poi.x, poi.y);*/ } } } delete features; // ok return TRUE return TRUE; } catch (const std::exception&) { LogErrorTrace(__FILE__, __LINE__, _T("COTSOxfordImpl::GetXRayByPoints: exception.")); } // error, return false return FALSE; } } // namespace Controller