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();
52 ROOT_DOC->close(true);
55 void Model_Session::startOperation(const std::string& theId)
57 ROOT_DOC->startOperation();
58 ROOT_DOC->operationId(theId);
59 static std::shared_ptr<Events_Message> aStartedMsg
60 (new Events_Message(Events_Loop::eventByName("StartOperation")));
61 Events_Loop::loop()->send(aStartedMsg);
62 // remove all useless documents that has been closed: on start of operation undo/redo is cleared
63 std::list<std::shared_ptr<ModelAPI_Document> > aUsedDocs = allOpenedDocuments();
64 Model_Application::getApplication()->removeUselessDocuments(aUsedDocs);
67 void Model_Session::finishOperation()
69 setCheckTransactions(false);
70 ROOT_DOC->finishOperation();
71 setCheckTransactions(true);
74 void Model_Session::abortOperation()
76 setCheckTransactions(false);
77 ROOT_DOC->abortOperation();
78 setCheckTransactions(true);
79 // here the update mechanism may work after abort, so, supress the warnings about
80 // modifications outside of the transactions
81 bool aWasCheck = myCheckTransactions;
82 myCheckTransactions = false;
83 static std::shared_ptr<Events_Message> anAbortMsg
84 (new Events_Message(Events_Loop::eventByName("AbortOperation")));
85 Events_Loop::loop()->send(anAbortMsg);
86 myCheckTransactions = true;
87 myCheckTransactions = aWasCheck;
90 bool Model_Session::isOperation()
92 return ROOT_DOC->isOperation();
95 bool Model_Session::isModified()
97 return ROOT_DOC->isModified();
100 bool Model_Session::canUndo()
102 return ROOT_DOC->canUndo();
105 void Model_Session::undo()
107 setCheckTransactions(false);
109 setCheckTransactions(true);
112 bool Model_Session::canRedo()
114 return ROOT_DOC->canRedo();
117 void Model_Session::redo()
119 setCheckTransactions(false);
121 setCheckTransactions(true);
124 //! Returns stack of performed operations
125 std::list<std::string> Model_Session::undoList()
127 return ROOT_DOC->undoList();
129 //! Returns stack of rolled back operations
130 std::list<std::string> Model_Session::redoList()
132 return ROOT_DOC->redoList();
135 FeaturePtr Model_Session::createFeature(string theFeatureID, Model_Document* theDocOwner)
137 if (this != myImpl) {
138 return myImpl->createFeature(theFeatureID, theDocOwner);
141 // load all information about plugins, features and attributes
144 if (myPlugins.find(theFeatureID) != myPlugins.end()) {
145 std::pair<std::string, std::string>& aPlugin = myPlugins[theFeatureID]; // plugin and doc kind
146 if (!aPlugin.second.empty() && aPlugin.second != theDocOwner->kind()) {
148 string("Feature '") + theFeatureID + "' can be created only in document '"
149 + aPlugin.second + "' by the XML definition");
152 myCurrentPluginName = aPlugin.first;
153 if (myPluginObjs.find(myCurrentPluginName) == myPluginObjs.end()) {
154 // load plugin library if not yet done
155 Config_ModuleReader::loadPlugin(myCurrentPluginName);
157 if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
158 FeaturePtr aCreated = myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
161 string("Can not initialize feature '") + theFeatureID + "' in plugin '"
162 + myCurrentPluginName + "'");
166 Events_Error::send(string("Can not load plugin '") + myCurrentPluginName + "'");
169 Events_Error::send(string("Feature '") + theFeatureID + "' not found in any plugin");
172 return FeaturePtr(); // return nothing
175 std::shared_ptr<ModelAPI_Document> Model_Session::moduleDocument()
177 bool aFirstCall = !Model_Application::getApplication()->hasDocument("root");
179 // creation of the root document is always outside of the transaction, so, avoid checking it
180 setCheckTransactions(false);
182 std::shared_ptr<ModelAPI_Document> aDoc = std::shared_ptr<ModelAPI_Document>(
183 Model_Application::getApplication()->getDocument("root"));
185 // creation of the root document is always outside of the transaction, so, avoid checking it
186 setCheckTransactions(true);
191 std::shared_ptr<ModelAPI_Document> Model_Session::document(std::string theDocID)
193 return std::shared_ptr<ModelAPI_Document>(
194 Model_Application::getApplication()->getDocument(theDocID));
197 bool Model_Session::hasModuleDocument()
199 return Model_Application::getApplication()->hasDocument("root");
202 std::shared_ptr<ModelAPI_Document> Model_Session::activeDocument()
204 if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
205 myCurrentDoc = moduleDocument();
209 void Model_Session::setActiveDocument(
210 std::shared_ptr<ModelAPI_Document> theDoc, bool theSendSignal)
212 if (myCurrentDoc != theDoc) {
213 myCurrentDoc = theDoc;
214 if (theDoc.get() && theSendSignal) {
215 // syncronize the document: it may be just opened or opened but removed before
216 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(theDoc);
218 bool aWasChecked = myCheckTransactions;
219 setCheckTransactions(false);
220 aDoc->objects()->synchronizeFeatures(false, true, true);
222 setCheckTransactions(true);
224 static std::shared_ptr<Events_Message> aMsg(
225 new Events_Message(Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED)));
226 Events_Loop::loop()->send(aMsg);
231 std::list<std::shared_ptr<ModelAPI_Document> > Model_Session::allOpenedDocuments()
233 list<std::shared_ptr<ModelAPI_Document> > aResult;
234 aResult.push_back(moduleDocument());
235 // add subs recursively
236 list<std::shared_ptr<ModelAPI_Document> >::iterator aDoc = aResult.begin();
237 for(; aDoc != aResult.end(); aDoc++) {
238 DocumentPtr anAPIDoc = *aDoc;
239 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(anAPIDoc);
241 const std::set<std::string> aSubs = aDoc->subDocuments(true);
242 std::set<std::string>::const_iterator aSubIter = aSubs.cbegin();
243 for(; aSubIter != aSubs.cend(); aSubIter++) {
244 if (!Model_Application::getApplication()->isLoadByDemand(*aSubIter)) {
245 aResult.push_back(Model_Application::getApplication()->getDocument(*aSubIter));
253 bool Model_Session::isLoadByDemand(const std::string theDocID)
255 return Model_Application::getApplication()->isLoadByDemand(theDocID);
258 std::shared_ptr<ModelAPI_Document> Model_Session::copy(
259 std::shared_ptr<ModelAPI_Document> theSource, std::string theID)
261 // create a new document
262 std::shared_ptr<Model_Document> aNew = std::dynamic_pointer_cast<Model_Document>(
263 Model_Application::getApplication()->getDocument(theID));
264 // make a copy of all labels
265 TDF_Label aSourceRoot = std::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main()
267 TDF_Label aTargetRoot = aNew->document()->Main().Father();
268 Handle(TDF_DataSet) aDS = new TDF_DataSet;
269 aDS->AddLabel(aSourceRoot);
270 TDF_ClosureTool::Closure(aDS);
271 Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
272 aRT->SetRelocation(aSourceRoot, aTargetRoot);
273 TDF_CopyTool::Copy(aDS, aRT);
275 aNew->objects()->synchronizeFeatures(false, true, true);
279 Model_Session::Model_Session()
281 myPluginsInfoLoaded = false;
282 myCheckTransactions = true;
283 ModelAPI_Session::setSession(std::shared_ptr<ModelAPI_Session>(this));
284 // register the configuration reading listener
285 Events_Loop* aLoop = Events_Loop::loop();
286 static const Events_ID kFeatureEvent =
287 Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
288 aLoop->registerListener(this, kFeatureEvent);
289 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED), 0, true);
290 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED), 0, true);
291 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED), 0, true);
292 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_VALIDATOR_LOADED));
295 void Model_Session::processEvent(const std::shared_ptr<Events_Message>& theMessage)
297 static const Events_ID kFeatureEvent =
298 Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
299 static const Events_ID kValidatorEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
300 if (theMessage->eventID() == kFeatureEvent) {
301 const std::shared_ptr<Config_FeatureMessage> aMsg =
302 std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
304 // proccess the plugin info, load plugin
305 if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
306 myPlugins[aMsg->id()] = std::pair<std::string, std::string>(
307 aMsg->pluginLibrary(), aMsg->documentKind());
310 const std::shared_ptr<Config_AttributeMessage> aMsgAttr =
311 std::dynamic_pointer_cast<Config_AttributeMessage>(theMessage);
313 if (!aMsgAttr->isObligatory()) {
314 validators()->registerNotObligatory(aMsgAttr->featureId(), aMsgAttr->attributeId());
316 if(aMsgAttr->isConcealment()) {
317 validators()->registerConcealment(aMsgAttr->featureId(), aMsgAttr->attributeId());
319 if (!aMsgAttr->caseId().empty()) {
320 validators()->registerCase(aMsgAttr->featureId(), aMsgAttr->attributeId(),
321 aMsgAttr->switchId(), aMsgAttr->caseId());
325 // plugins information was started to load, so, it will be loaded
326 myPluginsInfoLoaded = true;
327 } else if (theMessage->eventID() == kValidatorEvent) {
328 std::shared_ptr<Config_ValidatorMessage> aMsg =
329 std::dynamic_pointer_cast<Config_ValidatorMessage>(theMessage);
331 if (aMsg->attributeId().empty()) { // feature validator
332 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->parameters());
333 } else { // attribute validator
334 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->attributeId(),
338 } else { // create/update/delete
339 if (myCheckTransactions && !isOperation())
340 Events_Error::send("Modification of data structure outside of the transaction");
341 // if part is deleted, make the root as the current document (on undo of Parts creations)
342 static const Events_ID kDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
343 if (theMessage->eventID() == kDeletedEvent) {
344 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aDeleted =
345 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
347 aDeleted->groups().find(ModelAPI_ResultPart::group()) != aDeleted->groups().end())
349 setActiveDocument(moduleDocument());
355 void Model_Session::LoadPluginsInfo()
357 if (myPluginsInfoLoaded) // nothing to do
360 // Read plugins information from XML files
361 Config_ModuleReader aModuleReader(Config_FeatureMessage::MODEL_EVENT());
362 aModuleReader.readAll();
363 std::set<std::string> aFiles = aModuleReader.modulePluginFiles();
364 std::set<std::string>::iterator it = aFiles.begin();
365 for ( ; it != aFiles.end(); it++ ) {
366 Config_ValidatorReader aValidatorReader (*it);
367 aValidatorReader.readAll();
372 void Model_Session::registerPlugin(ModelAPI_Plugin* thePlugin)
374 myPluginObjs[myCurrentPluginName] = thePlugin;
375 static Events_ID EVENT_LOAD = Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED);
376 ModelAPI_EventCreator::get()->sendUpdated(ObjectPtr(), EVENT_LOAD);
377 Events_Loop::loop()->flush(EVENT_LOAD);
378 // If the plugin has an ability to process GUI events, register it
379 Events_Listener* aListener = dynamic_cast<Events_Listener*>(thePlugin);
381 Events_Loop* aLoop = Events_Loop::loop();
382 static Events_ID aStateRequestEventId =
383 Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_REQUEST);
384 aLoop->registerListener(aListener, aStateRequestEventId);
388 ModelAPI_ValidatorsFactory* Model_Session::validators()
390 static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;