From e234c6705db93ce460c074ebd168fe66fe8efd5f Mon Sep 17 00:00:00 2001 From: Alexey Kondratyev Date: Thu, 14 Oct 2021 12:56:51 +0300 Subject: [PATCH] [bos #26444] [EDF] (2021) SHAPER: Improve treatment of parameters Add fixes. --- .../InitializationPlugin_EvalListener.cpp | 15 ++++---- src/ModelAPI/ModelAPI_Events.cpp | 12 +++---- src/ModelAPI/ModelAPI_Events.h | 28 +++++++-------- src/ParametersPlugin/CMakeLists.txt | 1 + .../ParametersPlugin_WidgetParamsMgr.cpp | 27 +++++++------- .../Test/TestImportParameters.py} | 36 ++++++++----------- .../Test/TestParameterCreationError.py | 21 +++++++++-- src/ParametersPlugin/data/parameters.txt | 11 ++++++ .../doc/TUI_parameterFeature.rst | 2 +- src/ParametersPlugin/doc/examples/File.txt | 6 ++-- .../model/parameter/import_parameter.py | 12 +++++-- 11 files changed, 101 insertions(+), 70 deletions(-) rename src/{PythonAPI/Test/TestInsertParameter.py => ParametersPlugin/Test/TestImportParameters.py} (62%) create mode 100644 src/ParametersPlugin/data/parameters.txt diff --git a/src/InitializationPlugin/InitializationPlugin_EvalListener.cpp b/src/InitializationPlugin/InitializationPlugin_EvalListener.cpp index 57c216f17..26126b8f5 100644 --- a/src/InitializationPlugin/InitializationPlugin_EvalListener.cpp +++ b/src/InitializationPlugin/InitializationPlugin_EvalListener.cpp @@ -78,7 +78,7 @@ InitializationPlugin_EvalListener::InitializationPlugin_EvalListener() aLoop->registerListener(this, ModelAPI_ParameterEvalMessage::eventId(), NULL, true); aLoop->registerListener(this, ModelAPI_BuildEvalMessage::eventId(), NULL, true); aLoop->registerListener(this, ModelAPI_ComputePositionsMessage::eventId(), NULL, true); - aLoop->registerListener(this, ModelAPI_PathEvalMessage::eventId(), NULL, true); + aLoop->registerListener(this, ModelAPI_ImportParametersMessage::eventId(), NULL, true); myInterp = std::shared_ptr(new InitializationPlugin_PyInterp()); myInterp->initialize(); @@ -180,12 +180,15 @@ void InitializationPlugin_EvalListener::processEvent( } aMsg->setResults(aParamsList, anError); } - else if (theMessage->eventID() == ModelAPI_PathEvalMessage::eventId()) + else if (theMessage->eventID() == ModelAPI_ImportParametersMessage::eventId()) { - std::shared_ptr aMsg = - std::dynamic_pointer_cast(theMessage); - std::string aPath = aMsg->parameter(); - myInterp->runString("from salome.shaper import model; doc = model.activeDocument(); model.importParameters(doc, \"" + aPath + "\")"); + std::shared_ptr aMsg = + std::dynamic_pointer_cast(theMessage); + std::string anImportScript("from salome.shaper import model;"); + std::string aDocScript("doc = model.activeDocument();"); + std::string anParamImpScript("model.importParameters(doc, \""); + std::string aPath = aMsg->filename(); + myInterp->runString(anImportScript + aDocScript + anParamImpScript + aPath + "\")"); } } diff --git a/src/ModelAPI/ModelAPI_Events.cpp b/src/ModelAPI/ModelAPI_Events.cpp index 4688732d8..506b26189 100644 --- a/src/ModelAPI/ModelAPI_Events.cpp +++ b/src/ModelAPI/ModelAPI_Events.cpp @@ -189,24 +189,24 @@ const std::string& ModelAPI_ParameterEvalMessage::error() const } /// Creates an empty message -ModelAPI_PathEvalMessage::ModelAPI_PathEvalMessage(const Events_ID theID, const void* theSender) +ModelAPI_ImportParametersMessage::ModelAPI_ImportParametersMessage(const Events_ID theID, const void* theSender) :Events_Message(theID, theSender) { } -ModelAPI_PathEvalMessage::~ModelAPI_PathEvalMessage() +ModelAPI_ImportParametersMessage::~ModelAPI_ImportParametersMessage() { } -std::string ModelAPI_PathEvalMessage::parameter() const +std::string ModelAPI_ImportParametersMessage::filename() const { - return myParam; + return myFilename; } -void ModelAPI_PathEvalMessage::setParameter(std::string theParam) +void ModelAPI_ImportParametersMessage::setFilename(std::string theFilename) { - myParam = theParam; + myFilename = theFilename; } ModelAPI_BuildEvalMessage::ModelAPI_BuildEvalMessage( diff --git a/src/ModelAPI/ModelAPI_Events.h b/src/ModelAPI/ModelAPI_Events.h index 3d04cae18..2ebd25b0f 100644 --- a/src/ModelAPI/ModelAPI_Events.h +++ b/src/ModelAPI/ModelAPI_Events.h @@ -352,42 +352,42 @@ class ModelAPI_ParameterEvalMessage : public Events_Message MODELAPI_EXPORT const std::string& error() const; }; -class ModelAPI_PathEvalMessage : public Events_Message +class ModelAPI_ImportParametersMessage : public Events_Message { - std::string myParam; ///< parameters that should be evaluated + std::string myFilename; ///< filename where where parameters are stored std::string myError; ///< error of processing, empty if there is no error public: /// Static. Returns EventID of the message. MODELAPI_EXPORT static Events_ID& eventId() { - static const char* MY_PARAMETER_EVALUATION_EVENT_ID("PathEvalMessage"); + static const char* MY_PARAMETER_EVALUATION_EVENT_ID("ImportParametersMessage"); static Events_ID anId = Events_Loop::eventByName(MY_PARAMETER_EVALUATION_EVENT_ID); return anId; } /// Useful method that creates and sends the event. /// Returns the message, processed, with the resulting fields filled. - MODELAPI_EXPORT static std::shared_ptr + MODELAPI_EXPORT static std::shared_ptr send(std::string theParameter, const void* theSender) { - std::shared_ptr aMessage = - std::shared_ptr( - new ModelAPI_PathEvalMessage(eventId(), theSender)); - aMessage->setParameter(theParameter); + std::shared_ptr aMessage = + std::shared_ptr( + new ModelAPI_ImportParametersMessage(eventId(), theSender)); + aMessage->setFilename(theParameter); Events_Loop::loop()->send(aMessage); return aMessage; } /// Creates an empty message - MODELAPI_EXPORT ModelAPI_PathEvalMessage(const Events_ID theID, const void* theSender = 0); + MODELAPI_EXPORT ModelAPI_ImportParametersMessage(const Events_ID theID, const void* theSender = 0); /// The virtual destructor - MODELAPI_EXPORT virtual ~ModelAPI_PathEvalMessage(); + MODELAPI_EXPORT virtual ~ModelAPI_ImportParametersMessage(); - /// Returns a parameter stored in the message - MODELAPI_EXPORT std::string parameter() const; - /// Sets a parameter to the message - MODELAPI_EXPORT void setParameter(std::string theParam); + /// Returns a filename stored in the message + MODELAPI_EXPORT std::string filename() const; + /// Sets a filename to the message + MODELAPI_EXPORT void setFilename(std::string theFilename); }; class ModelAPI_BuildEvalMessage : public Events_Message diff --git a/src/ParametersPlugin/CMakeLists.txt b/src/ParametersPlugin/CMakeLists.txt index 9f1140baa..4244e870b 100644 --- a/src/ParametersPlugin/CMakeLists.txt +++ b/src/ParametersPlugin/CMakeLists.txt @@ -137,4 +137,5 @@ if(${HAVE_SALOME}) endforeach(tfile ${TEST_NAMES}) install(FILES ${TMP_TESTS_NAMES} DESTINATION ${TEST_INSTALL_DIRECTORY}) + install(DIRECTORY data DESTINATION ${TEST_INSTALL_DIRECTORY}) endif(${HAVE_SALOME}) diff --git a/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.cpp b/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.cpp index 3f3385ec9..ac367dfac 100644 --- a/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.cpp +++ b/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.cpp @@ -698,21 +698,24 @@ void ParametersPlugin_WidgetParamsMgr::onRemove() void ParametersPlugin_WidgetParamsMgr::onImport() { - QStringList aPathes = QFileDialog::getOpenFileNames(nullptr, "Select txt file", "", "Text files (*.txt)", nullptr, {QFileDialog::ExistingFiles}); - if (aPathes.empty()) + std::string aWinText("Select txt file"); + std::string aFileType("Text files (*.txt);;All files (*.*)"); + QFlags aOption = {QFileDialog::ExistingFile}; + QString aQPath = QFileDialog::getOpenFileName(nullptr, + aWinText.c_str(), "", + aFileType.c_str(), nullptr, + aOption); + if (aQPath.size() == 0) return; - for (auto aPathIter = aPathes.begin(); aPathIter != aPathes.end(); ++aPathIter) - { - std::string aPath(aPathIter->toStdString()); - std::shared_ptr aMessage = - std::shared_ptr( - new ModelAPI_PathEvalMessage(ModelAPI_PathEvalMessage::eventId())); - aMessage->setParameter(aPath); - Events_Loop::loop()->send(aMessage); - } + std::string aPath(aQPath.toStdString()); + std::shared_ptr aMessage = + std::shared_ptr( + new ModelAPI_ImportParametersMessage(ModelAPI_ImportParametersMessage::eventId())); + aMessage->setFilename(aPath); + Events_Loop::loop()->send(aMessage); + updateParametersFeatures(); - /*updateFeaturesPart();*/ updateParametersPart(); } diff --git a/src/PythonAPI/Test/TestInsertParameter.py b/src/ParametersPlugin/Test/TestImportParameters.py similarity index 62% rename from src/PythonAPI/Test/TestInsertParameter.py rename to src/ParametersPlugin/Test/TestImportParameters.py index 739206e4e..25750d0f3 100644 --- a/src/PythonAPI/Test/TestInsertParameter.py +++ b/src/ParametersPlugin/Test/TestImportParameters.py @@ -1,4 +1,4 @@ -# Copyright (C) 2014-2020 CEA/DEN, EDF R&D +# Copyright (C) 2014-2021 CEA/DEN, EDF R&D # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -20,38 +20,30 @@ from salome.shaper import model import os +from PyQt5.Qt import QApplication + +import salome +salome.salome_init_without_session() +salome.salome_init(1) +if QApplication.instance() is None: + app = QApplication([]) + +data_dir = os.path.join(os.path.dirname(inspect.getfile(lambda: None)), "data") + model.begin() partSet = model.moduleDocument() Part_1 = model.addPart(partSet) Part_1_doc = Part_1.document() -data_dir = os.path.join(os.path.dirname(sys.argv[0]), "Test_data") - -nameFile = "PythonAPI_test_parametres1.txt" +nameFile = "parameters.txt" aDir = os.path.join(data_dir, nameFile) -aFile = open(nameFile, 'w', encoding = "utf_8") - -aFile.write("Longueur 36. # \"Longueur de la pièce\"\n") -aFile.write("Largeur 24. # Largeur de la pièce\n") -aFile.write("Hauteur Longueur*Largeur\n") -aFile.write("\n") -aFile.write(" \n") -aFile.write("Largeur2\n") -aFile.write("Largeur3 #Comment\n") -aFile.write("A12 5. * 5.\n") -aFile.write("# Comment\n") -aFile.write("Name#Comment\n") -aFile.write(" # Comm\n") -aFile.write("Longueur2 36. #\"Comment\" #Comm #Comm\n") - -aFile.close() - -aListOfParameters = model.importParameters(Part_1_doc, nameFile) +aListOfParameters = model.importParameters(Part_1_doc, aDir) Box_1 = model.addBox(Part_1_doc, "Longueur", "Largeur", "Hauteur") assert(len(Box_1.feature().error()) == 0) assert(len(aListOfParameters) > 0) assert(len(aListOfParameters) == 5) +assert(model.checkPythonDump()) diff --git a/src/ParametersPlugin/Test/TestParameterCreationError.py b/src/ParametersPlugin/Test/TestParameterCreationError.py index 08b2746e3..45da43336 100644 --- a/src/ParametersPlugin/Test/TestParameterCreationError.py +++ b/src/ParametersPlugin/Test/TestParameterCreationError.py @@ -1,6 +1,21 @@ -### -### SHAPER component -### +# Copyright (C) 2014-2021 CEA/DEN, EDF R&D +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +# from salome.shaper import model diff --git a/src/ParametersPlugin/data/parameters.txt b/src/ParametersPlugin/data/parameters.txt new file mode 100644 index 000000000..f0538715f --- /dev/null +++ b/src/ParametersPlugin/data/parameters.txt @@ -0,0 +1,11 @@ +Longueur 36. # "Longueur de la pièce" +Largeur 24. # Largeur de la pièce +Hauteur Longueur*Largeur + +Largeur2 +Largeur3 #Comment +A12 5. * 5. +# Comment +Name#Comment + # Comm +Longueur2 36. #\"Comment\" #Comm #Comm \ No newline at end of file diff --git a/src/ParametersPlugin/doc/TUI_parameterFeature.rst b/src/ParametersPlugin/doc/TUI_parameterFeature.rst index ae2b0cb14..04480ce43 100644 --- a/src/ParametersPlugin/doc/TUI_parameterFeature.rst +++ b/src/ParametersPlugin/doc/TUI_parameterFeature.rst @@ -9,5 +9,5 @@ Create Parameter :language: python :download:`Download this script ` -:download:`Download file for script ` +:download:`Download parameters file sample ` diff --git a/src/ParametersPlugin/doc/examples/File.txt b/src/ParametersPlugin/doc/examples/File.txt index 5f5a7bc97..5ab7e7825 100644 --- a/src/ParametersPlugin/doc/examples/File.txt +++ b/src/ParametersPlugin/doc/examples/File.txt @@ -1,3 +1,3 @@ -ax 10.0 -bx 15.0 -cx ax+bx \ No newline at end of file +Width 10.0 #Width of square +Height 15.0 # Height of square +Area Width*Height # Area of square \ No newline at end of file diff --git a/src/PythonAPI/model/parameter/import_parameter.py b/src/PythonAPI/model/parameter/import_parameter.py index d844f7880..0c3bb31bd 100644 --- a/src/PythonAPI/model/parameter/import_parameter.py +++ b/src/PythonAPI/model/parameter/import_parameter.py @@ -20,6 +20,11 @@ from salome.shaper import model import codecs +def changeTab(theLine): + aResult = theLine.split("#")[0].replace("\t"," ") + aResult += theLine[len(aResult):] + return aResult + def importParameters(theDocument, theFileName): aResult = [] @@ -30,11 +35,12 @@ def importParameters(theDocument, theFileName): for aLine in aFile: aLine = aLine.rstrip("\n") + aLine = changeTab(aLine) + aName = "" aParameter = "" aComment = "" - isOK = True - isComment = False + aFirstText = aLine.split(" ")[0] aName = aFirstText.split("#")[0].strip() @@ -52,7 +58,7 @@ def importParameters(theDocument, theFileName): try: aResult.append(model.addParameter(theDocument, aName, aParameter.strip(), aComment.strip())) except SyntaxError as anError: - anError + print(anError) aFile.close() return aResult -- 2.39.2