Salome HOME
Merge branch 'BR_PYTHON_PLUGIN' of newgeom:newgeom.git into Dev_0.6.1
[modules/shaper.git] / src / Model / Model_Document.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_Document.cxx
4 // Created:     28 Feb 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include <Model_Document.h>
8 #include <Model_Data.h>
9 #include <Model_Application.h>
10 #include <Model_Session.h>
11 #include <Model_Events.h>
12 #include <Model_ResultPart.h>
13 #include <Model_ResultConstruction.h>
14 #include <Model_ResultBody.h>
15 #include <Model_ResultGroup.h>
16 #include <ModelAPI_Validator.h>
17 #include <Events_Loop.h>
18 #include <Events_Error.h>
19
20 #include <TDataStd_Integer.hxx>
21 #include <TDataStd_Comment.hxx>
22 #include <TDF_ChildIDIterator.hxx>
23 #include <TDataStd_ReferenceArray.hxx>
24 #include <TDataStd_HLabelArray1.hxx>
25 #include <TDataStd_Name.hxx>
26 #include <TDF_Reference.hxx>
27 #include <TDF_ChildIDIterator.hxx>
28 #include <TDF_LabelMapHasher.hxx>
29 #include <OSD_File.hxx>
30 #include <OSD_Path.hxx>
31
32 #include <climits>
33 #ifndef WIN32
34 #include <sys/stat.h>
35 #endif
36
37 #ifdef WIN32
38 # define _separator_ '\\'
39 #else
40 # define _separator_ '/'
41 #endif
42
43 static const int UNDO_LIMIT = 10;  // number of possible undo operations
44
45 static const int TAG_GENERAL = 1;  // general properties tag
46 static const int TAG_OBJECTS = 2;  // tag of the objects sub-tree (features, results)
47 static const int TAG_HISTORY = 3;  // tag of the history sub-tree (python dump)
48
49 // feature sub-labels
50 static const int TAG_FEATURE_ARGUMENTS = 1;  ///< where the arguments are located
51 static const int TAG_FEATURE_RESULTS = 2;  ///< where the results are located
52
53 ///
54 /// 0:1:2 - where features are located
55 /// 0:1:2:N:1 - data of the feature N
56 /// 0:1:2:N:2:K:1 - data of the K result of the feature N
57
58 Model_Document::Model_Document(const std::string theID, const std::string theKind)
59     : myID(theID), myKind(theKind),
60       myDoc(new TDocStd_Document("BinOcaf"))  // binary OCAF format
61 {
62   myDoc->SetUndoLimit(UNDO_LIMIT);  
63   myTransactionsCounter = 0;
64   myTransactionSave = 0;
65   myNestedNum = -1;
66   myExecuteFeatures = true;
67   // to have something in the document and avoid empty doc open/save problem
68   // in transaction for nesting correct working
69   myDoc->NewCommand();
70   TDataStd_Integer::Set(myDoc->Main().Father(), 0);
71   myDoc->CommitCommand();
72 }
73
74 /// Returns the file name of this document by the nameof directory and identifuer of a document
75 static TCollection_ExtendedString DocFileName(const char* theFileName, const std::string& theID)
76 {
77   TCollection_ExtendedString aPath((const Standard_CString) theFileName);
78   // remove end-separators
79   while(aPath.Length() && (aPath.Value(aPath.Length()) == '\\' || aPath.Value(aPath.Length()) == '/'))
80     aPath.Remove(aPath.Length());
81   aPath += _separator_;
82   aPath += theID.c_str();
83   aPath += ".cbf";  // standard binary file extension
84   return aPath;
85 }
86
87 bool Model_Document::load(const char* theFileName)
88 {
89   Handle(Model_Application) anApp = Model_Application::getApplication();
90   if (this == Model_Session::get()->moduleDocument().get()) {
91     anApp->setLoadPath(theFileName);
92   }
93   TCollection_ExtendedString aPath(DocFileName(theFileName, myID));
94   PCDM_ReaderStatus aStatus = (PCDM_ReaderStatus) -1;
95   try {
96     aStatus = anApp->Open(aPath, myDoc);
97   } catch (Standard_Failure) {
98     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
99     Events_Error::send(
100         std::string("Exception in opening of document: ") + aFail->GetMessageString());
101     return false;
102   }
103   bool isError = aStatus != PCDM_RS_OK;
104   if (isError) {
105     switch (aStatus) {
106       case PCDM_RS_UnknownDocument:
107         Events_Error::send(std::string("Can not open document: unknown format"));
108         break;
109       case PCDM_RS_AlreadyRetrieved:
110         Events_Error::send(std::string("Can not open document: already opened"));
111         break;
112       case PCDM_RS_AlreadyRetrievedAndModified:
113         Events_Error::send(
114             std::string("Can not open document: already opened and modified"));
115         break;
116       case PCDM_RS_NoDriver:
117         Events_Error::send(std::string("Can not open document: driver library is not found"));
118         break;
119       case PCDM_RS_UnknownFileDriver:
120         Events_Error::send(std::string("Can not open document: unknown driver for opening"));
121         break;
122       case PCDM_RS_OpenError:
123         Events_Error::send(std::string("Can not open document: file open error"));
124         break;
125       case PCDM_RS_NoVersion:
126         Events_Error::send(std::string("Can not open document: invalid version"));
127         break;
128       case PCDM_RS_NoModel:
129         Events_Error::send(std::string("Can not open document: no data model"));
130         break;
131       case PCDM_RS_NoDocument:
132         Events_Error::send(std::string("Can not open document: no document inside"));
133         break;
134       case PCDM_RS_FormatFailure:
135         Events_Error::send(std::string("Can not open document: format failure"));
136         break;
137       case PCDM_RS_TypeNotFoundInSchema:
138         Events_Error::send(std::string("Can not open document: invalid object"));
139         break;
140       case PCDM_RS_UnrecognizedFileFormat:
141         Events_Error::send(std::string("Can not open document: unrecognized file format"));
142         break;
143       case PCDM_RS_MakeFailure:
144         Events_Error::send(std::string("Can not open document: make failure"));
145         break;
146       case PCDM_RS_PermissionDenied:
147         Events_Error::send(std::string("Can not open document: permission denied"));
148         break;
149       case PCDM_RS_DriverFailure:
150         Events_Error::send(std::string("Can not open document: driver failure"));
151         break;
152       default:
153         Events_Error::send(std::string("Can not open document: unknown error"));
154         break;
155     }
156   }
157   if (!isError) {
158     myDoc->SetUndoLimit(UNDO_LIMIT);
159     // to avoid the problem that feature is created in the current, not this, document
160     Model_Session::get()->setActiveDocument(anApp->getDocument(myID), false);
161     synchronizeFeatures(false, true);
162     Model_Session::get()->setActiveDocument(Model_Session::get()->moduleDocument(), false);
163     Model_Session::get()->setActiveDocument(anApp->getDocument(myID), true);
164   }
165   return !isError;
166 }
167
168 bool Model_Document::save(const char* theFileName, std::list<std::string>& theResults)
169 {
170   // create a directory in the root document if it is not yet exist
171   Handle(Model_Application) anApp = Model_Application::getApplication();
172   if (this == Model_Session::get()->moduleDocument().get()) {
173 #ifdef WIN32
174     CreateDirectory(theFileName, NULL);
175 #else
176     mkdir(theFileName, 0x1ff);
177 #endif
178   }
179   // filename in the dir is id of document inside of the given directory
180   TCollection_ExtendedString aPath(DocFileName(theFileName, myID));
181   PCDM_StoreStatus aStatus;
182   try {
183     aStatus = anApp->SaveAs(myDoc, aPath);
184   } catch (Standard_Failure) {
185     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
186     Events_Error::send(
187         std::string("Exception in saving of document: ") + aFail->GetMessageString());
188     return false;
189   }
190   bool isDone = aStatus == PCDM_SS_OK || aStatus == PCDM_SS_No_Obj;
191   if (!isDone) {
192     switch (aStatus) {
193       case PCDM_SS_DriverFailure:
194         Events_Error::send(std::string("Can not save document: save driver-library failure"));
195         break;
196       case PCDM_SS_WriteFailure:
197         Events_Error::send(std::string("Can not save document: file writing failure"));
198         break;
199       case PCDM_SS_Failure:
200       default:
201         Events_Error::send(std::string("Can not save document"));
202         break;
203     }
204   }
205   myTransactionSave = myTransactionsCounter;
206   if (isDone) {  // save also sub-documents if any
207     theResults.push_back(TCollection_AsciiString(aPath).ToCString());
208     std::set<std::string>::iterator aSubIter = mySubs.begin();
209     for (; aSubIter != mySubs.end() && isDone; aSubIter++) {
210       isDone = subDoc(*aSubIter)->save(theFileName, theResults);
211     }
212     if (isDone) { // also try to copy the not-activated sub-documents
213       // they are not in mySubs but as ResultParts
214       int aPartsNum = size(ModelAPI_ResultPart::group());
215       for(int aPart = 0; aPart < aPartsNum; aPart++) {
216         ResultPartPtr aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>
217           (object(ModelAPI_ResultPart::group(), aPart));
218         if (aPartRes) {
219           std::string aDocName = aPartRes->data()->name();
220           if (!aDocName.empty() && mySubs.find(aDocName) == mySubs.end()) {
221             // just copy file
222             TCollection_AsciiString aSubPath(DocFileName(anApp->loadPath().c_str(), aDocName));
223             OSD_Path aPath(aSubPath);
224             OSD_File aFile(aPath);
225             if (aFile.Exists()) {
226               TCollection_AsciiString aDestinationDir(DocFileName(theFileName, aDocName));
227               OSD_Path aDestination(aDestinationDir);
228               aFile.Copy(aDestination);
229               theResults.push_back(aDestinationDir.ToCString());
230             } else {
231               Events_Error::send(
232                 std::string("Can not open file ") + aSubPath.ToCString() + " for saving");
233             }
234           }
235         }
236       }
237     }
238   }
239   return isDone;
240 }
241
242 void Model_Document::close(const bool theForever)
243 {
244   std::shared_ptr<ModelAPI_Session> aPM = Model_Session::get();
245   if (this != aPM->moduleDocument().get() && this == aPM->activeDocument().get()) {
246     aPM->setActiveDocument(aPM->moduleDocument());
247   }
248   // close all subs
249   std::set<std::string>::iterator aSubIter = mySubs.begin();
250   for (; aSubIter != mySubs.end(); aSubIter++)
251     subDoc(*aSubIter)->close(theForever);
252   mySubs.clear();
253
254   // close for thid document needs no transaction in this document
255   std::static_pointer_cast<Model_Session>(Model_Session::get())->setCheckTransactions(false);
256
257   // delete all features of this document
258   std::shared_ptr<ModelAPI_Document> aThis = 
259     Model_Application::getApplication()->getDocument(myID);
260   Events_Loop* aLoop = Events_Loop::loop();
261   NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeaturesIter(myObjs);
262   for(; aFeaturesIter.More(); aFeaturesIter.Next()) {
263     FeaturePtr aFeature = aFeaturesIter.Value();
264     static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
265     ModelAPI_EventCreator::get()->sendDeleted(aThis, ModelAPI_Feature::group());
266     ModelAPI_EventCreator::get()->sendUpdated(aFeature, EVENT_DISP);
267     aFeature->eraseResults();
268     aFeature->erase();
269   }
270   myObjs.Clear();
271   aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
272   aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
273
274   // close all only if it is really asked, otherwise it can be undoed/redoed
275   if (theForever) {
276     if (myDoc->CanClose() == CDM_CCS_OK)
277       myDoc->Close();
278   }
279
280   std::static_pointer_cast<Model_Session>(Model_Session::get())->setCheckTransactions(true);
281 }
282
283 void Model_Document::startOperation()
284 {
285   if (myDoc->HasOpenCommand()) {  // start of nested command
286     if (myNestedNum == -1) {
287       myNestedNum = 0;
288       myDoc->InitDeltaCompaction();
289     }
290     myIsEmptyTr[myTransactionsCounter] = !myDoc->CommitCommand();
291     myTransactionsCounter++;
292     myDoc->OpenCommand();
293   } else {  // start the simple command
294     myDoc->NewCommand();
295   }
296   // new command for all subs
297   std::set<std::string>::iterator aSubIter = mySubs.begin();
298   for (; aSubIter != mySubs.end(); aSubIter++)
299     subDoc(*aSubIter)->startOperation();
300 }
301
302 bool Model_Document::compactNested()
303 {
304   bool allWasEmpty = true;
305   while (myNestedNum != -1) {
306     myTransactionsCounter--;
307     if (!myIsEmptyTr[myTransactionsCounter]) {
308       allWasEmpty = false;
309     }
310     myIsEmptyTr.erase(myTransactionsCounter);
311     myNestedNum--;
312   }
313   myIsEmptyTr[myTransactionsCounter] = allWasEmpty;
314   myTransactionsCounter++;
315   if (allWasEmpty) {
316     // Issue 151: if everything is empty, it is a problem for OCCT to work with it, 
317     // just commit the empty that returns nothing
318     myDoc->CommitCommand();
319   } else {
320     myDoc->PerformDeltaCompaction();
321   }
322   return !allWasEmpty;
323 }
324
325 void Model_Document::finishOperation()
326 {
327   // just to be sure that everybody knows that changes were performed
328   if (!myDoc->HasOpenCommand() && myNestedNum != -1)
329     std::static_pointer_cast<Model_Session>(Model_Session::get())
330         ->setCheckTransactions(false);  // for nested transaction commit
331   synchronizeBackRefs();
332   Events_Loop* aLoop = Events_Loop::loop();
333   aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
334   aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
335   aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
336   aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TOHIDE));
337   aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
338   // this must be here just after everything is finished but before real transaction stop
339   // to avoid messages about modifications outside of the transaction
340   // and to rebuild everything after all updates and creates
341   if (Model_Session::get()->moduleDocument().get() == this) { // once for root document
342     Events_Loop::loop()->autoFlush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
343     static std::shared_ptr<Events_Message> aFinishMsg
344       (new Events_Message(Events_Loop::eventByName("FinishOperation")));
345     Events_Loop::loop()->send(aFinishMsg);
346     Events_Loop::loop()->autoFlush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED), false);
347   }
348   // to avoid "updated" message appearance by updater
349   //aLoop->clear(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
350
351   if (!myDoc->HasOpenCommand() && myNestedNum != -1)
352     std::static_pointer_cast<Model_Session>(Model_Session::get())
353         ->setCheckTransactions(true);  // for nested transaction commit
354
355   // finish for all subs first: to avoid nested finishing and "isOperation" calls problems inside
356   std::set<std::string>::iterator aSubIter = mySubs.begin();
357   for (; aSubIter != mySubs.end(); aSubIter++)
358     subDoc(*aSubIter)->finishOperation();
359
360   if (myNestedNum != -1)  // this nested transaction is owervritten
361     myNestedNum++;
362   if (!myDoc->HasOpenCommand()) {
363     if (myNestedNum != -1) {
364       myNestedNum--;
365       compactNested();
366     }
367   } else {
368     // returns false if delta is empty and no transaction was made
369     myIsEmptyTr[myTransactionsCounter] = !myDoc->CommitCommand();  // && (myNestedNum == -1);
370     myTransactionsCounter++;
371   }
372 }
373
374 void Model_Document::abortOperation()
375 {
376   if (myNestedNum > 0 && !myDoc->HasOpenCommand()) {  // abort all what was done in nested
377       // first compact all nested
378     if (compactNested()) {
379       myDoc->Undo(); // undo only compacted, if not: do not undo the empty transaction
380     }
381     myDoc->ClearRedos();
382     myTransactionsCounter--;
383     myIsEmptyTr.erase(myTransactionsCounter);
384   } else {
385     if (myNestedNum == 0)  // abort only high-level
386       myNestedNum = -1;
387     myDoc->AbortCommand();
388   }
389   synchronizeFeatures(true, false); // references were not changed since transaction start
390   // abort for all subs
391   std::set<std::string>::iterator aSubIter = mySubs.begin();
392   for (; aSubIter != mySubs.end(); aSubIter++)
393     subDoc(*aSubIter)->abortOperation();
394 }
395
396 bool Model_Document::isOperation()
397 {
398   // operation is opened for all documents: no need to check subs
399   return myDoc->HasOpenCommand() == Standard_True ;
400 }
401
402 bool Model_Document::isModified()
403 {
404   // is modified if at least one operation was commited and not undoed
405   return myTransactionsCounter != myTransactionSave || isOperation();
406 }
407
408 bool Model_Document::canUndo()
409 {
410   if (myDoc->GetAvailableUndos() > 0 && myNestedNum != 0
411       && myTransactionsCounter != 0 /* for omitting the first useless transaction */)
412     return true;
413   // check other subs contains operation that can be undoed
414   std::set<std::string>::iterator aSubIter = mySubs.begin();
415   for (; aSubIter != mySubs.end(); aSubIter++)
416     if (subDoc(*aSubIter)->canUndo())
417       return true;
418   return false;
419 }
420
421 void Model_Document::undo()
422 {
423   myTransactionsCounter--;
424   if (myNestedNum > 0)
425     myNestedNum--;
426   if (!myIsEmptyTr[myTransactionsCounter])
427     myDoc->Undo();
428   synchronizeFeatures(true, true);
429   // undo for all subs
430   std::set<std::string>::iterator aSubIter = mySubs.begin();
431   for (; aSubIter != mySubs.end(); aSubIter++)
432     subDoc(*aSubIter)->undo();
433 }
434
435 bool Model_Document::canRedo()
436 {
437   if (myDoc->GetAvailableRedos() > 0)
438     return true;
439   // check other subs contains operation that can be redoed
440   std::set<std::string>::iterator aSubIter = mySubs.begin();
441   for (; aSubIter != mySubs.end(); aSubIter++)
442     if (subDoc(*aSubIter)->canRedo())
443       return true;
444   return false;
445 }
446
447 void Model_Document::redo()
448 {
449   if (myNestedNum != -1)
450     myNestedNum++;
451   if (!myIsEmptyTr[myTransactionsCounter])
452     myDoc->Redo();
453   myTransactionsCounter++;
454   synchronizeFeatures(true, true);
455   // redo for all subs
456   std::set<std::string>::iterator aSubIter = mySubs.begin();
457   for (; aSubIter != mySubs.end(); aSubIter++)
458     subDoc(*aSubIter)->redo();
459 }
460
461 /// Appenad to the array of references a new referenced label
462 static void AddToRefArray(TDF_Label& theArrayLab, TDF_Label& theReferenced)
463 {
464   Handle(TDataStd_ReferenceArray) aRefs;
465   if (!theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
466     aRefs = TDataStd_ReferenceArray::Set(theArrayLab, 0, 0);
467     aRefs->SetValue(0, theReferenced);
468   } else {  // extend array by one more element
469     Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
470                                                                         aRefs->Upper() + 1);
471     for (int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
472       aNewArray->SetValue(a, aRefs->Value(a));
473     }
474     aNewArray->SetValue(aRefs->Upper() + 1, theReferenced);
475     aRefs->SetInternalArray(aNewArray);
476   }
477 }
478
479 FeaturePtr Model_Document::addFeature(std::string theID)
480 {
481   TDF_Label anEmptyLab;
482   FeaturePtr anEmptyFeature;
483   FeaturePtr aFeature = ModelAPI_Session::get()->createFeature(theID);
484   if (!aFeature)
485     return aFeature;
486   std::shared_ptr<Model_Document> aDocToAdd = std::dynamic_pointer_cast<Model_Document>(
487       aFeature->documentToAdd());
488   if (aFeature) {
489     TDF_Label aFeatureLab;
490     if (!aFeature->isAction()) {  // do not add action to the data model
491       TDF_Label aFeaturesLab = aDocToAdd->featuresLabel();
492       aFeatureLab = aFeaturesLab.NewChild();
493       aDocToAdd->initData(aFeature, aFeatureLab, TAG_FEATURE_ARGUMENTS);
494       // keep the feature ID to restore document later correctly
495       TDataStd_Comment::Set(aFeatureLab, aFeature->getKind().c_str());
496       aDocToAdd->myObjs.Bind(aFeatureLab, aFeature);
497       // store feature in the history of features array
498       if (aFeature->isInHistory()) {
499         AddToRefArray(aFeaturesLab, aFeatureLab);
500       }
501     }
502     if (!aFeature->isAction()) {  // do not add action to the data model
503       // event: feature is added
504       static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
505       ModelAPI_EventCreator::get()->sendUpdated(aFeature, anEvent);
506     } else { // feature must be executed
507        // no creation event => updater not working, problem with remove part
508       aFeature->execute();
509     }
510   }
511   return aFeature;
512 }
513
514 /// Appenad to the array of references a new referenced label.
515 /// If theIndex is not -1, removes element at thisindex, not theReferenced.
516 /// \returns the index of removed element
517 static int RemoveFromRefArray(TDF_Label theArrayLab, TDF_Label theReferenced, const int theIndex =
518                                   -1)
519 {
520   int aResult = -1;  // no returned
521   Handle(TDataStd_ReferenceArray) aRefs;
522   if (theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
523     if (aRefs->Length() == 1) {  // just erase an array
524       if ((theIndex == -1 && aRefs->Value(0) == theReferenced) || theIndex == 0) {
525         theArrayLab.ForgetAttribute(TDataStd_ReferenceArray::GetID());
526       }
527       aResult = 0;
528     } else {  // reduce the array
529       Handle(TDataStd_HLabelArray1) aNewArray = new TDataStd_HLabelArray1(aRefs->Lower(),
530                                                                           aRefs->Upper() - 1);
531       int aCount = aRefs->Lower();
532       for (int a = aCount; a <= aRefs->Upper(); a++, aCount++) {
533         if ((theIndex == -1 && aRefs->Value(a) == theReferenced) || theIndex == a) {
534           aCount--;
535           aResult = a;
536         } else {
537           aNewArray->SetValue(aCount, aRefs->Value(a));
538         }
539       }
540       aRefs->SetInternalArray(aNewArray);
541     }
542   }
543   return aResult;
544 }
545
546 void Model_Document::removeFeature(FeaturePtr theFeature, const bool theCheck)
547 {
548   if (theCheck) {
549     // check the feature: it must have no depended objects on it
550     std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
551     for(; aResIter != theFeature->results().cend(); aResIter++) {
552       std::shared_ptr<Model_Data> aData = 
553         std::dynamic_pointer_cast<Model_Data>((*aResIter)->data());
554       if (aData && !aData->refsToMe().empty()) {
555         Events_Error::send(
556           "Feature '" + theFeature->data()->name() + "' is used and can not be deleted");
557         return;
558       }
559     }
560   }
561
562   std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theFeature->data());
563   if (aData) {
564     TDF_Label aFeatureLabel = aData->label().Father();
565     if (myObjs.IsBound(aFeatureLabel))
566       myObjs.UnBind(aFeatureLabel);
567     else
568       return;  // not found feature => do not remove
569     // erase fields
570     theFeature->erase();
571     // erase all attributes under the label of feature
572     aFeatureLabel.ForgetAllAttributes();
573     // remove it from the references array
574     if (theFeature->isInHistory()) {
575       RemoveFromRefArray(featuresLabel(), aFeatureLabel);
576     }
577   }
578   // event: feature is deleted
579   ModelAPI_EventCreator::get()->sendDeleted(theFeature->document(), ModelAPI_Feature::group());
580 }
581
582 FeaturePtr Model_Document::feature(TDF_Label& theLabel)
583 {
584   if (myObjs.IsBound(theLabel))
585     return myObjs.Find(theLabel);
586   return FeaturePtr();  // not found
587 }
588
589 ObjectPtr Model_Document::object(TDF_Label theLabel)
590 {
591   // try feature by label
592   FeaturePtr aFeature = feature(theLabel);
593   if (aFeature)
594     return feature(theLabel);
595   TDF_Label aFeatureLabel = theLabel.Father().Father();  // let's suppose it is result
596   aFeature = feature(aFeatureLabel);
597   if (aFeature) {
598     const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
599     std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
600     for (; aRIter != aResults.cend(); aRIter++) {
601       std::shared_ptr<Model_Data> aResData = std::dynamic_pointer_cast<Model_Data>(
602           (*aRIter)->data());
603       if (aResData->label().Father().IsEqual(theLabel))
604         return *aRIter;
605     }
606   }
607   return FeaturePtr();  // not found
608 }
609
610 std::shared_ptr<ModelAPI_Document> Model_Document::subDocument(std::string theDocID)
611 {
612   // just store sub-document identifier here to manage it later
613   if (mySubs.find(theDocID) == mySubs.end())
614     mySubs.insert(theDocID);
615   return Model_Application::getApplication()->getDocument(theDocID);
616 }
617
618 std::shared_ptr<Model_Document> Model_Document::subDoc(std::string theDocID)
619 {
620   // just store sub-document identifier here to manage it later
621   if (mySubs.find(theDocID) == mySubs.end())
622     mySubs.insert(theDocID);
623   return std::dynamic_pointer_cast<Model_Document>(
624     Model_Application::getApplication()->getDocument(theDocID));
625 }
626
627 ObjectPtr Model_Document::object(const std::string& theGroupID, const int theIndex,
628                                  const bool theHidden)
629 {
630   if (theGroupID == ModelAPI_Feature::group()) {
631     if (theHidden) {
632       int anIndex = 0;
633       TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
634       for (; aLabIter.More(); aLabIter.Next()) {
635         if (theIndex == anIndex) {
636           TDF_Label aFLabel = aLabIter.Value()->Label();
637           return feature(aFLabel);
638         }
639         anIndex++;
640       }
641     } else {
642       Handle(TDataStd_ReferenceArray) aRefs;
643       if (!featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
644         return ObjectPtr();
645       if (aRefs->Lower() > theIndex || aRefs->Upper() < theIndex)
646         return ObjectPtr();
647       TDF_Label aFeatureLabel = aRefs->Value(theIndex);
648       return feature(aFeatureLabel);
649     }
650   } else {
651     // comment must be in any feature: it is kind
652     int anIndex = 0;
653     TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
654     for (; aLabIter.More(); aLabIter.Next()) {
655       TDF_Label aFLabel = aLabIter.Value()->Label();
656       FeaturePtr aFeature = feature(aFLabel);
657       const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
658       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
659       for (; aRIter != aResults.cend(); aRIter++) {
660         if ((*aRIter)->groupName() != theGroupID) continue;
661         bool isIn = theHidden && (*aRIter)->isInHistory();
662         if (!isIn && (*aRIter)->isInHistory()) { // check that there is nobody references this result
663           isIn = !(*aRIter)->isConcealed();
664         }
665         if (isIn) {
666           if (anIndex == theIndex)
667             return *aRIter;
668           anIndex++;
669         }
670       }
671     }
672   }
673   // not found
674   return ObjectPtr();
675 }
676
677 int Model_Document::size(const std::string& theGroupID, const bool theHidden)
678 {
679   int aResult = 0;
680   if (theGroupID == ModelAPI_Feature::group()) {
681     if (theHidden) {
682       return myObjs.Size();
683     } else {
684       Handle(TDataStd_ReferenceArray) aRefs;
685       if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs))
686         return aRefs->Length();
687     }
688   } else {
689     // comment must be in any feature: it is kind
690     TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
691     for (; aLabIter.More(); aLabIter.Next()) {
692       TDF_Label aFLabel = aLabIter.Value()->Label();
693       FeaturePtr aFeature = feature(aFLabel);
694       if (!aFeature) // may be on close
695         continue;
696       const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
697       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
698       for (; aRIter != aResults.cend(); aRIter++) {
699         if ((*aRIter)->groupName() != theGroupID) continue;
700         bool isIn = theHidden;
701         if (!isIn && (*aRIter)->isInHistory()) { // check that there is nobody references this result
702           isIn = !(*aRIter)->isConcealed();
703         }
704         if (isIn)
705           aResult++;
706       }
707     }
708   }
709   // group is not found
710   return aResult;
711 }
712
713 TDF_Label Model_Document::featuresLabel()
714 {
715   return myDoc->Main().FindChild(TAG_OBJECTS);
716 }
717
718 void Model_Document::setUniqueName(FeaturePtr theFeature)
719 {
720   if (!theFeature->data()->name().empty())
721     return;  // not needed, name is already defined
722   std::string aName;  // result
723   // first count all objects of such kind to start with index = count + 1
724   int aNumObjects = 0;
725   NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myObjs);
726   for (; aFIter.More(); aFIter.Next()) {
727     if (aFIter.Value()->getKind() == theFeature->getKind())
728       aNumObjects++;
729   }
730   // generate candidate name
731   std::stringstream aNameStream;
732   aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
733   aName = aNameStream.str();
734   // check this is unique, if not, increase index by 1
735   for (aFIter.Initialize(myObjs); aFIter.More();) {
736     FeaturePtr aFeature = aFIter.Value();
737     bool isSameName = aFeature->data()->name() == aName;
738     if (!isSameName) {  // check also results to avoid same results names (actual for Parts)
739       const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
740       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
741       for (; aRIter != aResults.cend(); aRIter++) {
742         isSameName = (*aRIter)->data()->name() == aName;
743       }
744     }
745     if (isSameName) {
746       aNumObjects++;
747       std::stringstream aNameStream;
748       aNameStream << theFeature->getKind() << "_" << aNumObjects + 1;
749       aName = aNameStream.str();
750       // reinitialize iterator to make sure a new name is unique
751       aFIter.Initialize(myObjs);
752     } else
753       aFIter.Next();
754   }
755   theFeature->data()->setName(aName);
756 }
757
758 void Model_Document::initData(ObjectPtr theObj, TDF_Label theLab, const int theTag)
759 {
760   std::shared_ptr<ModelAPI_Document> aThis = Model_Application::getApplication()->getDocument(
761       myID);
762   std::shared_ptr<Model_Data> aData(new Model_Data);
763   aData->setLabel(theLab.FindChild(theTag));
764   aData->setObject(theObj);
765   theObj->setDoc(aThis);
766   theObj->setData(aData);
767   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
768   if (aFeature) {
769     setUniqueName(aFeature);  // must be before "initAttributes" because duplicate part uses name
770     aFeature->initAttributes();
771   }
772 }
773
774 void Model_Document::synchronizeFeatures(const bool theMarkUpdated, const bool theUpdateReferences)
775 {
776   std::shared_ptr<ModelAPI_Document> aThis = 
777     Model_Application::getApplication()->getDocument(myID);
778   // after all updates, sends a message that groups of features were created or updated
779   std::static_pointer_cast<Model_Session>(Model_Session::get())
780     ->setCheckTransactions(false);
781   Events_Loop* aLoop = Events_Loop::loop();
782   aLoop->activateFlushes(false);
783
784   // update all objects by checking are they of labels or not
785   std::set<FeaturePtr> aNewFeatures, aKeptFeatures;
786   TDF_ChildIDIterator aLabIter(featuresLabel(), TDataStd_Comment::GetID());
787   for (; aLabIter.More(); aLabIter.Next()) {
788     TDF_Label aFeatureLabel = aLabIter.Value()->Label();
789     FeaturePtr aFeature;
790     if (!myObjs.IsBound(aFeatureLabel)) {  // a new feature is inserted
791       // create a feature
792       aFeature = ModelAPI_Session::get()->createFeature(
793           TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(aLabIter.Value())->Get())
794               .ToCString());
795       if (!aFeature) {  // somethig is wrong, most probably, the opened document has invalid structure
796         Events_Error::send("Invalid type of object in the document");
797         aLabIter.Value()->Label().ForgetAllAttributes();
798         continue;
799       }
800       // this must be before "setData" to redo the sketch line correctly
801       myObjs.Bind(aFeatureLabel, aFeature);
802       aNewFeatures.insert(aFeature);
803       initData(aFeature, aFeatureLabel, TAG_FEATURE_ARGUMENTS);
804
805       // event: model is updated
806       static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
807       ModelAPI_EventCreator::get()->sendUpdated(aFeature, anEvent);
808     } else {  // nothing is changed, both iterators are incremented
809       aFeature = myObjs.Find(aFeatureLabel);
810       aKeptFeatures.insert(aFeature);
811       if (theMarkUpdated) {
812         static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
813         ModelAPI_EventCreator::get()->sendUpdated(aFeature, anEvent);
814       }
815     }
816   }
817   // update results of thefeatures (after features created because they may be connected, like sketch and sub elements)
818   TDF_ChildIDIterator aLabIter2(featuresLabel(), TDataStd_Comment::GetID());
819   for (; aLabIter2.More(); aLabIter2.Next()) {
820     TDF_Label aFeatureLabel = aLabIter2.Value()->Label();
821     if (myObjs.IsBound(aFeatureLabel)) {  // a new feature is inserted
822       FeaturePtr aFeature = myObjs.Find(aFeatureLabel);
823       updateResults(aFeature);
824     }
825   }
826
827   // check all features are checked: if not => it was removed
828   NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFIter(myObjs);
829   while (aFIter.More()) {
830     if (aKeptFeatures.find(aFIter.Value()) == aKeptFeatures.end()
831         && aNewFeatures.find(aFIter.Value()) == aNewFeatures.end()) {
832       FeaturePtr aFeature = aFIter.Value();
833       // event: model is updated
834       //if (aFeature->isInHistory()) {
835         ModelAPI_EventCreator::get()->sendDeleted(aThis, ModelAPI_Feature::group());
836       //}
837       // results of this feature must be redisplayed (hided)
838       static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
839       const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
840       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
841       // redisplay also removed feature (used for sketch and AISObject)
842       ModelAPI_EventCreator::get()->sendUpdated(aFeature, EVENT_DISP);
843       aFeature->erase();
844       // unbind after the "erase" call: on abort sketch is removes sub-objects that corrupts aFIter
845       TDF_Label aLab = aFIter.Key();
846       aFIter.Next();
847       myObjs.UnBind(aLab);
848     } else
849       aFIter.Next();
850   }
851
852   if (theUpdateReferences) {
853     synchronizeBackRefs();
854   }
855
856   myExecuteFeatures = false;
857   aLoop->activateFlushes(true);
858
859   aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
860   aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
861   aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
862   aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
863   aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_TOHIDE));
864   std::static_pointer_cast<Model_Session>(Model_Session::get())
865     ->setCheckTransactions(true);
866   myExecuteFeatures = true;
867 }
868
869 void Model_Document::synchronizeBackRefs()
870 {
871   std::shared_ptr<ModelAPI_Document> aThis = 
872     Model_Application::getApplication()->getDocument(myID);
873   // keeps the concealed flags of result to catch the change and create created/deleted events
874   std::list<std::pair<ResultPtr, bool> > aConcealed;
875   // first cycle: erase all data about back-references
876   NCollection_DataMap<TDF_Label, FeaturePtr>::Iterator aFeatures(myObjs);
877   for(; aFeatures.More(); aFeatures.Next()) {
878     FeaturePtr aFeature = aFeatures.Value();
879     std::shared_ptr<Model_Data> aFData = 
880       std::dynamic_pointer_cast<Model_Data>(aFeature->data());
881     if (aFData) {
882       aFData->eraseBackReferences();
883     }
884     const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
885     std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
886     for (; aRIter != aResults.cend(); aRIter++) {
887       std::shared_ptr<Model_Data> aResData = 
888         std::dynamic_pointer_cast<Model_Data>((*aRIter)->data());
889       if (aResData) {
890         aConcealed.push_back(std::pair<ResultPtr, bool>(*aRIter, (*aRIter)->isConcealed()));
891         aResData->eraseBackReferences();
892       }
893     }
894   }
895
896   // second cycle: set new back-references: only features may have reference, iterate only them
897   ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
898   for(aFeatures.Initialize(myObjs); aFeatures.More(); aFeatures.Next()) {
899     FeaturePtr aFeature = aFeatures.Value();
900     std::shared_ptr<Model_Data> aFData = 
901       std::dynamic_pointer_cast<Model_Data>(aFeature->data());
902     if (aFData) {
903       std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
904       aFData->referencesToObjects(aRefs);
905       std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRefsIter = aRefs.begin();
906       for(; aRefsIter != aRefs.end(); aRefsIter++) {
907         std::list<ObjectPtr>::iterator aRefTo = aRefsIter->second.begin();
908         for(; aRefTo != aRefsIter->second.end(); aRefTo++) {
909           if (*aRefTo) {
910             std::shared_ptr<Model_Data> aRefData = 
911               std::dynamic_pointer_cast<Model_Data>((*aRefTo)->data());
912             aRefData->addBackReference(aFeature, aRefsIter->first); // here the Concealed flag is updated
913           }
914         }
915       }
916     }
917   }
918   std::list<std::pair<ResultPtr, bool> >::iterator aCIter = aConcealed.begin();
919   for(; aCIter != aConcealed.end(); aCIter++) {
920     if (aCIter->first->isConcealed() != aCIter->second) { // somethign is changed => produce event
921       if (aCIter->second) { // was concealed become not => creation event
922         static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
923         ModelAPI_EventCreator::get()->sendUpdated(aCIter->first, anEvent);
924       } else { // was not concealed become concealed => delete event
925         ModelAPI_EventCreator::get()->sendDeleted(aThis, aCIter->first->groupName());
926         // redisplay for the viewer (it must be disappeared also)
927         static Events_ID EVENT_DISP = 
928           Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
929         ModelAPI_EventCreator::get()->sendUpdated(aCIter->first, EVENT_DISP);
930       }
931     }
932   }
933 }
934
935 TDF_Label Model_Document::resultLabel(
936   const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex) 
937 {
938   const std::shared_ptr<Model_Data>& aData = 
939     std::dynamic_pointer_cast<Model_Data>(theFeatureData);
940   return aData->label().Father().FindChild(TAG_FEATURE_RESULTS).FindChild(theResultIndex + 1);
941 }
942
943 void Model_Document::storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
944                                  std::shared_ptr<ModelAPI_Result> theResult,
945                                  const int theResultIndex)
946 {
947   std::shared_ptr<ModelAPI_Document> aThis = 
948     Model_Application::getApplication()->getDocument(myID);
949   theResult->setDoc(aThis);
950   initData(theResult, resultLabel(theFeatureData, theResultIndex), TAG_FEATURE_ARGUMENTS);
951   if (theResult->data()->name().empty()) {  // if was not initialized, generate event and set a name
952     theResult->data()->setName(theFeatureData->name());
953   }
954 }
955
956 std::shared_ptr<ModelAPI_ResultConstruction> Model_Document::createConstruction(
957     const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
958 {
959   TDF_Label aLab = resultLabel(theFeatureData, theIndex);
960   TDataStd_Comment::Set(aLab, ModelAPI_ResultConstruction::group().c_str());
961   ObjectPtr anOldObject = object(aLab);
962   std::shared_ptr<ModelAPI_ResultConstruction> aResult;
963   if (anOldObject) {
964     aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anOldObject);
965   }
966   if (!aResult) {
967     aResult = std::shared_ptr<ModelAPI_ResultConstruction>(new Model_ResultConstruction);
968     storeResult(theFeatureData, aResult, theIndex);
969   }
970   return aResult;
971 }
972
973 std::shared_ptr<ModelAPI_ResultBody> Model_Document::createBody(
974     const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
975 {
976   TDF_Label aLab = resultLabel(theFeatureData, theIndex);
977   TDataStd_Comment::Set(aLab, ModelAPI_ResultBody::group().c_str());
978   ObjectPtr anOldObject = object(aLab);
979   std::shared_ptr<ModelAPI_ResultBody> aResult;
980   if (anOldObject) {
981     aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(anOldObject);
982   }
983   if (!aResult) {
984     aResult = std::shared_ptr<ModelAPI_ResultBody>(new Model_ResultBody);
985     storeResult(theFeatureData, aResult, theIndex);
986   }
987   return aResult;
988 }
989
990 std::shared_ptr<ModelAPI_ResultPart> Model_Document::createPart(
991     const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
992 {
993   TDF_Label aLab = resultLabel(theFeatureData, theIndex);
994   TDataStd_Comment::Set(aLab, ModelAPI_ResultPart::group().c_str());
995   ObjectPtr anOldObject = object(aLab);
996   std::shared_ptr<ModelAPI_ResultPart> aResult;
997   if (anOldObject) {
998     aResult = std::dynamic_pointer_cast<ModelAPI_ResultPart>(anOldObject);
999   }
1000   if (!aResult) {
1001     aResult = std::shared_ptr<ModelAPI_ResultPart>(new Model_ResultPart);
1002     storeResult(theFeatureData, aResult, theIndex);
1003   }
1004   return aResult;
1005 }
1006
1007 std::shared_ptr<ModelAPI_ResultGroup> Model_Document::createGroup(
1008     const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
1009 {
1010   TDF_Label aLab = resultLabel(theFeatureData, theIndex);
1011   TDataStd_Comment::Set(aLab, ModelAPI_ResultGroup::group().c_str());
1012   ObjectPtr anOldObject = object(aLab);
1013   std::shared_ptr<ModelAPI_ResultGroup> aResult;
1014   if (anOldObject) {
1015     aResult = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(anOldObject);
1016   }
1017   if (!aResult) {
1018     aResult = std::shared_ptr<ModelAPI_ResultGroup>(new Model_ResultGroup(theFeatureData));
1019     storeResult(theFeatureData, aResult, theIndex);
1020   }
1021   return aResult;
1022 }
1023
1024 std::shared_ptr<ModelAPI_Feature> Model_Document::feature(
1025     const std::shared_ptr<ModelAPI_Result>& theResult)
1026 {
1027   std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(theResult->data());
1028   if (aData) {
1029     TDF_Label aFeatureLab = aData->label().Father().Father().Father();
1030     return feature(aFeatureLab);
1031   }
1032   return FeaturePtr();
1033 }
1034
1035 void Model_Document::updateResults(FeaturePtr theFeature)
1036 {
1037   // for not persistent is will be done by parametric updater automatically
1038   //if (!theFeature->isPersistentResult()) return;
1039   // check the existing results and remove them if there is nothing on the label
1040   std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
1041   while(aResIter != theFeature->results().cend()) {
1042     ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(*aResIter);
1043     if (aBody) {
1044       if (!aBody->data()->isValid()) { 
1045         // found a disappeared result => remove it
1046         theFeature->removeResult(aBody);
1047         // start iterate from beginning because iterator is corrupted by removing
1048         aResIter = theFeature->results().cbegin();
1049         continue;
1050       }
1051     }
1052     aResIter++;
1053   }
1054   // it may be on undo
1055   if (!theFeature->data() || !theFeature->data()->isValid())
1056     return;
1057   // check that results are presented on all labels
1058   int aResSize = theFeature->results().size();
1059   TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
1060   for(; aLabIter.More(); aLabIter.Next()) {
1061     // here must be GUID of the feature
1062     int aResIndex = aLabIter.Value().Tag() - 1;
1063     ResultPtr aNewBody;
1064     if (aResSize <= aResIndex) {
1065       TDF_Label anArgLab = aLabIter.Value();
1066       Handle(TDataStd_Comment) aGroup;
1067       if (anArgLab.FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
1068         if (aGroup->Get() == ModelAPI_ResultBody::group().c_str()) {
1069           aNewBody = createBody(theFeature->data(), aResIndex);
1070         } else if (aGroup->Get() == ModelAPI_ResultPart::group().c_str()) {
1071           aNewBody = createPart(theFeature->data(), aResIndex);
1072         } else if (aGroup->Get() == ModelAPI_ResultConstruction::group().c_str()) {
1073           theFeature->execute(); // construction shapes are needed for sketch solver
1074           break;
1075         } else if (aGroup->Get() == ModelAPI_ResultGroup::group().c_str()) {
1076           aNewBody = createGroup(theFeature->data(), aResIndex);
1077         } else {
1078           Events_Error::send(std::string("Unknown type of result is found in the document:") +
1079             TCollection_AsciiString(aGroup->Get()).ToCString());
1080         }
1081       }
1082       if (aNewBody) {
1083         theFeature->setResult(aNewBody, aResIndex);
1084       }
1085     }
1086   }
1087 }
1088
1089 Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper)
1090 {
1091   return TDF_LabelMapHasher::HashCode(theLab, theUpper);
1092
1093 }
1094 Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2)
1095 {
1096   return TDF_LabelMapHasher::IsEqual(theLab1, theLab2);
1097 }
1098
1099 void Model_Document::addNamingName(const TDF_Label theLabel, std::string theName)
1100 {
1101   myNamingNames[theName] = theLabel;
1102 }
1103
1104 TDF_Label Model_Document::findNamingName(std::string theName)
1105 {
1106   std::map<std::string, TDF_Label>::iterator aFind = myNamingNames.find(theName);
1107   if (aFind == myNamingNames.end())
1108     return TDF_Label(); // not found
1109   return aFind->second;
1110 }