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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
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)
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)
43 fillAttribute(theName, name());
44 fillAttribute(theExpression, expression());
45 if (!theComment.empty())
46 fillAttribute(theComment, comment());
52 void ParametersAPI_Parameter::setValue(const double theValue)
54 // convert value to the expression string
55 std::ostringstream aValueStr;
57 fillAttribute(aValueStr.str(), expression());
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();
68 ParametersAPI_Parameter::~ParametersAPI_Parameter()
72 void ParametersAPI_Parameter::dump(ModelHighAPI_Dumper& theDumper) const
74 FeaturePtr aBase = feature();
75 const std::string& aDocName = theDumper.name(aBase->document());
76 const std::string& aParamName = theDumper.name(aBase, false, true);
78 AttributeStringPtr anExpr = aBase->string(ParametersPlugin_Parameter::EXPRESSION_ID());
79 AttributeStringPtr aComment = aBase->string(ParametersPlugin_Parameter::COMMENT_ID());
81 theDumper << "model.addParameter(" << aDocName << ", \"" << aParamName << "\", " << anExpr;
82 if (aComment->isInitialized() && !aComment->value().empty())
83 theDumper << ", " << aComment;
84 theDumper << ")" << std::endl;
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)
93 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ParametersAPI_Parameter::ID());
94 return ParameterPtr(new ParametersAPI_Parameter(aFeature, theName, theExpression, theComment));
97 //--------------------------------------------------------------------------------------
98 void removeParameter(const std::shared_ptr<ModelAPI_Document> & thePart,
99 const ParameterPtr & theParameter)
101 FeaturePtr aParam = theParameter->feature();
103 ModelAPI_ReplaceParameterMessage::send(aParam, 0);
104 thePart->removeFeature(aParam);