Salome HOME
Copyright update 2020
[modules/shaper.git] / src / ParametersPlugin / ParametersPlugin_Parameter.cpp
1 // Copyright (C) 2014-2020  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <pyconfig.h>
21
22 #include "ParametersPlugin_Parameter.h"
23
24 #include <ModelAPI_AttributeString.h>
25 #include <ModelAPI_ResultParameter.h>
26 #include <ModelAPI_AttributeDouble.h>
27 #include <ModelAPI_AttributeRefList.h>
28 #include <ModelAPI_Tools.h>
29 #include <ModelAPI_Session.h>
30 #include <ModelAPI_Validator.h>
31 #include <ModelAPI_Events.h>
32
33 #include <string>
34 #include <sstream>
35
36 ParametersPlugin_Parameter::ParametersPlugin_Parameter()
37 {
38 }
39
40 ParametersPlugin_Parameter::~ParametersPlugin_Parameter()
41 {
42 }
43
44 void ParametersPlugin_Parameter::initAttributes()
45 {
46   data()->addAttribute(VARIABLE_ID(), ModelAPI_AttributeString::typeId());
47   data()->addAttribute(EXPRESSION_ID(), ModelAPI_AttributeString::typeId());
48
49   data()->addAttribute(COMMENT_ID(), ModelAPI_AttributeString::typeId());
50   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), COMMENT_ID());
51
52   data()->addAttribute(EXPRESSION_ERROR_ID(), ModelAPI_AttributeString::typeId());
53   data()->string(EXPRESSION_ERROR_ID())->setIsArgument(false);
54   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXPRESSION_ERROR_ID());
55
56   data()->addAttribute(ARGUMENTS_ID(), ModelAPI_AttributeRefList::typeId());
57   data()->reflist(ARGUMENTS_ID())->setIsArgument(false);
58   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ARGUMENTS_ID());
59 }
60
61 bool ParametersPlugin_Parameter::isInHistory()
62 {
63   return false;
64 }
65
66 void ParametersPlugin_Parameter::attributeChanged(const std::string& theID)
67 {
68   if (theID == EXPRESSION_ID())
69     updateExpression();
70
71   data()->execState(ModelAPI_StateMustBeUpdated);
72 }
73
74 void ParametersPlugin_Parameter::updateName()
75 {
76   std::string aName = string(VARIABLE_ID())->value();
77   data()->setName(aName);
78
79   ResultParameterPtr aParam = document()->createParameter(data());
80   std::string anOldName = aParam->data()->name();
81   aParam->data()->setName(aName);
82   setResult(aParam);
83
84
85   // #2474 : if parameter name now hides/shows the higher level parameter name,
86   // update the depended expressions
87   DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
88   if (aParam->document() != aRootDoc) {
89     std::list<std::string> aNames; // collect names in the root document that must be checked
90     aNames.push_back(aName);
91     if (anOldName != aName) {
92       aNames.push_back(anOldName);
93     }
94     std::list<std::string>::iterator aNIter = aNames.begin();
95     for (; aNIter != aNames.end(); aNIter++) {
96       double aValue;
97       ResultParameterPtr aRootParam;
98       FeaturePtr aThis =
99         std::dynamic_pointer_cast<ModelAPI_Feature>(string(VARIABLE_ID())->owner());
100       if (ModelAPI_Tools::findVariable(aThis, *aNIter, aValue, aRootParam, aRootDoc)) {
101         std::set<std::shared_ptr<ModelAPI_Attribute> > anAttributes =
102           aRootParam->data()->refsToMe();
103         std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anAttributeIt =
104           anAttributes.cbegin();
105         for (; anAttributeIt != anAttributes.cend(); ++anAttributeIt) {
106           const AttributePtr& anAttribute = *anAttributeIt;
107           ModelAPI_AttributeEvalMessage::send(anAttribute, NULL);
108         }
109       }
110     }
111   }
112 }
113
114 bool ParametersPlugin_Parameter::updateExpression()
115 {
116   std::string anExpression = string(EXPRESSION_ID())->value();
117
118   std::string outErrorMessage;
119   double aValue = evaluate(anExpression, outErrorMessage);
120
121   data()->string(EXPRESSION_ERROR_ID())->setValue(outErrorMessage);
122   if (!outErrorMessage.empty())
123     return false;
124
125   ResultParameterPtr aParam = document()->createParameter(data());
126   AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
127   aValueAttribute->setValue(aValue);
128   setResult(aParam);
129
130   return true;
131 }
132
133 void ParametersPlugin_Parameter::execute()
134 {
135   updateName();
136   if (!updateExpression())
137     setError("Expression error.", false);
138 }
139
140 double ParametersPlugin_Parameter::evaluate(const std::string& theExpression, std::string& theError)
141 {
142   FeaturePtr aMyPtr = std::dynamic_pointer_cast<ModelAPI_Feature>(data()->owner());
143   std::shared_ptr<ModelAPI_ParameterEvalMessage> aProcessMessage =
144     ModelAPI_ParameterEvalMessage::send(aMyPtr, this);
145
146   double aResult = 0;
147   if (aProcessMessage->isProcessed()) {
148     const std::list<ResultParameterPtr>& aParamsList = aProcessMessage->params();
149     aResult = aProcessMessage->result();
150     theError = aProcessMessage->error();
151     // compare the list of parameters to store if changed
152     AttributeRefListPtr aParams = reflist(ARGUMENTS_ID());
153     bool aDifferent = aParams->size() != aParamsList.size();
154     if (!aDifferent) {
155       std::list<ResultParameterPtr>::const_iterator aNewIter = aParamsList.begin();
156       std::list<ObjectPtr> anOldList = aParams->list();
157       std::list<ObjectPtr>::const_iterator anOldIter = anOldList.begin();
158       for(; !aDifferent && aNewIter != aParamsList.end(); aNewIter++, anOldIter++) {
159         if (*aNewIter != *anOldIter)
160           aDifferent = true;
161       }
162     }
163     if (aDifferent) {
164       aParams->clear();
165       std::list<ResultParameterPtr>::const_iterator aNewIter = aParamsList.begin();
166       for(; aNewIter != aParamsList.end(); aNewIter++) {
167         aParams->append(*aNewIter);
168       }
169     }
170   } else { // error: python interpreter is not active
171     theError = "Python interpreter is not available";
172   }
173   return aResult;
174 }
175
176 bool ParametersPlugin_Parameter::isPreviewNeeded() const
177 {
178   return true;
179 }