OTSControlFunExport.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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. listXRayPoints.push_back(pXRayPoint);
  293. }
  294. std::vector<BrukerFeature>listFeatures;
  295. for (int i = 0; i < xraynum; i++)
  296. {
  297. auto feature = parts[i]->GetOTSParticlePtr()->GetFeature();
  298. auto otsSegs = feature->GetSegmentsList();
  299. BrukerSegment* segArray = new BrukerSegment[otsSegs.size()];
  300. for (int j = 0; j < otsSegs.size(); j++)
  301. {
  302. auto seg = otsSegs[j];
  303. BrukerSegment* oSegment = &segArray[j];
  304. oSegment->XCount = seg->GetLength();
  305. oSegment->XStart = seg->GetStart();
  306. oSegment->Y = seg->GetHeight();
  307. }
  308. BrukerFeature oFeature;
  309. oFeature.SegmentCount = otsSegs.size();
  310. oFeature.pSegment = segArray;
  311. listFeatures.push_back(oFeature);
  312. }
  313. // set get quantify info flag
  314. if (bQuant)
  315. {
  316. m_pEDS->SetQuantification(TRUE);
  317. }
  318. else
  319. {
  320. m_pEDS->SetQuantification(FALSE);
  321. }
  322. bool bRet = m_pEDS->GetXRayByFeatures(listXRayPoints, listFeatures, a_nMilliseconds);
  323. //DWORD* xrd;
  324. for (int i = 0; i < xraynum; i++)
  325. {
  326. auto part = parts[i]->GetOTSParticlePtr();
  327. part->SetXrayInfo(listXRayPoints[i]);
  328. 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!
  329. }
  330. return bRet;
  331. }
  332. bool COTSControlFunExport::GetXRayBySinglePoint(unsigned long a_nMilliseconds, Point point, cli::array<unsigned long>^% a_XrayData, String^% a_strEleResult, bool bQuant)
  333. {
  334. std::vector<CPosXrayPtr> listXRayPoints;
  335. CPosXrayPtr pXRayPoint = CPosXrayPtr(new CPosXray());
  336. pXRayPoint->SetPosition(CPoint(point.X, point.Y));
  337. listXRayPoints.push_back(pXRayPoint);
  338. // set get quantify info flag
  339. if (bQuant)
  340. {
  341. m_pEDS->SetQuantification(TRUE);
  342. }
  343. else
  344. {
  345. m_pEDS->SetQuantification(FALSE);
  346. }
  347. bool bRet = m_pEDS->GetXRayByPoints(listXRayPoints, a_nMilliseconds);
  348. DWORD* xrd;
  349. xrd = listXRayPoints[0]->GetXrayData();
  350. for (int j = 0; j < (int)EDSConst::XANA_CHANNELS; j++)
  351. {
  352. a_XrayData[j] = xrd[j];
  353. }
  354. if (bQuant)
  355. {
  356. CElementChemistriesList& listElement = listXRayPoints[0]->GetElementQuantifyData();
  357. int nElementNum = (int)listElement.size();
  358. CString strElementResult = _T("");
  359. for (auto pElement : listElement)
  360. {
  361. CString strResult;
  362. CString strName = pElement->GetName();
  363. double dPercent = pElement->GetPercentage();
  364. strResult.Format(_T("%s: %f\n"), strName, dPercent);
  365. strElementResult += strResult;
  366. }
  367. a_strEleResult = gcnew String(strElementResult);
  368. }
  369. return bRet;
  370. }
  371. bool COTSControlFunExport::GetXRayBySingleFeature(unsigned long a_nMilliseconds, COTSFeatureClr^ feature, cli::array<unsigned long>^% a_XrayData, String^% a_strEleResult, bool bQuant)
  372. {
  373. std::vector<CPosXrayPtr> listXRayPoints;
  374. CPosXrayPtr pXRayPoint = CPosXrayPtr(new CPosXray());
  375. listXRayPoints.push_back(pXRayPoint);
  376. std::vector<BrukerSegment> listSegment;
  377. std::vector<BrukerFeature>listFeatures;
  378. auto otsSegs = feature->GetSegmentsList();
  379. for (int j = 0; j < otsSegs->Count; j++)
  380. {
  381. auto seg = otsSegs[j];
  382. BrukerSegment oSegment;
  383. oSegment.XCount = seg->GetLength();
  384. oSegment.XStart = seg->GetStart();
  385. oSegment.Y = seg->GetHeight();
  386. listSegment.push_back(oSegment);
  387. }
  388. BrukerFeature ofeature;
  389. ofeature.SegmentCount = listSegment.size();
  390. ofeature.pSegment = &listSegment[0];
  391. listFeatures.push_back(ofeature);
  392. bool bRet = FALSE;
  393. // set get quantify info flag
  394. if (bQuant)
  395. {
  396. m_pEDS->SetQuantification(TRUE);
  397. }
  398. else
  399. {
  400. m_pEDS->SetQuantification(FALSE);
  401. }
  402. bRet = m_pEDS->GetXRayByFeatures(listXRayPoints, listFeatures, a_nMilliseconds);
  403. DWORD* xrd;
  404. pXRayPoint = listXRayPoints[0];
  405. xrd = pXRayPoint->GetXrayData();
  406. for (int i = 0; i < (int)EDSConst::XANA_CHANNELS; i++)
  407. {
  408. a_XrayData[i] = xrd[i];
  409. }
  410. if (bQuant)
  411. {
  412. CString strElementResult = listXRayPoints[0]->GetQuantifiedElementsStr();
  413. a_strEleResult = gcnew String(strElementResult);
  414. }
  415. return bRet;
  416. }
  417. 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)
  418. {
  419. std::vector<CPosXrayPtr> listXRayPoints;
  420. CPosXrayPtr pXRayPoint = CPosXrayPtr(new CPosXray());
  421. pXRayPoint->SetPosition(CPoint(a_BSEX, a_BSEY));
  422. listXRayPoints.push_back(pXRayPoint);
  423. m_pEDS->SetQuantification(TRUE);
  424. bool bRet = m_pEDS->GetXRayByPoints(listXRayPoints, a_nMilliseconds);
  425. DWORD* xrd;
  426. pXRayPoint = listXRayPoints[0];
  427. xrd = pXRayPoint->GetXrayData();
  428. // element quantify data
  429. a_nElementNum = (int)listXRayPoints[0]->GetElementQuantifyData().size();
  430. for (int i = 0; i < (int)EDSConst::XANA_CHANNELS; i++)
  431. {
  432. a_XrayData[i] = xrd[i];
  433. }
  434. CString strElementResult = listXRayPoints[0]->GetQuantifiedElementsStr();
  435. a_strResult = gcnew String(strElementResult);
  436. return bRet;
  437. }
  438. int COTSControlFunExport::AcquireBSEImage( cli::array<System::Byte>^% a_ImgData)
  439. {
  440. int bRet = 0;
  441. CSize sz;
  442. int height, width;
  443. CBSEImgPtr pbseImg = nullptr;
  444. pbseImg = m_pScan->AcquireBSEImage();
  445. if (pbseImg == nullptr)
  446. {
  447. return bRet;
  448. }
  449. sz = pbseImg->GetImageSize();
  450. height = sz.cx;
  451. width = sz.cy;
  452. bRet = height * width;
  453. BYTE* lpData = pbseImg->GetImageDataPointer();
  454. for (int i = 0; i < bRet; i++)
  455. {
  456. a_ImgData[i] = lpData[i];
  457. }
  458. return bRet;
  459. }
  460. int COTSControlFunExport::GetEDSControllerID(const CString& EDSControllerName)
  461. {
  462. char* lpEdsName[] = { "OffLine","Bruker"};
  463. int iLen = sizeof(lpEdsName) / sizeof(*lpEdsName);
  464. int i = 0;
  465. for (i = 0; i < iLen; i++)
  466. {
  467. if (0 == strcmp(lpEdsName[i], EDSControllerName))
  468. {
  469. return i;
  470. }
  471. }
  472. return -1;
  473. }
  474. void COTSControlFunExport::SetExpectCount(int expectcount)
  475. {
  476. m_pEDS->SetExpectCount(expectcount);
  477. }
  478. int COTSControlFunExport::GetExpectCount()
  479. {
  480. return m_pEDS->GetExpectCount();
  481. }
  482. }