]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_Objects.cpp
Salome HOME
Compsolids: initial implementation of sub-results of results on the data model level.
[modules/shaper.git] / src / Model / Model_Objects.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_Objects.cxx
4 // Created:     15 May 2015
5 // Author:      Mikhail PONIKAROV
6
7 #include <Model_Objects.h>
8 #include <Model_Data.h>
9 #include <Model_Document.h>
10 #include <Model_Events.h>
11 #include <Model_Session.h>
12 #include <Model_ResultPart.h>
13 #include <Model_ResultConstruction.h>
14 #include <Model_ResultBody.h>
15 #include <Model_ResultCompSolid.h>
16 #include <Model_ResultGroup.h>
17 #include <Model_ResultParameter.h>
18 #include <ModelAPI_Validator.h>
19 #include <ModelAPI_CompositeFeature.h>
20 #include <ModelAPI_Tools.h>
21
22 #include <Events_Loop.h>
23 #include <Events_Error.h>
24
25 #include <TDataStd_Integer.hxx>
26 #include <TDataStd_Comment.hxx>
27 #include <TDF_ChildIDIterator.hxx>
28 #include <TDataStd_ReferenceArray.hxx>
29 #include <TDataStd_HLabelArray1.hxx>
30 #include <TDataStd_Name.hxx>
31 #include <TDF_Reference.hxx>
32 #include <TDF_ChildIDIterator.hxx>
33 #include <TDF_LabelMapHasher.hxx>
34
35 static const int TAG_OBJECTS = 2;  // tag of the objects sub-tree (features, results)
36
37 // feature sub-labels
38 static const int TAG_FEATURE_ARGUMENTS = 1;  ///< where the arguments are located
39 static const int TAG_FEATURE_RESULTS = 2;  ///< where the results are located
40
41 ///
42 /// 0:1:2 - where features are located
43 /// 0:1:2:N:1 - data of the feature N
44 /// 0:1:2:N:2:K:1 - data of the K result of the feature N
45
46 Model_Objects::Model_Objects(TDF_Label theMainLab) : myMain(theMainLab)
47 {
48 }
49
50 void Model_Objects::setOwner(DocumentPtr theDoc)
51 {
52   myDoc = theDoc;
53   // update all fields and recreate features and result objects if needed
54   synchronizeFeatures(false, true, true);
55   myHistory.clear();
56 }
57
58 Model_Objects::~Model_Objects()
59 {
60   // delete all features of this document
61   Events_Loop* aLoop = Events_Loop::loop();
62   // erase one by one to avoid access from the feature destructor itself from he map
63   while(!myFeatures.IsEmpty()) {
64     NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeaturesIter(myFeatures);
65     FeaturePtr aFeature = aFeaturesIter.Value();
66     static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
67     ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
68     ModelAPI_EventCreator::get()->sendUpdated(aFeature, EVENT_DISP);
69     aFeature->eraseResults();
70     aFeature->erase();
71     myFeatures.UnBind(aFeaturesIter.Key());
72   }
73   aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
74   aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
75
76 }
77
78 /// Appends to the array of references a new referenced label
79 static void AddToRefArray(TDF_Label& theArrayLab, TDF_Label& theReferenced, TDF_Label& thePrevLab)
80 {
81   Handle(TDataStd_ReferenceArray) aRefs;
82   if (!theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
83     aRefs = TDataStd_ReferenceArray::Set(theArrayLab, 0, 0);
84     aRefs->SetValue(0, theReferenced);
85   } else {  // extend array by one more element
86     Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
87                                                                         aRefs->Upper() + 1);
88     int aPassedPrev = 0; // prev feature is found and passed
89     if (thePrevLab.IsNull()) { // null means that inserted feature must be the first
90       aNewArray->SetValue(aRefs->Lower(), theReferenced);
91       aPassedPrev = 1;
92     }
93     for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
94       aNewArray->SetValue(a + aPassedPrev, aRefs->Value(a));
95       if (!aPassedPrev && aRefs->Value(a).IsEqual(thePrevLab)) {
96         aPassedPrev = 1;
97         aNewArray->SetValue(a + 1, theReferenced);
98       }
99     }
100     if (!aPassedPrev) // not found: unknown situation
101       aNewArray->SetValue(aRefs->Upper() + 1, theReferenced);
102     aRefs->SetInternalArray(aNewArray);
103   }
104 }
105
106 void Model_Objects::addFeature(FeaturePtr theFeature, const FeaturePtr theAfterThis)
107 {
108   if (!theFeature->isAction()) {  // do not add action to the data model
109     TDF_Label aFeaturesLab = featuresLabel();
110     TDF_Label aFeatureLab = aFeaturesLab.NewChild();
111     // store feature in the features array: before "initData" because in macro features
112     // in initData it creates new features, appeared later than this
113     TDF_Label aPrevFeateureLab;
114     if (theAfterThis.get()) { // searching for the previous feature label
115       std::shared_ptr<Model_Data> aPrevData = 
116         std::dynamic_pointer_cast<Model_Data>(theAfterThis->data());
117       if (aPrevData.get()) {
118         aPrevFeateureLab = aPrevData->label().Father();
119       }
120     }
121     AddToRefArray(aFeaturesLab, aFeatureLab, aPrevFeateureLab);
122
123     // keep the feature ID to restore document later correctly
124     TDataStd_Comment::Set(aFeatureLab, theFeature->getKind().c_str());
125     myFeatures.Bind(aFeatureLab, theFeature);
126     // must be after binding to the map because of "Box" macro feature that 
127     // creates other features in "initData"
128     initData(theFeature, aFeatureLab, TAG_FEATURE_ARGUMENTS);
129     // event: feature is added
130     static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
131     ModelAPI_EventCreator::get()->sendUpdated(theFeature, anEvent);
132     theFeature->setDisabled(false); // by default created feature is enabled
133     updateHistory(ModelAPI_Feature::group());
134   } else { // make feature has not-null data anyway
135     theFeature->setData(Model_Data::invalidData());
136     theFeature->setDoc(myDoc);
137   }
138 }
139
140 /// Appends to the array of references a new referenced label.
141 /// If theIndex is not -1, removes element at this index, not theReferenced.
142 /// \returns the index of removed element
143 static int RemoveFromRefArray(TDF_Label theArrayLab, TDF_Label theReferenced, 
144   const int theIndex = -1)
145 {
146   int aResult = -1;  // no returned
147   Handle(TDataStd_ReferenceArray) aRefs;
148   if (theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
149     if (aRefs->Length() == 1) {  // just erase an array
150       if ((theIndex == -1 && aRefs->Value(0) == theReferenced) || theIndex == 0) {
151         theArrayLab.ForgetAttribute(TDataStd_ReferenceArray::GetID());
152       }
153       aResult = 0;
154     } else {  // reduce the array
155       Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
156                                                                           aRefs->Upper() - 1);
157       int aCount = aRefs->Lower();
158       for (int a = aCount; a <= aRefs->Upper(); a++, aCount++) {
159         if ((theIndex == -1 && aRefs->Value(a) == theReferenced) || theIndex == a) {
160           aCount--;
161           aResult = a;
162         } else {
163           aNewArray->SetValue(aCount, aRefs->Value(a));
164         }
165       }
166       aRefs->SetInternalArray(aNewArray);
167     }
168   }
169   return aResult;
170 }
171
172 void Model_Objects::refsToFeature(FeaturePtr theFeature,
173   std::set<std::shared_ptr<ModelAPI_Feature> >& theRefs, const bool isSendError)
174 {
175   // check the feature: it must have no depended objects on it
176   // the dependencies can be in the feature results
177   std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
178   for(; aResIter != theFeature->results().cend(); aResIter++) {
179     ResultPtr aResult = (*aResIter);
180     std::shared_ptr<Model_Data> aData = 
181       std::dynamic_pointer_cast<Model_Data>(aResult->data());
182     if (aData.get() != NULL) {
183       const std::set<AttributePtr>& aRefs = aData->refsToMe();
184       std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
185       for(; aRefIt != aRefLast; aRefIt++) {
186         FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
187         if (aFeature.get() != NULL)
188           theRefs.insert(aFeature);
189       }
190     }
191   }
192   // the dependencies can be in the feature itself
193   std::shared_ptr<Model_Data> aData = 
194       std::dynamic_pointer_cast<Model_Data>(theFeature->data());
195   if (aData && !aData->refsToMe().empty()) {
196     const std::set<AttributePtr>& aRefs = aData->refsToMe();
197     std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin(), aRefLast = aRefs.end();
198     for(; aRefIt != aRefLast; aRefIt++) {
199       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
200       if (aFeature.get() != NULL)
201         theRefs.insert(aFeature);
202     }
203   }
204
205   if (!theRefs.empty() && isSendError) {
206     Events_Error::send(
207       "Feature '" + theFeature->data()->name() + "' is used and can not be deleted");
208   }
209 }
210
211 void Model_Objects::removeFeature(FeaturePtr theFeature)
212 {
213   std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theFeature->data());
214   if (aData && aData->isValid()) {
215     // checking that the sub-element of composite feature is removed: if yes, inform the owner
216     std::set<std::shared_ptr<ModelAPI_Feature> > aRefs;
217     refsToFeature(theFeature, aRefs, false);
218     std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aRefIter = aRefs.begin();
219     for(; aRefIter != aRefs.end(); aRefIter++) {
220       std::shared_ptr<ModelAPI_CompositeFeature> aComposite = 
221         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aRefIter);
222       if (aComposite.get()) {
223         aComposite->removeFeature(theFeature);
224       }
225     }
226     // this must be before erase since theFeature erasing removes all information about
227     // the feature results and groups of results
228     // To reproduce: create sketch, extrusion, remove sketch => constructions tree is not updated
229     clearHistory(theFeature);
230     // erase fields
231     theFeature->erase();
232
233     TDF_Label aFeatureLabel = aData->label().Father();
234     if (myFeatures.IsBound(aFeatureLabel))
235       myFeatures.UnBind(aFeatureLabel);
236
237     static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
238     ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
239     // erase all attributes under the label of feature
240     aFeatureLabel.ForgetAllAttributes();
241     // remove it from the references array
242     RemoveFromRefArray(featuresLabel(), aFeatureLabel);
243     // event: feature is deleted
244     ModelAPI_EventCreator::get()->sendDeleted(theFeature->document(), ModelAPI_Feature::group());
245     // the redisplay signal should be flushed in order to erase the feature presentation in the viewer
246     Events_Loop::loop()->flush(EVENT_DISP);
247     updateHistory(ModelAPI_Feature::group());
248   }
249 }
250
251 void Model_Objects::moveFeature(FeaturePtr theMoved, FeaturePtr theAfterThis)
252 {
253   TDF_Label aFeaturesLab = featuresLabel();
254   Handle(TDataStd_ReferenceArray) aRefs;
255   if (!aFeaturesLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
256     return;
257   TDF_Label anAfterLab, aMovedLab = 
258     std::dynamic_pointer_cast<Model_Data>(theMoved->data())->label().Father();
259   if (theAfterThis.get())
260     anAfterLab = std::dynamic_pointer_cast<Model_Data>(theAfterThis->data())->label().Father();
261
262   Handle(TDataStd_HLabelArray1) aNewArray = 
263     new TDataStd_HLabelArray1(aRefs->Lower(), aRefs->Upper());
264   int aPassedMovedFrom = 0; // the prev feature location is found and passed
265   int aPassedMovedTo = 0; // the feature is added and this location is passed
266   if (!theAfterThis.get()) { // null means that inserted feature must be the first
267     aNewArray->SetValue(aRefs->Lower(), aMovedLab);
268     aPassedMovedTo = 1;
269   }
270   for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
271     if (aPassedMovedTo == 0 && aRefs->Value(a) == anAfterLab) { // add two
272       aPassedMovedTo++;
273       aNewArray->SetValue(a - aPassedMovedFrom, anAfterLab);
274       if (a + 1 - aPassedMovedFrom <= aRefs->Upper())
275         aNewArray->SetValue(a + 1 - aPassedMovedFrom, aMovedLab);
276     } else if (aPassedMovedFrom == 0 && aRefs->Value(a) == aMovedLab) { // skip
277       aPassedMovedFrom++;
278     } else { // just copy one
279       if (a - aPassedMovedFrom + aPassedMovedTo <= aRefs->Upper())
280         aNewArray->SetValue(a - aPassedMovedFrom + aPassedMovedTo, aRefs->Value(a));
281     }
282   }
283   if (!aPassedMovedFrom || !aPassedMovedTo) {// not found: unknown situation
284     if (!aPassedMovedFrom) {
285       static std::string aMovedFromError("The moved feature is not found");
286       Events_Error::send(aMovedFromError);
287     } else {
288       static std::string aMovedToError("The 'after' feature for movement is not found");
289       Events_Error::send(aMovedToError);
290     }
291     return;
292   }
293   // store the new array
294   aRefs->SetInternalArray(aNewArray);
295   // update the feature and the history
296   clearHistory(theMoved);
297   static Events_ID EVENT_UPD = Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED);
298   ModelAPI_EventCreator::get()->sendUpdated(theMoved, EVENT_UPD);
299 }
300
301 void Model_Objects::clearHistory(ObjectPtr theObj)
302 {
303   if (theObj) {
304     const std::string aGroup = theObj->groupName();
305     std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(aGroup);
306     if (aHIter != myHistory.end())
307       myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
308     if (theObj->groupName() == ModelAPI_Feature::group()) { // clear results group of the feature
309       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
310       std::string aResultGroup = featureResultGroup(aFeature);
311       if (!aResultGroup.empty()) {
312         std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = 
313           myHistory.find(aResultGroup);
314         if (aHIter != myHistory.end())
315           myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
316       }
317     }
318   }
319 }
320
321 void Model_Objects::createHistory(const std::string& theGroupID)
322 {
323   std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroupID);
324   if (aHIter == myHistory.end()) {
325     myHistory[theGroupID] = std::vector<ObjectPtr>();
326     std::vector<ObjectPtr>& aResult = myHistory[theGroupID];
327     // iterate the array of references and get feature by feature from the array
328     bool isFeature = theGroupID == ModelAPI_Feature::group();
329     Handle(TDataStd_ReferenceArray) aRefs;
330     if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
331       for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
332         FeaturePtr aFeature = feature(aRefs->Value(a));
333         if (aFeature.get()) {
334           // if feature is in sub-component, remove it from history: it is in sub-tree of sub-component
335           if (!ModelAPI_Tools::compositeOwner(aFeature).get()) {
336             if (isFeature) { // here may be also disabled features
337               if (aFeature->isInHistory()) {
338                 aResult.push_back(aFeature);
339               }
340             } else if (!aFeature->isDisabled()) { // iterate all results of not-disabled feature
341               const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
342               std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
343               for (; aRIter != aResults.cend(); aRIter++) {
344                 ResultPtr aRes = *aRIter;
345                 if (aRes->groupName() != theGroupID) break; // feature have only same group results
346                 if (!aRes->isDisabled() && aRes->isInHistory() && !aRes->isConcealed()) {
347                   aResult.push_back(*aRIter);
348                 }
349               }
350             }
351           }
352         }
353       }
354     }
355   }
356 }
357
358 void Model_Objects::updateHistory(const std::shared_ptr<ModelAPI_Object> theObject)
359 {
360   clearHistory(theObject);
361 }
362
363 void Model_Objects::updateHistory(const std::string theGroup)
364 {
365   std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = myHistory.find(theGroup);
366   if (aHIter != myHistory.end())
367     myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
368 }
369
370 FeaturePtr Model_Objects::feature(TDF_Label theLabel) const
371 {
372   if (myFeatures.IsBound(theLabel))
373     return myFeatures.Find(theLabel);
374   return FeaturePtr();  // not found
375 }
376
377 ObjectPtr Model_Objects::object(TDF_Label theLabel)
378 {
379   // try feature by label
380   FeaturePtr aFeature = feature(theLabel);
381   if (aFeature)
382     return feature(theLabel);
383   TDF_Label aFeatureLabel = theLabel.Father().Father();  // let's suppose it is result
384   aFeature = feature(aFeatureLabel);
385   bool isSubResult = false;
386   if (!aFeature.get() && aFeatureLabel.Depth() > 1) { // let's suppose this is sub-result of result
387     aFeatureLabel = aFeatureLabel.Father().Father();
388     aFeature = feature(aFeatureLabel);
389     isSubResult = true;
390   }
391   if (aFeature) {
392     const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
393     std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
394     for (; aRIter != aResults.cend(); aRIter++) {
395       if (isSubResult) {
396         ResultCompSolidPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aRIter);
397         if (aCompRes) {
398           int aNumSubs = aCompRes->numberOfSubs();
399           for(int a = 0; a < aNumSubs; a++) {
400             ResultPtr aSub = aCompRes->subResult(a);
401             if (aSub.get()) {
402               std::shared_ptr<Model_Data> aSubData = std::dynamic_pointer_cast<Model_Data>(
403                   aSub->data());
404               if (aSubData->label().Father().IsEqual(theLabel))
405                 return aSub;
406             }
407           }
408         }
409       } else {
410         std::shared_ptr<Model_Data> aResData = std::dynamic_pointer_cast<Model_Data>(
411             (*aRIter)->data());
412         if (aResData->label().Father().IsEqual(theLabel))
413           return *aRIter;
414       }
415     }
416   }
417   return FeaturePtr();  // not found
418 }
419
420 ObjectPtr Model_Objects::object(const std::string& theGroupID, const int theIndex)
421 {
422   if (theIndex == -1)
423     return ObjectPtr();
424   createHistory(theGroupID);
425   return myHistory[theGroupID][theIndex];
426 }
427
428 std::shared_ptr<ModelAPI_Object> Model_Objects::objectByName(
429     const std::string& theGroupID, const std::string& theName)
430 {
431   createHistory(theGroupID);
432   std::vector<ObjectPtr>& allObjs = myHistory[theGroupID];
433   std::vector<ObjectPtr>::iterator anObjIter = allObjs.begin();
434   for(; anObjIter != allObjs.end(); anObjIter++) {
435     if ((*anObjIter)->data()->name() == theName)
436       return *anObjIter;
437   }
438   // not found
439   return ObjectPtr();
440 }
441
442 const int Model_Objects::index(std::shared_ptr<ModelAPI_Object> theObject)
443 {
444   std::string aGroup = theObject->groupName();
445   createHistory(aGroup);
446   std::vector<ObjectPtr>& allObjs = myHistory[aGroup];
447   std::vector<ObjectPtr>::iterator anObjIter = allObjs.begin(); // iterate to search object
448   for(int anIndex = 0; anObjIter != allObjs.end(); anObjIter++, anIndex++) {
449     if ((*anObjIter) == theObject)
450       return anIndex;
451   }
452   // not found
453   return -1;
454 }
455
456 int Model_Objects::size(const std::string& theGroupID)
457 {
458   createHistory(theGroupID);
459   return myHistory[theGroupID].size();
460 }
461
462 void Model_Objects::allResults(const std::string& theGroupID, std::list<ResultPtr>& theResults)
463 {
464   // iterate the array of references and get feature by feature from the array
465   Handle(TDataStd_ReferenceArray) aRefs;
466   if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
467     for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
468       FeaturePtr aFeature = feature(aRefs->Value(a));
469       if (aFeature.get()) {
470         const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
471         std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
472         for (; aRIter != aResults.cend(); aRIter++) {
473           ResultPtr aRes = *aRIter;
474           if (aRes->groupName() != theGroupID) break; // feature have only same group results
475           if (aRes->isInHistory() && !aRes->isConcealed()) {
476             theResults.push_back(*aRIter);
477           }
478         }
479       }
480     }
481   }
482 }
483
484
485 TDF_Label Model_Objects::featuresLabel() const
486 {
487   return myMain.FindChild(TAG_OBJECTS);
488 }
489
490 void Model_Objects::setUniqueName(FeaturePtr theFeature)
491 {
492   if (!theFeature->data()->name().empty())
493     return;  // not needed, name is already defined
494   std::string aName;  // result
495   // first count all features of such kind to start with index = count + 1
496   int aNumObjects = -1; // this feature is already in this map
497   NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
498   for (; aFIter.More(); aFIter.Next()) {
499     if (aFIter.Value()->getKind() == theFeature->getKind())
500       aNumObjects++;
501   }
502   // generate candidate name
503   std::stringstream aNameStream;
504   aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
505   aName = aNameStream.str();
506   // check this is unique, if not, increase index by 1
507   for (aFIter.Initialize(myFeatures); aFIter.More();) {
508     FeaturePtr aFeature = aFIter.Value();
509     bool isSameName = aFeature->data()->name() == aName;
510     if (!isSameName) {  // check also results to avoid same results names (actual for Parts)
511       const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
512       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
513       for (; aRIter != aResults.cend(); aRIter++) {
514         isSameName = (*aRIter)->data()->name() == aName;
515       }
516     }
517     if (isSameName) {
518       aNumObjects++;
519       std::stringstream aNameStream;
520       aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
521       aName = aNameStream.str();
522       // reinitialize iterator to make sure a new name is unique
523       aFIter.Initialize(myFeatures);
524     } else
525       aFIter.Next();
526   }
527   theFeature->data()->setName(aName);
528 }
529
530 void Model_Objects::initData(ObjectPtr theObj, TDF_Label theLab, const int theTag)
531 {
532   std::shared_ptr<Model_Data> aData(new Model_Data);
533   aData->setLabel(theLab.FindChild(theTag));
534   aData->setObject(theObj);
535   theObj->setDoc(myDoc);
536   theObj->setData(aData);
537   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
538   if (aFeature) {
539     setUniqueName(aFeature);  // must be before "initAttributes" because duplicate part uses name
540   }
541   theObj->initAttributes();
542 }
543
544 void Model_Objects::synchronizeFeatures(
545   const bool theMarkUpdated, const bool theUpdateReferences, const bool theFlush)
546 {
547   Model_Document* anOwner = std::dynamic_pointer_cast<Model_Document>(myDoc).get();
548   if (!anOwner) // this may happen on creation of document: nothing there, so nothing to synchronize
549     return;
550   // after all updates, sends a message that groups of features were created or updated
551   Events_Loop* aLoop = Events_Loop::loop();
552   static Events_ID aDispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
553   static Events_ID aCreateEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
554   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
555   static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
556   static Events_ID aDeleteEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
557   static Events_ID aToHideEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
558   bool isActive = aLoop->activateFlushes(false);
559
560   // update all objects by checking are they on labels or not
561   std::set<FeaturePtr> aNewFeatures, aKeptFeatures;
562   TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
563   for (; aLabIter.More(); aLabIter.Next()) {
564     TDF_Label aFeatureLabel = aLabIter.Value()->Label();
565     FeaturePtr aFeature;
566     if (!myFeatures.IsBound(aFeatureLabel)) {  // a new feature is inserted
567       // create a feature
568       aFeature = std::dynamic_pointer_cast<Model_Session>(ModelAPI_Session::get())->createFeature(
569         TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(aLabIter.Value())->Get())
570         .ToCString(), anOwner);
571       if (!aFeature) {  // somethig is wrong, most probably, the opened document has invalid structure
572         Events_Error::send("Invalid type of object in the document");
573         aLabIter.Value()->Label().ForgetAllAttributes();
574         continue;
575       }
576       // this must be before "setData" to redo the sketch line correctly
577       myFeatures.Bind(aFeatureLabel, aFeature);
578       aNewFeatures.insert(aFeature);
579       initData(aFeature, aFeatureLabel, TAG_FEATURE_ARGUMENTS);
580       updateHistory(aFeature);
581       aFeature->setDisabled(false); // by default created feature is enabled (this allows to recreate the results before "setCurrent" is called)
582
583       // event: model is updated
584       ModelAPI_EventCreator::get()->sendUpdated(aFeature, aCreateEvent);
585     } else {  // nothing is changed, both iterators are incremented
586       aFeature = myFeatures.Find(aFeatureLabel);
587       aKeptFeatures.insert(aFeature);
588       if (theMarkUpdated) {
589         ModelAPI_EventCreator::get()->sendUpdated(aFeature, anUpdateEvent);
590       }
591     }
592   }
593
594   // check all features are checked: if not => it was removed
595   NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
596   while (aFIter.More()) {
597     if (aKeptFeatures.find(aFIter.Value()) == aKeptFeatures.end()
598       && aNewFeatures.find(aFIter.Value()) == aNewFeatures.end()) {
599         FeaturePtr aFeature = aFIter.Value();
600         // event: model is updated
601         //if (aFeature->isInHistory()) {
602         ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
603         //}
604         // results of this feature must be redisplayed (hided)
605         // redisplay also removed feature (used for sketch and AISObject)
606         ModelAPI_EventCreator::get()->sendUpdated(aFeature, aRedispEvent);
607         updateHistory(aFeature);
608         aFeature->erase();
609         // unbind after the "erase" call: on abort sketch is removes sub-objects that corrupts aFIter
610         myFeatures.UnBind(aFIter.Key());
611         // reinitialize iterator because unbind may corrupt the previous order in the map
612         aFIter.Initialize(myFeatures);
613     } else
614       aFIter.Next();
615   }
616
617   if (theUpdateReferences) {
618     synchronizeBackRefs();
619   }
620   // update results of the features (after features created because they may be connected, like sketch and sub elements)
621   // After synchronisation of back references because sketch must be set in sub-elements before "execute" by updateResults
622   std::list<FeaturePtr> aComposites; // composites must be updated after their subs (issue 360)
623   TDF_ChildIDIterator aLabIter2(featuresLabel(), TDataStd_Comment::GetID());
624   for (; aLabIter2.More(); aLabIter2.Next()) {
625     TDF_Label aFeatureLabel = aLabIter2.Value()->Label();
626     if (myFeatures.IsBound(aFeatureLabel)) {  // a new feature is inserted
627       FeaturePtr aFeature = myFeatures.Find(aFeatureLabel);
628       if (std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature).get())
629         aComposites.push_back(aFeature);
630       updateResults(aFeature);
631     }
632   }
633   std::list<FeaturePtr>::iterator aComposite = aComposites.begin();
634   for(; aComposite != aComposites.end(); aComposite++) {
635     updateResults(*aComposite);
636   }
637
638   // the synchronize should be done after updateResults in order to correct back references of updated results
639   if (theUpdateReferences) {
640     synchronizeBackRefs();
641   }
642   if (theMarkUpdated) { // this means there is no control what was modified => remove history cash
643     myHistory.clear();
644   }
645
646   anOwner->executeFeatures() = false;
647   aLoop->activateFlushes(isActive);
648
649   if (theFlush) {
650     aLoop->flush(aCreateEvent);
651     aLoop->flush(aDeleteEvent);
652     aLoop->flush(anUpdateEvent);
653     aLoop->flush(aRedispEvent);
654     aLoop->flush(aToHideEvent);
655   }
656   anOwner->executeFeatures() = true;
657 }
658
659 void Model_Objects::synchronizeBackRefs()
660 {
661   // keeps the concealed flags of result to catch the change and create created/deleted events
662   std::list<std::pair<ResultPtr, bool> > aConcealed;
663   // first cycle: erase all data about back-references
664   NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeatures(myFeatures);
665   for(; aFeatures.More(); aFeatures.Next()) {
666     FeaturePtr aFeature = aFeatures.Value();
667     std::shared_ptr<Model_Data> aFData = 
668       std::dynamic_pointer_cast<Model_Data>(aFeature->data());
669     if (aFData) {
670       aFData->eraseBackReferences();
671     }
672     const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
673     std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
674     for (; aRIter != aResults.cend(); aRIter++) {
675       std::shared_ptr<Model_Data> aResData = 
676         std::dynamic_pointer_cast<Model_Data>((*aRIter)->data());
677       if (aResData) {
678         aConcealed.push_back(std::pair<ResultPtr, bool>(*aRIter, (*aRIter)->isConcealed()));
679         aResData->eraseBackReferences();
680       }
681     }
682   }
683
684   // second cycle: set new back-references: only features may have reference, iterate only them
685   ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
686   for(aFeatures.Initialize(myFeatures); aFeatures.More(); aFeatures.Next()) {
687     FeaturePtr aFeature = aFeatures.Value();
688     std::shared_ptr<Model_Data> aFData = 
689       std::dynamic_pointer_cast<Model_Data>(aFeature->data());
690     if (aFData) {
691       std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
692       aFData->referencesToObjects(aRefs);
693       std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator 
694         aRefsIter = aRefs.begin();
695       for(; aRefsIter != aRefs.end(); aRefsIter++) {
696         std::list<ObjectPtr>::iterator aRefTo = aRefsIter->second.begin();
697         for(; aRefTo != aRefsIter->second.end(); aRefTo++) {
698           if (*aRefTo) {
699             std::shared_ptr<Model_Data> aRefData = 
700               std::dynamic_pointer_cast<Model_Data>((*aRefTo)->data());
701             aRefData->addBackReference(aFeature, aRefsIter->first); // here the Concealed flag is updated
702             // update enable/disable status: the nested status must be equal to the composite
703             CompositeFeaturePtr aComp = 
704               std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
705             if (aComp.get()) {
706               FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>(*aRefTo);
707               if (aReferenced.get()) {
708                 aReferenced->setDisabled(aComp->isDisabled());
709               }
710             }
711           }
712         }
713       }
714     }
715   }
716   std::list<std::pair<ResultPtr, bool> >::iterator aCIter = aConcealed.begin();
717   for(; aCIter != aConcealed.end(); aCIter++) {
718     if (aCIter->first->isConcealed() != aCIter->second) { // somethign is changed => produce event
719       if (aCIter->second) { // was concealed become not => creation event
720         static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
721         ModelAPI_EventCreator::get()->sendUpdated(aCIter->first, anEvent);
722       } else { // was not concealed become concealed => delete event
723         ModelAPI_EventCreator::get()->sendDeleted(myDoc, aCIter->first->groupName());
724         // redisplay for the viewer (it must be disappeared also)
725         static Events_ID EVENT_DISP = 
726           Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
727         ModelAPI_EventCreator::get()->sendUpdated(aCIter->first, EVENT_DISP);
728       }
729     }
730   }
731 }
732
733 TDF_Label Model_Objects::resultLabel(
734   const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex) 
735 {
736   const std::shared_ptr<Model_Data>& aData = 
737     std::dynamic_pointer_cast<Model_Data>(theFeatureData);
738   return aData->label().Father().FindChild(TAG_FEATURE_RESULTS).FindChild(theResultIndex + 1);
739 }
740
741 void Model_Objects::storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
742                                  std::shared_ptr<ModelAPI_Result> theResult,
743                                  const int theResultIndex)
744 {
745   theResult->setDoc(myDoc);
746   initData(theResult, resultLabel(theFeatureData, theResultIndex), TAG_FEATURE_ARGUMENTS);
747   if (theResult->data()->name().empty()) {  // if was not initialized, generate event and set a name
748     std::stringstream aNewName;
749     aNewName<<theFeatureData->name();
750     if (theResultIndex > 0) // if there are several results, add unique prefix starting from second
751       aNewName<<"_"<<theResultIndex + 1;
752     theResult->data()->setName(aNewName.str());
753   }
754 }
755
756 std::shared_ptr<ModelAPI_ResultConstruction> Model_Objects::createConstruction(
757     const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
758 {
759   TDF_Label aLab = resultLabel(theFeatureData, theIndex);
760   TDataStd_Comment::Set(aLab, ModelAPI_ResultConstruction::group().c_str());
761   ObjectPtr anOldObject = object(aLab);
762   std::shared_ptr<ModelAPI_ResultConstruction> aResult;
763   if (anOldObject) {
764     aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anOldObject);
765   }
766   if (!aResult) {
767     aResult = std::shared_ptr<ModelAPI_ResultConstruction>(new Model_ResultConstruction);
768     storeResult(theFeatureData, aResult, theIndex);
769   }
770   return aResult;
771 }
772
773 std::shared_ptr<ModelAPI_ResultBody> Model_Objects::createBody(
774     const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
775 {
776   TDF_Label aLab = resultLabel(theFeatureData, theIndex);
777   // for feature create compsolid, but for result sub create body: 
778   // only one level of recursion is supported now
779   ResultPtr aResultOwner = std::dynamic_pointer_cast<ModelAPI_Result>(theFeatureData->owner());
780   ObjectPtr anOldObject;
781   if (aResultOwner.get()) {
782     TDataStd_Comment::Set(aLab, ModelAPI_ResultBody::group().c_str());
783   } else { // in compsolid (higher level result) old object probably may be found
784     TDataStd_Comment::Set(aLab, ModelAPI_ResultCompSolid::group().c_str());
785     anOldObject = object(aLab);
786   }
787   std::shared_ptr<ModelAPI_ResultBody> aResult;
788   if (anOldObject) {
789     aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(anOldObject);
790   }
791   if (!aResult) {
792     // create compsolid anyway; if it is compsolid, it will create sub-bodies internally
793     if (aResultOwner.get()) {
794       aResult = std::shared_ptr<ModelAPI_ResultBody>(new Model_ResultBody);
795     } else {
796       aResult = std::shared_ptr<ModelAPI_ResultBody>(new Model_ResultCompSolid);
797     }
798     storeResult(theFeatureData, aResult, theIndex);
799   }
800   return aResult;
801 }
802
803 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::createPart(
804     const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
805 {
806   TDF_Label aLab = resultLabel(theFeatureData, theIndex);
807   TDataStd_Comment::Set(aLab, ModelAPI_ResultPart::group().c_str());
808   ObjectPtr anOldObject = object(aLab);
809   std::shared_ptr<ModelAPI_ResultPart> aResult;
810   if (anOldObject) {
811     aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(anOldObject);
812   }
813   if (!aResult) {
814     aResult = std::shared_ptr<ModelAPI_ResultPart>(new Model_ResultPart);
815     storeResult(theFeatureData, aResult, theIndex);
816   }
817   return aResult;
818 }
819
820 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::copyPart(
821     const std::shared_ptr<ModelAPI_ResultPart>& theOrigin,
822     const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
823 {
824   std::shared_ptr<ModelAPI_ResultPart> aResult = createPart(theFeatureData, theIndex);
825   aResult->data()->reference(Model_ResultPart::BASE_REF_ID())->setValue(theOrigin);
826   return aResult;
827 }
828
829 std::shared_ptr<ModelAPI_ResultGroup> Model_Objects::createGroup(
830     const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
831 {
832   TDF_Label aLab = resultLabel(theFeatureData, theIndex);
833   TDataStd_Comment::Set(aLab, ModelAPI_ResultGroup::group().c_str());
834   ObjectPtr anOldObject = object(aLab);
835   std::shared_ptr<ModelAPI_ResultGroup> aResult;
836   if (anOldObject) {
837     aResult = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(anOldObject);
838   }
839   if (!aResult) {
840     aResult = std::shared_ptr<ModelAPI_ResultGroup>(new Model_ResultGroup(theFeatureData));
841     storeResult(theFeatureData, aResult, theIndex);
842   }
843   return aResult;
844 }
845
846 std::shared_ptr<ModelAPI_ResultParameter> Model_Objects::createParameter(
847       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
848 {
849   TDF_Label aLab = resultLabel(theFeatureData, theIndex);
850   TDataStd_Comment::Set(aLab, ModelAPI_ResultParameter::group().c_str());
851   ObjectPtr anOldObject = object(aLab);
852   std::shared_ptr<ModelAPI_ResultParameter> aResult;
853   if (anOldObject) {
854     aResult = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(anOldObject);
855   }
856   if (!aResult) {
857     aResult = std::shared_ptr<ModelAPI_ResultParameter>(new Model_ResultParameter);
858     storeResult(theFeatureData, aResult, theIndex);
859   }
860   return aResult;
861 }
862
863 std::shared_ptr<ModelAPI_Feature> Model_Objects::feature(
864     const std::shared_ptr<ModelAPI_Result>& theResult)
865 {
866   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theResult->data());
867   if (aData) {
868     TDF_Label aFeatureLab = aData->label().Father().Father().Father();
869     FeaturePtr aFeature = feature(aFeatureLab);
870     if (!aFeature.get() && aFeatureLab.Depth() > 1) { // this may be sub-result of result
871       aFeatureLab = aFeatureLab.Father().Father();
872       aFeature = feature(aFeatureLab);
873     }
874     return aFeature;
875   }
876   return FeaturePtr();
877 }
878
879 std::string Model_Objects::featureResultGroup(FeaturePtr theFeature)
880 {
881   if (theFeature->data()->isValid()) {
882     TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
883     if (aLabIter.More()) {
884       TDF_Label anArgLab = aLabIter.Value();
885       Handle(TDataStd_Comment) aGroup;
886       if (aLabIter.Value().FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
887         return TCollection_AsciiString(aGroup->Get()).ToCString();
888       }
889     }
890   }
891   static std::string anEmpty;
892   return anEmpty; // not found
893 }
894
895 void Model_Objects::updateResults(FeaturePtr theFeature)
896 {
897   // for not persistent is will be done by parametric updater automatically
898   //if (!theFeature->isPersistentResult()) return;
899   // check the existing results and remove them if there is nothing on the label
900   std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
901   while(aResIter != theFeature->results().cend()) {
902     ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(*aResIter);
903     if (aBody.get()) {
904       if (!aBody->data()->isValid()) { 
905         // found a disappeared result => remove it
906         theFeature->eraseResultFromList(aBody);
907         // start iterate from beginning because iterator is corrupted by removing
908         aResIter = theFeature->results().cbegin();
909         continue;
910       }
911     }
912     aResIter++;
913   }
914   // it may be on undo
915   if (!theFeature->data() || !theFeature->data()->isValid() || theFeature->isDisabled())
916     return;
917   // check that results are presented on all labels
918   int aResSize = theFeature->results().size();
919   TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
920   for(; aLabIter.More(); aLabIter.Next()) {
921     // here must be GUID of the feature
922     int aResIndex = aLabIter.Value().Tag() - 1;
923     ResultPtr aNewBody;
924     if (aResSize <= aResIndex) {
925       TDF_Label anArgLab = aLabIter.Value();
926       Handle(TDataStd_Comment) aGroup;
927       if (anArgLab.FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
928         if (aGroup->Get() == ModelAPI_ResultBody::group().c_str() || 
929             aGroup->Get() == ModelAPI_ResultCompSolid::group().c_str()) {
930           aNewBody = createBody(theFeature->data(), aResIndex);
931         } else if (aGroup->Get() == ModelAPI_ResultPart::group().c_str()) {
932           std::shared_ptr<ModelAPI_ResultPart> aNewP = createPart(theFeature->data(), aResIndex); 
933           theFeature->setResult(aNewP, aResIndex);
934           if (!aNewP->partDoc().get())
935             theFeature->execute(); // create the part result: it is better to restore the previous result if it is possible
936           break;
937         } else if (aGroup->Get() == ModelAPI_ResultConstruction::group().c_str()) {
938           theFeature->execute(); // construction shapes are needed for sketch solver
939           break;
940         } else if (aGroup->Get() == ModelAPI_ResultGroup::group().c_str()) {
941           aNewBody = createGroup(theFeature->data(), aResIndex);
942         } else if (aGroup->Get() == ModelAPI_ResultParameter::group().c_str()) {
943           theFeature->attributeChanged("expression"); // just produce a value
944           break;
945         } else {
946           Events_Error::send(std::string("Unknown type of result is found in the document:") +
947             TCollection_AsciiString(aGroup->Get()).ToCString());
948         }
949       }
950       if (aNewBody) {
951         theFeature->setResult(aNewBody, aResIndex);
952       }
953     }
954   }
955 }
956
957 ResultPtr Model_Objects::findByName(const std::string theName)
958 {
959   NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator anObjIter(myFeatures);
960   for(; anObjIter.More(); anObjIter.Next()) {
961     FeaturePtr& aFeature = anObjIter.ChangeValue();
962     if (!aFeature.get() || aFeature->isDisabled()) // may be on close
963       continue;
964     const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
965     std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
966     for (; aRIter != aResults.cend(); aRIter++) {
967       ResultPtr aRes = *aRIter;
968       if (aRes.get() && aRes->data() && aRes->data()->isValid() && !aRes->isDisabled() &&
969           aRes->data()->name() == theName) {
970         return aRes;
971       }
972     }
973   }
974   // not found
975   return ResultPtr();
976 }
977
978 FeaturePtr Model_Objects::nextFeature(FeaturePtr theCurrent, const bool theReverse)
979 {
980   std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theCurrent->data());
981   if (aData && aData->isValid()) {
982     TDF_Label aFeatureLabel = aData->label().Father();
983     Handle(TDataStd_ReferenceArray) aRefs;
984     if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
985       for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) { // iterate all existing features
986         if (aRefs->Value(a).IsEqual(aFeatureLabel)) {
987           a += theReverse ? -1 : 1;
988           if (a >= aRefs->Lower() && a <= aRefs->Upper())
989             return feature(aRefs->Value(a));
990           break; // finish iiteration: it's last feature
991         }
992       }
993     }
994   }
995   return FeaturePtr(); // not found, last, or something is wrong
996 }
997
998 FeaturePtr Model_Objects::firstFeature()
999 {
1000   Handle(TDataStd_ReferenceArray) aRefs;
1001   if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1002     return feature(aRefs->Value(aRefs->Lower()));
1003   }
1004   return FeaturePtr(); // no features at all
1005 }
1006
1007 FeaturePtr Model_Objects::lastFeature()
1008 {
1009   Handle(TDataStd_ReferenceArray) aRefs;
1010   if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1011     return feature(aRefs->Value(aRefs->Upper()));
1012   }
1013   return FeaturePtr(); // no features at all
1014 }
1015
1016 std::list<std::shared_ptr<ModelAPI_Feature> > Model_Objects::allFeatures()
1017 {
1018   std::list<std::shared_ptr<ModelAPI_Feature> > aResult;
1019   Handle(TDataStd_ReferenceArray) aRefs;
1020   if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1021     for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
1022       FeaturePtr aFeature = feature(aRefs->Value(a));
1023       if (aFeature.get())
1024         aResult.push_back(aFeature);
1025     }
1026   }
1027   return aResult;
1028 }
1029
1030 int Model_Objects::numInternalFeatures()
1031 {
1032   Handle(TDataStd_ReferenceArray) aRefs;
1033   if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1034     return aRefs->Upper() - aRefs->Lower() + 1;
1035   }
1036   return 0; // invalid
1037 }
1038
1039 std::shared_ptr<ModelAPI_Feature> Model_Objects::internalFeature(const int theIndex)
1040 {
1041   Handle(TDataStd_ReferenceArray) aRefs;
1042   if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1043     return feature(aRefs->Value(aRefs->Lower() + theIndex));
1044   }
1045   return FeaturePtr(); // invalid
1046 }
1047
1048 Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper)
1049 {
1050   return TDF_LabelMapHasher::HashCode(theLab, theUpper);
1051
1052 }
1053 Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2)
1054 {
1055   return TDF_LabelMapHasher::IsEqual(theLab1, theLab2);
1056 }