Salome HOME
ef7408e36f335d3f30a6d9782977442b71f090c7
[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
18 static Standard_GUID kInvalidGUID("caee5ce4-34b1-4b29-abcb-685287d18096");
19
20
21 Model_Expression::Model_Expression(TDF_Label& theLabel)
22 {
23   myIsInitialized = true;
24   if (!theLabel.FindAttribute(TDataStd_Name::GetID(), myText)) {
25     myText = TDataStd_Name::Set(theLabel, TCollection_ExtendedString());
26 //    myIsInitialized = false;
27   }
28   if (!theLabel.FindAttribute(TDataStd_Comment::GetID(), myError)) {
29     myError = TDataStd_Comment::Set(theLabel, TCollection_ExtendedString());
30 //    myIsInitialized = false;
31   }
32   if (!theLabel.FindAttribute(TDataStd_ExtStringList::GetID(), myUsedParameters)) {
33     myUsedParameters = TDataStd_ExtStringList::Set(theLabel);
34 //    myIsInitialized = false;
35   }
36   // All this attributes should not set myIsInitialized = false.
37   // This attributes are optional and may absent in old files. So this is OK.
38   // The reason to set myIsInitialized = false is only absence of
39   // the value attribute.
40 }
41
42 void Model_Expression::setText(const std::string& theValue)
43 {
44   if (text() != theValue)
45     myText->Set(TCollection_ExtendedString(theValue.c_str()));
46
47   setError(text().empty() ? "" : "Not a double value.");
48 }
49
50 std::string Model_Expression::text() const 
51 {
52   return TCollection_AsciiString(myText->Get()).ToCString();
53 }
54
55 void Model_Expression::setError(const std::string& theError)
56 {
57   if (error() != theError)
58     myError->Set(TCollection_ExtendedString(theError.c_str()));
59 }
60
61 std::string Model_Expression::error()
62 {
63   return TCollection_AsciiString(myError->Get()).ToCString();
64 }
65
66 void Model_Expression::setUsedParameters(const std::set<std::string>& theUsedParameters)
67 {
68   myUsedParameters->Clear();
69   std::set<std::string>::const_iterator anIt = theUsedParameters.begin();
70   for (; anIt != theUsedParameters.end(); ++anIt)
71     myUsedParameters->Append(TCollection_ExtendedString(anIt->c_str()));
72 }
73
74 std::set<std::string> Model_Expression::usedParameters() const
75 {
76   std::set<std::string> aResult;
77   TDataStd_ListIteratorOfListOfExtendedString aIt;
78   for (aIt.Initialize(myUsedParameters->List()); aIt.More(); aIt.Next())
79     aResult.insert(TCollection_AsciiString(aIt.Value()).ToCString());
80   return aResult;
81 }
82
83 Model_ExpressionDouble::Model_ExpressionDouble(TDF_Label& theLabel)
84     : Model_Expression(theLabel)
85 {
86   if (!theLabel.FindAttribute(TDataStd_Real::GetID(), myReal)) {
87     myReal = TDataStd_Real::Set(theLabel, 0.);
88     myIsInitialized = false;
89     // MPV: temporarily to support the previously saved files (to check and resolve bugs), to be removed
90     Handle(TDataStd_RealArray) anOldArray;
91     if (theLabel.Father().FindAttribute(TDataStd_RealArray::GetID(), anOldArray) == Standard_True) {
92       myReal->Set(anOldArray->Value(theLabel.Tag() - 1));
93       myIsInitialized = true;
94       Handle(TDataStd_ExtStringArray) anOldExp;
95       if (theLabel.Father().FindAttribute(TDataStd_ExtStringArray::GetID(), anOldExp) == Standard_True) {
96         myText->Set(anOldExp->Value(theLabel.Tag() - 1));
97       }
98     } else {
99       Handle(TDataStd_Real) anOldReal;
100       if (theLabel.Father().FindAttribute(TDataStd_Real::GetID(), anOldReal)) {
101         myIsInitialized = true;
102         myReal->Set(anOldReal->Get());
103         Handle(TDataStd_Name) aText;
104         if (theLabel.Father().FindAttribute(TDataStd_Name::GetID(), aText)) {
105           myText->Set(aText->Get());
106         }
107       }
108     }
109   }
110 }
111
112 void Model_ExpressionDouble::setValue(const double theValue)
113 {
114   if (value() != theValue)
115     myReal->Set(theValue);
116 }
117
118 double Model_ExpressionDouble::value()
119 {
120   return myReal->Get();
121 }
122
123 void Model_ExpressionDouble::setInvalid(const bool theFlag)
124 {
125   if (theFlag) {
126     TDataStd_UAttribute::Set(myReal->Label(), kInvalidGUID);
127   } else {
128     myReal->Label().ForgetAttribute(kInvalidGUID);
129   }
130 }
131
132 bool Model_ExpressionDouble::isInvalid()
133 {
134   return myReal->Label().IsAttribute(kInvalidGUID) == Standard_True;
135 }
136
137
138 Model_ExpressionInteger::Model_ExpressionInteger(TDF_Label& theLabel)
139     : Model_Expression(theLabel)
140 {
141   if (!theLabel.FindAttribute(TDataStd_Integer::GetID(), myInteger)) {
142     myInteger = TDataStd_Integer::Set(theLabel, 0);
143     myIsInitialized = false;
144   }
145 }
146
147 void Model_ExpressionInteger::setValue(const int theValue)
148 {
149   if (value() != theValue)
150     myInteger->Set(theValue);
151 }
152
153 int Model_ExpressionInteger::value()
154 {
155   return myInteger->Get();
156 }
157
158 void Model_ExpressionInteger::setInvalid(const bool theFlag)
159 {
160   if (theFlag) {
161     TDataStd_UAttribute::Set(myInteger->Label(), kInvalidGUID);
162   } else {
163     myInteger->Label().ForgetAttribute(kInvalidGUID);
164   }
165 }
166
167 bool Model_ExpressionInteger::isInvalid()
168 {
169   return myInteger->Label().IsAttribute(kInvalidGUID) == Standard_True;
170 }