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