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