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 return ROOT_DOC->load(theFileName);
41 bool Model_Session::save(const char* theFileName, std::list<std::string>& theResults)
43 return ROOT_DOC->save(theFileName, theResults);
46 void Model_Session::closeAll()
48 ROOT_DOC->close(true);
49 Model_Application::getApplication()->deleteAllDocuments();
52 void Model_Session::startOperation()
54 ROOT_DOC->startOperation();
55 static std::shared_ptr<Events_Message> aStartedMsg
56 (new Events_Message(Events_Loop::eventByName("StartOperation")));
57 Events_Loop::loop()->send(aStartedMsg);
58 // remove all useless documents that has been closed: on start of operation undo/redo is cleared
59 std::list<std::shared_ptr<ModelAPI_Document> > aUsedDocs = allOpenedDocuments();
60 Model_Application::getApplication()->removeUselessDocuments(aUsedDocs);
63 void Model_Session::finishOperation()
65 ROOT_DOC->finishOperation();
68 void Model_Session::abortOperation()
70 ROOT_DOC->abortOperation();
71 // here the update mechanism may work after abort, so, supress the warnings about
72 // modifications outside of the transactions
73 bool aWasCheck = myCheckTransactions;
74 myCheckTransactions = false;
75 static std::shared_ptr<Events_Message> anAbortMsg
76 (new Events_Message(Events_Loop::eventByName("AbortOperation")));
77 Events_Loop::loop()->send(anAbortMsg);
78 myCheckTransactions = true;
79 myCheckTransactions = aWasCheck;
82 bool Model_Session::isOperation()
84 return ROOT_DOC->isOperation();
87 bool Model_Session::isModified()
89 return ROOT_DOC->isModified();
92 bool Model_Session::canUndo()
94 return ROOT_DOC->canUndo();
97 void Model_Session::undo()
102 bool Model_Session::canRedo()
104 return ROOT_DOC->canRedo();
107 void Model_Session::redo()
112 FeaturePtr Model_Session::createFeature(string theFeatureID)
114 if (this != myImpl) {
115 return myImpl->createFeature(theFeatureID);
118 // load all information about plugins, features and attributes
121 if (myPlugins.find(theFeatureID) != myPlugins.end()) {
122 std::pair<std::string, std::string>& aPlugin = myPlugins[theFeatureID]; // plugin and doc kind
123 if (!aPlugin.second.empty() && aPlugin.second != activeDocument()->kind()) {
125 string("Feature '") + theFeatureID + "' can be created only in document '"
126 + aPlugin.second + "' by the XML definition");
129 myCurrentPluginName = aPlugin.first;
130 if (myPluginObjs.find(myCurrentPluginName) == myPluginObjs.end()) {
131 // load plugin library if not yet done
132 Config_ModuleReader::loadPlugin(myCurrentPluginName);
134 if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
135 FeaturePtr aCreated = myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
138 string("Can not initialize feature '") + theFeatureID + "' in plugin '"
139 + myCurrentPluginName + "'");
143 Events_Error::send(string("Can not load plugin '") + myCurrentPluginName + "'");
146 Events_Error::send(string("Feature '") + theFeatureID + "' not found in any plugin");
149 return FeaturePtr(); // return nothing
152 std::shared_ptr<ModelAPI_Document> Model_Session::moduleDocument()
154 return std::shared_ptr<ModelAPI_Document>(
155 Model_Application::getApplication()->getDocument("root"));
158 std::shared_ptr<ModelAPI_Document> Model_Session::document(std::string theDocID)
160 return std::shared_ptr<ModelAPI_Document>(
161 Model_Application::getApplication()->getDocument(theDocID));
164 bool Model_Session::hasModuleDocument()
166 return Model_Application::getApplication()->hasDocument("root");
169 std::shared_ptr<ModelAPI_Document> Model_Session::activeDocument()
171 if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
172 myCurrentDoc = moduleDocument();
176 void Model_Session::setActiveDocument(
177 std::shared_ptr<ModelAPI_Document> theDoc, bool theSendSignal)
179 if (myCurrentDoc != theDoc) {
180 myCurrentDoc = theDoc;
182 static std::shared_ptr<Events_Message> aMsg(
183 new Events_Message(Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED)));
184 Events_Loop::loop()->send(aMsg);
189 std::list<std::shared_ptr<ModelAPI_Document> > Model_Session::allOpenedDocuments()
191 list<std::shared_ptr<ModelAPI_Document> > aResult;
192 aResult.push_back(moduleDocument());
193 // add subs recursively
194 list<std::shared_ptr<ModelAPI_Document> >::iterator aDoc = aResult.begin();
195 for(; aDoc != aResult.end(); aDoc++) {
196 DocumentPtr anAPIDoc = *aDoc;
197 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(anAPIDoc);
199 const std::set<std::string> aSubs = aDoc->subDocuments(true);
200 std::set<std::string>::const_iterator aSubIter = aSubs.cbegin();
201 for(; aSubIter != aSubs.cend(); aSubIter++) {
202 if (!Model_Application::getApplication()->isLoadByDemand(*aSubIter)) {
203 aResult.push_back(Model_Application::getApplication()->getDocument(*aSubIter));
211 std::shared_ptr<ModelAPI_Document> Model_Session::copy(
212 std::shared_ptr<ModelAPI_Document> theSource, std::string theID)
214 // create a new document
215 std::shared_ptr<Model_Document> aNew = std::dynamic_pointer_cast<Model_Document>(
216 Model_Application::getApplication()->getDocument(theID));
217 // make a copy of all labels
218 TDF_Label aSourceRoot = std::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main()
220 TDF_Label aTargetRoot = aNew->document()->Main().Father();
221 Handle(TDF_DataSet) aDS = new TDF_DataSet;
222 aDS->AddLabel(aSourceRoot);
223 TDF_ClosureTool::Closure(aDS);
224 Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
225 aRT->SetRelocation(aSourceRoot, aTargetRoot);
226 TDF_CopyTool::Copy(aDS, aRT);
228 aNew->synchronizeFeatures(false, true);
232 Model_Session::Model_Session()
234 myPluginsInfoLoaded = false;
235 myCheckTransactions = true;
236 ModelAPI_Session::setSession(std::shared_ptr<ModelAPI_Session>(this));
237 // register the configuration reading listener
238 Events_Loop* aLoop = Events_Loop::loop();
239 static const Events_ID kFeatureEvent =
240 Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
241 aLoop->registerListener(this, kFeatureEvent);
242 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED), 0, true);
243 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED), 0, true);
244 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED), 0, true);
245 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_VALIDATOR_LOADED));
248 void Model_Session::processEvent(const std::shared_ptr<Events_Message>& theMessage)
250 static const Events_ID kFeatureEvent =
251 Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
252 static const Events_ID kValidatorEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
253 if (theMessage->eventID() == kFeatureEvent) {
254 const std::shared_ptr<Config_FeatureMessage> aMsg =
255 std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
257 // proccess the plugin info, load plugin
258 if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
259 myPlugins[aMsg->id()] = std::pair<std::string, std::string>(
260 aMsg->pluginLibrary(), aMsg->documentKind());
263 const std::shared_ptr<Config_AttributeMessage> aMsgAttr =
264 std::dynamic_pointer_cast<Config_AttributeMessage>(theMessage);
266 if (!aMsgAttr->isObligatory()) {
267 validators()->registerNotObligatory(aMsgAttr->featureId(), aMsgAttr->attributeId());
269 if(aMsgAttr->isConcealment()) {
270 validators()->registerConcealment(aMsgAttr->featureId(), aMsgAttr->attributeId());
275 // plugins information was started to load, so, it will be loaded
276 myPluginsInfoLoaded = true;
277 } else if (theMessage->eventID() == kValidatorEvent) {
278 std::shared_ptr<Config_ValidatorMessage> aMsg =
279 std::dynamic_pointer_cast<Config_ValidatorMessage>(theMessage);
281 if (aMsg->attributeId().empty()) { // feature validator
282 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->parameters());
283 } else { // attribute validator
284 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->attributeId(),
288 } else { // create/update/delete
289 if (myCheckTransactions && !isOperation())
290 Events_Error::send("Modification of data structure outside of the transaction");
291 // if part is deleted, make the root as the current document (on undo of Parts creations)
292 static const Events_ID kDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
293 if (theMessage->eventID() == kDeletedEvent) {
294 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aDeleted =
295 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
297 aDeleted->groups().find(ModelAPI_ResultPart::group()) != aDeleted->groups().end())
299 setActiveDocument(moduleDocument());
305 void Model_Session::LoadPluginsInfo()
307 if (myPluginsInfoLoaded) // nothing to do
310 // Read plugins information from XML files
311 Config_ModuleReader aXMLReader(Config_FeatureMessage::MODEL_EVENT());
312 aXMLReader.readAll();
315 void Model_Session::registerPlugin(ModelAPI_Plugin* thePlugin)
317 myPluginObjs[myCurrentPluginName] = thePlugin;
318 static Events_ID EVENT_LOAD = Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED);
319 ModelAPI_EventCreator::get()->sendUpdated(ObjectPtr(), EVENT_LOAD);
320 Events_Loop::loop()->flush(EVENT_LOAD);
321 // If the plugin has an ability to process GUI events, register it
322 Events_Listener* aListener = dynamic_cast<Events_Listener*>(thePlugin);
324 Events_Loop* aLoop = Events_Loop::loop();
325 static Events_ID aStateRequestEventId =
326 Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_REQUEST);
327 aLoop->registerListener(aListener, aStateRequestEventId);
331 ModelAPI_ValidatorsFactory* Model_Session::validators()
333 static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;