1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_Session.cxx
4 // Created: 20 Mar 2014
5 // Author: Mikhail PONIKAROV
7 #include <Model_Session.h>
8 #include <ModelAPI_Feature.h>
9 #include <ModelAPI_Plugin.h>
10 #include <Model_Data.h>
11 #include <Model_Document.h>
12 #include <Model_Objects.h>
13 #include <Model_Application.h>
14 #include <Model_Events.h>
15 #include <Model_Validator.h>
16 #include <ModelAPI_Events.h>
17 #include <Events_Loop.h>
18 #include <Events_Error.h>
19 #include <Config_FeatureMessage.h>
20 #include <Config_AttributeMessage.h>
21 #include <Config_ValidatorMessage.h>
22 #include <Config_ModuleReader.h>
23 #include <Config_ValidatorReader.h>
24 #include <ModelAPI_ResultPart.h>
25 #include <ModelAPI_Tools.h>
27 #include <TDF_CopyTool.hxx>
28 #include <TDF_DataSet.hxx>
29 #include <TDF_RelocationTable.hxx>
30 #include <TDF_ClosureTool.hxx>
34 static Model_Session* myImpl = new Model_Session();
36 // t oredirect all calls to the root document
37 #define ROOT_DOC std::dynamic_pointer_cast<Model_Document>(moduleDocument())
39 bool Model_Session::load(const char* theFileName)
41 bool aRes = ROOT_DOC->load(theFileName, ROOT_DOC);
45 bool Model_Session::save(const char* theFileName, std::list<std::string>& theResults)
47 return ROOT_DOC->save(theFileName, theResults);
50 void Model_Session::closeAll()
52 Model_Application::getApplication()->deleteAllDocuments();
55 void Model_Session::startOperation(const std::string& theId, const bool theAttachedToNested)
57 myOperationAttachedToNext = theAttachedToNested;
58 ROOT_DOC->startOperation();
59 ROOT_DOC->operationId(theId);
60 static std::shared_ptr<Events_Message> aStartedMsg
61 (new Events_Message(Events_Loop::eventByName("StartOperation")));
62 Events_Loop::loop()->send(aStartedMsg);
63 // remove all useless documents that has been closed: on start of operation undo/redo is cleared
64 std::list<std::shared_ptr<ModelAPI_Document> > aUsedDocs = allOpenedDocuments();
65 Model_Application::getApplication()->removeUselessDocuments(aUsedDocs);
68 void Model_Session::finishOperation()
70 setCheckTransactions(false);
71 ROOT_DOC->finishOperation();
72 if (myOperationAttachedToNext) { // twice, with nested
73 ROOT_DOC->finishOperation();
74 myOperationAttachedToNext = false;
76 setCheckTransactions(true);
79 void Model_Session::abortOperation()
81 setCheckTransactions(false);
82 ROOT_DOC->abortOperation();
83 if (myOperationAttachedToNext) { // twice, with nested
84 ROOT_DOC->abortOperation();
85 myOperationAttachedToNext = false;
87 setCheckTransactions(true);
88 // here the update mechanism may work after abort, so, supress the warnings about
89 // modifications outside of the transactions
90 bool aWasCheck = myCheckTransactions;
91 myCheckTransactions = false;
92 static std::shared_ptr<Events_Message> anAbortMsg
93 (new Events_Message(Events_Loop::eventByName("AbortOperation")));
94 Events_Loop::loop()->send(anAbortMsg);
95 myCheckTransactions = true;
96 myCheckTransactions = aWasCheck;
99 bool Model_Session::isOperation()
101 return ROOT_DOC->isOperation();
104 bool Model_Session::isModified()
106 return ROOT_DOC->isModified();
109 bool Model_Session::canUndo()
111 return ROOT_DOC->canUndo();
114 void Model_Session::undo()
116 setCheckTransactions(false);
118 setCheckTransactions(true);
121 bool Model_Session::canRedo()
123 return ROOT_DOC->canRedo();
126 void Model_Session::redo()
128 setCheckTransactions(false);
130 setCheckTransactions(true);
133 //! Returns stack of performed operations
134 std::list<std::string> Model_Session::undoList()
136 return ROOT_DOC->undoList();
138 //! Returns stack of rolled back operations
139 std::list<std::string> Model_Session::redoList()
141 return ROOT_DOC->redoList();
144 FeaturePtr Model_Session::createFeature(string theFeatureID, Model_Document* theDocOwner)
146 if (this != myImpl) {
147 return myImpl->createFeature(theFeatureID, theDocOwner);
150 // load all information about plugins, features and attributes
153 if (myPlugins.find(theFeatureID) != myPlugins.end()) {
154 std::pair<std::string, std::string>& aPlugin = myPlugins[theFeatureID]; // plugin and doc kind
155 if (!aPlugin.second.empty() && aPlugin.second != theDocOwner->kind()) {
157 string("Feature '") + theFeatureID + "' can be created only in document '"
158 + aPlugin.second + "' by the XML definition");
161 myCurrentPluginName = aPlugin.first;
162 if (myPluginObjs.find(myCurrentPluginName) == myPluginObjs.end()) {
163 // load plugin library if not yet done
164 Config_ModuleReader::loadPlugin(myCurrentPluginName);
166 if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
167 FeaturePtr aCreated = myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
170 string("Can not initialize feature '") + theFeatureID + "' in plugin '"
171 + myCurrentPluginName + "'");
175 Events_Error::send(string("Can not load plugin '") + myCurrentPluginName + "'");
178 Events_Error::send(string("Feature '") + theFeatureID + "' not found in any plugin");
181 return FeaturePtr(); // return nothing
184 std::shared_ptr<ModelAPI_Document> Model_Session::moduleDocument()
186 bool aFirstCall = !Model_Application::getApplication()->hasDocument("root");
188 // creation of the root document is always outside of the transaction, so, avoid checking it
189 setCheckTransactions(false);
191 std::shared_ptr<ModelAPI_Document> aDoc = std::shared_ptr<ModelAPI_Document>(
192 Model_Application::getApplication()->getDocument("root"));
194 // creation of the root document is always outside of the transaction, so, avoid checking it
195 setCheckTransactions(true);
200 std::shared_ptr<ModelAPI_Document> Model_Session::document(std::string theDocID)
202 return std::shared_ptr<ModelAPI_Document>(
203 Model_Application::getApplication()->getDocument(theDocID));
206 bool Model_Session::hasModuleDocument()
208 return Model_Application::getApplication()->hasDocument("root");
211 std::shared_ptr<ModelAPI_Document> Model_Session::activeDocument()
213 if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
214 myCurrentDoc = moduleDocument();
218 /// makes the last feature in the document as the current
219 static void makeCurrentLast(std::shared_ptr<ModelAPI_Document> theDoc) {
221 FeaturePtr aLast = std::dynamic_pointer_cast<Model_Document>(theDoc)->lastFeature();
222 theDoc->setCurrentFeature(aLast, false);
226 void Model_Session::setActiveDocument(
227 std::shared_ptr<ModelAPI_Document> theDoc, bool theSendSignal)
229 if (myCurrentDoc != theDoc) {
230 if (myCurrentDoc.get())
231 myCurrentDoc->setActive(false);
233 theDoc->setActive(true);
235 std::shared_ptr<ModelAPI_Document> aPrevious = myCurrentDoc;
236 myCurrentDoc = theDoc;
237 if (theDoc.get() && theSendSignal) {
238 // syncronize the document: it may be just opened or opened but removed before
239 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(theDoc);
241 bool aWasChecked = myCheckTransactions;
242 setCheckTransactions(false);
243 TDF_LabelList anEmptyUpdated;
244 aDoc->objects()->synchronizeFeatures(anEmptyUpdated, true, true);
246 setCheckTransactions(true);
248 static std::shared_ptr<Events_Message> aMsg(
249 new Events_Message(Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED)));
250 Events_Loop::loop()->send(aMsg);
252 // make the current state correct and synchronised in the module and sub-documents
253 if (isOperation()) { // do it only in transaction, not on opening of document
254 if (myCurrentDoc == moduleDocument()) {
255 // make the current feature the latest in root, in previous root current become also last
256 makeCurrentLast(aPrevious);
257 makeCurrentLast(myCurrentDoc);
259 // make the current feature the latest in sub, root current feature becomes this sub
260 makeCurrentLast(myCurrentDoc);
261 DocumentPtr aRoot = moduleDocument();
262 ResultPtr aPartRes = ModelAPI_Tools::findPartResult(aRoot, myCurrentDoc);
263 if (aPartRes.get()) {
264 FeaturePtr aPartFeat = aRoot->feature(aPartRes);
265 if (aPartFeat.get()) {
266 aRoot->setCurrentFeature(aPartFeat, false);
274 std::list<std::shared_ptr<ModelAPI_Document> > Model_Session::allOpenedDocuments()
276 list<std::shared_ptr<ModelAPI_Document> > aResult;
277 aResult.push_back(moduleDocument());
278 // add subs recursively
279 list<std::shared_ptr<ModelAPI_Document> >::iterator aDoc = aResult.begin();
280 for(; aDoc != aResult.end(); aDoc++) {
281 DocumentPtr anAPIDoc = *aDoc;
282 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(anAPIDoc);
284 const std::set<std::string> aSubs = aDoc->subDocuments(true);
285 std::set<std::string>::const_iterator aSubIter = aSubs.cbegin();
286 for(; aSubIter != aSubs.cend(); aSubIter++) {
287 if (!Model_Application::getApplication()->isLoadByDemand(*aSubIter)) {
288 aResult.push_back(Model_Application::getApplication()->getDocument(*aSubIter));
296 bool Model_Session::isLoadByDemand(const std::string theDocID)
298 return Model_Application::getApplication()->isLoadByDemand(theDocID);
301 std::shared_ptr<ModelAPI_Document> Model_Session::copy(
302 std::shared_ptr<ModelAPI_Document> theSource, std::string theID)
304 // create a new document
305 std::shared_ptr<Model_Document> aNew = std::dynamic_pointer_cast<Model_Document>(
306 Model_Application::getApplication()->getDocument(theID));
307 // make a copy of all labels
308 TDF_Label aSourceRoot = std::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main()
310 TDF_Label aTargetRoot = aNew->document()->Main().Father();
311 Handle(TDF_DataSet) aDS = new TDF_DataSet;
312 aDS->AddLabel(aSourceRoot);
313 TDF_ClosureTool::Closure(aDS);
314 Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
315 aRT->SetRelocation(aSourceRoot, aTargetRoot);
316 TDF_CopyTool::Copy(aDS, aRT);
318 TDF_LabelList anEmptyUpdated;
319 aNew->objects()->synchronizeFeatures(anEmptyUpdated, true, true);
323 Model_Session::Model_Session()
325 myPluginsInfoLoaded = false;
326 myCheckTransactions = true;
327 myOperationAttachedToNext = false;
328 ModelAPI_Session::setSession(std::shared_ptr<ModelAPI_Session>(this));
329 // register the configuration reading listener
330 Events_Loop* aLoop = Events_Loop::loop();
331 static const Events_ID kFeatureEvent =
332 Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
333 aLoop->registerListener(this, kFeatureEvent);
334 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED), 0, true);
335 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED), 0, true);
336 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED), 0, true);
337 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_VALIDATOR_LOADED));
340 void Model_Session::processEvent(const std::shared_ptr<Events_Message>& theMessage)
342 static const Events_ID kFeatureEvent =
343 Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
344 static const Events_ID kValidatorEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
345 if (theMessage->eventID() == kFeatureEvent) {
346 const std::shared_ptr<Config_FeatureMessage> aMsg =
347 std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
349 // process the plugin info, load plugin
350 if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
351 myPlugins[aMsg->id()] = std::pair<std::string, std::string>(
352 aMsg->pluginLibrary(), aMsg->documentKind());
355 const std::shared_ptr<Config_AttributeMessage> aMsgAttr =
356 std::dynamic_pointer_cast<Config_AttributeMessage>(theMessage);
358 if (!aMsgAttr->isObligatory()) {
359 validators()->registerNotObligatory(aMsgAttr->featureId(), aMsgAttr->attributeId());
361 if(aMsgAttr->isConcealment()) {
362 validators()->registerConcealment(aMsgAttr->featureId(), aMsgAttr->attributeId());
364 if (!aMsgAttr->caseId().empty()) {
365 validators()->registerCase(aMsgAttr->featureId(), aMsgAttr->attributeId(),
366 aMsgAttr->switchId(), aMsgAttr->caseId());
370 // plugins information was started to load, so, it will be loaded
371 myPluginsInfoLoaded = true;
372 } else if (theMessage->eventID() == kValidatorEvent) {
373 std::shared_ptr<Config_ValidatorMessage> aMsg =
374 std::dynamic_pointer_cast<Config_ValidatorMessage>(theMessage);
376 if (aMsg->attributeId().empty()) { // feature validator
377 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->parameters());
378 } else { // attribute validator
379 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->attributeId(),
383 } else { // create/update/delete
384 if (myCheckTransactions && !isOperation())
385 Events_Error::send("Modification of data structure outside of the transaction");
386 // if part is deleted, make the root as the current document (on undo of Parts creations)
387 static const Events_ID kDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
388 if (theMessage->eventID() == kDeletedEvent) {
389 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aDeleted =
390 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
392 aDeleted->groups().find(ModelAPI_ResultPart::group()) != aDeleted->groups().end() &&
393 !ModelAPI_Tools::findPartResult(moduleDocument(), activeDocument()).get()) // another part may be disabled
395 setActiveDocument(moduleDocument());
401 void Model_Session::LoadPluginsInfo()
403 if (myPluginsInfoLoaded) // nothing to do
406 // Read plugins information from XML files
407 Config_ModuleReader aModuleReader(Config_FeatureMessage::MODEL_EVENT());
408 aModuleReader.readAll();
409 std::set<std::string> aFiles = aModuleReader.modulePluginFiles();
410 std::set<std::string>::iterator it = aFiles.begin();
411 for ( ; it != aFiles.end(); it++ ) {
412 Config_ValidatorReader aValidatorReader (*it);
413 aValidatorReader.readAll();
418 void Model_Session::registerPlugin(ModelAPI_Plugin* thePlugin)
420 myPluginObjs[myCurrentPluginName] = thePlugin;
421 static Events_ID EVENT_LOAD = Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED);
422 ModelAPI_EventCreator::get()->sendUpdated(ObjectPtr(), EVENT_LOAD);
423 Events_Loop::loop()->flush(EVENT_LOAD);
424 // If the plugin has an ability to process GUI events, register it
425 Events_Listener* aListener = dynamic_cast<Events_Listener*>(thePlugin);
427 Events_Loop* aLoop = Events_Loop::loop();
428 static Events_ID aStateRequestEventId =
429 Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_REQUEST);
430 aLoop->registerListener(aListener, aStateRequestEventId);
434 ModelAPI_ValidatorsFactory* Model_Session::validators()
436 static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;
440 int Model_Session::transactionID()
442 return ROOT_DOC->transactionID();