Salome HOME
updated copyright message
[modules/shaper.git] / src / Model / Model_Expression.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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 <Locale_Convert.h>
23
24 #include <TCollection_AsciiString.hxx>
25 #include <TCollection_ExtendedString.hxx>
26 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
27 #include <TDataStd_UAttribute.hxx>
28
29 #include <TDataStd_RealArray.hxx>
30 #include <TDataStd_ExtStringArray.hxx>
31
32 #include <limits>
33
34 static Standard_GUID kInvalidGUID("caee5ce4-34b1-4b29-abcb-685287d18096");
35
36
37 Model_Expression::Model_Expression(TDF_Label& theLabel)
38 {
39   if (!theLabel.FindAttribute(TDataStd_Name::GetID(), myText)) {
40     myText = TDataStd_Name::Set(theLabel, TCollection_ExtendedString());
41   }
42   if (!theLabel.FindAttribute(TDataStd_Comment::GetID(), myError)) {
43     myError = TDataStd_Comment::Set(theLabel, TCollection_ExtendedString());
44   }
45   if (!theLabel.FindAttribute(TDataStd_ExtStringList::GetID(), myUsedParameters)) {
46     myUsedParameters = TDataStd_ExtStringList::Set(theLabel);
47   }
48 }
49
50 void Model_Expression::setText(const std::wstring& theValue)
51 {
52   if (text() != theValue) {
53     myText->Set(TCollection_ExtendedString(theValue.c_str()));
54     myIsInitialized = true; // the value will be set very soon
55   }
56
57   setError(text().empty() ? "" : "Not a double value.");
58 }
59
60 std::wstring Model_Expression::text() const
61 {
62   return Locale::Convert::toWString(myText->Get().ToExtString());
63 }
64
65 void Model_Expression::setError(const std::string& theError)
66 {
67   if (error() != theError)
68     myError->Set(TCollection_ExtendedString(theError.c_str()));
69 }
70
71 std::string Model_Expression::error()
72 {
73   return TCollection_AsciiString(myError->Get()).ToCString();
74 }
75
76 void Model_Expression::setUsedParameters(const std::set<std::wstring>& theUsedParameters)
77 {
78   myUsedParameters->Clear();
79   std::set<std::wstring>::const_iterator anIt = theUsedParameters.begin();
80   for (; anIt != theUsedParameters.end(); ++anIt)
81     myUsedParameters->Append(TCollection_ExtendedString(anIt->c_str()));
82 }
83
84 std::set<std::wstring> Model_Expression::usedParameters() const
85 {
86   std::set<std::wstring> aResult;
87   TDataStd_ListIteratorOfListOfExtendedString aIt;
88   for (aIt.Initialize(myUsedParameters->List()); aIt.More(); aIt.Next())
89     aResult.insert(Locale::Convert::toWString(aIt.Value().ToExtString()));
90   return aResult;
91 }
92
93 ///////////////// Model_ExpressionDouble /////////////
94 Model_ExpressionDouble::Model_ExpressionDouble(TDF_Label& theLabel)
95     : Model_Expression(theLabel)
96 {
97   myLab = theLabel;
98   reinit();
99 }
100
101 void Model_ExpressionDouble::reinit()
102 {
103   if (!myLab.FindAttribute(TDataStd_Real::GetID(), myReal)) {
104     myIsInitialized = false;
105   } else {
106     myIsInitialized = true;
107   }
108 }
109
110 void Model_ExpressionDouble::setValue(const double theValue)
111 {
112   if (!myIsInitialized || myReal.IsNull()) {
113     myReal = TDataStd_Real::Set(myText->Label(), theValue);
114     myIsInitialized = true;
115   } else if (value() != theValue) {
116     myReal->Set(theValue);
117   }
118 }
119
120 double Model_ExpressionDouble::value()
121 {
122   if (myIsInitialized && !myReal.IsNull())
123     return myReal->Get();
124   return std::numeric_limits<double>::max(); // error
125 }
126
127 void Model_ExpressionDouble::setInvalid(const bool theFlag)
128 {
129   if (theFlag) {
130     TDataStd_UAttribute::Set(myText->Label(), kInvalidGUID);
131   } else {
132     myText->Label().ForgetAttribute(kInvalidGUID);
133   }
134 }
135
136 bool Model_ExpressionDouble::isInvalid()
137 {
138   return myText->Label().IsAttribute(kInvalidGUID) == Standard_True;
139 }
140
141 ///////////////// Model_ExpressionInteger /////////////
142 Model_ExpressionInteger::Model_ExpressionInteger(TDF_Label& theLabel)
143     : Model_Expression(theLabel)
144 {
145   myLab = theLabel;
146   reinit();
147 }
148
149 void Model_ExpressionInteger::reinit()
150 {
151   if (!myLab.FindAttribute(TDataStd_Integer::GetID(), myInteger)) {
152     myIsInitialized = false;
153   } else {
154     myIsInitialized = true;
155   }
156 }
157
158 void Model_ExpressionInteger::setValue(const int theValue)
159 {
160   if (!myIsInitialized || myInteger.IsNull()) {
161     myInteger = TDataStd_Integer::Set(myText->Label(), theValue);
162     myIsInitialized = true;
163   } else if (value() != theValue) {
164     myInteger->Set(theValue);
165   }
166 }
167
168 int Model_ExpressionInteger::value()
169 {
170   if (myIsInitialized && !myInteger.IsNull())
171     return myInteger->Get();
172   return std::numeric_limits<int>::max(); // error
173 }
174
175 void Model_ExpressionInteger::setInvalid(const bool theFlag)
176 {
177   if (theFlag) {
178     TDataStd_UAttribute::Set(myText->Label(), kInvalidGUID);
179   } else {
180     myText->Label().ForgetAttribute(kInvalidGUID);
181   }
182 }
183
184 bool Model_ExpressionInteger::isInvalid()
185 {
186   return myText->Label().IsAttribute(kInvalidGUID) == Standard_True;
187 }