Salome HOME
Issue #3044: Undo list contains empty string for Load python script
[modules/shaper.git] / src / Model / Model_Session.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <Model_Session.h>
21 #include <ModelAPI_Feature.h>
22 #include <ModelAPI_Plugin.h>
23 #include <Model_Data.h>
24 #include <Model_Document.h>
25 #include <Model_Objects.h>
26 #include <Model_Application.h>
27 #include <Model_Events.h>
28 #include <Model_Validator.h>
29 #include <Model_FiltersFactory.h>
30 #include <ModelAPI_Events.h>
31 #include <Events_Loop.h>
32 #include <Events_InfoMessage.h>
33 #include <Config_FeatureMessage.h>
34 #include <Config_AttributeMessage.h>
35 #include <Config_ValidatorMessage.h>
36 #include <Config_ModuleReader.h>
37 #include <Config_ValidatorReader.h>
38 #include <Config_PluginMessage.h>
39
40 #include <ModelAPI_CompositeFeature.h>
41 #include <ModelAPI_ResultPart.h>
42 #include <ModelAPI_Tools.h>
43
44 #include <TDF_ChildIDIterator.hxx>
45 #include <TDF_CopyTool.hxx>
46 #include <TDF_DataSet.hxx>
47 #include <TDF_RelocationTable.hxx>
48 #include <TDF_ClosureTool.hxx>
49
50 #include <TNaming_Builder.hxx>
51 #include <TNaming_Iterator.hxx>
52 #include <TNaming_NamedShape.hxx>
53
54 #include <TopoDS_Shape.hxx>
55
56 static Model_Session* myImpl = new Model_Session();
57
58 // to redirect all calls to the root document
59 #define ROOT_DOC std::dynamic_pointer_cast<Model_Document>(moduleDocument())
60
61 bool Model_Session::load(const char* theFileName)
62 {
63   bool aRes = ROOT_DOC->load(theFileName, "root", ROOT_DOC);
64   return aRes;
65 }
66
67 bool Model_Session::save(const char* theFileName, std::list<std::string>& theResults)
68 {
69   return ROOT_DOC->save(theFileName, "root", theResults);
70 }
71
72 void Model_Session::closeAll()
73 {
74   Model_Application::getApplication()->deleteAllDocuments();
75   static const Events_ID aDocChangeEvent = Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED);
76   static std::shared_ptr<Events_Message> aMsg(new Events_Message(aDocChangeEvent));
77   Events_Loop::loop()->send(aMsg);
78   Events_Loop::loop()->flush(aDocChangeEvent);
79 }
80
81 void Model_Session::startOperation(const std::string& theId, const bool theAttachedToNested)
82 {
83   myOperationAttachedToNext.push_back(theAttachedToNested);
84   ROOT_DOC->startOperation();
85   ROOT_DOC->operationId(theId);
86   static std::shared_ptr<Events_Message> aStartedMsg
87     (new Events_Message(Events_Loop::eventByName("StartOperation")));
88   Events_Loop::loop()->send(aStartedMsg);
89   // remove all useless documents that has been closed: on start of operation undo/redo is cleared
90   // MPV: this code is dangerous now because it may close the document that is activated right now
91   // but not in the list of the opened documents yet (create, delete, undo, activate Part)
92   // later this must be updated by correct usage of uniques IDs of documents, not names of results
93   //std::list<std::shared_ptr<ModelAPI_Document> > aUsedDocs = allOpenedDocuments();
94   //Model_Application::getApplication()->removeUselessDocuments(aUsedDocs);
95 }
96
97 void Model_Session::finishOperation()
98 {
99   setCheckTransactions(false);
100   ROOT_DOC->finishOperation();
101   if (!myOperationAttachedToNext.empty()) {
102     while (myOperationAttachedToNext.back()) {
103       // with nested, the first transaction can not be attached
104       ROOT_DOC->finishOperation();
105       myOperationAttachedToNext.pop_back();
106     }
107     myOperationAttachedToNext.pop_back();
108   }
109   setCheckTransactions(true);
110 }
111
112 void Model_Session::abortOperation()
113 {
114   setCheckTransactions(false);
115   ROOT_DOC->abortOperation();
116   if (!myOperationAttachedToNext.empty()) {
117     while (myOperationAttachedToNext.back()) {
118       // with nested, the first transaction can not be attached
119       ROOT_DOC->abortOperation();
120       myOperationAttachedToNext.pop_back();
121     }
122     myOperationAttachedToNext.pop_back();
123   }
124   setCheckTransactions(true);
125   // here the update mechanism may work after abort, so, suppress the warnings about
126   // modifications outside of the transactions
127   bool aWasCheck = myCheckTransactions;
128   myCheckTransactions = false;
129   static std::shared_ptr<Events_Message> anAbortMsg
130     (new Events_Message(Events_Loop::eventByName("AbortOperation")));
131   Events_Loop::loop()->send(anAbortMsg);
132   myCheckTransactions = true;
133   myCheckTransactions = aWasCheck;
134 }
135
136 bool Model_Session::isOperation()
137 {
138   return ROOT_DOC->isOperation();
139 }
140
141 bool Model_Session::isModified()
142 {
143   return ROOT_DOC->isModified();
144 }
145
146 void Model_Session::clearUndos()
147 {
148   ROOT_DOC->clearUndos();
149 }
150
151 bool Model_Session::canUndo()
152 {
153   return ROOT_DOC->canUndo();
154 }
155
156 void Model_Session::undo()
157 {
158   setCheckTransactions(false);
159   ROOT_DOC->undo();
160   setCheckTransactions(true);
161 }
162
163 bool Model_Session::canRedo()
164 {
165   return ROOT_DOC->canRedo();
166 }
167
168 void Model_Session::redo()
169 {
170   setCheckTransactions(false);
171   ROOT_DOC->redo();
172   setCheckTransactions(true);
173 }
174
175 //! Returns stack of performed operations
176 std::list<std::string> Model_Session::undoList()
177 {
178   return ROOT_DOC->undoList();
179 }
180 //! Returns stack of rolled back operations
181 std::list<std::string> Model_Session::redoList()
182 {
183   return ROOT_DOC->redoList();
184 }
185
186 ModelAPI_Plugin* Model_Session::getPlugin(const std::string& thePluginName)
187 {
188   if (myPluginObjs.find(thePluginName) == myPluginObjs.end()) {
189     // before load the used plugins
190     if (myUsePlugins.find(thePluginName) != myUsePlugins.end()) {
191       std::string aUse = myUsePlugins[thePluginName];
192       std::stringstream aUseStream(aUse);
193       std::string aPluginName;
194       while (std::getline(aUseStream, aPluginName, ',')) {
195         if (myPluginObjs.find(aPluginName) == myPluginObjs.end())
196           getPlugin(aPluginName);
197       }
198     }
199     // load plugin library if not yet done
200     Config_ModuleReader::loadPlugin(thePluginName);
201   }
202   if (myPluginObjs.find(thePluginName) == myPluginObjs.end()) {
203     Events_InfoMessage("Model_Session", "Can not load plugin '%1'").arg(thePluginName).send();
204     return NULL;
205   }
206   return myPluginObjs[thePluginName];
207 }
208
209 FeaturePtr Model_Session::createFeature(std::string theFeatureID, Model_Document* theDocOwner)
210 {
211   if (this != myImpl) {
212     return myImpl->createFeature(theFeatureID, theDocOwner);
213   }
214
215   // load all information about plugins, features and attributes
216   LoadPluginsInfo();
217
218   if (myPlugins.find(theFeatureID) != myPlugins.end()) {
219     std::pair<std::string, std::string>& aPlugin = myPlugins[theFeatureID]; // plugin and doc kind
220     if (!aPlugin.second.empty() && aPlugin.second != theDocOwner->kind()) {
221       Events_InfoMessage("Model_Session",
222         "Feature '%1' can be created only in document '%2' by the XML definition")
223         .arg(theFeatureID).arg(aPlugin.second).send();
224       return FeaturePtr();
225     }
226     ModelAPI_Plugin* aPluginObj = getPlugin(aPlugin.first);
227     if (aPluginObj) {
228       FeaturePtr aCreated = aPluginObj->createFeature(theFeatureID);
229       if (!aCreated) {
230         Events_InfoMessage("Model_Session", "Can not initialize feature '%1' in plugin '%2'")
231           .arg(theFeatureID).arg(aPlugin.first).send();
232       }
233       return aCreated;
234     } else {
235       Events_InfoMessage("Model_Session", "Can not load plugin '%1'").arg(aPlugin.first).send();
236     }
237   } else {
238     Events_InfoMessage("Model_Session",
239       "Feature '%1' not found in any plugin").arg(theFeatureID).send();
240   }
241
242   return FeaturePtr();  // return nothing
243 }
244
245 std::shared_ptr<ModelAPI_Document> Model_Session::moduleDocument()
246 {
247   Handle(Model_Application) anApp = Model_Application::getApplication();
248   bool aFirstCall = !anApp->hasRoot();
249   if (aFirstCall) {
250     // to be sure that plugins are loaded,
251     // even before the first "createFeature" call (in unit tests)
252
253     LoadPluginsInfo();
254     // creation of the root document is always outside of the transaction, so, avoid checking it
255     setCheckTransactions(false);
256     anApp->createDocument(0); // 0 is a root ID
257     // creation of the root document is always outside of the transaction, so, avoid checking it
258     setCheckTransactions(true);
259   }
260   return anApp->rootDocument();
261 }
262
263 std::shared_ptr<ModelAPI_Document> Model_Session::document(int theDocID)
264 {
265   return std::shared_ptr<ModelAPI_Document>(
266       Model_Application::getApplication()->document(theDocID));
267 }
268
269 bool Model_Session::hasModuleDocument()
270 {
271   return Model_Application::getApplication()->hasRoot();
272 }
273
274 std::shared_ptr<ModelAPI_Document> Model_Session::activeDocument()
275 {
276   if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
277     myCurrentDoc = moduleDocument();
278   return myCurrentDoc;
279 }
280
281 /// makes the last feature in the document as the current
282 static void makeCurrentLast(std::shared_ptr<ModelAPI_Document> theDoc) {
283   if (theDoc.get()) {
284     FeaturePtr aLast = std::dynamic_pointer_cast<Model_Document>(theDoc)->lastFeature();
285     // if last is nested into something else, make this something else as last:
286     // otherwise it will look like edition of sub-element, so, the main will be disabled
287     if (aLast.get()) {
288       CompositeFeaturePtr aMain = ModelAPI_Tools::compositeOwner(aLast);
289       while(aMain.get()) {
290         aLast = aMain;
291         aMain = ModelAPI_Tools::compositeOwner(aLast);
292       }
293     }
294     theDoc->setCurrentFeature(aLast, false);
295   }
296 }
297
298 void Model_Session::setActiveDocument(
299   std::shared_ptr<ModelAPI_Document> theDoc, bool theSendSignal)
300 {
301   if (myCurrentDoc != theDoc) {
302     if (myCurrentDoc.get())
303       myCurrentDoc->setActive(false);
304     if (theDoc.get())
305       theDoc->setActive(true);
306
307     std::shared_ptr<ModelAPI_Document> aPrevious = myCurrentDoc;
308     myCurrentDoc = theDoc;
309     if (theDoc.get() && theSendSignal) {
310       // this must be before the synchronization call because features in PartSet lower than this
311       // part feature must be disabled and don't recomputed anymore (issue 1156,
312       // translation feature is failed on activation of Part 2)
313       if (isOperation()) { // do it only in transaction, not on opening of document
314         DocumentPtr aRoot = moduleDocument();
315         if (myCurrentDoc != aRoot) {
316           FeaturePtr aPartFeat = ModelAPI_Tools::findPartFeature(aRoot, myCurrentDoc);
317           if (aPartFeat.get()) {
318             aRoot->setCurrentFeature(aPartFeat, false);
319           }
320         }
321       }
322       // synchronize the document: it may be just opened or opened but removed before
323       std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(theDoc);
324       if (aDoc.get()) {
325         bool aWasChecked = myCheckTransactions;
326         setCheckTransactions(false);
327         TDF_LabelList anEmptyUpdated;
328         aDoc->objects()->synchronizeFeatures(anEmptyUpdated, true, true, false, true);
329         if (aWasChecked)
330             setCheckTransactions(true);
331       }
332       static std::shared_ptr<Events_Message> aMsg(
333           new Events_Message(Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED)));
334       Events_Loop::loop()->send(aMsg);
335     }
336     // make the current state correct and synchronized in the module and sub-documents
337     if (isOperation()) { // do it only in transaction, not on opening of document
338       if (myCurrentDoc == moduleDocument()) {
339         // make the current feature the latest in root, in previous root current become also last
340         makeCurrentLast(aPrevious);
341         makeCurrentLast(myCurrentDoc);
342       } else {
343         // make the current feature the latest in sub, root current feature becomes this sub
344         makeCurrentLast(myCurrentDoc);
345       }
346     }
347   }
348 }
349
350 std::list<std::shared_ptr<ModelAPI_Document> > Model_Session::allOpenedDocuments()
351 {
352   std::list<std::shared_ptr<ModelAPI_Document> > aResult;
353   aResult.push_back(moduleDocument());
354   // add subs recursively
355   std::list<std::shared_ptr<ModelAPI_Document> >::iterator aDoc = aResult.begin();
356   for(; aDoc != aResult.end(); aDoc++) {
357     DocumentPtr anAPIDoc = *aDoc;
358     std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(anAPIDoc);
359     if (aDoc) {
360       const std::set<int> aSubs = aDoc->subDocuments();
361       std::set<int>::const_iterator aSubIter = aSubs.cbegin();
362       for(; aSubIter != aSubs.cend(); aSubIter++) {
363         aResult.push_back(Model_Application::getApplication()->document(*aSubIter));
364       }
365     }
366   }
367   return aResult;
368 }
369
370 bool Model_Session::isLoadByDemand(const std::string theDocID, const int theDocIndex)
371 {
372   return Model_Application::getApplication()->isLoadByDemand(theDocID, theDocIndex);
373 }
374
375 std::shared_ptr<ModelAPI_Document> Model_Session::copy(
376     std::shared_ptr<ModelAPI_Document> theSource, const int theDestID)
377 {
378   std::shared_ptr<Model_Document> aNew = Model_Application::getApplication()->document(theDestID);
379   // make a copy of all labels
380   TDF_Label aSourceRoot = std::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main()
381       .Father();
382   TDF_Label aTargetRoot = aNew->document()->Main().Father();
383   Handle(TDF_DataSet) aDS = new TDF_DataSet;
384   aDS->AddLabel(aSourceRoot);
385   TDF_ClosureTool::Closure(aDS);
386   Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable(Standard_True);
387   aRT->SetRelocation(aSourceRoot, aTargetRoot);
388   TDF_CopyTool::Copy(aDS, aRT);
389
390   // TODO: remove after fix in OCCT.
391   // All named shapes are stored in reversed order, so to fix this we reverse them back.
392   for(TDF_ChildIDIterator aChildIter(aTargetRoot, TNaming_NamedShape::GetID(), true);
393       aChildIter.More();
394       aChildIter.Next()) {
395     Handle(TNaming_NamedShape) aNamedShape =
396       Handle(TNaming_NamedShape)::DownCast(aChildIter.Value());
397     if (aNamedShape.IsNull()) {
398       continue;
399     }
400
401     TopoDS_Shape aShape = aNamedShape->Get();
402     if(aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND) {
403       continue;
404     }
405
406     TNaming_Evolution anEvol = aNamedShape->Evolution();
407     std::list<std::pair<TopoDS_Shape, TopoDS_Shape> > aShapePairs; // to store old and new shapes
408     for(TNaming_Iterator anIter(aNamedShape); anIter.More(); anIter.Next()) {
409       aShapePairs.push_back(
410         std::pair<TopoDS_Shape, TopoDS_Shape>(anIter.OldShape(), anIter.NewShape()));
411     }
412
413     // Add in reverse order.
414     TDF_Label aLabel = aNamedShape->Label();
415     TNaming_Builder aBuilder(aLabel);
416     for(std::list<std::pair<TopoDS_Shape, TopoDS_Shape> >::iterator aPairsIter =
417           aShapePairs.begin();
418         aPairsIter != aShapePairs.end();
419         aPairsIter++) {
420       if (anEvol == TNaming_GENERATED) {
421         aBuilder.Generated(aPairsIter->first, aPairsIter->second);
422       } else if (anEvol == TNaming_MODIFY) {
423         aBuilder.Modify(aPairsIter->first, aPairsIter->second);
424       } else if (anEvol == TNaming_DELETE) {
425         aBuilder.Delete(aPairsIter->first);
426       } else if (anEvol == TNaming_PRIMITIVE) {
427         aBuilder.Generated(aPairsIter->second);
428       } else if (anEvol == TNaming_SELECTED) {
429         aBuilder.Select(aPairsIter->second, aPairsIter->first);
430       }
431     }
432   }
433
434   TDF_LabelList anEmptyUpdated;
435   aNew->objects()->synchronizeFeatures(anEmptyUpdated, true, true, true, true);
436   return aNew;
437 }
438
439 Model_Session::Model_Session()
440 {
441   myPluginsInfoLoaded = false;
442   myCheckTransactions = true;
443   ModelAPI_Session::setSession(std::shared_ptr<ModelAPI_Session>(this));
444   // register the configuration reading listener
445   Events_Loop* aLoop = Events_Loop::loop();
446   static const Events_ID kFeatureEvent =
447     Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
448   aLoop->registerListener(this, kFeatureEvent);
449   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED), 0, true);
450   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED), 0, true);
451   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED), 0, true);
452   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_VALIDATOR_LOADED));
453   aLoop->registerListener(this, Events_Loop::eventByName(Config_PluginMessage::EVENT_ID()));
454 }
455
456 void Model_Session::processEvent(const std::shared_ptr<Events_Message>& theMessage)
457 {
458   static const Events_ID kFeatureEvent =
459     Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
460   static const Events_ID kValidatorEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
461   static const Events_ID kPluginEvent = Events_Loop::eventByName(Config_PluginMessage::EVENT_ID());
462   if (theMessage->eventID() == kFeatureEvent) {
463     const std::shared_ptr<Config_FeatureMessage> aMsg =
464       std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
465     if (aMsg) {
466
467       // process the plugin info, load plugin
468       if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
469         myPlugins[aMsg->id()] = std::pair<std::string, std::string>(
470           aMsg->pluginLibrary(), aMsg->documentKind());
471       }
472     } else {
473       const std::shared_ptr<Config_AttributeMessage> aMsgAttr =
474         std::dynamic_pointer_cast<Config_AttributeMessage>(theMessage);
475       if (aMsgAttr) {
476
477         if (!aMsgAttr->isObligatory()) {
478           validators()->registerNotObligatory(aMsgAttr->featureId(), aMsgAttr->attributeId());
479         }
480         if(aMsgAttr->isConcealment()) {
481           validators()->registerConcealment(aMsgAttr->featureId(), aMsgAttr->attributeId());
482         }
483         if(aMsgAttr->isMainArgument()) {
484           validators()->registerMainArgument(aMsgAttr->featureId(), aMsgAttr->attributeId());
485         }
486         const std::list<std::pair<std::string, std::string> >& aCases = aMsgAttr->getCases();
487         if (!aCases.empty()) {
488           validators()->registerCase(aMsgAttr->featureId(), aMsgAttr->attributeId(), aCases);
489         }
490         if (aMsgAttr->isGeometricalSelection()) {
491           validators()->registerGeometricalSelection(aMsgAttr->featureId(),
492                                                      aMsgAttr->attributeId());
493         }
494       }
495     }
496     // plugins information was started to load, so, it will be loaded
497     myPluginsInfoLoaded = true;
498   } else if (theMessage->eventID() == kValidatorEvent) {
499     std::shared_ptr<Config_ValidatorMessage> aMsg =
500       std::dynamic_pointer_cast<Config_ValidatorMessage>(theMessage);
501     if (aMsg) {
502       if (aMsg->attributeId().empty()) {  // feature validator
503         validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->parameters());
504       } else {  // attribute validator
505         validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->attributeId(),
506                                       aMsg->parameters());
507       }
508     }
509   } else if (theMessage->eventID() == kPluginEvent) { // plugin is started to load
510     std::shared_ptr<Config_PluginMessage> aMsg =
511       std::dynamic_pointer_cast<Config_PluginMessage>(theMessage);
512     if (aMsg.get()) {
513       myCurrentPluginName = aMsg->pluginId();
514       if (!aMsg->uses().empty()) {
515         myUsePlugins[myCurrentPluginName] = aMsg->uses();
516       }
517     }
518   } else {  // create/update/delete
519     if (myCheckTransactions && !isOperation()) {
520       // check it is done in real opened document: 2958
521       bool aIsActual = true;
522       static const Events_ID kDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
523       if (theMessage->eventID() == kDeletedEvent) {
524         aIsActual = false;
525         std::shared_ptr<ModelAPI_ObjectDeletedMessage> aDeleted =
526           std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
527         std::list<std::shared_ptr<ModelAPI_Document> > allOpened =
528           Model_Session::allOpenedDocuments();
529         std::list<std::pair<std::shared_ptr<ModelAPI_Document>, std::string>>::const_iterator
530           aGIter = aDeleted->groups().cbegin();
531         for (; !aIsActual && aGIter != aDeleted->groups().cend(); aGIter++) {
532           std::list<std::shared_ptr<ModelAPI_Document> >::iterator anOpened = allOpened.begin();
533           for(; anOpened != allOpened.end(); anOpened++) {
534             if (aGIter->first == *anOpened) {
535               aIsActual = true;
536               break;
537             }
538           }
539         }
540       }
541
542       if (aIsActual)
543         Events_InfoMessage("Model_Session",
544           "Modification of data structure outside of the transaction").send();
545     }
546     // if part is deleted, make the root as the current document (on undo of Parts creations)
547     static const Events_ID kDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
548     if (theMessage->eventID() == kDeletedEvent) {
549       std::shared_ptr<ModelAPI_ObjectDeletedMessage> aDeleted =
550         std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
551
552       std::list<std::pair<std::shared_ptr<ModelAPI_Document>, std::string>>::const_iterator
553         aGIter = aDeleted->groups().cbegin();
554       for (; aGIter != aDeleted->groups().cend(); aGIter++) {
555         if (aGIter->second == ModelAPI_ResultPart::group())
556           break;
557       }
558       if (aGIter != aDeleted->groups().cend())
559       {
560          // check that the current feature of the session is still the active Part (even disabled)
561         bool aFound = false;
562         FeaturePtr aCurrentPart = moduleDocument()->currentFeature(true);
563         if (aCurrentPart.get()) {
564           const std::list<std::shared_ptr<ModelAPI_Result> >& aResList = aCurrentPart->results();
565           std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResList.begin();
566           for(; !aFound && aRes != aResList.end(); aRes++) {
567             ResultPartPtr aPRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes);
568             if (aPRes.get() && aPRes->isActivated() && aPRes->partDoc() == activeDocument()) {
569               aFound = true;
570
571             }
572           }
573         }
574         if (!aFound) { // if not, the part was removed, so activate the module document
575           if (myCurrentDoc.get())
576             setActiveDocument(moduleDocument());
577         }
578       }
579     }
580   }
581 }
582
583 void Model_Session::LoadPluginsInfo()
584 {
585   if (myPluginsInfoLoaded)  // nothing to do
586     return;
587   // Read plugins information from XML files
588   Config_ModuleReader aModuleReader(Config_FeatureMessage::MODEL_EVENT());
589   aModuleReader.readAll();
590   std::set<std::string> aFiles = aModuleReader.modulePluginFiles();
591   std::set<std::string>::iterator it = aFiles.begin();
592   for ( ; it != aFiles.end(); it++ ) {
593     Config_ValidatorReader aValidatorReader (*it);
594     aValidatorReader.readAll();
595   };
596
597 }
598
599 void Model_Session::registerPlugin(ModelAPI_Plugin* thePlugin)
600 {
601   myPluginObjs[myCurrentPluginName] = thePlugin;
602   static Events_ID EVENT_LOAD = Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED);
603   ModelAPI_EventCreator::get()->sendUpdated(ObjectPtr(), EVENT_LOAD, false);
604   // If the plugin has an ability to process GUI events, register it
605   Events_Listener* aListener = dynamic_cast<Events_Listener*>(thePlugin);
606   if (aListener) {
607     Events_Loop* aLoop = Events_Loop::loop();
608     static Events_ID aStateRequestEventId =
609         Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_REQUEST);
610     aLoop->registerListener(aListener, aStateRequestEventId);
611   }
612 }
613
614 ModelAPI_ValidatorsFactory* Model_Session::validators()
615 {
616   static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;
617   return aFactory;
618 }
619
620 ModelAPI_FiltersFactory* Model_Session::filters()
621 {
622   static Model_FiltersFactory* aFactory = new Model_FiltersFactory;
623   return aFactory;
624 }
625
626 int Model_Session::transactionID()
627 {
628   return ROOT_DOC->transactionID();
629 }
630
631 bool Model_Session::isAutoUpdateBlocked()
632 {
633   Handle(Model_Application) anApp = Model_Application::getApplication();
634   if (!anApp->hasRoot()) // when document is not yet created, do not create it by such simple call
635     return false;
636   return !ROOT_DOC->autoRecomutationState();
637 }
638
639 void Model_Session::blockAutoUpdate(const bool theBlock)
640 {
641   bool aCurrentState = isAutoUpdateBlocked();
642   if (aCurrentState != theBlock) {
643     // if there is no operation, start it to avoid modifications outside of transaction
644     bool isOperation = this->isOperation();
645     if (!isOperation)
646       startOperation("Auto update");
647     ROOT_DOC->setAutoRecomutationState(!theBlock);
648     static Events_Loop* aLoop = Events_Loop::loop();
649     if (theBlock) {
650       static const Events_ID kAutoOff = aLoop->eventByName(EVENT_AUTOMATIC_RECOMPUTATION_DISABLE);
651       std::shared_ptr<Events_Message> aMsg(new Events_Message(kAutoOff));
652       aLoop->send(aMsg);
653     } else {
654       // if there is no operation, start it to avoid modifications outside of transaction
655       bool isOperation = this->isOperation();
656       if (!isOperation)
657         startOperation("Auto update enabling");
658       static const Events_ID kAutoOn = aLoop->eventByName(EVENT_AUTOMATIC_RECOMPUTATION_ENABLE);
659       std::shared_ptr<Events_Message> aMsg(new Events_Message(kAutoOn));
660       aLoop->send(aMsg);
661     }
662     if (!isOperation) {
663       finishOperation();
664       // append this transaction to the previous one: don't need this separated operation in list
665       ROOT_DOC->appendTransactionToPrevious();
666     }
667   }
668 }