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