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_Objects.h>
13 #include <Model_Application.h>
14 #include <Model_Events.h>
15 #include <Model_Validator.h>
16 #include <ModelAPI_Events.h>
17 #include <Events_Loop.h>
18 #include <Events_InfoMessage.h>
19 #include <Config_FeatureMessage.h>
20 #include <Config_AttributeMessage.h>
21 #include <Config_ValidatorMessage.h>
22 #include <Config_ModuleReader.h>
23 #include <Config_ValidatorReader.h>
25 #include <ModelAPI_CompositeFeature.h>
26 #include <ModelAPI_ResultPart.h>
27 #include <ModelAPI_Tools.h>
29 #include <TDF_ChildIDIterator.hxx>
30 #include <TDF_CopyTool.hxx>
31 #include <TDF_DataSet.hxx>
32 #include <TDF_RelocationTable.hxx>
33 #include <TDF_ClosureTool.hxx>
35 #include <TNaming_Builder.hxx>
36 #include <TNaming_Iterator.hxx>
37 #include <TNaming_NamedShape.hxx>
39 #include <TopoDS_Shape.hxx>
41 static Model_Session* myImpl = new Model_Session();
43 // t oredirect all calls to the root document
44 #define ROOT_DOC std::dynamic_pointer_cast<Model_Document>(moduleDocument())
46 bool Model_Session::load(const char* theFileName)
48 bool aRes = ROOT_DOC->load(theFileName, "root", ROOT_DOC);
52 bool Model_Session::save(const char* theFileName, std::list<std::string>& theResults)
54 return ROOT_DOC->save(theFileName, "root", theResults);
57 void Model_Session::closeAll()
59 Model_Application::getApplication()->deleteAllDocuments();
62 void Model_Session::startOperation(const std::string& theId, const bool theAttachedToNested)
64 myOperationAttachedToNext = theAttachedToNested;
65 ROOT_DOC->startOperation();
66 ROOT_DOC->operationId(theId);
67 static std::shared_ptr<Events_Message> aStartedMsg
68 (new Events_Message(Events_Loop::eventByName("StartOperation")));
69 Events_Loop::loop()->send(aStartedMsg);
70 // remove all useless documents that has been closed: on start of operation undo/redo is cleared
71 // MPV: this code is dangerous now because it may close the document that is activated right now
72 // but not in the list of the opened documents yet (create, delete, undo, activate Part)
73 // later this must be updated by correct usage of uniques IDs of documents, not names of results
74 //std::list<std::shared_ptr<ModelAPI_Document> > aUsedDocs = allOpenedDocuments();
75 //Model_Application::getApplication()->removeUselessDocuments(aUsedDocs);
78 void Model_Session::finishOperation()
80 setCheckTransactions(false);
81 ROOT_DOC->finishOperation();
82 if (myOperationAttachedToNext) { // twice, with nested
83 ROOT_DOC->finishOperation();
84 myOperationAttachedToNext = false;
86 setCheckTransactions(true);
89 void Model_Session::abortOperation()
91 setCheckTransactions(false);
92 ROOT_DOC->abortOperation();
93 if (myOperationAttachedToNext) { // twice, with nested
94 ROOT_DOC->abortOperation();
95 myOperationAttachedToNext = false;
97 setCheckTransactions(true);
98 // here the update mechanism may work after abort, so, supress the warnings about
99 // modifications outside of the transactions
100 bool aWasCheck = myCheckTransactions;
101 myCheckTransactions = false;
102 static std::shared_ptr<Events_Message> anAbortMsg
103 (new Events_Message(Events_Loop::eventByName("AbortOperation")));
104 Events_Loop::loop()->send(anAbortMsg);
105 myCheckTransactions = true;
106 myCheckTransactions = aWasCheck;
109 bool Model_Session::isOperation()
111 return ROOT_DOC->isOperation();
114 bool Model_Session::isModified()
116 return ROOT_DOC->isModified();
119 bool Model_Session::canUndo()
121 return ROOT_DOC->canUndo();
124 void Model_Session::undo()
126 setCheckTransactions(false);
128 setCheckTransactions(true);
131 bool Model_Session::canRedo()
133 return ROOT_DOC->canRedo();
136 void Model_Session::redo()
138 setCheckTransactions(false);
140 setCheckTransactions(true);
143 //! Returns stack of performed operations
144 std::list<std::string> Model_Session::undoList()
146 return ROOT_DOC->undoList();
148 //! Returns stack of rolled back operations
149 std::list<std::string> Model_Session::redoList()
151 return ROOT_DOC->redoList();
154 FeaturePtr Model_Session::createFeature(std::string theFeatureID, Model_Document* theDocOwner)
156 if (this != myImpl) {
157 return myImpl->createFeature(theFeatureID, theDocOwner);
160 // load all information about plugins, features and attributes
163 if (myPlugins.find(theFeatureID) != myPlugins.end()) {
164 std::pair<std::string, std::string>& aPlugin = myPlugins[theFeatureID]; // plugin and doc kind
165 if (!aPlugin.second.empty() && aPlugin.second != theDocOwner->kind()) {
166 Events_InfoMessage("Model_Session",
167 "Feature '%1' can be created only in document '%2' by the XML definition")
168 .arg(theFeatureID).arg(aPlugin.second).send();
171 myCurrentPluginName = aPlugin.first;
172 if (myPluginObjs.find(myCurrentPluginName) == myPluginObjs.end()) {
173 // load plugin library if not yet done
174 Config_ModuleReader::loadPlugin(myCurrentPluginName);
176 if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
177 FeaturePtr aCreated = myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
179 Events_InfoMessage("Model_Session",
180 "Can not initialize feature '%1' in plugin '%2'")
181 .arg(theFeatureID).arg(myCurrentPluginName).send();
185 Events_InfoMessage("Model_Session",
186 "Can not load plugin '%1'").arg(myCurrentPluginName).send();
189 Events_InfoMessage("Model_Session",
190 "Feature '%1' not found in any plugin").arg(theFeatureID).send();
193 return FeaturePtr(); // return nothing
196 std::shared_ptr<ModelAPI_Document> Model_Session::moduleDocument()
198 Handle(Model_Application) anApp = Model_Application::getApplication();
199 bool aFirstCall = !anApp->hasRoot();
201 // to be sure that plugins are loaded,
202 // even before the first "createFeature" call (in unit tests)
205 // creation of the root document is always outside of the transaction, so, avoid checking it
206 setCheckTransactions(false);
207 anApp->createDocument(0); // 0 is a root ID
208 // creation of the root document is always outside of the transaction, so, avoid checking it
209 setCheckTransactions(true);
211 return anApp->rootDocument();
214 std::shared_ptr<ModelAPI_Document> Model_Session::document(int theDocID)
216 return std::shared_ptr<ModelAPI_Document>(
217 Model_Application::getApplication()->document(theDocID));
220 bool Model_Session::hasModuleDocument()
222 return Model_Application::getApplication()->hasRoot();
225 std::shared_ptr<ModelAPI_Document> Model_Session::activeDocument()
227 if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
228 myCurrentDoc = moduleDocument();
232 /// makes the last feature in the document as the current
233 static void makeCurrentLast(std::shared_ptr<ModelAPI_Document> theDoc) {
235 FeaturePtr aLast = std::dynamic_pointer_cast<Model_Document>(theDoc)->lastFeature();
236 // if last is nested into something else, make this something else as last:
237 // otherwise it will look like edition of sub-element, so, the main will be disabled
239 CompositeFeaturePtr aMain = ModelAPI_Tools::compositeOwner(aLast);
242 aMain = ModelAPI_Tools::compositeOwner(aLast);
245 theDoc->setCurrentFeature(aLast, false);
249 void Model_Session::setActiveDocument(
250 std::shared_ptr<ModelAPI_Document> theDoc, bool theSendSignal)
252 if (myCurrentDoc != theDoc) {
253 if (myCurrentDoc.get())
254 myCurrentDoc->setActive(false);
256 theDoc->setActive(true);
258 std::shared_ptr<ModelAPI_Document> aPrevious = myCurrentDoc;
259 myCurrentDoc = theDoc;
260 if (theDoc.get() && theSendSignal) {
261 // this must be before the synchronisation call because features in PartSet lower than this
262 // part feature must be disabled and don't recomputed anymore (issue 1156,
263 // translation feature is failed on activation of Part 2)
264 if (isOperation()) { // do it only in transaction, not on opening of document
265 DocumentPtr aRoot = moduleDocument();
266 if (myCurrentDoc != aRoot) {
267 FeaturePtr aPartFeat = ModelAPI_Tools::findPartFeature(aRoot, myCurrentDoc);
268 if (aPartFeat.get()) {
269 aRoot->setCurrentFeature(aPartFeat, false);
273 // syncronize the document: it may be just opened or opened but removed before
274 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(theDoc);
276 bool aWasChecked = myCheckTransactions;
277 setCheckTransactions(false);
278 TDF_LabelList anEmptyUpdated;
279 aDoc->objects()->synchronizeFeatures(anEmptyUpdated, true, true, false, true);
281 setCheckTransactions(true);
283 static std::shared_ptr<Events_Message> aMsg(
284 new Events_Message(Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED)));
285 Events_Loop::loop()->send(aMsg);
287 // make the current state correct and synchronised in the module and sub-documents
288 if (isOperation()) { // do it only in transaction, not on opening of document
289 if (myCurrentDoc == moduleDocument()) {
290 // make the current feature the latest in root, in previous root current become also last
291 makeCurrentLast(aPrevious);
292 makeCurrentLast(myCurrentDoc);
294 // make the current feature the latest in sub, root current feature becomes this sub
295 makeCurrentLast(myCurrentDoc);
301 std::list<std::shared_ptr<ModelAPI_Document> > Model_Session::allOpenedDocuments()
303 std::list<std::shared_ptr<ModelAPI_Document> > aResult;
304 aResult.push_back(moduleDocument());
305 // add subs recursively
306 std::list<std::shared_ptr<ModelAPI_Document> >::iterator aDoc = aResult.begin();
307 for(; aDoc != aResult.end(); aDoc++) {
308 DocumentPtr anAPIDoc = *aDoc;
309 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(anAPIDoc);
311 const std::set<int> aSubs = aDoc->subDocuments();
312 std::set<int>::const_iterator aSubIter = aSubs.cbegin();
313 for(; aSubIter != aSubs.cend(); aSubIter++) {
314 aResult.push_back(Model_Application::getApplication()->document(*aSubIter));
321 bool Model_Session::isLoadByDemand(const std::string theDocID)
323 return Model_Application::getApplication()->isLoadByDemand(theDocID);
326 std::shared_ptr<ModelAPI_Document> Model_Session::copy(
327 std::shared_ptr<ModelAPI_Document> theSource, const int theDestID)
329 std::shared_ptr<Model_Document> aNew = Model_Application::getApplication()->document(theDestID);
330 // make a copy of all labels
331 TDF_Label aSourceRoot = std::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main()
333 TDF_Label aTargetRoot = aNew->document()->Main().Father();
334 Handle(TDF_DataSet) aDS = new TDF_DataSet;
335 aDS->AddLabel(aSourceRoot);
336 TDF_ClosureTool::Closure(aDS);
337 Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable(Standard_True);
338 aRT->SetRelocation(aSourceRoot, aTargetRoot);
339 TDF_CopyTool::Copy(aDS, aRT);
341 // TODO: remove after fix in OCCT.
342 // All named shapes are stored in reversed order, so to fix this we reverse them back.
343 for(TDF_ChildIDIterator aChildIter(aTargetRoot, TNaming_NamedShape::GetID(), true);
346 Handle(TNaming_NamedShape) aNamedShape =
347 Handle(TNaming_NamedShape)::DownCast(aChildIter.Value());
348 if (aNamedShape.IsNull()) {
352 TopoDS_Shape aShape = aNamedShape->Get();
353 if(aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND) {
357 TNaming_Evolution anEvol = aNamedShape->Evolution();
358 std::list<std::pair<TopoDS_Shape, TopoDS_Shape> > aShapePairs; // to store old and new shapes
359 for(TNaming_Iterator anIter(aNamedShape); anIter.More(); anIter.Next()) {
360 aShapePairs.push_back(
361 std::pair<TopoDS_Shape, TopoDS_Shape>(anIter.OldShape(), anIter.NewShape()));
364 // Add in reverse order.
365 TDF_Label aLabel = aNamedShape->Label();
366 TNaming_Builder aBuilder(aLabel);
367 for(std::list<std::pair<TopoDS_Shape, TopoDS_Shape> >::iterator aPairsIter =
369 aPairsIter != aShapePairs.end();
371 if (anEvol == TNaming_GENERATED) {
372 aBuilder.Generated(aPairsIter->first, aPairsIter->second);
373 } else if (anEvol == TNaming_MODIFY) {
374 aBuilder.Modify(aPairsIter->first, aPairsIter->second);
375 } else if (anEvol == TNaming_DELETE) {
376 aBuilder.Delete(aPairsIter->first);
377 } else if (anEvol == TNaming_PRIMITIVE) {
378 aBuilder.Generated(aPairsIter->second);
379 } else if (anEvol == TNaming_SELECTED) {
380 aBuilder.Select(aPairsIter->second, aPairsIter->first);
385 TDF_LabelList anEmptyUpdated;
386 aNew->objects()->synchronizeFeatures(anEmptyUpdated, true, true, true, true);
390 Model_Session::Model_Session()
392 myPluginsInfoLoaded = false;
393 myCheckTransactions = true;
394 myOperationAttachedToNext = false;
395 ModelAPI_Session::setSession(std::shared_ptr<ModelAPI_Session>(this));
396 // register the configuration reading listener
397 Events_Loop* aLoop = Events_Loop::loop();
398 static const Events_ID kFeatureEvent =
399 Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
400 aLoop->registerListener(this, kFeatureEvent);
401 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED), 0, true);
402 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED), 0, true);
403 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED), 0, true);
404 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_VALIDATOR_LOADED));
407 void Model_Session::processEvent(const std::shared_ptr<Events_Message>& theMessage)
409 static const Events_ID kFeatureEvent =
410 Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
411 static const Events_ID kValidatorEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
412 if (theMessage->eventID() == kFeatureEvent) {
413 const std::shared_ptr<Config_FeatureMessage> aMsg =
414 std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
417 // process the plugin info, load plugin
418 if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
419 myPlugins[aMsg->id()] = std::pair<std::string, std::string>(
420 aMsg->pluginLibrary(), aMsg->documentKind());
423 const std::shared_ptr<Config_AttributeMessage> aMsgAttr =
424 std::dynamic_pointer_cast<Config_AttributeMessage>(theMessage);
427 if (!aMsgAttr->isObligatory()) {
428 validators()->registerNotObligatory(aMsgAttr->featureId(), aMsgAttr->attributeId());
430 if(aMsgAttr->isConcealment()) {
431 validators()->registerConcealment(aMsgAttr->featureId(), aMsgAttr->attributeId());
433 const std::list<std::pair<std::string, std::string> >& aCases = aMsgAttr->getCases();
434 if (!aCases.empty()) {
435 validators()->registerCase(aMsgAttr->featureId(), aMsgAttr->attributeId(), aCases);
439 // plugins information was started to load, so, it will be loaded
440 myPluginsInfoLoaded = true;
441 } else if (theMessage->eventID() == kValidatorEvent) {
442 std::shared_ptr<Config_ValidatorMessage> aMsg =
443 std::dynamic_pointer_cast<Config_ValidatorMessage>(theMessage);
445 if (aMsg->attributeId().empty()) { // feature validator
446 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->parameters());
447 } else { // attribute validator
448 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->attributeId(),
452 } else { // create/update/delete
453 if (myCheckTransactions && !isOperation())
454 Events_InfoMessage("Model_Session",
455 "Modification of data structure outside of the transaction").send();
456 // if part is deleted, make the root as the current document (on undo of Parts creations)
457 static const Events_ID kDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
458 if (theMessage->eventID() == kDeletedEvent) {
459 std::shared_ptr<ModelAPI_ObjectDeletedMessage> aDeleted =
460 std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
462 aDeleted->groups().find(ModelAPI_ResultPart::group()) != aDeleted->groups().end())
464 // check that the current feature of the session is still the active Part (even disabled)
466 FeaturePtr aCurrentPart = moduleDocument()->currentFeature(true);
467 if (aCurrentPart.get()) {
468 const std::list<std::shared_ptr<ModelAPI_Result> >& aResList = aCurrentPart->results();
469 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResList.begin();
470 for(; !aFound && aRes != aResList.end(); aRes++) {
471 ResultPartPtr aPRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes);
472 if (aPRes.get() && aPRes->isActivated() && aPRes->partDoc() == activeDocument()) {
478 if (!aFound) { // if not, the part was removed, so activate the module document
479 if (myCurrentDoc.get())
480 setActiveDocument(moduleDocument());
487 void Model_Session::LoadPluginsInfo()
489 if (myPluginsInfoLoaded) // nothing to do
491 // Read plugins information from XML files
492 Config_ModuleReader aModuleReader(Config_FeatureMessage::MODEL_EVENT());
493 aModuleReader.readAll();
494 std::set<std::string> aFiles = aModuleReader.modulePluginFiles();
495 std::set<std::string>::iterator it = aFiles.begin();
496 for ( ; it != aFiles.end(); it++ ) {
497 Config_ValidatorReader aValidatorReader (*it);
498 aValidatorReader.readAll();
503 void Model_Session::registerPlugin(ModelAPI_Plugin* thePlugin)
505 myPluginObjs[myCurrentPluginName] = thePlugin;
506 static Events_ID EVENT_LOAD = Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED);
507 ModelAPI_EventCreator::get()->sendUpdated(ObjectPtr(), EVENT_LOAD, false);
508 // If the plugin has an ability to process GUI events, register it
509 Events_Listener* aListener = dynamic_cast<Events_Listener*>(thePlugin);
511 Events_Loop* aLoop = Events_Loop::loop();
512 static Events_ID aStateRequestEventId =
513 Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_REQUEST);
514 aLoop->registerListener(aListener, aStateRequestEventId);
518 ModelAPI_ValidatorsFactory* Model_Session::validators()
520 static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;
524 int Model_Session::transactionID()
526 return ROOT_DOC->transactionID();