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 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 //! Returns stack of performed operations
122 std::list<std::string> Model_Session::undoList()
124 std::list<std::string> temp;
125 temp.push_front("Part");
126 temp.push_front("Sketch");
127 temp.push_front("Extrusion");
130 //! Returns stack of rolled back operations
131 std::list<std::string> Model_Session::redoList()
133 std::list<std::string> temp;
137 FeaturePtr Model_Session::createFeature(string theFeatureID)
139 if (this != myImpl) {
140 return myImpl->createFeature(theFeatureID);
143 // load all information about plugins, features and attributes
146 if (myPlugins.find(theFeatureID) != myPlugins.end()) {
147 std::pair<std::string, std::string>& aPlugin = myPlugins[theFeatureID]; // plugin and doc kind
148 if (!aPlugin.second.empty() && aPlugin.second != activeDocument()->kind()) {
150 string("Feature '") + theFeatureID + "' can be created only in document '"
151 + aPlugin.second + "' by the XML definition");
154 myCurrentPluginName = aPlugin.first;
155 if (myPluginObjs.find(myCurrentPluginName) == myPluginObjs.end()) {
156 // load plugin library if not yet done
157 Config_ModuleReader::loadPlugin(myCurrentPluginName);
159 if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
160 FeaturePtr aCreated = myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
163 string("Can not initialize feature '") + theFeatureID + "' in plugin '"
164 + myCurrentPluginName + "'");
168 Events_Error::send(string("Can not load plugin '") + myCurrentPluginName + "'");
171 Events_Error::send(string("Feature '") + theFeatureID + "' not found in any plugin");
174 return FeaturePtr(); // return nothing
177 std::shared_ptr<ModelAPI_Document> Model_Session::moduleDocument()
179 return std::shared_ptr<ModelAPI_Document>(
180 Model_Application::getApplication()->getDocument("root"));
183 std::shared_ptr<ModelAPI_Document> Model_Session::document(std::string theDocID)
185 return std::shared_ptr<ModelAPI_Document>(
186 Model_Application::getApplication()->getDocument(theDocID));
189 bool Model_Session::hasModuleDocument()
191 return Model_Application::getApplication()->hasDocument("root");
194 std::shared_ptr<ModelAPI_Document> Model_Session::activeDocument()
196 if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
197 myCurrentDoc = moduleDocument();
201 void Model_Session::setActiveDocument(
202 std::shared_ptr<ModelAPI_Document> theDoc, bool theSendSignal)
204 if (myCurrentDoc != theDoc) {
205 myCurrentDoc = theDoc;
206 if (theDoc.get() && theSendSignal) {
207 // syncronize the document: it may be just opened or opened but removed before
208 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(theDoc);
210 bool aWasChecked = myCheckTransactions;
211 setCheckTransactions(false);
212 aDoc->synchronizeFeatures(false, true);
214 setCheckTransactions(true);
216 static std::shared_ptr<Events_Message> aMsg(
217 new Events_Message(Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED)));
218 Events_Loop::loop()->send(aMsg);
223 std::list<std::shared_ptr<ModelAPI_Document> > Model_Session::allOpenedDocuments()
225 list<std::shared_ptr<ModelAPI_Document> > aResult;
226 aResult.push_back(moduleDocument());
227 // add subs recursively
228 list<std::shared_ptr<ModelAPI_Document> >::iterator aDoc = aResult.begin();
229 for(; aDoc != aResult.end(); aDoc++) {
230 DocumentPtr anAPIDoc = *aDoc;
231 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(anAPIDoc);
233 const std::set<std::string> aSubs = aDoc->subDocuments(true);
234 std::set<std::string>::const_iterator aSubIter = aSubs.cbegin();
235 for(; aSubIter != aSubs.cend(); aSubIter++) {
236 if (!Model_Application::getApplication()->isLoadByDemand(*aSubIter)) {
237 aResult.push_back(Model_Application::getApplication()->getDocument(*aSubIter));
245 std::shared_ptr<ModelAPI_Document> Model_Session::copy(
246 std::shared_ptr<ModelAPI_Document> theSource, std::string theID)
248 // create a new document
249 std::shared_ptr<Model_Document> aNew = std::dynamic_pointer_cast<Model_Document>(
250 Model_Application::getApplication()->getDocument(theID));
251 // make a copy of all labels
252 TDF_Label aSourceRoot = std::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main()
254 TDF_Label aTargetRoot = aNew->document()->Main().Father();
255 Handle(TDF_DataSet) aDS = new TDF_DataSet;
256 aDS->AddLabel(aSourceRoot);
257 TDF_ClosureTool::Closure(aDS);
258 Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
259 aRT->SetRelocation(aSourceRoot, aTargetRoot);
260 TDF_CopyTool::Copy(aDS, aRT);
262 aNew->synchronizeFeatures(false, true);
266 Model_Session::Model_Session()
268 myPluginsInfoLoaded = false;
269 myCheckTransactions = true;
270 ModelAPI_Session::setSession(std::shared_ptr<ModelAPI_Session>(this));
271 // register the configuration reading listener
272 Events_Loop* aLoop = Events_Loop::loop();
273 static const Events_ID kFeatureEvent =
274 Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
275 aLoop->registerListener(this, kFeatureEvent);
276 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED), 0, true);
277 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED), 0, true);
278 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED), 0, true);
279 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_VALIDATOR_LOADED));
282 void Model_Session::processEvent(const std::shared_ptr<Events_Message>& theMessage)
284 static const Events_ID kFeatureEvent =
285 Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
286 static const Events_ID kValidatorEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
287 if (theMessage->eventID() == kFeatureEvent) {
288 const std::shared_ptr<Config_FeatureMessage> aMsg =
289 std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
291 // proccess the plugin info, load plugin
292 if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
293 myPlugins[aMsg->id()] = std::pair<std::string, std::string>(
294 aMsg->pluginLibrary(), aMsg->documentKind());
297 const std::shared_ptr<Config_AttributeMessage> aMsgAttr =
298 std::dynamic_pointer_cast<Config_AttributeMessage>(theMessage);
300 if (!aMsgAttr->isObligatory()) {
301 validators()->registerNotObligatory(aMsgAttr->featureId(), aMsgAttr->attributeId());
303 if(aMsgAttr->isConcealment()) {
304 validators()->registerConcealment(aMsgAttr->featureId(), aMsgAttr->attributeId());
309 // plugins information was started to load, so, it will be loaded
310 myPluginsInfoLoaded = true;
311 } else if (theMessage->eventID() == kValidatorEvent) {
312 std::shared_ptr<Config_ValidatorMessage> aMsg =
313 std::dynamic_pointer_cast<Config_ValidatorMessage>(theMessage);
315 if (aMsg->attributeId().empty()) { // feature validator
316 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->parameters());
317 } else { // attribute validator
318 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->attributeId(),
322 } else { // create/update/delete
323 if (myCheckTransactions && !isOperation())
324 Events_Error::send("Modification of data structure outside of the transaction");
325 // if part is deleted, make the root as the current document (on undo of Parts creations)
326 static const Events_ID kDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
327 if (theMessage->eventID() == kDeletedEvent) {
328 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aDeleted =
329 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
331 aDeleted->groups().find(ModelAPI_ResultPart::group()) != aDeleted->groups().end())
333 setActiveDocument(moduleDocument());
339 void Model_Session::LoadPluginsInfo()
341 if (myPluginsInfoLoaded) // nothing to do
344 // Read plugins information from XML files
345 Config_ModuleReader aXMLReader(Config_FeatureMessage::MODEL_EVENT());
346 aXMLReader.readAll();
349 void Model_Session::registerPlugin(ModelAPI_Plugin* thePlugin)
351 myPluginObjs[myCurrentPluginName] = thePlugin;
352 static Events_ID EVENT_LOAD = Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED);
353 ModelAPI_EventCreator::get()->sendUpdated(ObjectPtr(), EVENT_LOAD);
354 Events_Loop::loop()->flush(EVENT_LOAD);
355 // If the plugin has an ability to process GUI events, register it
356 Events_Listener* aListener = dynamic_cast<Events_Listener*>(thePlugin);
358 Events_Loop* aLoop = Events_Loop::loop();
359 static Events_ID aStateRequestEventId =
360 Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_REQUEST);
361 aLoop->registerListener(aListener, aStateRequestEventId);
365 ModelAPI_ValidatorsFactory* Model_Session::validators()
367 static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;