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