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>
29 static Model_Session* myImpl = new Model_Session();
31 // t oredirect all calls to the root document
32 #define ROOT_DOC boost::dynamic_pointer_cast<Model_Document>(moduleDocument())
34 bool Model_Session::load(const char* theFileName)
36 return ROOT_DOC->load(theFileName);
39 bool Model_Session::save(const char* theFileName, std::list<std::string>& theResults)
41 return ROOT_DOC->save(theFileName, theResults);
44 void Model_Session::startOperation()
46 ROOT_DOC->startOperation();
49 void Model_Session::finishOperation()
51 ROOT_DOC->finishOperation();
52 static boost::shared_ptr<Events_Message> aFinishMsg
53 (new Events_Message(Events_Loop::eventByName("FinishOperation")));
54 Events_Loop::loop()->send(aFinishMsg);
57 void Model_Session::abortOperation()
59 ROOT_DOC->abortOperation();
60 static boost::shared_ptr<Events_Message> anAbortMsg
61 (new Events_Message(Events_Loop::eventByName("AbortOperation")));
62 Events_Loop::loop()->send(anAbortMsg);
65 bool Model_Session::isOperation()
67 return ROOT_DOC->isOperation();
70 bool Model_Session::isModified()
72 return ROOT_DOC->isModified();
75 bool Model_Session::canUndo()
77 return ROOT_DOC->canUndo();
80 void Model_Session::undo()
85 bool Model_Session::canRedo()
87 return ROOT_DOC->canRedo();
90 void Model_Session::redo()
95 FeaturePtr Model_Session::createFeature(string theFeatureID)
98 return myImpl->createFeature(theFeatureID);
102 if (myPlugins.find(theFeatureID) != myPlugins.end()) {
103 std::pair<std::string, std::string>& aPlugin = myPlugins[theFeatureID]; // plugin and doc kind
104 if (!aPlugin.second.empty() && aPlugin.second != activeDocument()->kind()) {
106 string("Feature '") + theFeatureID + "' can be created only in document '"
107 + aPlugin.second + "' by the XML definition");
110 myCurrentPluginName = aPlugin.first;
111 if (myPluginObjs.find(myCurrentPluginName) == myPluginObjs.end()) {
112 // load plugin library if not yet done
113 //TODO: Get info from Config about python libraries
114 if (myCurrentPluginName.compare(string("PythonFeaturesPlugin")) == 0) {
116 PyObject* module = PyImport_ImportModule(myCurrentPluginName.c_str());
117 assert(module != NULL);
119 Config_ModuleReader::loadLibrary(myCurrentPluginName);
122 if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
123 FeaturePtr aCreated = myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
126 string("Can not initialize feature '") + theFeatureID + "' in plugin '"
127 + myCurrentPluginName + "'");
131 Events_Error::send(string("Can not load plugin '") + myCurrentPluginName + "'");
134 Events_Error::send(string("Feature '") + theFeatureID + "' not found in any plugin");
137 return FeaturePtr(); // return nothing
140 boost::shared_ptr<ModelAPI_Document> Model_Session::moduleDocument()
142 return boost::shared_ptr<ModelAPI_Document>(
143 Model_Application::getApplication()->getDocument("root"));
146 bool Model_Session::hasModuleDocument()
148 return Model_Application::getApplication()->hasDocument("root");
151 boost::shared_ptr<ModelAPI_Document> Model_Session::activeDocument()
153 if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
154 myCurrentDoc = moduleDocument();
158 void Model_Session::setActiveDocument(boost::shared_ptr<ModelAPI_Document> theDoc)
160 if (myCurrentDoc != theDoc) {
161 myCurrentDoc = theDoc;
162 static boost::shared_ptr<Events_Message> aMsg(new Events_Message(Events_Loop::eventByName("CurrentDocumentChanged")));
163 Events_Loop::loop()->send(aMsg);
167 std::list<boost::shared_ptr<ModelAPI_Document> > Model_Session::allOpenedDocuments()
169 list<boost::shared_ptr<ModelAPI_Document> > aResult;
170 aResult.push_back(moduleDocument());
171 // add subs recursively
172 list<boost::shared_ptr<ModelAPI_Document> >::iterator aDoc = aResult.begin();
173 for(; aDoc != aResult.end(); aDoc++) {
174 DocumentPtr anAPIDoc = *aDoc;
175 boost::shared_ptr<Model_Document> aDoc = boost::dynamic_pointer_cast<Model_Document>(anAPIDoc);
177 std::set<std::string>::const_iterator aSubIter = aDoc->subDocuments().cbegin();
178 for(; aSubIter != aDoc->subDocuments().cend(); aSubIter++) {
179 if (!Model_Application::getApplication()->isLoadByDemand(*aSubIter)) {
180 aResult.push_back(Model_Application::getApplication()->getDocument(*aSubIter));
188 boost::shared_ptr<ModelAPI_Document> Model_Session::copy(
189 boost::shared_ptr<ModelAPI_Document> theSource, std::string theID)
191 // create a new document
192 boost::shared_ptr<Model_Document> aNew = boost::dynamic_pointer_cast<Model_Document>(
193 Model_Application::getApplication()->getDocument(theID));
194 // make a copy of all labels
195 TDF_Label aSourceRoot = boost::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main()
197 TDF_Label aTargetRoot = aNew->document()->Main().Father();
198 Handle(TDF_DataSet) aDS = new TDF_DataSet;
199 aDS->AddLabel(aSourceRoot);
200 TDF_ClosureTool::Closure(aDS);
201 Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
202 aRT->SetRelocation(aSourceRoot, aTargetRoot);
203 TDF_CopyTool::Copy(aDS, aRT);
205 aNew->synchronizeFeatures(false, true);
209 Model_Session::Model_Session()
211 myPluginsInfoLoaded = false;
212 myCheckTransactions = true;
213 ModelAPI_Session::setSession(boost::shared_ptr<ModelAPI_Session>(this));
214 // register the configuration reading listener
215 Events_Loop* aLoop = Events_Loop::loop();
216 static const Events_ID kFeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
217 aLoop->registerListener(this, kFeatureEvent);
218 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED), 0, true);
219 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED), 0, true);
220 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED), 0, true);
221 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_VALIDATOR_LOADED));
224 void Model_Session::processEvent(const boost::shared_ptr<Events_Message>& theMessage)
226 static const Events_ID kFeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
227 static const Events_ID kValidatorEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
228 if (theMessage->eventID() == kFeatureEvent) {
229 const boost::shared_ptr<Config_FeatureMessage> aMsg =
230 boost::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
232 // proccess the plugin info, load plugin
233 if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
234 myPlugins[aMsg->id()] = std::pair<std::string, std::string>(
235 aMsg->pluginLibrary(), aMsg->documentKind());
238 // plugins information was started to load, so, it will be loaded
239 myPluginsInfoLoaded = true;
240 } else if (theMessage->eventID() == kValidatorEvent) {
241 boost::shared_ptr<Config_ValidatorMessage> aMsg =
242 boost::dynamic_pointer_cast<Config_ValidatorMessage>(theMessage);
244 if (aMsg->attributeId().empty()) { // feature validator
245 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->parameters());
246 } else { // attribute validator
247 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->attributeId(),
251 } else { // create/update/delete
252 if (myCheckTransactions && !isOperation())
253 Events_Error::send("Modification of data structure outside of the transaction");
257 void Model_Session::LoadPluginsInfo()
259 if (myPluginsInfoLoaded) // nothing to do
262 // Read plugins information from XML files
263 Config_ModuleReader aXMLReader("FeatureRegisterEvent");
264 aXMLReader.readAll();
267 void Model_Session::registerPlugin(ModelAPI_Plugin* thePlugin)
269 myPluginObjs[myCurrentPluginName] = thePlugin;
270 static Events_ID EVENT_LOAD = Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED);
271 ModelAPI_EventCreator::get()->sendUpdated(ObjectPtr(), EVENT_LOAD);
272 Events_Loop::loop()->flush(EVENT_LOAD);
275 ModelAPI_ValidatorsFactory* Model_Session::validators()
277 static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;