1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_Expression.cpp
5 // Author: Sergey POKHODENKO
7 #include "Model_Expression.h"
9 #include <TCollection_AsciiString.hxx>
10 #include <TCollection_ExtendedString.hxx>
11 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
12 #include <TDataStd_UAttribute.hxx>
14 #include <TDataStd_RealArray.hxx>
15 #include <TDataStd_ExtStringArray.hxx>
18 static Standard_GUID kInvalidGUID("caee5ce4-34b1-4b29-abcb-685287d18096");
21 Model_Expression::Model_Expression(TDF_Label& theLabel)
23 myIsInitialized = true;
24 if (!theLabel.FindAttribute(TDataStd_Name::GetID(), myText)) {
25 myText = TDataStd_Name::Set(theLabel, TCollection_ExtendedString());
26 // myIsInitialized = false;
28 if (!theLabel.FindAttribute(TDataStd_Comment::GetID(), myError)) {
29 myError = TDataStd_Comment::Set(theLabel, TCollection_ExtendedString());
30 // myIsInitialized = false;
32 if (!theLabel.FindAttribute(TDataStd_ExtStringList::GetID(), myUsedParameters)) {
33 myUsedParameters = TDataStd_ExtStringList::Set(theLabel);
34 // myIsInitialized = false;
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.
42 void Model_Expression::setText(const std::string& theValue)
44 if (text() != theValue)
45 myText->Set(TCollection_ExtendedString(theValue.c_str()));
47 setError(text().empty() ? "" : "Not a double value.");
50 std::string Model_Expression::text() const
52 return TCollection_AsciiString(myText->Get()).ToCString();
55 void Model_Expression::setError(const std::string& theError)
57 if (error() != theError)
58 myError->Set(TCollection_ExtendedString(theError.c_str()));
61 std::string Model_Expression::error()
63 return TCollection_AsciiString(myError->Get()).ToCString();
66 void Model_Expression::setUsedParameters(const std::set<std::string>& theUsedParameters)
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()));
74 std::set<std::string> Model_Expression::usedParameters() const
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());
83 Model_ExpressionDouble::Model_ExpressionDouble(TDF_Label& theLabel)
84 : Model_Expression(theLabel)
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));
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());
112 void Model_ExpressionDouble::setValue(const double theValue)
114 if (value() != theValue)
115 myReal->Set(theValue);
118 double Model_ExpressionDouble::value()
120 return myReal->Get();
123 void Model_ExpressionDouble::setInvalid(const bool theFlag)
126 TDataStd_UAttribute::Set(myReal->Label(), kInvalidGUID);
128 myReal->Label().ForgetAttribute(kInvalidGUID);
132 bool Model_ExpressionDouble::isInvalid()
134 return myReal->Label().IsAttribute(kInvalidGUID) == Standard_True;
138 Model_ExpressionInteger::Model_ExpressionInteger(TDF_Label& theLabel)
139 : Model_Expression(theLabel)
141 if (!theLabel.FindAttribute(TDataStd_Integer::GetID(), myInteger)) {
142 myInteger = TDataStd_Integer::Set(theLabel, 0);
143 myIsInitialized = false;
147 void Model_ExpressionInteger::setValue(const int theValue)
149 if (value() != theValue)
150 myInteger->Set(theValue);
153 int Model_ExpressionInteger::value()
155 return myInteger->Get();
158 void Model_ExpressionInteger::setInvalid(const bool theFlag)
161 TDataStd_UAttribute::Set(myInteger->Label(), kInvalidGUID);
163 myInteger->Label().ForgetAttribute(kInvalidGUID);
167 bool Model_ExpressionInteger::isInvalid()
169 return myInteger->Label().IsAttribute(kInvalidGUID) == Standard_True;