Salome HOME
Meet the coding style (line length <= 100)
[modules/shaper.git] / src / ParametersAPI / ParametersAPI_Parameter.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2 // Name   : ParametersAPI_Parameter.cpp
3 // Purpose:
4 //
5 // History:
6 // 16/06/16 - Sergey POKHODENKO - Creation of the file
7
8 //--------------------------------------------------------------------------------------
9 #include "ParametersAPI_Parameter.h"
10 //--------------------------------------------------------------------------------------
11 #include <ModelHighAPI_Dumper.h>
12 #include <ModelHighAPI_Tools.h>
13 #include <ModelAPI_ResultParameter.h>
14 //--------------------------------------------------------------------------------------
15 ParametersAPI_Parameter::ParametersAPI_Parameter(
16     const std::shared_ptr<ModelAPI_Feature> & theFeature)
17 : ModelHighAPI_Interface(theFeature)
18 {
19   initialize();
20 }
21
22 ParametersAPI_Parameter::ParametersAPI_Parameter(
23     const std::shared_ptr<ModelAPI_Feature> & theFeature,
24     const std::string & theName,
25     const std::string & theExpression,
26     const std::string & theComment)
27 : ModelHighAPI_Interface(theFeature)
28 {
29   if (initialize()) {
30     fillAttribute(theName, name());
31     fillAttribute(theExpression, expression());
32     if (!theComment.empty())
33       fillAttribute(theComment, comment());
34
35     execute();
36   }
37 }
38
39 void ParametersAPI_Parameter::setValue(const double theValue)
40 {
41   // convert value to the expression string
42   std::ostringstream aValueStr;
43   aValueStr<<theValue;
44   fillAttribute(aValueStr.str(), expression());
45   execute();
46 }
47
48 double ParametersAPI_Parameter::value() {
49   ResultParameterPtr aRes =
50     std::dynamic_pointer_cast<ModelAPI_ResultParameter>(feature()->firstResult());
51   // it may raise an exception if result is invalid
52   return aRes->data()->real(ModelAPI_ResultParameter::VALUE())->value();
53 }
54
55 ParametersAPI_Parameter::~ParametersAPI_Parameter()
56 {
57 }
58
59 void ParametersAPI_Parameter::dump(ModelHighAPI_Dumper& theDumper) const
60 {
61   FeaturePtr aBase = feature();
62   const std::string& aDocName = theDumper.name(aBase->document());
63   const std::string& aParamName = theDumper.name(aBase, false, true);
64
65   AttributeStringPtr anExpr   = aBase->string(ParametersPlugin_Parameter::EXPRESSION_ID());
66   AttributeStringPtr aComment = aBase->string(ParametersPlugin_Parameter::COMMENT_ID());
67
68   theDumper << "model.addParameter(" << aDocName << ", \"" << aParamName << "\", " << anExpr;
69   if (aComment->isInitialized() && !aComment->value().empty())
70     theDumper << ", " << aComment;
71   theDumper << ")" << std::endl;
72 }
73
74 //--------------------------------------------------------------------------------------
75 ParameterPtr addParameter(const std::shared_ptr<ModelAPI_Document> & thePart,
76                           const std::string & theName,
77                           const std::string & theExpression,
78                           const std::string & theComment)
79 {
80   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ParametersAPI_Parameter::ID());
81   return ParameterPtr(new ParametersAPI_Parameter(aFeature, theName, theExpression, theComment));
82 }