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