Salome HOME
Fix of renaming function
[modules/shaper.git] / src / Model / Model_Data.cpp
1 // File:        Model_Data.hxx
2 // Created:     21 Mar 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include <Model_Data.h>
6 #include <Model_AttributeDocRef.h>
7 #include <Model_AttributeDouble.h>
8 #include <Model_AttributeReference.h>
9 #include <Model_AttributeRefAttr.h>
10 #include <Model_AttributeRefList.h>
11 #include <GeomData_Point.h>
12 #include <GeomData_Point2D.h>
13 #include <GeomData_Dir.h>
14 #include <TDataStd_Name.hxx>
15 #include "Model_Events.h"
16 #include <Events_Loop.h>
17
18 using namespace std;
19
20 Model_Data::Model_Data()
21 {
22 }
23
24 void Model_Data::setLabel(TDF_Label& theLab)
25 {
26   myLab = theLab;
27 }
28
29 string Model_Data::getName()
30 {
31   Handle(TDataStd_Name) aName;
32   if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
33     return string(TCollection_AsciiString(aName->Get()).ToCString());
34   return ""; // not defined
35 }
36
37 void Model_Data::setName(string theName)
38 {
39   bool isModified = false;
40   Handle(TDataStd_Name) aName;
41   if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
42     TDataStd_Name::Set(myLab, theName.c_str());
43     isModified = true;
44   } else {
45     isModified = !aName->Get().IsEqual(theName.c_str());
46     if (isModified)
47       aName->Set(theName.c_str());
48   }
49   if (isModified) {
50     static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_UPDATED);
51     Model_FeatureUpdatedMessage aMsg(myFeature, anEvent);
52     Events_Loop::loop()->send(aMsg, false);
53   }
54 }
55
56 void Model_Data::addAttribute(string theID, string theAttrType)
57 {
58   TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
59   ModelAPI_Attribute* anAttr = 0;
60   if (theAttrType == ModelAPI_AttributeDocRef::type())
61     anAttr = new Model_AttributeDocRef(anAttrLab);
62   else if (theAttrType == ModelAPI_AttributeDouble::type())
63     anAttr = new Model_AttributeDouble(anAttrLab);
64   else if (theAttrType == ModelAPI_AttributeReference::type())
65     anAttr = new Model_AttributeReference(anAttrLab);
66   else if (theAttrType == ModelAPI_AttributeRefAttr::type())
67     anAttr = new Model_AttributeRefAttr(anAttrLab);
68   else if (theAttrType == ModelAPI_AttributeRefList::type())
69     anAttr = new Model_AttributeRefList(anAttrLab);
70   else if (theAttrType == GeomData_Point::type())
71     anAttr = new GeomData_Point(anAttrLab);
72   else if (theAttrType == GeomData_Dir::type())
73     anAttr = new GeomData_Dir(anAttrLab);
74   else if (theAttrType == GeomData_Point2D::type())
75     anAttr = new GeomData_Point2D(anAttrLab);
76
77   if (anAttr) {
78     myAttrs[theID] = boost::shared_ptr<ModelAPI_Attribute>(anAttr);
79     anAttr->setFeature(myFeature);
80   }
81   else
82     ; // TODO: generate error on unknown attribute request and/or add mechanism for customization
83 }
84
85 boost::shared_ptr<ModelAPI_AttributeDocRef> Model_Data::docRef(const string theID)
86 {
87   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
88   if (aFound == myAttrs.end()) {
89     // TODO: generate error on unknown attribute request and/or add mechanism for customization
90     return boost::shared_ptr<ModelAPI_AttributeDocRef>();
91   }
92   boost::shared_ptr<ModelAPI_AttributeDocRef> aRes = 
93     boost::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(aFound->second);
94   if (!aRes) {
95     // TODO: generate error on invalid attribute type request
96   }
97   return aRes;
98 }
99
100 boost::shared_ptr<ModelAPI_AttributeDouble> Model_Data::real(const string theID)
101 {
102   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
103   if (aFound == myAttrs.end()) {
104     // TODO: generate error on unknown attribute request and/or add mechanism for customization
105     return boost::shared_ptr<ModelAPI_AttributeDouble>();
106   }
107   boost::shared_ptr<ModelAPI_AttributeDouble> aRes = 
108     boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aFound->second);
109   if (!aRes) {
110     // TODO: generate error on invalid attribute type request
111   }
112   return aRes;
113 }
114
115 boost::shared_ptr<ModelAPI_AttributeReference> Model_Data::reference(const string theID)
116 {
117   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
118   if (aFound == myAttrs.end()) {
119     // TODO: generate error on unknown attribute request and/or add mechanism for customization
120     return boost::shared_ptr<ModelAPI_AttributeReference>();
121   }
122   boost::shared_ptr<ModelAPI_AttributeReference> aRes = 
123     boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aFound->second);
124   if (!aRes) {
125     // TODO: generate error on invalid attribute type request
126   }
127   return aRes;
128 }
129
130 boost::shared_ptr<ModelAPI_AttributeRefAttr> Model_Data::refattr(const string theID)
131 {
132   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
133   if (aFound == myAttrs.end()) {
134     // TODO: generate error on unknown attribute request and/or add mechanism for customization
135     return boost::shared_ptr<ModelAPI_AttributeRefAttr>();
136   }
137   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRes = 
138     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aFound->second);
139   if (!aRes) {
140     // TODO: generate error on invalid attribute type request
141   }
142   return aRes;
143 }
144
145 boost::shared_ptr<ModelAPI_AttributeRefList> Model_Data::reflist(const string theID)
146 {
147   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
148   if (aFound == myAttrs.end()) {
149     // TODO: generate error on unknown attribute request and/or add mechanism for customization
150     return boost::shared_ptr<ModelAPI_AttributeRefList>();
151   }
152   boost::shared_ptr<ModelAPI_AttributeRefList> aRes = 
153     boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aFound->second);
154   if (!aRes) {
155     // TODO: generate error on invalid attribute type request
156   }
157   return aRes;
158 }
159
160 boost::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string theID)
161 {
162   boost::shared_ptr<ModelAPI_Attribute> aResult;
163   if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
164     return aResult;
165   return myAttrs[theID];
166 }
167
168 const string& Model_Data::id(const boost::shared_ptr<ModelAPI_Attribute> theAttr)
169 {
170   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
171   for(; anAttr != myAttrs.end(); anAttr++) {
172     if (anAttr->second == theAttr) return anAttr->first;
173   }
174   // not found
175   static string anEmpty;
176   return anEmpty;
177 }
178
179 bool Model_Data::isEqual(const boost::shared_ptr<ModelAPI_Data> theData)
180 {
181   boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(theData);
182   if (aData)
183     return myLab.IsEqual(aData->myLab) == Standard_True;
184   return false;
185 }
186
187 bool Model_Data::isValid()
188 {
189   return !myLab.IsNull() && myLab.HasAttribute();
190 }