From: Alexey Kondratyev Date: Mon, 11 Oct 2021 07:33:06 +0000 (+0300) Subject: [bos #26444] : [EDF] (2021) SHAPER: Improve treatment of parameters X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=acea558cbec7f31b1aaf146dbddad051a661071f;p=modules%2Fshaper.git [bos #26444] : [EDF] (2021) SHAPER: Improve treatment of parameters Add python function to import parameters from file. Add test for checking importing parameters. Add "Import file" button in parameter manager to import parameters. Update documentation. --- diff --git a/src/InitializationPlugin/InitializationPlugin_EvalListener.cpp b/src/InitializationPlugin/InitializationPlugin_EvalListener.cpp index 76a388ee5..57c216f17 100644 --- a/src/InitializationPlugin/InitializationPlugin_EvalListener.cpp +++ b/src/InitializationPlugin/InitializationPlugin_EvalListener.cpp @@ -78,6 +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); myInterp = std::shared_ptr(new InitializationPlugin_PyInterp()); myInterp->initialize(); @@ -179,6 +180,13 @@ void InitializationPlugin_EvalListener::processEvent( } aMsg->setResults(aParamsList, anError); } + else if (theMessage->eventID() == ModelAPI_PathEvalMessage::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 + "\")"); + } } //================================================================================================= diff --git a/src/ModelAPI/ModelAPI_Events.cpp b/src/ModelAPI/ModelAPI_Events.cpp index 6b0ea3ef5..4688732d8 100644 --- a/src/ModelAPI/ModelAPI_Events.cpp +++ b/src/ModelAPI/ModelAPI_Events.cpp @@ -188,6 +188,27 @@ const std::string& ModelAPI_ParameterEvalMessage::error() const return myError; } +/// Creates an empty message +ModelAPI_PathEvalMessage::ModelAPI_PathEvalMessage(const Events_ID theID, const void* theSender) + :Events_Message(theID, theSender) +{ + +} + +ModelAPI_PathEvalMessage::~ModelAPI_PathEvalMessage() +{ +} + +std::string ModelAPI_PathEvalMessage::parameter() const +{ + return myParam; +} + +void ModelAPI_PathEvalMessage::setParameter(std::string theParam) +{ + myParam = theParam; +} + ModelAPI_BuildEvalMessage::ModelAPI_BuildEvalMessage( const Events_ID theID, const void* theSender) : Events_Message(theID, theSender), myIsProcessed(false) diff --git a/src/ModelAPI/ModelAPI_Events.h b/src/ModelAPI/ModelAPI_Events.h index a5dc0dde9..3d04cae18 100644 --- a/src/ModelAPI/ModelAPI_Events.h +++ b/src/ModelAPI/ModelAPI_Events.h @@ -352,6 +352,44 @@ class ModelAPI_ParameterEvalMessage : public Events_Message MODELAPI_EXPORT const std::string& error() const; }; +class ModelAPI_PathEvalMessage : public Events_Message +{ + std::string myParam; ///< parameters that should be evaluated + 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 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 + send(std::string theParameter, const void* theSender) + { + std::shared_ptr aMessage = + std::shared_ptr( + new ModelAPI_PathEvalMessage(eventId(), theSender)); + aMessage->setParameter(theParameter); + Events_Loop::loop()->send(aMessage); + return aMessage; + } + + /// Creates an empty message + MODELAPI_EXPORT ModelAPI_PathEvalMessage(const Events_ID theID, const void* theSender = 0); + /// The virtual destructor + MODELAPI_EXPORT virtual ~ModelAPI_PathEvalMessage(); + + /// 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); +}; + class ModelAPI_BuildEvalMessage : public Events_Message { FeaturePtr myParam; ///< parameters that should be evaluated diff --git a/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.cpp b/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.cpp index 62a432a8b..b2ca35211 100644 --- a/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.cpp +++ b/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.cpp @@ -54,6 +54,7 @@ #include #include #include +#include enum ColumnType { Col_Name, @@ -253,6 +254,10 @@ ParametersPlugin_WidgetParamsMgr::ParametersPlugin_WidgetParamsMgr(QWidget* theP connect(myInsertBtn, SIGNAL(clicked(bool)), SLOT(onInsert())); aBtnLayout->addWidget(myInsertBtn); + myImportBtn = new QPushButton(translate("Import file"), this); + connect(myImportBtn, SIGNAL(clicked(bool)), SLOT(onImport())); + aBtnLayout->addWidget(myImportBtn); + myRemoveBtn = new QPushButton(translate("Remove"), this); connect(myRemoveBtn, SIGNAL(clicked(bool)), SLOT(onRemove())); aBtnLayout->addWidget(myRemoveBtn); @@ -691,6 +696,26 @@ 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()) + 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); + } + updateParametersFeatures(); + /*updateFeaturesPart();*/ + updateParametersPart(); +} + void ParametersPlugin_WidgetParamsMgr::onUp() { QTreeWidgetItem* aCurrentItem = selectedItem(); diff --git a/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.h b/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.h index 58f717e45..2a08f023e 100644 --- a/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.h +++ b/src/ParametersPlugin/ParametersPlugin_WidgetParamsMgr.h @@ -109,6 +109,9 @@ private slots: /// Slot for reaction on remove parameter void onRemove(); + /// Slot for reaction on import parameter + void onImport(); + /// Slot for reaction on shift up void onUp(); @@ -173,6 +176,7 @@ private: QPushButton* myAddBtn; QPushButton* myInsertBtn; QPushButton* myRemoveBtn; + QPushButton* myImportBtn; QToolButton* myUpBtn; QToolButton* myDownBtn; diff --git a/src/ParametersPlugin/doc/images/Manager.png b/src/ParametersPlugin/doc/images/Manager.png index a7e812d67..99d0c1e69 100644 Binary files a/src/ParametersPlugin/doc/images/Manager.png and b/src/ParametersPlugin/doc/images/Manager.png differ diff --git a/src/ParametersPlugin/doc/images/parameters.png b/src/ParametersPlugin/doc/images/parameters.png index 4f1e1f66c..fbf1d7b57 100755 Binary files a/src/ParametersPlugin/doc/images/parameters.png and b/src/ParametersPlugin/doc/images/parameters.png differ diff --git a/src/ParametersPlugin/doc/managerFeature.rst b/src/ParametersPlugin/doc/managerFeature.rst index 7575ee81e..d0704d97b 100644 --- a/src/ParametersPlugin/doc/managerFeature.rst +++ b/src/ParametersPlugin/doc/managerFeature.rst @@ -29,6 +29,11 @@ The following dialog box with parameter table appears: - **Add** button adds a new empty string in the end of table. Default **Name** is **, **Expression** is ** - **Insert** button adds a new empty string before the selected parameter; - **Delete** button removes the selected parameter from the table; +- **Import** button import parameters from .txt files. Parameters must be written in file in separate lines like example: **Name** **Expression** #**Comment** +Sample.txt: +''Longueur 36. # Longueur de la pièce'' +''Largeur 24 #Largeur de la pièce'' +''Hauteur Longueur * Largeur'' - **Modify parameter position** button moves the selected parameter | |param_up| one string higher in the table, diff --git a/src/PythonAPI/Test/TestInsertParameter.py b/src/PythonAPI/Test/TestInsertParameter.py new file mode 100644 index 000000000..246a07a0e --- /dev/null +++ b/src/PythonAPI/Test/TestInsertParameter.py @@ -0,0 +1,56 @@ +# Copyright (C) 2014-2020 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 +import os + +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" + +aDir = os.path.join(data_dir, nameFile) + +aFile = open(nameFile, 'w') + +aFile.write("Longueur 36. # \"Comment\"\n") +aFile.write("Largeur 24. # Comment\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) +Box_1 = model.addBox(Part_1_doc, "Longueur", "Largeur", "Hauteur") + +assert(len(Box_1.feature().error()) == 0) +assert(len(aListOfParameters) > 0) +assert(len(aListOfParameters) == 8) diff --git a/src/PythonAPI/model/parameter/__init__.py b/src/PythonAPI/model/parameter/__init__.py index f0c7b22e8..0f0f64a18 100644 --- a/src/PythonAPI/model/parameter/__init__.py +++ b/src/PythonAPI/model/parameter/__init__.py @@ -19,4 +19,6 @@ """Package for Parameter plugin for the Parametric Geometry API of the Modeler. """ -from ParametersAPI import addParameter, removeParameter \ No newline at end of file +from ParametersAPI import addParameter, removeParameter + +from .import_parameter import * diff --git a/src/PythonAPI/model/parameter/import_parameter.py b/src/PythonAPI/model/parameter/import_parameter.py new file mode 100644 index 000000000..d75275dfc --- /dev/null +++ b/src/PythonAPI/model/parameter/import_parameter.py @@ -0,0 +1,54 @@ +# 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 + +def importParameters(theDocument, theFileName): + + aResult = [] + try: + aFile = open(theFileName, 'r') + except IOError: + return aResult + + for aLine in aFile: + aLine = aLine.rstrip("\n") + aName = "" + aParameter = "" + aComment = "" + isOK = True + isComment = False + aFirstText = aLine.split(" ")[0] + + aName = aFirstText.split("#")[0] + + aLine = aLine.lstrip(aName) + + aParameter = aLine.split("#")[0] + + aLine = aLine.lstrip(aParameter) + aLine = aLine.lstrip("#") + + aComment = aLine + + if(len(aName) > 0): + aResult.append(model.addParameter(theDocument, aName.strip(), aParameter.strip(), aComment.strip())) + + aFile.close() + return aResult