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 std::pair<std::string, std::string>& aPlugin = myPlugins[theFeatureID]; // plugin and doc kind
94 if (!aPlugin.second.empty() && aPlugin.second != activeDocument()->kind()) {
96 string("Feature '") + theFeatureID + "' can not be created in document '"
97 + aPlugin.second + "' by the XML definition");
100 myCurrentPluginName = aPlugin.first;
101 if (myPluginObjs.find(myCurrentPluginName) == myPluginObjs.end()) {
102 // load plugin library if not yet done
103 Config_ModuleReader::loadLibrary(myCurrentPluginName);
105 if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
106 FeaturePtr aCreated = myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
109 string("Can not initialize feature '") + theFeatureID + "' in plugin '"
110 + myCurrentPluginName + "'");
114 Events_Error::send(string("Can not load plugin '") + myCurrentPluginName + "'");
117 Events_Error::send(string("Feature '") + theFeatureID + "' not found in any plugin");
120 return FeaturePtr(); // return nothing
123 boost::shared_ptr<ModelAPI_Document> Model_Session::moduleDocument()
125 return boost::shared_ptr<ModelAPI_Document>(
126 Model_Application::getApplication()->getDocument("root"));
129 bool Model_Session::hasModuleDocument()
131 return Model_Application::getApplication()->hasDocument("root");
134 boost::shared_ptr<ModelAPI_Document> Model_Session::activeDocument()
136 if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
137 myCurrentDoc = moduleDocument();
141 void Model_Session::setActiveDocument(boost::shared_ptr<ModelAPI_Document> theDoc)
143 if (myCurrentDoc != theDoc) {
144 myCurrentDoc = theDoc;
145 static boost::shared_ptr<Events_Message> aMsg(new Events_Message(Events_Loop::eventByName("CurrentDocumentChanged")));
146 Events_Loop::loop()->send(aMsg);
150 std::list<boost::shared_ptr<ModelAPI_Document> > Model_Session::allOpenedDocuments()
152 list<boost::shared_ptr<ModelAPI_Document> > aResult;
153 aResult.push_back(moduleDocument());
154 // add subs recursively
155 list<boost::shared_ptr<ModelAPI_Document> >::iterator aDoc = aResult.begin();
156 for(; aDoc != aResult.end(); aDoc++) {
157 DocumentPtr anAPIDoc = *aDoc;
158 boost::shared_ptr<Model_Document> aDoc = boost::dynamic_pointer_cast<Model_Document>(anAPIDoc);
160 std::set<std::string>::const_iterator aSubIter = aDoc->subDocuments().cbegin();
161 for(; aSubIter != aDoc->subDocuments().cend(); aSubIter++) {
162 if (!Model_Application::getApplication()->isLoadByDemand(*aSubIter)) {
163 aResult.push_back(Model_Application::getApplication()->getDocument(*aSubIter));
171 boost::shared_ptr<ModelAPI_Document> Model_Session::copy(
172 boost::shared_ptr<ModelAPI_Document> theSource, std::string theID)
174 // create a new document
175 boost::shared_ptr<Model_Document> aNew = boost::dynamic_pointer_cast<Model_Document>(
176 Model_Application::getApplication()->getDocument(theID));
177 // make a copy of all labels
178 TDF_Label aSourceRoot = boost::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main()
180 TDF_Label aTargetRoot = aNew->document()->Main().Father();
181 Handle(TDF_DataSet) aDS = new TDF_DataSet;
182 aDS->AddLabel(aSourceRoot);
183 TDF_ClosureTool::Closure(aDS);
184 Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
185 aRT->SetRelocation(aSourceRoot, aTargetRoot);
186 TDF_CopyTool::Copy(aDS, aRT);
188 aNew->synchronizeFeatures();
192 Model_Session::Model_Session()
194 myPluginsInfoLoaded = false;
195 myCheckTransactions = true;
196 ModelAPI_Session::setSession(boost::shared_ptr<ModelAPI_Session>(this));
197 // register the configuration reading listener
198 Events_Loop* aLoop = Events_Loop::loop();
199 static const Events_ID kFeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
200 aLoop->registerListener(this, kFeatureEvent);
201 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
202 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
203 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
204 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_VALIDATOR_LOADED));
207 void Model_Session::processEvent(const boost::shared_ptr<Events_Message>& theMessage)
209 static const Events_ID kFeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
210 static const Events_ID kValidatorEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
211 if (theMessage->eventID() == kFeatureEvent) {
212 const boost::shared_ptr<Config_FeatureMessage> aMsg =
213 boost::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
215 // proccess the plugin info, load plugin
216 if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
217 myPlugins[aMsg->id()] = std::pair<std::string, std::string>(
218 aMsg->pluginLibrary(), aMsg->documentKind());
221 // plugins information was started to load, so, it will be loaded
222 myPluginsInfoLoaded = true;
223 } else if (theMessage->eventID() == kValidatorEvent) {
224 boost::shared_ptr<Config_ValidatorMessage> aMsg =
225 boost::dynamic_pointer_cast<Config_ValidatorMessage>(theMessage);
227 if (aMsg->attributeId().empty()) { // feature validator
228 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->parameters());
229 } else { // attribute validator
230 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->attributeId(),
234 } else { // create/update/delete
235 if (myCheckTransactions && !isOperation())
236 Events_Error::send("Modification of data structure outside of the transaction");
240 void Model_Session::LoadPluginsInfo()
242 if (myPluginsInfoLoaded) // nothing to do
245 // Read plugins information from XML files
246 Config_ModuleReader aXMLReader("FeatureRegisterEvent");
247 aXMLReader.readAll();
250 void Model_Session::registerPlugin(ModelAPI_Plugin* thePlugin)
252 myPluginObjs[myCurrentPluginName] = thePlugin;
253 static Events_ID EVENT_LOAD = Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED);
254 ModelAPI_EventCreator::get()->sendUpdated(ObjectPtr(), EVENT_LOAD);
255 Events_Loop::loop()->flush(EVENT_LOAD);
258 ModelAPI_ValidatorsFactory* Model_Session::validators()
260 static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;