]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_Data.cpp
Salome HOME
Sources of the application adopted to RHEL6 x64. The newest version of Eclipse IDE...
[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 <Model_AttributeBoolean.h>
12 #include <GeomData_Point.h>
13 #include <GeomData_Point2D.h>
14 #include <GeomData_Dir.h>
15 #include <TDataStd_Name.hxx>
16 #include "Model_Events.h"
17 #include <Events_Loop.h>
18 #include <Events_Error.h>
19
20
21 using namespace std;
22
23 Model_Data::Model_Data()
24 {
25 }
26
27 void Model_Data::setLabel(TDF_Label theLab)
28 {
29   myLab = theLab;
30 }
31
32 string Model_Data::name()
33 {
34   Handle(TDataStd_Name) aName;
35   if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
36     return string(TCollection_AsciiString(aName->Get()).ToCString());
37   return ""; // not defined
38 }
39
40 void Model_Data::setName(string theName)
41 {
42   bool isModified = false;
43   Handle(TDataStd_Name) aName;
44   if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
45     TDataStd_Name::Set(myLab, theName.c_str());
46     isModified = true;
47   } else {
48     isModified = !aName->Get().IsEqual(theName.c_str());
49     if (isModified)
50       aName->Set(theName.c_str());
51   }
52   if (isModified) {
53     static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
54     ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent, false);
55   }
56 }
57
58 void Model_Data::addAttribute(string theID, string theAttrType)
59 {
60   TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
61   ModelAPI_Attribute* anAttr = 0;
62   if (theAttrType == ModelAPI_AttributeDocRef::type())
63     anAttr = new Model_AttributeDocRef(anAttrLab);
64   else if (theAttrType == ModelAPI_AttributeDouble::type())
65     anAttr = new Model_AttributeDouble(anAttrLab);
66   else if (theAttrType == ModelAPI_AttributeReference::type())
67     anAttr = new Model_AttributeReference(anAttrLab);
68   else if (theAttrType == ModelAPI_AttributeRefAttr::type())
69     anAttr = new Model_AttributeRefAttr(anAttrLab);
70   else if (theAttrType == ModelAPI_AttributeRefList::type())
71     anAttr = new Model_AttributeRefList(anAttrLab);
72   else if (theAttrType == GeomData_Point::type())
73     anAttr = new GeomData_Point(anAttrLab);
74   else if (theAttrType == GeomData_Dir::type())
75     anAttr = new GeomData_Dir(anAttrLab);
76   else if (theAttrType == GeomData_Point2D::type())
77     anAttr = new GeomData_Point2D(anAttrLab);
78   else if (theAttrType == Model_AttributeBoolean::type())
79     anAttr = new Model_AttributeBoolean(anAttrLab);
80
81   if (anAttr) {
82     myAttrs[theID] = boost::shared_ptr<ModelAPI_Attribute>(anAttr);
83     anAttr->setObject(myObject);
84   }
85   else {
86     Events_Error::send("Can not create unknown type of attribute " + theAttrType);
87   }
88 }
89
90 boost::shared_ptr<ModelAPI_AttributeDocRef> Model_Data::docRef(const string theID)
91 {
92   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
93   if (aFound == myAttrs.end()) {
94     // TODO: generate error on unknown attribute request and/or add mechanism for customization
95     return boost::shared_ptr<ModelAPI_AttributeDocRef>();
96   }
97   boost::shared_ptr<ModelAPI_AttributeDocRef> aRes = 
98     boost::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(aFound->second);
99   if (!aRes) {
100     // TODO: generate error on invalid attribute type request
101   }
102   return aRes;
103 }
104
105 boost::shared_ptr<ModelAPI_AttributeDouble> Model_Data::real(const string theID)
106 {
107   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
108   if (aFound == myAttrs.end()) {
109     // TODO: generate error on unknown attribute request and/or add mechanism for customization
110     return boost::shared_ptr<ModelAPI_AttributeDouble>();
111   }
112   boost::shared_ptr<ModelAPI_AttributeDouble> aRes = 
113     boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aFound->second);
114   if (!aRes) {
115     // TODO: generate error on invalid attribute type request
116   }
117   return aRes;
118 }
119
120 boost::shared_ptr<ModelAPI_AttributeBoolean> Model_Data::boolean(const std::string theID)
121 {
122   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
123   if (aFound == myAttrs.end()) {
124     // TODO: generate error on unknown attribute request and/or add mechanism for customization
125     return boost::shared_ptr<ModelAPI_AttributeBoolean>();
126   }
127   boost::shared_ptr<ModelAPI_AttributeBoolean> aRes = 
128     boost::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aFound->second);
129   if (!aRes) {
130     // TODO: generate error on invalid attribute type request
131   }
132   return aRes;
133 }
134
135 boost::shared_ptr<ModelAPI_AttributeReference> Model_Data::reference(const string theID)
136 {
137   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
138   if (aFound == myAttrs.end()) {
139     // TODO: generate error on unknown attribute request and/or add mechanism for customization
140     return boost::shared_ptr<ModelAPI_AttributeReference>();
141   }
142   boost::shared_ptr<ModelAPI_AttributeReference> aRes = 
143     boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aFound->second);
144   if (!aRes) {
145     // TODO: generate error on invalid attribute type request
146   }
147   return aRes;
148 }
149
150 boost::shared_ptr<ModelAPI_AttributeRefAttr> Model_Data::refattr(const string theID)
151 {
152   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
153   if (aFound == myAttrs.end()) {
154     // TODO: generate error on unknown attribute request and/or add mechanism for customization
155     return boost::shared_ptr<ModelAPI_AttributeRefAttr>();
156   }
157   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRes = 
158     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aFound->second);
159   if (!aRes) {
160     // TODO: generate error on invalid attribute type request
161   }
162   return aRes;
163 }
164
165 boost::shared_ptr<ModelAPI_AttributeRefList> Model_Data::reflist(const string theID)
166 {
167   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
168   if (aFound == myAttrs.end()) {
169     // TODO: generate error on unknown attribute request and/or add mechanism for customization
170     return boost::shared_ptr<ModelAPI_AttributeRefList>();
171   }
172   boost::shared_ptr<ModelAPI_AttributeRefList> aRes = 
173     boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aFound->second);
174   if (!aRes) {
175     // TODO: generate error on invalid attribute type request
176   }
177   return aRes;
178 }
179
180 boost::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string theID)
181 {
182   boost::shared_ptr<ModelAPI_Attribute> aResult;
183   if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
184     return aResult;
185   return myAttrs[theID];
186 }
187
188 const string& Model_Data::id(const boost::shared_ptr<ModelAPI_Attribute> theAttr)
189 {
190   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
191   for(; anAttr != myAttrs.end(); anAttr++) {
192     if (anAttr->second == theAttr) return anAttr->first;
193   }
194   // not found
195   static string anEmpty;
196   return anEmpty;
197 }
198
199 bool Model_Data::isEqual(const boost::shared_ptr<ModelAPI_Data> theData)
200 {
201   boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(theData);
202   if (aData)
203     return myLab.IsEqual(aData->myLab) == Standard_True;
204   return false;
205 }
206
207 bool Model_Data::isValid()
208 {
209   return !myLab.IsNull() && myLab.HasAttribute();
210 }
211
212 list<boost::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const string theType)
213 {
214   list<boost::shared_ptr<ModelAPI_Attribute> > aResult;
215   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter = myAttrs.begin();
216   for(; anAttrsIter != myAttrs.end(); anAttrsIter++) {
217     if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
218       aResult.push_back(anAttrsIter->second);
219     }
220   }
221   return aResult;
222 }
223
224 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
225 {
226   theAttr->setInitialized();
227   static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
228   ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
229 }