#include "ToolDialog.h" #include #include #include #include #include ////////////////////////////////////////////////////////////////////////// #include "Qsci/qsciapis.h" #include "Qsci/qscilexercpp.h" #include "Qsci/qscilexercpp.h" #include "Qsci/qscilexerlua.h" #include "Qsci/qsciscintilla.h" ////////////////////////////////////////////////////////////////////////// ToolDialogImpl::ToolDialogImpl(QWidget *parent, DllTool* pDllTool) : DllToolDialog(parent) , m_pDllTool(pDllTool) { ui.setupUi(this); this->setWindowFlags(Qt::Dialog | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint /*| Qt::WindowStaysOnTopHint*/); // 设置比例 QList sizes; sizes << 1000 << 5000; ui.splitter->setSizes(sizes); // 显示控件初始化 hwndUnit = new HWndUnit(this); Util::ShowUnitInWidget(ui.Image_widget, hwndUnit); hwndUnit->setWndTitle(("WindowHandle")); connect(this, SIGNAL(sigUpdateUI()), this, SLOT(on_UpdateUI())); ///////////////////////////////>>//////////////// scriptEdit = new QsciScintilla(ui.widgetEditor); scriptEdit->setMarginType(0, QsciScintilla::MarginType:: NumberMargin); //设置编号为0的页边显示行号。 scriptEdit->setMarginLineNumbers(0, true); //启用行号 scriptEdit->setMarginWidth(0, 26); // QsciLexerCPP* textLexer = new QsciLexerCPP; // textLexer = new QsciLexerCPP; // textLexer->setColor(QColor("#008000"), QsciLexerCPP::Comment); // textLexer->setColor(QColor("#ff0000"), QsciLexerCPP::Number); // textLexer->setColor(QColor("#008000"), QsciLexerCPP::CommentLineDoc); // textLexer->setColor(QColor("#008000"), QsciLexerCPP::DoubleQuotedString); // textLexer->setColor(QColor("#ff00ff"), QsciLexerCPP::SingleQuotedString); // textLexer->setColor(QColor("#0055ff"), QsciLexerCPP::Keyword); // textLexer->setColor(QColor("#0055ff"), QsciLexerCPP::PreProcessor); // // // textLexer->setDefaultPaper("#464646"); // scriptEdit->setLexer(textLexer); //开启自动缩进 scriptEdit->setAutoIndent(true); //设置缩进的显示方式 //scriptEdit->setIndentationGuides(QsciScintilla::SC_IV_LOOKBOTH); //显示选中的行号 scriptEdit->setCaretLineVisible(true); //显示选中行号的背景色 scriptEdit->setCaretLineBackgroundColor(Qt::lightGray); //左侧行号显示的背景色 scriptEdit->setMarginsBackgroundColor(Qt::gray); //设置括号匹配 scriptEdit->setBraceMatching(QsciScintilla::BraceMatch::SloppyBraceMatch); scriptEdit->setFolding(QsciScintilla::FoldStyle::BoxedTreeFoldStyle);//折叠样式 scriptEdit->setFoldMarginColors(Qt::gray, Qt::lightGray);//折叠栏颜色 scriptEdit->setAutoCompletionThreshold(3);//设置每输入一个字符就会出现自动补全的提示 scriptEdit->setFont(QFont("Courier New")); //设置字体 scriptEdit->SendScintilla(QsciScintilla::SCI_SETCODEPAGE, QsciScintilla::SC_CP_UTF8); //设置编码为UTF-8 connect(scriptEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified())); QVBoxLayout* vLayout = new QVBoxLayout(); vLayout->setContentsMargins(1, 1, 1, 1); vLayout->setObjectName(QStringLiteral("vLayout")); vLayout->addWidget(scriptEdit); ui.widgetEditor->setLayout(vLayout); ////////////////////////////////<Clear(); //绑定 HDevProcedureCall proc_call_fib(proc_fib); for (int i = 0; i < m_HalconScript.Function.size(); i++) { H_FUNCTION& mFunction = m_HalconScript.Function[i]; for (int j = 0; j < mFunction.Interfaces_io.size(); j++) { H_INTERFACE_HOBJECT& h_inf = mFunction.Interfaces_io[j]; QString strName = h_inf.strName; HImage img = mFunction.Interfaces_io[j].ho_Object; proc_call_fib.SetInputIconicParamObject(strName.toStdString().c_str(), img); // hwndUnit->ShowImage(img); // hwndUnit->Refresh(); } for (int j = 0; j < mFunction.Interfaces_ic.size(); j++) { QString strName = mFunction.Interfaces_ic.at(j).strName; if (strName == "WindowHandle") { HTuple hv_WindowHandle = hwndUnit->getHWndCtrl()->getWindowID(); proc_call_fib.SetInputCtrlParamTuple(strName.toStdString().c_str(), hv_WindowHandle); } else { HTuple hv_Tuple = mFunction.Interfaces_ic.at(j).hv_HTuple; proc_call_fib.SetInputCtrlParamTuple(strName.toStdString().c_str(), hv_Tuple); } } } //执行 proc_call_fib.Execute(); for (int i = 0; i < m_HalconScript.Function.size(); i++) { H_FUNCTION& mFunction = m_HalconScript.Function[i]; for (int j = 0; j < mFunction.Interfaces_oo.size(); j++) { H_INTERFACE_HOBJECT& h_Inf = mFunction.Interfaces_oo[j]; QString strName = h_Inf.strName; HObject obj; //if (h_Inf.strSem_type == ("object")) { obj = proc_call_fib.GetOutputIconicParamObject(strName.toStdString().c_str()); mFunction.Interfaces_oo[j].ho_Object = obj; } GetDebugDataPtr()->addObj(mFunction.Interfaces_oo[j].ho_Object, qRgb(255, 0, 0)); } } for (int i = 0; i < m_HalconScript.Function.size(); i++) { H_FUNCTION& mFunction = m_HalconScript.Function[i]; for (int j = 0; j < mFunction.Interfaces_oc.size(); j++) { H_INTERFACE_HTUPLE& h_Inf = mFunction.Interfaces_oc[j]; QString strName = h_Inf.strName; HTuple tup; proc_call_fib.GetOutputCtrlParamTuple(strName.toStdString().c_str(), & tup ); mFunction.Interfaces_oc[j].hv_HTuple = tup; GetDebugDataPtr()->addMsg(tup); } } } catch (HException& HDevExpDefaultException) { HTuple hv_Exception; HDevExpDefaultException.ToHTuple(&hv_Exception); int nExceptionID = (hv_Exception.D()); QString strCodeName = HDevExpDefaultException.ProcName().Text(); QString strErrorMsg = HDevExpDefaultException.ErrorMessage().Text(); } catch (HDevEngineException& hdev_exception) { string msg = (("### %s %d:%s\n"), hdev_exception.ProgLineName(), hdev_exception.ProgLineNum(), hdev_exception.Message()); QString strError(msg.c_str()); qWarning() << strError; } } catch (HException& HDevExpDefaultException) { HTuple hv_Exception; HDevExpDefaultException.ToHTuple(&hv_Exception); int nExceptionID = (hv_Exception.D()); QString strCodeName = HDevExpDefaultException.ProcName().Text(); QString strErrorMsg = HDevExpDefaultException.ErrorMessage().Text(); } catch (HDevEngineException& hdev_exception) { string msg = (("### %s %d:%s\n"), hdev_exception.ProgLineName(), hdev_exception.ProgLineNum(), hdev_exception.Message()); QString strError(msg.c_str()); qWarning() << strError; } } // hwndUnit->ShowImage(m_Image); //hwndUnit->ShowObj(m_hoBrushObj, qRgb(255, 0, 0), 0.5); // hwndUnit->Refresh(); } catch (...) { qWarning() << "Execute() Error"; } emit sigUpdateUI(); //m_TestClass.SetValue(m_Value3); return VPEnum::RETURN_VALUE::Success; } /// /// /// /// void ToolDialogImpl::Running(bool bRun) { } /// /// /// /// void ToolDialogImpl::timerEvent(QTimerEvent* event) { } /// /// /// /// /// bool ToolDialogImpl::bindValuePtrByName(const QString strName, const int nIndex) { // 先添加输入端口 for (int i = 0; i < m_HalconScript.Function.size(); i++) { H_FUNCTION& mFunction = m_HalconScript.Function[i]; if (mFunction.strName != "main") { // 添加输入的图形变量 for (int i = 0; i < mFunction.Interfaces_io.size(); i++) { if (strName == mFunction.Interfaces_io[i].strName) { m_pDllTool->SetValue(nIndex, mFunction.Interfaces_io[i].ho_Object); return true; } } // 添加输入的 控制变量 for (int i = 0; i < mFunction.Interfaces_ic.size(); i++) { if (mFunction.Interfaces_ic[i].strName != "WindowHandle") { if (strName == mFunction.Interfaces_ic[i].strName) { m_pDllTool->SetValue(nIndex, mFunction.Interfaces_ic[i].hv_HTuple); return true; } } } // 添加输出的 图形变量 for (int i = 0; i < mFunction.Interfaces_oo.size(); i++) { if (strName == mFunction.Interfaces_oo[i].strName) { m_pDllTool->SetValue(nIndex, mFunction.Interfaces_oo[i].ho_Object); return true; } } // 添加输出的 控制变量 for (int i = 0; i < mFunction.Interfaces_oc.size(); i++) { if (strName == mFunction.Interfaces_oc[i].strName) { m_pDllTool->SetValue(nIndex, mFunction.Interfaces_oc[i].hv_HTuple); return true; } } } } return true; } /// /// /// /// /// /// bool ToolDialogImpl::Serialized(QDataStream& ar, bool bIsOut) { int paranum;//参数数量 if (bIsOut)//保存参数流程 { paranum = 5; ar << paranum;//先保存参数数量 ar << (int)1 << m_strScriptBaseName; ar << (int)2 << m_strProcName; ar << (int)3 << m_HalconScript.strFileVersion; ar << (int)4 << m_HalconScript.strHalconVersion; ar << (int)5 << (int)m_HalconScript.Function.size(); { for (int i = 0; i < m_HalconScript.Function.size(); i++) { H_FUNCTION& mFunction = m_HalconScript.Function[i]; ar << mFunction.strName; ar << mFunction.strFunctionName; ar << (int)mFunction.Src.size(); for (int j = 0; j < mFunction.Src.size(); j++) { ar << mFunction.Src.at(j); } ar << (int)mFunction.Interfaces_io.size(); for (int k = 0; k < mFunction.Interfaces_io.size(); k++) { H_INTERFACE_HOBJECT& inf = mFunction.Interfaces_io[k]; ar << inf.strName; ar << inf.strBase_type; ar << inf.strDimension; ar << inf.strSem_type; } ar << (int)mFunction.Interfaces_oo.size(); for (int k = 0; k < mFunction.Interfaces_oo.size(); k++) { H_INTERFACE_HOBJECT& inf = mFunction.Interfaces_oo[k]; ar << inf.strName; ar << inf.strBase_type; ar << inf.strDimension; ar << inf.strSem_type; } ar << (int)mFunction.Interfaces_ic.size(); for (int k = 0; k < mFunction.Interfaces_ic.size(); k++) { H_INTERFACE_HTUPLE& inf = mFunction.Interfaces_ic[k]; ar << inf.strName; ar << inf.strBase_type; ar << inf.strDimension; ar << inf.strSem_type; } ar << (int)mFunction.Interfaces_oc.size(); for (int k = 0; k < mFunction.Interfaces_oc.size(); k++) { H_INTERFACE_HTUPLE& inf = mFunction.Interfaces_oc[k]; ar << inf.strName; ar << inf.strBase_type; ar << inf.strDimension; ar << inf.strSem_type; } } } } else//加载参数流程,参数加载顺序一定要跟保存顺序一致 { int nFumctionNameSize = 0; int para; ar >> paranum;//读取参数数量 for (int i = 0; i < paranum; i++) { ar >> para; switch (para) { case 1: ar >> m_strScriptBaseName; break; case 2: ar >> m_strProcName; break; case 3: ar >> m_HalconScript.strFileVersion; break; case 4: ar >> m_HalconScript.strHalconVersion; break; case 5: ar >> nFumctionNameSize; break; default: { qWarning() << "Serialized(In) Error" ; return false; } break; } } { ui.comboBoxFunction->clear(); for (int i = 0; i < nFumctionNameSize; i++) { int nSrcSize = 0; int nInterfaces_ioSize = 0; int nInterfaces_ooSize = 0; int nInterfaces_icSize = 0; int nInterfaces_ocSize = 0; H_FUNCTION mFunction; ar >> mFunction.strName; ar >> mFunction.strFunctionName; ar >> nSrcSize; for (int j = 0; j < nSrcSize; j++) { QString strSrc; ar >> strSrc; mFunction.Src.push_back(strSrc); } ar >> nInterfaces_ioSize; for (int k = 0; k < nInterfaces_ioSize; k++) { H_INTERFACE_HOBJECT inf; ar >> inf.strName; ar >> inf.strBase_type; ar >> inf.strDimension; ar >> inf.strSem_type; mFunction.Interfaces_io.push_back(inf); } ar >> nInterfaces_ooSize; for (int k = 0; k < nInterfaces_ooSize; k++) { H_INTERFACE_HOBJECT inf; ar >> inf.strName; ar >> inf.strBase_type; ar >> inf.strDimension; ar >> inf.strSem_type; mFunction.Interfaces_oo.push_back(inf); } ar >> nInterfaces_icSize; for (int k = 0; k < nInterfaces_icSize; k++) { H_INTERFACE_HTUPLE inf; ar >> inf.strName; ar >> inf.strBase_type; ar >> inf.strDimension; ar >> inf.strSem_type; mFunction.Interfaces_ic.push_back(inf); } ar >> nInterfaces_ocSize; for (int k = 0; k < nInterfaces_ocSize; k++) { H_INTERFACE_HTUPLE inf; ar >> inf.strName; ar >> inf.strBase_type; ar >> inf.strDimension; ar >> inf.strSem_type; mFunction.Interfaces_oc.push_back(inf); } m_HalconScript.Function.push_back(mFunction); ui.comboBoxFunction->addItem(mFunction.strFunctionName); ui.comboBoxFunction->setCurrentIndex(1); } } //1 将反序列化的数据,刷新到UI UpdateScriptByEdit(m_strFunctionName, true); QString strPath = "./TempScript.hdev"; //更新 编辑器的代码到数据结构 // 编译之前保存脚本到临时文件 SaveXML(strPath); // 编译成功 if (Compile(strPath)) { }; //3 添加端口 //4 完成数据与端口的绑定 } return true; } void ToolDialogImpl::on_UpdateUI() { } /// /// 确定 /// void ToolDialogImpl::on_btnOK_clicked() { this->hide(); } /// /// 取消按钮 /// void ToolDialogImpl::on_btnCancel_clicked() { // 将本工具的恢复到打开工具之前的状态 RecoverData(); } /// /// 测试按钮 /// void ToolDialogImpl::on_btnExecute_clicked() { QElapsedTimer toolTimer; toolTimer.start(); // 发送事件 ToolEvent* pToolEvent = new ToolEvent(m_strPouName, m_strInstanceName, TOOL_EVENT_TYPE::TOOL_TRIGGER); QCoreApplication::sendEvent(m_pEventTarget, pToolEvent); // 统计返回值 VPEnum::RETURN_VALUE ret = pToolEvent->ret; double nExecTime = toolTimer.elapsed(); QString str; str = QString("耗时: %1 ms").arg(nExecTime, 0, 'G', 5); ui.label_time->setText(str); str = QString("状态: %1 ").arg(QMetaEnum::fromType().key((short)ret)); ui.label_state->setText(str); delete pToolEvent; } /// /// /// void ToolDialogImpl::on_btnLoad_clicked() { QString runPath = QCoreApplication::applicationDirPath(); //获取exe路径 QString OpenFile, OpenFilePath; OpenFile = QFileDialog::getOpenFileName(this, "please choose an Script file", runPath + "//..//Scrtpt", "Image Files(*.hdev);"); if (!OpenFile.isEmpty()) { // 清理端口信息 再开始加载新的脚本 resetValueToInf(); if (ParseToolXML(OpenFile)) { qDebug() << "Load Script is " << OpenFile << "ok "; } QDateTime time = QDateTime::currentDateTime(); QFileInfo fileInfo(OpenFile); m_strScriptBaseName = QString("%1%2%3%4") .arg(fileInfo.completeBaseName()) .arg("<") .arg(QString::number(time.toTime_t())) .arg(">"); // 编译脚本 //更新 UpdateScriptByEdit(m_strFunctionName, true); // 添加端口 addNewValueToInf(); } } /// /// /// void ToolDialogImpl::on_btnExport_clicked() { // 编译之前保存脚本到临时文件 QString runPath = QCoreApplication::applicationDirPath(); //获取exe路径 QString strFileName = "Project_" + QDateTime::currentDateTime().toString("[yyyy-MM-dd hh-mm]"); QString strPath = runPath + "//..//Scrtpt//" + strFileName; QString strFilePath = QFileDialog::getSaveFileName ( this, "Save As", strPath, ("VisionPlus Files (*.hdev)") ); // 如果选定了文件名 if (!strFilePath.isNull()) { SaveXML(strFilePath); } } void ToolDialogImpl::on_btnCompile_clicked() { if (scriptEdit) { // QString strSrc = scriptEdit->text(); QString strPath = "./TempScript.hdev"; //更新 编辑器的代码到数据结构 UpdateScriptByEdit(m_strFunctionName, false); // 编译之前保存脚本到临时文件 SaveXML(strPath); // 编译成功 if (Compile(strPath)) { }; } } /// /// /// void ToolDialogImpl::documentWasModified() { } /// /// ParseToolXML /// /// /// bool ToolDialogImpl::ParseToolXML(QString strPath) { pugi::xml_document xmlDoc; if (!xmlDoc.load_file(strPath.toStdString().c_str() , pugi::parse_default, pugi::encoding_utf8)) { return false; } m_HalconScript.Reset(); ui.comboBoxFunction->clear(); pugi::xml_node nodeHdevelop = xmlDoc.child("hdevelop"); // 读取hdevelop节 m_HalconScript.strFileVersion = nodeHdevelop.attribute("file_version").as_string(); m_HalconScript.strHalconVersion = nodeHdevelop.attribute("halcon_version").as_string(); // 读取 procedure 节 pugi::xml_node nodeProcedure = nodeHdevelop.child("procedure"); for (pugi::xml_node nodePro = nodeProcedure; nodePro; nodePro = nodePro.next_sibling()) { H_FUNCTION mFunction; mFunction.Reset(); int ix = 0; QString strName(nodePro.attribute("name").as_string()); mFunction.strName = strName; if (strName != ("main")) { m_strProcName = mFunction.strName; } QString strFunction; strFunction = mFunction.strName + " ( ";// // 读取 interface 节 pugi::xml_node nodeInterface = nodePro.child("interface"); // 读取 io 节 pugi::xml_node nodeIO = nodeInterface.child("io"); ix = 0; for (pugi::xml_node nodeIF = nodeIO.first_child(); nodeIF; nodeIF = nodeIF.next_sibling()) { H_INTERFACE_HOBJECT inf; // 读取hdevelop节 inf.strName = nodeIF.attribute("name").as_string(); inf.strBase_type = nodeIF.attribute("base_type").as_string(); inf.strDimension = nodeIF.attribute("dimension").as_string(); mFunction.Interfaces_io.push_back(inf); if (ix > 0) { strFunction = strFunction + (", "); } strFunction = strFunction + inf.strName;// Name ( io ix++; } strFunction = strFunction + (" : ");// Name ( io : // 读取 oo 节 pugi::xml_node nodeOO = nodeInterface.child("oo"); ix = 0; for (pugi::xml_node nodeIF = nodeOO.first_child(); nodeIF; nodeIF = nodeIF.next_sibling()) { H_INTERFACE_HOBJECT inf; // 读取hdevelop节 inf.strName = nodeIF.attribute("name").as_string(); inf.strBase_type = nodeIF.attribute("base_type").as_string(); inf.strDimension = nodeIF.attribute("dimension").as_string(); mFunction.Interfaces_oo.push_back(inf); if (ix > 0) { strFunction = strFunction + (", "); } strFunction = strFunction + inf.strName;// Name ( io : oo ix++; } strFunction = strFunction + (" : ");// Name ( io : oo : // 读取 ic 节 pugi::xml_node nodeIC = nodeInterface.child("ic"); ix = 0; for (pugi::xml_node nodeIF = nodeIC.first_child(); nodeIF; nodeIF = nodeIF.next_sibling()) { H_INTERFACE_HTUPLE inf; // 读取hdevelop节 inf.strName = nodeIF.attribute("name").as_string(); inf.strBase_type = nodeIF.attribute("base_type").as_string(); inf.strDimension = nodeIF.attribute("dimension").as_string(); mFunction.Interfaces_ic.push_back(inf); if (ix > 0) { strFunction = strFunction + (", "); } strFunction = strFunction + inf.strName;// Name ( io : oo : ic ix++; } strFunction = strFunction + (" : ");// Name ( io : oo : ic : // 读取 oc 节 pugi::xml_node nodeOC = nodeInterface.child("oc"); ix = 0; for (pugi::xml_node nodeIF = nodeOC.first_child(); nodeIF; nodeIF = nodeIF.next_sibling()) { H_INTERFACE_HTUPLE inf; // 读取hdevelop节 inf.strName = nodeIF.attribute("name").as_string(); inf.strBase_type = nodeIF.attribute("base_type").as_string(); inf.strDimension = nodeIF.attribute("dimension").as_string(); mFunction.Interfaces_oc.push_back(inf); if (ix > 0) { strFunction = strFunction + (", "); } strFunction = strFunction + inf.strName;// Name ( io : oo : ic : oc ix++; } strFunction = strFunction + " )";// Name ( io : oo : ic : oc ) mFunction.strFunctionName = strFunction; // 读取 body 节 pugi::xml_node nodeBody = nodePro.child("body"); //遍历 for (pugi::xml_node node = nodeBody.first_child(); node != nullptr; node = node.next_sibling()) { QString strSrc = node.child_value();//node.text().as_string() if (!strSrc.isEmpty()) { mFunction.Src.push_back(QString(strSrc)); } } // 读取 body 节 pugi::xml_node nodeParameter = nodePro.child("docu").child("parameters")/*.child("parameter")*/; for (pugi::xml_node nodeIF = nodeParameter.first_child(); nodeIF; nodeIF = nodeIF.next_sibling()) { QString str = nodeIF.attribute("id").as_string(); QString strIName(str); for (pugi::xml_node node = nodeIF.first_child(); node != nullptr; node = node.next_sibling()) { QString strSem_type = node.child_value();//node.text().as_string() for (int i = 0; i < mFunction.Interfaces_io.size(); i++) { if (strIName == mFunction.Interfaces_io.at(i).strName) { mFunction.Interfaces_io[i].strSem_type = strSem_type; } } for (int i = 0; i < mFunction.Interfaces_oo.size(); i++) { if (strIName == mFunction.Interfaces_oo.at(i).strName) { mFunction.Interfaces_oo[i].strSem_type = strSem_type; } } for (int i = 0; i < mFunction.Interfaces_ic.size(); i++) { if (strIName == mFunction.Interfaces_ic.at(i).strName) { mFunction.Interfaces_ic[i].strSem_type = strSem_type; } } for (int i = 0; i < mFunction.Interfaces_oc.size(); i++) { if (strIName == mFunction.Interfaces_oc.at(i).strName) { mFunction.Interfaces_oc[i].strSem_type = strSem_type; } } } } m_HalconScript.Function.push_back(mFunction); m_strFunctionName = strFunction; ui.comboBoxFunction->addItem(strFunction); ui.comboBoxFunction->setCurrentIndex(1); } return TRUE; } /// /// /// /// /// bool ToolDialogImpl::SaveXML(QString strPath) { // //char * p = str); pugi::xml_document xmlDoc; pugi::xml_node nodeHdevelop = xmlDoc.append_child("hdevelop"); // 声明 pugi::xml_node pre = xmlDoc.prepend_child(pugi::node_declaration); pre.append_attribute("version") = "1.0"; pre.append_attribute("encoding") = "UTF-8"; // 增加hdevelop version 节 nodeHdevelop.append_attribute("file_version").set_value((m_HalconScript.strFileVersion.toStdString().c_str() )); nodeHdevelop.append_attribute("halcon_version").set_value((m_HalconScript.strHalconVersion.toStdString().c_str())); for (int i = 0; i < m_HalconScript.Function.size(); i++) { H_FUNCTION& mFunction = m_HalconScript.Function[i]; // 增加 procedure 节 pugi::xml_node nodeProcedure = nodeHdevelop.append_child("procedure"); nodeProcedure.append_attribute("name").set_value(mFunction.strName.toStdString().c_str()); // 增加 interface 节 pugi::xml_node nodeInterface = nodeProcedure.append_child("interface"); if (mFunction.Interfaces_io.size() > 0) { // 在interface 中 增加 IO 节 pugi::xml_node nodeIO = nodeInterface.append_child("io"); for (int32_t i = 0; i < mFunction.Interfaces_io.size(); ++i) { pugi::xml_node nodePar = nodeIO.append_child("par"); H_INTERFACE_HOBJECT& inf = mFunction.Interfaces_io[i]; // 增加属性 nodePar.append_attribute("name").set_value(inf.strName.toStdString().c_str()); nodePar.append_attribute("base_type").set_value(inf.strBase_type.toStdString().c_str()); nodePar.append_attribute("dimension").set_value(inf.strDimension.toStdString().c_str()); } } if (mFunction.Interfaces_oo.size() > 0) { // 在interface 中 增加 OO 节 pugi::xml_node nodeOO = nodeInterface.append_child("oo"); for (int32_t i = 0; i < mFunction.Interfaces_oo.size(); ++i) { pugi::xml_node nodePar = nodeOO.append_child("par"); H_INTERFACE_HOBJECT& inf = mFunction.Interfaces_oo[i]; // 增加属性 nodePar.append_attribute("name").set_value(inf.strName.toStdString().c_str()); nodePar.append_attribute("base_type").set_value(inf.strBase_type.toStdString().c_str()); nodePar.append_attribute("dimension").set_value(inf.strDimension.toStdString().c_str()); } } if (mFunction.Interfaces_ic.size() > 0) { // 在interface 中 增加 IC 节 pugi::xml_node nodeIC = nodeInterface.append_child("ic"); for (int32_t i = 0; i < mFunction.Interfaces_ic.size(); ++i) { pugi::xml_node nodePar = nodeIC.append_child("par"); H_INTERFACE_HTUPLE& inf = mFunction.Interfaces_ic[i]; // nodePar.append_attribute("name").set_value(inf.strName.toStdString().c_str()); nodePar.append_attribute("base_type").set_value(inf.strBase_type.toStdString().c_str()); nodePar.append_attribute("dimension").set_value(inf.strDimension.toStdString().c_str()); } } if (mFunction.Interfaces_oc.size() > 0) { // 在interface 中 增加 OC 节 pugi::xml_node nodeOC = nodeInterface.append_child("oc"); for (int32_t i = 0; i < mFunction.Interfaces_oc.size(); ++i) { pugi::xml_node nodePar = nodeOC.append_child("par"); H_INTERFACE_HTUPLE& inf = mFunction.Interfaces_oc[i]; // 增加属性 nodePar.append_attribute("name").set_value(inf.strName.toStdString().c_str()); nodePar.append_attribute("base_type").set_value(inf.strBase_type.toStdString().c_str()); nodePar.append_attribute("dimension").set_value(inf.strDimension.toStdString().c_str()); } } // 增加 body 节 pugi::xml_node nodeBody = nodeProcedure.append_child("body"); // 在body 中 增加 l 节 for (size_t i = 0; i < mFunction.Src.size(); i++) { pugi::xml_node node1 = nodeBody.append_child("l"); pugi::xml_node nodeScr1 = node1.append_child(pugi::node_pcdata); QString strSrc = mFunction.Src.at(i); if (!strSrc.isEmpty() ) { strSrc = strSrc.trimmed(); } nodeScr1.set_value(mFunction.Src.at(i).toStdString().c_str()); } // 增加 docu 节 pugi::xml_node nodeDocu = nodeProcedure.append_child("docu"); nodeDocu.append_attribute("id").set_value(mFunction.strName.toStdString().c_str()); // 在docu 中 增加 short 节 pugi::xml_node nodeShort = nodeDocu.append_child("short"); nodeShort.append_attribute("lang").set_value("en_US"); pugi::xml_node nodeCommentBooks4 = nodeShort.append_child(pugi::node_pcdata); nodeCommentBooks4.set_value(mFunction.strName.toStdString().c_str()); // 在docu 中 增加 parameters 节 pugi::xml_node nodeParameters = nodeDocu.append_child("parameters"); for (int32_t i = 0; i < mFunction.Interfaces_io.size(); ++i) { H_INTERFACE_HOBJECT& inf = mFunction.Interfaces_io[i]; pugi::xml_node nodeBook = nodeParameters.append_child("parameter"); // 增加属性 nodeBook.append_attribute("id").set_value(inf.strName.toStdString().c_str()); if (!inf.strSem_type.isEmpty()) { pugi::xml_node nodeSem_type = nodeBook.append_child("sem_type"); pugi::xml_node nodeType = nodeSem_type.append_child(pugi::node_pcdata); nodeType.set_value(inf.strSem_type.toStdString().c_str()); } } for (int32_t i = 0; i < mFunction.Interfaces_oo.size(); ++i) { H_INTERFACE_HOBJECT& inf = mFunction.Interfaces_oo[i]; pugi::xml_node nodeBook = nodeParameters.append_child("parameter"); // 增加属性 nodeBook.append_attribute("id").set_value(inf.strName.toStdString().c_str()); if (!inf.strSem_type.isEmpty()) { pugi::xml_node nodeSem_type = nodeBook.append_child("sem_type"); pugi::xml_node nodeType = nodeSem_type.append_child(pugi::node_pcdata); nodeType.set_value(inf.strSem_type.toStdString().c_str()); } } for (int32_t i = 0; i < mFunction.Interfaces_ic.size(); ++i) { H_INTERFACE_HTUPLE& inf = mFunction.Interfaces_ic[i]; pugi::xml_node nodeBook = nodeParameters.append_child("parameter"); // 增加属性 nodeBook.append_attribute("id").set_value(inf.strName.toStdString().c_str()); if (!inf.strSem_type.isEmpty()) { pugi::xml_node nodeSem_type = nodeBook.append_child("sem_type"); pugi::xml_node nodeType = nodeSem_type.append_child(pugi::node_pcdata); nodeType.set_value(inf.strSem_type.toStdString().c_str()); } } for (int32_t i = 0; i < mFunction.Interfaces_oc.size(); ++i) { H_INTERFACE_HTUPLE& inf = mFunction.Interfaces_oc[i]; pugi::xml_node nodeBook = nodeParameters.append_child("parameter"); // 增加属性 nodeBook.append_attribute("id").set_value(inf.strName.toStdString().c_str()); if (!inf.strSem_type.isEmpty()) { pugi::xml_node nodeSem_type = nodeBook.append_child("sem_type"); pugi::xml_node nodeType = nodeSem_type.append_child(pugi::node_pcdata); nodeType.set_value(inf.strSem_type.toStdString().c_str()); } } } xmlDoc.save_file(strPath.toStdString().c_str(), "\t", 1U, pugi::encoding_utf8); return TRUE; } /// /// /// /// void ToolDialogImpl::UpdateScriptByEdit(QString FunctionName, bool isLoad) { if (isLoad) { for (int i = 0; i < m_HalconScript.Function.size(); i++) { H_FUNCTION& mFunction = m_HalconScript.Function[i]; if (mFunction.strName != ("main")) { //m_strProcName = mFunction.strName; QString strSrc; for (int i = 0; i < mFunction.Src.size(); i++) { QString src = mFunction.Src[i] + ("\r\n"); strSrc = strSrc + src; } scriptEdit->setText(strSrc); } } } else { QString veQString = scriptEdit->text(); QStringList listSrc = veQString.split('\r'); for (int i = 0; i < m_HalconScript.Function.size(); i++) { H_FUNCTION& mFunction = m_HalconScript.Function[i]; if (mFunction.strName != ("main")) { //if (mFunction.strFunctionName == FunctionName) { m_HalconScript.Function[i].Src.clear(); for (int j= 0;j < listSrc.size() ;j++) { QString str = listSrc[j]; m_HalconScript.Function[i].Src.push_back(str); } } } } } } /// /// /// /// bool ToolDialogImpl::Compile(QString strPath) { // TODO: 在此添加控件通知处理程序代码 try { QString str; //m_wndBindingFunction.GetLBText(m_wndBindingFunction.GetCurSel(), str); QString ComName = str; //指定脚本路径 if (m_strProcName.isEmpty()) { ui.textOutput->append("Compile Error ProcName.isEmpty !" ); return false; } my_program.LoadProgram(strPath.toStdString().c_str() ); proc_fib.LoadProcedure(my_program, m_strProcName.toStdString().c_str() ); //显式编译 if (1) { proc_fib.CompileUsedProcedures(); } ui.textOutput->append("Compile OK !"); QFile::remove(strPath); return true; } catch (HDevEngineException& hdev_exception) { hdev_exception = hdev_exception; // suppress warning string msg = (("### %s %d:%s"), hdev_exception.ProgLineName(), hdev_exception.ProgLineNum(), hdev_exception.Message()); QString strError(msg.c_str()); //qWarning() << strError; ui.textOutput->append(strError); return false; } return false; } void ToolDialogImpl::on_checkBoxEnableDebug_stateChanged(int arg1) { } /// /// /// /// bool ToolDialogImpl::addNewValueToInf() { // 通知exe端添加对应的接口数据结构 QList dynInfs; // 先添加输入端口 for (int i = 0; i < m_HalconScript.Function.size(); i++) { H_FUNCTION& mFunction = m_HalconScript.Function.at(i); if (mFunction.strName != "main") { // 添加输入的图形变量 for (int i = 0; i < mFunction.Interfaces_io.size(); i++) { H_INTERFACE_HOBJECT& h_inf = mFunction.Interfaces_io[i]; DLL_INF inf; inf.strName = h_inf.strName; inf.Direction = INF_DIRECTION::INF_DIR_IN; inf.value.passMode = VALUE_PASS_MODE::PASS_BY_VALUE; if (h_inf.strSem_type == "image") { inf.value.type = VALUE_TYPE::Type_HImage; } else { inf.value.type = VALUE_TYPE::Type_HObject; } inf.value.Ptr = (void**)&mFunction.Interfaces_io[i].ho_Object; inf.nIndex = m_pDllTool->GetInterfaceSize(); // 设置为动态接口 inf.bDynamic = true; // 动态添加 m_pDllTool->AddInterface(inf); // 添加到通知集合中 dynInfs.push_back(inf); } // 添加输入的 控制变量 for (int i = 0; i < mFunction.Interfaces_ic.size(); i++) { H_INTERFACE_HTUPLE h_inf = mFunction.Interfaces_ic[i]; // 屏蔽 WindowHandle 端口,此端口为内部端口 if (h_inf.strName != "WindowHandle") { DLL_INF inf; inf.strName = h_inf.strName; inf.Direction = INF_DIRECTION::INF_DIR_IN; inf.value.passMode = VALUE_PASS_MODE::PASS_BY_VALUE; inf.value.type = VALUE_TYPE::Type_HTuple; inf.value.Ptr = (void**)&mFunction.Interfaces_ic[i].hv_HTuple; inf.nIndex = m_pDllTool->GetInterfaceSize(); // 设置为动态接口 inf.bDynamic = true; // 动态添加 m_pDllTool->AddInterface(inf); // 添加到通知集合中 dynInfs.push_back(inf); } } // 添加输出的 图形变量 for (int i = 0; i < mFunction.Interfaces_oo.size(); i++) { H_INTERFACE_HOBJECT& h_inf = mFunction.Interfaces_oo[i]; DLL_INF inf; inf.strName = h_inf.strName; inf.Direction = INF_DIRECTION::INF_DIR_OUT; inf.value.passMode = VALUE_PASS_MODE::PASS_BY_VALUE; inf.value.type = VALUE_TYPE::Type_HObject; inf.value.Ptr = (void**)&mFunction.Interfaces_oo[i].ho_Object; inf.nIndex = m_pDllTool->GetInterfaceSize(); // 设置为动态接口 inf.bDynamic = true; // 动态添加 m_pDllTool->AddInterface(inf); // 添加到通知集合中 dynInfs.push_back(inf); } // 添加输出的 控制变量 for (int i = 0; i < mFunction.Interfaces_oc.size(); i++) { H_INTERFACE_HTUPLE& h_inf = mFunction.Interfaces_oc[i]; DLL_INF inf; inf.strName = h_inf.strName; inf.Direction = INF_DIRECTION::INF_DIR_OUT; inf.value.passMode = VALUE_PASS_MODE::PASS_BY_VALUE; inf.value.type = VALUE_TYPE::Type_HTuple; inf.value.Ptr = (void**)&mFunction.Interfaces_oc[i].hv_HTuple; inf.nIndex = m_pDllTool->GetInterfaceSize(); // 设置为动态接口 inf.bDynamic = true; // 动态添加 m_pDllTool->AddInterface(inf); // 添加到通知集合中 dynInfs.push_back(inf); } } } // 发送同步动态端口的通知 this->syncInterfacesToPou(dynInfs, true); return true; } /// /// /// /// bool ToolDialogImpl::resetValueToInf() { // 通知exe端添加对应的接口数据结构 QList dynInfs; // 要删除的接口名字 DLL_INF inf; // 删除输入端口 for (int i = 0; i < m_HalconScript.Function.size(); i++) { H_FUNCTION& mFunction = m_HalconScript.Function[i]; if (mFunction.strName != "main") { // 删除输入的图形变量 for (int j = 0; j < mFunction.Interfaces_io.size(); j++) { H_INTERFACE_HOBJECT h_inf = mFunction.Interfaces_io[j]; inf.strName = h_inf.strName; bool bRet = m_pDllTool->DelInterface(inf.strName);// 动态删除 if (bRet)// 如果删除成功,则添加到通知集合中 { dynInfs.push_back(inf);// 添加到通知集合中 } inf.reset(); } // 删除输入的 控制变量 for (int j = 0; j < mFunction.Interfaces_ic.size(); j++) { H_INTERFACE_HTUPLE& h_inf = mFunction.Interfaces_ic[j]; // 屏蔽 WindowHandle 端口,此端口为内部端口 if (h_inf.strName != "WindowHandle") { inf.strName = h_inf.strName; bool bRet = m_pDllTool->DelInterface(inf.strName);// 动态删除 if (bRet)// 如果删除成功,则添加到通知集合中 { dynInfs.push_back(inf);// 添加到通知集合中 } inf.reset(); } } // 删除输出的 图形变量 for (int j = 0; j < mFunction.Interfaces_oo.size(); j++) { H_INTERFACE_HOBJECT& h_inf = mFunction.Interfaces_oo[j]; inf.strName = h_inf.strName; bool bRet = m_pDllTool->DelInterface(inf.strName);// 动态删除 if (bRet)// 如果删除成功,则添加到通知集合中 { dynInfs.push_back(inf);// 添加到通知集合中 } inf.reset(); } // 删除输出的 控制变量 for (int j = 0; j < mFunction.Interfaces_oc.size(); j++) { H_INTERFACE_HTUPLE& h_inf = mFunction.Interfaces_oc[j]; inf.strName = h_inf.strName; bool bRet = m_pDllTool->DelInterface(inf.strName);// 动态删除 if (bRet)// 如果删除成功,则添加到通知集合中 { dynInfs.push_back(inf);// 添加到通知集合中 } inf.reset(); } } } // 发送同步动态端口的通知 if (dynInfs.size() > 0) { this->syncInterfacesToPou(dynInfs, false); } return true; } /// /// 2022-3-8 向exe中的同步最新的接口变动(一个或多个) /// /// void ToolDialogImpl::syncInterfacesToPou(QList pInfs, bool bAdd) { SyncInterfaceEvent infEvent;// = new SyncInterfaceEvent(); // 携带需要同步的多个接口 infEvent.setSyncValues(pInfs); // 设置要本工具的实例名称 infEvent.m_strInstanceName = this->m_strInstanceName; // 增加端口 infEvent.m_bAdd = bAdd; // post数值同步消息 QCoreApplication::sendEvent(this->m_pPouTarget, &infEvent); // 获取事件执行状态 bool bRet = infEvent.m_bSuccess; if (!bRet) { qWarning() << m_strInstanceName << " add interface error, reason: " << infEvent.m_strReason; } }