OTSControlFunExport.cpp 13 KB

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