ToolDialog.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  1. #include "ToolDialog.h"
  2. #include <QImage>
  3. #include <QFileInfo>
  4. #include <QFileDialog>
  5. #include <QElapsedTimer>
  6. #include <QGridLayout>
  7. //////////////////////////////////////////////////////////////////////////
  8. #include "Qsci/qsciapis.h"
  9. #include "Qsci/qscilexercpp.h"
  10. #include "Qsci/qscilexercpp.h"
  11. #include "Qsci/qscilexerlua.h"
  12. #include "Qsci/qsciscintilla.h"
  13. //////////////////////////////////////////////////////////////////////////
  14. ToolDialogImpl::ToolDialogImpl(QWidget *parent, DllTool* pDllTool)
  15. : DllToolDialog(parent)
  16. , m_pDllTool(pDllTool)
  17. {
  18. ui.setupUi(this);
  19. this->setWindowFlags(Qt::Dialog | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint /*| Qt::WindowStaysOnTopHint*/);
  20. // 设置比例
  21. QList<int> sizes;
  22. sizes << 1000 << 5000;
  23. ui.splitter->setSizes(sizes);
  24. // 显示控件初始化
  25. hwndUnit = new HWndUnit(this);
  26. Util::ShowUnitInWidget(ui.Image_widget, hwndUnit);
  27. hwndUnit->setWndTitle(("WindowHandle"));
  28. connect(this, SIGNAL(sigUpdateUI()), this, SLOT(on_UpdateUI()));
  29. ///////////////////////////////>>////////////////
  30. scriptEdit = new QsciScintilla(ui.widgetEditor);
  31. scriptEdit->setMarginType(0, QsciScintilla::MarginType:: NumberMargin); //设置编号为0的页边显示行号。
  32. scriptEdit->setMarginLineNumbers(0, true); //启用行号
  33. scriptEdit->setMarginWidth(0, 26);
  34. // QsciLexerCPP* textLexer = new QsciLexerCPP;
  35. // textLexer = new QsciLexerCPP;
  36. // textLexer->setColor(QColor("#008000"), QsciLexerCPP::Comment);
  37. // textLexer->setColor(QColor("#ff0000"), QsciLexerCPP::Number);
  38. // textLexer->setColor(QColor("#008000"), QsciLexerCPP::CommentLineDoc);
  39. // textLexer->setColor(QColor("#008000"), QsciLexerCPP::DoubleQuotedString);
  40. // textLexer->setColor(QColor("#ff00ff"), QsciLexerCPP::SingleQuotedString);
  41. // textLexer->setColor(QColor("#0055ff"), QsciLexerCPP::Keyword);
  42. // textLexer->setColor(QColor("#0055ff"), QsciLexerCPP::PreProcessor);
  43. //
  44. //
  45. // textLexer->setDefaultPaper("#464646");
  46. // scriptEdit->setLexer(textLexer);
  47. //开启自动缩进
  48. scriptEdit->setAutoIndent(true);
  49. //设置缩进的显示方式
  50. //scriptEdit->setIndentationGuides(QsciScintilla::SC_IV_LOOKBOTH);
  51. //显示选中的行号
  52. scriptEdit->setCaretLineVisible(true);
  53. //显示选中行号的背景色
  54. scriptEdit->setCaretLineBackgroundColor(Qt::lightGray);
  55. //左侧行号显示的背景色
  56. scriptEdit->setMarginsBackgroundColor(Qt::gray);
  57. //设置括号匹配
  58. scriptEdit->setBraceMatching(QsciScintilla::BraceMatch::SloppyBraceMatch);
  59. scriptEdit->setFolding(QsciScintilla::FoldStyle::BoxedTreeFoldStyle);//折叠样式
  60. scriptEdit->setFoldMarginColors(Qt::gray, Qt::lightGray);//折叠栏颜色
  61. scriptEdit->setAutoCompletionThreshold(3);//设置每输入一个字符就会出现自动补全的提示
  62. scriptEdit->setFont(QFont("Courier New")); //设置字体
  63. scriptEdit->SendScintilla(QsciScintilla::SCI_SETCODEPAGE, QsciScintilla::SC_CP_UTF8); //设置编码为UTF-8
  64. connect(scriptEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
  65. QVBoxLayout* vLayout = new QVBoxLayout();
  66. vLayout->setContentsMargins(1, 1, 1, 1);
  67. vLayout->setObjectName(QStringLiteral("vLayout"));
  68. vLayout->addWidget(scriptEdit);
  69. ui.widgetEditor->setLayout(vLayout);
  70. ////////////////////////////////<<///////////////
  71. }
  72. ToolDialogImpl::~ToolDialogImpl()
  73. {
  74. if (scriptEdit != nullptr)
  75. {
  76. delete scriptEdit;
  77. scriptEdit = nullptr;
  78. }
  79. }
  80. VPEnum::RETURN_VALUE ToolDialogImpl::Execute()
  81. {
  82. //return VPEnum::RETURN_VALUE::Success;
  83. try
  84. {
  85. {
  86. try
  87. {
  88. HImage m_Image;
  89. try
  90. {
  91. GetDebugDataPtr()->Clear();
  92. //绑定
  93. HDevProcedureCall proc_call_fib(proc_fib);
  94. for (int i = 0; i < m_HalconScript.Function.size(); i++)
  95. {
  96. H_FUNCTION& mFunction = m_HalconScript.Function[i];
  97. for (int j = 0; j < mFunction.Interfaces_io.size(); j++)
  98. {
  99. H_INTERFACE_HOBJECT& h_inf = mFunction.Interfaces_io[j];
  100. QString strName = h_inf.strName;
  101. HImage img = mFunction.Interfaces_io[j].ho_Object;
  102. proc_call_fib.SetInputIconicParamObject(strName.toStdString().c_str(), img);
  103. // hwndUnit->ShowImage(img);
  104. // hwndUnit->Refresh();
  105. }
  106. for (int j = 0; j < mFunction.Interfaces_ic.size(); j++)
  107. {
  108. QString strName = mFunction.Interfaces_ic.at(j).strName;
  109. if (strName == "WindowHandle")
  110. {
  111. HTuple hv_WindowHandle = hwndUnit->getHWndCtrl()->getWindowID();
  112. proc_call_fib.SetInputCtrlParamTuple(strName.toStdString().c_str(), hv_WindowHandle);
  113. }
  114. else
  115. {
  116. HTuple hv_Tuple = mFunction.Interfaces_ic.at(j).hv_HTuple;
  117. proc_call_fib.SetInputCtrlParamTuple(strName.toStdString().c_str(), hv_Tuple);
  118. }
  119. }
  120. }
  121. //执行
  122. proc_call_fib.Execute();
  123. for (int i = 0; i < m_HalconScript.Function.size(); i++)
  124. {
  125. H_FUNCTION& mFunction = m_HalconScript.Function[i];
  126. for (int j = 0; j < mFunction.Interfaces_oo.size(); j++)
  127. {
  128. H_INTERFACE_HOBJECT& h_Inf = mFunction.Interfaces_oo[j];
  129. QString strName = h_Inf.strName;
  130. HObject obj;
  131. //if (h_Inf.strSem_type == ("object"))
  132. {
  133. obj = proc_call_fib.GetOutputIconicParamObject(strName.toStdString().c_str());
  134. mFunction.Interfaces_oo[j].ho_Object = obj;
  135. }
  136. GetDebugDataPtr()->addObj(mFunction.Interfaces_oo[j].ho_Object, qRgb(255, 0, 0));
  137. }
  138. }
  139. for (int i = 0; i < m_HalconScript.Function.size(); i++)
  140. {
  141. H_FUNCTION& mFunction = m_HalconScript.Function[i];
  142. for (int j = 0; j < mFunction.Interfaces_oc.size(); j++)
  143. {
  144. H_INTERFACE_HTUPLE& h_Inf = mFunction.Interfaces_oc[j];
  145. QString strName = h_Inf.strName;
  146. HTuple tup;
  147. proc_call_fib.GetOutputCtrlParamTuple(strName.toStdString().c_str(), & tup );
  148. mFunction.Interfaces_oc[j].hv_HTuple = tup;
  149. GetDebugDataPtr()->addMsg(tup);
  150. }
  151. }
  152. }
  153. catch (HException& HDevExpDefaultException)
  154. {
  155. HTuple hv_Exception;
  156. HDevExpDefaultException.ToHTuple(&hv_Exception);
  157. int nExceptionID = (hv_Exception.D());
  158. QString strCodeName = HDevExpDefaultException.ProcName().Text();
  159. QString strErrorMsg = HDevExpDefaultException.ErrorMessage().Text();
  160. }
  161. catch (HDevEngineException& hdev_exception)
  162. {
  163. string msg = (("### %s %d:%s\n"), hdev_exception.ProgLineName(), hdev_exception.ProgLineNum(),
  164. hdev_exception.Message());
  165. QString strError(msg.c_str());
  166. qWarning() << strError;
  167. }
  168. }
  169. catch (HException& HDevExpDefaultException)
  170. {
  171. HTuple hv_Exception;
  172. HDevExpDefaultException.ToHTuple(&hv_Exception);
  173. int nExceptionID = (hv_Exception.D());
  174. QString strCodeName = HDevExpDefaultException.ProcName().Text();
  175. QString strErrorMsg = HDevExpDefaultException.ErrorMessage().Text();
  176. }
  177. catch (HDevEngineException& hdev_exception)
  178. {
  179. string msg = (("### %s %d:%s\n"), hdev_exception.ProgLineName(), hdev_exception.ProgLineNum(),
  180. hdev_exception.Message());
  181. QString strError(msg.c_str());
  182. qWarning() << strError;
  183. }
  184. }
  185. // hwndUnit->ShowImage(m_Image);
  186. //hwndUnit->ShowObj(m_hoBrushObj, qRgb(255, 0, 0), 0.5);
  187. // hwndUnit->Refresh();
  188. }
  189. catch (...)
  190. {
  191. qWarning() << "Execute() Error";
  192. }
  193. emit sigUpdateUI();
  194. //m_TestClass.SetValue(m_Value3);
  195. return VPEnum::RETURN_VALUE::Success;
  196. }
  197. /// <summary>
  198. ///
  199. /// </summary>
  200. /// <param name="bRun"></param>
  201. void ToolDialogImpl::Running(bool bRun)
  202. {
  203. }
  204. /// <summary>
  205. ///
  206. /// </summary>
  207. /// <param name="event"></param>
  208. void ToolDialogImpl::timerEvent(QTimerEvent* event)
  209. {
  210. }
  211. /// <summary>
  212. ///
  213. /// </summary>
  214. /// <param name="nIndex"></param>
  215. /// <returns></returns>
  216. bool ToolDialogImpl::bindValuePtrByName(const QString strName, const int nIndex)
  217. {
  218. // 先添加输入端口
  219. for (int i = 0; i < m_HalconScript.Function.size(); i++)
  220. {
  221. H_FUNCTION& mFunction = m_HalconScript.Function[i];
  222. if (mFunction.strName != "main")
  223. {
  224. // 添加输入的图形变量
  225. for (int i = 0; i < mFunction.Interfaces_io.size(); i++)
  226. {
  227. if (strName == mFunction.Interfaces_io[i].strName)
  228. {
  229. m_pDllTool->SetValue(nIndex, mFunction.Interfaces_io[i].ho_Object);
  230. return true;
  231. }
  232. }
  233. // 添加输入的 控制变量
  234. for (int i = 0; i < mFunction.Interfaces_ic.size(); i++)
  235. {
  236. if (mFunction.Interfaces_ic[i].strName != "WindowHandle")
  237. {
  238. if (strName == mFunction.Interfaces_ic[i].strName)
  239. {
  240. m_pDllTool->SetValue(nIndex, mFunction.Interfaces_ic[i].hv_HTuple);
  241. return true;
  242. }
  243. }
  244. }
  245. // 添加输出的 图形变量
  246. for (int i = 0; i < mFunction.Interfaces_oo.size(); i++)
  247. {
  248. if (strName == mFunction.Interfaces_oo[i].strName)
  249. {
  250. m_pDllTool->SetValue(nIndex, mFunction.Interfaces_oo[i].ho_Object);
  251. return true;
  252. }
  253. }
  254. // 添加输出的 控制变量
  255. for (int i = 0; i < mFunction.Interfaces_oc.size(); i++)
  256. {
  257. if (strName == mFunction.Interfaces_oc[i].strName)
  258. {
  259. m_pDllTool->SetValue(nIndex, mFunction.Interfaces_oc[i].hv_HTuple);
  260. return true;
  261. }
  262. }
  263. }
  264. }
  265. return true;
  266. }
  267. /// <summary>
  268. ///
  269. /// </summary>
  270. /// <param name="ar"></param>
  271. /// <param name="bIsOut"></param>
  272. /// <returns></returns>
  273. bool ToolDialogImpl::Serialized(QDataStream& ar, bool bIsOut)
  274. {
  275. int paranum;//参数数量
  276. if (bIsOut)//保存参数流程
  277. {
  278. paranum = 5;
  279. ar << paranum;//先保存参数数量
  280. ar << (int)1 << m_strScriptBaseName;
  281. ar << (int)2 << m_strProcName;
  282. ar << (int)3 << m_HalconScript.strFileVersion;
  283. ar << (int)4 << m_HalconScript.strHalconVersion;
  284. ar << (int)5 << (int)m_HalconScript.Function.size();
  285. {
  286. for (int i = 0; i < m_HalconScript.Function.size(); i++)
  287. {
  288. H_FUNCTION& mFunction = m_HalconScript.Function[i];
  289. ar << mFunction.strName;
  290. ar << mFunction.strFunctionName;
  291. ar << (int)mFunction.Src.size();
  292. for (int j = 0; j < mFunction.Src.size(); j++)
  293. {
  294. ar << mFunction.Src.at(j);
  295. }
  296. ar << (int)mFunction.Interfaces_io.size();
  297. for (int k = 0; k < mFunction.Interfaces_io.size(); k++)
  298. {
  299. H_INTERFACE_HOBJECT& inf = mFunction.Interfaces_io[k];
  300. ar << inf.strName;
  301. ar << inf.strBase_type;
  302. ar << inf.strDimension;
  303. ar << inf.strSem_type;
  304. }
  305. ar << (int)mFunction.Interfaces_oo.size();
  306. for (int k = 0; k < mFunction.Interfaces_oo.size(); k++)
  307. {
  308. H_INTERFACE_HOBJECT& inf = mFunction.Interfaces_oo[k];
  309. ar << inf.strName;
  310. ar << inf.strBase_type;
  311. ar << inf.strDimension;
  312. ar << inf.strSem_type;
  313. }
  314. ar << (int)mFunction.Interfaces_ic.size();
  315. for (int k = 0; k < mFunction.Interfaces_ic.size(); k++)
  316. {
  317. H_INTERFACE_HTUPLE& inf = mFunction.Interfaces_ic[k];
  318. ar << inf.strName;
  319. ar << inf.strBase_type;
  320. ar << inf.strDimension;
  321. ar << inf.strSem_type;
  322. }
  323. ar << (int)mFunction.Interfaces_oc.size();
  324. for (int k = 0; k < mFunction.Interfaces_oc.size(); k++)
  325. {
  326. H_INTERFACE_HTUPLE& inf = mFunction.Interfaces_oc[k];
  327. ar << inf.strName;
  328. ar << inf.strBase_type;
  329. ar << inf.strDimension;
  330. ar << inf.strSem_type;
  331. }
  332. }
  333. }
  334. }
  335. else//加载参数流程,参数加载顺序一定要跟保存顺序一致
  336. {
  337. int nFumctionNameSize = 0;
  338. int para;
  339. ar >> paranum;//读取参数数量
  340. for (int i = 0; i < paranum; i++)
  341. {
  342. ar >> para;
  343. switch (para)
  344. {
  345. case 1: ar >> m_strScriptBaseName; break;
  346. case 2: ar >> m_strProcName; break;
  347. case 3: ar >> m_HalconScript.strFileVersion; break;
  348. case 4: ar >> m_HalconScript.strHalconVersion; break;
  349. case 5: ar >> nFumctionNameSize; break;
  350. default:
  351. {
  352. qWarning() << "Serialized(In) Error" ;
  353. return false;
  354. }
  355. break;
  356. }
  357. }
  358. {
  359. ui.comboBoxFunction->clear();
  360. for (int i = 0; i < nFumctionNameSize; i++)
  361. {
  362. int nSrcSize = 0;
  363. int nInterfaces_ioSize = 0;
  364. int nInterfaces_ooSize = 0;
  365. int nInterfaces_icSize = 0;
  366. int nInterfaces_ocSize = 0;
  367. H_FUNCTION mFunction;
  368. ar >> mFunction.strName;
  369. ar >> mFunction.strFunctionName;
  370. ar >> nSrcSize;
  371. for (int j = 0; j < nSrcSize; j++)
  372. {
  373. QString strSrc;
  374. ar >> strSrc;
  375. mFunction.Src.push_back(strSrc);
  376. }
  377. ar >> nInterfaces_ioSize;
  378. for (int k = 0; k < nInterfaces_ioSize; k++)
  379. {
  380. H_INTERFACE_HOBJECT inf;
  381. ar >> inf.strName;
  382. ar >> inf.strBase_type;
  383. ar >> inf.strDimension;
  384. ar >> inf.strSem_type;
  385. mFunction.Interfaces_io.push_back(inf);
  386. }
  387. ar >> nInterfaces_ooSize;
  388. for (int k = 0; k < nInterfaces_ooSize; k++)
  389. {
  390. H_INTERFACE_HOBJECT inf;
  391. ar >> inf.strName;
  392. ar >> inf.strBase_type;
  393. ar >> inf.strDimension;
  394. ar >> inf.strSem_type;
  395. mFunction.Interfaces_oo.push_back(inf);
  396. }
  397. ar >> nInterfaces_icSize;
  398. for (int k = 0; k < nInterfaces_icSize; k++)
  399. {
  400. H_INTERFACE_HTUPLE inf;
  401. ar >> inf.strName;
  402. ar >> inf.strBase_type;
  403. ar >> inf.strDimension;
  404. ar >> inf.strSem_type;
  405. mFunction.Interfaces_ic.push_back(inf);
  406. }
  407. ar >> nInterfaces_ocSize;
  408. for (int k = 0; k < nInterfaces_ocSize; k++)
  409. {
  410. H_INTERFACE_HTUPLE inf;
  411. ar >> inf.strName;
  412. ar >> inf.strBase_type;
  413. ar >> inf.strDimension;
  414. ar >> inf.strSem_type;
  415. mFunction.Interfaces_oc.push_back(inf);
  416. }
  417. m_HalconScript.Function.push_back(mFunction);
  418. ui.comboBoxFunction->addItem(mFunction.strFunctionName);
  419. ui.comboBoxFunction->setCurrentIndex(1);
  420. }
  421. }
  422. //1 将反序列化的数据,刷新到UI
  423. UpdateScriptByEdit(m_strFunctionName, true);
  424. QString strPath = "./TempScript.hdev";
  425. //更新 编辑器的代码到数据结构
  426. // 编译之前保存脚本到临时文件
  427. SaveXML(strPath);
  428. // 编译成功
  429. if (Compile(strPath))
  430. {
  431. };
  432. //3 添加端口
  433. //4 完成数据与端口的绑定
  434. }
  435. return true;
  436. }
  437. void ToolDialogImpl::on_UpdateUI()
  438. {
  439. }
  440. /// <summary>
  441. /// 确定
  442. /// </summary>
  443. void ToolDialogImpl::on_btnOK_clicked()
  444. {
  445. this->hide();
  446. }
  447. /// <summary>
  448. /// 取消按钮
  449. /// </summary>
  450. void ToolDialogImpl::on_btnCancel_clicked()
  451. {
  452. // 将本工具的恢复到打开工具之前的状态
  453. RecoverData();
  454. }
  455. /// <summary>
  456. /// 测试按钮
  457. /// </summary>
  458. void ToolDialogImpl::on_btnExecute_clicked()
  459. {
  460. QElapsedTimer toolTimer;
  461. toolTimer.start();
  462. // 发送事件
  463. ToolEvent* pToolEvent = new ToolEvent(m_strPouName, m_strInstanceName, TOOL_EVENT_TYPE::TOOL_TRIGGER);
  464. QCoreApplication::sendEvent(m_pEventTarget, pToolEvent);
  465. // 统计返回值
  466. VPEnum::RETURN_VALUE ret = pToolEvent->ret;
  467. double nExecTime = toolTimer.elapsed();
  468. QString str;
  469. str = QString("耗时: %1 ms").arg(nExecTime, 0, 'G', 5);
  470. ui.label_time->setText(str);
  471. str = QString("状态: %1 ").arg(QMetaEnum::fromType<VPEnum::RETURN_VALUE>().key((short)ret));
  472. ui.label_state->setText(str);
  473. delete pToolEvent;
  474. }
  475. /// <summary>
  476. ///
  477. /// </summary>
  478. void ToolDialogImpl::on_btnLoad_clicked()
  479. {
  480. QString runPath = QCoreApplication::applicationDirPath(); //获取exe路径
  481. QString OpenFile, OpenFilePath;
  482. OpenFile = QFileDialog::getOpenFileName(this,
  483. "please choose an Script file",
  484. runPath + "//..//Scrtpt",
  485. "Image Files(*.hdev);");
  486. if (!OpenFile.isEmpty())
  487. {
  488. // 清理端口信息 再开始加载新的脚本
  489. resetValueToInf();
  490. if (ParseToolXML(OpenFile))
  491. {
  492. qDebug() << "Load Script is " << OpenFile << "ok ";
  493. }
  494. QDateTime time = QDateTime::currentDateTime();
  495. QFileInfo fileInfo(OpenFile);
  496. m_strScriptBaseName = QString("%1%2%3%4")
  497. .arg(fileInfo.completeBaseName())
  498. .arg("<")
  499. .arg(QString::number(time.toTime_t()))
  500. .arg(">");
  501. // 编译脚本
  502. //更新
  503. UpdateScriptByEdit(m_strFunctionName, true);
  504. // 添加端口
  505. addNewValueToInf();
  506. }
  507. }
  508. /// <summary>
  509. ///
  510. /// </summary>
  511. void ToolDialogImpl::on_btnExport_clicked()
  512. {
  513. // 编译之前保存脚本到临时文件
  514. QString runPath = QCoreApplication::applicationDirPath(); //获取exe路径
  515. QString strFileName = "Project_" + QDateTime::currentDateTime().toString("[yyyy-MM-dd hh-mm]");
  516. QString strPath = runPath + "//..//Scrtpt//" + strFileName;
  517. QString strFilePath = QFileDialog::getSaveFileName
  518. (
  519. this,
  520. "Save As",
  521. strPath,
  522. ("VisionPlus Files (*.hdev)")
  523. );
  524. // 如果选定了文件名
  525. if (!strFilePath.isNull())
  526. {
  527. SaveXML(strFilePath);
  528. }
  529. }
  530. void ToolDialogImpl::on_btnCompile_clicked()
  531. {
  532. if (scriptEdit)
  533. {
  534. // QString strSrc = scriptEdit->text();
  535. QString strPath = "./TempScript.hdev";
  536. //更新 编辑器的代码到数据结构
  537. UpdateScriptByEdit(m_strFunctionName, false);
  538. // 编译之前保存脚本到临时文件
  539. SaveXML(strPath);
  540. // 编译成功
  541. if (Compile(strPath))
  542. {
  543. };
  544. }
  545. }
  546. /// <summary>
  547. ///
  548. /// </summary>
  549. void ToolDialogImpl::documentWasModified()
  550. {
  551. }
  552. /// <summary>
  553. /// ParseToolXML
  554. /// </summary>
  555. /// <param name="strPath"></param>
  556. /// <returns></returns>
  557. bool ToolDialogImpl::ParseToolXML(QString strPath)
  558. {
  559. pugi::xml_document xmlDoc;
  560. if (!xmlDoc.load_file(strPath.toStdString().c_str() , pugi::parse_default, pugi::encoding_utf8))
  561. {
  562. return false;
  563. }
  564. m_HalconScript.Reset();
  565. ui.comboBoxFunction->clear();
  566. pugi::xml_node nodeHdevelop = xmlDoc.child("hdevelop");
  567. // 读取hdevelop节
  568. m_HalconScript.strFileVersion = nodeHdevelop.attribute("file_version").as_string();
  569. m_HalconScript.strHalconVersion = nodeHdevelop.attribute("halcon_version").as_string();
  570. // 读取 procedure 节
  571. pugi::xml_node nodeProcedure = nodeHdevelop.child("procedure");
  572. for (pugi::xml_node nodePro = nodeProcedure; nodePro; nodePro = nodePro.next_sibling())
  573. {
  574. H_FUNCTION mFunction;
  575. mFunction.Reset();
  576. int ix = 0;
  577. QString strName(nodePro.attribute("name").as_string());
  578. mFunction.strName = strName;
  579. if (strName != ("main"))
  580. {
  581. m_strProcName = mFunction.strName;
  582. }
  583. QString strFunction;
  584. strFunction = mFunction.strName + " ( ";//
  585. // 读取 interface 节
  586. pugi::xml_node nodeInterface = nodePro.child("interface");
  587. // 读取 io 节
  588. pugi::xml_node nodeIO = nodeInterface.child("io");
  589. ix = 0;
  590. for (pugi::xml_node nodeIF = nodeIO.first_child(); nodeIF; nodeIF = nodeIF.next_sibling())
  591. {
  592. H_INTERFACE_HOBJECT inf;
  593. // 读取hdevelop节
  594. inf.strName = nodeIF.attribute("name").as_string();
  595. inf.strBase_type = nodeIF.attribute("base_type").as_string();
  596. inf.strDimension = nodeIF.attribute("dimension").as_string();
  597. mFunction.Interfaces_io.push_back(inf);
  598. if (ix > 0)
  599. {
  600. strFunction = strFunction + (", ");
  601. }
  602. strFunction = strFunction + inf.strName;// Name ( io
  603. ix++;
  604. }
  605. strFunction = strFunction + (" : ");// Name ( io :
  606. // 读取 oo 节
  607. pugi::xml_node nodeOO = nodeInterface.child("oo");
  608. ix = 0;
  609. for (pugi::xml_node nodeIF = nodeOO.first_child(); nodeIF; nodeIF = nodeIF.next_sibling())
  610. {
  611. H_INTERFACE_HOBJECT inf;
  612. // 读取hdevelop节
  613. inf.strName = nodeIF.attribute("name").as_string();
  614. inf.strBase_type = nodeIF.attribute("base_type").as_string();
  615. inf.strDimension = nodeIF.attribute("dimension").as_string();
  616. mFunction.Interfaces_oo.push_back(inf);
  617. if (ix > 0)
  618. {
  619. strFunction = strFunction + (", ");
  620. }
  621. strFunction = strFunction + inf.strName;// Name ( io : oo
  622. ix++;
  623. }
  624. strFunction = strFunction + (" : ");// Name ( io : oo :
  625. // 读取 ic 节
  626. pugi::xml_node nodeIC = nodeInterface.child("ic");
  627. ix = 0;
  628. for (pugi::xml_node nodeIF = nodeIC.first_child(); nodeIF; nodeIF = nodeIF.next_sibling())
  629. {
  630. H_INTERFACE_HTUPLE inf;
  631. // 读取hdevelop节
  632. inf.strName = nodeIF.attribute("name").as_string();
  633. inf.strBase_type = nodeIF.attribute("base_type").as_string();
  634. inf.strDimension = nodeIF.attribute("dimension").as_string();
  635. mFunction.Interfaces_ic.push_back(inf);
  636. if (ix > 0)
  637. {
  638. strFunction = strFunction + (", ");
  639. }
  640. strFunction = strFunction + inf.strName;// Name ( io : oo : ic
  641. ix++;
  642. }
  643. strFunction = strFunction + (" : ");// Name ( io : oo : ic :
  644. // 读取 oc 节
  645. pugi::xml_node nodeOC = nodeInterface.child("oc");
  646. ix = 0;
  647. for (pugi::xml_node nodeIF = nodeOC.first_child(); nodeIF; nodeIF = nodeIF.next_sibling())
  648. {
  649. H_INTERFACE_HTUPLE inf;
  650. // 读取hdevelop节
  651. inf.strName = nodeIF.attribute("name").as_string();
  652. inf.strBase_type = nodeIF.attribute("base_type").as_string();
  653. inf.strDimension = nodeIF.attribute("dimension").as_string();
  654. mFunction.Interfaces_oc.push_back(inf);
  655. if (ix > 0)
  656. {
  657. strFunction = strFunction + (", ");
  658. }
  659. strFunction = strFunction + inf.strName;// Name ( io : oo : ic : oc
  660. ix++;
  661. }
  662. strFunction = strFunction + " )";// Name ( io : oo : ic : oc )
  663. mFunction.strFunctionName = strFunction;
  664. // 读取 body 节
  665. pugi::xml_node nodeBody = nodePro.child("body");
  666. //遍历
  667. for (pugi::xml_node node = nodeBody.first_child(); node != nullptr; node = node.next_sibling())
  668. {
  669. QString strSrc = node.child_value();//node.text().as_string()
  670. if (!strSrc.isEmpty())
  671. {
  672. mFunction.Src.push_back(QString(strSrc));
  673. }
  674. }
  675. // 读取 body 节
  676. pugi::xml_node nodeParameter = nodePro.child("docu").child("parameters")/*.child("parameter")*/;
  677. for (pugi::xml_node nodeIF = nodeParameter.first_child(); nodeIF; nodeIF = nodeIF.next_sibling())
  678. {
  679. QString str = nodeIF.attribute("id").as_string();
  680. QString strIName(str);
  681. for (pugi::xml_node node = nodeIF.first_child(); node != nullptr; node = node.next_sibling())
  682. {
  683. QString strSem_type = node.child_value();//node.text().as_string()
  684. for (int i = 0; i < mFunction.Interfaces_io.size(); i++)
  685. {
  686. if (strIName == mFunction.Interfaces_io.at(i).strName)
  687. {
  688. mFunction.Interfaces_io[i].strSem_type = strSem_type;
  689. }
  690. }
  691. for (int i = 0; i < mFunction.Interfaces_oo.size(); i++)
  692. {
  693. if (strIName == mFunction.Interfaces_oo.at(i).strName)
  694. {
  695. mFunction.Interfaces_oo[i].strSem_type = strSem_type;
  696. }
  697. }
  698. for (int i = 0; i < mFunction.Interfaces_ic.size(); i++)
  699. {
  700. if (strIName == mFunction.Interfaces_ic.at(i).strName)
  701. {
  702. mFunction.Interfaces_ic[i].strSem_type = strSem_type;
  703. }
  704. }
  705. for (int i = 0; i < mFunction.Interfaces_oc.size(); i++)
  706. {
  707. if (strIName == mFunction.Interfaces_oc.at(i).strName)
  708. {
  709. mFunction.Interfaces_oc[i].strSem_type = strSem_type;
  710. }
  711. }
  712. }
  713. }
  714. m_HalconScript.Function.push_back(mFunction);
  715. m_strFunctionName = strFunction;
  716. ui.comboBoxFunction->addItem(strFunction);
  717. ui.comboBoxFunction->setCurrentIndex(1);
  718. }
  719. return TRUE;
  720. }
  721. /// <summary>
  722. ///
  723. /// </summary>
  724. /// <param name="strPath"></param>
  725. /// <returns></returns>
  726. bool ToolDialogImpl::SaveXML(QString strPath)
  727. {
  728. //
  729. //char * p = str);
  730. pugi::xml_document xmlDoc;
  731. pugi::xml_node nodeHdevelop = xmlDoc.append_child("hdevelop");
  732. // 声明
  733. pugi::xml_node pre = xmlDoc.prepend_child(pugi::node_declaration);
  734. pre.append_attribute("version") = "1.0";
  735. pre.append_attribute("encoding") = "UTF-8";
  736. // 增加hdevelop version 节
  737. nodeHdevelop.append_attribute("file_version").set_value((m_HalconScript.strFileVersion.toStdString().c_str() ));
  738. nodeHdevelop.append_attribute("halcon_version").set_value((m_HalconScript.strHalconVersion.toStdString().c_str()));
  739. for (int i = 0; i < m_HalconScript.Function.size(); i++)
  740. {
  741. H_FUNCTION& mFunction = m_HalconScript.Function[i];
  742. // 增加 procedure 节
  743. pugi::xml_node nodeProcedure = nodeHdevelop.append_child("procedure");
  744. nodeProcedure.append_attribute("name").set_value(mFunction.strName.toStdString().c_str());
  745. // 增加 interface 节
  746. pugi::xml_node nodeInterface = nodeProcedure.append_child("interface");
  747. if (mFunction.Interfaces_io.size() > 0)
  748. {
  749. // 在interface 中 增加 IO 节
  750. pugi::xml_node nodeIO = nodeInterface.append_child("io");
  751. for (int32_t i = 0; i < mFunction.Interfaces_io.size(); ++i)
  752. {
  753. pugi::xml_node nodePar = nodeIO.append_child("par");
  754. H_INTERFACE_HOBJECT& inf = mFunction.Interfaces_io[i];
  755. // 增加属性
  756. nodePar.append_attribute("name").set_value(inf.strName.toStdString().c_str());
  757. nodePar.append_attribute("base_type").set_value(inf.strBase_type.toStdString().c_str());
  758. nodePar.append_attribute("dimension").set_value(inf.strDimension.toStdString().c_str());
  759. }
  760. }
  761. if (mFunction.Interfaces_oo.size() > 0)
  762. {
  763. // 在interface 中 增加 OO 节
  764. pugi::xml_node nodeOO = nodeInterface.append_child("oo");
  765. for (int32_t i = 0; i < mFunction.Interfaces_oo.size(); ++i)
  766. {
  767. pugi::xml_node nodePar = nodeOO.append_child("par");
  768. H_INTERFACE_HOBJECT& inf = mFunction.Interfaces_oo[i];
  769. // 增加属性
  770. nodePar.append_attribute("name").set_value(inf.strName.toStdString().c_str());
  771. nodePar.append_attribute("base_type").set_value(inf.strBase_type.toStdString().c_str());
  772. nodePar.append_attribute("dimension").set_value(inf.strDimension.toStdString().c_str());
  773. }
  774. }
  775. if (mFunction.Interfaces_ic.size() > 0)
  776. {
  777. // 在interface 中 增加 IC 节
  778. pugi::xml_node nodeIC = nodeInterface.append_child("ic");
  779. for (int32_t i = 0; i < mFunction.Interfaces_ic.size(); ++i)
  780. {
  781. pugi::xml_node nodePar = nodeIC.append_child("par");
  782. H_INTERFACE_HTUPLE& inf = mFunction.Interfaces_ic[i];
  783. //
  784. nodePar.append_attribute("name").set_value(inf.strName.toStdString().c_str());
  785. nodePar.append_attribute("base_type").set_value(inf.strBase_type.toStdString().c_str());
  786. nodePar.append_attribute("dimension").set_value(inf.strDimension.toStdString().c_str());
  787. }
  788. }
  789. if (mFunction.Interfaces_oc.size() > 0)
  790. {
  791. // 在interface 中 增加 OC 节
  792. pugi::xml_node nodeOC = nodeInterface.append_child("oc");
  793. for (int32_t i = 0; i < mFunction.Interfaces_oc.size(); ++i)
  794. {
  795. pugi::xml_node nodePar = nodeOC.append_child("par");
  796. H_INTERFACE_HTUPLE& inf = mFunction.Interfaces_oc[i];
  797. // 增加属性
  798. nodePar.append_attribute("name").set_value(inf.strName.toStdString().c_str());
  799. nodePar.append_attribute("base_type").set_value(inf.strBase_type.toStdString().c_str());
  800. nodePar.append_attribute("dimension").set_value(inf.strDimension.toStdString().c_str());
  801. }
  802. }
  803. // 增加 body 节
  804. pugi::xml_node nodeBody = nodeProcedure.append_child("body");
  805. // 在body 中 增加 l 节
  806. for (size_t i = 0; i < mFunction.Src.size(); i++)
  807. {
  808. pugi::xml_node node1 = nodeBody.append_child("l");
  809. pugi::xml_node nodeScr1 = node1.append_child(pugi::node_pcdata);
  810. QString strSrc = mFunction.Src.at(i);
  811. if (!strSrc.isEmpty() )
  812. {
  813. strSrc = strSrc.trimmed();
  814. }
  815. nodeScr1.set_value(mFunction.Src.at(i).toStdString().c_str());
  816. }
  817. // 增加 docu 节
  818. pugi::xml_node nodeDocu = nodeProcedure.append_child("docu");
  819. nodeDocu.append_attribute("id").set_value(mFunction.strName.toStdString().c_str());
  820. // 在docu 中 增加 short 节
  821. pugi::xml_node nodeShort = nodeDocu.append_child("short");
  822. nodeShort.append_attribute("lang").set_value("en_US");
  823. pugi::xml_node nodeCommentBooks4 = nodeShort.append_child(pugi::node_pcdata);
  824. nodeCommentBooks4.set_value(mFunction.strName.toStdString().c_str());
  825. // 在docu 中 增加 parameters 节
  826. pugi::xml_node nodeParameters = nodeDocu.append_child("parameters");
  827. for (int32_t i = 0; i < mFunction.Interfaces_io.size(); ++i)
  828. {
  829. H_INTERFACE_HOBJECT& inf = mFunction.Interfaces_io[i];
  830. pugi::xml_node nodeBook = nodeParameters.append_child("parameter");
  831. // 增加属性
  832. nodeBook.append_attribute("id").set_value(inf.strName.toStdString().c_str());
  833. if (!inf.strSem_type.isEmpty())
  834. {
  835. pugi::xml_node nodeSem_type = nodeBook.append_child("sem_type");
  836. pugi::xml_node nodeType = nodeSem_type.append_child(pugi::node_pcdata);
  837. nodeType.set_value(inf.strSem_type.toStdString().c_str());
  838. }
  839. }
  840. for (int32_t i = 0; i < mFunction.Interfaces_oo.size(); ++i)
  841. {
  842. H_INTERFACE_HOBJECT& inf = mFunction.Interfaces_oo[i];
  843. pugi::xml_node nodeBook = nodeParameters.append_child("parameter");
  844. // 增加属性
  845. nodeBook.append_attribute("id").set_value(inf.strName.toStdString().c_str());
  846. if (!inf.strSem_type.isEmpty())
  847. {
  848. pugi::xml_node nodeSem_type = nodeBook.append_child("sem_type");
  849. pugi::xml_node nodeType = nodeSem_type.append_child(pugi::node_pcdata);
  850. nodeType.set_value(inf.strSem_type.toStdString().c_str());
  851. }
  852. }
  853. for (int32_t i = 0; i < mFunction.Interfaces_ic.size(); ++i)
  854. {
  855. H_INTERFACE_HTUPLE& inf = mFunction.Interfaces_ic[i];
  856. pugi::xml_node nodeBook = nodeParameters.append_child("parameter");
  857. // 增加属性
  858. nodeBook.append_attribute("id").set_value(inf.strName.toStdString().c_str());
  859. if (!inf.strSem_type.isEmpty())
  860. {
  861. pugi::xml_node nodeSem_type = nodeBook.append_child("sem_type");
  862. pugi::xml_node nodeType = nodeSem_type.append_child(pugi::node_pcdata);
  863. nodeType.set_value(inf.strSem_type.toStdString().c_str());
  864. }
  865. }
  866. for (int32_t i = 0; i < mFunction.Interfaces_oc.size(); ++i)
  867. {
  868. H_INTERFACE_HTUPLE& inf = mFunction.Interfaces_oc[i];
  869. pugi::xml_node nodeBook = nodeParameters.append_child("parameter");
  870. // 增加属性
  871. nodeBook.append_attribute("id").set_value(inf.strName.toStdString().c_str());
  872. if (!inf.strSem_type.isEmpty())
  873. {
  874. pugi::xml_node nodeSem_type = nodeBook.append_child("sem_type");
  875. pugi::xml_node nodeType = nodeSem_type.append_child(pugi::node_pcdata);
  876. nodeType.set_value(inf.strSem_type.toStdString().c_str());
  877. }
  878. }
  879. }
  880. xmlDoc.save_file(strPath.toStdString().c_str(), "\t", 1U, pugi::encoding_utf8);
  881. return TRUE;
  882. }
  883. /// <summary>
  884. ///
  885. /// </summary>
  886. /// <param name="FunctionName"></param>
  887. void ToolDialogImpl::UpdateScriptByEdit(QString FunctionName, bool isLoad)
  888. {
  889. if (isLoad)
  890. {
  891. for (int i = 0; i < m_HalconScript.Function.size(); i++)
  892. {
  893. H_FUNCTION& mFunction = m_HalconScript.Function[i];
  894. if (mFunction.strName != ("main"))
  895. {
  896. //m_strProcName = mFunction.strName;
  897. QString strSrc;
  898. for (int i = 0; i < mFunction.Src.size(); i++)
  899. {
  900. QString src = mFunction.Src[i] + ("\r\n");
  901. strSrc = strSrc + src;
  902. }
  903. scriptEdit->setText(strSrc);
  904. }
  905. }
  906. }
  907. else
  908. {
  909. QString veQString = scriptEdit->text();
  910. QStringList listSrc = veQString.split('\r');
  911. for (int i = 0; i < m_HalconScript.Function.size(); i++)
  912. {
  913. H_FUNCTION& mFunction = m_HalconScript.Function[i];
  914. if (mFunction.strName != ("main"))
  915. {
  916. //if (mFunction.strFunctionName == FunctionName)
  917. {
  918. m_HalconScript.Function[i].Src.clear();
  919. for (int j= 0;j < listSrc.size() ;j++)
  920. {
  921. QString str = listSrc[j];
  922. m_HalconScript.Function[i].Src.push_back(str);
  923. }
  924. }
  925. }
  926. }
  927. }
  928. }
  929. /// <summary>
  930. ///
  931. /// </summary>
  932. /// <param name="strPath"></param>
  933. bool ToolDialogImpl::Compile(QString strPath)
  934. {
  935. // TODO: 在此添加控件通知处理程序代码
  936. try
  937. {
  938. QString str;
  939. //m_wndBindingFunction.GetLBText(m_wndBindingFunction.GetCurSel(), str);
  940. QString ComName = str;
  941. //指定脚本路径
  942. if (m_strProcName.isEmpty())
  943. {
  944. ui.textOutput->append("Compile Error ProcName.isEmpty !" );
  945. return false;
  946. }
  947. my_program.LoadProgram(strPath.toStdString().c_str() );
  948. proc_fib.LoadProcedure(my_program, m_strProcName.toStdString().c_str() );
  949. //显式编译
  950. if (1)
  951. {
  952. proc_fib.CompileUsedProcedures();
  953. }
  954. ui.textOutput->append("Compile OK !");
  955. QFile::remove(strPath);
  956. return true;
  957. }
  958. catch (HDevEngineException& hdev_exception)
  959. {
  960. hdev_exception = hdev_exception; // suppress warning
  961. string msg = (("### %s %d:%s"), hdev_exception.ProgLineName(), hdev_exception.ProgLineNum(),
  962. hdev_exception.Message());
  963. QString strError(msg.c_str());
  964. //qWarning() << strError;
  965. ui.textOutput->append(strError);
  966. return false;
  967. }
  968. return false;
  969. }
  970. void ToolDialogImpl::on_checkBoxEnableDebug_stateChanged(int arg1)
  971. {
  972. }
  973. /// <summary>
  974. ///
  975. /// </summary>
  976. /// <returns></returns>
  977. bool ToolDialogImpl::addNewValueToInf()
  978. {
  979. // 通知exe端添加对应的接口数据结构
  980. QList<DLL_INF> dynInfs;
  981. // 先添加输入端口
  982. for (int i = 0; i < m_HalconScript.Function.size(); i++)
  983. {
  984. H_FUNCTION& mFunction = m_HalconScript.Function.at(i);
  985. if (mFunction.strName != "main")
  986. {
  987. // 添加输入的图形变量
  988. for (int i = 0; i < mFunction.Interfaces_io.size(); i++)
  989. {
  990. H_INTERFACE_HOBJECT& h_inf = mFunction.Interfaces_io[i];
  991. DLL_INF inf;
  992. inf.strName = h_inf.strName;
  993. inf.Direction = INF_DIRECTION::INF_DIR_IN;
  994. inf.value.passMode = VALUE_PASS_MODE::PASS_BY_VALUE;
  995. if (h_inf.strSem_type == "image")
  996. {
  997. inf.value.type = VALUE_TYPE::Type_HImage;
  998. }
  999. else
  1000. {
  1001. inf.value.type = VALUE_TYPE::Type_HObject;
  1002. }
  1003. inf.value.Ptr = (void**)&mFunction.Interfaces_io[i].ho_Object;
  1004. inf.nIndex = m_pDllTool->GetInterfaceSize();
  1005. // 设置为动态接口
  1006. inf.bDynamic = true;
  1007. // 动态添加
  1008. m_pDllTool->AddInterface(inf);
  1009. // 添加到通知集合中
  1010. dynInfs.push_back(inf);
  1011. }
  1012. // 添加输入的 控制变量
  1013. for (int i = 0; i < mFunction.Interfaces_ic.size(); i++)
  1014. {
  1015. H_INTERFACE_HTUPLE h_inf = mFunction.Interfaces_ic[i];
  1016. // 屏蔽 WindowHandle 端口,此端口为内部端口
  1017. if (h_inf.strName != "WindowHandle")
  1018. {
  1019. DLL_INF inf;
  1020. inf.strName = h_inf.strName;
  1021. inf.Direction = INF_DIRECTION::INF_DIR_IN;
  1022. inf.value.passMode = VALUE_PASS_MODE::PASS_BY_VALUE;
  1023. inf.value.type = VALUE_TYPE::Type_HTuple;
  1024. inf.value.Ptr = (void**)&mFunction.Interfaces_ic[i].hv_HTuple;
  1025. inf.nIndex = m_pDllTool->GetInterfaceSize();
  1026. // 设置为动态接口
  1027. inf.bDynamic = true;
  1028. // 动态添加
  1029. m_pDllTool->AddInterface(inf);
  1030. // 添加到通知集合中
  1031. dynInfs.push_back(inf);
  1032. }
  1033. }
  1034. // 添加输出的 图形变量
  1035. for (int i = 0; i < mFunction.Interfaces_oo.size(); i++)
  1036. {
  1037. H_INTERFACE_HOBJECT& h_inf = mFunction.Interfaces_oo[i];
  1038. DLL_INF inf;
  1039. inf.strName = h_inf.strName;
  1040. inf.Direction = INF_DIRECTION::INF_DIR_OUT;
  1041. inf.value.passMode = VALUE_PASS_MODE::PASS_BY_VALUE;
  1042. inf.value.type = VALUE_TYPE::Type_HObject;
  1043. inf.value.Ptr = (void**)&mFunction.Interfaces_oo[i].ho_Object;
  1044. inf.nIndex = m_pDllTool->GetInterfaceSize();
  1045. // 设置为动态接口
  1046. inf.bDynamic = true;
  1047. // 动态添加
  1048. m_pDllTool->AddInterface(inf);
  1049. // 添加到通知集合中
  1050. dynInfs.push_back(inf);
  1051. }
  1052. // 添加输出的 控制变量
  1053. for (int i = 0; i < mFunction.Interfaces_oc.size(); i++)
  1054. {
  1055. H_INTERFACE_HTUPLE& h_inf = mFunction.Interfaces_oc[i];
  1056. DLL_INF inf;
  1057. inf.strName = h_inf.strName;
  1058. inf.Direction = INF_DIRECTION::INF_DIR_OUT;
  1059. inf.value.passMode = VALUE_PASS_MODE::PASS_BY_VALUE;
  1060. inf.value.type = VALUE_TYPE::Type_HTuple;
  1061. inf.value.Ptr = (void**)&mFunction.Interfaces_oc[i].hv_HTuple;
  1062. inf.nIndex = m_pDllTool->GetInterfaceSize();
  1063. // 设置为动态接口
  1064. inf.bDynamic = true;
  1065. // 动态添加
  1066. m_pDllTool->AddInterface(inf);
  1067. // 添加到通知集合中
  1068. dynInfs.push_back(inf);
  1069. }
  1070. }
  1071. }
  1072. // 发送同步动态端口的通知
  1073. this->syncInterfacesToPou(dynInfs, true);
  1074. return true;
  1075. }
  1076. /// <summary>
  1077. ///
  1078. /// </summary>
  1079. /// <returns></returns>
  1080. bool ToolDialogImpl::resetValueToInf()
  1081. {
  1082. // 通知exe端添加对应的接口数据结构
  1083. QList<DLL_INF> dynInfs;
  1084. // 要删除的接口名字
  1085. DLL_INF inf;
  1086. // 删除输入端口
  1087. for (int i = 0; i < m_HalconScript.Function.size(); i++)
  1088. {
  1089. H_FUNCTION& mFunction = m_HalconScript.Function[i];
  1090. if (mFunction.strName != "main")
  1091. {
  1092. // 删除输入的图形变量
  1093. for (int j = 0; j < mFunction.Interfaces_io.size(); j++)
  1094. {
  1095. H_INTERFACE_HOBJECT h_inf = mFunction.Interfaces_io[j];
  1096. inf.strName = h_inf.strName;
  1097. bool bRet = m_pDllTool->DelInterface(inf.strName);// 动态删除
  1098. if (bRet)// 如果删除成功,则添加到通知集合中
  1099. {
  1100. dynInfs.push_back(inf);// 添加到通知集合中
  1101. }
  1102. inf.reset();
  1103. }
  1104. // 删除输入的 控制变量
  1105. for (int j = 0; j < mFunction.Interfaces_ic.size(); j++)
  1106. {
  1107. H_INTERFACE_HTUPLE& h_inf = mFunction.Interfaces_ic[j];
  1108. // 屏蔽 WindowHandle 端口,此端口为内部端口
  1109. if (h_inf.strName != "WindowHandle")
  1110. {
  1111. inf.strName = h_inf.strName;
  1112. bool bRet = m_pDllTool->DelInterface(inf.strName);// 动态删除
  1113. if (bRet)// 如果删除成功,则添加到通知集合中
  1114. {
  1115. dynInfs.push_back(inf);// 添加到通知集合中
  1116. }
  1117. inf.reset();
  1118. }
  1119. }
  1120. // 删除输出的 图形变量
  1121. for (int j = 0; j < mFunction.Interfaces_oo.size(); j++)
  1122. {
  1123. H_INTERFACE_HOBJECT& h_inf = mFunction.Interfaces_oo[j];
  1124. inf.strName = h_inf.strName;
  1125. bool bRet = m_pDllTool->DelInterface(inf.strName);// 动态删除
  1126. if (bRet)// 如果删除成功,则添加到通知集合中
  1127. {
  1128. dynInfs.push_back(inf);// 添加到通知集合中
  1129. }
  1130. inf.reset();
  1131. }
  1132. // 删除输出的 控制变量
  1133. for (int j = 0; j < mFunction.Interfaces_oc.size(); j++)
  1134. {
  1135. H_INTERFACE_HTUPLE& h_inf = mFunction.Interfaces_oc[j];
  1136. inf.strName = h_inf.strName;
  1137. bool bRet = m_pDllTool->DelInterface(inf.strName);// 动态删除
  1138. if (bRet)// 如果删除成功,则添加到通知集合中
  1139. {
  1140. dynInfs.push_back(inf);// 添加到通知集合中
  1141. }
  1142. inf.reset();
  1143. }
  1144. }
  1145. }
  1146. // 发送同步动态端口的通知
  1147. if (dynInfs.size() > 0)
  1148. {
  1149. this->syncInterfacesToPou(dynInfs, false);
  1150. }
  1151. return true;
  1152. }
  1153. /// <summary>
  1154. /// 2022-3-8 向exe中的同步最新的接口变动(一个或多个)
  1155. /// </summary>
  1156. /// <param name="pInfs"></param>
  1157. void ToolDialogImpl::syncInterfacesToPou(QList<DLL_INF> pInfs, bool bAdd)
  1158. {
  1159. SyncInterfaceEvent infEvent;// = new SyncInterfaceEvent();
  1160. // 携带需要同步的多个接口
  1161. infEvent.setSyncValues(pInfs);
  1162. // 设置要本工具的实例名称
  1163. infEvent.m_strInstanceName = this->m_strInstanceName;
  1164. // 增加端口
  1165. infEvent.m_bAdd = bAdd;
  1166. // post数值同步消息
  1167. QCoreApplication::sendEvent(this->m_pPouTarget, &infEvent);
  1168. // 获取事件执行状态
  1169. bool bRet = infEvent.m_bSuccess;
  1170. if (!bRet)
  1171. {
  1172. qWarning() << m_strInstanceName << " add interface error, reason: " << infEvent.m_strReason;
  1173. }
  1174. }