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()
55 ROOT_DOC->startOperation();
56 static std::shared_ptr<Events_Message> aStartedMsg
57 (new Events_Message(Events_Loop::eventByName("StartOperation")));
58 Events_Loop::loop()->send(aStartedMsg);
59 // remove all useless documents that has been closed: on start of operation undo/redo is cleared
60 std::list<std::shared_ptr<ModelAPI_Document> > aUsedDocs = allOpenedDocuments();
61 Model_Application::getApplication()->removeUselessDocuments(aUsedDocs);
64 void Model_Session::finishOperation()
66 setCheckTransactions(false);
67 ROOT_DOC->finishOperation();
68 setCheckTransactions(true);
71 void Model_Session::abortOperation()
73 setCheckTransactions(false);
74 ROOT_DOC->abortOperation();
75 setCheckTransactions(true);
76 // here the update mechanism may work after abort, so, supress the warnings about
77 // modifications outside of the transactions
78 bool aWasCheck = myCheckTransactions;
79 myCheckTransactions = false;
80 static std::shared_ptr<Events_Message> anAbortMsg
81 (new Events_Message(Events_Loop::eventByName("AbortOperation")));
82 Events_Loop::loop()->send(anAbortMsg);
83 myCheckTransactions = true;
84 myCheckTransactions = aWasCheck;
87 bool Model_Session::isOperation()
89 return ROOT_DOC->isOperation();
92 bool Model_Session::isModified()
94 return ROOT_DOC->isModified();
97 bool Model_Session::canUndo()
99 return ROOT_DOC->canUndo();
102 void Model_Session::undo()
104 setCheckTransactions(false);
106 setCheckTransactions(true);
109 bool Model_Session::canRedo()
111 return ROOT_DOC->canRedo();
114 void Model_Session::redo()
116 setCheckTransactions(false);
118 setCheckTransactions(true);
121 FeaturePtr Model_Session::createFeature(string theFeatureID)
123 if (this != myImpl) {
124 return myImpl->createFeature(theFeatureID);
127 // load all information about plugins, features and attributes
130 if (myPlugins.find(theFeatureID) != myPlugins.end()) {
131 std::pair<std::string, std::string>& aPlugin = myPlugins[theFeatureID]; // plugin and doc kind
132 if (!aPlugin.second.empty() && aPlugin.second != activeDocument()->kind()) {
134 string("Feature '") + theFeatureID + "' can be created only in document '"
135 + aPlugin.second + "' by the XML definition");
138 myCurrentPluginName = aPlugin.first;
139 if (myPluginObjs.find(myCurrentPluginName) == myPluginObjs.end()) {
140 // load plugin library if not yet done
141 Config_ModuleReader::loadPlugin(myCurrentPluginName);
143 if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
144 FeaturePtr aCreated = myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
147 string("Can not initialize feature '") + theFeatureID + "' in plugin '"
148 + myCurrentPluginName + "'");
152 Events_Error::send(string("Can not load plugin '") + myCurrentPluginName + "'");
155 Events_Error::send(string("Feature '") + theFeatureID + "' not found in any plugin");
158 return FeaturePtr(); // return nothing
161 std::shared_ptr<ModelAPI_Document> Model_Session::moduleDocument()
163 return std::shared_ptr<ModelAPI_Document>(
164 Model_Application::getApplication()->getDocument("root"));
167 std::shared_ptr<ModelAPI_Document> Model_Session::document(std::string theDocID)
169 return std::shared_ptr<ModelAPI_Document>(
170 Model_Application::getApplication()->getDocument(theDocID));
173 bool Model_Session::hasModuleDocument()
175 return Model_Application::getApplication()->hasDocument("root");
178 std::shared_ptr<ModelAPI_Document> Model_Session::activeDocument()
180 if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
181 myCurrentDoc = moduleDocument();
185 void Model_Session::setActiveDocument(
186 std::shared_ptr<ModelAPI_Document> theDoc, bool theSendSignal)
188 if (myCurrentDoc != theDoc) {
189 myCurrentDoc = theDoc;
190 if (theDoc.get() && theSendSignal) {
191 // syncronize the document: it may be just opened or opened but removed before
192 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(theDoc);
194 bool aWasChecked = myCheckTransactions;
195 setCheckTransactions(false);
196 aDoc->synchronizeFeatures(false, true);
198 setCheckTransactions(true);
200 static std::shared_ptr<Events_Message> aMsg(
201 new Events_Message(Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED)));
202 Events_Loop::loop()->send(aMsg);
207 std::list<std::shared_ptr<ModelAPI_Document> > Model_Session::allOpenedDocuments()
209 list<std::shared_ptr<ModelAPI_Document> > aResult;
210 aResult.push_back(moduleDocument());
211 // add subs recursively
212 list<std::shared_ptr<ModelAPI_Document> >::iterator aDoc = aResult.begin();
213 for(; aDoc != aResult.end(); aDoc++) {
214 DocumentPtr anAPIDoc = *aDoc;
215 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(anAPIDoc);
217 const std::set<std::string> aSubs = aDoc->subDocuments(true);
218 std::set<std::string>::const_iterator aSubIter = aSubs.cbegin();
219 for(; aSubIter != aSubs.cend(); aSubIter++) {
220 if (!Model_Application::getApplication()->isLoadByDemand(*aSubIter)) {
221 aResult.push_back(Model_Application::getApplication()->getDocument(*aSubIter));
229 std::shared_ptr<ModelAPI_Document> Model_Session::copy(
230 std::shared_ptr<ModelAPI_Document> theSource, std::string theID)
232 // create a new document
233 std::shared_ptr<Model_Document> aNew = std::dynamic_pointer_cast<Model_Document>(
234 Model_Application::getApplication()->getDocument(theID));
235 // make a copy of all labels
236 TDF_Label aSourceRoot = std::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main()
238 TDF_Label aTargetRoot = aNew->document()->Main().Father();
239 Handle(TDF_DataSet) aDS = new TDF_DataSet;
240 aDS->AddLabel(aSourceRoot);
241 TDF_ClosureTool::Closure(aDS);
242 Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
243 aRT->SetRelocation(aSourceRoot, aTargetRoot);
244 TDF_CopyTool::Copy(aDS, aRT);
246 aNew->synchronizeFeatures(false, true);
250 Model_Session::Model_Session()
252 myPluginsInfoLoaded = false;
253 myCheckTransactions = true;
254 ModelAPI_Session::setSession(std::shared_ptr<ModelAPI_Session>(this));
255 // register the configuration reading listener
256 Events_Loop* aLoop = Events_Loop::loop();
257 static const Events_ID kFeatureEvent =
258 Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
259 aLoop->registerListener(this, kFeatureEvent);
260 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED), 0, true);
261 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED), 0, true);
262 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED), 0, true);
263 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_VALIDATOR_LOADED));
266 void Model_Session::processEvent(const std::shared_ptr<Events_Message>& theMessage)
268 static const Events_ID kFeatureEvent =
269 Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
270 static const Events_ID kValidatorEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
271 if (theMessage->eventID() == kFeatureEvent) {
272 const std::shared_ptr<Config_FeatureMessage> aMsg =
273 std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
275 // proccess the plugin info, load plugin
276 if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
277 myPlugins[aMsg->id()] = std::pair<std::string, std::string>(
278 aMsg->pluginLibrary(), aMsg->documentKind());
281 const std::shared_ptr<Config_AttributeMessage> aMsgAttr =
282 std::dynamic_pointer_cast<Config_AttributeMessage>(theMessage);
284 if (!aMsgAttr->isObligatory()) {
285 validators()->registerNotObligatory(aMsgAttr->featureId(), aMsgAttr->attributeId());
287 if(aMsgAttr->isConcealment()) {
288 validators()->registerConcealment(aMsgAttr->featureId(), aMsgAttr->attributeId());
293 // plugins information was started to load, so, it will be loaded
294 myPluginsInfoLoaded = true;
295 } else if (theMessage->eventID() == kValidatorEvent) {
296 std::shared_ptr<Config_ValidatorMessage> aMsg =
297 std::dynamic_pointer_cast<Config_ValidatorMessage>(theMessage);
299 if (aMsg->attributeId().empty()) { // feature validator
300 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->parameters());
301 } else { // attribute validator
302 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->attributeId(),
306 } else { // create/update/delete
307 if (myCheckTransactions && !isOperation())
308 Events_Error::send("Modification of data structure outside of the transaction");
309 // if part is deleted, make the root as the current document (on undo of Parts creations)
310 static const Events_ID kDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
311 if (theMessage->eventID() == kDeletedEvent) {
312 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aDeleted =
313 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
315 aDeleted->groups().find(ModelAPI_ResultPart::group()) != aDeleted->groups().end())
317 setActiveDocument(moduleDocument());
323 void Model_Session::LoadPluginsInfo()
325 if (myPluginsInfoLoaded) // nothing to do
328 // Read plugins information from XML files
329 Config_ModuleReader aXMLReader(Config_FeatureMessage::MODEL_EVENT());
330 aXMLReader.readAll();
333 void Model_Session::registerPlugin(ModelAPI_Plugin* thePlugin)
335 myPluginObjs[myCurrentPluginName] = thePlugin;
336 static Events_ID EVENT_LOAD = Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED);
337 ModelAPI_EventCreator::get()->sendUpdated(ObjectPtr(), EVENT_LOAD);
338 Events_Loop::loop()->flush(EVENT_LOAD);
339 // If the plugin has an ability to process GUI events, register it
340 Events_Listener* aListener = dynamic_cast<Events_Listener*>(thePlugin);
342 Events_Loop* aLoop = Events_Loop::loop();
343 static Events_ID aStateRequestEventId =
344 Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_REQUEST);
345 aLoop->registerListener(aListener, aStateRequestEventId);
349 ModelAPI_ValidatorsFactory* Model_Session::validators()
351 static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;