]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_Objects.cpp
Salome HOME
Fix for the TestParameterRename.py crash sometimes on Linux: map removes shared point...
[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   if (aFeature) {
386     const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
387     std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
388     for (; aRIter != aResults.cend(); aRIter++) {
389       std::shared_ptr<Model_Data> aResData = std::dynamic_pointer_cast<Model_Data>(
390           (*aRIter)->data());
391       if (aResData->label().Father().IsEqual(theLabel))
392         return *aRIter;
393     }
394   }
395   return FeaturePtr();  // not found
396 }
397
398 ObjectPtr Model_Objects::object(const std::string& theGroupID, const int theIndex)
399 {
400   if (theIndex == -1)
401     return ObjectPtr();
402   createHistory(theGroupID);
403   return myHistory[theGroupID][theIndex];
404 }
405
406 std::shared_ptr<ModelAPI_Object> Model_Objects::objectByName(
407     const std::string& theGroupID, const std::string& theName)
408 {
409   createHistory(theGroupID);
410   std::vector<ObjectPtr>& allObjs = myHistory[theGroupID];
411   std::vector<ObjectPtr>::iterator anObjIter = allObjs.begin();
412   for(; anObjIter != allObjs.end(); anObjIter++) {
413     if ((*anObjIter)->data()->name() == theName)
414       return *anObjIter;
415   }
416   // not found
417   return ObjectPtr();
418 }
419
420 const int Model_Objects::index(std::shared_ptr<ModelAPI_Object> theObject)
421 {
422   std::string aGroup = theObject->groupName();
423   createHistory(aGroup);
424   std::vector<ObjectPtr>& allObjs = myHistory[aGroup];
425   std::vector<ObjectPtr>::iterator anObjIter = allObjs.begin(); // iterate to search object
426   for(int anIndex = 0; anObjIter != allObjs.end(); anObjIter++, anIndex++) {
427     if ((*anObjIter) == theObject)
428       return anIndex;
429   }
430   // not found
431   return -1;
432 }
433
434 int Model_Objects::size(const std::string& theGroupID)
435 {
436   createHistory(theGroupID);
437   return myHistory[theGroupID].size();
438 }
439
440 void Model_Objects::allResults(const std::string& theGroupID, std::list<ResultPtr>& theResults)
441 {
442   // iterate the array of references and get feature by feature from the array
443   Handle(TDataStd_ReferenceArray) aRefs;
444   if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
445     for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
446       FeaturePtr aFeature = feature(aRefs->Value(a));
447       if (aFeature.get()) {
448         const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
449         std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
450         for (; aRIter != aResults.cend(); aRIter++) {
451           ResultPtr aRes = *aRIter;
452           if (aRes->groupName() != theGroupID) break; // feature have only same group results
453           if (aRes->isInHistory() && !aRes->isConcealed()) {
454             theResults.push_back(*aRIter);
455           }
456         }
457       }
458     }
459   }
460 }
461
462
463 TDF_Label Model_Objects::featuresLabel() const
464 {
465   return myMain.FindChild(TAG_OBJECTS);
466 }
467
468 void Model_Objects::setUniqueName(FeaturePtr theFeature)
469 {
470   if (!theFeature->data()->name().empty())
471     return;  // not needed, name is already defined
472   std::string aName;  // result
473   // first count all features of such kind to start with index = count + 1
474   int aNumObjects = -1; // this feature is already in this map
475   NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
476   for (; aFIter.More(); aFIter.Next()) {
477     if (aFIter.Value()->getKind() == theFeature->getKind())
478       aNumObjects++;
479   }
480   // generate candidate name
481   std::stringstream aNameStream;
482   aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
483   aName = aNameStream.str();
484   // check this is unique, if not, increase index by 1
485   for (aFIter.Initialize(myFeatures); aFIter.More();) {
486     FeaturePtr aFeature = aFIter.Value();
487     bool isSameName = aFeature->data()->name() == aName;
488     if (!isSameName) {  // check also results to avoid same results names (actual for Parts)
489       const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
490       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
491       for (; aRIter != aResults.cend(); aRIter++) {
492         isSameName = (*aRIter)->data()->name() == aName;
493       }
494     }
495     if (isSameName) {
496       aNumObjects++;
497       std::stringstream aNameStream;
498       aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
499       aName = aNameStream.str();
500       // reinitialize iterator to make sure a new name is unique
501       aFIter.Initialize(myFeatures);
502     } else
503       aFIter.Next();
504   }
505   theFeature->data()->setName(aName);
506 }
507
508 void Model_Objects::initData(ObjectPtr theObj, TDF_Label theLab, const int theTag)
509 {
510   std::shared_ptr<Model_Data> aData(new Model_Data);
511   aData->setLabel(theLab.FindChild(theTag));
512   aData->setObject(theObj);
513   theObj->setDoc(myDoc);
514   theObj->setData(aData);
515   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
516   if (aFeature) {
517     setUniqueName(aFeature);  // must be before "initAttributes" because duplicate part uses name
518   }
519   theObj->initAttributes();
520 }
521
522 void Model_Objects::synchronizeFeatures(
523   const bool theMarkUpdated, const bool theUpdateReferences, const bool theFlush)
524 {
525   Model_Document* anOwner = std::dynamic_pointer_cast<Model_Document>(myDoc).get();
526   if (!anOwner) // this may happen on creation of document: nothing there, so nothing to synchronize
527     return;
528   // after all updates, sends a message that groups of features were created or updated
529   Events_Loop* aLoop = Events_Loop::loop();
530   static Events_ID aDispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
531   static Events_ID aCreateEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
532   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
533   static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
534   static Events_ID aDeleteEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
535   static Events_ID aToHideEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
536   bool isActive = aLoop->activateFlushes(false);
537
538   // update all objects by checking are they on labels or not
539   std::set<FeaturePtr> aNewFeatures, aKeptFeatures;
540   TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
541   for (; aLabIter.More(); aLabIter.Next()) {
542     TDF_Label aFeatureLabel = aLabIter.Value()->Label();
543     FeaturePtr aFeature;
544     if (!myFeatures.IsBound(aFeatureLabel)) {  // a new feature is inserted
545       // create a feature
546       aFeature = std::dynamic_pointer_cast<Model_Session>(ModelAPI_Session::get())->createFeature(
547         TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(aLabIter.Value())->Get())
548         .ToCString(), anOwner);
549       if (!aFeature) {  // somethig is wrong, most probably, the opened document has invalid structure
550         Events_Error::send("Invalid type of object in the document");
551         aLabIter.Value()->Label().ForgetAllAttributes();
552         continue;
553       }
554       // this must be before "setData" to redo the sketch line correctly
555       myFeatures.Bind(aFeatureLabel, aFeature);
556       aNewFeatures.insert(aFeature);
557       initData(aFeature, aFeatureLabel, TAG_FEATURE_ARGUMENTS);
558       updateHistory(aFeature);
559       aFeature->setDisabled(false); // by default created feature is enabled (this allows to recreate the results before "setCurrent" is called)
560
561       // event: model is updated
562       ModelAPI_EventCreator::get()->sendUpdated(aFeature, aCreateEvent);
563     } else {  // nothing is changed, both iterators are incremented
564       aFeature = myFeatures.Find(aFeatureLabel);
565       aKeptFeatures.insert(aFeature);
566       if (theMarkUpdated) {
567         ModelAPI_EventCreator::get()->sendUpdated(aFeature, anUpdateEvent);
568       }
569     }
570   }
571
572   // check all features are checked: if not => it was removed
573   NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myFeatures);
574   while (aFIter.More()) {
575     if (aKeptFeatures.find(aFIter.Value()) == aKeptFeatures.end()
576       && aNewFeatures.find(aFIter.Value()) == aNewFeatures.end()) {
577         FeaturePtr aFeature = aFIter.Value();
578         // event: model is updated
579         //if (aFeature->isInHistory()) {
580         ModelAPI_EventCreator::get()->sendDeleted(myDoc, ModelAPI_Feature::group());
581         //}
582         // results of this feature must be redisplayed (hided)
583         // redisplay also removed feature (used for sketch and AISObject)
584         ModelAPI_EventCreator::get()->sendUpdated(aFeature, aRedispEvent);
585         updateHistory(aFeature);
586         aFeature->erase();
587         // unbind after the "erase" call: on abort sketch is removes sub-objects that corrupts aFIter
588         myFeatures.UnBind(aFIter.Key());
589         // reinitialize iterator because unbind may corrupt the previous order in the map
590         aFIter.Initialize(myFeatures);
591     } else
592       aFIter.Next();
593   }
594
595   if (theUpdateReferences) {
596     synchronizeBackRefs();
597   }
598   // update results of the features (after features created because they may be connected, like sketch and sub elements)
599   // After synchronisation of back references because sketch must be set in sub-elements before "execute" by updateResults
600   std::list<FeaturePtr> aComposites; // composites must be updated after their subs (issue 360)
601   TDF_ChildIDIterator aLabIter2(featuresLabel(), TDataStd_Comment::GetID());
602   for (; aLabIter2.More(); aLabIter2.Next()) {
603     TDF_Label aFeatureLabel = aLabIter2.Value()->Label();
604     if (myFeatures.IsBound(aFeatureLabel)) {  // a new feature is inserted
605       FeaturePtr aFeature = myFeatures.Find(aFeatureLabel);
606       if (std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature).get())
607         aComposites.push_back(aFeature);
608       updateResults(aFeature);
609     }
610   }
611   std::list<FeaturePtr>::iterator aComposite = aComposites.begin();
612   for(; aComposite != aComposites.end(); aComposite++) {
613     updateResults(*aComposite);
614   }
615
616   // the synchronize should be done after updateResults in order to correct back references of updated results
617   if (theUpdateReferences) {
618     synchronizeBackRefs();
619   }
620   if (theMarkUpdated) { // this means there is no control what was modified => remove history cash
621     myHistory.clear();
622   }
623
624   anOwner->executeFeatures() = false;
625   aLoop->activateFlushes(isActive);
626
627   if (theFlush) {
628     aLoop->flush(aCreateEvent);
629     aLoop->flush(aDeleteEvent);
630     aLoop->flush(anUpdateEvent);
631     aLoop->flush(aRedispEvent);
632     aLoop->flush(aToHideEvent);
633   }
634   anOwner->executeFeatures() = true;
635 }
636
637 void Model_Objects::synchronizeBackRefs()
638 {
639   // keeps the concealed flags of result to catch the change and create created/deleted events
640   std::list<std::pair<ResultPtr, bool> > aConcealed;
641   // first cycle: erase all data about back-references
642   NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeatures(myFeatures);
643   for(; aFeatures.More(); aFeatures.Next()) {
644     FeaturePtr aFeature = aFeatures.Value();
645     std::shared_ptr<Model_Data> aFData = 
646       std::dynamic_pointer_cast<Model_Data>(aFeature->data());
647     if (aFData) {
648       aFData->eraseBackReferences();
649     }
650     const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
651     std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
652     for (; aRIter != aResults.cend(); aRIter++) {
653       std::shared_ptr<Model_Data> aResData = 
654         std::dynamic_pointer_cast<Model_Data>((*aRIter)->data());
655       if (aResData) {
656         aConcealed.push_back(std::pair<ResultPtr, bool>(*aRIter, (*aRIter)->isConcealed()));
657         aResData->eraseBackReferences();
658       }
659     }
660   }
661
662   // second cycle: set new back-references: only features may have reference, iterate only them
663   ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
664   for(aFeatures.Initialize(myFeatures); aFeatures.More(); aFeatures.Next()) {
665     FeaturePtr aFeature = aFeatures.Value();
666     std::shared_ptr<Model_Data> aFData = 
667       std::dynamic_pointer_cast<Model_Data>(aFeature->data());
668     if (aFData) {
669       std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
670       aFData->referencesToObjects(aRefs);
671       std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator 
672         aRefsIter = aRefs.begin();
673       for(; aRefsIter != aRefs.end(); aRefsIter++) {
674         std::list<ObjectPtr>::iterator aRefTo = aRefsIter->second.begin();
675         for(; aRefTo != aRefsIter->second.end(); aRefTo++) {
676           if (*aRefTo) {
677             std::shared_ptr<Model_Data> aRefData = 
678               std::dynamic_pointer_cast<Model_Data>((*aRefTo)->data());
679             aRefData->addBackReference(aFeature, aRefsIter->first); // here the Concealed flag is updated
680             // update enable/disable status: the nested status must be equal to the composite
681             CompositeFeaturePtr aComp = 
682               std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
683             if (aComp.get()) {
684               FeaturePtr aReferenced = std::dynamic_pointer_cast<ModelAPI_Feature>(*aRefTo);
685               if (aReferenced.get()) {
686                 aReferenced->setDisabled(aComp->isDisabled());
687               }
688             }
689           }
690         }
691       }
692     }
693   }
694   std::list<std::pair<ResultPtr, bool> >::iterator aCIter = aConcealed.begin();
695   for(; aCIter != aConcealed.end(); aCIter++) {
696     if (aCIter->first->isConcealed() != aCIter->second) { // somethign is changed => produce event
697       if (aCIter->second) { // was concealed become not => creation event
698         static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
699         ModelAPI_EventCreator::get()->sendUpdated(aCIter->first, anEvent);
700       } else { // was not concealed become concealed => delete event
701         ModelAPI_EventCreator::get()->sendDeleted(myDoc, aCIter->first->groupName());
702         // redisplay for the viewer (it must be disappeared also)
703         static Events_ID EVENT_DISP = 
704           Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
705         ModelAPI_EventCreator::get()->sendUpdated(aCIter->first, EVENT_DISP);
706       }
707     }
708   }
709 }
710
711 TDF_Label Model_Objects::resultLabel(
712   const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex) 
713 {
714   const std::shared_ptr<Model_Data>& aData = 
715     std::dynamic_pointer_cast<Model_Data>(theFeatureData);
716   return aData->label().Father().FindChild(TAG_FEATURE_RESULTS).FindChild(theResultIndex + 1);
717 }
718
719 void Model_Objects::storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
720                                  std::shared_ptr<ModelAPI_Result> theResult,
721                                  const int theResultIndex)
722 {
723   theResult->setDoc(myDoc);
724   initData(theResult, resultLabel(theFeatureData, theResultIndex), TAG_FEATURE_ARGUMENTS);
725   if (theResult->data()->name().empty()) {  // if was not initialized, generate event and set a name
726     std::stringstream aNewName;
727     aNewName<<theFeatureData->name();
728     if (theResultIndex > 0) // if there are several results, add unique prefix starting from second
729       aNewName<<"_"<<theResultIndex + 1;
730     theResult->data()->setName(aNewName.str());
731   }
732 }
733
734 std::shared_ptr<ModelAPI_ResultConstruction> Model_Objects::createConstruction(
735     const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
736 {
737   TDF_Label aLab = resultLabel(theFeatureData, theIndex);
738   TDataStd_Comment::Set(aLab, ModelAPI_ResultConstruction::group().c_str());
739   ObjectPtr anOldObject = object(aLab);
740   std::shared_ptr<ModelAPI_ResultConstruction> aResult;
741   if (anOldObject) {
742     aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anOldObject);
743   }
744   if (!aResult) {
745     aResult = std::shared_ptr<ModelAPI_ResultConstruction>(new Model_ResultConstruction);
746     storeResult(theFeatureData, aResult, theIndex);
747   }
748   return aResult;
749 }
750
751 std::shared_ptr<ModelAPI_ResultBody> Model_Objects::createBody(
752     const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
753 {
754   TDF_Label aLab = resultLabel(theFeatureData, theIndex);
755   TDataStd_Comment::Set(aLab, ModelAPI_ResultBody::group().c_str());
756   ObjectPtr anOldObject = object(aLab);
757   std::shared_ptr<ModelAPI_ResultBody> aResult;
758   if (anOldObject) {
759     aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(anOldObject);
760   }
761   if (!aResult) {
762     aResult = std::shared_ptr<ModelAPI_ResultBody>(new Model_ResultBody);
763     storeResult(theFeatureData, aResult, theIndex);
764   }
765   return aResult;
766 }
767
768 std::shared_ptr<ModelAPI_ResultCompSolid> Model_Objects::createCompSolid(
769     const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
770 {
771   TDF_Label aLab = resultLabel(theFeatureData, theIndex);
772   TDataStd_Comment::Set(aLab, ModelAPI_ResultCompSolid::group().c_str());
773   ObjectPtr anOldObject = object(aLab);
774   std::shared_ptr<ModelAPI_ResultCompSolid> aResult;
775   if (anOldObject) {
776     aResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(anOldObject);
777   }
778   if (!aResult) {
779     aResult = std::shared_ptr<ModelAPI_ResultCompSolid>(new Model_ResultCompSolid);
780     storeResult(theFeatureData, aResult, theIndex);
781   }
782   return aResult;
783 }
784
785 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::createPart(
786     const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
787 {
788   TDF_Label aLab = resultLabel(theFeatureData, theIndex);
789   TDataStd_Comment::Set(aLab, ModelAPI_ResultPart::group().c_str());
790   ObjectPtr anOldObject = object(aLab);
791   std::shared_ptr<ModelAPI_ResultPart> aResult;
792   if (anOldObject) {
793     aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(anOldObject);
794   }
795   if (!aResult) {
796     aResult = std::shared_ptr<ModelAPI_ResultPart>(new Model_ResultPart);
797     storeResult(theFeatureData, aResult, theIndex);
798   }
799   return aResult;
800 }
801
802 std::shared_ptr<ModelAPI_ResultPart> Model_Objects::copyPart(
803     const std::shared_ptr<ModelAPI_ResultPart>& theOrigin,
804     const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
805 {
806   std::shared_ptr<ModelAPI_ResultPart> aResult = createPart(theFeatureData, theIndex);
807   aResult->data()->reference(Model_ResultPart::BASE_REF_ID())->setValue(theOrigin);
808   return aResult;
809 }
810
811 std::shared_ptr<ModelAPI_ResultGroup> Model_Objects::createGroup(
812     const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
813 {
814   TDF_Label aLab = resultLabel(theFeatureData, theIndex);
815   TDataStd_Comment::Set(aLab, ModelAPI_ResultGroup::group().c_str());
816   ObjectPtr anOldObject = object(aLab);
817   std::shared_ptr<ModelAPI_ResultGroup> aResult;
818   if (anOldObject) {
819     aResult = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(anOldObject);
820   }
821   if (!aResult) {
822     aResult = std::shared_ptr<ModelAPI_ResultGroup>(new Model_ResultGroup(theFeatureData));
823     storeResult(theFeatureData, aResult, theIndex);
824   }
825   return aResult;
826 }
827
828 std::shared_ptr<ModelAPI_ResultParameter> Model_Objects::createParameter(
829       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
830 {
831   TDF_Label aLab = resultLabel(theFeatureData, theIndex);
832   TDataStd_Comment::Set(aLab, ModelAPI_ResultParameter::group().c_str());
833   ObjectPtr anOldObject = object(aLab);
834   std::shared_ptr<ModelAPI_ResultParameter> aResult;
835   if (anOldObject) {
836     aResult = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(anOldObject);
837   }
838   if (!aResult) {
839     aResult = std::shared_ptr<ModelAPI_ResultParameter>(new Model_ResultParameter);
840     storeResult(theFeatureData, aResult, theIndex);
841   }
842   return aResult;
843 }
844
845 std::shared_ptr<ModelAPI_Feature> Model_Objects::feature(
846     const std::shared_ptr<ModelAPI_Result>& theResult)
847 {
848   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theResult->data());
849   if (aData) {
850     TDF_Label aFeatureLab = aData->label().Father().Father().Father();
851     return feature(aFeatureLab);
852   }
853   return FeaturePtr();
854 }
855
856 std::string Model_Objects::featureResultGroup(FeaturePtr theFeature)
857 {
858   if (theFeature->data()->isValid()) {
859     TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
860     if (aLabIter.More()) {
861       TDF_Label anArgLab = aLabIter.Value();
862       Handle(TDataStd_Comment) aGroup;
863       if (aLabIter.Value().FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
864         return TCollection_AsciiString(aGroup->Get()).ToCString();
865       }
866     }
867   }
868   static std::string anEmpty;
869   return anEmpty; // not found
870 }
871
872 void Model_Objects::updateResults(FeaturePtr theFeature)
873 {
874   // for not persistent is will be done by parametric updater automatically
875   //if (!theFeature->isPersistentResult()) return;
876   // check the existing results and remove them if there is nothing on the label
877   std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
878   while(aResIter != theFeature->results().cend()) {
879     ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(*aResIter);
880     if (aBody.get()) {
881       if (!aBody->data()->isValid()) { 
882         // found a disappeared result => remove it
883         theFeature->eraseResultFromList(aBody);
884         // start iterate from beginning because iterator is corrupted by removing
885         aResIter = theFeature->results().cbegin();
886         continue;
887       }
888     }
889     aResIter++;
890   }
891   // it may be on undo
892   if (!theFeature->data() || !theFeature->data()->isValid() || theFeature->isDisabled())
893     return;
894   // check that results are presented on all labels
895   int aResSize = theFeature->results().size();
896   TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
897   for(; aLabIter.More(); aLabIter.Next()) {
898     // here must be GUID of the feature
899     int aResIndex = aLabIter.Value().Tag() - 1;
900     ResultPtr aNewBody;
901     if (aResSize <= aResIndex) {
902       TDF_Label anArgLab = aLabIter.Value();
903       Handle(TDataStd_Comment) aGroup;
904       if (anArgLab.FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
905         if (aGroup->Get() == ModelAPI_ResultBody::group().c_str()) {
906           aNewBody = createBody(theFeature->data(), aResIndex);
907         } else if (aGroup->Get() == ModelAPI_ResultPart::group().c_str()) {
908           std::shared_ptr<ModelAPI_ResultPart> aNewP = createPart(theFeature->data(), aResIndex); 
909           theFeature->setResult(aNewP, aResIndex);
910           if (!aNewP->partDoc().get())
911             theFeature->execute(); // create the part result: it is better to restore the previous result if it is possible
912           break;
913         } else if (aGroup->Get() == ModelAPI_ResultConstruction::group().c_str()) {
914           theFeature->execute(); // construction shapes are needed for sketch solver
915           break;
916         } else if (aGroup->Get() == ModelAPI_ResultGroup::group().c_str()) {
917           aNewBody = createGroup(theFeature->data(), aResIndex);
918         } else if (aGroup->Get() == ModelAPI_ResultParameter::group().c_str()) {
919           theFeature->attributeChanged("expression"); // just produce a value
920           break;
921         } else {
922           Events_Error::send(std::string("Unknown type of result is found in the document:") +
923             TCollection_AsciiString(aGroup->Get()).ToCString());
924         }
925       }
926       if (aNewBody) {
927         theFeature->setResult(aNewBody, aResIndex);
928       }
929     }
930   }
931 }
932
933 ResultPtr Model_Objects::findByName(const std::string theName)
934 {
935   NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator anObjIter(myFeatures);
936   for(; anObjIter.More(); anObjIter.Next()) {
937     FeaturePtr& aFeature = anObjIter.ChangeValue();
938     if (!aFeature.get() || aFeature->isDisabled()) // may be on close
939       continue;
940     const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
941     std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
942     for (; aRIter != aResults.cend(); aRIter++) {
943       ResultPtr aRes = *aRIter;
944       if (aRes.get() && aRes->data() && aRes->data()->isValid() && !aRes->isDisabled() &&
945           aRes->data()->name() == theName) {
946         return aRes;
947       }
948     }
949   }
950   // not found
951   return ResultPtr();
952 }
953
954 FeaturePtr Model_Objects::nextFeature(FeaturePtr theCurrent, const bool theReverse)
955 {
956   std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theCurrent->data());
957   if (aData && aData->isValid()) {
958     TDF_Label aFeatureLabel = aData->label().Father();
959     Handle(TDataStd_ReferenceArray) aRefs;
960     if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
961       for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) { // iterate all existing features
962         if (aRefs->Value(a).IsEqual(aFeatureLabel)) {
963           a += theReverse ? -1 : 1;
964           if (a >= aRefs->Lower() && a <= aRefs->Upper())
965             return feature(aRefs->Value(a));
966           break; // finish iiteration: it's last feature
967         }
968       }
969     }
970   }
971   return FeaturePtr(); // not found, last, or something is wrong
972 }
973
974 FeaturePtr Model_Objects::firstFeature()
975 {
976   Handle(TDataStd_ReferenceArray) aRefs;
977   if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
978     return feature(aRefs->Value(aRefs->Lower()));
979   }
980   return FeaturePtr(); // no features at all
981 }
982
983 FeaturePtr Model_Objects::lastFeature()
984 {
985   Handle(TDataStd_ReferenceArray) aRefs;
986   if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
987     return feature(aRefs->Value(aRefs->Upper()));
988   }
989   return FeaturePtr(); // no features at all
990 }
991
992 std::list<std::shared_ptr<ModelAPI_Feature> > Model_Objects::allFeatures()
993 {
994   std::list<std::shared_ptr<ModelAPI_Feature> > aResult;
995   Handle(TDataStd_ReferenceArray) aRefs;
996   if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
997     for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
998       FeaturePtr aFeature = feature(aRefs->Value(a));
999       if (aFeature.get())
1000         aResult.push_back(aFeature);
1001     }
1002   }
1003   return aResult;
1004 }
1005
1006 int Model_Objects::numInternalFeatures()
1007 {
1008   Handle(TDataStd_ReferenceArray) aRefs;
1009   if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1010     return aRefs->Upper() - aRefs->Lower() + 1;
1011   }
1012   return 0; // invalid
1013 }
1014
1015 std::shared_ptr<ModelAPI_Feature> Model_Objects::internalFeature(const int theIndex)
1016 {
1017   Handle(TDataStd_ReferenceArray) aRefs;
1018   if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
1019     return feature(aRefs->Value(aRefs->Lower() + theIndex));
1020   }
1021   return FeaturePtr(); // invalid
1022 }
1023
1024 Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper)
1025 {
1026   return TDF_LabelMapHasher::HashCode(theLab, theUpper);
1027
1028 }
1029 Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2)
1030 {
1031   return TDF_LabelMapHasher::IsEqual(theLab1, theLab2);
1032 }