OTSControlFunExport.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. #include "stdafx.h"
  2. #include "OTSControlFunExport.h"
  3. #include "Bruker\OTSSEMBruker.h"
  4. #include "Simulate\OTSSemSim.h"
  5. #include "Simulate\OTSEDSSim.h"
  6. #include "Simulate\OTSScanSim.h"
  7. #include "Bruker\OTSEDSBrucker.h"
  8. #include "Bruker\OTSScanBrucker.h"
  9. using namespace OTSController;
  10. namespace OTSCLRINTERFACE
  11. {
  12. typedef enum class EDSDevID
  13. {
  14. Invalid = -1,
  15. OFFLINE = 0,
  16. BRUKER = 1,
  17. OXFORD = 2,
  18. }EDS_DEV_ID;
  19. bool COTSControlFunExport::Init()
  20. {
  21. int iDevID = this->GetEDSControllerID(CString(m_DeviceType));
  22. if (nullptr == m_pSem)
  23. {
  24. switch (iDevID)
  25. {
  26. case (int)EDS_DEV_ID::BRUKER:
  27. {
  28. m_pSem = new COTSSEMBruker();
  29. break;
  30. }
  31. case (int)EDS_DEV_ID::OFFLINE:
  32. {
  33. m_pSem = new COTSSemSim();
  34. break;
  35. }
  36. default:
  37. break;
  38. }
  39. }
  40. if (nullptr == m_pScan)
  41. {
  42. switch (iDevID)
  43. {
  44. case (int)EDS_DEV_ID::BRUKER:
  45. {
  46. m_pScan = new COTSScanBrucker();
  47. break;
  48. }
  49. case (int)EDS_DEV_ID::OFFLINE:
  50. {
  51. m_pScan = new COTSScanSim();
  52. break;
  53. }
  54. default:
  55. break;
  56. }
  57. }
  58. if (nullptr == m_pEDS)
  59. {
  60. switch (iDevID)
  61. {
  62. case (int)EDS_DEV_ID::BRUKER:
  63. {
  64. m_pEDS = new COTSEDSBrucker();
  65. break;
  66. }
  67. case (int)EDS_DEV_ID::OFFLINE:
  68. {
  69. m_pEDS = new COTSEDSSim();
  70. break;
  71. }
  72. default:
  73. break;
  74. }
  75. }
  76. if (m_pSem == nullptr || m_pScan == nullptr || m_pEDS == nullptr)
  77. {
  78. return false;
  79. }
  80. else
  81. {
  82. return true;
  83. }
  84. }
  85. bool COTSControlFunExport::ConncetSem()
  86. {
  87. if (m_pSem->IsConnected())
  88. {
  89. return true;
  90. }
  91. BOOL bRev = m_pSem->Connect();
  92. if (bRev)
  93. {
  94. if (ScanInit() && EDSInit())
  95. {
  96. return true;
  97. }
  98. else
  99. {
  100. return false;
  101. }
  102. }
  103. return bRev;
  104. }
  105. bool COTSControlFunExport::DisconnectSem()
  106. {
  107. BOOL bRev = m_pSem->Disconnect();
  108. return bRev;
  109. }
  110. bool COTSControlFunExport::IsConnected()
  111. {
  112. BOOL bRev = false;
  113. if (m_pSem != nullptr)
  114. {
  115. bRev = m_pSem->IsConnected();
  116. }
  117. return bRev;
  118. }
  119. bool COTSControlFunExport::CollectSpectrum(unsigned long a_nMilliseconds, Point a_oPoint, cli::array<unsigned long>^% a_XrayData)
  120. {
  121. CPoint pt;
  122. pt.x = a_oPoint.X;
  123. pt.y = a_oPoint.Y;
  124. bool bRet = m_pEDS->CollectSpectrum(a_nMilliseconds, pt);
  125. DWORD* xrd;
  126. xrd = m_pEDS->GetXRayData();
  127. for (int i = 0; i < (int)EDSConst::XANA_CHANNELS; i++)
  128. {
  129. a_XrayData[i] = xrd[i];
  130. }
  131. return bRet;
  132. }
  133. bool COTSControlFunExport::CollectSpectrum(unsigned long a_nMilliseconds, cli::array<unsigned long>^% a_XrayData, unsigned long a_nBufferSize)
  134. {
  135. long* aData = new long[a_nBufferSize];
  136. memset(aData, 0, a_nBufferSize);
  137. bool bRet = m_pEDS->CollectSpectrum(a_nMilliseconds, aData, a_nBufferSize);
  138. for (int i = a_nBufferSize; i < (int)a_nBufferSize; i++)
  139. {
  140. a_XrayData[i] = aData[i];
  141. }
  142. delete[] aData;
  143. return bRet;
  144. }
  145. bool COTSControlFunExport::CollectSpectrum(unsigned long a_nMilliseconds, cli::array<unsigned long>^% a_XrayData)
  146. {
  147. bool bRet = m_pEDS->CollectSpectrum(a_nMilliseconds);
  148. DWORD* xrd;
  149. xrd = m_pEDS->GetXRayData();
  150. for (int i = 0; i < (int)EDSConst::XANA_CHANNELS; i++)
  151. {
  152. a_XrayData[i] = xrd[i];
  153. }
  154. return bRet;
  155. }
  156. bool COTSControlFunExport::GetXRayByPoints(unsigned long a_nMilliseconds, cli::array<CPosXrayClr^>^% a_XrayData, bool bQuant)
  157. {
  158. std::vector<CPosXrayPtr> listXRayPoints;
  159. for (int i = 0; i < a_XrayData->Length; i++)
  160. {
  161. listXRayPoints.push_back(a_XrayData[i]->GetPosXrayPtr());
  162. }
  163. // set get quantify info flag
  164. if (bQuant)
  165. {
  166. m_pEDS->SetQuantification(TRUE);
  167. }
  168. else
  169. {
  170. m_pEDS->SetQuantification(FALSE);
  171. }
  172. bool bRet = m_pEDS->GetXRayByPoints(listXRayPoints, a_nMilliseconds);
  173. return bRet;
  174. }
  175. bool COTSControlFunExport::GetXRayByPoints(unsigned long a_nMilliseconds, cli::array<Point>^ points, cli::array<COTSParticleClr^>^ parts, bool bQuant)
  176. {
  177. std::vector<CPosXrayPtr> listXRayPoints;
  178. for (int i = 0; i < points->Length; i++)
  179. {
  180. CPosXrayPtr pXRayPoint = parts[i]->GetXray()->GetPosXrayPtr();
  181. pXRayPoint->SetPosition(CPoint(points[i].X, points[i].Y));
  182. pXRayPoint->SetIndex(parts[i]->GetXray()->GetIndex());
  183. listXRayPoints.push_back(pXRayPoint);
  184. }
  185. if (bQuant)
  186. {
  187. m_pEDS->SetQuantification(TRUE);
  188. }
  189. else
  190. {
  191. m_pEDS->SetQuantification(FALSE);
  192. }
  193. bool bRet = m_pEDS->GetXRayByPoints(listXRayPoints, a_nMilliseconds);
  194. for (int i = 0; i < points->Length; i++)
  195. {
  196. auto part = parts[i]->GetOTSParticlePtr();
  197. part->SetXrayInfo(listXRayPoints[i]);
  198. }
  199. return bRet;
  200. }
  201. bool COTSControlFunExport::QuantifyXrayByParts(cli::array<COTSParticleClr^>^ parts)
  202. {
  203. std::vector<CPosXrayPtr> xrays;
  204. for (int i=0;i<parts->Length;i++)
  205. {
  206. xrays.push_back(parts[i]->GetXray()->GetPosXrayPtr());
  207. }
  208. auto bRet = m_pEDS->QuantifyXrays(xrays);
  209. return bRet;
  210. }
  211. bool COTSControlFunExport::QuantifyXrayByPart(COTSParticleClr^ part)
  212. {
  213. auto xray = part->GetXray();
  214. auto bRet = m_pEDS->QuantifyXray(xray->GetPosXrayPtr());
  215. return bRet;
  216. }
  217. 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)
  218. {
  219. std::vector<CPosXrayPtr> listXRayPoints;
  220. long xraynum = features->Length;
  221. for (int i = 0; i < xraynum; i++)
  222. {
  223. CPosXrayPtr pXRayPoint = CPosXrayPtr(new CPosXray());
  224. listXRayPoints.push_back(pXRayPoint);
  225. }
  226. std::vector<BrukerFeature>listFeatures;
  227. for (int i = 0; i < xraynum; i++)
  228. {
  229. auto feature = features[i]->GetOTSFeaturePtr();
  230. auto otsSegs = feature->GetSegmentsList();
  231. BrukerSegment* segArray = new BrukerSegment[otsSegs.size()];
  232. for (int j = 0; j < otsSegs.size(); j++)
  233. {
  234. auto seg = otsSegs[j];
  235. BrukerSegment* oSegment = &segArray[j];
  236. oSegment->XCount = seg->GetLength();
  237. oSegment->XStart = seg->GetStart();
  238. oSegment->Y = seg->GetHeight();
  239. }
  240. BrukerFeature oFeature;
  241. oFeature.SegmentCount = otsSegs.size();
  242. oFeature.pSegment = segArray;
  243. listFeatures.push_back(oFeature);
  244. }
  245. // set get quantify info flag
  246. if (bQuant)
  247. {
  248. m_pEDS->SetQuantification(TRUE);
  249. }
  250. else
  251. {
  252. m_pEDS->SetQuantification(FALSE);
  253. }
  254. bool bRet = m_pEDS->GetXRayByFeatures(listXRayPoints, listFeatures, a_nMilliseconds);
  255. DWORD* xrd;
  256. for (int i = 0; i < xraynum; i++)
  257. {
  258. xrd = listXRayPoints[i]->GetXrayData();
  259. for (int j = 0; j < (int)EDSConst::XANA_CHANNELS; j++)
  260. {
  261. a_XrayData[i][j] = xrd[j];
  262. }
  263. 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!
  264. }
  265. if (bQuant)
  266. {
  267. for (int i = 0; i < features->Length; i++)
  268. {
  269. CElementChemistriesList& listElement = listXRayPoints[i]->GetElementQuantifyData();
  270. int nElementNum = (int)listElement.size();
  271. CString strElementResult = _T("");
  272. for (auto pElement : listElement)
  273. {
  274. CString strResult;
  275. CString strName = pElement->GetName();
  276. double dPercent = pElement->GetPercentage();
  277. strResult.Format(_T("%s: %f\n"), strName, dPercent);
  278. strElementResult += strResult;
  279. }
  280. a_strEleResult[i] = gcnew String(strElementResult);
  281. }
  282. }
  283. return bRet;
  284. }
  285. bool COTSControlFunExport::GetXRayByFeatures(unsigned long a_nMilliseconds, cli::array<COTSParticleClr^>^ parts, bool bQuant)
  286. {
  287. std::vector<CPosXrayPtr> listXRayPoints;
  288. long xraynum = parts->Length;
  289. for (int i = 0; i < xraynum; i++)
  290. {
  291. CPosXrayPtr pXRayPoint = parts[i]->GetXray()->GetPosXrayPtr();
  292. pXRayPoint->SetPosition(CPoint(parts[i]->GetXRayPos()->X, parts[i]->GetXRayPos()->Y));
  293. listXRayPoints.push_back(pXRayPoint);
  294. }
  295. std::vector<BrukerFeature>listFeatures;
  296. for (int i = 0; i < xraynum; i++)
  297. {
  298. auto feature = parts[i]->GetOTSParticlePtr()->GetFeature();
  299. auto otsSegs = feature->GetSegmentsList();
  300. BrukerSegment* segArray = new BrukerSegment[otsSegs.size()];
  301. for (int j = 0; j < otsSegs.size(); j++)
  302. {
  303. auto seg = otsSegs[j];
  304. BrukerSegment* oSegment = &segArray[j];
  305. oSegment->XCount = seg->GetLength();
  306. oSegment->XStart = seg->GetStart();
  307. oSegment->Y = seg->GetHeight();
  308. }
  309. BrukerFeature oFeature;
  310. oFeature.SegmentCount = otsSegs.size();
  311. oFeature.pSegment = segArray;
  312. listFeatures.push_back(oFeature);
  313. }
  314. // set get quantify info flag
  315. if (bQuant)
  316. {
  317. m_pEDS->SetQuantification(TRUE);
  318. }
  319. else
  320. {
  321. m_pEDS->SetQuantification(FALSE);
  322. }
  323. bool bRet = m_pEDS->GetXRayByFeatures(listXRayPoints, listFeatures, a_nMilliseconds);
  324. //DWORD* xrd;
  325. for (int i = 0; i < xraynum; i++)
  326. {
  327. auto part = parts[i]->GetOTSParticlePtr();
  328. part->SetXrayInfo(listXRayPoints[i]);
  329. 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!
  330. }
  331. return bRet;
  332. }
  333. bool COTSControlFunExport::GetXRayBySinglePoint(unsigned long a_nMilliseconds, Point point, cli::array<unsigned long>^% a_XrayData, String^% a_strEleResult, bool bQuant)
  334. {
  335. std::vector<CPosXrayPtr> listXRayPoints;
  336. CPosXrayPtr pXRayPoint = CPosXrayPtr(new CPosXray());
  337. pXRayPoint->SetPosition(CPoint(point.X, point.Y));
  338. listXRayPoints.push_back(pXRayPoint);
  339. // set get quantify info flag
  340. if (bQuant)
  341. {
  342. m_pEDS->SetQuantification(TRUE);
  343. }
  344. else
  345. {
  346. m_pEDS->SetQuantification(FALSE);
  347. }
  348. bool bRet = m_pEDS->GetXRayByPoints(listXRayPoints, a_nMilliseconds);
  349. DWORD* xrd;
  350. xrd = listXRayPoints[0]->GetXrayData();
  351. for (int j = 0; j < (int)EDSConst::XANA_CHANNELS; j++)
  352. {
  353. a_XrayData[j] = xrd[j];
  354. }
  355. if (bQuant)
  356. {
  357. CElementChemistriesList& listElement = listXRayPoints[0]->GetElementQuantifyData();
  358. int nElementNum = (int)listElement.size();
  359. CString strElementResult = _T("");
  360. for (auto pElement : listElement)
  361. {
  362. CString strResult;
  363. CString strName = pElement->GetName();
  364. double dPercent = pElement->GetPercentage();
  365. strResult.Format(_T("%s: %f\n"), strName, dPercent);
  366. strElementResult += strResult;
  367. }
  368. a_strEleResult = gcnew String(strElementResult);
  369. }
  370. return bRet;
  371. }
  372. bool COTSControlFunExport::GetXRayBySingleFeature(unsigned long a_nMilliseconds, COTSFeatureClr^ feature, cli::array<unsigned long>^% a_XrayData, String^% a_strEleResult, bool bQuant)
  373. {
  374. std::vector<CPosXrayPtr> listXRayPoints;
  375. CPosXrayPtr pXRayPoint = CPosXrayPtr(new CPosXray());
  376. listXRayPoints.push_back(pXRayPoint);
  377. std::vector<BrukerSegment> listSegment;
  378. std::vector<BrukerFeature>listFeatures;
  379. auto otsSegs = feature->GetSegmentsList();
  380. for (int j = 0; j < otsSegs->Count; j++)
  381. {
  382. auto seg = otsSegs[j];
  383. BrukerSegment oSegment;
  384. oSegment.XCount = seg->GetLength();
  385. oSegment.XStart = seg->GetStart();
  386. oSegment.Y = seg->GetHeight();
  387. listSegment.push_back(oSegment);
  388. }
  389. BrukerFeature ofeature;
  390. ofeature.SegmentCount = listSegment.size();
  391. ofeature.pSegment = &listSegment[0];
  392. listFeatures.push_back(ofeature);
  393. bool bRet = FALSE;
  394. // set get quantify info flag
  395. if (bQuant)
  396. {
  397. m_pEDS->SetQuantification(TRUE);
  398. }
  399. else
  400. {
  401. m_pEDS->SetQuantification(FALSE);
  402. }
  403. bRet = m_pEDS->GetXRayByFeatures(listXRayPoints, listFeatures, a_nMilliseconds);
  404. DWORD* xrd;
  405. pXRayPoint = listXRayPoints[0];
  406. xrd = pXRayPoint->GetXrayData();
  407. for (int i = 0; i < (int)EDSConst::XANA_CHANNELS; i++)
  408. {
  409. a_XrayData[i] = xrd[i];
  410. }
  411. if (bQuant)
  412. {
  413. CString strElementResult = listXRayPoints[0]->GetQuantifiedElementsStr();
  414. a_strEleResult = gcnew String(strElementResult);
  415. }
  416. return bRet;
  417. }
  418. 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)
  419. {
  420. std::vector<CPosXrayPtr> listXRayPoints;
  421. CPosXrayPtr pXRayPoint = CPosXrayPtr(new CPosXray());
  422. pXRayPoint->SetPosition(CPoint(a_BSEX, a_BSEY));
  423. listXRayPoints.push_back(pXRayPoint);
  424. m_pEDS->SetQuantification(TRUE);
  425. bool bRet = m_pEDS->GetXRayByPoints(listXRayPoints, a_nMilliseconds);
  426. DWORD* xrd;
  427. pXRayPoint = listXRayPoints[0];
  428. xrd = pXRayPoint->GetXrayData();
  429. // element quantify data
  430. a_nElementNum = (int)listXRayPoints[0]->GetElementQuantifyData().size();
  431. for (int i = 0; i < (int)EDSConst::XANA_CHANNELS; i++)
  432. {
  433. a_XrayData[i] = xrd[i];
  434. }
  435. CString strElementResult = listXRayPoints[0]->GetQuantifiedElementsStr();
  436. a_strResult = gcnew String(strElementResult);
  437. return bRet;
  438. }
  439. int COTSControlFunExport::AcquireBSEImage( cli::array<System::Byte>^% a_ImgData)
  440. {
  441. int bRet = 0;
  442. CSize sz;
  443. int height, width;
  444. CBSEImgPtr pbseImg = nullptr;
  445. pbseImg = m_pScan->AcquireBSEImage();
  446. if (pbseImg == nullptr)
  447. {
  448. return bRet;
  449. }
  450. sz = pbseImg->GetImageSize();
  451. height = sz.cx;
  452. width = sz.cy;
  453. bRet = height * width;
  454. BYTE* lpData = pbseImg->GetImageDataPointer();
  455. for (int i = 0; i < bRet; i++)
  456. {
  457. a_ImgData[i] = lpData[i];
  458. }
  459. return bRet;
  460. }
  461. int COTSControlFunExport::GetEDSControllerID(const CString& EDSControllerName)
  462. {
  463. char* lpEdsName[] = { "OffLine","Bruker"};
  464. int iLen = sizeof(lpEdsName) / sizeof(*lpEdsName);
  465. int i = 0;
  466. for (i = 0; i < iLen; i++)
  467. {
  468. if (0 == strcmp(lpEdsName[i], EDSControllerName))
  469. {
  470. return i;
  471. }
  472. }
  473. return -1;
  474. }
  475. void COTSControlFunExport::SetExpectCount(int expectcount)
  476. {
  477. m_pEDS->SetExpectCount(expectcount);
  478. }
  479. int COTSControlFunExport::GetExpectCount()
  480. {
  481. return m_pEDS->GetExpectCount();
  482. }
  483. void COTSControlFunExport::SetQuantificationParam(bool ifauto, String^ knownelements)
  484. {
  485. CString knownele = knownelements;
  486. m_pEDS->SetQuantificationParam(ifauto, knownele);
  487. }
  488. }