Salome HOME
Get rid of compilation warnings. Part II. MSVC warnings.
[modules/shaper.git] / src / Model / Model_Session.cpp
1 // Copyright (C) 2014-2020  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 bool Model_Session::canUndo()
147 {
148   return ROOT_DOC->canUndo();
149 }
150
151 void Model_Session::undo()
152 {
153   setCheckTransactions(false);
154   ROOT_DOC->undo();
155   setCheckTransactions(true);
156 }
157
158 bool Model_Session::canRedo()
159 {
160   return ROOT_DOC->canRedo();
161 }
162
163 void Model_Session::redo()
164 {
165   setCheckTransactions(false);
166   ROOT_DOC->redo();
167   setCheckTransactions(true);
168 }
169
170 //! Returns stack of performed operations
171 std::list<std::string> Model_Session::undoList()
172 {
173   return ROOT_DOC->undoList();
174 }
175 //! Returns stack of rolled back operations
176 std::list<std::string> Model_Session::redoList()
177 {
178   return ROOT_DOC->redoList();
179 }
180
181 ModelAPI_Plugin* Model_Session::getPlugin(const std::string& thePluginName)
182 {
183   if (myPluginObjs.find(thePluginName) == myPluginObjs.end()) {
184     // before load the used plugins
185     if (myUsePlugins.find(thePluginName) != myUsePlugins.end()) {
186       std::string aUse = myUsePlugins[thePluginName];
187       std::stringstream aUseStream(aUse);
188       std::string aPluginName;
189       while (std::getline(aUseStream, aPluginName, ',')) {
190         if (myPluginObjs.find(aPluginName) == myPluginObjs.end())
191           getPlugin(aPluginName);
192       }
193     }
194     // load plugin library if not yet done
195     Config_ModuleReader::loadPlugin(thePluginName);
196   }
197   if (myPluginObjs.find(thePluginName) == myPluginObjs.end()) {
198     Events_InfoMessage("Model_Session", "Can not load plugin '%1'").arg(thePluginName).send();
199     return NULL;
200   }
201   return myPluginObjs[thePluginName];
202 }
203
204 FeaturePtr Model_Session::createFeature(std::string theFeatureID, Model_Document* theDocOwner)
205 {
206   if (this != myImpl) {
207     return myImpl->createFeature(theFeatureID, theDocOwner);
208   }
209
210   // load all information about plugins, features and attributes
211   LoadPluginsInfo();
212
213   if (myPlugins.find(theFeatureID) != myPlugins.end()) {
214     std::pair<std::string, std::string>& aPlugin = myPlugins[theFeatureID]; // plugin and doc kind
215     if (!aPlugin.second.empty() && aPlugin.second != theDocOwner->kind()) {
216       Events_InfoMessage("Model_Session",
217         "Feature '%1' can be created only in document '%2' by the XML definition")
218         .arg(theFeatureID).arg(aPlugin.second).send();
219       return FeaturePtr();
220     }
221     ModelAPI_Plugin* aPluginObj = getPlugin(aPlugin.first);
222     if (aPluginObj) {
223       FeaturePtr aCreated = aPluginObj->createFeature(theFeatureID);
224       if (!aCreated) {
225         Events_InfoMessage("Model_Session", "Can not initialize feature '%1' in plugin '%2'")
226           .arg(theFeatureID).arg(aPlugin.first).send();
227       }
228       return aCreated;
229     } else {
230       Events_InfoMessage("Model_Session", "Can not load plugin '%1'").arg(aPlugin.first).send();
231     }
232   } else {
233     Events_InfoMessage("Model_Session",
234       "Feature '%1' not found in any plugin").arg(theFeatureID).send();
235   }
236
237   return FeaturePtr();  // return nothing
238 }
239
240 std::shared_ptr<ModelAPI_Document> Model_Session::moduleDocument()
241 {
242   Handle(Model_Application) anApp = Model_Application::getApplication();
243   bool aFirstCall = !anApp->hasRoot();
244   if (aFirstCall) {
245     // to be sure that plugins are loaded,
246     // even before the first "createFeature" call (in unit tests)
247
248     LoadPluginsInfo();
249     // creation of the root document is always outside of the transaction, so, avoid checking it
250     setCheckTransactions(false);
251     anApp->createDocument(0); // 0 is a root ID
252     // creation of the root document is always outside of the transaction, so, avoid checking it
253     setCheckTransactions(true);
254   }
255   return anApp->rootDocument();
256 }
257
258 std::shared_ptr<ModelAPI_Document> Model_Session::document(int theDocID)
259 {
260   return std::shared_ptr<ModelAPI_Document>(
261       Model_Application::getApplication()->document(theDocID));
262 }
263
264 bool Model_Session::hasModuleDocument()
265 {
266   return Model_Application::getApplication()->hasRoot();
267 }
268
269 std::shared_ptr<ModelAPI_Document> Model_Session::activeDocument()
270 {
271   if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
272     myCurrentDoc = moduleDocument();
273   return myCurrentDoc;
274 }
275
276 /// makes the last feature in the document as the current
277 static void makeCurrentLast(std::shared_ptr<ModelAPI_Document> theDoc) {
278   if (theDoc.get()) {
279     FeaturePtr aLast = std::dynamic_pointer_cast<Model_Document>(theDoc)->lastFeature();
280     // if last is nested into something else, make this something else as last:
281     // otherwise it will look like edition of sub-element, so, the main will be disabled
282     if (aLast.get()) {
283       CompositeFeaturePtr aMain = ModelAPI_Tools::compositeOwner(aLast);
284       while(aMain.get()) {
285         aLast = aMain;
286         aMain = ModelAPI_Tools::compositeOwner(aLast);
287       }
288     }
289     theDoc->setCurrentFeature(aLast, false);
290   }
291 }
292
293 void Model_Session::setActiveDocument(
294   std::shared_ptr<ModelAPI_Document> theDoc, bool theSendSignal)
295 {
296   if (myCurrentDoc != theDoc) {
297     if (myCurrentDoc.get())
298       myCurrentDoc->setActive(false);
299     if (theDoc.get())
300       theDoc->setActive(true);
301
302     std::shared_ptr<ModelAPI_Document> aPrevious = myCurrentDoc;
303     myCurrentDoc = theDoc;
304     if (theDoc.get() && theSendSignal) {
305       // this must be before the synchronization call because features in PartSet lower than this
306       // part feature must be disabled and don't recomputed anymore (issue 1156,
307       // translation feature is failed on activation of Part 2)
308       if (isOperation()) { // do it only in transaction, not on opening of document
309         DocumentPtr aRoot = moduleDocument();
310         if (myCurrentDoc != aRoot) {
311           FeaturePtr aPartFeat = ModelAPI_Tools::findPartFeature(aRoot, myCurrentDoc);
312           if (aPartFeat.get()) {
313             aRoot->setCurrentFeature(aPartFeat, false);
314           }
315         }
316       }
317       // synchronize the document: it may be just opened or opened but removed before
318       std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(theDoc);
319       if (aDoc.get()) {
320         bool aWasChecked = myCheckTransactions;
321         setCheckTransactions(false);
322         TDF_LabelList anEmptyUpdated;
323         aDoc->objects()->synchronizeFeatures(anEmptyUpdated, true, true, false, true);
324         if (aWasChecked)
325             setCheckTransactions(true);
326       }
327       static std::shared_ptr<Events_Message> aMsg(
328           new Events_Message(Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED)));
329       Events_Loop::loop()->send(aMsg);
330     }
331     // make the current state correct and synchronized in the module and sub-documents
332     if (isOperation()) { // do it only in transaction, not on opening of document
333       if (myCurrentDoc == moduleDocument()) {
334         // make the current feature the latest in root, in previous root current become also last
335         makeCurrentLast(aPrevious);
336         makeCurrentLast(myCurrentDoc);
337       } else {
338         // make the current feature the latest in sub, root current feature becomes this sub
339         makeCurrentLast(myCurrentDoc);
340       }
341     }
342   }
343 }
344
345 std::list<std::shared_ptr<ModelAPI_Document> > Model_Session::allOpenedDocuments()
346 {
347   std::list<std::shared_ptr<ModelAPI_Document> > aResult;
348   aResult.push_back(moduleDocument());
349   // add subs recursively
350   std::list<std::shared_ptr<ModelAPI_Document> >::iterator aDocIt = aResult.begin();
351   for(; aDocIt != aResult.end(); aDocIt++) {
352     DocumentPtr anAPIDoc = *aDocIt;
353     std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(anAPIDoc);
354     if (aDoc) {
355       const std::set<int> aSubs = aDoc->subDocuments();
356       std::set<int>::const_iterator aSubIter = aSubs.cbegin();
357       for(; aSubIter != aSubs.cend(); aSubIter++) {
358         aResult.push_back(Model_Application::getApplication()->document(*aSubIter));
359       }
360     }
361   }
362   return aResult;
363 }
364
365 bool Model_Session::isLoadByDemand(const std::string theDocID, const int theDocIndex)
366 {
367   return Model_Application::getApplication()->isLoadByDemand(theDocID, theDocIndex);
368 }
369
370 std::shared_ptr<ModelAPI_Document> Model_Session::copy(
371     std::shared_ptr<ModelAPI_Document> theSource, const int theDestID)
372 {
373   std::shared_ptr<Model_Document> aNew = Model_Application::getApplication()->document(theDestID);
374   // make a copy of all labels
375   TDF_Label aSourceRoot = std::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main()
376       .Father();
377   TDF_Label aTargetRoot = aNew->document()->Main().Father();
378   Handle(TDF_DataSet) aDS = new TDF_DataSet;
379   aDS->AddLabel(aSourceRoot);
380   TDF_ClosureTool::Closure(aDS);
381   Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable(Standard_True);
382   aRT->SetRelocation(aSourceRoot, aTargetRoot);
383   TDF_CopyTool::Copy(aDS, aRT);
384
385   // TODO: remove after fix in OCCT.
386   // All named shapes are stored in reversed order, so to fix this we reverse them back.
387   for(TDF_ChildIDIterator aChildIter(aTargetRoot, TNaming_NamedShape::GetID(), true);
388       aChildIter.More();
389       aChildIter.Next()) {
390     Handle(TNaming_NamedShape) aNamedShape =
391       Handle(TNaming_NamedShape)::DownCast(aChildIter.Value());
392     if (aNamedShape.IsNull()) {
393       continue;
394     }
395
396     TopoDS_Shape aShape = aNamedShape->Get();
397     if(aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND) {
398       continue;
399     }
400
401     TNaming_Evolution anEvol = aNamedShape->Evolution();
402     std::list<std::pair<TopoDS_Shape, TopoDS_Shape> > aShapePairs; // to store old and new shapes
403     for(TNaming_Iterator anIter(aNamedShape); anIter.More(); anIter.Next()) {
404       aShapePairs.push_back(
405         std::pair<TopoDS_Shape, TopoDS_Shape>(anIter.OldShape(), anIter.NewShape()));
406     }
407
408     // Add in reverse order.
409     TDF_Label aLabel = aNamedShape->Label();
410     TNaming_Builder aBuilder(aLabel);
411     for(std::list<std::pair<TopoDS_Shape, TopoDS_Shape> >::iterator aPairsIter =
412           aShapePairs.begin();
413         aPairsIter != aShapePairs.end();
414         aPairsIter++) {
415       if (anEvol == TNaming_GENERATED) {
416         aBuilder.Generated(aPairsIter->first, aPairsIter->second);
417       } else if (anEvol == TNaming_MODIFY) {
418         aBuilder.Modify(aPairsIter->first, aPairsIter->second);
419       } else if (anEvol == TNaming_DELETE) {
420         aBuilder.Delete(aPairsIter->first);
421       } else if (anEvol == TNaming_PRIMITIVE) {
422         aBuilder.Generated(aPairsIter->second);
423       } else if (anEvol == TNaming_SELECTED) {
424         aBuilder.Select(aPairsIter->second, aPairsIter->first);
425       }
426     }
427   }
428
429   TDF_LabelList anEmptyUpdated;
430   aNew->objects()->synchronizeFeatures(anEmptyUpdated, true, true, true, true);
431   return aNew;
432 }
433
434 Model_Session::Model_Session()
435 {
436   myPluginsInfoLoaded = false;
437   myCheckTransactions = true;
438   ModelAPI_Session::setSession(std::shared_ptr<ModelAPI_Session>(this));
439   // register the configuration reading listener
440   Events_Loop* aLoop = Events_Loop::loop();
441   static const Events_ID kFeatureEvent =
442     Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
443   aLoop->registerListener(this, kFeatureEvent);
444   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED), 0, true);
445   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED), 0, true);
446   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED), 0, true);
447   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_VALIDATOR_LOADED));
448   aLoop->registerListener(this, Events_Loop::eventByName(Config_PluginMessage::EVENT_ID()));
449 }
450
451 void Model_Session::processEvent(const std::shared_ptr<Events_Message>& theMessage)
452 {
453   static const Events_ID kFeatureEvent =
454     Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
455   static const Events_ID kValidatorEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
456   static const Events_ID kPluginEvent = Events_Loop::eventByName(Config_PluginMessage::EVENT_ID());
457   if (theMessage->eventID() == kFeatureEvent) {
458     const std::shared_ptr<Config_FeatureMessage> aMsg =
459       std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
460     if (aMsg) {
461
462       // process the plugin info, load plugin
463       if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
464         myPlugins[aMsg->id()] = std::pair<std::string, std::string>(
465           aMsg->pluginLibrary(), aMsg->documentKind());
466       }
467     } else {
468       const std::shared_ptr<Config_AttributeMessage> aMsgAttr =
469         std::dynamic_pointer_cast<Config_AttributeMessage>(theMessage);
470       if (aMsgAttr) {
471
472         if (!aMsgAttr->isObligatory()) {
473           validators()->registerNotObligatory(aMsgAttr->featureId(), aMsgAttr->attributeId());
474         }
475         if(aMsgAttr->isConcealment()) {
476           validators()->registerConcealment(aMsgAttr->featureId(), aMsgAttr->attributeId());
477         }
478         if(aMsgAttr->isMainArgument()) {
479           validators()->registerMainArgument(aMsgAttr->featureId(), aMsgAttr->attributeId());
480         }
481         const std::list<std::pair<std::string, std::string> >& aCases = aMsgAttr->getCases();
482         if (!aCases.empty()) {
483           validators()->registerCase(aMsgAttr->featureId(), aMsgAttr->attributeId(), aCases);
484         }
485         if (aMsgAttr->isGeometricalSelection()) {
486           validators()->registerGeometricalSelection(aMsgAttr->featureId(),
487                                                      aMsgAttr->attributeId());
488         }
489       }
490     }
491     // plugins information was started to load, so, it will be loaded
492     myPluginsInfoLoaded = true;
493   } else if (theMessage->eventID() == kValidatorEvent) {
494     std::shared_ptr<Config_ValidatorMessage> aMsg =
495       std::dynamic_pointer_cast<Config_ValidatorMessage>(theMessage);
496     if (aMsg) {
497       if (aMsg->attributeId().empty()) {  // feature validator
498         validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->parameters());
499       } else {  // attribute validator
500         validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->attributeId(),
501                                       aMsg->parameters());
502       }
503     }
504   } else if (theMessage->eventID() == kPluginEvent) { // plugin is started to load
505     std::shared_ptr<Config_PluginMessage> aMsg =
506       std::dynamic_pointer_cast<Config_PluginMessage>(theMessage);
507     if (aMsg.get()) {
508       myCurrentPluginName = aMsg->pluginId();
509       if (!aMsg->uses().empty()) {
510         myUsePlugins[myCurrentPluginName] = aMsg->uses();
511       }
512     }
513   } else {  // create/update/delete
514     if (myCheckTransactions && !isOperation()) {
515       // check it is done in real opened document: 2958
516       bool aIsActual = true;
517       static const Events_ID kDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
518       if (theMessage->eventID() == kDeletedEvent) {
519         aIsActual = false;
520         std::shared_ptr<ModelAPI_ObjectDeletedMessage> aDeleted =
521           std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
522         std::list<std::shared_ptr<ModelAPI_Document> > allOpened =
523           Model_Session::allOpenedDocuments();
524         std::list<std::pair<std::shared_ptr<ModelAPI_Document>, std::string>>::const_iterator
525           aGIter = aDeleted->groups().cbegin();
526         for (; !aIsActual && aGIter != aDeleted->groups().cend(); aGIter++) {
527           std::list<std::shared_ptr<ModelAPI_Document> >::iterator anOpened = allOpened.begin();
528           for(; anOpened != allOpened.end(); anOpened++) {
529             if (aGIter->first == *anOpened) {
530               aIsActual = true;
531               break;
532             }
533           }
534         }
535       }
536
537       if (aIsActual)
538         Events_InfoMessage("Model_Session",
539           "Modification of data structure outside of the transaction").send();
540     }
541     // if part is deleted, make the root as the current document (on undo of Parts creations)
542     static const Events_ID kDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
543     if (theMessage->eventID() == kDeletedEvent) {
544       std::shared_ptr<ModelAPI_ObjectDeletedMessage> aDeleted =
545         std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
546
547       std::list<std::pair<std::shared_ptr<ModelAPI_Document>, std::string>>::const_iterator
548         aGIter = aDeleted->groups().cbegin();
549       for (; aGIter != aDeleted->groups().cend(); aGIter++) {
550         if (aGIter->second == ModelAPI_ResultPart::group())
551           break;
552       }
553       if (aGIter != aDeleted->groups().cend())
554       {
555          // check that the current feature of the session is still the active Part (even disabled)
556         bool aFound = false;
557         FeaturePtr aCurrentPart = moduleDocument()->currentFeature(true);
558         if (aCurrentPart.get()) {
559           const std::list<std::shared_ptr<ModelAPI_Result> >& aResList = aCurrentPart->results();
560           std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResList.begin();
561           for(; !aFound && aRes != aResList.end(); aRes++) {
562             ResultPartPtr aPRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes);
563             if (aPRes.get() && aPRes->isActivated() && aPRes->partDoc() == activeDocument()) {
564               aFound = true;
565
566             }
567           }
568         }
569         if (!aFound) { // if not, the part was removed, so activate the module document
570           if (myCurrentDoc.get())
571             setActiveDocument(moduleDocument());
572         }
573       }
574     }
575   }
576 }
577
578 void Model_Session::LoadPluginsInfo()
579 {
580   if (myPluginsInfoLoaded)  // nothing to do
581     return;
582   // Read plugins information from XML files
583   Config_ModuleReader aModuleReader(Config_FeatureMessage::MODEL_EVENT());
584   aModuleReader.readAll();
585   std::set<std::string> aFiles = aModuleReader.modulePluginFiles();
586   std::set<std::string>::iterator it = aFiles.begin();
587   for ( ; it != aFiles.end(); it++ ) {
588     Config_ValidatorReader aValidatorReader (*it);
589     aValidatorReader.readAll();
590   };
591
592 }
593
594 void Model_Session::registerPlugin(ModelAPI_Plugin* thePlugin)
595 {
596   myPluginObjs[myCurrentPluginName] = thePlugin;
597   static Events_ID EVENT_LOAD = Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED);
598   ModelAPI_EventCreator::get()->sendUpdated(ObjectPtr(), EVENT_LOAD, false);
599   // If the plugin has an ability to process GUI events, register it
600   Events_Listener* aListener = dynamic_cast<Events_Listener*>(thePlugin);
601   if (aListener) {
602     Events_Loop* aLoop = Events_Loop::loop();
603     static Events_ID aStateRequestEventId =
604         Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_REQUEST);
605     aLoop->registerListener(aListener, aStateRequestEventId);
606   }
607 }
608
609 ModelAPI_ValidatorsFactory* Model_Session::validators()
610 {
611   static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;
612   return aFactory;
613 }
614
615 ModelAPI_FiltersFactory* Model_Session::filters()
616 {
617   static Model_FiltersFactory* aFactory = new Model_FiltersFactory;
618   return aFactory;
619 }
620
621 int Model_Session::transactionID()
622 {
623   return ROOT_DOC->transactionID();
624 }
625
626 bool Model_Session::isAutoUpdateBlocked()
627 {
628   Handle(Model_Application) anApp = Model_Application::getApplication();
629   if (!anApp->hasRoot()) // when document is not yet created, do not create it by such simple call
630     return false;
631   return !ROOT_DOC->autoRecomutationState();
632 }
633
634 void Model_Session::blockAutoUpdate(const bool theBlock)
635 {
636   bool aCurrentState = isAutoUpdateBlocked();
637   if (aCurrentState != theBlock) {
638     // if there is no operation, start it to avoid modifications outside of transaction
639     bool isOperation = this->isOperation();
640     if (!isOperation)
641       startOperation("Auto update");
642     ROOT_DOC->setAutoRecomutationState(!theBlock);
643     static Events_Loop* aLoop = Events_Loop::loop();
644     if (theBlock) {
645       static const Events_ID kAutoOff = aLoop->eventByName(EVENT_AUTOMATIC_RECOMPUTATION_DISABLE);
646       std::shared_ptr<Events_Message> aMsg(new Events_Message(kAutoOff));
647       aLoop->send(aMsg);
648     } else {
649       // if there is no operation, start it to avoid modifications outside of transaction
650       isOperation = this->isOperation();
651       if (!isOperation)
652         startOperation("Auto update enabling");
653       static const Events_ID kAutoOn = aLoop->eventByName(EVENT_AUTOMATIC_RECOMPUTATION_ENABLE);
654       std::shared_ptr<Events_Message> aMsg(new Events_Message(kAutoOn));
655       aLoop->send(aMsg);
656     }
657     if (!isOperation) {
658       finishOperation();
659       // append this transaction to the previous one: don't need this separated operation in list
660       ROOT_DOC->appendTransactionToPrevious();
661     }
662   }
663 }