OTSControlFunExport.cpp 13 KB

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