1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
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)
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)
41 fillAttribute(theName, name());
42 fillAttribute(theExpression, expression());
43 if (!theComment.empty())
44 fillAttribute(theComment, comment());
50 void ParametersAPI_Parameter::setValue(const double theValue)
52 // convert value to the expression string
53 std::ostringstream aValueStr;
55 fillAttribute(aValueStr.str(), expression());
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();
66 ParametersAPI_Parameter::~ParametersAPI_Parameter()
70 void ParametersAPI_Parameter::dump(ModelHighAPI_Dumper& theDumper) const
72 FeaturePtr aBase = feature();
73 const std::string& aDocName = theDumper.name(aBase->document());
74 const std::string& aParamName = theDumper.name(aBase, false, true);
76 AttributeStringPtr anExpr = aBase->string(ParametersPlugin_Parameter::EXPRESSION_ID());
77 AttributeStringPtr aComment = aBase->string(ParametersPlugin_Parameter::COMMENT_ID());
79 theDumper << "model.addParameter(" << aDocName << ", \"" << aParamName << "\", " << anExpr;
80 if (aComment->isInitialized() && !aComment->value().empty())
81 theDumper << ", " << aComment;
82 theDumper << ")" << std::endl;
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)
91 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ParametersAPI_Parameter::ID());
92 return ParameterPtr(new ParametersAPI_Parameter(aFeature, theName, theExpression, theComment));