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 <Events_Loop.h>
16 #include <Events_Error.h>
17 #include <Config_FeatureMessage.h>
18 #include <Config_AttributeMessage.h>
19 #include <Config_ValidatorMessage.h>
20 #include <Config_ModuleReader.h>
21 #include <ModelAPI_ResultPart.h>
23 #include <TDF_CopyTool.hxx>
24 #include <TDF_DataSet.hxx>
25 #include <TDF_RelocationTable.hxx>
26 #include <TDF_ClosureTool.hxx>
30 static Model_Session* myImpl = new Model_Session();
32 // t oredirect all calls to the root document
33 #define ROOT_DOC std::dynamic_pointer_cast<Model_Document>(moduleDocument())
35 bool Model_Session::load(const char* theFileName)
37 return ROOT_DOC->load(theFileName);
40 bool Model_Session::save(const char* theFileName, std::list<std::string>& theResults)
42 return ROOT_DOC->save(theFileName, theResults);
45 void Model_Session::closeAll()
47 ROOT_DOC->close(true);
48 Model_Application::getApplication()->deleteAllDocuments();
51 void Model_Session::startOperation()
53 ROOT_DOC->startOperation();
54 static std::shared_ptr<Events_Message> aStartedMsg
55 (new Events_Message(Events_Loop::eventByName("StartOperation")));
56 Events_Loop::loop()->send(aStartedMsg);
57 // remove all useless documents that has been closed: on start of operation undo/redo is cleared
58 std::list<std::shared_ptr<ModelAPI_Document> > aUsedDocs = allOpenedDocuments();
59 Model_Application::getApplication()->removeUselessDocuments(aUsedDocs);
62 void Model_Session::finishOperation()
64 ROOT_DOC->finishOperation();
67 void Model_Session::abortOperation()
69 ROOT_DOC->abortOperation();
70 // here the update mechanism may work after abort, so, supress the warnings about
71 // modifications outside of the transactions
72 bool aWasCheck = myCheckTransactions;
73 myCheckTransactions = false;
74 static std::shared_ptr<Events_Message> anAbortMsg
75 (new Events_Message(Events_Loop::eventByName("AbortOperation")));
76 Events_Loop::loop()->send(anAbortMsg);
77 myCheckTransactions = true;
78 myCheckTransactions = aWasCheck;
81 bool Model_Session::isOperation()
83 return ROOT_DOC->isOperation();
86 bool Model_Session::isModified()
88 return ROOT_DOC->isModified();
91 bool Model_Session::canUndo()
93 return ROOT_DOC->canUndo();
96 void Model_Session::undo()
101 bool Model_Session::canRedo()
103 return ROOT_DOC->canRedo();
106 void Model_Session::redo()
111 FeaturePtr Model_Session::createFeature(string theFeatureID)
113 if (this != myImpl) {
114 return myImpl->createFeature(theFeatureID);
117 // load all information about plugins, features and attributes
120 if (myPlugins.find(theFeatureID) != myPlugins.end()) {
121 std::pair<std::string, std::string>& aPlugin = myPlugins[theFeatureID]; // plugin and doc kind
122 if (!aPlugin.second.empty() && aPlugin.second != activeDocument()->kind()) {
124 string("Feature '") + theFeatureID + "' can be created only in document '"
125 + aPlugin.second + "' by the XML definition");
128 myCurrentPluginName = aPlugin.first;
129 if (myPluginObjs.find(myCurrentPluginName) == myPluginObjs.end()) {
130 // load plugin library if not yet done
131 Config_ModuleReader::loadPlugin(myCurrentPluginName);
133 if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
134 FeaturePtr aCreated = myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
137 string("Can not initialize feature '") + theFeatureID + "' in plugin '"
138 + myCurrentPluginName + "'");
142 Events_Error::send(string("Can not load plugin '") + myCurrentPluginName + "'");
145 Events_Error::send(string("Feature '") + theFeatureID + "' not found in any plugin");
148 return FeaturePtr(); // return nothing
151 std::shared_ptr<ModelAPI_Document> Model_Session::moduleDocument()
153 return std::shared_ptr<ModelAPI_Document>(
154 Model_Application::getApplication()->getDocument("root"));
157 std::shared_ptr<ModelAPI_Document> Model_Session::document(std::string theDocID)
159 return std::shared_ptr<ModelAPI_Document>(
160 Model_Application::getApplication()->getDocument(theDocID));
163 bool Model_Session::hasModuleDocument()
165 return Model_Application::getApplication()->hasDocument("root");
168 std::shared_ptr<ModelAPI_Document> Model_Session::activeDocument()
170 if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
171 myCurrentDoc = moduleDocument();
175 void Model_Session::setActiveDocument(
176 std::shared_ptr<ModelAPI_Document> theDoc, bool theSendSignal)
178 if (myCurrentDoc != theDoc) {
179 myCurrentDoc = theDoc;
181 static std::shared_ptr<Events_Message> aMsg(new Events_Message(Events_Loop::eventByName("CurrentDocumentChanged")));
182 Events_Loop::loop()->send(aMsg);
187 std::list<std::shared_ptr<ModelAPI_Document> > Model_Session::allOpenedDocuments()
189 list<std::shared_ptr<ModelAPI_Document> > aResult;
190 aResult.push_back(moduleDocument());
191 // add subs recursively
192 list<std::shared_ptr<ModelAPI_Document> >::iterator aDoc = aResult.begin();
193 for(; aDoc != aResult.end(); aDoc++) {
194 DocumentPtr anAPIDoc = *aDoc;
195 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(anAPIDoc);
197 const std::set<std::string> aSubs = aDoc->subDocuments(true);
198 std::set<std::string>::const_iterator aSubIter = aSubs.cbegin();
199 for(; aSubIter != aSubs.cend(); aSubIter++) {
200 if (!Model_Application::getApplication()->isLoadByDemand(*aSubIter)) {
201 aResult.push_back(Model_Application::getApplication()->getDocument(*aSubIter));
209 std::shared_ptr<ModelAPI_Document> Model_Session::copy(
210 std::shared_ptr<ModelAPI_Document> theSource, std::string theID)
212 // create a new document
213 std::shared_ptr<Model_Document> aNew = std::dynamic_pointer_cast<Model_Document>(
214 Model_Application::getApplication()->getDocument(theID));
215 // make a copy of all labels
216 TDF_Label aSourceRoot = std::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main()
218 TDF_Label aTargetRoot = aNew->document()->Main().Father();
219 Handle(TDF_DataSet) aDS = new TDF_DataSet;
220 aDS->AddLabel(aSourceRoot);
221 TDF_ClosureTool::Closure(aDS);
222 Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
223 aRT->SetRelocation(aSourceRoot, aTargetRoot);
224 TDF_CopyTool::Copy(aDS, aRT);
226 aNew->synchronizeFeatures(false, true);
230 Model_Session::Model_Session()
232 myPluginsInfoLoaded = false;
233 myCheckTransactions = true;
234 ModelAPI_Session::setSession(std::shared_ptr<ModelAPI_Session>(this));
235 // register the configuration reading listener
236 Events_Loop* aLoop = Events_Loop::loop();
237 static const Events_ID kFeatureEvent =
238 Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
239 aLoop->registerListener(this, kFeatureEvent);
240 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED), 0, true);
241 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED), 0, true);
242 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED), 0, true);
243 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_VALIDATOR_LOADED));
246 void Model_Session::processEvent(const std::shared_ptr<Events_Message>& theMessage)
248 static const Events_ID kFeatureEvent =
249 Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
250 static const Events_ID kValidatorEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
251 if (theMessage->eventID() == kFeatureEvent) {
252 const std::shared_ptr<Config_FeatureMessage> aMsg =
253 std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
255 // proccess the plugin info, load plugin
256 if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
257 myPlugins[aMsg->id()] = std::pair<std::string, std::string>(
258 aMsg->pluginLibrary(), aMsg->documentKind());
261 const std::shared_ptr<Config_AttributeMessage> aMsgAttr =
262 std::dynamic_pointer_cast<Config_AttributeMessage>(theMessage);
264 if (!aMsgAttr->isObligatory()) {
265 validators()->registerNotObligatory(aMsgAttr->featureId(), aMsgAttr->attributeId());
267 if(aMsgAttr->isConcealment()) {
268 validators()->registerConcealment(aMsgAttr->featureId(), aMsgAttr->attributeId());
273 // plugins information was started to load, so, it will be loaded
274 myPluginsInfoLoaded = true;
275 } else if (theMessage->eventID() == kValidatorEvent) {
276 std::shared_ptr<Config_ValidatorMessage> aMsg =
277 std::dynamic_pointer_cast<Config_ValidatorMessage>(theMessage);
279 if (aMsg->attributeId().empty()) { // feature validator
280 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->parameters());
281 } else { // attribute validator
282 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->attributeId(),
286 } else { // create/update/delete
287 if (myCheckTransactions && !isOperation())
288 Events_Error::send("Modification of data structure outside of the transaction");
289 // if part is deleted, make the root as the current document (on undo of Parts creations)
290 static const Events_ID kDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
291 if (theMessage->eventID() == kDeletedEvent) {
292 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aDeleted =
293 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
295 aDeleted->groups().find(ModelAPI_ResultPart::group()) != aDeleted->groups().end())
297 setActiveDocument(moduleDocument());
303 void Model_Session::LoadPluginsInfo()
305 if (myPluginsInfoLoaded) // nothing to do
308 // Read plugins information from XML files
309 Config_ModuleReader aXMLReader(Config_FeatureMessage::MODEL_EVENT());
310 aXMLReader.readAll();
313 void Model_Session::registerPlugin(ModelAPI_Plugin* thePlugin)
315 myPluginObjs[myCurrentPluginName] = thePlugin;
316 static Events_ID EVENT_LOAD = Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED);
317 ModelAPI_EventCreator::get()->sendUpdated(ObjectPtr(), EVENT_LOAD);
318 Events_Loop::loop()->flush(EVENT_LOAD);
319 // If the plugin has an ability to process GUI events, register it
320 Events_Listener* aListener = dynamic_cast<Events_Listener*>(thePlugin);
322 Events_Loop* aLoop = Events_Loop::loop();
323 static Events_ID aStateRequestEventId =
324 Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_REQUEST);
325 aLoop->registerListener(aListener, aStateRequestEventId);
329 ModelAPI_ValidatorsFactory* Model_Session::validators()
331 static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;