OTSControlFunExport.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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 (!Init())
  88. {
  89. return false;
  90. }
  91. if (m_pSem->IsConnected())
  92. {
  93. return true;
  94. }
  95. BOOL bRev = m_pSem->Connect();
  96. if (bRev)
  97. {
  98. if (ScanInit() && EDSInit())
  99. {
  100. return true;
  101. }
  102. else
  103. {
  104. return false;
  105. }
  106. }
  107. return bRev;
  108. }
  109. bool COTSControlFunExport::DisconnectSem()
  110. {
  111. BOOL bRev = m_pSem->Disconnect();
  112. return bRev;
  113. }
  114. bool COTSControlFunExport::IsConnected()
  115. {
  116. BOOL bRev = false;
  117. if (m_pSem != nullptr)
  118. {
  119. bRev = m_pSem->IsConnected();
  120. }
  121. return bRev;
  122. }
  123. bool COTSControlFunExport::StartScanTable(int a_nMatrixIndex, unsigned int nP, cli::array<System::Int16>^ pnxx, cli::array<System::Int16>^ pnyy)
  124. {
  125. int* pnx = new int[nP];
  126. int* pny = new int[nP];
  127. memset(pnx, 0, sizeof(int) * nP);
  128. memset(pny, 0, sizeof(int) * nP);
  129. bool bRet = false;
  130. if (m_pScan->StartScanTable(a_nMatrixIndex, nP, pnx, pny))
  131. {
  132. for (int i = 0; i < (int)nP; i++)
  133. {
  134. pnxx[i] = pnx[i];
  135. pnyy[i] = pny[i];
  136. }
  137. bRet = true;
  138. }
  139. else
  140. {
  141. bRet = false;
  142. }
  143. delete[] pny;
  144. delete[] pnx;
  145. return bRet;
  146. }
  147. bool COTSControlFunExport::CollectSpectrum(unsigned long a_nMilliseconds, Point a_oPoint, cli::array<unsigned long>^% a_XrayData)
  148. {
  149. CPoint pt;
  150. pt.x = a_oPoint.X;
  151. pt.y = a_oPoint.Y;
  152. bool bRet = m_pEDS->CollectSpectrum(a_nMilliseconds, pt);
  153. DWORD* xrd;
  154. xrd = m_pEDS->GetXRayData();
  155. for (int i = 0; i < (int)EDSConst::XANA_CHANNELS; i++)
  156. {
  157. a_XrayData[i] = xrd[i];
  158. }
  159. return bRet;
  160. }
  161. bool COTSControlFunExport::CollectSpectrum(unsigned long a_nMilliseconds, cli::array<unsigned long>^% a_XrayData, unsigned long a_nBufferSize)
  162. {
  163. long* aData = new long[a_nBufferSize];
  164. memset(aData, 0, a_nBufferSize);
  165. bool bRet = m_pEDS->CollectSpectrum(a_nMilliseconds, aData, a_nBufferSize);
  166. for (int i = a_nBufferSize; i < (int)a_nBufferSize; i++)
  167. {
  168. a_XrayData[i] = aData[i];
  169. }
  170. delete[] aData;
  171. return bRet;
  172. }
  173. bool COTSControlFunExport::CollectSpectrum(unsigned long a_nMilliseconds, cli::array<unsigned long>^% a_XrayData)
  174. {
  175. bool bRet = m_pEDS->CollectSpectrum(a_nMilliseconds);
  176. DWORD* xrd;
  177. xrd = m_pEDS->GetXRayData();
  178. for (int i = 0; i < (int)EDSConst::XANA_CHANNELS; i++)
  179. {
  180. a_XrayData[i] = xrd[i];
  181. }
  182. return bRet;
  183. }
  184. bool COTSControlFunExport::GetXRayByPoints(unsigned long a_nMilliseconds, cli::array<CPosXrayClr^>^% a_XrayData, bool bQuant)
  185. {
  186. std::vector<CPosXrayPtr> listXRayPoints;
  187. for (int i = 0; i < a_XrayData->Length; i++)
  188. {
  189. listXRayPoints.push_back(a_XrayData[i]->GetPosXrayPtr());
  190. }
  191. // set get quantify info flag
  192. if (bQuant)
  193. {
  194. m_pEDS->SetQuantification(TRUE);
  195. }
  196. else
  197. {
  198. m_pEDS->SetQuantification(FALSE);
  199. }
  200. bool bRet = m_pEDS->GetXRayByPoints(listXRayPoints, a_nMilliseconds);
  201. return bRet;
  202. }
  203. bool COTSControlFunExport::GetXRayByPoints(unsigned long a_nMilliseconds, cli::array<Point>^ points, cli::array<COTSParticleClr^>^ parts, bool bQuant)
  204. {
  205. std::vector<CPosXrayPtr> listXRayPoints;
  206. for (int i = 0; i < points->Length; i++)
  207. {
  208. CPosXrayPtr pXRayPoint = parts[i]->GetXray()->GetPosXrayPtr();
  209. pXRayPoint->SetPosition(CPoint(points[i].X, points[i].Y));
  210. listXRayPoints.push_back(pXRayPoint);
  211. }
  212. if (bQuant)
  213. {
  214. m_pEDS->SetQuantification(TRUE);
  215. }
  216. else
  217. {
  218. m_pEDS->SetQuantification(FALSE);
  219. }
  220. bool bRet = m_pEDS->GetXRayByPoints(listXRayPoints, a_nMilliseconds);
  221. for (int i = 0; i < points->Length; i++)
  222. {
  223. auto part = parts[i]->GetOTSParticlePtr();
  224. part->SetXrayInfo(listXRayPoints[i]);
  225. }
  226. return bRet;
  227. }
  228. bool COTSControlFunExport::QuantifyXrayByParts(cli::array<COTSParticleClr^>^ parts)
  229. {
  230. std::vector<CPosXrayPtr> xrays;
  231. for (int i=0;i<parts->Length;i++)
  232. {
  233. xrays.push_back(parts[i]->GetXray()->GetPosXrayPtr());
  234. }
  235. auto bRet = m_pEDS->QuantifyXrayByParts(xrays);
  236. return bRet;
  237. }
  238. bool COTSControlFunExport::QuantifyXrayByPart(COTSParticleClr^ part)
  239. {
  240. auto xray = part->GetXray();
  241. auto bRet = m_pEDS->QuantifyXrayByPart(xray->GetPosXrayPtr());
  242. return bRet;
  243. }
  244. 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)
  245. {
  246. std::vector<CPosXrayPtr> listXRayPoints;
  247. long xraynum = features->Length;
  248. for (int i = 0; i < xraynum; i++)
  249. {
  250. CPosXrayPtr pXRayPoint = CPosXrayPtr(new CPosXray());
  251. listXRayPoints.push_back(pXRayPoint);
  252. }
  253. std::vector<BrukerFeature>listFeatures;
  254. for (int i = 0; i < xraynum; i++)
  255. {
  256. auto feature = features[i]->GetOTSFeaturePtr();
  257. auto otsSegs = feature->GetSegmentsList();
  258. BrukerSegment* segArray = new BrukerSegment[otsSegs.size()];
  259. for (int j = 0; j < otsSegs.size(); j++)
  260. {
  261. auto seg = otsSegs[j];
  262. BrukerSegment* oSegment = &segArray[j];
  263. oSegment->XCount = seg->GetLength();
  264. oSegment->XStart = seg->GetStart();
  265. oSegment->Y = seg->GetHeight();
  266. }
  267. BrukerFeature oFeature;
  268. oFeature.SegmentCount = otsSegs.size();
  269. oFeature.pSegment = segArray;
  270. listFeatures.push_back(oFeature);
  271. }
  272. // set get quantify info flag
  273. if (bQuant)
  274. {
  275. m_pEDS->SetQuantification(TRUE);
  276. }
  277. else
  278. {
  279. m_pEDS->SetQuantification(FALSE);
  280. }
  281. bool bRet = m_pEDS->GetXRayByFeatures(listXRayPoints, listFeatures, a_nMilliseconds);
  282. DWORD* xrd;
  283. for (int i = 0; i < xraynum; i++)
  284. {
  285. xrd = listXRayPoints[i]->GetXrayData();
  286. for (int j = 0; j < (int)EDSConst::XANA_CHANNELS; j++)
  287. {
  288. a_XrayData[i][j] = xrd[j];
  289. }
  290. 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!
  291. }
  292. if (bQuant)
  293. {
  294. for (int i = 0; i < features->Length; i++)
  295. {
  296. CElementChemistriesList& listElement = listXRayPoints[i]->GetElementQuantifyData();
  297. int nElementNum = (int)listElement.size();
  298. CString strElementResult = _T("");
  299. for (auto pElement : listElement)
  300. {
  301. CString strResult;
  302. CString strName = pElement->GetName();
  303. double dPercent = pElement->GetPercentage();
  304. strResult.Format(_T("%s: %f\n"), strName, dPercent);
  305. strElementResult += strResult;
  306. }
  307. a_strEleResult[i] = gcnew String(strElementResult);
  308. }
  309. }
  310. return bRet;
  311. }
  312. bool COTSControlFunExport::GetXRayByFeatures(unsigned long a_nMilliseconds, cli::array<COTSParticleClr^>^ parts, bool bQuant)
  313. {
  314. std::vector<CPosXrayPtr> listXRayPoints;
  315. long xraynum = parts->Length;
  316. for (int i = 0; i < xraynum; i++)
  317. {
  318. CPosXrayPtr pXRayPoint = parts[i]->GetXray()->GetPosXrayPtr();
  319. listXRayPoints.push_back(pXRayPoint);
  320. }
  321. std::vector<BrukerFeature>listFeatures;
  322. for (int i = 0; i < xraynum; i++)
  323. {
  324. auto feature = parts[i]->GetOTSParticlePtr()->GetFeature();
  325. auto otsSegs = feature->GetSegmentsList();
  326. BrukerSegment* segArray = new BrukerSegment[otsSegs.size()];
  327. for (int j = 0; j < otsSegs.size(); j++)
  328. {
  329. auto seg = otsSegs[j];
  330. BrukerSegment* oSegment = &segArray[j];
  331. oSegment->XCount = seg->GetLength();
  332. oSegment->XStart = seg->GetStart();
  333. oSegment->Y = seg->GetHeight();
  334. }
  335. BrukerFeature oFeature;
  336. oFeature.SegmentCount = otsSegs.size();
  337. oFeature.pSegment = segArray;
  338. listFeatures.push_back(oFeature);
  339. }
  340. // set get quantify info flag
  341. if (bQuant)
  342. {
  343. m_pEDS->SetQuantification(TRUE);
  344. }
  345. else
  346. {
  347. m_pEDS->SetQuantification(FALSE);
  348. }
  349. bool bRet = m_pEDS->GetXRayByFeatures(listXRayPoints, listFeatures, a_nMilliseconds);
  350. //DWORD* xrd;
  351. for (int i = 0; i < xraynum; i++)
  352. {
  353. auto part = parts[i]->GetOTSParticlePtr();
  354. part->SetXrayInfo(listXRayPoints[i]);
  355. 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!
  356. }
  357. return bRet;
  358. }
  359. bool COTSControlFunExport::GetXRayBySinglePoint(unsigned long a_nMilliseconds, Point point, cli::array<unsigned long>^% a_XrayData, String^% a_strEleResult, bool bQuant)
  360. {
  361. std::vector<CPosXrayPtr> listXRayPoints;
  362. CPosXrayPtr pXRayPoint = CPosXrayPtr(new CPosXray());
  363. pXRayPoint->SetPosition(CPoint(point.X, point.Y));
  364. listXRayPoints.push_back(pXRayPoint);
  365. // set get quantify info flag
  366. if (bQuant)
  367. {
  368. m_pEDS->SetQuantification(TRUE);
  369. }
  370. else
  371. {
  372. m_pEDS->SetQuantification(FALSE);
  373. }
  374. bool bRet = m_pEDS->GetXRayByPoints(listXRayPoints, a_nMilliseconds);
  375. DWORD* xrd;
  376. xrd = listXRayPoints[0]->GetXrayData();
  377. for (int j = 0; j < (int)EDSConst::XANA_CHANNELS; j++)
  378. {
  379. a_XrayData[j] = xrd[j];
  380. }
  381. if (bQuant)
  382. {
  383. CElementChemistriesList& listElement = listXRayPoints[0]->GetElementQuantifyData();
  384. int nElementNum = (int)listElement.size();
  385. CString strElementResult = _T("");
  386. for (auto pElement : listElement)
  387. {
  388. CString strResult;
  389. CString strName = pElement->GetName();
  390. double dPercent = pElement->GetPercentage();
  391. strResult.Format(_T("%s: %f\n"), strName, dPercent);
  392. strElementResult += strResult;
  393. }
  394. a_strEleResult = gcnew String(strElementResult);
  395. }
  396. return bRet;
  397. }
  398. bool COTSControlFunExport::GetXRayBySingleFeature(unsigned long a_nMilliseconds, COTSFeatureClr^ feature, cli::array<unsigned long>^% a_XrayData, String^% a_strEleResult, bool bQuant)
  399. {
  400. std::vector<CPosXrayPtr> listXRayPoints;
  401. CPosXrayPtr pXRayPoint = CPosXrayPtr(new CPosXray());
  402. listXRayPoints.push_back(pXRayPoint);
  403. std::vector<BrukerSegment> listSegment;
  404. std::vector<BrukerFeature>listFeatures;
  405. auto otsSegs = feature->GetSegmentsList();
  406. for (int j = 0; j < otsSegs->Count; j++)
  407. {
  408. auto seg = otsSegs[j];
  409. BrukerSegment oSegment;
  410. oSegment.XCount = seg->GetLength();
  411. oSegment.XStart = seg->GetStart();
  412. oSegment.Y = seg->GetHeight();
  413. listSegment.push_back(oSegment);
  414. }
  415. BrukerFeature ofeature;
  416. ofeature.SegmentCount = listSegment.size();
  417. ofeature.pSegment = &listSegment[0];
  418. listFeatures.push_back(ofeature);
  419. bool bRet = FALSE;
  420. // set get quantify info flag
  421. if (bQuant)
  422. {
  423. m_pEDS->SetQuantification(TRUE);
  424. }
  425. else
  426. {
  427. m_pEDS->SetQuantification(FALSE);
  428. }
  429. bRet = m_pEDS->GetXRayByFeatures(listXRayPoints, listFeatures, a_nMilliseconds);
  430. DWORD* xrd;
  431. pXRayPoint = listXRayPoints[0];
  432. xrd = pXRayPoint->GetXrayData();
  433. for (int i = 0; i < (int)EDSConst::XANA_CHANNELS; i++)
  434. {
  435. a_XrayData[i] = xrd[i];
  436. }
  437. if (bQuant)
  438. {
  439. CString strElementResult = listXRayPoints[0]->GetQuantifiedElementsStr();
  440. a_strEleResult = gcnew String(strElementResult);
  441. }
  442. return bRet;
  443. }
  444. 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)
  445. {
  446. std::vector<CPosXrayPtr> listXRayPoints;
  447. CPosXrayPtr pXRayPoint = CPosXrayPtr(new CPosXray());
  448. pXRayPoint->SetPosition(CPoint(a_BSEX, a_BSEY));
  449. listXRayPoints.push_back(pXRayPoint);
  450. m_pEDS->SetQuantification(TRUE);
  451. bool bRet = m_pEDS->GetXRayByPoints(listXRayPoints, a_nMilliseconds);
  452. DWORD* xrd;
  453. pXRayPoint = listXRayPoints[0];
  454. xrd = pXRayPoint->GetXrayData();
  455. // element quantify data
  456. a_nElementNum = (int)listXRayPoints[0]->GetElementQuantifyData().size();
  457. for (int i = 0; i < (int)EDSConst::XANA_CHANNELS; i++)
  458. {
  459. a_XrayData[i] = xrd[i];
  460. }
  461. CString strElementResult = listXRayPoints[0]->GetQuantifiedElementsStr();
  462. a_strResult = gcnew String(strElementResult);
  463. return bRet;
  464. }
  465. int COTSControlFunExport::AcquireBSEImage( cli::array<System::Byte>^% a_ImgData)
  466. {
  467. int bRet = 0;
  468. CSize sz;
  469. int height, width;
  470. CBSEImgPtr pbseImg = nullptr;
  471. pbseImg = m_pScan->AcquireBSEImage();
  472. if (pbseImg == nullptr)
  473. {
  474. return bRet;
  475. }
  476. sz = pbseImg->GetImageSize();
  477. height = sz.cx;
  478. width = sz.cy;
  479. bRet = height * width;
  480. BYTE* lpData = pbseImg->GetImageDataPointer();
  481. for (int i = 0; i < bRet; i++)
  482. {
  483. a_ImgData[i] = lpData[i];
  484. }
  485. return bRet;
  486. }
  487. int COTSControlFunExport::GetEDSControllerID(const CString& EDSControllerName)
  488. {
  489. char* lpEdsName[] = { "OffLine","Bruker"};
  490. int iLen = sizeof(lpEdsName) / sizeof(*lpEdsName);
  491. int i = 0;
  492. for (i = 0; i < iLen; i++)
  493. {
  494. if (0 == strcmp(lpEdsName[i], EDSControllerName))
  495. {
  496. return i;
  497. }
  498. }
  499. return -1;
  500. }
  501. void COTSControlFunExport::SetExpectCount(int expectcount)
  502. {
  503. m_pEDS->SetExpectCount(expectcount);
  504. }
  505. int COTSControlFunExport::GetExpectCount()
  506. {
  507. return m_pEDS->GetExpectCount();
  508. }
  509. }