Salome HOME
Copyright update 2020
[modules/shaper.git] / src / Model / Model_Expression.cpp
1 // Copyright (C) 2014-2020  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "Model_Expression.h"
21
22 #include <TCollection_AsciiString.hxx>
23 #include <TCollection_ExtendedString.hxx>
24 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
25 #include <TDataStd_UAttribute.hxx>
26
27 #include <TDataStd_RealArray.hxx>
28 #include <TDataStd_ExtStringArray.hxx>
29
30 #include <limits>
31
32 static Standard_GUID kInvalidGUID("caee5ce4-34b1-4b29-abcb-685287d18096");
33
34
35 Model_Expression::Model_Expression(TDF_Label& theLabel)
36 {
37   if (!theLabel.FindAttribute(TDataStd_Name::GetID(), myText)) {
38     myText = TDataStd_Name::Set(theLabel, TCollection_ExtendedString());
39   }
40   if (!theLabel.FindAttribute(TDataStd_Comment::GetID(), myError)) {
41     myError = TDataStd_Comment::Set(theLabel, TCollection_ExtendedString());
42   }
43   if (!theLabel.FindAttribute(TDataStd_ExtStringList::GetID(), myUsedParameters)) {
44     myUsedParameters = TDataStd_ExtStringList::Set(theLabel);
45   }
46 }
47
48 void Model_Expression::setText(const std::string& theValue)
49 {
50   if (text() != theValue) {
51     myText->Set(TCollection_ExtendedString(theValue.c_str()));
52     myIsInitialized = true; // the value will be set very soon
53   }
54
55   setError(text().empty() ? "" : "Not a double value.");
56 }
57
58 std::string Model_Expression::text() const
59 {
60   return TCollection_AsciiString(myText->Get()).ToCString();
61 }
62
63 void Model_Expression::setError(const std::string& theError)
64 {
65   if (error() != theError)
66     myError->Set(TCollection_ExtendedString(theError.c_str()));
67 }
68
69 std::string Model_Expression::error()
70 {
71   return TCollection_AsciiString(myError->Get()).ToCString();
72 }
73
74 void Model_Expression::setUsedParameters(const std::set<std::string>& theUsedParameters)
75 {
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()));
80 }
81
82 std::set<std::string> Model_Expression::usedParameters() const
83 {
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());
88   return aResult;
89 }
90
91 ///////////////// Model_ExpressionDouble /////////////
92 Model_ExpressionDouble::Model_ExpressionDouble(TDF_Label& theLabel)
93     : Model_Expression(theLabel)
94 {
95   myLab = theLabel;
96   reinit();
97 }
98
99 void Model_ExpressionDouble::reinit()
100 {
101   if (!myLab.FindAttribute(TDataStd_Real::GetID(), myReal)) {
102     myIsInitialized = false;
103   } else {
104     myIsInitialized = true;
105   }
106 }
107
108 void Model_ExpressionDouble::setValue(const double theValue)
109 {
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);
115   }
116 }
117
118 double Model_ExpressionDouble::value()
119 {
120   if (myIsInitialized && !myReal.IsNull())
121     return myReal->Get();
122   return std::numeric_limits<double>::max(); // error
123 }
124
125 void Model_ExpressionDouble::setInvalid(const bool theFlag)
126 {
127   if (theFlag) {
128     TDataStd_UAttribute::Set(myText->Label(), kInvalidGUID);
129   } else {
130     myText->Label().ForgetAttribute(kInvalidGUID);
131   }
132 }
133
134 bool Model_ExpressionDouble::isInvalid()
135 {
136   return myText->Label().IsAttribute(kInvalidGUID) == Standard_True;
137 }
138
139 ///////////////// Model_ExpressionInteger /////////////
140 Model_ExpressionInteger::Model_ExpressionInteger(TDF_Label& theLabel)
141     : Model_Expression(theLabel)
142 {
143   myLab = theLabel;
144   reinit();
145 }
146
147 void Model_ExpressionInteger::reinit()
148 {
149   if (!myLab.FindAttribute(TDataStd_Integer::GetID(), myInteger)) {
150     myIsInitialized = false;
151   } else {
152     myIsInitialized = true;
153   }
154 }
155
156 void Model_ExpressionInteger::setValue(const int theValue)
157 {
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);
163   }
164 }
165
166 int Model_ExpressionInteger::value()
167 {
168   if (myIsInitialized && !myInteger.IsNull())
169     return myInteger->Get();
170   return std::numeric_limits<int>::max(); // error
171 }
172
173 void Model_ExpressionInteger::setInvalid(const bool theFlag)
174 {
175   if (theFlag) {
176     TDataStd_UAttribute::Set(myText->Label(), kInvalidGUID);
177   } else {
178     myText->Label().ForgetAttribute(kInvalidGUID);
179   }
180 }
181
182 bool Model_ExpressionInteger::isInvalid()
183 {
184   return myText->Label().IsAttribute(kInvalidGUID) == Standard_True;
185 }