]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_Document.cpp
Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[modules/shaper.git] / src / Model / Model_Document.cpp
1 // File:        Model_Document.cxx
2 // Created:     28 Feb 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include <Model_Document.h>
6 #include <Model_Data.h>
7 #include <Model_Application.h>
8 #include <Model_PluginManager.h>
9 #include <Model_Events.h>
10 #include <Model_ResultPart.h>
11 #include <Model_ResultConstruction.h>
12 #include <Model_ResultBody.h>
13 #include <Events_Loop.h>
14 #include <Events_Error.h>
15
16 #include <TDataStd_Integer.hxx>
17 #include <TDataStd_Comment.hxx>
18 #include <TDF_ChildIDIterator.hxx>
19 #include <TDataStd_ReferenceArray.hxx>
20 #include <TDataStd_HLabelArray1.hxx>
21 #include <TDataStd_Name.hxx>
22 #include <TDF_Reference.hxx>
23
24 #include <climits>
25 #ifndef WIN32
26 #include <sys/stat.h>
27 #endif
28
29 #ifdef WIN32
30 # define _separator_ '\\'
31 #else
32 # define _separator_ '/'
33 #endif
34
35 static const int UNDO_LIMIT = 10; // number of possible undo operations
36
37 static const int TAG_GENERAL = 1; // general properties tag
38 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree (features, results)
39 static const int TAG_HISTORY = 3; // tag of the history sub-tree (python dump)
40
41 // feature sub-labels
42 static const int TAG_FEATURE_ARGUMENTS = 1; ///< where the arguments are located
43 static const int TAG_FEATURE_RESULTS = 2; ///< where the results are located
44
45 /// Returns the file name of this document by the nameof directory and identifuer of a document
46 static TCollection_ExtendedString DocFileName(const char* theFileName, const std::string& theID)
47 {
48   TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
49   aPath += _separator_;
50   aPath += theID.c_str();
51   aPath += ".cbf"; // standard binary file extension
52   return aPath;
53 }
54
55 bool Model_Document::load(const char* theFileName)
56 {
57   Handle(Model_Application) anApp = Model_Application::getApplication();
58   if (this == Model_PluginManager::get()->rootDocument().get()) {
59     anApp->setLoadPath(theFileName);
60   }
61   TCollection_ExtendedString aPath (DocFileName(theFileName, myID));
62   PCDM_ReaderStatus aStatus = (PCDM_ReaderStatus) -1;
63   try
64   {
65     aStatus = anApp->Open(aPath, myDoc);
66   }
67   catch (Standard_Failure)
68   {
69     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
70     Events_Error::send(std::string("Exception in opening of document: ") + aFail->GetMessageString());
71     return false;
72   }
73   bool isError = aStatus != PCDM_RS_OK;
74   if (isError)
75   {
76     switch (aStatus)
77     {
78     case PCDM_RS_UnknownDocument: 
79       Events_Error::send(std::string("Can not open document: PCDM_RS_UnknownDocument")); break;
80     case PCDM_RS_AlreadyRetrieved: 
81       Events_Error::send(std::string("Can not open document: PCDM_RS_AlreadyRetrieved")); break;
82     case PCDM_RS_AlreadyRetrievedAndModified:
83       Events_Error::send(
84         std::string("Can not open document: PCDM_RS_AlreadyRetrievedAndModified"));
85       break;
86     case PCDM_RS_NoDriver:
87       Events_Error::send(std::string("Can not open document: PCDM_RS_NoDriver")); break;
88     case PCDM_RS_UnknownFileDriver:
89       Events_Error::send(std::string("Can not open document: PCDM_RS_UnknownFileDriver")); break;
90     case PCDM_RS_OpenError:
91       Events_Error::send(std::string("Can not open document: PCDM_RS_OpenError")); break;
92     case PCDM_RS_NoVersion:
93       Events_Error::send(std::string("Can not open document: PCDM_RS_NoVersion")); break;
94     case PCDM_RS_NoModel:
95       Events_Error::send(std::string("Can not open document: PCDM_RS_NoModel")); break;
96     case PCDM_RS_NoDocument:
97       Events_Error::send(std::string("Can not open document: PCDM_RS_NoDocument")); break;
98     case PCDM_RS_FormatFailure:
99       Events_Error::send(std::string("Can not open document: PCDM_RS_FormatFailure")); break;
100     case PCDM_RS_TypeNotFoundInSchema:
101       Events_Error::send(std::string("Can not open document: PCDM_RS_TypeNotFoundInSchema")); 
102       break;
103     case PCDM_RS_UnrecognizedFileFormat:
104       Events_Error::send(std::string("Can not open document: PCDM_RS_UnrecognizedFileFormat")); 
105       break;
106     case PCDM_RS_MakeFailure:
107       Events_Error::send(std::string("Can not open document: PCDM_RS_MakeFailure")); break;
108     case PCDM_RS_PermissionDenied:
109       Events_Error::send(std::string("Can not open document: PCDM_RS_PermissionDenied")); break;
110     case PCDM_RS_DriverFailure:
111       Events_Error::send(std::string("Can not open document: PCDM_RS_DriverFailure")); break;
112     default:
113       Events_Error::send(std::string("Can not open document: unknown error")); break;
114     }
115   }
116   if (!isError) {
117     myDoc->SetUndoLimit(UNDO_LIMIT);
118     synchronizeFeatures();
119   }
120   return !isError;
121 }
122
123 bool Model_Document::save(const char* theFileName)
124 {
125   // create a directory in the root document if it is not yet exist
126   if (this == Model_PluginManager::get()->rootDocument().get()) {
127 #ifdef WIN32
128     CreateDirectory(theFileName, NULL);
129 #else
130     mkdir(theFileName, 0x1ff); 
131 #endif
132   }
133   // filename in the dir is id of document inside of the given directory
134   TCollection_ExtendedString aPath(DocFileName(theFileName, myID));
135   PCDM_StoreStatus aStatus;
136   try {
137     aStatus = Model_Application::getApplication()->SaveAs(myDoc, aPath);
138   }
139   catch (Standard_Failure) {
140     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
141     Events_Error::send(std::string("Exception in saving of document: ") + aFail->GetMessageString());
142     return false;
143   }
144   bool isDone = aStatus == PCDM_SS_OK || aStatus == PCDM_SS_No_Obj;
145   if (!isDone)
146   {
147     switch (aStatus)
148     {
149     case PCDM_SS_DriverFailure:
150       Events_Error::send(std::string("Can not save document: PCDM_SS_DriverFailure"));
151       break;
152     case PCDM_SS_WriteFailure:
153       Events_Error::send(std::string("Can not save document: PCDM_SS_WriteFailure"));
154       break;
155     case PCDM_SS_Failure:
156     default:
157       Events_Error::send(std::string("Can not save document: PCDM_SS_Failure"));
158       break;
159     }
160   }
161   myTransactionsAfterSave = 0;
162   if (isDone) { // save also sub-documents if any
163     std::set<std::string>::iterator aSubIter = mySubs.begin();
164     for(; aSubIter != mySubs.end() && isDone; aSubIter++)
165       isDone = subDocument(*aSubIter)->save(theFileName);
166   }
167   return isDone;
168 }
169
170 void Model_Document::close()
171 {
172   boost::shared_ptr<ModelAPI_PluginManager> aPM = Model_PluginManager::get();
173   if (this != aPM->rootDocument().get() && 
174       this == aPM->currentDocument().get()) {
175     aPM->setCurrentDocument(aPM->rootDocument());
176   }
177   // close all subs
178   std::set<std::string>::iterator aSubIter = mySubs.begin();
179   for(; aSubIter != mySubs.end(); aSubIter++)
180     subDocument(*aSubIter)->close();
181   mySubs.clear();
182   // close this
183   /* do not close because it can be undoed
184   if (myDoc->CanClose() == CDM_CCS_OK)
185     myDoc->Close();
186   Model_Application::getApplication()->deleteDocument(myID);
187   */
188 }
189
190 void Model_Document::startOperation()
191 {
192   if (myDoc->HasOpenCommand()) { // start of nested command
193     if (myNestedNum == -1) {
194       myNestedNum = 0;
195       myDoc->InitDeltaCompaction();
196     }
197     myIsEmptyTr[myTransactionsAfterSave] = false;
198     myTransactionsAfterSave++;
199     myDoc->NewCommand();
200   } else { // start of simple command
201     myDoc->NewCommand();
202   }
203   // new command for all subs
204   std::set<std::string>::iterator aSubIter = mySubs.begin();
205   for(; aSubIter != mySubs.end(); aSubIter++)
206     subDocument(*aSubIter)->startOperation();
207 }
208
209 void Model_Document::compactNested() {
210   while(myNestedNum != -1) {
211     myTransactionsAfterSave--;
212     myIsEmptyTr.erase(myTransactionsAfterSave);
213     myNestedNum--;
214   }
215   myIsEmptyTr[myTransactionsAfterSave] = false;
216   myTransactionsAfterSave++;
217   myDoc->PerformDeltaCompaction();
218 }
219
220 void Model_Document::finishOperation()
221 {
222   // just to be sure that everybody knows that changes were performed
223   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
224   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
225   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
226
227   if (myNestedNum != -1) // this nested transaction is owervritten
228     myNestedNum++;
229   if (!myDoc->HasOpenCommand()) {
230     if (myNestedNum != -1) {
231       myNestedNum--;
232       compactNested();
233     }
234   } else {
235     // returns false if delta is empty and no transaction was made
236     myIsEmptyTr[myTransactionsAfterSave] = !myDoc->CommitCommand() && (myNestedNum == -1);
237     myTransactionsAfterSave++;
238   }
239
240   // finish for all subs
241   std::set<std::string>::iterator aSubIter = mySubs.begin();
242   for(; aSubIter != mySubs.end(); aSubIter++)
243     subDocument(*aSubIter)->finishOperation();
244 }
245
246 void Model_Document::abortOperation()
247 {
248   if (myNestedNum > 0 && !myDoc->HasOpenCommand()) { // abort all what was done in nested
249     // first compact all nested
250     compactNested();
251     // for nested it is undo and clear redos
252     myDoc->Undo();
253     myDoc->ClearRedos();
254     myTransactionsAfterSave--;
255     myIsEmptyTr.erase(myTransactionsAfterSave);
256   } else {
257     if (myNestedNum == 0) // abort only high-level
258       myNestedNum = -1;
259     myDoc->AbortCommand();
260   }
261   synchronizeFeatures(true);
262   // abort for all subs
263   std::set<std::string>::iterator aSubIter = mySubs.begin();
264     for(; aSubIter != mySubs.end(); aSubIter++)
265     subDocument(*aSubIter)->abortOperation();
266 }
267
268 bool Model_Document::isOperation()
269 {
270   // operation is opened for all documents: no need to check subs
271   return myDoc->HasOpenCommand() == Standard_True;
272 }
273
274 bool Model_Document::isModified()
275 {
276   // is modified if at least one operation was commited and not undoed
277   return myTransactionsAfterSave > 0;
278 }
279
280 bool Model_Document::canUndo()
281 {
282   if (myDoc->GetAvailableUndos() > 0 && myNestedNum != 0 && myTransactionsAfterSave != 0 /* for omitting the first useless transaction */)
283     return true;
284   // check other subs contains operation that can be undoed
285   std::set<std::string>::iterator aSubIter = mySubs.begin();
286   for(; aSubIter != mySubs.end(); aSubIter++)
287     if (subDocument(*aSubIter)->canUndo())
288       return true;
289   return false;
290 }
291
292 void Model_Document::undo()
293 {
294   myTransactionsAfterSave--;
295   if (myNestedNum > 0) myNestedNum--;
296   if (!myIsEmptyTr[myTransactionsAfterSave])
297     myDoc->Undo();
298   synchronizeFeatures(true);
299   // undo for all subs
300   std::set<std::string>::iterator aSubIter = mySubs.begin();
301   for(; aSubIter != mySubs.end(); aSubIter++)
302     subDocument(*aSubIter)->undo();
303 }
304
305 bool Model_Document::canRedo()
306 {
307   if (myDoc->GetAvailableRedos() > 0)
308     return true;
309   // check other subs contains operation that can be redoed
310   std::set<std::string>::iterator aSubIter = mySubs.begin();
311   for(; aSubIter != mySubs.end(); aSubIter++)
312     if (subDocument(*aSubIter)->canRedo())
313       return true;
314   return false;
315 }
316
317 void Model_Document::redo()
318 {
319   if (myNestedNum != -1) myNestedNum++;
320   if (!myIsEmptyTr[myTransactionsAfterSave])
321     myDoc->Redo();
322   myTransactionsAfterSave++;
323   synchronizeFeatures(true);
324   // redo for all subs
325   std::set<std::string>::iterator aSubIter = mySubs.begin();
326   for(; aSubIter != mySubs.end(); aSubIter++)
327     subDocument(*aSubIter)->redo();
328 }
329
330 /// Appenad to the array of references a new referenced label
331 static void AddToRefArray(TDF_Label& theArrayLab, TDF_Label& theReferenced) {
332   Handle(TDataStd_ReferenceArray) aRefs;
333   if (!theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
334     aRefs = TDataStd_ReferenceArray::Set(theArrayLab, 0, 0);
335     aRefs->SetValue(0, theReferenced);
336   } else { // extend array by one more element
337     Handle(TDataStd_HLabelArray1) aNewArray = 
338       new TDataStd_HLabelArray1(aRefs->Lower(), aRefs->Upper() + 1);
339     for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
340       aNewArray->SetValue(a, aRefs->Value(a));
341     }
342     aNewArray->SetValue(aRefs->Upper() + 1, theReferenced);
343     aRefs->SetInternalArray(aNewArray);
344   }
345 }
346
347 FeaturePtr Model_Document::addFeature(std::string theID)
348 {
349   TDF_Label anEmptyLab;
350   FeaturePtr anEmptyFeature;
351   FeaturePtr aFeature = ModelAPI_PluginManager::get()->createFeature(theID);
352   if (aFeature) {
353     TDF_Label aFeatureLab;
354     if (!aFeature->isAction()) {// do not add action to the data model
355       TDF_Label aFeaturesLab = groupLabel(ModelAPI_Feature::group());
356       aFeatureLab = aFeaturesLab.NewChild();
357       initData(aFeature, aFeatureLab, TAG_FEATURE_ARGUMENTS);
358       // keep the feature ID to restore document later correctly
359       TDataStd_Comment::Set(aFeatureLab, aFeature->getKind().c_str());
360       setUniqueName(aFeature);
361       myObjs[ModelAPI_Feature::group()].push_back(aFeature);
362       // store feature in the history of features array
363       if (aFeature->isInHistory()) {
364         AddToRefArray(aFeaturesLab, aFeatureLab);
365       }
366     }
367     if (!aFeature->isAction()) {// do not add action to the data model
368       // event: feature is added
369       static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
370       ModelAPI_EventCreator::get()->sendUpdated(aFeature, anEvent);
371     }
372   }
373   return aFeature;
374 }
375
376 /// Appenad to the array of references a new referenced label.
377 /// If theIndex is not -1, removes element at thisindex, not theReferenced.
378 /// \returns the index of removed element
379 static int RemoveFromRefArray(
380   TDF_Label theArrayLab, TDF_Label theReferenced, const int theIndex = -1) {
381   int aResult = -1; // no returned
382   Handle(TDataStd_ReferenceArray) aRefs;
383   if (theArrayLab.FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
384     if (aRefs->Length() == 1) { // just erase an array
385       if ((theIndex == -1 && aRefs->Value(0) == theReferenced) || theIndex == 0) {
386         theArrayLab.ForgetAttribute(TDataStd_ReferenceArray::GetID());
387       }
388       aResult = 0;
389     } else { // reduce the array
390       Handle(TDataStd_HLabelArray1) aNewArray = 
391         new TDataStd_HLabelArray1(aRefs->Lower(), aRefs->Upper() - 1);
392       int aCount = aRefs->Lower();
393       for(int a = aCount; a <= aRefs->Upper(); a++, aCount++) {
394         if ((theIndex == -1 && aRefs->Value(a) == theReferenced) || theIndex == a) {
395           aCount--;
396           aResult = a;
397         } else {
398           aNewArray->SetValue(aCount, aRefs->Value(a));
399         }
400       }
401       aRefs->SetInternalArray(aNewArray);
402     }
403   }
404   return aResult;
405 }
406
407 void Model_Document::removeFeature(FeaturePtr theFeature)
408 {
409   boost::shared_ptr<Model_Data> aData = boost::static_pointer_cast<Model_Data>(theFeature->data());
410   TDF_Label aFeatureLabel = aData->label().Father();
411   // remove feature from the myObjects list
412   std::vector<ObjectPtr>& aVec = myObjs[ModelAPI_Feature::group()];
413   std::vector<ObjectPtr>::iterator anIter = aVec.begin();
414   while(anIter != aVec.end()) {
415     if (*anIter == theFeature) {
416       anIter = aVec.erase(anIter);
417     } else {
418       anIter++;
419     }
420   }
421   // erase all attributes under the label of feature
422   aFeatureLabel.ForgetAllAttributes();
423   // remove it from the references array
424   RemoveFromRefArray(groupLabel(ModelAPI_Feature::group()), aData->label());
425
426   // event: feature is deleted
427   ModelAPI_EventCreator::get()->sendDeleted(theFeature->document(), ModelAPI_Feature::group());
428 }
429
430 /// returns the object group name by the object label
431 static std::string groupName(TDF_Label theObjectLabel) {
432   TDF_Label aGroupLab = theObjectLabel.Father();
433   Handle(TDataStd_Comment) aComment;
434   if (aGroupLab.FindAttribute(TDataStd_Comment::GetID(), aComment))
435     return std::string(TCollection_AsciiString(aComment->Get()).ToCString());
436   return ""; // not found
437 }
438
439 FeaturePtr Model_Document::feature(TDF_Label& theLabel)
440 {
441   // iterate all features, may be optimized later by keeping labels-map
442   std::vector<ObjectPtr>& aVec = myObjs[ModelAPI_Feature::group()];
443   std::vector<ObjectPtr>::iterator aFIter = aVec.begin();
444   for(; aFIter != aVec.end(); aFIter++) {
445     boost::shared_ptr<Model_Data> aData = 
446       boost::dynamic_pointer_cast<Model_Data>((*aFIter)->data());
447     if (aData->label().IsEqual(theLabel))
448       return boost::dynamic_pointer_cast<ModelAPI_Feature>(*aFIter);
449   }
450   return FeaturePtr(); // not found
451 }
452
453 ObjectPtr Model_Document::object(TDF_Label& theLabel)
454 {
455   // iterate all features, may be optimized later by keeping labels-map
456   std::vector<ObjectPtr>& aVec = myObjs[ModelAPI_Feature::group()];
457   std::vector<ObjectPtr>::iterator aFIter = aVec.begin();
458   for(; aFIter != aVec.end(); aFIter++) {
459     boost::shared_ptr<Model_Data> aData = 
460       boost::dynamic_pointer_cast<Model_Data>((*aFIter)->data());
461     if (aData->label().IsEqual(theLabel))
462       return *aFIter;
463     std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = 
464       boost::dynamic_pointer_cast<ModelAPI_Feature>(*aFIter)->results();
465     std::list<boost::shared_ptr<ModelAPI_Result> >::iterator aRIter = aResults.begin();
466     for(; aRIter != aResults.end(); aRIter++) {
467       boost::shared_ptr<Model_Data> aResData = 
468         boost::dynamic_pointer_cast<Model_Data>((*aRIter)->data());
469       if (aResData->label().IsEqual(theLabel))
470         return *aRIter;
471     }
472   }
473   return FeaturePtr(); // not found
474 }
475
476 boost::shared_ptr<ModelAPI_Document> Model_Document::subDocument(std::string theDocID)
477 {
478   // just store sub-document identifier here to manage it later
479   if (mySubs.find(theDocID) == mySubs.end())
480     mySubs.insert(theDocID);
481   return Model_Application::getApplication()->getDocument(theDocID);
482 }
483
484 ObjectPtr Model_Document::object(const std::string& theGroupID, const int theIndex)
485 {
486   if (theGroupID == ModelAPI_Feature::group()) {
487     // features may be not in history but in the myObjs, so, iterate all
488     int anIndex = 0;
489     std::map<std::string, std::vector<ObjectPtr> >::iterator aFind = 
490       myObjs.find(ModelAPI_Feature::group());
491     if (aFind != myObjs.end()) {
492       std::vector<ObjectPtr>::iterator aFIter = aFind->second.begin();
493       for(; aFIter != aFind->second.end(); aFIter++) {
494         if ((*aFIter)->isInHistory()) {
495           if (theIndex == anIndex)
496             return *aFIter;
497           anIndex++;
498         }
499       }
500     }
501   } else {
502     // iterate all features in order to find the needed result
503     std::map<std::string, std::vector<ObjectPtr> >::iterator aFind = 
504       myObjs.find(ModelAPI_Feature::group());
505     if (aFind != myObjs.end()) {
506       std::vector<ObjectPtr>::iterator aFIter = aFind->second.begin();
507       for(int anIndex = 0; aFIter != aFind->second.end(); aFIter++) {
508         const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = 
509           boost::dynamic_pointer_cast<ModelAPI_Feature>(*aFIter)->results();
510         std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
511         for(; aRIter != aResults.cend(); aRIter++) {
512           if ((*aRIter)->isInHistory() && (*aRIter)->groupName() == theGroupID) {
513             if (anIndex == theIndex)
514               return *aRIter;
515             anIndex++;
516           }
517         }
518       }
519     }
520   }
521   // not found
522   return ObjectPtr();
523 }
524
525 int Model_Document::size(const std::string& theGroupID) 
526 {
527   int aResult = 0;
528   if (theGroupID == ModelAPI_Feature::group()) {
529     // features may be not in history but in the myObjs, so, iterate all
530     std::map<std::string, std::vector<ObjectPtr> >::iterator aFind = 
531       myObjs.find(ModelAPI_Feature::group());
532     if (aFind != myObjs.end()) {
533       std::vector<ObjectPtr>::iterator aFIter = aFind->second.begin();
534       for(; aFIter != aFind->second.end(); aFIter++) {
535         if ((*aFIter)->isInHistory()) {
536           aResult++;
537         }
538       }
539     }
540   } else {
541     // iterate all features in order to find the needed result
542     std::map<std::string, std::vector<ObjectPtr> >::iterator aFind = 
543       myObjs.find(ModelAPI_Feature::group());
544     if (aFind != myObjs.end()) {
545       std::vector<ObjectPtr>::iterator aFIter = aFind->second.begin();
546       for(; aFIter != aFind->second.end(); aFIter++) {
547         const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = 
548           boost::dynamic_pointer_cast<ModelAPI_Feature>(*aFIter)->results();
549         std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
550         for(; aRIter != aResults.cend(); aRIter++) {
551           if ((*aRIter)->isInHistory() && (*aRIter)->groupName() == theGroupID) {
552             aResult++;
553           }
554         }
555       }
556     }
557   }
558   // group is not found
559   return aResult;
560 }
561
562 Model_Document::Model_Document(const std::string theID)
563     : myID(theID), myDoc(new TDocStd_Document("BinOcaf")) // binary OCAF format
564 {
565   myDoc->SetUndoLimit(UNDO_LIMIT);
566   myTransactionsAfterSave = 0;
567   myNestedNum = -1;
568   //myDoc->SetNestedTransactionMode();
569   // to have something in the document and avoid empty doc open/save problem
570   // in transaction for nesting correct working
571   myDoc->NewCommand();
572   TDataStd_Integer::Set(myDoc->Main().Father(), 0);
573   myDoc->CommitCommand();
574 }
575
576 TDF_Label Model_Document::groupLabel(const std::string theGroup)
577 {
578   // searching for existing
579   TCollection_ExtendedString aGroup(theGroup.c_str());
580   TDF_ChildIDIterator aGroupIter(myDoc->Main().FindChild(TAG_OBJECTS), TDataStd_Comment::GetID());
581   for(; aGroupIter.More(); aGroupIter.Next()) {
582     Handle(TDataStd_Comment) aName = Handle(TDataStd_Comment)::DownCast(aGroupIter.Value());
583     if (aName->Get() == aGroup)
584       return aGroupIter.Value()->Label();
585   }
586   // create a new
587   TDF_Label aNew = myDoc->Main().FindChild(TAG_OBJECTS).NewChild();
588   TDataStd_Comment::Set(aNew, aGroup);
589   return aNew;
590 }
591
592 void Model_Document::setUniqueName(FeaturePtr theFeature)
593 {
594   std::string aName; // result
595   // first count all objects of such kind to start with index = count + 1
596   int a, aNumObjects = 0;
597   int aSize = size(ModelAPI_Feature::group());
598   for(a = 0; a < aSize; a++) {
599     if (boost::dynamic_pointer_cast<ModelAPI_Feature>(object(ModelAPI_Feature::group(), a))->
600         getKind() == theFeature->getKind())
601       aNumObjects++;
602   }
603   // generate candidate name
604   std::stringstream aNameStream;
605   aNameStream<<theFeature->getKind()<<"_"<<aNumObjects + 1;
606   aName = aNameStream.str();
607   // check this is unique, if not, increase index by 1
608   for(a = 0; a < aSize;) {
609     if (boost::dynamic_pointer_cast<ModelAPI_Feature>(object(ModelAPI_Feature::group(), a))
610           ->data()->name() == aName) {
611       aNumObjects++;
612       std::stringstream aNameStream;
613       aNameStream<<theFeature->getKind()<<"_"<<aNumObjects + 1;
614       aName = aNameStream.str();
615       // reinitialize iterator to make sure a new name is unique
616       a = 0;
617     } else a++;
618   }
619   theFeature->data()->setName(aName);
620 }
621
622 void Model_Document::initData(ObjectPtr theObj, TDF_Label& theLab, const int theTag) {
623   boost::shared_ptr<ModelAPI_Document> aThis = 
624     Model_Application::getApplication()->getDocument(myID);
625   boost::shared_ptr<Model_Data> aData(new Model_Data);
626   aData->setLabel(theLab.FindChild(theTag + 1));
627   aData->setObject(theObj);
628   theObj->setDoc(aThis);
629   theObj->setData(aData);
630   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
631   if (aFeature) aFeature->initAttributes();
632 }
633
634 void Model_Document::synchronizeFeatures(const bool theMarkUpdated)
635 {
636   boost::shared_ptr<ModelAPI_Document> aThis = 
637     Model_Application::getApplication()->getDocument(myID);
638   // update all objects: iterate from the end: as they appeared in the list
639   std::map<std::string, std::vector<ObjectPtr> >::reverse_iterator aGroupIter = myObjs.rbegin();
640   for(; aGroupIter != myObjs.rend(); aGroupIter++) {
641     std::vector<ObjectPtr>::iterator anObjIter = aGroupIter->second.begin();
642     // and in parallel iterate labels of features
643     const std::string& aGroupName = aGroupIter->first;
644     TDF_ChildIDIterator aLabIter(groupLabel(aGroupName), TDataStd_Comment::GetID());
645     while(anObjIter != aGroupIter->second.end() || aLabIter.More()) {
646       static const int INFINITE_TAG = INT_MAX; // no label means that it exists somwhere in infinite
647       int aFeatureTag = INFINITE_TAG; 
648       if (anObjIter != aGroupIter->second.end()) { // existing tag for feature
649         boost::shared_ptr<Model_Data> aData = 
650           boost::dynamic_pointer_cast<Model_Data>((*anObjIter)->data());
651         aFeatureTag = aData->label().Tag();
652       }
653       int aDSTag = INFINITE_TAG; 
654       if (aLabIter.More()) { // next label in DS is existing
655         aDSTag = aLabIter.Value()->Label().Tag();
656       }
657       if (aDSTag > aFeatureTag) { // feature is removed
658         ObjectPtr anObj = *anObjIter;
659         anObjIter = aGroupIter->second.erase(anObjIter);
660         // event: model is updated
661         if (anObj->isInHistory()) {
662           ModelAPI_EventCreator::get()->sendDeleted(aThis, aGroupName);
663         }
664         ModelAPI_EventCreator::get()->sendDeleted(aThis, aGroupName);
665       } else if (aDSTag < aFeatureTag) { // a new feature is inserted
666         // create a feature
667         TDF_Label aLab = aLabIter.Value()->Label();
668         ObjectPtr aNewObj = ModelAPI_PluginManager::get()->createFeature(
669           TCollection_AsciiString(Handle(TDataStd_Comment)::DownCast(aLabIter.Value())->Get())
670           .ToCString());
671         // this must be before "setData" to redo the sketch line correctly
672         if (anObjIter == aGroupIter->second.end()) {
673           aGroupIter->second.push_back(aNewObj);
674           anObjIter = aGroupIter->second.end();
675         } else {
676           anObjIter++;
677           aGroupIter->second.insert(anObjIter, aNewObj);
678         }
679         initData(aNewObj, aLab, TAG_FEATURE_ARGUMENTS);
680
681         // event: model is updated
682         static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
683         ModelAPI_EventCreator::get()->sendUpdated(aNewObj, anEvent);
684         // feature for this label is added, so go to the next label
685         aLabIter.Next();
686       } else { // nothing is changed, both iterators are incremented
687         if (theMarkUpdated) {
688           static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
689           ModelAPI_EventCreator::get()->sendUpdated(*anObjIter, anEvent);
690         }
691         anObjIter++;
692         aLabIter.Next();
693       }
694     }
695   }
696   // after all updates, sends a message that groups of features were created or updated
697   boost::static_pointer_cast<Model_PluginManager>(Model_PluginManager::get())->
698     setCheckTransactions(false);
699   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
700   if (theMarkUpdated)
701     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
702   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
703   boost::static_pointer_cast<Model_PluginManager>(Model_PluginManager::get())->
704     setCheckTransactions(true);
705 }
706
707 void Model_Document::storeResult(boost::shared_ptr<ModelAPI_Data> theFeatureData,
708   boost::shared_ptr<ModelAPI_Result> theResult, const int theResultIndex)
709 {
710   boost::shared_ptr<ModelAPI_Document> aThis = 
711     Model_Application::getApplication()->getDocument(myID);
712   theResult->setDoc(aThis);
713   initData(theResult, boost::dynamic_pointer_cast<Model_Data>(theFeatureData)->
714     label().Father().FindChild(TAG_FEATURE_RESULTS), theResultIndex);
715   if (theResult->data()->name().empty()) { // if was not initialized, generate event and set a name
716     static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
717     ModelAPI_EventCreator::get()->sendUpdated(theResult, anEvent);
718     theResult->data()->setName(theFeatureData->name());
719   }
720 }
721
722 boost::shared_ptr<ModelAPI_ResultConstruction> Model_Document::createConstruction(
723   const boost::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
724 {
725   boost::shared_ptr<ModelAPI_ResultConstruction> aResult(new Model_ResultConstruction());
726   storeResult(theFeatureData, aResult);
727   return aResult;
728 }
729
730 boost::shared_ptr<ModelAPI_ResultBody> Model_Document::createBody(
731   const boost::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
732 {
733   boost::shared_ptr<ModelAPI_ResultBody> aResult(new Model_ResultBody());
734   storeResult(theFeatureData, aResult);
735   return aResult;
736 }
737
738 boost::shared_ptr<ModelAPI_ResultPart> Model_Document::createPart(
739   const boost::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex)
740 {
741   boost::shared_ptr<ModelAPI_ResultPart> aResult(new Model_ResultPart());
742   storeResult(theFeatureData, aResult);
743   return aResult;
744 }
745
746 boost::shared_ptr<ModelAPI_Feature> Model_Document::feature(
747   const boost::shared_ptr<ModelAPI_Result>& theResult)
748 {
749   // iterate all features in order to find the needed result
750   std::map<std::string, std::vector<ObjectPtr> >::iterator aFind = 
751     myObjs.find(ModelAPI_Feature::group());
752   if (aFind != myObjs.end()) {
753     std::vector<ObjectPtr>::iterator aFIter = aFind->second.begin();
754     for(; aFIter != aFind->second.end(); aFIter++) {
755       FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(*aFIter);
756       const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = aFeature->results();
757       std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
758       for(; aRIter != aResults.cend(); aRIter++) {
759         if (*aRIter == theResult) {
760           return aFeature;
761         }
762       }
763     }
764   }
765   return FeaturePtr();
766 }