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