| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634 | #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<unsigned long>^% 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<unsigned long>^% 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<unsigned long>^% 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<CPosXrayClr^>^% a_XrayData, bool bQuant)	{		std::vector<CPosXrayPtr> 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<Point>^ points, cli::array<COTSParticleClr^>^ parts,  bool bQuant)	{		std::vector<CPosXrayPtr> 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<COTSParticleClr^>^ parts)	{		std::vector<CPosXrayPtr> xrays;		for (int i=0;i<parts->Length;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<COTSFeatureClr^>^ features,  cli::array<cli::array<unsigned long>^>^% a_XrayData, cli::array<String^>^% a_strEleResult, bool bQuant)	{		std::vector<CPosXrayPtr> listXRayPoints;		long xraynum = features->Length;		for (int i = 0; i < xraynum; i++)		{			CPosXrayPtr pXRayPoint = CPosXrayPtr(new CPosXray());			listXRayPoints.push_back(pXRayPoint);		}				std::vector<BrukerFeature>listFeatures;		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<COTSParticleClr^>^ parts,  bool bQuant)	{		std::vector<CPosXrayPtr> 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::vector<BrukerFeature>listFeatures;		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<unsigned long>^% a_XrayData, String^% a_strEleResult, bool bQuant)	{		std::vector<CPosXrayPtr> 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<unsigned long>^% a_XrayData, String^% a_strEleResult, bool bQuant)	{		std::vector<CPosXrayPtr> listXRayPoints;		CPosXrayPtr pXRayPoint = CPosXrayPtr(new CPosXray());		listXRayPoints.push_back(pXRayPoint);		std::vector<BrukerSegment> listSegment;		std::vector<BrukerFeature>listFeatures;		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<unsigned long>^% a_XrayData, int^% a_nElementNum, String^% a_strResult)	{		std::vector<CPosXrayPtr> 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<System::Byte>^% 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);	}}
 |