Salome HOME
1529d3bc087cf1c78bd0c24e7c23561ad2027dae
[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 Model_Expression::Model_Expression(TDF_Label& theLabel)
15 {
16   myIsInitialized = true;
17   if (!theLabel.FindAttribute(TDataStd_Real::GetID(), myReal)) {
18     myReal = TDataStd_Real::Set(theLabel, 0.);
19     myIsInitialized = false;
20   }
21   if (!theLabel.FindAttribute(TDataStd_Name::GetID(), myText)) {
22     myText = TDataStd_Name::Set(theLabel, TCollection_ExtendedString());
23     myIsInitialized = false;
24   }
25   if (!theLabel.FindAttribute(TDataStd_Comment::GetID(), myError)) {
26     myError = TDataStd_Comment::Set(theLabel, TCollection_ExtendedString());
27     myIsInitialized = false;
28   }
29   if (!theLabel.FindAttribute(TDataStd_ExtStringList::GetID(), myUsedParameters)) {
30     myUsedParameters = TDataStd_ExtStringList::Set(theLabel);
31     myIsInitialized = false;
32   }
33 }
34
35 void Model_Expression::setValue(const double theValue)
36 {
37   if (value() != theValue)
38     myReal->Set(theValue);
39 }
40
41 double Model_Expression::value()
42 {
43   return myReal->Get();
44 }
45
46 void Model_Expression::setText(const std::string& theValue)
47 {
48   if (text() != theValue)
49     myText->Set(TCollection_ExtendedString(theValue.c_str()));
50 }
51
52 std::string Model_Expression::text() const 
53 {
54   return TCollection_AsciiString(myText->Get()).ToCString();
55 }
56
57 static Standard_GUID kInvalidGUID("caee5ce4-34b1-4b29-abcb-685287d18096");
58
59 void Model_Expression::setInvalid(const bool theFlag)
60 {
61   if (theFlag) {
62     TDataStd_UAttribute::Set(myReal->Label(), kInvalidGUID);
63   } else {
64     myReal->Label().ForgetAttribute(kInvalidGUID);
65   }
66 }
67
68 bool Model_Expression::isInvalid()
69 {
70   return myReal->Label().IsAttribute(kInvalidGUID) == Standard_True;
71 }
72
73 void Model_Expression::setError(const std::string& theError)
74 {
75   if (error() != theError)
76     myError->Set(TCollection_ExtendedString(theError.c_str()));
77 }
78
79 std::string Model_Expression::error()
80 {
81   return TCollection_AsciiString(myError->Get()).ToCString();
82 }
83
84 void Model_Expression::setUsedParameters(const std::set<std::string>& theUsedParameters)
85 {
86   myUsedParameters->Clear();
87   std::set<std::string>::const_iterator anIt = theUsedParameters.begin();
88   for (; anIt != theUsedParameters.end(); ++anIt)
89     myUsedParameters->Append(TCollection_ExtendedString(anIt->c_str()));
90 }
91
92 std::set<std::string> Model_Expression::usedParameters() const
93 {
94   std::set<std::string> aResult;
95   TDataStd_ListIteratorOfListOfExtendedString aIt;
96   for (aIt.Initialize(myUsedParameters->List()); aIt.More(); aIt.Next())
97     aResult.insert(TCollection_AsciiString(aIt.Value()).ToCString());
98   return aResult;
99 }