Salome HOME
5c2c7130c6c830df98a6103e206f2c11f2a1b5ab
[modules/shaper.git] / src / ParametersAPI / ParametersAPI_Parameter.cpp
1 // Copyright (C) 2014-2021  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 "ParametersAPI_Parameter.h"
21 //--------------------------------------------------------------------------------------
22 #include <ModelHighAPI_Dumper.h>
23 #include <ModelHighAPI_Tools.h>
24 #include <ModelAPI_ResultParameter.h>
25 #include <ModelAPI_Events.h>
26 //--------------------------------------------------------------------------------------
27 ParametersAPI_Parameter::ParametersAPI_Parameter(
28     const std::shared_ptr<ModelAPI_Feature> & theFeature)
29 : ModelHighAPI_Interface(theFeature)
30 {
31   initialize();
32 }
33
34 ParametersAPI_Parameter::ParametersAPI_Parameter(
35     const std::shared_ptr<ModelAPI_Feature> & theFeature,
36     const std::string & theName,
37     const std::string & theExpression,
38     const std::wstring & theComment)
39 : ModelHighAPI_Interface(theFeature)
40 {
41   if (initialize()) {
42     fillAttribute(theName, name());
43     fillAttribute(theExpression, expression());
44     if (!theComment.empty())
45       fillAttribute(theComment, comment());
46
47     execute();
48   }
49 }
50
51 void ParametersAPI_Parameter::setValue(const double theValue)
52 {
53   // convert value to the expression string
54   std::ostringstream aValueStr;
55   aValueStr<<theValue;
56   fillAttribute(aValueStr.str(), expression());
57   execute();
58 }
59
60 double ParametersAPI_Parameter::value() {
61   ResultParameterPtr aRes =
62     std::dynamic_pointer_cast<ModelAPI_ResultParameter>(feature()->firstResult());
63   // it may raise an exception if result is invalid
64   return aRes->data()->real(ModelAPI_ResultParameter::VALUE())->value();
65 }
66
67 ParametersAPI_Parameter::~ParametersAPI_Parameter()
68 {
69 }
70
71 void ParametersAPI_Parameter::dump(ModelHighAPI_Dumper& theDumper) const
72 {
73   FeaturePtr aBase = feature();
74   const std::string& aDocName = theDumper.name(aBase->document());
75   const std::string& aParamName = theDumper.name(aBase, false, true);
76
77   AttributeStringPtr anExpr   = aBase->string(ParametersPlugin_Parameter::EXPRESSION_ID());
78   AttributeStringPtr aComment = aBase->string(ParametersPlugin_Parameter::COMMENT_ID());
79
80   theDumper << "model.addParameter(" << aDocName << ", \"" << aParamName << "\", " << anExpr;
81   if (aComment->isInitialized() && !aComment->value().empty())
82     theDumper << ", " << aComment;
83   theDumper << ")" << std::endl;
84 }
85
86 //--------------------------------------------------------------------------------------
87 ParameterPtr addParameter(const std::shared_ptr<ModelAPI_Document> & thePart,
88                           const std::string & theName,
89                           const std::string & theExpression,
90                           const std::wstring & theComment)
91 {
92   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ParametersAPI_Parameter::ID());
93   ParameterPtr aParam(new ParametersAPI_Parameter(aFeature, theName, theExpression, theComment));
94
95   if (!aParam->feature()->error().empty())
96   {
97     std::string anError("Error with parameter \"");
98     anError += theName + "\": " + aParam->feature()->error();
99     throw anError;
100   }
101   return aParam;
102 }
103
104 //--------------------------------------------------------------------------------------
105 void removeParameter(const std::shared_ptr<ModelAPI_Document> & thePart,
106                      const ParameterPtr & theParameter)
107 {
108   FeaturePtr aParam = theParameter->feature();
109   if (aParam) {
110     ModelAPI_ReplaceParameterMessage::send(aParam, 0);
111     thePart->removeFeature(aParam);
112   }
113 }