1 // File: Model_Session.cxx
2 // Created: 20 Mar 2014
3 // Author: Mikhail PONIKAROV
5 #include <Model_Session.h>
6 #include <ModelAPI_Feature.h>
7 #include <ModelAPI_Plugin.h>
8 #include <Model_Data.h>
9 #include <Model_Document.h>
10 #include <Model_Application.h>
11 #include <Model_Events.h>
12 #include <Model_Validator.h>
13 #include <Events_Loop.h>
14 #include <Events_Error.h>
15 #include <Config_FeatureMessage.h>
16 #include <Config_ValidatorMessage.h>
17 #include <Config_ModuleReader.h>
19 #include <TDF_CopyTool.hxx>
20 #include <TDF_DataSet.hxx>
21 #include <TDF_RelocationTable.hxx>
22 #include <TDF_ClosureTool.hxx>
26 static Model_Session* myImpl = new Model_Session();
28 // t oredirect all calls to the root document
29 #define ROOT_DOC boost::dynamic_pointer_cast<Model_Document>(moduleDocument())
31 bool Model_Session::load(const char* theFileName)
33 return ROOT_DOC->load(theFileName);
36 bool Model_Session::save(const char* theFileName, std::list<std::string>& theResults)
38 return ROOT_DOC->save(theFileName, theResults);
41 void Model_Session::startOperation()
43 ROOT_DOC->startOperation();
46 void Model_Session::finishOperation()
48 ROOT_DOC->finishOperation();
51 void Model_Session::abortOperation()
53 ROOT_DOC->abortOperation();
56 bool Model_Session::isOperation()
58 return ROOT_DOC->isOperation();
61 bool Model_Session::isModified()
63 return ROOT_DOC->isModified();
66 bool Model_Session::canUndo()
68 return ROOT_DOC->canUndo();
71 void Model_Session::undo()
76 bool Model_Session::canRedo()
78 return ROOT_DOC->canRedo();
81 void Model_Session::redo()
86 FeaturePtr Model_Session::createFeature(string theFeatureID)
89 return myImpl->createFeature(theFeatureID);
92 if (myPlugins.find(theFeatureID) != myPlugins.end()) {
93 myCurrentPluginName = myPlugins[theFeatureID];
94 if (myPluginObjs.find(myCurrentPluginName) == myPluginObjs.end()) {
95 // load plugin library if not yet done
96 Config_ModuleReader::loadLibrary(myCurrentPluginName);
98 if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
99 FeaturePtr aCreated = myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
102 string("Can not initialize feature '") + theFeatureID + "' in plugin '"
103 + myCurrentPluginName + "'");
107 Events_Error::send(string("Can not load plugin '") + myCurrentPluginName + "'");
110 Events_Error::send(string("Feature '") + theFeatureID + "' not found in any plugin");
113 return FeaturePtr(); // return nothing
116 boost::shared_ptr<ModelAPI_Document> Model_Session::moduleDocument()
118 return boost::shared_ptr<ModelAPI_Document>(
119 Model_Application::getApplication()->getDocument("root"));
122 bool Model_Session::hasModuleDocument()
124 return Model_Application::getApplication()->hasDocument("root");
127 boost::shared_ptr<ModelAPI_Document> Model_Session::activeDocument()
129 if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
130 myCurrentDoc = moduleDocument();
134 void Model_Session::setActiveDocument(boost::shared_ptr<ModelAPI_Document> theDoc)
136 myCurrentDoc = theDoc;
137 static Events_Message aMsg(Events_Loop::eventByName("CurrentDocumentChanged"));
138 Events_Loop::loop()->send(aMsg);
141 boost::shared_ptr<ModelAPI_Document> Model_Session::copy(
142 boost::shared_ptr<ModelAPI_Document> theSource, std::string theID)
144 // create a new document
145 boost::shared_ptr<Model_Document> aNew = boost::dynamic_pointer_cast<Model_Document>(
146 Model_Application::getApplication()->getDocument(theID));
147 // make a copy of all labels
148 TDF_Label aSourceRoot = boost::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main()
150 TDF_Label aTargetRoot = aNew->document()->Main().Father();
151 Handle(TDF_DataSet) aDS = new TDF_DataSet;
152 aDS->AddLabel(aSourceRoot);
153 TDF_ClosureTool::Closure(aDS);
154 Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
155 aRT->SetRelocation(aSourceRoot, aTargetRoot);
156 TDF_CopyTool::Copy(aDS, aRT);
158 aNew->synchronizeFeatures();
162 Model_Session::Model_Session()
164 myPluginsInfoLoaded = false;
165 myCheckTransactions = true;
166 ModelAPI_Session::setSession(boost::shared_ptr<ModelAPI_Session>(this));
167 // register the configuration reading listener
168 Events_Loop* aLoop = Events_Loop::loop();
169 static const Events_ID kFeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
170 aLoop->registerListener(this, kFeatureEvent);
171 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
172 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
173 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
174 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_VALIDATOR_LOADED));
177 void Model_Session::processEvent(const Events_Message* theMessage)
179 static const Events_ID kFeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
180 static const Events_ID kValidatorEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
181 if (theMessage->eventID() == kFeatureEvent) {
182 const Config_FeatureMessage* aMsg = dynamic_cast<const Config_FeatureMessage*>(theMessage);
184 // proccess the plugin info, load plugin
185 if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
186 myPlugins[aMsg->id()] = aMsg->pluginLibrary();
189 // plugins information was started to load, so, it will be loaded
190 myPluginsInfoLoaded = true;
191 } else if (theMessage->eventID() == kValidatorEvent) {
192 const Config_ValidatorMessage* aMsg = dynamic_cast<const Config_ValidatorMessage*>(theMessage);
194 if (aMsg->attributeId().empty()) { // feature validator
195 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->parameters());
196 } else { // attribute validator
197 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->attributeId(),
201 } else { // create/update/delete
202 if (myCheckTransactions && !isOperation())
203 Events_Error::send("Modification of data structure outside of the transaction");
207 void Model_Session::LoadPluginsInfo()
209 if (myPluginsInfoLoaded) // nothing to do
212 // Read plugins information from XML files
213 Config_ModuleReader aXMLReader("FeatureRegisterEvent");
214 aXMLReader.readAll();
217 void Model_Session::registerPlugin(ModelAPI_Plugin* thePlugin)
219 myPluginObjs[myCurrentPluginName] = thePlugin;
220 static Events_ID EVENT_LOAD = Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED);
221 ModelAPI_EventCreator::get()->sendUpdated(ObjectPtr(), EVENT_LOAD);
222 Events_Loop::loop()->flush(EVENT_LOAD);
225 ModelAPI_ValidatorsFactory* Model_Session::validators()
227 static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;