Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[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 email : webmaster.salome@opencascade.com<mailto: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 //--------------------------------------------------------------------------------------
26 ParametersAPI_Parameter::ParametersAPI_Parameter(
27     const std::shared_ptr<ModelAPI_Feature> & theFeature)
28 : ModelHighAPI_Interface(theFeature)
29 {
30   initialize();
31 }
32
33 ParametersAPI_Parameter::ParametersAPI_Parameter(
34     const std::shared_ptr<ModelAPI_Feature> & theFeature,
35     const std::string & theName,
36     const std::string & theExpression,
37     const std::string & theComment)
38 : ModelHighAPI_Interface(theFeature)
39 {
40   if (initialize()) {
41     fillAttribute(theName, name());
42     fillAttribute(theExpression, expression());
43     if (!theComment.empty())
44       fillAttribute(theComment, comment());
45
46     execute();
47   }
48 }
49
50 void ParametersAPI_Parameter::setValue(const double theValue)
51 {
52   // convert value to the expression string
53   std::ostringstream aValueStr;
54   aValueStr<<theValue;
55   fillAttribute(aValueStr.str(), expression());
56   execute();
57 }
58
59 double ParametersAPI_Parameter::value() {
60   ResultParameterPtr aRes =
61     std::dynamic_pointer_cast<ModelAPI_ResultParameter>(feature()->firstResult());
62   // it may raise an exception if result is invalid
63   return aRes->data()->real(ModelAPI_ResultParameter::VALUE())->value();
64 }
65
66 ParametersAPI_Parameter::~ParametersAPI_Parameter()
67 {
68 }
69
70 void ParametersAPI_Parameter::dump(ModelHighAPI_Dumper& theDumper) const
71 {
72   FeaturePtr aBase = feature();
73   const std::string& aDocName = theDumper.name(aBase->document());
74   const std::string& aParamName = theDumper.name(aBase, false, true);
75
76   AttributeStringPtr anExpr   = aBase->string(ParametersPlugin_Parameter::EXPRESSION_ID());
77   AttributeStringPtr aComment = aBase->string(ParametersPlugin_Parameter::COMMENT_ID());
78
79   theDumper << "model.addParameter(" << aDocName << ", \"" << aParamName << "\", " << anExpr;
80   if (aComment->isInitialized() && !aComment->value().empty())
81     theDumper << ", " << aComment;
82   theDumper << ")" << std::endl;
83 }
84
85 //--------------------------------------------------------------------------------------
86 ParameterPtr addParameter(const std::shared_ptr<ModelAPI_Document> & thePart,
87                           const std::string & theName,
88                           const std::string & theExpression,
89                           const std::string & theComment)
90 {
91   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ParametersAPI_Parameter::ID());
92   return ParameterPtr(new ParametersAPI_Parameter(aFeature, theName, theExpression, theComment));
93 }