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.
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<InitializationPlugin_PyInterp>(new InitializationPlugin_PyInterp());
myInterp->initialize();
}
aMsg->setResults(aParamsList, anError);
}
+ else if (theMessage->eventID() == ModelAPI_PathEvalMessage::eventId())
+ {
+ std::shared_ptr<ModelAPI_PathEvalMessage> aMsg =
+ std::dynamic_pointer_cast<ModelAPI_PathEvalMessage>(theMessage);
+ std::string aPath = aMsg->parameter();
+ myInterp->runString("from salome.shaper import model; doc = model.activeDocument(); model.importParameters(doc, \"" + aPath + "\")");
+ }
}
//=================================================================================================
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)
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<ModelAPI_PathEvalMessage>
+ send(std::string theParameter, const void* theSender)
+ {
+ std::shared_ptr<ModelAPI_PathEvalMessage> aMessage =
+ std::shared_ptr<ModelAPI_PathEvalMessage>(
+ 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
#include <QKeyEvent>
#include <QDialogButtonBox>
#include <QShortcut>
+#include <QFileDialog>
enum ColumnType {
Col_Name,
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);
}
}
+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<ModelAPI_PathEvalMessage> aMessage =
+ std::shared_ptr<ModelAPI_PathEvalMessage>(
+ new ModelAPI_PathEvalMessage(ModelAPI_PathEvalMessage::eventId()));
+ aMessage->setParameter(aPath);
+ Events_Loop::loop()->send(aMessage);
+ }
+ updateParametersFeatures();
+ /*updateFeaturesPart();*/
+ updateParametersPart();
+}
+
void ParametersPlugin_WidgetParamsMgr::onUp()
{
QTreeWidgetItem* aCurrentItem = selectedItem();
/// Slot for reaction on remove parameter
void onRemove();
+ /// Slot for reaction on import parameter
+ void onImport();
+
/// Slot for reaction on shift up
void onUp();
QPushButton* myAddBtn;
QPushButton* myInsertBtn;
QPushButton* myRemoveBtn;
+ QPushButton* myImportBtn;
QToolButton* myUpBtn;
QToolButton* myDownBtn;
- **Add** button adds a new empty string in the end of table. Default **Name** is *<NoName>*, **Expression** is *<NoValue>*
- **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,
--- /dev/null
+# 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)
"""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 *
--- /dev/null
+# 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