Salome HOME
Fix for the TestCompositeFeaturesOnCompSolids.py test passing: after commit of nested...
[modules/shaper.git] / src / Model / Model_Expression.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_Expression.cpp
4 // Created:     5 Aug 2015
5 // Author:      Sergey POKHODENKO
6
7 #include "Model_Expression.h"
8
9 #include <TCollection_AsciiString.hxx>
10 #include <TCollection_ExtendedString.hxx>
11 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
12 #include <TDataStd_UAttribute.hxx>
13
14 #include <TDataStd_RealArray.hxx>
15 #include <TDataStd_ExtStringArray.hxx>
16
17 #include <limits>
18
19 static Standard_GUID kInvalidGUID("caee5ce4-34b1-4b29-abcb-685287d18096");
20
21
22 Model_Expression::Model_Expression(TDF_Label& theLabel)
23 {
24   if (!theLabel.FindAttribute(TDataStd_Name::GetID(), myText)) {
25     myText = TDataStd_Name::Set(theLabel, TCollection_ExtendedString());
26   }
27   if (!theLabel.FindAttribute(TDataStd_Comment::GetID(), myError)) {
28     myError = TDataStd_Comment::Set(theLabel, TCollection_ExtendedString());
29   }
30   if (!theLabel.FindAttribute(TDataStd_ExtStringList::GetID(), myUsedParameters)) {
31     myUsedParameters = TDataStd_ExtStringList::Set(theLabel);
32   }
33 }
34
35 void Model_Expression::setText(const std::string& theValue)
36 {
37   if (text() != theValue)
38     myText->Set(TCollection_ExtendedString(theValue.c_str()));
39
40   setError(text().empty() ? "" : "Not a double value.");
41 }
42
43 std::string Model_Expression::text() const 
44 {
45   return TCollection_AsciiString(myText->Get()).ToCString();
46 }
47
48 void Model_Expression::setError(const std::string& theError)
49 {
50   if (error() != theError)
51     myError->Set(TCollection_ExtendedString(theError.c_str()));
52 }
53
54 std::string Model_Expression::error()
55 {
56   return TCollection_AsciiString(myError->Get()).ToCString();
57 }
58
59 void Model_Expression::setUsedParameters(const std::set<std::string>& theUsedParameters)
60 {
61   myUsedParameters->Clear();
62   std::set<std::string>::const_iterator anIt = theUsedParameters.begin();
63   for (; anIt != theUsedParameters.end(); ++anIt)
64     myUsedParameters->Append(TCollection_ExtendedString(anIt->c_str()));
65 }
66
67 std::set<std::string> Model_Expression::usedParameters() const
68 {
69   std::set<std::string> aResult;
70   TDataStd_ListIteratorOfListOfExtendedString aIt;
71   for (aIt.Initialize(myUsedParameters->List()); aIt.More(); aIt.Next())
72     aResult.insert(TCollection_AsciiString(aIt.Value()).ToCString());
73   return aResult;
74 }
75
76 Model_ExpressionDouble::Model_ExpressionDouble(TDF_Label& theLabel)
77     : Model_Expression(theLabel)
78 {
79   if (!theLabel.FindAttribute(TDataStd_Real::GetID(), myReal)) {
80     myIsInitialized = false;
81     // MPV: temporarily to support the previously saved files (to check and resolve bugs), to be removed
82     Handle(TDataStd_RealArray) anOldArray;
83     if (theLabel.Father().FindAttribute(TDataStd_RealArray::GetID(), anOldArray) == Standard_True) {
84       myReal = TDataStd_Real::Set(theLabel, 0.);
85       myReal->Set(anOldArray->Value(theLabel.Tag() - 1));
86       myIsInitialized = true;
87       Handle(TDataStd_ExtStringArray) anOldExp;
88       if (theLabel.Father().FindAttribute(TDataStd_ExtStringArray::GetID(), anOldExp) == Standard_True) {
89         myText->Set(anOldExp->Value(theLabel.Tag() - 1));
90       }
91     } else {
92       Handle(TDataStd_Real) anOldReal;
93       if (theLabel.Father().FindAttribute(TDataStd_Real::GetID(), anOldReal)) {
94         myIsInitialized = true;
95         myReal = TDataStd_Real::Set(theLabel, 0.);
96         myReal->Set(anOldReal->Get());
97         Handle(TDataStd_Name) aText;
98         if (theLabel.Father().FindAttribute(TDataStd_Name::GetID(), aText)) {
99           myText->Set(aText->Get());
100         }
101       }
102     }
103   } else
104     myIsInitialized = true;
105 }
106
107 void Model_ExpressionDouble::setValue(const double theValue)
108 {
109   if (!myIsInitialized) {
110     myReal = TDataStd_Real::Set(myText->Label(), theValue);
111     myIsInitialized = true;
112   } else if (value() != theValue) {
113     myReal->Set(theValue);
114   }
115 }
116
117 double Model_ExpressionDouble::value()
118 {
119   if (myIsInitialized)
120     return myReal->Get();
121   return std::numeric_limits<double>::max(); // error
122 }
123
124 void Model_ExpressionDouble::setInvalid(const bool theFlag)
125 {
126   if (theFlag) {
127     TDataStd_UAttribute::Set(myText->Label(), kInvalidGUID);
128   } else {
129     myText->Label().ForgetAttribute(kInvalidGUID);
130   }
131 }
132
133 bool Model_ExpressionDouble::isInvalid()
134 {
135   return myText->Label().IsAttribute(kInvalidGUID) == Standard_True;
136 }
137
138
139 Model_ExpressionInteger::Model_ExpressionInteger(TDF_Label& theLabel)
140     : Model_Expression(theLabel)
141 {
142   if (!theLabel.FindAttribute(TDataStd_Integer::GetID(), myInteger)) {
143     myIsInitialized = false;
144   } else
145     myIsInitialized = true;
146 }
147
148 void Model_ExpressionInteger::setValue(const int theValue)
149 {
150   if (!myIsInitialized) {
151     myInteger = TDataStd_Integer::Set(myText->Label(), theValue);
152     myIsInitialized = true;
153   } else if (value() != theValue) {
154     myInteger->Set(theValue);
155   }
156 }
157
158 int Model_ExpressionInteger::value()
159 {
160   if (myIsInitialized)
161     return myInteger->Get();
162   return std::numeric_limits<int>::max(); // error
163 }
164
165 void Model_ExpressionInteger::setInvalid(const bool theFlag)
166 {
167   if (theFlag) {
168     TDataStd_UAttribute::Set(myText->Label(), kInvalidGUID);
169   } else {
170     myText->Label().ForgetAttribute(kInvalidGUID);
171   }
172 }
173
174 bool Model_ExpressionInteger::isInvalid()
175 {
176   return myText->Label().IsAttribute(kInvalidGUID) == Standard_True;
177 }