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_Application.h>
13 #include <Model_Events.h>
14 #include <Model_Validator.h>
15 #include <ModelAPI_Events.h>
16 #include <Events_Loop.h>
17 #include <Events_Error.h>
18 #include <Config_FeatureMessage.h>
19 #include <Config_AttributeMessage.h>
20 #include <Config_ValidatorMessage.h>
21 #include <Config_ModuleReader.h>
22 #include <ModelAPI_ResultPart.h>
24 #include <TDF_CopyTool.hxx>
25 #include <TDF_DataSet.hxx>
26 #include <TDF_RelocationTable.hxx>
27 #include <TDF_ClosureTool.hxx>
31 static Model_Session* myImpl = new Model_Session();
33 // t oredirect all calls to the root document
34 #define ROOT_DOC std::dynamic_pointer_cast<Model_Document>(moduleDocument())
36 bool Model_Session::load(const char* theFileName)
38 bool aRes = ROOT_DOC->load(theFileName);
42 bool Model_Session::save(const char* theFileName, std::list<std::string>& theResults)
44 return ROOT_DOC->save(theFileName, theResults);
47 void Model_Session::closeAll()
49 ROOT_DOC->close(true);
50 Model_Application::getApplication()->deleteAllDocuments();
53 void Model_Session::startOperation(const std::string& theId)
55 ROOT_DOC->startOperation();
56 ROOT_DOC->operationId(theId);
57 static std::shared_ptr<Events_Message> aStartedMsg
58 (new Events_Message(Events_Loop::eventByName("StartOperation")));
59 Events_Loop::loop()->send(aStartedMsg);
60 // remove all useless documents that has been closed: on start of operation undo/redo is cleared
61 std::list<std::shared_ptr<ModelAPI_Document> > aUsedDocs = allOpenedDocuments();
62 Model_Application::getApplication()->removeUselessDocuments(aUsedDocs);
65 void Model_Session::finishOperation()
67 setCheckTransactions(false);
68 ROOT_DOC->finishOperation();
69 setCheckTransactions(true);
72 void Model_Session::abortOperation()
74 setCheckTransactions(false);
75 ROOT_DOC->abortOperation();
76 setCheckTransactions(true);
77 // here the update mechanism may work after abort, so, supress the warnings about
78 // modifications outside of the transactions
79 bool aWasCheck = myCheckTransactions;
80 myCheckTransactions = false;
81 static std::shared_ptr<Events_Message> anAbortMsg
82 (new Events_Message(Events_Loop::eventByName("AbortOperation")));
83 Events_Loop::loop()->send(anAbortMsg);
84 myCheckTransactions = true;
85 myCheckTransactions = aWasCheck;
88 bool Model_Session::isOperation()
90 return ROOT_DOC->isOperation();
93 bool Model_Session::isModified()
95 return ROOT_DOC->isModified();
98 bool Model_Session::canUndo()
100 return ROOT_DOC->canUndo();
103 void Model_Session::undo()
105 setCheckTransactions(false);
107 setCheckTransactions(true);
110 bool Model_Session::canRedo()
112 return ROOT_DOC->canRedo();
115 void Model_Session::redo()
117 setCheckTransactions(false);
119 setCheckTransactions(true);
122 //! Returns stack of performed operations
123 std::list<std::string> Model_Session::undoList()
125 return ROOT_DOC->undoList();
127 //! Returns stack of rolled back operations
128 std::list<std::string> Model_Session::redoList()
130 return ROOT_DOC->redoList();
133 FeaturePtr Model_Session::createFeature(string theFeatureID)
135 if (this != myImpl) {
136 return myImpl->createFeature(theFeatureID);
139 // load all information about plugins, features and attributes
142 if (myPlugins.find(theFeatureID) != myPlugins.end()) {
143 std::pair<std::string, std::string>& aPlugin = myPlugins[theFeatureID]; // plugin and doc kind
144 if (!aPlugin.second.empty() && aPlugin.second != activeDocument()->kind()) {
146 string("Feature '") + theFeatureID + "' can be created only in document '"
147 + aPlugin.second + "' by the XML definition");
150 myCurrentPluginName = aPlugin.first;
151 if (myPluginObjs.find(myCurrentPluginName) == myPluginObjs.end()) {
152 // load plugin library if not yet done
153 Config_ModuleReader::loadPlugin(myCurrentPluginName);
155 if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
156 FeaturePtr aCreated = myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
159 string("Can not initialize feature '") + theFeatureID + "' in plugin '"
160 + myCurrentPluginName + "'");
164 Events_Error::send(string("Can not load plugin '") + myCurrentPluginName + "'");
167 Events_Error::send(string("Feature '") + theFeatureID + "' not found in any plugin");
170 return FeaturePtr(); // return nothing
173 std::shared_ptr<ModelAPI_Document> Model_Session::moduleDocument()
175 return std::shared_ptr<ModelAPI_Document>(
176 Model_Application::getApplication()->getDocument("root"));
179 std::shared_ptr<ModelAPI_Document> Model_Session::document(std::string theDocID)
181 return std::shared_ptr<ModelAPI_Document>(
182 Model_Application::getApplication()->getDocument(theDocID));
185 bool Model_Session::hasModuleDocument()
187 return Model_Application::getApplication()->hasDocument("root");
190 std::shared_ptr<ModelAPI_Document> Model_Session::activeDocument()
192 if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
193 myCurrentDoc = moduleDocument();
197 void Model_Session::setActiveDocument(
198 std::shared_ptr<ModelAPI_Document> theDoc, bool theSendSignal)
200 if (myCurrentDoc != theDoc) {
201 myCurrentDoc = theDoc;
202 if (theDoc.get() && theSendSignal) {
203 // syncronize the document: it may be just opened or opened but removed before
204 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(theDoc);
206 bool aWasChecked = myCheckTransactions;
207 setCheckTransactions(false);
208 aDoc->synchronizeFeatures(false, true);
210 setCheckTransactions(true);
212 static std::shared_ptr<Events_Message> aMsg(
213 new Events_Message(Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED)));
214 Events_Loop::loop()->send(aMsg);
219 std::list<std::shared_ptr<ModelAPI_Document> > Model_Session::allOpenedDocuments()
221 list<std::shared_ptr<ModelAPI_Document> > aResult;
222 aResult.push_back(moduleDocument());
223 // add subs recursively
224 list<std::shared_ptr<ModelAPI_Document> >::iterator aDoc = aResult.begin();
225 for(; aDoc != aResult.end(); aDoc++) {
226 DocumentPtr anAPIDoc = *aDoc;
227 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(anAPIDoc);
229 const std::set<std::string> aSubs = aDoc->subDocuments(true);
230 std::set<std::string>::const_iterator aSubIter = aSubs.cbegin();
231 for(; aSubIter != aSubs.cend(); aSubIter++) {
232 if (!Model_Application::getApplication()->isLoadByDemand(*aSubIter)) {
233 aResult.push_back(Model_Application::getApplication()->getDocument(*aSubIter));
241 std::shared_ptr<ModelAPI_Document> Model_Session::copy(
242 std::shared_ptr<ModelAPI_Document> theSource, std::string theID)
244 // create a new document
245 std::shared_ptr<Model_Document> aNew = std::dynamic_pointer_cast<Model_Document>(
246 Model_Application::getApplication()->getDocument(theID));
247 // make a copy of all labels
248 TDF_Label aSourceRoot = std::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main()
250 TDF_Label aTargetRoot = aNew->document()->Main().Father();
251 Handle(TDF_DataSet) aDS = new TDF_DataSet;
252 aDS->AddLabel(aSourceRoot);
253 TDF_ClosureTool::Closure(aDS);
254 Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
255 aRT->SetRelocation(aSourceRoot, aTargetRoot);
256 TDF_CopyTool::Copy(aDS, aRT);
258 aNew->synchronizeFeatures(false, true);
262 Model_Session::Model_Session()
264 myPluginsInfoLoaded = false;
265 myCheckTransactions = true;
266 ModelAPI_Session::setSession(std::shared_ptr<ModelAPI_Session>(this));
267 // register the configuration reading listener
268 Events_Loop* aLoop = Events_Loop::loop();
269 static const Events_ID kFeatureEvent =
270 Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
271 aLoop->registerListener(this, kFeatureEvent);
272 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED), 0, true);
273 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED), 0, true);
274 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED), 0, true);
275 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_VALIDATOR_LOADED));
278 void Model_Session::processEvent(const std::shared_ptr<Events_Message>& theMessage)
280 static const Events_ID kFeatureEvent =
281 Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
282 static const Events_ID kValidatorEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
283 if (theMessage->eventID() == kFeatureEvent) {
284 const std::shared_ptr<Config_FeatureMessage> aMsg =
285 std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
287 // proccess the plugin info, load plugin
288 if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
289 myPlugins[aMsg->id()] = std::pair<std::string, std::string>(
290 aMsg->pluginLibrary(), aMsg->documentKind());
293 const std::shared_ptr<Config_AttributeMessage> aMsgAttr =
294 std::dynamic_pointer_cast<Config_AttributeMessage>(theMessage);
296 if (!aMsgAttr->isObligatory()) {
297 validators()->registerNotObligatory(aMsgAttr->featureId(), aMsgAttr->attributeId());
299 if(aMsgAttr->isConcealment()) {
300 validators()->registerConcealment(aMsgAttr->featureId(), aMsgAttr->attributeId());
305 // plugins information was started to load, so, it will be loaded
306 myPluginsInfoLoaded = true;
307 } else if (theMessage->eventID() == kValidatorEvent) {
308 std::shared_ptr<Config_ValidatorMessage> aMsg =
309 std::dynamic_pointer_cast<Config_ValidatorMessage>(theMessage);
311 if (aMsg->attributeId().empty()) { // feature validator
312 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->parameters());
313 } else { // attribute validator
314 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->attributeId(),
318 } else { // create/update/delete
319 if (myCheckTransactions && !isOperation())
320 Events_Error::send("Modification of data structure outside of the transaction");
321 // if part is deleted, make the root as the current document (on undo of Parts creations)
322 static const Events_ID kDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
323 if (theMessage->eventID() == kDeletedEvent) {
324 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aDeleted =
325 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
327 aDeleted->groups().find(ModelAPI_ResultPart::group()) != aDeleted->groups().end())
329 setActiveDocument(moduleDocument());
335 void Model_Session::LoadPluginsInfo()
337 if (myPluginsInfoLoaded) // nothing to do
340 // Read plugins information from XML files
341 Config_ModuleReader aXMLReader(Config_FeatureMessage::MODEL_EVENT());
342 aXMLReader.readAll();
345 void Model_Session::registerPlugin(ModelAPI_Plugin* thePlugin)
347 myPluginObjs[myCurrentPluginName] = thePlugin;
348 static Events_ID EVENT_LOAD = Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED);
349 ModelAPI_EventCreator::get()->sendUpdated(ObjectPtr(), EVENT_LOAD);
350 Events_Loop::loop()->flush(EVENT_LOAD);
351 // If the plugin has an ability to process GUI events, register it
352 Events_Listener* aListener = dynamic_cast<Events_Listener*>(thePlugin);
354 Events_Loop* aLoop = Events_Loop::loop();
355 static Events_ID aStateRequestEventId =
356 Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_REQUEST);
357 aLoop->registerListener(aListener, aStateRequestEventId);
361 ModelAPI_ValidatorsFactory* Model_Session::validators()
363 static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;