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>
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(
182 new Events_Message(Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED)));
183 Events_Loop::loop()->send(aMsg);
188 std::list<std::shared_ptr<ModelAPI_Document> > Model_Session::allOpenedDocuments()
190 list<std::shared_ptr<ModelAPI_Document> > aResult;
191 aResult.push_back(moduleDocument());
192 // add subs recursively
193 list<std::shared_ptr<ModelAPI_Document> >::iterator aDoc = aResult.begin();
194 for(; aDoc != aResult.end(); aDoc++) {
195 DocumentPtr anAPIDoc = *aDoc;
196 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(anAPIDoc);
198 const std::set<std::string> aSubs = aDoc->subDocuments(true);
199 std::set<std::string>::const_iterator aSubIter = aSubs.cbegin();
200 for(; aSubIter != aSubs.cend(); aSubIter++) {
201 if (!Model_Application::getApplication()->isLoadByDemand(*aSubIter)) {
202 aResult.push_back(Model_Application::getApplication()->getDocument(*aSubIter));
210 std::shared_ptr<ModelAPI_Document> Model_Session::copy(
211 std::shared_ptr<ModelAPI_Document> theSource, std::string theID)
213 // create a new document
214 std::shared_ptr<Model_Document> aNew = std::dynamic_pointer_cast<Model_Document>(
215 Model_Application::getApplication()->getDocument(theID));
216 // make a copy of all labels
217 TDF_Label aSourceRoot = std::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main()
219 TDF_Label aTargetRoot = aNew->document()->Main().Father();
220 Handle(TDF_DataSet) aDS = new TDF_DataSet;
221 aDS->AddLabel(aSourceRoot);
222 TDF_ClosureTool::Closure(aDS);
223 Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
224 aRT->SetRelocation(aSourceRoot, aTargetRoot);
225 TDF_CopyTool::Copy(aDS, aRT);
227 aNew->synchronizeFeatures(false, true);
231 Model_Session::Model_Session()
233 myPluginsInfoLoaded = false;
234 myCheckTransactions = true;
235 ModelAPI_Session::setSession(std::shared_ptr<ModelAPI_Session>(this));
236 // register the configuration reading listener
237 Events_Loop* aLoop = Events_Loop::loop();
238 static const Events_ID kFeatureEvent = 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 = Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
249 static const Events_ID kValidatorEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
250 if (theMessage->eventID() == kFeatureEvent) {
251 const std::shared_ptr<Config_FeatureMessage> aMsg =
252 std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
254 // proccess the plugin info, load plugin
255 if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
256 myPlugins[aMsg->id()] = std::pair<std::string, std::string>(
257 aMsg->pluginLibrary(), aMsg->documentKind());
260 const std::shared_ptr<Config_AttributeMessage> aMsgAttr =
261 std::dynamic_pointer_cast<Config_AttributeMessage>(theMessage);
263 if (!aMsgAttr->isObligatory()) {
264 validators()->registerNotObligatory(aMsgAttr->featureId(), aMsgAttr->attributeId());
266 if(aMsgAttr->isConcealment()) {
267 validators()->registerConcealment(aMsgAttr->featureId(), aMsgAttr->attributeId());
272 // plugins information was started to load, so, it will be loaded
273 myPluginsInfoLoaded = true;
274 } else if (theMessage->eventID() == kValidatorEvent) {
275 std::shared_ptr<Config_ValidatorMessage> aMsg =
276 std::dynamic_pointer_cast<Config_ValidatorMessage>(theMessage);
278 if (aMsg->attributeId().empty()) { // feature validator
279 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->parameters());
280 } else { // attribute validator
281 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->attributeId(),
285 } else { // create/update/delete
286 if (myCheckTransactions && !isOperation())
287 Events_Error::send("Modification of data structure outside of the transaction");
291 void Model_Session::LoadPluginsInfo()
293 if (myPluginsInfoLoaded) // nothing to do
296 // Read plugins information from XML files
297 Config_ModuleReader aXMLReader(Config_FeatureMessage::MODEL_EVENT());
298 aXMLReader.readAll();
301 void Model_Session::registerPlugin(ModelAPI_Plugin* thePlugin)
303 myPluginObjs[myCurrentPluginName] = thePlugin;
304 static Events_ID EVENT_LOAD = Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED);
305 ModelAPI_EventCreator::get()->sendUpdated(ObjectPtr(), EVENT_LOAD);
306 Events_Loop::loop()->flush(EVENT_LOAD);
307 // If the plugin has an ability to process GUI events, register it
308 Events_Listener* aListener = dynamic_cast<Events_Listener*>(thePlugin);
310 Events_Loop* aLoop = Events_Loop::loop();
311 static Events_ID aStateRequestEventId =
312 Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_REQUEST);
313 aLoop->registerListener(aListener, aStateRequestEventId);
317 ModelAPI_ValidatorsFactory* Model_Session::validators()
319 static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;