TaskWindowExecute.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. #include "stdafx.h"
  2. #include <Mmsystem.h>
  3. #pragma comment(lib, "winmm.lib ")
  4. #include "VisionPlus.h"
  5. #include "TaskWindow.h"
  6. #include "ToolWindow.h"
  7. #include "MainFrm.h"
  8. #include "GlobalSetting.h"
  9. #include "Utility.h"
  10. #include "TaskControlWindow.h"
  11. #include "HWindow.h"
  12. #include "Preferences.h"
  13. //// 禁用第661行的警告
  14. //#pragma warning(disable:4700)
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20. extern CTaskControlWindow* g_pTaskControlWindow;
  21. ////////////////////////////////////////////////////////////////////
  22. // 处理流程控制器
  23. void ProcessManager(CTaskWindow* linkprocess)
  24. {
  25. if (/*(linkprocess->m_SelTools.empty() == false) &&*/ //处理流程不为空
  26. (linkprocess->m_TaskMode == TASK_MODE::MAIN_TASK)) //任务模式为主任务
  27. {
  28. // 执行任务
  29. linkprocess->RunProcess();
  30. }
  31. else
  32. {
  33. Sleep(1000);
  34. }
  35. }
  36. ////////////////////////////////////////////////////////////////
  37. // 处理流程控制器
  38. UINT Process(LPVOID lpParam)
  39. {
  40. UINT result = 1;
  41. CTaskWindow* linkprocess = (CTaskWindow*)lpParam;
  42. while (WAIT_OBJECT_0 == WaitForSingleObject(linkprocess->m_hExecuteEven, INFINITE))
  43. {
  44. if (linkprocess->m_bExitThread)
  45. {
  46. return 0;
  47. }
  48. ProcessManager(linkprocess);
  49. }
  50. return result;
  51. }
  52. ////////////////////////////////////////////////////////
  53. // 执行任务 2018.10.15
  54. void CTaskWindow::RunProcess()
  55. {
  56. int t1 = 0;
  57. int t2 = 0;
  58. CString strRunTime;
  59. CString strInfo;
  60. t2 = timeGetTime();
  61. for (int i = 0; i < m_SelTools.size(); i++)
  62. {
  63. TOOL& tool = m_SelTools[i];
  64. t1 = timeGetTime();
  65. // printf("Tool:%S ID: %d forID%d NextID%d\r\n", tool.strShowName, tool.nToolID, i, tool.nNextToolID);
  66. ExProt(tool);
  67. if (tool.bEnable)
  68. {
  69. for (unsigned int j = 0; j < tool.Interfaces.size(); j++)
  70. {
  71. TOOL_INTERFACE& inf = tool.Interfaces[j];
  72. if (inf.InfDirection == INF_IN)
  73. {
  74. BOOL bStatus = DoDllInput(/*tool,*/ inf);
  75. }
  76. }
  77. int nRetCode = -1;
  78. tool.pDllPtr->Trigger(!g_nRunningState);//采用g_bRunningState 区分是运行还是运行一次
  79. tool.dTraggerTime = t1;
  80. if (tool.nInSleep != 0)
  81. {
  82. Sleep(tool.nInSleep);
  83. }
  84. nRetCode = DoDllExecute(tool);
  85. if (tool.pDllPtr->m_Even.hEvenHandle != NULL)
  86. {
  87. t2 = timeGetTime();
  88. }
  89. if (tool.nOutSleep != 0)
  90. {
  91. Sleep(tool.nOutSleep);
  92. }
  93. //printf("Tool:%S nRetCode:%d\r\n", tool.strShowName, nRetCode);
  94. //if (nRetCode != tool.nExecCode)
  95. {
  96. tool.nExecCode = nRetCode;
  97. RedrawToolExecCode(tool);
  98. }
  99. //异常统计
  100. if (nRetCode == RT_ERROR)
  101. {
  102. tool.nAbnormalCount++;
  103. }
  104. if (RT_TIMEOUT == nRetCode) // TimeOut
  105. {
  106. tool.dRunTime = -1;
  107. // OUTPUT_MSG msg;
  108. // strRunTime.Format(_T("%s: TimeOut"), tool.strShowName);
  109. // msg.strMsg = strRunTime;
  110. // g_pMainFrame->ShowOutputInfo(msg, LOG);
  111. }
  112. //t = ((double)cv::getTickCount() - t) / cv::getTickFrequency();
  113. t1 = timeGetTime() - t1;
  114. CString str;
  115. str.Format(_T("%d\r\n"), t1);
  116. //TRACE(str);
  117. tool.dRunTime = (double)t1 /** 1000.0*/;
  118. tool.dFrequency = 1.0 / t1 * 1000;
  119. if (nRetCode == RT_GOTO)
  120. {
  121. if (tool.hNextTool.nToolID != -1)
  122. {
  123. TOOL toolTemp;
  124. toolTemp.strName = tool.hNextTool.strName;
  125. toolTemp.strShowName = tool.hNextTool.strShowName;
  126. TOOL* pTargetTool = GetToolByTool(toolTemp);
  127. if (pTargetTool != NULL)
  128. {
  129. tool.hNextTool.nToolID = pTargetTool->nToolID;
  130. i = tool.hNextTool.nToolID - 1;
  131. }
  132. else
  133. {
  134. CString str;
  135. str.Format(_T("Task[%d] 模块 [%s] 找不到数据 [%s] "),m_nIndex, tool.strShowName, toolTemp.strShowName);
  136. USES_CONVERSION;
  137. char * s_str = T2A(str);
  138. g_Logger.Error(1 , __FILE__, __LINE__, s_str);
  139. }
  140. if (tool.nToolID > tool.hNextTool.nToolID)
  141. {
  142. CString str;
  143. str.Format(_T("Task[%d] 模块 [%s] 跳转到模块 [%s] 有死循环的风险,请注意"), m_nIndex, tool.strShowName, tool.hNextTool.strShowName);
  144. USES_CONVERSION;
  145. char * s_str = T2A(str);
  146. g_Logger.Warn(1, __FILE__, __LINE__, s_str);
  147. }
  148. }
  149. }
  150. if (m_editor.IsWindowVisible())
  151. {
  152. if (g_pTaskView)
  153. {
  154. DEUBGOBJ obj = tool.pDllPtr->GetDeubgDispObj();
  155. g_pTaskView->m_wndDisp.AddImage(obj.mImage);
  156. g_pTaskView->m_wndDisp.AddOjb(obj.mObj);
  157. }
  158. }
  159. }
  160. else
  161. {
  162. tool.nExecCode = RT_INVALID;
  163. }
  164. }
  165. if (m_editor.IsWindowVisible())
  166. {
  167. if (g_pTaskView)
  168. g_pTaskView->m_wndDisp.Refresh();
  169. }
  170. if (m_SelTools.size())
  171. {
  172. g_pTaskControlWindow->UpdateUI();
  173. m_dRunningCount++;
  174. thePrefs.m_nRunningCount++;
  175. thePrefs.m_nTotalRunningCount++;
  176. }
  177. else
  178. {
  179. Sleep(1000);
  180. }
  181. t2 = timeGetTime() - t2;
  182. Sleep(m_nSleepTime);
  183. m_RunningTime = t2;
  184. SetEvent(m_hDoneEven);
  185. }
  186. void CTaskWindow::ExProt(/*const */TOOL& tool)
  187. {
  188. if (tool.Type == TOOL_TYPE::TOOL_BY_PROT)
  189. {
  190. TOOL_INTERFACE Sinf;
  191. for (unsigned int j = 0; j < tool.Interfaces.size(); j++)
  192. {
  193. TOOL_INTERFACE& inf = tool.Interfaces[j];
  194. if (inf.InfDirection == INF_IN)
  195. {
  196. Sinf = inf;
  197. if (inf.bShow)
  198. {
  199. BOOL bStatus = DoDllProt(tool, Sinf);
  200. break;
  201. }
  202. }
  203. }
  204. }
  205. }
  206. ///////////////////////////////////////////////////////
  207. // 重绘Tool的返回值状态
  208. void CTaskWindow::RedrawToolExecCode(const TOOL& tool)
  209. {
  210. // 如果值有变动,则需要进行重绘
  211. if (this->IsWindowVisible())
  212. {
  213. CDiagramEntityContainer* objs = static_cast<CDiagramEntityContainer*> (m_editor.GetDiagramEntityContainer());
  214. if (objs)
  215. {
  216. for (UINT nIndex = 0; nIndex < objs->GetSize(); nIndex++)
  217. {
  218. CFunctionBlock* obj = (CFunctionBlock*)objs->GetAt(nIndex);
  219. if (obj->Tools.strShowName == tool.strShowName)
  220. {
  221. obj->SetToolResult(tool.nExecCode);
  222. m_editor.InvalidateRect(tool.rect);
  223. }
  224. }
  225. }
  226. }
  227. // CRect textRect;
  228. // m_wndTree.GetItemRect(tool.hHandle, &textRect, TRUE);
  229. //
  230. // CRect rcDraw(textRect.right, textRect.top, textRect.right + IMAGE_TOOL_CODE_WIDTH, textRect.top + IMAGE_TOOL_CODE_WIDTH);
  231. // m_wndTree.InvalidateRect(&rcDraw);
  232. }
  233. //===========================================================================================
  234. ////////////////////////////////////////////////////////
  235. // 执行Dll的Input操作(根据不同类型,不同的处理)
  236. bool CTaskWindow::DoDllInput(/*TOOL& tool,*/ TOOL_INTERFACE& inf)
  237. {
  238. TOOL_INTERFACE* pLinkInf = GetHigherLinkInterface(inf);
  239. if (pLinkInf != NULL)
  240. {
  241. ValueLink(pLinkInf, inf);
  242. return true;
  243. }
  244. return false;
  245. }
  246. bool CTaskWindow::DoDllProt(TOOL& tool, TOOL_INTERFACE& inf)
  247. {
  248. TOOL_INTERFACE* pLinkInf =/* GetHigherLinkInterface*/&(inf);
  249. if (pLinkInf == NULL)
  250. {
  251. return FALSE;
  252. }
  253. for (UINT i = 0; i < tool.Interfaces.size(); i++)
  254. {
  255. TOOL_INTERFACE& Inf = tool.Interfaces[i];
  256. if (Inf.InfDirection == INF_DIRECTION::INF_OUT)
  257. {
  258. if (Inf.VarType == pLinkInf->VarType)
  259. {
  260. ValueLink(pLinkInf, Inf);
  261. }
  262. }
  263. }
  264. return true;
  265. }
  266. void CTaskWindow::ValueLink(/*const */TOOL_INTERFACE* pLinkInf,/* const*/ TOOL_INTERFACE& inf)
  267. {
  268. VAR_TYPE VarType = inf.VarType;
  269. //////////////////////////////////////////////////////////////////////////
  270. switch (VarType)
  271. {
  272. case VAR_TYPE::Bool_Var:
  273. {
  274. bool Value = CallOutputByDll<bool>(*pLinkInf);
  275. CallInputByDll<bool>(Value, inf);
  276. }break;
  277. case VAR_TYPE::Int_Var:
  278. {
  279. int Value = CallOutputByDll<int>(*pLinkInf);
  280. CallInputByDll<int>(Value, inf);
  281. }break;
  282. case VAR_TYPE::float_Var:
  283. {
  284. float Value = CallOutputByDll<float>(*pLinkInf);
  285. CallInputByDll<float>(Value, inf);
  286. }break;
  287. case VAR_TYPE::Doublel_Var:
  288. {
  289. }break;
  290. case VAR_TYPE::CharP_Var:
  291. {
  292. char* Value = CallOutputByDll<char*>(*pLinkInf);
  293. CallInputByDll<char*>(Value, inf);
  294. }break;
  295. case VAR_TYPE::String_Var:
  296. {
  297. }break;
  298. case VAR_TYPE::CString_Var:
  299. {
  300. if (inf.InfWay == INF_WAY::INF_BY_VALUE)
  301. {
  302. CString Value = CallOutputByDll<CString>(*pLinkInf);
  303. CallInputByDll<CString>(Value, inf);
  304. }
  305. else
  306. {
  307. CString& LinkValue = CallOutputByDll<CString>(*pLinkInf);
  308. CallInputByDll<CString>(LinkValue, inf);
  309. }
  310. }break;
  311. case VAR_TYPE::IpImageP_Var:
  312. {
  313. IplImage* Value = CallOutputByDll<IplImage*>(*pLinkInf);
  314. CallInputByDll<IplImage*>(Value, inf);
  315. }break;
  316. case VAR_TYPE::Mat_Var:
  317. {
  318. Mat Value = CallOutputByDll<Mat>(*pLinkInf);
  319. CallInputByDll<Mat>(Value, inf);
  320. }break;
  321. case VAR_TYPE::HTuple_Var:
  322. {
  323. HTuple Value = CallOutputByDll<HTuple>(*pLinkInf);
  324. CallInputByDll<HTuple>(Value, inf);
  325. }break;
  326. case VAR_TYPE::HObject_Vat:
  327. {
  328. HObject Value = CallOutputByDll<HObject>(*pLinkInf);
  329. CallInputByDll<HObject>(Value, inf);
  330. }break;
  331. case VAR_TYPE::HImage_Var:
  332. {
  333. HImage Value = CallOutputByDll<HImage>(*pLinkInf);
  334. CallInputByDll<HImage>(Value, inf);
  335. }break;
  336. case VAR_TYPE::ISocketP_Var:
  337. {
  338. ISocket* Value = CallOutputByDll<ISocket*>(*pLinkInf);
  339. CallInputByDll<ISocket*>(Value, inf);
  340. }break;
  341. case VAR_TYPE::ArrayInP_Var:
  342. {
  343. if (inf.InfWay == INF_WAY::INF_BY_VALUE)
  344. {
  345. Array1 Value = CallOutputByDll<Array1>(*pLinkInf);
  346. CallInputByDll<Array1>(Value, inf);
  347. }
  348. else if (inf.InfWay == INF_WAY::INF_BY_ADDRESS)
  349. {
  350. Array1& LinkValue = CallOutputByDll<Array1>(*pLinkInf);
  351. CallInputByDll<Array1>(LinkValue, inf);
  352. }
  353. }break;
  354. case VAR_TYPE::ArrayOutP_Var:
  355. {
  356. if (inf.InfWay == INF_WAY::INF_BY_VALUE)
  357. {
  358. Array1 Value = CallOutputByDll<Array1>(*pLinkInf);
  359. CallInputByDll<Array1>(Value, inf);
  360. }
  361. else if (inf.InfWay == INF_WAY::INF_BY_ADDRESS)
  362. {
  363. Array1& Value = CallOutputByDll<Array1>(*pLinkInf);
  364. CallInputByDll<Array1>(Value, inf);
  365. }
  366. }break;
  367. case VAR_TYPE::ArrayRobotPos_Vat:
  368. {
  369. Array1 Value = CallOutputByDll<Array1>(*pLinkInf);
  370. CallInputByDll<Array1>(Value, inf);
  371. }break;
  372. case VAR_TYPE::HTuple_Pos_Var:
  373. {
  374. HPOS Value = CallOutputByDll<HPOS>(*pLinkInf);
  375. CallInputByDll<HPOS>(Value, inf);
  376. }break;
  377. case VAR_TYPE::HTuple_HomMat2D_Var:
  378. {
  379. HHomMat2D Value = CallOutputByDll<HHomMat2D>(*pLinkInf);
  380. CallInputByDll<HHomMat2D>(Value, inf);
  381. }break;
  382. case VAR_TYPE::HTuple_Point_Var:
  383. {
  384. HPoint Value = CallOutputByDll<HPoint>(*pLinkInf);
  385. CallInputByDll<HPoint>(Value, inf);
  386. }break;
  387. case VAR_TYPE::HTuple_Line_Var:
  388. {
  389. HLine Value = CallOutputByDll<HLine>(*pLinkInf);
  390. CallInputByDll<HLine>(Value, inf);
  391. }break;
  392. case VAR_TYPE::HTuple_Circle_Var:
  393. {
  394. HCircle Value = CallOutputByDll<HCircle>(*pLinkInf);
  395. CallInputByDll<HCircle>(Value, inf);
  396. }break;
  397. case VAR_TYPE::HTuple_Disp_Var:
  398. {
  399. HDISP Value = CallOutputByDll<HDISP>(*pLinkInf);
  400. CallInputByDll<HDISP>(Value, inf);
  401. }break;
  402. case VAR_TYPE::HTuple_Window_Var:
  403. {
  404. HWINDOW Value = CallOutputByDll<HWINDOW>(*pLinkInf);
  405. CallInputByDll<HWINDOW>(Value, inf);
  406. }break;
  407. case VAR_TYPE::HTuple_Ncc_Modle_Var:
  408. {
  409. HNCCMODLE Value = CallOutputByDll<HNCCMODLE>(*pLinkInf);
  410. CallInputByDll<HNCCMODLE>(Value, inf);
  411. }break;
  412. case VAR_TYPE::HTuple_Shape_Modle_Var:
  413. {
  414. CString strMsg;
  415. strMsg.Format(_T("Task[%d] ValueLink 未实现: %d"), m_nIndex, VarType);
  416. USES_CONVERSION;
  417. char * s_str = T2A(strMsg);
  418. g_Logger.Error(0, __FILE__, __LINE__, s_str);
  419. }break;
  420. case VAR_TYPE::QHalconWindow_Var:
  421. {
  422. QHalconWindow* Value = CallOutputByDll<QHalconWindow*>(*pLinkInf);
  423. CallInputByDll<QHalconWindow*>(Value, inf);
  424. }break;
  425. case VAR_TYPE::HTuple_Calibration_Var:
  426. {
  427. HCALIBRATION Value = CallOutputByDll<HCALIBRATION>(*pLinkInf);
  428. CallInputByDll<HCALIBRATION>(Value, inf);
  429. }break;
  430. case VAR_TYPE::HTuple_CamParam_PoseCalib_Var:
  431. {
  432. HCAMPARAM_POSECALIB Value = CallOutputByDll<HCAMPARAM_POSECALIB>(*pLinkInf);
  433. CallInputByDll<HCAMPARAM_POSECALIB>(Value, inf);
  434. }break;
  435. default:
  436. CString strMsg;
  437. strMsg.Format(_T("Task [%d]ValueLink 未知的参数类型: %d"), m_nIndex, VarType);
  438. USES_CONVERSION;
  439. char * s_str = T2A(strMsg);
  440. g_Logger.Error(0, __FILE__, __LINE__, s_str);
  441. break;
  442. }
  443. }
  444. ////////////////////////////////////////////////////////////////
  445. // 专门用来获取Dll中绑定控件的接口
  446. CWnd* CTaskWindow::DoDllControlOutputWnd(TOOL_INTERFACE& inf)
  447. {
  448. CWnd* pRet = &CallOutputByDll<CWnd>(inf);
  449. return pRet;
  450. }
  451. QHalconWindow*& CTaskWindow::DoDllControlOutputImage(TOOL_INTERFACE& inf)
  452. {
  453. QHalconWindow*& pRet = CallOutputByDll<QHalconWindow *>(inf);
  454. return pRet;
  455. }
  456. ///////////////////////////////////////////////////////
  457. // 执行execute
  458. int CTaskWindow::DoDllExecute(TOOL& tool)
  459. {
  460. int nRetCode = RT_NONE;
  461. if (g_nRunningState != 0)
  462. {
  463. nRetCode = tool.pDllPtr->Execute();
  464. }
  465. else
  466. {
  467. //g_Logger.Warn(0, __FILE__, __LINE__, "运行失败,系统为[busy]状态 RunningState:[%d]", g_nRunningState);
  468. Sleep(1);
  469. }
  470. CString str;
  471. str.Format(_T("nRetCode %d\r\n"), nRetCode);
  472. TRACE(str);
  473. return nRetCode;
  474. }
  475. ////////////////////////////////////////////////////////////////////
  476. // 当Item执行时
  477. void CTaskWindow::OnItemExecute(const HTREEITEM& handle)
  478. {
  479. // 重置所有工具执行状态
  480. ResetToolExecCode();
  481. // 根据句柄查询到工具
  482. TOOL* pActiveTool = GetToolByHandle(handle);
  483. if (pActiveTool->bEnable == false)
  484. {
  485. g_pMainFrame->PopupMessage(MSG_WAY::_WARNING, _T("操作被禁止"), _T("被禁用的工具,禁止该操作....."));
  486. return;
  487. }
  488. for (unsigned int i = 0; i < pActiveTool->Interfaces.size(); i++)
  489. {
  490. TOOL_INTERFACE& inf = pActiveTool->Interfaces[i];
  491. // 如果是输入接口,判断是否需要从link取值
  492. if (inf.InfDirection == INF_IN)
  493. {
  494. DoDllInput(/**pActiveTool,*/ inf);
  495. }
  496. }
  497. // 执行execute
  498. pActiveTool->nExecCode = pActiveTool->pDllPtr->Execute();
  499. Invalidate(TRUE);
  500. }
  501. /*
  502. 此处的执行流程为
  503. 1: 更新输入参数,以便 Execute() 函数获取到最新的值
  504. 2: 执行Execute() 函数,该函数的返回值用来判断执行是成功或者失败
  505. 3: 最后更新输出参数,让下一个工具获得最新的数据
  506. */
  507. /////////////////////////////////////////////////////////
  508. // Execute All
  509. void CTaskWindow::OnItemExecuteAll()
  510. {
  511. return;
  512. // 重置所有工具执行状态
  513. ResetToolExecCode();
  514. // 从头到尾执行所有的execute
  515. for (unsigned int i = 0; i < m_SelTools.size(); i++)
  516. {
  517. TOOL& tool = m_SelTools[i];
  518. if (tool.bEnable)
  519. {
  520. tool.nExecCode = RT_NONE;
  521. for (unsigned int j = 0; j < tool.Interfaces.size(); j++)
  522. {
  523. TOOL_INTERFACE& inf = tool.Interfaces[j];
  524. if (inf.InfDirection == INF_IN)
  525. {
  526. BOOL bStatus = DoDllInput(/*tool,*/ inf);
  527. if (!bStatus)
  528. {
  529. CString strMsg;
  530. strMsg.Format(_T("Task[%d] DoDllInput Error"), m_nIndex);
  531. USES_CONVERSION;
  532. char * s_str = T2A(strMsg);
  533. g_Logger.Error(0, __FILE__, __LINE__, s_str);
  534. }
  535. }
  536. }
  537. int nRetCode = DoDllExecute(tool);
  538. tool.nExecCode = nRetCode;
  539. }
  540. else
  541. {
  542. tool.nExecCode = RT_INVALID;
  543. }
  544. }
  545. // m_wndTree.Invalidate(TRUE);
  546. }
  547. //////////////////////////////////////////////////////////
  548. // 从dll中获取工具指针
  549. BOOL CTaskWindow::GetToolPtrByDll(TOOL& tool)
  550. {
  551. // 加载Dll
  552. HINSTANCE hDllTool = ::LoadLibrary(tool.strDll);
  553. assert(hDllTool != NULL);
  554. // 调用GetToolPtr接口
  555. typedef CDllTool* (*GETTOOLPTR)();
  556. GETTOOLPTR GetToolPtr = NULL;
  557. GetToolPtr = (GETTOOLPTR)GetProcAddress(hDllTool, FUNCTION_GETTOOLPTR);
  558. if (GetToolPtr == NULL)
  559. {
  560. g_Logger.Error(0, __FILE__, __LINE__, "Task[%d] Not found %s()", m_nIndex, FUNCTION_GETTOOLPTR);
  561. return FALSE;
  562. }
  563. // 保存工具指针
  564. tool.pDllPtr = GetToolPtr();
  565. tool.hDllTool = hDllTool;
  566. // 2019-3-23 增加用于dll向exe同步的回调函数
  567. tool.pDllPtr->ControlCallback = std::bind(&CTaskControlWindow::ControlCallback, g_pTaskControlWindow, std::placeholders::_1, std::placeholders::_2);
  568. return TRUE;
  569. }
  570. /////////////////////////////////////////////////////////////////////////////////
  571. // 调用dll中的Output
  572. template<typename T>
  573. T& CTaskWindow::CallOutputByDll(TOOL_INTERFACE& inf)
  574. {
  575. //T* ret = NULL;
  576. TOOL* pTool = GetToolByInterface(inf);
  577. ASSERT(pTool != NULL);
  578. if (pTool == NULL)
  579. {
  580. CString strMsg;
  581. strMsg.Format(_T("pTool == NULL \n[%s --%s]"), inf.strParentName, inf.strName);
  582. BCGPMessageBox(strMsg, MB_YESNO | MB_ICONERROR, 0, NULL, _T("Error"));
  583. // return 0;
  584. }
  585. CDllTool* pPtr = pTool->pDllPtr;
  586. return pPtr->Value<T>(inf.nIndex - 1);
  587. //return ret;
  588. }
  589. // 调用dll中的Input
  590. template<typename T>
  591. void CTaskWindow::CallInputByDll(T& t, TOOL_INTERFACE& inf)
  592. {
  593. TOOL* pTool = GetToolByInterface(inf);
  594. if (pTool != NULL)
  595. {
  596. ASSERT(pTool != NULL);
  597. CDllTool* pPtr = pTool->pDllPtr;
  598. if (inf.InfWay == INF_BY_VALUE)
  599. {
  600. pPtr->SetValue<T>(inf.nIndex - 1, t);
  601. }
  602. else
  603. {
  604. pPtr->SetValue<T>(inf.nIndex - 1, t, INF_BY_ADDRESS);
  605. }
  606. }
  607. else
  608. {
  609. CString strMsg;
  610. strMsg.Format(_T("Task[%d] %s.%s"), m_nIndex, inf.strParentName, inf.strName);
  611. USES_CONVERSION;
  612. char * s_str = T2A(strMsg);
  613. g_Logger.Error(0, __FILE__, __LINE__, s_str);
  614. }
  615. }
  616. INTERFACE_DISP_TYPE CTaskWindow::UpdateUIValue(CString& str, HImage* hv_Image, HObject* hv_Obj, TOOL_INTERFACE* pInf)
  617. {
  618. INTERFACE_DISP_TYPE mType = INTERFACE_DISP_TYPE::IVDT_NONE;
  619. if (! thePrefs.m_bEnableSmartTips)
  620. {
  621. return mType;
  622. }
  623. if (pInf == NULL)
  624. {
  625. return mType;
  626. }
  627. if (pInf->VarType == VAR_TYPE::Bool_Var)
  628. {
  629. bool LinkValue = false;
  630. LinkValue = CallOutputByDll<bool>(*pInf);
  631. str.Format(_T("%d"), LinkValue);
  632. mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
  633. }
  634. // int类型
  635. else if (pInf->VarType == VAR_TYPE::Int_Var)
  636. {
  637. if (pInf->InfWay == INF_BY_VALUE)
  638. {
  639. int nValue = CallOutputByDll<int>(*pInf);
  640. str.Format(_T("%d"), nValue);
  641. }
  642. else if (pInf->InfWay == INF_BY_ADDRESS)
  643. {
  644. int* pValue = CallOutputByDll<int*>(*pInf);
  645. if (pValue)
  646. {
  647. str.Format(_T("%d"), *pValue);
  648. }
  649. }
  650. mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
  651. }
  652. // CString类型
  653. else if (pInf->VarType == VAR_TYPE::CString_Var)
  654. {
  655. if (pInf->InfWay == INF_BY_VALUE)
  656. {
  657. CString LinkValue;
  658. LinkValue = CallOutputByDll<CString>(*pInf);
  659. str.Format(_T("%s"), LinkValue);
  660. }
  661. else if (pInf->InfWay == INF_BY_ADDRESS)
  662. {
  663. CString* pValue = CallOutputByDll<CString*>(*pInf);
  664. str.Format(_T("%s"), *pValue);
  665. }
  666. mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
  667. }
  668. // lpImage类型
  669. else if (pInf->VarType == VAR_TYPE::IpImageP_Var)
  670. {
  671. str = _T("Image NULL");
  672. IplImage* LinkValue = NULL;
  673. LinkValue = CallOutputByDll<IplImage*>(*pInf);
  674. if (LinkValue != NULL)
  675. {
  676. str.Format(_T("ImageSize:\r\n%d,%d"), LinkValue->width, LinkValue->height);
  677. *hv_Obj = IplImageToHImage(LinkValue);
  678. }
  679. mType = INTERFACE_DISP_TYPE::IVDT_HIMAGE;
  680. }
  681. // Mat类型
  682. else if (pInf->VarType == VAR_TYPE::Mat_Var)
  683. {
  684. str = _T("Image NULL");
  685. Mat LinkValue;
  686. LinkValue = CallOutputByDll<Mat>(*pInf);
  687. if (!LinkValue.empty())
  688. {
  689. str.Format(_T("ImageSize:\r\n%d,%d"), LinkValue.cols, LinkValue.rows);
  690. IplImage imgTmp = IplImage(LinkValue);
  691. IplImage *input = cvCloneImage(&imgTmp);
  692. *hv_Obj = IplImageToHImage(input);
  693. }
  694. mType = INTERFACE_DISP_TYPE::IVDT_HIMAGE;
  695. }
  696. // HTuple类型
  697. else if (pInf->VarType == VAR_TYPE:: HTuple_Var)
  698. {
  699. HTuple LinkValue;
  700. LinkValue.Clear();
  701. LinkValue = CallOutputByDll<HTuple>(*pInf);
  702. str.Format(_T("HTuple"));
  703. mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
  704. }
  705. // HObject类型
  706. else if (pInf->VarType == VAR_TYPE:: HObject_Vat)
  707. {
  708. HObject LinkValue;
  709. LinkValue.Clear();
  710. LinkValue = CallOutputByDll<HObject>(*pInf);
  711. str.Format(_T("HObject"));
  712. if (LinkValue.Key() != UNDEF_KEY)
  713. {
  714. *hv_Obj = LinkValue;
  715. }
  716. mType = INTERFACE_DISP_TYPE::IVDT_OBJECT;
  717. }
  718. // HImage类型
  719. else if (pInf->VarType == VAR_TYPE:: HImage_Var)
  720. {
  721. if (pInf->InfWay == INF_BY_VALUE)
  722. {
  723. HImage LinkValue;
  724. LinkValue.Clear();
  725. LinkValue = CallOutputByDll<HImage>(*pInf);
  726. if (LinkValue.Key() != UNDEF_KEY)
  727. {
  728. *hv_Image = LinkValue;
  729. str.Format(_T("HImage"));
  730. }
  731. }
  732. else if (pInf->InfWay == INF_BY_ADDRESS)
  733. {
  734. HImage* LinkValue = CallOutputByDll<HImage*>(*pInf);
  735. if (LinkValue != NULL)
  736. {
  737. hv_Image = LinkValue;
  738. }
  739. str.Format(_T("HImage"));
  740. }
  741. mType = INTERFACE_DISP_TYPE::IVDT_HIMAGE;
  742. }
  743. // char*类型
  744. else if (pInf->VarType == VAR_TYPE::CharP_Var)
  745. {
  746. char* LinkValue = NULL;
  747. LinkValue = CallOutputByDll<char*>(*pInf);
  748. if (LinkValue != NULL)
  749. {
  750. str.Format(_T("%s"), LinkValue);
  751. }
  752. mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
  753. }
  754. // ISocketClient 类型
  755. else if (pInf->VarType == VAR_TYPE:: ISocketP_Var)
  756. {
  757. ISocket* LinkValue = NULL;
  758. LinkValue = CallOutputByDll<ISocket*>(*pInf);
  759. if (LinkValue != NULL)
  760. {
  761. str = LinkValue->ip();
  762. }
  763. else
  764. {
  765. str = _T("ISocket* LinkValue == NULL;");
  766. }
  767. mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
  768. }
  769. // Array1 类型
  770. else if (pInf->VarType == VAR_TYPE:: ArrayInP_Var)
  771. {
  772. //Array1 LinkValue;
  773. //LinkValue.mData ={0};
  774. //LinkValue.nValidData = 0;
  775. //LinkValue = CallOutputByDll<Array1>(*pInf);
  776. //str.Format(_T("%d"), LinkValue.nValidData);
  777. str = _T("In P");
  778. mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
  779. }
  780. // Array1 类型
  781. else if (pInf->VarType == VAR_TYPE:: ArrayOutP_Var)
  782. {
  783. //Array1 LinkValue;
  784. //LinkValue.mData ={0};
  785. //LinkValue.nValidData = 0;
  786. //LinkValue = CallOutputByDll<Array1>(*pInf);
  787. //str.Format(_T("%d"), LinkValue.nValidData);
  788. str = _T("Out P");
  789. mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
  790. }
  791. // Array1 类型
  792. else if (pInf->VarType == VAR_TYPE:: ArrayRobotPos_Vat)
  793. {
  794. str = _T("Out");
  795. mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
  796. }
  797. // float 类型
  798. else if (pInf->VarType == VAR_TYPE:: float_Var)
  799. {
  800. float LinkValue = 0;
  801. LinkValue = CallOutputByDll<float>(*pInf);
  802. str.Format(_T("%f"), LinkValue);
  803. mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
  804. }
  805. // HTuple_Pos 类型
  806. else if (pInf->VarType == VAR_TYPE:: HTuple_Pos_Var)
  807. {
  808. HPOS LinkValue;
  809. LinkValue.hv_Angle.Clear();
  810. LinkValue.hv_Column.Clear();
  811. LinkValue.hv_Row.Clear();
  812. LinkValue.hv_HomMat2D.Clear();
  813. LinkValue = CallOutputByDll<HPOS>(*pInf);
  814. for (int i = 0; i < LinkValue.hv_Row.TupleLength(); i++)
  815. {
  816. try
  817. {
  818. CString mStr;
  819. double r = 0.0;
  820. if (LinkValue.hv_Row[i].Length() > 0)
  821. {
  822. r = LinkValue.hv_Row[i].D();
  823. }
  824. double c = 0.0;
  825. if (LinkValue.hv_Row[i].Length() > 0)
  826. {
  827. c = LinkValue.hv_Column[i].D();
  828. }
  829. double a = 0.0;
  830. if (LinkValue.hv_Angle[i].Length() > 0)
  831. {
  832. HTuple aa;
  833. //TupleDeg(LinkValue.hv_Angle[i], &aa);
  834. aa = LinkValue.hv_Angle[i];
  835. a = aa.D();
  836. }
  837. mStr.Format(_T("X:%.2f--Y:%.2f--A:%.2f \r\n"), c, r, a);
  838. str = mStr + str;
  839. }
  840. catch (...) {}
  841. }
  842. //str.Format(_T("HTuple_Pos"));
  843. mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
  844. }
  845. // HTuple_HomMat2D 类型
  846. else if (pInf->VarType == VAR_TYPE:: HTuple_HomMat2D_Var)
  847. {
  848. // HHOMMAT2D LinkValue ;
  849. // LinkValue = CallOutputByDll<HHOMMAT2D>(*pInf);
  850. str.Format(_T("H_HomMat2D"));
  851. mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
  852. }
  853. //Ncc Modle 类型
  854. else if (pInf->VarType == VAR_TYPE:: HTuple_Ncc_Modle_Var)
  855. {
  856. str.Format(_T("Hcc Modle"));
  857. mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
  858. }
  859. // VAR_TYPE:: HTuple_Disp_Var 类型
  860. else if (pInf->VarType == VAR_TYPE:: HTuple_Disp_Var)
  861. {
  862. HDISP LinkValue;
  863. LinkValue.ho_DispObj.Clear();
  864. LinkValue = CallOutputByDll<HDISP>(*pInf);
  865. str.Format(_T("HObject_Disp"));
  866. if (LinkValue.ho_DispObj.Key() != UNDEF_KEY)
  867. {
  868. *hv_Obj = LinkValue.ho_DispObj;
  869. }
  870. mType = INTERFACE_DISP_TYPE::IVDT_OBJECT;
  871. }
  872. // PARAM_HWINDOW 类型
  873. else if (pInf->VarType == VAR_TYPE:: HTuple_Window_Var)
  874. {
  875. HWINDOW LinkValue;
  876. LinkValue.hv_Window.Clear();
  877. LinkValue = CallOutputByDll<HWINDOW>(*pInf);
  878. if (LinkValue.hv_Window == -1)
  879. {
  880. str.Format(_T("NULL"));
  881. }
  882. else
  883. {
  884. str.Format(_T("Window Handle"));
  885. }
  886. mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
  887. }
  888. // QHalconWindow 类型
  889. else if (pInf->VarType == VAR_TYPE:: QHalconWindow_Var)
  890. {
  891. // QHalconWindow* LinkValue;
  892. // LinkValue = NULL;
  893. // LinkValue = CallOutputByDll<QHalconWindow*>(*pInf);
  894. // if (LinkValue == -NULL)
  895. // {
  896. // str.Format(_T("NULL"));
  897. // }
  898. // else
  899. // {
  900. str.Format(_T("QWindow Handle"));
  901. // }
  902. mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
  903. }
  904. // VAR_TYPE:: HTuple_Window_Var 类型
  905. else if (pInf->VarType == VAR_TYPE:: HTuple_Calibration_Var)
  906. {
  907. str.Format(_T("HCalibration"));
  908. mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
  909. }
  910. else if (pInf->VarType == VAR_TYPE::HTuple_CamParam_PoseCalib_Var)
  911. {
  912. str.Format(_T("HCamParam_PoseCalib"));
  913. mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
  914. }
  915. // VAR_TYPE:: HTuple_Point_Var 类型
  916. else if (pInf->VarType == VAR_TYPE::HTuple_Point_Var)
  917. {
  918. str.Format(_T("HPoints"));
  919. mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
  920. }
  921. else
  922. {
  923. str.Format(_T("NULL"));
  924. }
  925. return mType;
  926. }
  927. /////////////////////////////////////////////////////////
  928. // ExecuteTool
  929. void CTaskWindow::OnExecuteTool(CString strToolName)
  930. {
  931. if (m_SelTools.size() < 0 || m_SelTools.size() > 500)
  932. {
  933. CString strMsg;
  934. strMsg.Format(_T("Task[%d] ExecuteTool Error"), m_nIndex);
  935. USES_CONVERSION;
  936. char * s_str = T2A(strMsg);
  937. g_Logger.Error(0, __FILE__, __LINE__, s_str);
  938. return;
  939. }
  940. // 重置所有工具执行状态
  941. ResetToolExecCode();
  942. // 从头到尾执行所有的execute
  943. for (unsigned int i = 0; i < m_SelTools.size(); i++)
  944. {
  945. TOOL& tool = m_SelTools[i];
  946. if (tool.bEnable)
  947. {
  948. if (tool.strShowName == strToolName)
  949. {
  950. tool.nExecCode = RT_NONE;
  951. for (unsigned int j = 0; j < tool.Interfaces.size(); j++)
  952. {
  953. TOOL_INTERFACE& inf = tool.Interfaces[j];
  954. if (inf.InfDirection == INF_IN && inf.bShow == true && inf.nUseCount > 0)
  955. {
  956. BOOL bStatus = DoDllInput(/*tool,*/ inf);
  957. if (!bStatus)
  958. {
  959. CString str = tool.strShowName +_T(".") + inf.strName;
  960. CString strMsg;
  961. strMsg.Format(_T("Task[%d] %s DoDllInput Error"), m_nIndex, str);
  962. USES_CONVERSION;
  963. char * s_str = T2A(strMsg);
  964. g_Logger.Error(0, __FILE__, __LINE__, s_str);
  965. }
  966. }
  967. }
  968. int nRetCode = 0;
  969. nRetCode = tool.pDllPtr->Execute();
  970. tool.nExecCode = nRetCode;
  971. }
  972. }
  973. else
  974. {
  975. tool.nExecCode = RT_INVALID;
  976. }
  977. }
  978. // m_wndTree.Invalidate(TRUE);
  979. }
  980. //////////////////////////////////////////////////////
  981. // 添加一个新的输入接口到所选的工具中,返回插入的索引值
  982. int CTaskWindow::AddDynamicInterface(TOOL& activeTool, const DLL_INF& dllInf, const HTREEITEM& handle)
  983. {
  984. int newIndex = 0;
  985. // 生成新的输入接口
  986. TOOL_INTERFACE newInterface;
  987. newInterface.strParentName = activeTool.strShowName;
  988. newInterface.strName = dllInf.strName;
  989. newInterface.VarType = dllInf.VarType;
  990. newInterface.InfDirection = dllInf.InfDirection;
  991. newInterface.InfWay = dllInf.InfWay;
  992. newInterface.InfType = dllInf.InfType;
  993. // 首先检查是否有重复的
  994. for (unsigned int i = 0; i < activeTool.Interfaces.size(); i++)
  995. {
  996. if (newInterface.strName.Compare(activeTool.Interfaces[i].strName) == 0)
  997. {
  998. CString strMsg;
  999. strMsg.Format(_T("Task[%d] 添加 %s 接口失败,此名字的接口已存在!"), m_nIndex, newInterface.strName);
  1000. USES_CONVERSION;
  1001. char * s_str = T2A(strMsg);
  1002. g_Logger.Error(0, __FILE__, __LINE__, s_str);
  1003. return -1;
  1004. }
  1005. }
  1006. // 寻找添加位置
  1007. int nIgnoreCount = 0;
  1008. bool bFind = false;
  1009. for (unsigned int i = 0; i < activeTool.Interfaces.size(); i++)
  1010. {
  1011. TOOL_INTERFACE& inf = activeTool.Interfaces[i];
  1012. // 需要跳过控件接口以及隐藏的接口
  1013. if (inf.InfType == INF_CONTROL || !inf.bShow)
  1014. {
  1015. nIgnoreCount++;
  1016. }
  1017. if (newInterface.InfDirection == INF_IN)
  1018. {
  1019. if (inf.InfDirection == INF_OUT || inf.InfType == INF_CONTROL)
  1020. {
  1021. bFind = true;
  1022. }
  1023. }
  1024. else if (newInterface.InfDirection == INF_OUT)
  1025. {
  1026. if (inf.InfType == INF_CONTROL)
  1027. {
  1028. bFind = true;
  1029. }
  1030. }
  1031. // 找到位置加入新的接口节点
  1032. if (bFind)
  1033. {
  1034. newIndex = i;
  1035. newInterface.nIndex = i;
  1036. activeTool.Interfaces.insert(activeTool.Interfaces.begin() + newIndex, newInterface);
  1037. // 然后后序所有的节点序号后移
  1038. for (unsigned int j = newIndex; j < activeTool.Interfaces.size(); j++)
  1039. {
  1040. activeTool.Interfaces[j].nIndex++;
  1041. }
  1042. break;
  1043. }
  1044. }
  1045. // 如果没找到的话,则说明tool没有输出和控件接口,那么直接添加到最后
  1046. if (!bFind)
  1047. {
  1048. activeTool.Interfaces.push_back(newInterface);
  1049. newIndex = (int)activeTool.Interfaces.size() - 1;
  1050. }
  1051. // 然后将接口添加到界面中
  1052. AddNewInterfaceToTree(handle, activeTool.Interfaces[newIndex], newIndex - nIgnoreCount);
  1053. return newIndex;
  1054. }
  1055. /////////////////////////////////////////////////////////////
  1056. // 添加一个新的接口到界面中
  1057. void CTaskWindow::AddNewInterfaceToTree(const HTREEITEM& hTool, TOOL_INTERFACE& newInf, const int& nPos)
  1058. {
  1059. // 强制展开节点
  1060. // m_wndTree.Expand(hTool, TVE_EXPAND);
  1061. //
  1062. // int nCount = 1;
  1063. // HTREEITEM hAfter = m_wndTree.GetChildItem(hTool);
  1064. // while (NULL != hAfter && nCount<nPos)
  1065. // {
  1066. // nCount++;
  1067. //
  1068. // hAfter = m_wndTree.GetNextSiblingItem(hAfter);
  1069. // }
  1070. //
  1071. // // 将新的接口信息添加到树形控件中指定位置中
  1072. // if (newInf.InfDirection == INF_IN)
  1073. // {
  1074. // newInf.hHandle = m_wndTree.InsertItem(newInf.strName, 1 /* Image inedex */, 1, hTool, hAfter);
  1075. // }
  1076. // else
  1077. // {
  1078. // newInf.hHandle = m_wndTree.InsertItem(newInf.strName, 2 /* Image inedex */, 2, hTool, hAfter);
  1079. // }
  1080. }
  1081. ///////////////////////////////////////////////////////////
  1082. // 2019-3-23 动态隐藏接口
  1083. void CTaskWindow::OnItemHideInterface(const HTREEITEM& handle)
  1084. {
  1085. }