| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720 | #include "stdafx.h"#include "OTSOxfordImpl.h"#include "ControllerHelper.h"#include "COTSUtilityDllFunExport.h"#include "OxfordImplConst.h"#ifdef _DEBUG#define new DEBUG_NEW#endif#include <Oxford/OxfordWrapper/OxfordControllerWrapper.h> namespace OTSController{    OxfordImpl::OxfordImpl(void)    {		if (ManagedGlobals::oxfordController == nullptr)		{			ManagedGlobals::oxfordController = gcnew OxfordControllerWrapper();			m_bInit = false;		}    }    OxfordImpl::~OxfordImpl(void)    {		CloseClient();		    }	void OxfordImpl::CloseClient(void)	{		ManagedGlobals::oxfordController->CloseClient();			}    bool OxfordImpl::Connect()    {		if (!m_bInit)		{								if (!ManagedGlobals::oxfordController->Init())			{							 return false;			}			if (!ManagedGlobals::oxfordController->IsConnected())			{							return false;			}						m_bInit = true;		}          		        return m_bInit;    }	bool OxfordImpl::DisConnect()	{		if (m_bInit)		{			CloseClient();			m_bInit = false;		}				return true;	}    bool OxfordImpl::IsConnected()    {        if (m_bInit)        {			if (!ManagedGlobals::oxfordController->IsConnected())			{				return false;			}			return true;        }        return false;    }    bool OxfordImpl::GetPositionXY(double& a_dPosX, double& a_dPosY)    {        if (m_bInit)        {						if (!ManagedGlobals::oxfordController->GetPositionXY(a_dPosX, a_dPosY))			{				LogErrorTrace(__FILE__, __LINE__, _T("GetPositionXY command failed."));				return false;			}			return true;        }        return false;    }    bool OxfordImpl::SetPositionXY(double a_dPosX, double a_dPosY)    {        if (m_bInit)        {						if (!ManagedGlobals::oxfordController->SetPositionXY(a_dPosX, a_dPosY))			{				LogErrorTrace(__FILE__, __LINE__, _T("SetPositionXY command failed."));				return false;			}			return true;        }        return false;    }    bool OxfordImpl::GetWorkingDistance(double& a_dWorkingDistance)    {        if ( m_bInit)        {						if (!ManagedGlobals::oxfordController->GetWorkingDistance(a_dWorkingDistance))			{				LogErrorTrace(__FILE__, __LINE__, _T("GetWorkingDistance command failed."));				return false;			}			return true;        }        return false;    }    bool OxfordImpl::SetWorkingDistance(double a_dWorkingDistance)    {        if (m_bInit)        {						if (!ManagedGlobals::oxfordController->SetWorkingDistance(a_dWorkingDistance))			{				LogErrorTrace(__FILE__, __LINE__, _T("SetWorkingDistance command failed."));				return false;			}			return true;        }        return false;    }    bool OxfordImpl::GetMagnification(double& a_dMagnification)    {        if (m_bInit)        {					if (!ManagedGlobals::oxfordController->GetMagnification(a_dMagnification))			{				LogErrorTrace(__FILE__, __LINE__, _T("GetMagnification command failed."));				return false;			}			return true;        }        return false;    }    bool OxfordImpl::SetMagnification(double a_dMagnification)    {        if (m_bInit)        {			if (!ManagedGlobals::oxfordController->SetMagnification(a_dMagnification))			{				LogErrorTrace(__FILE__, __LINE__, _T("SetMagnification command failed."));				return false;			}			return true;        }        return false;    }    bool OxfordImpl::GetHighVoltage(double& a_dHighVoltage)    {        if ( m_bInit)        {			if (!ManagedGlobals::oxfordController->GetHighVoltage(a_dHighVoltage))			{				LogErrorTrace(__FILE__, __LINE__, _T("GetHighVoltage command failed."));				return false;			}			return true;        }        return false;    }    bool OxfordImpl::SetHighVoltage(double a_dHighVoltage)    {        if (m_bInit)        {			if (!ManagedGlobals::oxfordController->SetHighVoltage(a_dHighVoltage))			{				LogErrorTrace(__FILE__, __LINE__, _T("SetHighVoltage command failed."));				return false;			}			return true;        }        return false;    }    bool OxfordImpl::GetBeamOn(bool& a_bBeamOn)    {        if (m_bInit)        {			if (!ManagedGlobals::oxfordController->GetBeamOn(a_bBeamOn))			{				LogErrorTrace(__FILE__, __LINE__, _T("GetBeamOn command failed."));				return false;			}			return true;        }        return false;    }    bool OxfordImpl::SetBeamOn(bool a_bBeamOn)    {        if ( m_bInit)        {			if (!ManagedGlobals::oxfordController->SetBeamOn(a_bBeamOn))			{				LogErrorTrace(__FILE__, __LINE__, _T("SetBeamOn command failed."));				return false;			}			return true;        }        return false;    }    bool OxfordImpl::GetBeamBlank(bool& a_bBeamBlank)    {        if ( m_bInit)        {			if (!ManagedGlobals::oxfordController->GetBeamBlank(a_bBeamBlank))			{				LogErrorTrace(__FILE__, __LINE__, _T("GetBeamBlank command failed."));				return false;			}			return true;        }        return false;    }    bool OxfordImpl::SetBeamBlank(bool a_bBeamBlank)    {        if (m_bInit)        {			if (!ManagedGlobals::oxfordController->SetBeamBlank(a_bBeamBlank))			{				LogErrorTrace(__FILE__, __LINE__, _T("SetBeamBlank command failed."));				return false;			}			return true;        }        return false;    }    bool OxfordImpl::GetExternal(bool& a_bExternal)    {        if (m_bInit)        {			if (!ManagedGlobals::oxfordController->GetExternalScan(a_bExternal))			{				LogErrorTrace(__FILE__, __LINE__, _T("GetExternal command failed."));				return false;			}			return true;        }        return false;    }    bool OxfordImpl::SetExternal(bool a_bExternal)    {        if ( m_bInit)        {			if (!ManagedGlobals::oxfordController->SetExternalScan(a_bExternal))			{				LogErrorTrace(__FILE__, __LINE__, _T("SetExternal command failed."));				return false;			}			CString s = __T("");				if (a_bExternal)				{					s = __T("On");				}				else				{					s = __T("Off");				}			LogWarn(__FILE__, __LINE__, _T("SetExternal: ") +s);			return true;        }        return false;    }    bool OxfordImpl::CollectXrayData(const long a_nAcTime, long* a_pnCounts, DWORD a_nBufferSize)    {        if (m_bInit)        {			if (!ManagedGlobals::oxfordController->CollectXrayPoint(a_nAcTime, a_pnCounts, a_nBufferSize))			{				LogErrorTrace(__FILE__, __LINE__, _T("CollectXrayData command failed."));				return false;			}			return true;        }        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_bInit)        {			if (!ManagedGlobals::oxfordController->CollectXrayPoint(a_dPosX, a_dPosY, a_nAcTime, a_pnCounts, a_nBufferSize))			{				LogErrorTrace(__FILE__, __LINE__, _T("CollectXrayDataAtPos command failed."));				return false;			}			return true;        }        return false;    }	bool OxfordImpl::StopCollectXrayData()	{		if (m_bInit)		{			if (!ManagedGlobals::oxfordController->StopXrayCollecting())			{				LogErrorTrace(__FILE__, __LINE__, _T("stopxraycollecting command failed."));				return false;			}			return true;		}		return false;	}    	    bool OxfordImpl::SetBeamPosition(const double a_dPosX, const double a_dPosY)    {        if ( m_bInit)        {			if (!ManagedGlobals::oxfordController->SetBeamPosition(a_dPosX, a_dPosY))			{				LogErrorTrace(__FILE__, __LINE__, _T("SetBeamPosition command failed."));				return false;			}			return true;        }        return false;    }    bool OxfordImpl::SetScanSpeed(const long a_nMilliseconds)    {        if (m_bInit)        {			if (!ManagedGlobals::oxfordController->SetScanSpeed(a_nMilliseconds))			{				LogErrorTrace(__FILE__, __LINE__, _T("SetScanSpeed command failed."));				return false;			}			return true;        }        return false;    }    bool OxfordImpl::GetImageSize(long& a_nWidth, long& a_nHeight)    {        if ( m_bInit)        {			if (!ManagedGlobals::oxfordController->GetImageSize(a_nWidth, a_nHeight))			{				LogErrorTrace(__FILE__, __LINE__, _T("GetImageSize command failed."));				return false;			}			return true;        }        return false;    }    bool OxfordImpl::SetImageSize(const long a_nWidth, const long a_nHeight)    {        if (m_bInit)        {			if (!ManagedGlobals::oxfordController->SetImageSize(a_nWidth, a_nHeight))			{				LogErrorTrace(__FILE__, __LINE__, _T("SetImageSize command failed."));				return false;			}			return true;        }        return false;    }    bool OxfordImpl::CollectImage(BYTE* a_pImageBits)    {        if (m_bInit)        {			if (!ManagedGlobals::oxfordController->CollectImage(a_pImageBits))			{				LogErrorTrace(__FILE__, __LINE__, _T("CollectImage command failed."));				return false;			}			return true;        }        return false;    }	bool OxfordImpl::QuantifySpectrum(unsigned char* cResult)	{		if ( m_bInit)		{			if (!ManagedGlobals::oxfordController->QuantifySpectrum(cResult))			{				LogErrorTrace(__FILE__, __LINE__, _T("CollectImage command failed."));				return false;			}			return true;		}		return false;	}	BOOL OxfordImpl::GetXRayByPoints(CPosXraysList& a_listXrayPois, DWORD a_nACTimeMS)	{			// 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();			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 (!ManagedGlobals::oxfordController->CollectXrayPoints(a_nACTimeMS, segmentArray, nCollectCount, GENERALXRAYCHANNELS))			{				LogErrorTrace(__FILE__, __LINE__, _T("CollectXrayList command failed."));				return (int)OxfordWrapperErrorCode::COMMAND_FAILED;			}			// 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 (!ManagedGlobals::oxfordController->CollectXrayPoint(a_nACTimeMS, (long*)nChannelData, GENERALXRAYCHANNELS))					{						// error						CString s;						s.Format(_T("Call CollectXRayPoint 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;		// error, return false		return FALSE;	}	BOOL OxfordImpl::GetXRayByFeatures(CPosXraysList&a_listXrayPois, std::vector<BrukerFeature>& a_vFeatures, DWORD a_nXRayAQTime)	{					// 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 (!ManagedGlobals::oxfordController->CollectXrayFeatures(a_nXRayAQTime, features, nCollectCount, GENERALXRAYCHANNELS))			{				LogTrace(__FILE__, __LINE__, _T("COTSOxfordImpl::GetXRayArea failed"));				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 (!ManagedGlobals::oxfordController->CollectXrayPoint(a_nXRayAQTime, (long*)nChannelData, GENERALXRAYCHANNELS))					{						LogErrorTrace(__FILE__, __LINE__, _T("CollectXrayData command failed."));						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;			return TRUE;	}}
 |