123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263 |
- #include "stdafx.h"
- #include <Mmsystem.h>
- #pragma comment(lib, "winmm.lib ")
- #include "VisionPlus.h"
- #include "TaskWindow.h"
- #include "ToolWindow.h"
- #include "MainFrm.h"
- #include "GlobalSetting.h"
- #include "Utility.h"
- #include "TaskControlWindow.h"
- #include "HWindow.h"
- #include "Preferences.h"
- //// 禁用第661行的警告
- //#pragma warning(disable:4700)
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- extern CTaskControlWindow* g_pTaskControlWindow;
- ////////////////////////////////////////////////////////////////////
- // 处理流程控制器
- void ProcessManager(CTaskWindow* linkprocess)
- {
- if (/*(linkprocess->m_SelTools.empty() == false) &&*/ //处理流程不为空
- (linkprocess->m_TaskMode == TASK_MODE::MAIN_TASK)) //任务模式为主任务
- {
- // 执行任务
- linkprocess->RunProcess();
- }
- else
- {
- Sleep(1000);
- }
- }
- ////////////////////////////////////////////////////////////////
- // 处理流程控制器
- UINT Process(LPVOID lpParam)
- {
- UINT result = 1;
- CTaskWindow* linkprocess = (CTaskWindow*)lpParam;
- while (WAIT_OBJECT_0 == WaitForSingleObject(linkprocess->m_hExecuteEven, INFINITE))
- {
- if (linkprocess->m_bExitThread)
- {
- return 0;
- }
- ProcessManager(linkprocess);
- }
- return result;
- }
- ////////////////////////////////////////////////////////
- // 执行任务 2018.10.15
- void CTaskWindow::RunProcess()
- {
- int t1 = 0;
- int t2 = 0;
- CString strRunTime;
- CString strInfo;
- t2 = timeGetTime();
- for (int i = 0; i < m_SelTools.size(); i++)
- {
- TOOL& tool = m_SelTools[i];
- t1 = timeGetTime();
- // printf("Tool:%S ID: %d forID%d NextID%d\r\n", tool.strShowName, tool.nToolID, i, tool.nNextToolID);
- ExProt(tool);
- if (tool.bEnable)
- {
- for (unsigned int j = 0; j < tool.Interfaces.size(); j++)
- {
- TOOL_INTERFACE& inf = tool.Interfaces[j];
- if (inf.InfDirection == INF_IN)
- {
- BOOL bStatus = DoDllInput(/*tool,*/ inf);
- }
- }
- int nRetCode = -1;
- tool.pDllPtr->Trigger(!g_nRunningState);//采用g_bRunningState 区分是运行还是运行一次
- tool.dTraggerTime = t1;
- if (tool.nInSleep != 0)
- {
- Sleep(tool.nInSleep);
- }
-
- nRetCode = DoDllExecute(tool);
- if (tool.pDllPtr->m_Even.hEvenHandle != NULL)
- {
- t2 = timeGetTime();
- }
- if (tool.nOutSleep != 0)
- {
- Sleep(tool.nOutSleep);
- }
- //printf("Tool:%S nRetCode:%d\r\n", tool.strShowName, nRetCode);
- //if (nRetCode != tool.nExecCode)
- {
- tool.nExecCode = nRetCode;
- RedrawToolExecCode(tool);
- }
- //异常统计
- if (nRetCode == RT_ERROR)
- {
- tool.nAbnormalCount++;
- }
- if (RT_TIMEOUT == nRetCode) // TimeOut
- {
- tool.dRunTime = -1;
- // OUTPUT_MSG msg;
- // strRunTime.Format(_T("%s: TimeOut"), tool.strShowName);
- // msg.strMsg = strRunTime;
- // g_pMainFrame->ShowOutputInfo(msg, LOG);
- }
- //t = ((double)cv::getTickCount() - t) / cv::getTickFrequency();
- t1 = timeGetTime() - t1;
- CString str;
- str.Format(_T("%d\r\n"), t1);
- //TRACE(str);
- tool.dRunTime = (double)t1 /** 1000.0*/;
- tool.dFrequency = 1.0 / t1 * 1000;
- if (nRetCode == RT_GOTO)
- {
- if (tool.hNextTool.nToolID != -1)
- {
- TOOL toolTemp;
- toolTemp.strName = tool.hNextTool.strName;
- toolTemp.strShowName = tool.hNextTool.strShowName;
- TOOL* pTargetTool = GetToolByTool(toolTemp);
- if (pTargetTool != NULL)
- {
- tool.hNextTool.nToolID = pTargetTool->nToolID;
- i = tool.hNextTool.nToolID - 1;
- }
- else
- {
- CString str;
- str.Format(_T("Task[%d] 模块 [%s] 找不到数据 [%s] "),m_nIndex, tool.strShowName, toolTemp.strShowName);
- USES_CONVERSION;
- char * s_str = T2A(str);
- g_Logger.Error(1 , __FILE__, __LINE__, s_str);
- }
- if (tool.nToolID > tool.hNextTool.nToolID)
- {
- CString str;
- str.Format(_T("Task[%d] 模块 [%s] 跳转到模块 [%s] 有死循环的风险,请注意"), m_nIndex, tool.strShowName, tool.hNextTool.strShowName);
- USES_CONVERSION;
- char * s_str = T2A(str);
- g_Logger.Warn(1, __FILE__, __LINE__, s_str);
- }
- }
- }
- if (m_editor.IsWindowVisible())
- {
- if (g_pTaskView)
- {
- DEUBGOBJ obj = tool.pDllPtr->GetDeubgDispObj();
- g_pTaskView->m_wndDisp.AddImage(obj.mImage);
- g_pTaskView->m_wndDisp.AddOjb(obj.mObj);
- }
- }
- }
- else
- {
- tool.nExecCode = RT_INVALID;
- }
- }
- if (m_editor.IsWindowVisible())
- {
- if (g_pTaskView)
- g_pTaskView->m_wndDisp.Refresh();
- }
- if (m_SelTools.size())
- {
- g_pTaskControlWindow->UpdateUI();
- m_dRunningCount++;
- thePrefs.m_nRunningCount++;
- thePrefs.m_nTotalRunningCount++;
- }
- else
- {
- Sleep(1000);
- }
-
- t2 = timeGetTime() - t2;
- Sleep(m_nSleepTime);
- m_RunningTime = t2;
-
- SetEvent(m_hDoneEven);
- }
- void CTaskWindow::ExProt(/*const */TOOL& tool)
- {
- if (tool.Type == TOOL_TYPE::TOOL_BY_PROT)
- {
- TOOL_INTERFACE Sinf;
- for (unsigned int j = 0; j < tool.Interfaces.size(); j++)
- {
- TOOL_INTERFACE& inf = tool.Interfaces[j];
- if (inf.InfDirection == INF_IN)
- {
- Sinf = inf;
- if (inf.bShow)
- {
- BOOL bStatus = DoDllProt(tool, Sinf);
- break;
- }
- }
- }
- }
- }
- ///////////////////////////////////////////////////////
- // 重绘Tool的返回值状态
- void CTaskWindow::RedrawToolExecCode(const TOOL& tool)
- {
- // 如果值有变动,则需要进行重绘
- if (this->IsWindowVisible())
- {
- CDiagramEntityContainer* objs = static_cast<CDiagramEntityContainer*> (m_editor.GetDiagramEntityContainer());
- if (objs)
- {
- for (UINT nIndex = 0; nIndex < objs->GetSize(); nIndex++)
- {
- CFunctionBlock* obj = (CFunctionBlock*)objs->GetAt(nIndex);
- if (obj->Tools.strShowName == tool.strShowName)
- {
- obj->SetToolResult(tool.nExecCode);
- m_editor.InvalidateRect(tool.rect);
- }
- }
- }
- }
- // CRect textRect;
- // m_wndTree.GetItemRect(tool.hHandle, &textRect, TRUE);
- //
- // CRect rcDraw(textRect.right, textRect.top, textRect.right + IMAGE_TOOL_CODE_WIDTH, textRect.top + IMAGE_TOOL_CODE_WIDTH);
- // m_wndTree.InvalidateRect(&rcDraw);
- }
- //===========================================================================================
- ////////////////////////////////////////////////////////
- // 执行Dll的Input操作(根据不同类型,不同的处理)
- bool CTaskWindow::DoDllInput(/*TOOL& tool,*/ TOOL_INTERFACE& inf)
- {
- TOOL_INTERFACE* pLinkInf = GetHigherLinkInterface(inf);
- if (pLinkInf != NULL)
- {
- ValueLink(pLinkInf, inf);
- return true;
- }
- return false;
- }
- bool CTaskWindow::DoDllProt(TOOL& tool, TOOL_INTERFACE& inf)
- {
- TOOL_INTERFACE* pLinkInf =/* GetHigherLinkInterface*/&(inf);
- if (pLinkInf == NULL)
- {
- return FALSE;
- }
- for (UINT i = 0; i < tool.Interfaces.size(); i++)
- {
- TOOL_INTERFACE& Inf = tool.Interfaces[i];
- if (Inf.InfDirection == INF_DIRECTION::INF_OUT)
- {
- if (Inf.VarType == pLinkInf->VarType)
- {
- ValueLink(pLinkInf, Inf);
- }
- }
- }
- return true;
- }
- void CTaskWindow::ValueLink(/*const */TOOL_INTERFACE* pLinkInf,/* const*/ TOOL_INTERFACE& inf)
- {
- VAR_TYPE VarType = inf.VarType;
- //////////////////////////////////////////////////////////////////////////
- switch (VarType)
- {
- case VAR_TYPE::Bool_Var:
- {
- bool Value = CallOutputByDll<bool>(*pLinkInf);
- CallInputByDll<bool>(Value, inf);
- }break;
- case VAR_TYPE::Int_Var:
- {
- int Value = CallOutputByDll<int>(*pLinkInf);
- CallInputByDll<int>(Value, inf);
- }break;
- case VAR_TYPE::float_Var:
- {
- float Value = CallOutputByDll<float>(*pLinkInf);
- CallInputByDll<float>(Value, inf);
- }break;
- case VAR_TYPE::Doublel_Var:
- {
- }break;
- case VAR_TYPE::CharP_Var:
- {
- char* Value = CallOutputByDll<char*>(*pLinkInf);
- CallInputByDll<char*>(Value, inf);
- }break;
- case VAR_TYPE::String_Var:
- {
- }break;
- case VAR_TYPE::CString_Var:
- {
- if (inf.InfWay == INF_WAY::INF_BY_VALUE)
- {
- CString Value = CallOutputByDll<CString>(*pLinkInf);
- CallInputByDll<CString>(Value, inf);
- }
- else
- {
- CString& LinkValue = CallOutputByDll<CString>(*pLinkInf);
- CallInputByDll<CString>(LinkValue, inf);
- }
- }break;
- case VAR_TYPE::IpImageP_Var:
- {
- IplImage* Value = CallOutputByDll<IplImage*>(*pLinkInf);
- CallInputByDll<IplImage*>(Value, inf);
- }break;
- case VAR_TYPE::Mat_Var:
- {
- Mat Value = CallOutputByDll<Mat>(*pLinkInf);
- CallInputByDll<Mat>(Value, inf);
- }break;
- case VAR_TYPE::HTuple_Var:
- {
- HTuple Value = CallOutputByDll<HTuple>(*pLinkInf);
- CallInputByDll<HTuple>(Value, inf);
- }break;
- case VAR_TYPE::HObject_Vat:
- {
- HObject Value = CallOutputByDll<HObject>(*pLinkInf);
- CallInputByDll<HObject>(Value, inf);
- }break;
- case VAR_TYPE::HImage_Var:
- {
- HImage Value = CallOutputByDll<HImage>(*pLinkInf);
- CallInputByDll<HImage>(Value, inf);
- }break;
- case VAR_TYPE::ISocketP_Var:
- {
- ISocket* Value = CallOutputByDll<ISocket*>(*pLinkInf);
- CallInputByDll<ISocket*>(Value, inf);
- }break;
- case VAR_TYPE::ArrayInP_Var:
- {
- if (inf.InfWay == INF_WAY::INF_BY_VALUE)
- {
- Array1 Value = CallOutputByDll<Array1>(*pLinkInf);
- CallInputByDll<Array1>(Value, inf);
- }
- else if (inf.InfWay == INF_WAY::INF_BY_ADDRESS)
- {
- Array1& LinkValue = CallOutputByDll<Array1>(*pLinkInf);
- CallInputByDll<Array1>(LinkValue, inf);
- }
- }break;
- case VAR_TYPE::ArrayOutP_Var:
- {
- if (inf.InfWay == INF_WAY::INF_BY_VALUE)
- {
- Array1 Value = CallOutputByDll<Array1>(*pLinkInf);
- CallInputByDll<Array1>(Value, inf);
- }
- else if (inf.InfWay == INF_WAY::INF_BY_ADDRESS)
- {
- Array1& Value = CallOutputByDll<Array1>(*pLinkInf);
- CallInputByDll<Array1>(Value, inf);
- }
- }break;
- case VAR_TYPE::ArrayRobotPos_Vat:
- {
- Array1 Value = CallOutputByDll<Array1>(*pLinkInf);
- CallInputByDll<Array1>(Value, inf);
- }break;
- case VAR_TYPE::HTuple_Pos_Var:
- {
- HPOS Value = CallOutputByDll<HPOS>(*pLinkInf);
- CallInputByDll<HPOS>(Value, inf);
- }break;
- case VAR_TYPE::HTuple_HomMat2D_Var:
- {
- HHomMat2D Value = CallOutputByDll<HHomMat2D>(*pLinkInf);
- CallInputByDll<HHomMat2D>(Value, inf);
- }break;
- case VAR_TYPE::HTuple_Point_Var:
- {
- HPoint Value = CallOutputByDll<HPoint>(*pLinkInf);
- CallInputByDll<HPoint>(Value, inf);
- }break;
- case VAR_TYPE::HTuple_Line_Var:
- {
- HLine Value = CallOutputByDll<HLine>(*pLinkInf);
- CallInputByDll<HLine>(Value, inf);
- }break;
- case VAR_TYPE::HTuple_Circle_Var:
- {
- HCircle Value = CallOutputByDll<HCircle>(*pLinkInf);
- CallInputByDll<HCircle>(Value, inf);
- }break;
- case VAR_TYPE::HTuple_Disp_Var:
- {
- HDISP Value = CallOutputByDll<HDISP>(*pLinkInf);
- CallInputByDll<HDISP>(Value, inf);
- }break;
- case VAR_TYPE::HTuple_Window_Var:
- {
- HWINDOW Value = CallOutputByDll<HWINDOW>(*pLinkInf);
- CallInputByDll<HWINDOW>(Value, inf);
- }break;
- case VAR_TYPE::HTuple_Ncc_Modle_Var:
- {
- HNCCMODLE Value = CallOutputByDll<HNCCMODLE>(*pLinkInf);
- CallInputByDll<HNCCMODLE>(Value, inf);
- }break;
- case VAR_TYPE::HTuple_Shape_Modle_Var:
- {
- CString strMsg;
- strMsg.Format(_T("Task[%d] ValueLink 未实现: %d"), m_nIndex, VarType);
- USES_CONVERSION;
- char * s_str = T2A(strMsg);
- g_Logger.Error(0, __FILE__, __LINE__, s_str);
- }break;
- case VAR_TYPE::QHalconWindow_Var:
- {
- QHalconWindow* Value = CallOutputByDll<QHalconWindow*>(*pLinkInf);
- CallInputByDll<QHalconWindow*>(Value, inf);
- }break;
- case VAR_TYPE::HTuple_Calibration_Var:
- {
- HCALIBRATION Value = CallOutputByDll<HCALIBRATION>(*pLinkInf);
- CallInputByDll<HCALIBRATION>(Value, inf);
- }break;
- case VAR_TYPE::HTuple_CamParam_PoseCalib_Var:
- {
- HCAMPARAM_POSECALIB Value = CallOutputByDll<HCAMPARAM_POSECALIB>(*pLinkInf);
- CallInputByDll<HCAMPARAM_POSECALIB>(Value, inf);
- }break;
- default:
- CString strMsg;
- strMsg.Format(_T("Task [%d]ValueLink 未知的参数类型: %d"), m_nIndex, VarType);
- USES_CONVERSION;
- char * s_str = T2A(strMsg);
- g_Logger.Error(0, __FILE__, __LINE__, s_str);
- break;
- }
- }
- ////////////////////////////////////////////////////////////////
- // 专门用来获取Dll中绑定控件的接口
- CWnd* CTaskWindow::DoDllControlOutputWnd(TOOL_INTERFACE& inf)
- {
- CWnd* pRet = &CallOutputByDll<CWnd>(inf);
- return pRet;
- }
- QHalconWindow*& CTaskWindow::DoDllControlOutputImage(TOOL_INTERFACE& inf)
- {
- QHalconWindow*& pRet = CallOutputByDll<QHalconWindow *>(inf);
- return pRet;
- }
- ///////////////////////////////////////////////////////
- // 执行execute
- int CTaskWindow::DoDllExecute(TOOL& tool)
- {
- int nRetCode = RT_NONE;
- if (g_nRunningState != 0)
- {
- nRetCode = tool.pDllPtr->Execute();
- }
- else
- {
- //g_Logger.Warn(0, __FILE__, __LINE__, "运行失败,系统为[busy]状态 RunningState:[%d]", g_nRunningState);
- Sleep(1);
- }
- CString str;
- str.Format(_T("nRetCode %d\r\n"), nRetCode);
- TRACE(str);
- return nRetCode;
- }
- ////////////////////////////////////////////////////////////////////
- // 当Item执行时
- void CTaskWindow::OnItemExecute(const HTREEITEM& handle)
- {
- // 重置所有工具执行状态
- ResetToolExecCode();
- // 根据句柄查询到工具
- TOOL* pActiveTool = GetToolByHandle(handle);
- if (pActiveTool->bEnable == false)
- {
- g_pMainFrame->PopupMessage(MSG_WAY::_WARNING, _T("操作被禁止"), _T("被禁用的工具,禁止该操作....."));
- return;
- }
- for (unsigned int i = 0; i < pActiveTool->Interfaces.size(); i++)
- {
- TOOL_INTERFACE& inf = pActiveTool->Interfaces[i];
- // 如果是输入接口,判断是否需要从link取值
- if (inf.InfDirection == INF_IN)
- {
- DoDllInput(/**pActiveTool,*/ inf);
- }
- }
- // 执行execute
- pActiveTool->nExecCode = pActiveTool->pDllPtr->Execute();
- Invalidate(TRUE);
- }
- /*
- 此处的执行流程为
- 1: 更新输入参数,以便 Execute() 函数获取到最新的值
- 2: 执行Execute() 函数,该函数的返回值用来判断执行是成功或者失败
- 3: 最后更新输出参数,让下一个工具获得最新的数据
- */
- /////////////////////////////////////////////////////////
- // Execute All
- void CTaskWindow::OnItemExecuteAll()
- {
- return;
- // 重置所有工具执行状态
- ResetToolExecCode();
- // 从头到尾执行所有的execute
- for (unsigned int i = 0; i < m_SelTools.size(); i++)
- {
- TOOL& tool = m_SelTools[i];
- if (tool.bEnable)
- {
- tool.nExecCode = RT_NONE;
- for (unsigned int j = 0; j < tool.Interfaces.size(); j++)
- {
- TOOL_INTERFACE& inf = tool.Interfaces[j];
- if (inf.InfDirection == INF_IN)
- {
- BOOL bStatus = DoDllInput(/*tool,*/ inf);
- if (!bStatus)
- {
- CString strMsg;
- strMsg.Format(_T("Task[%d] DoDllInput Error"), m_nIndex);
- USES_CONVERSION;
- char * s_str = T2A(strMsg);
- g_Logger.Error(0, __FILE__, __LINE__, s_str);
- }
- }
- }
- int nRetCode = DoDllExecute(tool);
- tool.nExecCode = nRetCode;
- }
- else
- {
- tool.nExecCode = RT_INVALID;
- }
- }
- // m_wndTree.Invalidate(TRUE);
- }
- //////////////////////////////////////////////////////////
- // 从dll中获取工具指针
- BOOL CTaskWindow::GetToolPtrByDll(TOOL& tool)
- {
- // 加载Dll
- HINSTANCE hDllTool = ::LoadLibrary(tool.strDll);
- assert(hDllTool != NULL);
- // 调用GetToolPtr接口
- typedef CDllTool* (*GETTOOLPTR)();
- GETTOOLPTR GetToolPtr = NULL;
- GetToolPtr = (GETTOOLPTR)GetProcAddress(hDllTool, FUNCTION_GETTOOLPTR);
- if (GetToolPtr == NULL)
- {
- g_Logger.Error(0, __FILE__, __LINE__, "Task[%d] Not found %s()", m_nIndex, FUNCTION_GETTOOLPTR);
- return FALSE;
- }
- // 保存工具指针
- tool.pDllPtr = GetToolPtr();
- tool.hDllTool = hDllTool;
- // 2019-3-23 增加用于dll向exe同步的回调函数
- tool.pDllPtr->ControlCallback = std::bind(&CTaskControlWindow::ControlCallback, g_pTaskControlWindow, std::placeholders::_1, std::placeholders::_2);
- return TRUE;
- }
- /////////////////////////////////////////////////////////////////////////////////
- // 调用dll中的Output
- template<typename T>
- T& CTaskWindow::CallOutputByDll(TOOL_INTERFACE& inf)
- {
- //T* ret = NULL;
- TOOL* pTool = GetToolByInterface(inf);
- ASSERT(pTool != NULL);
- if (pTool == NULL)
- {
- CString strMsg;
- strMsg.Format(_T("pTool == NULL \n[%s --%s]"), inf.strParentName, inf.strName);
- BCGPMessageBox(strMsg, MB_YESNO | MB_ICONERROR, 0, NULL, _T("Error"));
- // return 0;
- }
- CDllTool* pPtr = pTool->pDllPtr;
- return pPtr->Value<T>(inf.nIndex - 1);
- //return ret;
- }
- // 调用dll中的Input
- template<typename T>
- void CTaskWindow::CallInputByDll(T& t, TOOL_INTERFACE& inf)
- {
- TOOL* pTool = GetToolByInterface(inf);
- if (pTool != NULL)
- {
- ASSERT(pTool != NULL);
- CDllTool* pPtr = pTool->pDllPtr;
- if (inf.InfWay == INF_BY_VALUE)
- {
- pPtr->SetValue<T>(inf.nIndex - 1, t);
- }
- else
- {
- pPtr->SetValue<T>(inf.nIndex - 1, t, INF_BY_ADDRESS);
- }
- }
- else
- {
- CString strMsg;
- strMsg.Format(_T("Task[%d] %s.%s"), m_nIndex, inf.strParentName, inf.strName);
- USES_CONVERSION;
- char * s_str = T2A(strMsg);
- g_Logger.Error(0, __FILE__, __LINE__, s_str);
- }
-
- }
- INTERFACE_DISP_TYPE CTaskWindow::UpdateUIValue(CString& str, HImage* hv_Image, HObject* hv_Obj, TOOL_INTERFACE* pInf)
- {
- INTERFACE_DISP_TYPE mType = INTERFACE_DISP_TYPE::IVDT_NONE;
- if (! thePrefs.m_bEnableSmartTips)
- {
- return mType;
- }
- if (pInf == NULL)
- {
- return mType;
- }
- if (pInf->VarType == VAR_TYPE::Bool_Var)
- {
- bool LinkValue = false;
- LinkValue = CallOutputByDll<bool>(*pInf);
- str.Format(_T("%d"), LinkValue);
- mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
- }
- // int类型
- else if (pInf->VarType == VAR_TYPE::Int_Var)
- {
- if (pInf->InfWay == INF_BY_VALUE)
- {
- int nValue = CallOutputByDll<int>(*pInf);
- str.Format(_T("%d"), nValue);
- }
- else if (pInf->InfWay == INF_BY_ADDRESS)
- {
- int* pValue = CallOutputByDll<int*>(*pInf);
- if (pValue)
- {
- str.Format(_T("%d"), *pValue);
- }
- }
- mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
- }
- // CString类型
- else if (pInf->VarType == VAR_TYPE::CString_Var)
- {
- if (pInf->InfWay == INF_BY_VALUE)
- {
- CString LinkValue;
- LinkValue = CallOutputByDll<CString>(*pInf);
- str.Format(_T("%s"), LinkValue);
- }
- else if (pInf->InfWay == INF_BY_ADDRESS)
- {
- CString* pValue = CallOutputByDll<CString*>(*pInf);
- str.Format(_T("%s"), *pValue);
- }
- mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
- }
- // lpImage类型
- else if (pInf->VarType == VAR_TYPE::IpImageP_Var)
- {
- str = _T("Image NULL");
- IplImage* LinkValue = NULL;
- LinkValue = CallOutputByDll<IplImage*>(*pInf);
- if (LinkValue != NULL)
- {
- str.Format(_T("ImageSize:\r\n%d,%d"), LinkValue->width, LinkValue->height);
- *hv_Obj = IplImageToHImage(LinkValue);
- }
- mType = INTERFACE_DISP_TYPE::IVDT_HIMAGE;
- }
- // Mat类型
- else if (pInf->VarType == VAR_TYPE::Mat_Var)
- {
- str = _T("Image NULL");
- Mat LinkValue;
- LinkValue = CallOutputByDll<Mat>(*pInf);
- if (!LinkValue.empty())
- {
- str.Format(_T("ImageSize:\r\n%d,%d"), LinkValue.cols, LinkValue.rows);
- IplImage imgTmp = IplImage(LinkValue);
- IplImage *input = cvCloneImage(&imgTmp);
- *hv_Obj = IplImageToHImage(input);
- }
- mType = INTERFACE_DISP_TYPE::IVDT_HIMAGE;
- }
- // HTuple类型
- else if (pInf->VarType == VAR_TYPE:: HTuple_Var)
- {
- HTuple LinkValue;
- LinkValue.Clear();
- LinkValue = CallOutputByDll<HTuple>(*pInf);
- str.Format(_T("HTuple"));
- mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
- }
- // HObject类型
- else if (pInf->VarType == VAR_TYPE:: HObject_Vat)
- {
- HObject LinkValue;
- LinkValue.Clear();
- LinkValue = CallOutputByDll<HObject>(*pInf);
- str.Format(_T("HObject"));
- if (LinkValue.Key() != UNDEF_KEY)
- {
- *hv_Obj = LinkValue;
- }
- mType = INTERFACE_DISP_TYPE::IVDT_OBJECT;
- }
- // HImage类型
- else if (pInf->VarType == VAR_TYPE:: HImage_Var)
- {
- if (pInf->InfWay == INF_BY_VALUE)
- {
- HImage LinkValue;
- LinkValue.Clear();
- LinkValue = CallOutputByDll<HImage>(*pInf);
- if (LinkValue.Key() != UNDEF_KEY)
- {
- *hv_Image = LinkValue;
- str.Format(_T("HImage"));
- }
- }
- else if (pInf->InfWay == INF_BY_ADDRESS)
- {
- HImage* LinkValue = CallOutputByDll<HImage*>(*pInf);
- if (LinkValue != NULL)
- {
- hv_Image = LinkValue;
- }
- str.Format(_T("HImage"));
- }
- mType = INTERFACE_DISP_TYPE::IVDT_HIMAGE;
- }
- // char*类型
- else if (pInf->VarType == VAR_TYPE::CharP_Var)
- {
- char* LinkValue = NULL;
- LinkValue = CallOutputByDll<char*>(*pInf);
- if (LinkValue != NULL)
- {
- str.Format(_T("%s"), LinkValue);
- }
- mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
- }
- // ISocketClient 类型
- else if (pInf->VarType == VAR_TYPE:: ISocketP_Var)
- {
- ISocket* LinkValue = NULL;
- LinkValue = CallOutputByDll<ISocket*>(*pInf);
- if (LinkValue != NULL)
- {
- str = LinkValue->ip();
- }
- else
- {
- str = _T("ISocket* LinkValue == NULL;");
- }
- mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
- }
- // Array1 类型
- else if (pInf->VarType == VAR_TYPE:: ArrayInP_Var)
- {
- //Array1 LinkValue;
- //LinkValue.mData ={0};
- //LinkValue.nValidData = 0;
- //LinkValue = CallOutputByDll<Array1>(*pInf);
- //str.Format(_T("%d"), LinkValue.nValidData);
- str = _T("In P");
- mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
- }
- // Array1 类型
- else if (pInf->VarType == VAR_TYPE:: ArrayOutP_Var)
- {
- //Array1 LinkValue;
- //LinkValue.mData ={0};
- //LinkValue.nValidData = 0;
- //LinkValue = CallOutputByDll<Array1>(*pInf);
- //str.Format(_T("%d"), LinkValue.nValidData);
- str = _T("Out P");
- mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
- }
- // Array1 类型
- else if (pInf->VarType == VAR_TYPE:: ArrayRobotPos_Vat)
- {
- str = _T("Out");
- mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
- }
- // float 类型
- else if (pInf->VarType == VAR_TYPE:: float_Var)
- {
- float LinkValue = 0;
- LinkValue = CallOutputByDll<float>(*pInf);
- str.Format(_T("%f"), LinkValue);
- mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
- }
- // HTuple_Pos 类型
- else if (pInf->VarType == VAR_TYPE:: HTuple_Pos_Var)
- {
- HPOS LinkValue;
- LinkValue.hv_Angle.Clear();
- LinkValue.hv_Column.Clear();
- LinkValue.hv_Row.Clear();
- LinkValue.hv_HomMat2D.Clear();
- LinkValue = CallOutputByDll<HPOS>(*pInf);
- for (int i = 0; i < LinkValue.hv_Row.TupleLength(); i++)
- {
- try
- {
- CString mStr;
- double r = 0.0;
- if (LinkValue.hv_Row[i].Length() > 0)
- {
- r = LinkValue.hv_Row[i].D();
- }
- double c = 0.0;
- if (LinkValue.hv_Row[i].Length() > 0)
- {
- c = LinkValue.hv_Column[i].D();
- }
- double a = 0.0;
- if (LinkValue.hv_Angle[i].Length() > 0)
- {
- HTuple aa;
- //TupleDeg(LinkValue.hv_Angle[i], &aa);
- aa = LinkValue.hv_Angle[i];
- a = aa.D();
- }
- mStr.Format(_T("X:%.2f--Y:%.2f--A:%.2f \r\n"), c, r, a);
- str = mStr + str;
- }
- catch (...) {}
- }
- //str.Format(_T("HTuple_Pos"));
- mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
- }
- // HTuple_HomMat2D 类型
- else if (pInf->VarType == VAR_TYPE:: HTuple_HomMat2D_Var)
- {
- // HHOMMAT2D LinkValue ;
- // LinkValue = CallOutputByDll<HHOMMAT2D>(*pInf);
- str.Format(_T("H_HomMat2D"));
- mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
- }
- //Ncc Modle 类型
- else if (pInf->VarType == VAR_TYPE:: HTuple_Ncc_Modle_Var)
- {
- str.Format(_T("Hcc Modle"));
- mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
- }
- // VAR_TYPE:: HTuple_Disp_Var 类型
- else if (pInf->VarType == VAR_TYPE:: HTuple_Disp_Var)
- {
- HDISP LinkValue;
- LinkValue.ho_DispObj.Clear();
- LinkValue = CallOutputByDll<HDISP>(*pInf);
- str.Format(_T("HObject_Disp"));
- if (LinkValue.ho_DispObj.Key() != UNDEF_KEY)
- {
- *hv_Obj = LinkValue.ho_DispObj;
- }
- mType = INTERFACE_DISP_TYPE::IVDT_OBJECT;
- }
- // PARAM_HWINDOW 类型
- else if (pInf->VarType == VAR_TYPE:: HTuple_Window_Var)
- {
- HWINDOW LinkValue;
- LinkValue.hv_Window.Clear();
- LinkValue = CallOutputByDll<HWINDOW>(*pInf);
- if (LinkValue.hv_Window == -1)
- {
- str.Format(_T("NULL"));
- }
- else
- {
- str.Format(_T("Window Handle"));
- }
- mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
- }
- // QHalconWindow 类型
- else if (pInf->VarType == VAR_TYPE:: QHalconWindow_Var)
- {
- // QHalconWindow* LinkValue;
- // LinkValue = NULL;
- // LinkValue = CallOutputByDll<QHalconWindow*>(*pInf);
- // if (LinkValue == -NULL)
- // {
- // str.Format(_T("NULL"));
- // }
- // else
- // {
- str.Format(_T("QWindow Handle"));
- // }
- mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
- }
- // VAR_TYPE:: HTuple_Window_Var 类型
- else if (pInf->VarType == VAR_TYPE:: HTuple_Calibration_Var)
- {
- str.Format(_T("HCalibration"));
- mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
- }
- else if (pInf->VarType == VAR_TYPE::HTuple_CamParam_PoseCalib_Var)
- {
- str.Format(_T("HCamParam_PoseCalib"));
- mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
- }
- // VAR_TYPE:: HTuple_Point_Var 类型
- else if (pInf->VarType == VAR_TYPE::HTuple_Point_Var)
- {
- str.Format(_T("HPoints"));
- mType = INTERFACE_DISP_TYPE::IVDT_CSTRING;
- }
- else
- {
- str.Format(_T("NULL"));
- }
- return mType;
- }
- /////////////////////////////////////////////////////////
- // ExecuteTool
- void CTaskWindow::OnExecuteTool(CString strToolName)
- {
- if (m_SelTools.size() < 0 || m_SelTools.size() > 500)
- {
- CString strMsg;
- strMsg.Format(_T("Task[%d] ExecuteTool Error"), m_nIndex);
- USES_CONVERSION;
- char * s_str = T2A(strMsg);
- g_Logger.Error(0, __FILE__, __LINE__, s_str);
- return;
- }
- // 重置所有工具执行状态
- ResetToolExecCode();
- // 从头到尾执行所有的execute
- for (unsigned int i = 0; i < m_SelTools.size(); i++)
- {
- TOOL& tool = m_SelTools[i];
- if (tool.bEnable)
- {
- if (tool.strShowName == strToolName)
- {
- tool.nExecCode = RT_NONE;
- for (unsigned int j = 0; j < tool.Interfaces.size(); j++)
- {
- TOOL_INTERFACE& inf = tool.Interfaces[j];
- if (inf.InfDirection == INF_IN && inf.bShow == true && inf.nUseCount > 0)
- {
- BOOL bStatus = DoDllInput(/*tool,*/ inf);
- if (!bStatus)
- {
- CString str = tool.strShowName +_T(".") + inf.strName;
-
- CString strMsg;
- strMsg.Format(_T("Task[%d] %s DoDllInput Error"), m_nIndex, str);
- USES_CONVERSION;
- char * s_str = T2A(strMsg);
- g_Logger.Error(0, __FILE__, __LINE__, s_str);
- }
- }
- }
- int nRetCode = 0;
- nRetCode = tool.pDllPtr->Execute();
- tool.nExecCode = nRetCode;
- }
- }
- else
- {
- tool.nExecCode = RT_INVALID;
- }
- }
- // m_wndTree.Invalidate(TRUE);
- }
- //////////////////////////////////////////////////////
- // 添加一个新的输入接口到所选的工具中,返回插入的索引值
- int CTaskWindow::AddDynamicInterface(TOOL& activeTool, const DLL_INF& dllInf, const HTREEITEM& handle)
- {
- int newIndex = 0;
- // 生成新的输入接口
- TOOL_INTERFACE newInterface;
- newInterface.strParentName = activeTool.strShowName;
- newInterface.strName = dllInf.strName;
- newInterface.VarType = dllInf.VarType;
- newInterface.InfDirection = dllInf.InfDirection;
- newInterface.InfWay = dllInf.InfWay;
- newInterface.InfType = dllInf.InfType;
- // 首先检查是否有重复的
- for (unsigned int i = 0; i < activeTool.Interfaces.size(); i++)
- {
- if (newInterface.strName.Compare(activeTool.Interfaces[i].strName) == 0)
- {
- CString strMsg;
- strMsg.Format(_T("Task[%d] 添加 %s 接口失败,此名字的接口已存在!"), m_nIndex, newInterface.strName);
- USES_CONVERSION;
- char * s_str = T2A(strMsg);
- g_Logger.Error(0, __FILE__, __LINE__, s_str);
- return -1;
- }
- }
- // 寻找添加位置
- int nIgnoreCount = 0;
- bool bFind = false;
- for (unsigned int i = 0; i < activeTool.Interfaces.size(); i++)
- {
- TOOL_INTERFACE& inf = activeTool.Interfaces[i];
- // 需要跳过控件接口以及隐藏的接口
- if (inf.InfType == INF_CONTROL || !inf.bShow)
- {
- nIgnoreCount++;
- }
- if (newInterface.InfDirection == INF_IN)
- {
- if (inf.InfDirection == INF_OUT || inf.InfType == INF_CONTROL)
- {
- bFind = true;
- }
- }
- else if (newInterface.InfDirection == INF_OUT)
- {
- if (inf.InfType == INF_CONTROL)
- {
- bFind = true;
- }
- }
- // 找到位置加入新的接口节点
- if (bFind)
- {
- newIndex = i;
- newInterface.nIndex = i;
- activeTool.Interfaces.insert(activeTool.Interfaces.begin() + newIndex, newInterface);
- // 然后后序所有的节点序号后移
- for (unsigned int j = newIndex; j < activeTool.Interfaces.size(); j++)
- {
- activeTool.Interfaces[j].nIndex++;
- }
- break;
- }
- }
- // 如果没找到的话,则说明tool没有输出和控件接口,那么直接添加到最后
- if (!bFind)
- {
- activeTool.Interfaces.push_back(newInterface);
- newIndex = (int)activeTool.Interfaces.size() - 1;
- }
- // 然后将接口添加到界面中
- AddNewInterfaceToTree(handle, activeTool.Interfaces[newIndex], newIndex - nIgnoreCount);
- return newIndex;
- }
- /////////////////////////////////////////////////////////////
- // 添加一个新的接口到界面中
- void CTaskWindow::AddNewInterfaceToTree(const HTREEITEM& hTool, TOOL_INTERFACE& newInf, const int& nPos)
- {
- // 强制展开节点
- // m_wndTree.Expand(hTool, TVE_EXPAND);
- //
- // int nCount = 1;
- // HTREEITEM hAfter = m_wndTree.GetChildItem(hTool);
- // while (NULL != hAfter && nCount<nPos)
- // {
- // nCount++;
- //
- // hAfter = m_wndTree.GetNextSiblingItem(hAfter);
- // }
- //
- // // 将新的接口信息添加到树形控件中指定位置中
- // if (newInf.InfDirection == INF_IN)
- // {
- // newInf.hHandle = m_wndTree.InsertItem(newInf.strName, 1 /* Image inedex */, 1, hTool, hAfter);
- // }
- // else
- // {
- // newInf.hHandle = m_wndTree.InsertItem(newInf.strName, 2 /* Image inedex */, 2, hTool, hAfter);
- // }
- }
- ///////////////////////////////////////////////////////////
- // 2019-3-23 动态隐藏接口
- void CTaskWindow::OnItemHideInterface(const HTREEITEM& handle)
- {
- }
|