1 // Copyright (C) 2014-2019 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
20 #include "ParametersAPI_Parameter.h"
21 //--------------------------------------------------------------------------------------
22 #include <ModelHighAPI_Dumper.h>
23 #include <ModelHighAPI_Tools.h>
24 #include <ModelAPI_ResultParameter.h>
25 #include <ModelAPI_Events.h>
26 //--------------------------------------------------------------------------------------
27 ParametersAPI_Parameter::ParametersAPI_Parameter(
28 const std::shared_ptr<ModelAPI_Feature> & theFeature)
29 : ModelHighAPI_Interface(theFeature)
34 ParametersAPI_Parameter::ParametersAPI_Parameter(
35 const std::shared_ptr<ModelAPI_Feature> & theFeature,
36 const std::string & theName,
37 const std::string & theExpression,
38 const std::string & theComment)
39 : ModelHighAPI_Interface(theFeature)
42 fillAttribute(theName, name());
43 fillAttribute(theExpression, expression());
44 if (!theComment.empty())
45 fillAttribute(theComment, comment());
51 void ParametersAPI_Parameter::setValue(const double theValue)
53 // convert value to the expression string
54 std::ostringstream aValueStr;
56 fillAttribute(aValueStr.str(), expression());
60 double ParametersAPI_Parameter::value() {
61 ResultParameterPtr aRes =
62 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(feature()->firstResult());
63 // it may raise an exception if result is invalid
64 return aRes->data()->real(ModelAPI_ResultParameter::VALUE())->value();
67 ParametersAPI_Parameter::~ParametersAPI_Parameter()
71 void ParametersAPI_Parameter::dump(ModelHighAPI_Dumper& theDumper) const
73 FeaturePtr aBase = feature();
74 const std::string& aDocName = theDumper.name(aBase->document());
75 const std::string& aParamName = theDumper.name(aBase, false, true);
77 AttributeStringPtr anExpr = aBase->string(ParametersPlugin_Parameter::EXPRESSION_ID());
78 AttributeStringPtr aComment = aBase->string(ParametersPlugin_Parameter::COMMENT_ID());
80 theDumper << "model.addParameter(" << aDocName << ", \"" << aParamName << "\", " << anExpr;
81 if (aComment->isInitialized() && !aComment->value().empty())
82 theDumper << ", " << aComment;
83 theDumper << ")" << std::endl;
86 //--------------------------------------------------------------------------------------
87 ParameterPtr addParameter(const std::shared_ptr<ModelAPI_Document> & thePart,
88 const std::string & theName,
89 const std::string & theExpression,
90 const std::string & theComment)
92 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ParametersAPI_Parameter::ID());
93 return ParameterPtr(new ParametersAPI_Parameter(aFeature, theName, theExpression, theComment));
96 //--------------------------------------------------------------------------------------
97 void removeParameter(const std::shared_ptr<ModelAPI_Document> & thePart,
98 const ParameterPtr & theParameter)
100 FeaturePtr aParam = theParameter->feature();
102 ModelAPI_ReplaceParameterMessage::send(aParam, 0);
103 thePart->removeFeature(aParam);