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