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>
26 #include <TDF_CopyTool.hxx>
27 #include <TDF_DataSet.hxx>
28 #include <TDF_RelocationTable.hxx>
29 #include <TDF_ClosureTool.hxx>
33 static Model_Session* myImpl = new Model_Session();
35 // t oredirect all calls to the root document
36 #define ROOT_DOC std::dynamic_pointer_cast<Model_Document>(moduleDocument())
38 bool Model_Session::load(const char* theFileName)
40 bool aRes = ROOT_DOC->load(theFileName, ROOT_DOC);
44 bool Model_Session::save(const char* theFileName, std::list<std::string>& theResults)
46 return ROOT_DOC->save(theFileName, theResults);
49 void Model_Session::closeAll()
51 Model_Application::getApplication()->deleteAllDocuments();
54 void Model_Session::startOperation(const std::string& theId)
56 ROOT_DOC->startOperation();
57 ROOT_DOC->operationId(theId);
58 static std::shared_ptr<Events_Message> aStartedMsg
59 (new Events_Message(Events_Loop::eventByName("StartOperation")));
60 Events_Loop::loop()->send(aStartedMsg);
61 // remove all useless documents that has been closed: on start of operation undo/redo is cleared
62 std::list<std::shared_ptr<ModelAPI_Document> > aUsedDocs = allOpenedDocuments();
63 Model_Application::getApplication()->removeUselessDocuments(aUsedDocs);
66 void Model_Session::finishOperation()
68 setCheckTransactions(false);
69 ROOT_DOC->finishOperation();
70 setCheckTransactions(true);
73 void Model_Session::abortOperation()
75 setCheckTransactions(false);
76 ROOT_DOC->abortOperation();
77 setCheckTransactions(true);
78 // here the update mechanism may work after abort, so, supress the warnings about
79 // modifications outside of the transactions
80 bool aWasCheck = myCheckTransactions;
81 myCheckTransactions = false;
82 static std::shared_ptr<Events_Message> anAbortMsg
83 (new Events_Message(Events_Loop::eventByName("AbortOperation")));
84 Events_Loop::loop()->send(anAbortMsg);
85 myCheckTransactions = true;
86 myCheckTransactions = aWasCheck;
89 bool Model_Session::isOperation()
91 return ROOT_DOC->isOperation();
94 bool Model_Session::isModified()
96 return ROOT_DOC->isModified();
99 bool Model_Session::canUndo()
101 return ROOT_DOC->canUndo();
104 void Model_Session::undo()
106 setCheckTransactions(false);
108 setCheckTransactions(true);
111 bool Model_Session::canRedo()
113 return ROOT_DOC->canRedo();
116 void Model_Session::redo()
118 setCheckTransactions(false);
120 setCheckTransactions(true);
123 //! Returns stack of performed operations
124 std::list<std::string> Model_Session::undoList()
126 return ROOT_DOC->undoList();
128 //! Returns stack of rolled back operations
129 std::list<std::string> Model_Session::redoList()
131 return ROOT_DOC->redoList();
134 FeaturePtr Model_Session::createFeature(string theFeatureID, Model_Document* theDocOwner)
136 if (this != myImpl) {
137 return myImpl->createFeature(theFeatureID, theDocOwner);
140 // load all information about plugins, features and attributes
143 if (myPlugins.find(theFeatureID) != myPlugins.end()) {
144 std::pair<std::string, std::string>& aPlugin = myPlugins[theFeatureID]; // plugin and doc kind
145 if (!aPlugin.second.empty() && aPlugin.second != theDocOwner->kind()) {
147 string("Feature '") + theFeatureID + "' can be created only in document '"
148 + aPlugin.second + "' by the XML definition");
151 myCurrentPluginName = aPlugin.first;
152 if (myPluginObjs.find(myCurrentPluginName) == myPluginObjs.end()) {
153 // load plugin library if not yet done
154 Config_ModuleReader::loadPlugin(myCurrentPluginName);
156 if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
157 FeaturePtr aCreated = myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
160 string("Can not initialize feature '") + theFeatureID + "' in plugin '"
161 + myCurrentPluginName + "'");
165 Events_Error::send(string("Can not load plugin '") + myCurrentPluginName + "'");
168 Events_Error::send(string("Feature '") + theFeatureID + "' not found in any plugin");
171 return FeaturePtr(); // return nothing
174 std::shared_ptr<ModelAPI_Document> Model_Session::moduleDocument()
176 bool aFirstCall = !Model_Application::getApplication()->hasDocument("root");
178 // creation of the root document is always outside of the transaction, so, avoid checking it
179 setCheckTransactions(false);
181 std::shared_ptr<ModelAPI_Document> aDoc = std::shared_ptr<ModelAPI_Document>(
182 Model_Application::getApplication()->getDocument("root"));
184 // creation of the root document is always outside of the transaction, so, avoid checking it
185 setCheckTransactions(true);
190 std::shared_ptr<ModelAPI_Document> Model_Session::document(std::string theDocID)
192 return std::shared_ptr<ModelAPI_Document>(
193 Model_Application::getApplication()->getDocument(theDocID));
196 bool Model_Session::hasModuleDocument()
198 return Model_Application::getApplication()->hasDocument("root");
201 std::shared_ptr<ModelAPI_Document> Model_Session::activeDocument()
203 if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
204 myCurrentDoc = moduleDocument();
208 void Model_Session::setActiveDocument(
209 std::shared_ptr<ModelAPI_Document> theDoc, bool theSendSignal)
211 if (myCurrentDoc != theDoc) {
212 myCurrentDoc = theDoc;
213 if (theDoc.get() && theSendSignal) {
214 // syncronize the document: it may be just opened or opened but removed before
215 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(theDoc);
217 bool aWasChecked = myCheckTransactions;
218 setCheckTransactions(false);
219 aDoc->objects()->synchronizeFeatures(false, true, true);
221 setCheckTransactions(true);
223 static std::shared_ptr<Events_Message> aMsg(
224 new Events_Message(Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED)));
225 Events_Loop::loop()->send(aMsg);
230 std::list<std::shared_ptr<ModelAPI_Document> > Model_Session::allOpenedDocuments()
232 list<std::shared_ptr<ModelAPI_Document> > aResult;
233 aResult.push_back(moduleDocument());
234 // add subs recursively
235 list<std::shared_ptr<ModelAPI_Document> >::iterator aDoc = aResult.begin();
236 for(; aDoc != aResult.end(); aDoc++) {
237 DocumentPtr anAPIDoc = *aDoc;
238 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(anAPIDoc);
240 const std::set<std::string> aSubs = aDoc->subDocuments(true);
241 std::set<std::string>::const_iterator aSubIter = aSubs.cbegin();
242 for(; aSubIter != aSubs.cend(); aSubIter++) {
243 if (!Model_Application::getApplication()->isLoadByDemand(*aSubIter)) {
244 aResult.push_back(Model_Application::getApplication()->getDocument(*aSubIter));
252 bool Model_Session::isLoadByDemand(const std::string theDocID)
254 return Model_Application::getApplication()->isLoadByDemand(theDocID);
257 std::shared_ptr<ModelAPI_Document> Model_Session::copy(
258 std::shared_ptr<ModelAPI_Document> theSource, std::string theID)
260 // create a new document
261 std::shared_ptr<Model_Document> aNew = std::dynamic_pointer_cast<Model_Document>(
262 Model_Application::getApplication()->getDocument(theID));
263 // make a copy of all labels
264 TDF_Label aSourceRoot = std::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main()
266 TDF_Label aTargetRoot = aNew->document()->Main().Father();
267 Handle(TDF_DataSet) aDS = new TDF_DataSet;
268 aDS->AddLabel(aSourceRoot);
269 TDF_ClosureTool::Closure(aDS);
270 Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
271 aRT->SetRelocation(aSourceRoot, aTargetRoot);
272 TDF_CopyTool::Copy(aDS, aRT);
274 aNew->objects()->synchronizeFeatures(false, true, true);
278 Model_Session::Model_Session()
280 myPluginsInfoLoaded = false;
281 myCheckTransactions = true;
282 ModelAPI_Session::setSession(std::shared_ptr<ModelAPI_Session>(this));
283 // register the configuration reading listener
284 Events_Loop* aLoop = Events_Loop::loop();
285 static const Events_ID kFeatureEvent =
286 Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
287 aLoop->registerListener(this, kFeatureEvent);
288 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED), 0, true);
289 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED), 0, true);
290 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED), 0, true);
291 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_VALIDATOR_LOADED));
294 void Model_Session::processEvent(const std::shared_ptr<Events_Message>& theMessage)
296 static const Events_ID kFeatureEvent =
297 Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
298 static const Events_ID kValidatorEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
299 if (theMessage->eventID() == kFeatureEvent) {
300 const std::shared_ptr<Config_FeatureMessage> aMsg =
301 std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
303 // proccess the plugin info, load plugin
304 if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
305 myPlugins[aMsg->id()] = std::pair<std::string, std::string>(
306 aMsg->pluginLibrary(), aMsg->documentKind());
309 const std::shared_ptr<Config_AttributeMessage> aMsgAttr =
310 std::dynamic_pointer_cast<Config_AttributeMessage>(theMessage);
312 if (!aMsgAttr->isObligatory()) {
313 validators()->registerNotObligatory(aMsgAttr->featureId(), aMsgAttr->attributeId());
315 if(aMsgAttr->isConcealment()) {
316 validators()->registerConcealment(aMsgAttr->featureId(), aMsgAttr->attributeId());
318 if (!aMsgAttr->caseId().empty()) {
319 validators()->registerCase(aMsgAttr->featureId(), aMsgAttr->attributeId(),
320 aMsgAttr->switchId(), aMsgAttr->caseId());
324 // plugins information was started to load, so, it will be loaded
325 myPluginsInfoLoaded = true;
326 } else if (theMessage->eventID() == kValidatorEvent) {
327 std::shared_ptr<Config_ValidatorMessage> aMsg =
328 std::dynamic_pointer_cast<Config_ValidatorMessage>(theMessage);
330 if (aMsg->attributeId().empty()) { // feature validator
331 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->parameters());
332 } else { // attribute validator
333 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->attributeId(),
337 } else { // create/update/delete
338 if (myCheckTransactions && !isOperation())
339 Events_Error::send("Modification of data structure outside of the transaction");
340 // if part is deleted, make the root as the current document (on undo of Parts creations)
341 static const Events_ID kDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
342 if (theMessage->eventID() == kDeletedEvent) {
343 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aDeleted =
344 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
346 aDeleted->groups().find(ModelAPI_ResultPart::group()) != aDeleted->groups().end())
348 setActiveDocument(moduleDocument());
354 void Model_Session::LoadPluginsInfo()
356 if (myPluginsInfoLoaded) // nothing to do
359 // Read plugins information from XML files
360 Config_ModuleReader aModuleReader(Config_FeatureMessage::MODEL_EVENT());
361 aModuleReader.readAll();
362 std::set<std::string> aFiles = aModuleReader.modulePluginFiles();
363 std::set<std::string>::iterator it = aFiles.begin();
364 for ( ; it != aFiles.end(); it++ ) {
365 Config_ValidatorReader aValidatorReader (*it);
366 aValidatorReader.readAll();
371 void Model_Session::registerPlugin(ModelAPI_Plugin* thePlugin)
373 myPluginObjs[myCurrentPluginName] = thePlugin;
374 static Events_ID EVENT_LOAD = Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED);
375 ModelAPI_EventCreator::get()->sendUpdated(ObjectPtr(), EVENT_LOAD);
376 Events_Loop::loop()->flush(EVENT_LOAD);
377 // If the plugin has an ability to process GUI events, register it
378 Events_Listener* aListener = dynamic_cast<Events_Listener*>(thePlugin);
380 Events_Loop* aLoop = Events_Loop::loop();
381 static Events_ID aStateRequestEventId =
382 Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_REQUEST);
383 aLoop->registerListener(aListener, aStateRequestEventId);
387 ModelAPI_ValidatorsFactory* Model_Session::validators()
389 static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;