| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556 |
- #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<OxfordXrayData> 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<BrukerFeature>& 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
|