OTSOxfordImpl.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. #include "stdafx.h"
  2. #include "OTSOxfordImpl.h"
  3. #include "ControllerHelper.h"
  4. #include "COTSUtilityDllFunExport.h"
  5. #include "OxfordImplConst.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #endif
  9. #include <Oxford/OxfordWrapper/OxfordControllerWrapper.h>
  10. namespace OTSController
  11. {
  12. OxfordImpl::OxfordImpl(void)
  13. {
  14. if (ManagedGlobals::oxfordController == nullptr)
  15. {
  16. ManagedGlobals::oxfordController = gcnew OxfordControllerWrapper();
  17. m_bInit = false;
  18. }
  19. }
  20. OxfordImpl::~OxfordImpl(void)
  21. {
  22. CloseClient();
  23. }
  24. void OxfordImpl::CloseClient(void)
  25. {
  26. ManagedGlobals::oxfordController->CloseClient();
  27. }
  28. bool OxfordImpl::Connect()
  29. {
  30. if (!m_bInit)
  31. {
  32. if (!ManagedGlobals::oxfordController->Init())
  33. {
  34. return false;
  35. }
  36. if (!ManagedGlobals::oxfordController->IsConnected())
  37. {
  38. return false;
  39. }
  40. m_bInit = true;
  41. }
  42. return m_bInit;
  43. }
  44. bool OxfordImpl::DisConnect()
  45. {
  46. if (m_bInit)
  47. {
  48. CloseClient();
  49. m_bInit = false;
  50. }
  51. return true;
  52. }
  53. bool OxfordImpl::IsConnected()
  54. {
  55. if (m_bInit)
  56. {
  57. if (!ManagedGlobals::oxfordController->IsConnected())
  58. {
  59. return false;
  60. }
  61. return true;
  62. }
  63. return false;
  64. }
  65. bool OxfordImpl::GetPositionXY(double& a_dPosX, double& a_dPosY)
  66. {
  67. if (m_bInit)
  68. {
  69. if (!ManagedGlobals::oxfordController->GetPositionXY(a_dPosX, a_dPosY))
  70. {
  71. LogErrorTrace(__FILE__, __LINE__, _T("GetPositionXY command failed."));
  72. return false;
  73. }
  74. return true;
  75. }
  76. return false;
  77. }
  78. bool OxfordImpl::SetPositionXY(double a_dPosX, double a_dPosY)
  79. {
  80. if (m_bInit)
  81. {
  82. if (!ManagedGlobals::oxfordController->SetPositionXY(a_dPosX, a_dPosY))
  83. {
  84. LogErrorTrace(__FILE__, __LINE__, _T("SetPositionXY command failed."));
  85. return false;
  86. }
  87. return true;
  88. }
  89. return false;
  90. }
  91. bool OxfordImpl::GetWorkingDistance(double& a_dWorkingDistance)
  92. {
  93. if ( m_bInit)
  94. {
  95. if (!ManagedGlobals::oxfordController->GetWorkingDistance(a_dWorkingDistance))
  96. {
  97. LogErrorTrace(__FILE__, __LINE__, _T("GetWorkingDistance command failed."));
  98. return false;
  99. }
  100. return true;
  101. }
  102. return false;
  103. }
  104. bool OxfordImpl::SetWorkingDistance(double a_dWorkingDistance)
  105. {
  106. if (m_bInit)
  107. {
  108. if (!ManagedGlobals::oxfordController->SetWorkingDistance(a_dWorkingDistance))
  109. {
  110. LogErrorTrace(__FILE__, __LINE__, _T("SetWorkingDistance command failed."));
  111. return false;
  112. }
  113. return true;
  114. }
  115. return false;
  116. }
  117. bool OxfordImpl::GetMagnification(double& a_dMagnification)
  118. {
  119. if (m_bInit)
  120. {
  121. if (!ManagedGlobals::oxfordController->GetMagnification(a_dMagnification))
  122. {
  123. LogErrorTrace(__FILE__, __LINE__, _T("GetMagnification command failed."));
  124. return false;
  125. }
  126. return true;
  127. }
  128. return false;
  129. }
  130. bool OxfordImpl::SetMagnification(double a_dMagnification)
  131. {
  132. if (m_bInit)
  133. {
  134. if (!ManagedGlobals::oxfordController->SetMagnification(a_dMagnification))
  135. {
  136. LogErrorTrace(__FILE__, __LINE__, _T("SetMagnification command failed."));
  137. return false;
  138. }
  139. return true;
  140. }
  141. return false;
  142. }
  143. bool OxfordImpl::GetHighVoltage(double& a_dHighVoltage)
  144. {
  145. if ( m_bInit)
  146. {
  147. if (!ManagedGlobals::oxfordController->GetHighVoltage(a_dHighVoltage))
  148. {
  149. LogErrorTrace(__FILE__, __LINE__, _T("GetHighVoltage command failed."));
  150. return false;
  151. }
  152. return true;
  153. }
  154. return false;
  155. }
  156. bool OxfordImpl::SetHighVoltage(double a_dHighVoltage)
  157. {
  158. if (m_bInit)
  159. {
  160. if (!ManagedGlobals::oxfordController->SetHighVoltage(a_dHighVoltage))
  161. {
  162. LogErrorTrace(__FILE__, __LINE__, _T("SetHighVoltage command failed."));
  163. return false;
  164. }
  165. return true;
  166. }
  167. return false;
  168. }
  169. bool OxfordImpl::GetBeamOn(bool& a_bBeamOn)
  170. {
  171. if (m_bInit)
  172. {
  173. if (!ManagedGlobals::oxfordController->GetBeamOn(a_bBeamOn))
  174. {
  175. LogErrorTrace(__FILE__, __LINE__, _T("GetBeamOn command failed."));
  176. return false;
  177. }
  178. return true;
  179. }
  180. return false;
  181. }
  182. bool OxfordImpl::SetBeamOn(bool a_bBeamOn)
  183. {
  184. if ( m_bInit)
  185. {
  186. if (!ManagedGlobals::oxfordController->SetBeamOn(a_bBeamOn))
  187. {
  188. LogErrorTrace(__FILE__, __LINE__, _T("SetBeamOn command failed."));
  189. return false;
  190. }
  191. return true;
  192. }
  193. return false;
  194. }
  195. bool OxfordImpl::GetBeamBlank(bool& a_bBeamBlank)
  196. {
  197. if ( m_bInit)
  198. {
  199. if (!ManagedGlobals::oxfordController->GetBeamBlank(a_bBeamBlank))
  200. {
  201. LogErrorTrace(__FILE__, __LINE__, _T("GetBeamBlank command failed."));
  202. return false;
  203. }
  204. return true;
  205. }
  206. return false;
  207. }
  208. bool OxfordImpl::SetBeamBlank(bool a_bBeamBlank)
  209. {
  210. if (m_bInit)
  211. {
  212. if (!ManagedGlobals::oxfordController->SetBeamBlank(a_bBeamBlank))
  213. {
  214. LogErrorTrace(__FILE__, __LINE__, _T("SetBeamBlank command failed."));
  215. return false;
  216. }
  217. return true;
  218. }
  219. return false;
  220. }
  221. bool OxfordImpl::GetExternal(bool& a_bExternal)
  222. {
  223. if (m_bInit)
  224. {
  225. if (!ManagedGlobals::oxfordController->GetExternalScan(a_bExternal))
  226. {
  227. LogErrorTrace(__FILE__, __LINE__, _T("GetExternal command failed."));
  228. return false;
  229. }
  230. return true;
  231. }
  232. return false;
  233. }
  234. bool OxfordImpl::SetExternal(bool a_bExternal)
  235. {
  236. if ( m_bInit)
  237. {
  238. if (!ManagedGlobals::oxfordController->SetExternalScan(a_bExternal))
  239. {
  240. LogErrorTrace(__FILE__, __LINE__, _T("SetExternal command failed."));
  241. return false;
  242. }
  243. CString s = __T("");
  244. if (a_bExternal)
  245. {
  246. s = __T("On");
  247. }
  248. else
  249. {
  250. s = __T("Off");
  251. }
  252. LogWarn(__FILE__, __LINE__, _T("SetExternal: ") +s);
  253. return true;
  254. }
  255. return false;
  256. }
  257. bool OxfordImpl::CollectXrayData(const long a_nAcTime, long* a_pnCounts, DWORD a_nBufferSize)
  258. {
  259. if (m_bInit)
  260. {
  261. if (!ManagedGlobals::oxfordController->CollectXrayPoint(a_nAcTime, a_pnCounts, a_nBufferSize))
  262. {
  263. LogErrorTrace(__FILE__, __LINE__, _T("CollectXrayData command failed."));
  264. return false;
  265. }
  266. return true;
  267. }
  268. return false;
  269. }
  270. bool OxfordImpl::CollectXrayDataAtPos(const double a_dPosX, const double a_dPosY, const long a_nAcTime, long* a_pnCounts, DWORD a_nBufferSize)
  271. {
  272. if (m_bInit)
  273. {
  274. if (!ManagedGlobals::oxfordController->CollectXrayPoint(a_dPosX, a_dPosY, a_nAcTime, a_pnCounts, a_nBufferSize))
  275. {
  276. LogErrorTrace(__FILE__, __LINE__, _T("CollectXrayDataAtPos command failed."));
  277. return false;
  278. }
  279. return true;
  280. }
  281. return false;
  282. }
  283. bool OxfordImpl::StopCollectXrayData()
  284. {
  285. if (m_bInit)
  286. {
  287. if (!ManagedGlobals::oxfordController->StopXrayCollecting())
  288. {
  289. LogErrorTrace(__FILE__, __LINE__, _T("stopxraycollecting command failed."));
  290. return false;
  291. }
  292. return true;
  293. }
  294. return false;
  295. }
  296. bool OxfordImpl::SetBeamPosition(const double a_dPosX, const double a_dPosY)
  297. {
  298. if ( m_bInit)
  299. {
  300. if (!ManagedGlobals::oxfordController->SetBeamPosition(a_dPosX, a_dPosY))
  301. {
  302. LogErrorTrace(__FILE__, __LINE__, _T("SetBeamPosition command failed."));
  303. return false;
  304. }
  305. return true;
  306. }
  307. return false;
  308. }
  309. bool OxfordImpl::SetScanSpeed(const long a_nMilliseconds)
  310. {
  311. if (m_bInit)
  312. {
  313. if (!ManagedGlobals::oxfordController->SetScanSpeed(a_nMilliseconds))
  314. {
  315. LogErrorTrace(__FILE__, __LINE__, _T("SetScanSpeed command failed."));
  316. return false;
  317. }
  318. return true;
  319. }
  320. return false;
  321. }
  322. bool OxfordImpl::GetImageSize(long& a_nWidth, long& a_nHeight)
  323. {
  324. if ( m_bInit)
  325. {
  326. if (!ManagedGlobals::oxfordController->GetImageSize(a_nWidth, a_nHeight))
  327. {
  328. LogErrorTrace(__FILE__, __LINE__, _T("GetImageSize command failed."));
  329. return false;
  330. }
  331. return true;
  332. }
  333. return false;
  334. }
  335. bool OxfordImpl::SetImageSize(const long a_nWidth, const long a_nHeight)
  336. {
  337. if (m_bInit)
  338. {
  339. if (!ManagedGlobals::oxfordController->SetImageSize(a_nWidth, a_nHeight))
  340. {
  341. LogErrorTrace(__FILE__, __LINE__, _T("SetImageSize command failed."));
  342. return false;
  343. }
  344. return true;
  345. }
  346. return false;
  347. }
  348. bool OxfordImpl::CollectImage(BYTE* a_pImageBits)
  349. {
  350. if (m_bInit)
  351. {
  352. if (!ManagedGlobals::oxfordController->CollectImage(a_pImageBits))
  353. {
  354. LogErrorTrace(__FILE__, __LINE__, _T("CollectImage command failed."));
  355. return false;
  356. }
  357. return true;
  358. }
  359. return false;
  360. }
  361. bool OxfordImpl::QuantifySpectrum(unsigned char* cResult)
  362. {
  363. if ( m_bInit)
  364. {
  365. if (!ManagedGlobals::oxfordController->QuantifySpectrum(cResult))
  366. {
  367. LogErrorTrace(__FILE__, __LINE__, _T("CollectImage command failed."));
  368. return false;
  369. }
  370. return true;
  371. }
  372. return false;
  373. }
  374. BOOL OxfordImpl::GetXRayByPoints(CPosXraysList& a_listXrayPois, DWORD a_nACTimeMS)
  375. {
  376. // do nothing if points list is empty
  377. if (a_listXrayPois.empty())
  378. {
  379. // points list is empty
  380. LogTrace(__FILE__, __LINE__, _T("COTSOxfordImpl::GetXRayByPoints: poits list is empty."));
  381. return TRUE;
  382. }
  383. // create array of BrukerSegment
  384. long nCollectCount = (long)a_listXrayPois.size();
  385. OxfordXrayData* segmentArray(new OxfordXrayData[nCollectCount]);
  386. for (int i = 0; i < nCollectCount; ++i)
  387. {
  388. CPoint poi = a_listXrayPois[i]->GetPosition();
  389. segmentArray[i].m_nPosX = poi.x;
  390. segmentArray[i].m_nPosY = poi.y;
  391. }
  392. if (!ManagedGlobals::oxfordController->CollectXrayPoints(a_nACTimeMS, segmentArray, nCollectCount, GENERALXRAYCHANNELS))
  393. {
  394. LogErrorTrace(__FILE__, __LINE__, _T("CollectXrayList command failed."));
  395. return (int)OxfordWrapperErrorCode::COMMAND_FAILED;
  396. }
  397. // get the specs for a_vXPoints
  398. for (int i = 0; i < nCollectCount; ++i)
  399. {
  400. // set spectrum data for the x-ray point
  401. a_listXrayPois[i]->SetXrayData((DWORD*)(segmentArray[i].m_pXrayData));
  402. if (m_bDoQuantification)
  403. {
  404. // quantify the spectrum
  405. CElementChemistriesList vElement = CElement::ExtractElementChemistrys(CControllerHelper::CharToString((const char*)segmentArray[i].m_strElementResult));
  406. a_listXrayPois[i]->SetElementQuantifyData(vElement);
  407. }
  408. }
  409. for (int i = 0; i < nCollectCount; ++i)
  410. {
  411. // check spectrum
  412. DWORD nTatolXrayCount = a_listXrayPois[i]->GetTotalCount();
  413. if (nTatolXrayCount < 20)
  414. {
  415. // captured an empty spectrum
  416. CPoint poi = a_listXrayPois[i]->GetPosition();
  417. // try to redo x-ray collection at the position
  418. static DWORD nChannelData[GENERALXRAYCHANNELS];
  419. memset(nChannelData, 0, sizeof(DWORD) * GENERALXRAYCHANNELS);
  420. if (!ManagedGlobals::oxfordController->CollectXrayPoint(a_nACTimeMS, (long*)nChannelData, GENERALXRAYCHANNELS))
  421. {
  422. // error
  423. CString s;
  424. s.Format(_T("Call CollectXRayPoint failed: index = %d(x:%d, y:%d))"), i, poi.x, poi.y);
  425. LogErrorTrace(__FILE__,__LINE__,s);
  426. return false;
  427. }
  428. // set spectrum with new spectrum
  429. a_listXrayPois[i]->SetXrayData(nChannelData);
  430. nTatolXrayCount = a_listXrayPois[i]->GetTotalCount();
  431. if (nTatolXrayCount < 20)
  432. {
  433. CString s1;
  434. s1.Format(_T("Single point spectrum still low count (%d counts), index = %d(x:%d, y:%d.) This could be caused by charging."),
  435. nTatolXrayCount, i, poi.x, poi.y);
  436. LogTrace(__FILE__, __LINE__, s1);
  437. }
  438. else
  439. {
  440. CString s2;
  441. s2.Format(_T("Single point spectrum collected successfully (%d counts), index = %d(x:%d, y:%d.)"),
  442. nTatolXrayCount, i, poi.x, poi.y);
  443. LogTrace(__FILE__, __LINE__, s2);
  444. }
  445. }
  446. }
  447. delete segmentArray;
  448. // ok return TRUE
  449. return TRUE;
  450. // error, return false
  451. return FALSE;
  452. }
  453. BOOL OxfordImpl::GetXRayByFeatures(CPosXraysList&a_listXrayPois, std::vector<BrukerFeature>& a_vFeatures, DWORD a_nXRayAQTime)
  454. {
  455. // do nothing if points list is empty
  456. if (a_listXrayPois.empty())
  457. {
  458. // points list is empty
  459. LogTrace(__FILE__, __LINE__, _T("COTSOxfordImpl::GetXRayByPoints: poits list is empty."));
  460. return TRUE;
  461. }
  462. if (a_listXrayPois.size() != a_vFeatures.size())
  463. {
  464. return FALSE;
  465. }
  466. // create array of BrukerSegment
  467. long nCollectCount = (long)a_listXrayPois.size();
  468. //convert the brukerfeature to oxfordfeature
  469. OxfordXrayData* features(new OxfordXrayData[nCollectCount]);
  470. int nTotalNum = 0;
  471. for (size_t i = 0; i < a_listXrayPois.size(); i++)
  472. {
  473. long nSegmentCount = (long)a_vFeatures[i].SegmentCount;
  474. features[i].m_nChordNum = nSegmentCount;
  475. long nPixelCount = 0;
  476. if (nSegmentCount > 0)
  477. {
  478. BrukerSegment* segs = a_vFeatures[i].pSegment;
  479. for (int j = 0; j < nSegmentCount; j++)
  480. {
  481. features[i].m_ChordList[j].m_nX = segs[j].XStart;
  482. features[i].m_ChordList[j].m_nY = segs[j].Y;
  483. features[i].m_ChordList[j].m_nLength = segs[j].XCount;
  484. nPixelCount += segs[j].XCount;
  485. }
  486. features[i].m_nPixelNum = nPixelCount;
  487. }
  488. else
  489. {
  490. // will generate according to the x-ray position
  491. // this shouldn't happen
  492. }
  493. nTotalNum += nPixelCount;
  494. }
  495. int a_nACTimeMS = a_nXRayAQTime / nTotalNum;
  496. if (!ManagedGlobals::oxfordController->CollectXrayFeatures(a_nXRayAQTime, features, nCollectCount, GENERALXRAYCHANNELS))
  497. {
  498. LogTrace(__FILE__, __LINE__, _T("COTSOxfordImpl::GetXRayArea failed"));
  499. return false;
  500. }
  501. // get the specs for a_vXPoints
  502. for (int i = 0; i < nCollectCount; ++i)
  503. {
  504. // set spectrum data for the x-ray point
  505. a_listXrayPois[i]->SetXrayData((DWORD*)(features[i].m_pXrayData));
  506. // set spectrum data for the x-ray point
  507. if (m_bDoQuantification)
  508. {
  509. // quantify the spectrum
  510. CElementChemistriesList vElement = CElement::ExtractElementChemistrys(CControllerHelper::CharToString((const char*)features[i].m_strElementResult));
  511. a_listXrayPois[i]->SetElementQuantifyData(vElement);
  512. }
  513. }
  514. for (int i = 0; i < nCollectCount; ++i)
  515. {
  516. // check spectrum
  517. DWORD nTatolXrayCount = a_listXrayPois[i]->GetTotalCount();
  518. if (nTatolXrayCount < 20)
  519. {
  520. // captured an empty spectrum
  521. CPoint poi = a_listXrayPois[i]->GetPosition();
  522. // try to redo x-ray collection at the position
  523. static DWORD nChannelData[GENERALXRAYCHANNELS];
  524. memset(nChannelData, 0, sizeof(DWORD) * GENERALXRAYCHANNELS);
  525. if (!ManagedGlobals::oxfordController->CollectXrayPoint(a_nXRayAQTime, (long*)nChannelData, GENERALXRAYCHANNELS))
  526. {
  527. LogErrorTrace(__FILE__, __LINE__, _T("CollectXrayData command failed."));
  528. return false;
  529. }
  530. // set spectrum with new spectrum
  531. a_listXrayPois[i]->SetXrayData(nChannelData);
  532. nTatolXrayCount = a_listXrayPois[i]->GetTotalCount();
  533. if (nTatolXrayCount < 20)
  534. {
  535. /*LogWarn(_T("Single point spectrum still low count (%d counts), index = %d(x:%d, y:%d.) This could be caused by charging."),
  536. nTatolXrayCount, i, poi.x, poi.y);*/
  537. }
  538. else
  539. {
  540. /*LogWarn(_T("Single point spectrum collected successfully (%d counts), index = %d(x:%d, y:%d.)"),
  541. nTatolXrayCount, i, poi.x, poi.y);*/
  542. }
  543. }
  544. }
  545. delete features;
  546. return TRUE;
  547. }
  548. }