Salome HOME
Provide dimensions edit
[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_AttributeSelection.h>
15 #include <Model_AttributeSelectionList.h>
16 #include <Model_Events.h>
17 #include <ModelAPI_Feature.h>
18 #include <ModelAPI_Result.h>
19 #include <ModelAPI_Validator.h>
20
21 #include <GeomData_Point.h>
22 #include <GeomData_Point2D.h>
23 #include <GeomData_Dir.h>
24 #include <Events_Loop.h>
25 #include <Events_Error.h>
26
27 #include <TDataStd_Name.hxx>
28 #include <TDataStd_UAttribute.hxx>
29
30 #include <string>
31
32 Model_Data::Model_Data()
33 {
34 }
35
36 void Model_Data::setLabel(TDF_Label theLab)
37 {
38   myLab = theLab;
39 }
40
41 std::string Model_Data::name()
42 {
43   Handle(TDataStd_Name) aName;
44   if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
45     return std::string(TCollection_AsciiString(aName->Get()).ToCString());
46   return "";  // not defined
47 }
48
49 void Model_Data::setName(const std::string& theName)
50 {
51   bool isModified = false;
52   Handle(TDataStd_Name) aName;
53   if (!myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
54     TDataStd_Name::Set(myLab, theName.c_str());
55     isModified = true;
56   } else {
57     isModified = !aName->Get().IsEqual(theName.c_str());
58     if (isModified)
59       aName->Set(theName.c_str());
60   }
61 }
62
63 void Model_Data::addAttribute(const std::string& theID, const std::string theAttrType)
64 {
65   TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
66   ModelAPI_Attribute* anAttr = 0;
67   if (theAttrType == ModelAPI_AttributeDocRef::type()) {
68     anAttr = new Model_AttributeDocRef(anAttrLab);
69   } else if (theAttrType == Model_AttributeInteger::type()) {
70     anAttr = new Model_AttributeInteger(anAttrLab);
71   } else if (theAttrType == ModelAPI_AttributeDouble::type()) {
72     anAttr = new Model_AttributeDouble(anAttrLab);
73   } else if (theAttrType == Model_AttributeBoolean::type()) {
74     anAttr = new Model_AttributeBoolean(anAttrLab);
75   } else if (theAttrType == Model_AttributeString::type()) {
76     anAttr = new Model_AttributeString(anAttrLab);
77   } else if (theAttrType == ModelAPI_AttributeReference::type()) {
78     anAttr = new Model_AttributeReference(anAttrLab);
79   } else if (theAttrType == ModelAPI_AttributeSelection::type()) {
80     anAttr = new Model_AttributeSelection(anAttrLab);
81   } else if (theAttrType == ModelAPI_AttributeSelectionList::type()) {
82     anAttr = new Model_AttributeSelectionList(anAttrLab);
83   } else if (theAttrType == ModelAPI_AttributeRefAttr::type()) {
84     anAttr = new Model_AttributeRefAttr(anAttrLab);
85   } else if (theAttrType == ModelAPI_AttributeRefList::type()) {
86     anAttr = new Model_AttributeRefList(anAttrLab);
87   } else if (theAttrType == GeomData_Point::type()) {
88     anAttr = new GeomData_Point(anAttrLab);
89   } else if (theAttrType == GeomData_Dir::type()) {
90     anAttr = new GeomData_Dir(anAttrLab);
91   } else if (theAttrType == GeomData_Point2D::type()) {
92     anAttr = new GeomData_Point2D(anAttrLab);
93   }
94   if (anAttr) {
95     myAttrs[theID] = boost::shared_ptr<ModelAPI_Attribute>(anAttr);
96     anAttr->setObject(myObject);
97   } else {
98     Events_Error::send("Can not create unknown type of attribute " + theAttrType);
99   }
100 }
101
102 boost::shared_ptr<ModelAPI_AttributeDocRef> Model_Data::document(const std::string& theID)
103 {
104   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = 
105     myAttrs.find(theID);
106   if (aFound == myAttrs.end()) {
107     // TODO: generate error on unknown attribute request and/or add mechanism for customization
108     return boost::shared_ptr<ModelAPI_AttributeDocRef>();
109   }
110   boost::shared_ptr<ModelAPI_AttributeDocRef> aRes = boost::dynamic_pointer_cast<
111       ModelAPI_AttributeDocRef>(aFound->second);
112   if (!aRes) {
113     // TODO: generate error on invalid attribute type request
114   }
115   return aRes;
116 }
117
118 boost::shared_ptr<ModelAPI_AttributeDouble> Model_Data::real(const std::string& theID)
119 {
120   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = 
121     myAttrs.find(theID);
122   if (aFound == myAttrs.end()) {
123     // TODO: generate error on unknown attribute request and/or add mechanism for customization
124     return boost::shared_ptr<ModelAPI_AttributeDouble>();
125   }
126   boost::shared_ptr<ModelAPI_AttributeDouble> aRes = boost::dynamic_pointer_cast<
127       ModelAPI_AttributeDouble>(aFound->second);
128   if (!aRes) {
129     // TODO: generate error on invalid attribute type request
130   }
131   return aRes;
132 }
133
134 boost::shared_ptr<ModelAPI_AttributeInteger> Model_Data::integer(const std::string& theID)
135 {
136   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = 
137     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_AttributeInteger>();
141   }
142   boost::shared_ptr<ModelAPI_AttributeInteger> aRes = boost::dynamic_pointer_cast<
143       ModelAPI_AttributeInteger>(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_AttributeBoolean> Model_Data::boolean(const std::string& theID)
151 {
152   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = 
153     myAttrs.find(theID);
154   if (aFound == myAttrs.end()) {
155     // TODO: generate error on unknown attribute request and/or add mechanism for customization
156     return boost::shared_ptr<ModelAPI_AttributeBoolean>();
157   }
158   boost::shared_ptr<ModelAPI_AttributeBoolean> aRes = boost::dynamic_pointer_cast<
159       ModelAPI_AttributeBoolean>(aFound->second);
160   if (!aRes) {
161     // TODO: generate error on invalid attribute type request
162   }
163   return aRes;
164 }
165
166 boost::shared_ptr<ModelAPI_AttributeString> Model_Data::string(const std::string& theID)
167 {
168   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = 
169     myAttrs.find(theID);
170   if (aFound == myAttrs.end()) {
171     // TODO: generate error on unknown attribute request and/or add mechanism for customization
172     return boost::shared_ptr<ModelAPI_AttributeString>();
173   }
174   boost::shared_ptr<ModelAPI_AttributeString> aRes =
175       boost::dynamic_pointer_cast<ModelAPI_AttributeString>(aFound->second);
176   if (!aRes) {
177     // TODO: generate error on invalid attribute type request
178   }
179   return aRes;
180
181 }
182
183 boost::shared_ptr<ModelAPI_AttributeReference> Model_Data::reference(const std::string& theID)
184 {
185   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = 
186     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_AttributeReference>();
190   }
191   boost::shared_ptr<ModelAPI_AttributeReference> aRes = boost::dynamic_pointer_cast<
192       ModelAPI_AttributeReference>(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_AttributeSelection> Model_Data::selection(const std::string& theID)
200 {
201   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = 
202     myAttrs.find(theID);
203   if (aFound == myAttrs.end()) {
204     // TODO: generate error on unknown attribute request and/or add mechanism for customization
205     return boost::shared_ptr<ModelAPI_AttributeSelection>();
206   }
207   boost::shared_ptr<ModelAPI_AttributeSelection> aRes = 
208     boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aFound->second);
209   if (!aRes) {
210     // TODO: generate error on invalid attribute type request
211   }
212   return aRes;
213 }
214
215 boost::shared_ptr<ModelAPI_AttributeSelectionList> 
216   Model_Data::selectionList(const std::string& theID)
217 {
218   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = 
219     myAttrs.find(theID);
220   if (aFound == myAttrs.end()) {
221     // TODO: generate error on unknown attribute request and/or add mechanism for customization
222     return boost::shared_ptr<ModelAPI_AttributeSelectionList>();
223   }
224   boost::shared_ptr<ModelAPI_AttributeSelectionList> aRes = 
225     boost::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aFound->second);
226   if (!aRes) {
227     // TODO: generate error on invalid attribute type request
228   }
229   return aRes;
230 }
231
232 boost::shared_ptr<ModelAPI_AttributeRefAttr> Model_Data::refattr(const std::string& theID)
233 {
234   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = 
235     myAttrs.find(theID);
236   if (aFound == myAttrs.end()) {
237     // TODO: generate error on unknown attribute request and/or add mechanism for customization
238     return boost::shared_ptr<ModelAPI_AttributeRefAttr>();
239   }
240   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRes = boost::dynamic_pointer_cast<
241       ModelAPI_AttributeRefAttr>(aFound->second);
242   if (!aRes) {
243     // TODO: generate error on invalid attribute type request
244   }
245   return aRes;
246 }
247
248 boost::shared_ptr<ModelAPI_AttributeRefList> Model_Data::reflist(const std::string& theID)
249 {
250   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = 
251     myAttrs.find(theID);
252   if (aFound == myAttrs.end()) {
253     // TODO: generate error on unknown attribute request and/or add mechanism for customization
254     return boost::shared_ptr<ModelAPI_AttributeRefList>();
255   }
256   boost::shared_ptr<ModelAPI_AttributeRefList> aRes = boost::dynamic_pointer_cast<
257       ModelAPI_AttributeRefList>(aFound->second);
258   if (!aRes) {
259     // TODO: generate error on invalid attribute type request
260   }
261   return aRes;
262 }
263
264 boost::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string& theID)
265 {
266   boost::shared_ptr<ModelAPI_Attribute> aResult;
267   if (myAttrs.find(theID) == myAttrs.end())  // no such attribute
268     return aResult;
269   return myAttrs[theID];
270 }
271
272 const std::string& Model_Data::id(const boost::shared_ptr<ModelAPI_Attribute>& theAttr)
273 {
274   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = 
275     myAttrs.begin();
276   for (; anAttr != myAttrs.end(); anAttr++) {
277     if (anAttr->second == theAttr)
278       return anAttr->first;
279   }
280   // not found
281   static std::string anEmpty;
282   return anEmpty;
283 }
284
285 bool Model_Data::isEqual(const boost::shared_ptr<ModelAPI_Data>& theData)
286 {
287   boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(theData);
288   if (aData)
289     return myLab.IsEqual(aData->myLab) == Standard_True ;
290   return false;
291 }
292
293 bool Model_Data::isValid()
294 {
295   return !myLab.IsNull() && myLab.HasAttribute();
296 }
297
298 std::list<boost::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
299 {
300   std::list<boost::shared_ptr<ModelAPI_Attribute> > aResult;
301   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter = 
302     myAttrs.begin();
303   for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
304     if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
305       aResult.push_back(anAttrsIter->second);
306     }
307   }
308   return aResult;
309 }
310
311 std::list<std::string> Model_Data::attributesIDs(const std::string& theType) 
312 {
313   std::list<std::string> aResult;
314   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter = 
315     myAttrs.begin();
316   for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
317     if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
318       aResult.push_back(anAttrsIter->first);
319     }
320   }
321   return aResult;
322 }
323
324 void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
325 {
326   theAttr->setInitialized();
327   if (theAttr->isArgument()) {
328     static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
329     ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
330   }
331 }
332
333 void Model_Data::erase()
334 {
335   if (!myLab.IsNull())
336     myLab.ForgetAllAttributes();
337 }
338
339 /// identifeir of the "must be updated" flag in the data tree
340 Standard_GUID kMustBeUpdatedGUID("baede74c-31a6-4416-9c4d-e48ce65f2005");
341
342 void Model_Data::mustBeUpdated(const bool theFlag)
343 {
344   if (theFlag)
345     TDataStd_UAttribute::Set(myLab, kMustBeUpdatedGUID);
346   else
347     myLab.ForgetAttribute(kMustBeUpdatedGUID);
348 }
349
350 bool Model_Data::mustBeUpdated()
351 {
352   return myLab.IsAttribute(kMustBeUpdatedGUID) == Standard_True;
353 }
354
355 int Model_Data::featureId() const
356 {
357   return myLab.Father().Tag(); // tag of the feature label
358 }
359
360 void Model_Data::eraseBackReferences()
361 {
362   myRefsToMe.clear();
363   boost::shared_ptr<ModelAPI_Result> aRes = 
364     boost::dynamic_pointer_cast<ModelAPI_Result>(myObject);
365   if (aRes)
366     aRes->setIsConcealed(false);
367 }
368
369 void Model_Data::addBackReference(FeaturePtr theFeature, std::string theAttrID)
370 {
371   myRefsToMe.insert(theFeature->data()->attribute(theAttrID));
372   if (ModelAPI_Session::get()->validators()->isConcealed(theFeature->getKind(), theAttrID)) {
373     boost::shared_ptr<ModelAPI_Result> aRes = 
374       boost::dynamic_pointer_cast<ModelAPI_Result>(myObject);
375     if (aRes) {
376       aRes->setIsConcealed(true);
377     }
378   }
379 }
380
381 void Model_Data::referencesToObjects(
382   std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs)
383 {
384   std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = myAttrs.begin();
385   std::list<ObjectPtr> aReferenced; // not inside of cycle to avoid excess memory menagement
386   for(; anAttr != myAttrs.end(); anAttr++) {
387     std::string aType = anAttr->second->attributeType();
388     if (aType == ModelAPI_AttributeReference::type()) { // reference to object
389       boost::shared_ptr<ModelAPI_AttributeReference> aRef = boost::dynamic_pointer_cast<
390           ModelAPI_AttributeReference>(anAttr->second);
391       aReferenced.push_back(aRef->value());
392       theRefs.push_back(std::pair<std::string, std::list<ObjectPtr> >(anAttr->first, aReferenced));
393     } else if (aType == ModelAPI_AttributeRefAttr::type()) { // reference to attribute or object
394       boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef = boost::dynamic_pointer_cast<
395           ModelAPI_AttributeRefAttr>(anAttr->second);
396       aReferenced.push_back(aRef->isObject() ? aRef->object() : aRef->attr()->owner());
397     } else if (aType == ModelAPI_AttributeRefList::type()) { // list of references
398       aReferenced = boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(anAttr->second)->list();
399     } else if (aType == ModelAPI_AttributeSelection::type()) { // selection attribute
400       boost::shared_ptr<ModelAPI_AttributeSelection> aRef = boost::dynamic_pointer_cast<
401           ModelAPI_AttributeSelection>(anAttr->second);
402       aReferenced.push_back(aRef->context());
403       theRefs.push_back(std::pair<std::string, std::list<ObjectPtr> >(anAttr->first, aReferenced));
404     } else if (aType == ModelAPI_AttributeSelectionList::type()) { // list of selection attributes
405       boost::shared_ptr<ModelAPI_AttributeSelectionList> aRef = boost::dynamic_pointer_cast<
406           ModelAPI_AttributeSelectionList>(anAttr->second);
407       for(int a = aRef->size() - 1; a >= 0; a--) {
408         aReferenced.push_back(aRef->value(a)->context());
409       }
410     } else
411       continue; // nothing to do, not reference
412
413     if (!aReferenced.empty()) {
414       theRefs.push_back(std::pair<std::string, std::list<ObjectPtr> >(anAttr->first, aReferenced));
415       aReferenced.clear();
416     }
417   }
418 }