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