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