]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_Data.cpp
Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[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_AttributeInteger.h>
8 #include <Model_AttributeDouble.h>
9 #include <Model_AttributeReference.h>
10 #include <Model_AttributeRefAttr.h>
11 #include <Model_AttributeRefList.h>
12 #include <Model_AttributeBoolean.h>
13 #include <Model_AttributeString.h>
14 #include <Model_Events.h>
15
16 #include <GeomData_Point.h>
17 #include <GeomData_Point2D.h>
18 #include <GeomData_Dir.h>
19 #include <Events_Loop.h>
20 #include <Events_Error.h>
21
22 #include <TDataStd_Name.hxx>
23 #include <TDataStd_UAttribute.hxx>
24
25 #include <string>
26
27 Model_Data::Model_Data()
28 {
29 }
30
31 void Model_Data::setLabel(TDF_Label theLab)
32 {
33   myLab = theLab;
34 }
35
36 std::string Model_Data::name()
37 {
38   Handle(TDataStd_Name) aName;
39   if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
40     return std::string(TCollection_AsciiString(aName->Get()).ToCString());
41   return "";  // not defined
42 }
43
44 void Model_Data::setName(const std::string& theName)
45 {
46   bool isModified = false;
47   Handle(TDataStd_Name) aName;
48   if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
49     TDataStd_Name::Set(myLab, theName.c_str());
50     isModified = true;
51   } else {
52     isModified = !aName->Get().IsEqual(theName.c_str());
53     if (isModified)
54       aName->Set(theName.c_str());
55   }
56 }
57
58 void Model_Data::addAttribute(const std::string& theID, const std::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 == Model_AttributeInteger::type()) {
65     anAttr = new Model_AttributeInteger(anAttrLab);
66   } else if (theAttrType == ModelAPI_AttributeDouble::type()) {
67     anAttr = new Model_AttributeDouble(anAttrLab);
68   } else if (theAttrType == Model_AttributeBoolean::type()) {
69     anAttr = new Model_AttributeBoolean(anAttrLab);
70   } else if (theAttrType == Model_AttributeString::type()) {
71     anAttr = new Model_AttributeString(anAttrLab);
72   } else if (theAttrType == ModelAPI_AttributeReference::type()) {
73     anAttr = new Model_AttributeReference(anAttrLab);
74   } else if (theAttrType == ModelAPI_AttributeRefAttr::type()) {
75     anAttr = new Model_AttributeRefAttr(anAttrLab);
76   } else if (theAttrType == ModelAPI_AttributeRefList::type()) {
77     anAttr = new Model_AttributeRefList(anAttrLab);
78   } else if (theAttrType == GeomData_Point::type()) {
79     anAttr = new GeomData_Point(anAttrLab);
80   } else if (theAttrType == GeomData_Dir::type()) {
81     anAttr = new GeomData_Dir(anAttrLab);
82   } else if (theAttrType == GeomData_Point2D::type()) {
83     anAttr = new GeomData_Point2D(anAttrLab);
84   }
85   if (anAttr) {
86     myAttrs[theID] = boost::shared_ptr<ModelAPI_Attribute>(anAttr);
87     anAttr->setObject(myObject);
88   } else {
89     Events_Error::send("Can not create unknown type of attribute " + theAttrType);
90   }
91 }
92
93 boost::shared_ptr<ModelAPI_AttributeDocRef> Model_Data::document(const std::string& theID)
94 {
95   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
96   if (aFound == myAttrs.end()) {
97     // TODO: generate error on unknown attribute request and/or add mechanism for customization
98     return boost::shared_ptr<ModelAPI_AttributeDocRef>();
99   }
100   boost::shared_ptr<ModelAPI_AttributeDocRef> aRes = boost::dynamic_pointer_cast<
101       ModelAPI_AttributeDocRef>(aFound->second);
102   if (!aRes) {
103     // TODO: generate error on invalid attribute type request
104   }
105   return aRes;
106 }
107
108 boost::shared_ptr<ModelAPI_AttributeDouble> Model_Data::real(const std::string& theID)
109 {
110   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
111   if (aFound == myAttrs.end()) {
112     // TODO: generate error on unknown attribute request and/or add mechanism for customization
113     return boost::shared_ptr<ModelAPI_AttributeDouble>();
114   }
115   boost::shared_ptr<ModelAPI_AttributeDouble> aRes = boost::dynamic_pointer_cast<
116       ModelAPI_AttributeDouble>(aFound->second);
117   if (!aRes) {
118     // TODO: generate error on invalid attribute type request
119   }
120   return aRes;
121 }
122
123 boost::shared_ptr<ModelAPI_AttributeInteger> Model_Data::integer(const std::string& theID)
124 {
125   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
126   if (aFound == myAttrs.end()) {
127     // TODO: generate error on unknown attribute request and/or add mechanism for customization
128     return boost::shared_ptr<ModelAPI_AttributeInteger>();
129   }
130   boost::shared_ptr<ModelAPI_AttributeInteger> aRes = boost::dynamic_pointer_cast<
131       ModelAPI_AttributeInteger>(aFound->second);
132   if (!aRes) {
133     // TODO: generate error on invalid attribute type request
134   }
135   return aRes;
136 }
137
138 boost::shared_ptr<ModelAPI_AttributeBoolean> Model_Data::boolean(const std::string& theID)
139 {
140   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
141   if (aFound == myAttrs.end()) {
142     // TODO: generate error on unknown attribute request and/or add mechanism for customization
143     return boost::shared_ptr<ModelAPI_AttributeBoolean>();
144   }
145   boost::shared_ptr<ModelAPI_AttributeBoolean> aRes = boost::dynamic_pointer_cast<
146       ModelAPI_AttributeBoolean>(aFound->second);
147   if (!aRes) {
148     // TODO: generate error on invalid attribute type request
149   }
150   return aRes;
151 }
152
153 boost::shared_ptr<ModelAPI_AttributeString> Model_Data::string(const std::string& theID)
154 {
155   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
156   if (aFound == myAttrs.end()) {
157     // TODO: generate error on unknown attribute request and/or add mechanism for customization
158     return boost::shared_ptr<ModelAPI_AttributeString>();
159   }
160   boost::shared_ptr<ModelAPI_AttributeString> aRes =
161       boost::dynamic_pointer_cast<ModelAPI_AttributeString>(aFound->second);
162   if (!aRes) {
163     // TODO: generate error on invalid attribute type request
164   }
165   return aRes;
166
167 }
168
169 boost::shared_ptr<ModelAPI_AttributeReference> Model_Data::reference(const std::string& theID)
170 {
171   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
172   if (aFound == myAttrs.end()) {
173     // TODO: generate error on unknown attribute request and/or add mechanism for customization
174     return boost::shared_ptr<ModelAPI_AttributeReference>();
175   }
176   boost::shared_ptr<ModelAPI_AttributeReference> aRes = boost::dynamic_pointer_cast<
177       ModelAPI_AttributeReference>(aFound->second);
178   if (!aRes) {
179     // TODO: generate error on invalid attribute type request
180   }
181   return aRes;
182 }
183
184 boost::shared_ptr<ModelAPI_AttributeRefAttr> Model_Data::refattr(const std::string& theID)
185 {
186   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
187   if (aFound == myAttrs.end()) {
188     // TODO: generate error on unknown attribute request and/or add mechanism for customization
189     return boost::shared_ptr<ModelAPI_AttributeRefAttr>();
190   }
191   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRes = boost::dynamic_pointer_cast<
192       ModelAPI_AttributeRefAttr>(aFound->second);
193   if (!aRes) {
194     // TODO: generate error on invalid attribute type request
195   }
196   return aRes;
197 }
198
199 boost::shared_ptr<ModelAPI_AttributeRefList> Model_Data::reflist(const std::string& theID)
200 {
201   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
202   if (aFound == myAttrs.end()) {
203     // TODO: generate error on unknown attribute request and/or add mechanism for customization
204     return boost::shared_ptr<ModelAPI_AttributeRefList>();
205   }
206   boost::shared_ptr<ModelAPI_AttributeRefList> aRes = boost::dynamic_pointer_cast<
207       ModelAPI_AttributeRefList>(aFound->second);
208   if (!aRes) {
209     // TODO: generate error on invalid attribute type request
210   }
211   return aRes;
212 }
213
214 boost::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
215 {
216   boost::shared_ptr<ModelAPI_Attribute> aResult;
217   if (myAttrs.find(theID) == myAttrs.end())  // no such attribute
218     return aResult;
219   return myAttrs[theID];
220 }
221
222 const std::string& Model_Data::id(const boost::shared_ptr<ModelAPI_Attribute>& theAttr)
223 {
224   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
225   for (; anAttr != myAttrs.end(); anAttr++) {
226     if (anAttr->second == theAttr)
227       return anAttr->first;
228   }
229   // not found
230   static std::string anEmpty;
231   return anEmpty;
232 }
233
234 bool Model_Data::isEqual(const boost::shared_ptr<ModelAPI_Data>& theData)
235 {
236   boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(theData);
237   if (aData)
238     return myLab.IsEqual(aData->myLab) == Standard_True ;
239   return false;
240 }
241
242 bool Model_Data::isValid()
243 {
244   return !myLab.IsNull() && myLab.HasAttribute();
245 }
246
247 std::list<boost::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
248 {
249   std::list<boost::shared_ptr<ModelAPI_Attribute> > aResult;
250   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter = 
251     myAttrs.begin();
252   for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
253     if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
254       aResult.push_back(anAttrsIter->second);
255     }
256   }
257   return aResult;
258 }
259
260 std::list<std::string> Model_Data::attributesIDs(const std::string& theType) 
261 {
262   std::list<std::string> aResult;
263   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter = 
264     myAttrs.begin();
265   for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
266     if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
267       aResult.push_back(anAttrsIter->first);
268     }
269   }
270   return aResult;
271 }
272
273 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
274 {
275   theAttr->setInitialized();
276   if (theAttr->isArgument()) {
277     static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
278     ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
279   }
280 }
281
282 void Model_Data::erase()
283 {
284   if (!myLab.IsNull())
285     myLab.ForgetAllAttributes();
286 }
287
288 Standard_GUID kMustBeUpdatedGUID("baede74c-31a6-4416-9c4d-e48ce65f2005");
289
290 void Model_Data::mustBeUpdated(const bool theFlag)
291 {
292   if (theFlag)
293     TDataStd_UAttribute::Set(myLab, kMustBeUpdatedGUID);
294   else
295     myLab.ForgetAttribute(kMustBeUpdatedGUID);
296 }
297
298 bool Model_Data::mustBeUpdated()
299 {
300   return myLab.IsAttribute(kMustBeUpdatedGUID) == Standard_True;
301 }