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 "Model_Expression.h"
22 #include <TCollection_AsciiString.hxx>
23 #include <TCollection_ExtendedString.hxx>
24 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
25 #include <TDataStd_UAttribute.hxx>
27 #include <TDataStd_RealArray.hxx>
28 #include <TDataStd_ExtStringArray.hxx>
32 static Standard_GUID kInvalidGUID("caee5ce4-34b1-4b29-abcb-685287d18096");
35 Model_Expression::Model_Expression(TDF_Label& theLabel)
37 if (!theLabel.FindAttribute(TDataStd_Name::GetID(), myText)) {
38 myText = TDataStd_Name::Set(theLabel, TCollection_ExtendedString());
40 if (!theLabel.FindAttribute(TDataStd_Comment::GetID(), myError)) {
41 myError = TDataStd_Comment::Set(theLabel, TCollection_ExtendedString());
43 if (!theLabel.FindAttribute(TDataStd_ExtStringList::GetID(), myUsedParameters)) {
44 myUsedParameters = TDataStd_ExtStringList::Set(theLabel);
48 void Model_Expression::setText(const std::string& theValue)
50 if (text() != theValue) {
51 myText->Set(TCollection_ExtendedString(theValue.c_str()));
52 myIsInitialized = true; // the value will be set very soon
55 setError(text().empty() ? "" : "Not a double value.");
58 std::string Model_Expression::text() const
60 return TCollection_AsciiString(myText->Get()).ToCString();
63 void Model_Expression::setError(const std::string& theError)
65 if (error() != theError)
66 myError->Set(TCollection_ExtendedString(theError.c_str()));
69 std::string Model_Expression::error()
71 return TCollection_AsciiString(myError->Get()).ToCString();
74 void Model_Expression::setUsedParameters(const std::set<std::string>& theUsedParameters)
76 myUsedParameters->Clear();
77 std::set<std::string>::const_iterator anIt = theUsedParameters.begin();
78 for (; anIt != theUsedParameters.end(); ++anIt)
79 myUsedParameters->Append(TCollection_ExtendedString(anIt->c_str()));
82 std::set<std::string> Model_Expression::usedParameters() const
84 std::set<std::string> aResult;
85 TDataStd_ListIteratorOfListOfExtendedString aIt;
86 for (aIt.Initialize(myUsedParameters->List()); aIt.More(); aIt.Next())
87 aResult.insert(TCollection_AsciiString(aIt.Value()).ToCString());
91 ///////////////// Model_ExpressionDouble /////////////
92 Model_ExpressionDouble::Model_ExpressionDouble(TDF_Label& theLabel)
93 : Model_Expression(theLabel)
99 void Model_ExpressionDouble::reinit()
101 if (!myLab.FindAttribute(TDataStd_Real::GetID(), myReal)) {
102 myIsInitialized = false;
104 myIsInitialized = true;
108 void Model_ExpressionDouble::setValue(const double theValue)
110 if (!myIsInitialized || myReal.IsNull()) {
111 myReal = TDataStd_Real::Set(myText->Label(), theValue);
112 myIsInitialized = true;
113 } else if (value() != theValue) {
114 myReal->Set(theValue);
118 double Model_ExpressionDouble::value()
120 if (myIsInitialized && !myReal.IsNull())
121 return myReal->Get();
122 return std::numeric_limits<double>::max(); // error
125 void Model_ExpressionDouble::setInvalid(const bool theFlag)
128 TDataStd_UAttribute::Set(myText->Label(), kInvalidGUID);
130 myText->Label().ForgetAttribute(kInvalidGUID);
134 bool Model_ExpressionDouble::isInvalid()
136 return myText->Label().IsAttribute(kInvalidGUID) == Standard_True;
139 ///////////////// Model_ExpressionInteger /////////////
140 Model_ExpressionInteger::Model_ExpressionInteger(TDF_Label& theLabel)
141 : Model_Expression(theLabel)
147 void Model_ExpressionInteger::reinit()
149 if (!myLab.FindAttribute(TDataStd_Integer::GetID(), myInteger)) {
150 myIsInitialized = false;
152 myIsInitialized = true;
156 void Model_ExpressionInteger::setValue(const int theValue)
158 if (!myIsInitialized || myInteger.IsNull()) {
159 myInteger = TDataStd_Integer::Set(myText->Label(), theValue);
160 myIsInitialized = true;
161 } else if (value() != theValue) {
162 myInteger->Set(theValue);
166 int Model_ExpressionInteger::value()
168 if (myIsInitialized && !myInteger.IsNull())
169 return myInteger->Get();
170 return std::numeric_limits<int>::max(); // error
173 void Model_ExpressionInteger::setInvalid(const bool theFlag)
176 TDataStd_UAttribute::Set(myText->Label(), kInvalidGUID);
178 myText->Label().ForgetAttribute(kInvalidGUID);
182 bool Model_ExpressionInteger::isInvalid()
184 return myText->Label().IsAttribute(kInvalidGUID) == Standard_True;