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();
44 static boost::shared_ptr<Events_Message> aStartedMsg
45 (new Events_Message(Events_Loop::eventByName("StartOperation")));
46 Events_Loop::loop()->send(aStartedMsg);
49 void Model_Session::finishOperation()
51 ROOT_DOC->finishOperation();
54 void Model_Session::abortOperation()
56 ROOT_DOC->abortOperation();
57 static boost::shared_ptr<Events_Message> anAbortMsg
58 (new Events_Message(Events_Loop::eventByName("AbortOperation")));
59 Events_Loop::loop()->send(anAbortMsg);
62 bool Model_Session::isOperation()
64 return ROOT_DOC->isOperation();
67 bool Model_Session::isModified()
69 return ROOT_DOC->isModified();
72 bool Model_Session::canUndo()
74 return ROOT_DOC->canUndo();
77 void Model_Session::undo()
82 bool Model_Session::canRedo()
84 return ROOT_DOC->canRedo();
87 void Model_Session::redo()
92 FeaturePtr Model_Session::createFeature(string theFeatureID)
95 return myImpl->createFeature(theFeatureID);
98 if (myPlugins.find(theFeatureID) != myPlugins.end()) {
99 std::pair<std::string, std::string>& aPlugin = myPlugins[theFeatureID]; // plugin and doc kind
100 if (!aPlugin.second.empty() && aPlugin.second != activeDocument()->kind()) {
102 string("Feature '") + theFeatureID + "' can be created only in document '"
103 + aPlugin.second + "' by the XML definition");
106 myCurrentPluginName = aPlugin.first;
107 if (myPluginObjs.find(myCurrentPluginName) == myPluginObjs.end()) {
108 // load plugin library if not yet done
109 Config_ModuleReader::loadLibrary(myCurrentPluginName);
111 if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
112 FeaturePtr aCreated = myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
115 string("Can not initialize feature '") + theFeatureID + "' in plugin '"
116 + myCurrentPluginName + "'");
120 Events_Error::send(string("Can not load plugin '") + myCurrentPluginName + "'");
123 Events_Error::send(string("Feature '") + theFeatureID + "' not found in any plugin");
126 return FeaturePtr(); // return nothing
129 boost::shared_ptr<ModelAPI_Document> Model_Session::moduleDocument()
131 return boost::shared_ptr<ModelAPI_Document>(
132 Model_Application::getApplication()->getDocument("root"));
135 bool Model_Session::hasModuleDocument()
137 return Model_Application::getApplication()->hasDocument("root");
140 boost::shared_ptr<ModelAPI_Document> Model_Session::activeDocument()
142 if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
143 myCurrentDoc = moduleDocument();
147 void Model_Session::setActiveDocument(
148 boost::shared_ptr<ModelAPI_Document> theDoc, bool theSendSignal)
150 if (myCurrentDoc != theDoc) {
151 myCurrentDoc = theDoc;
153 static boost::shared_ptr<Events_Message> aMsg(new Events_Message(Events_Loop::eventByName("CurrentDocumentChanged")));
154 Events_Loop::loop()->send(aMsg);
159 std::list<boost::shared_ptr<ModelAPI_Document> > Model_Session::allOpenedDocuments()
161 list<boost::shared_ptr<ModelAPI_Document> > aResult;
162 aResult.push_back(moduleDocument());
163 // add subs recursively
164 list<boost::shared_ptr<ModelAPI_Document> >::iterator aDoc = aResult.begin();
165 for(; aDoc != aResult.end(); aDoc++) {
166 DocumentPtr anAPIDoc = *aDoc;
167 boost::shared_ptr<Model_Document> aDoc = boost::dynamic_pointer_cast<Model_Document>(anAPIDoc);
169 std::set<std::string>::const_iterator aSubIter = aDoc->subDocuments().cbegin();
170 for(; aSubIter != aDoc->subDocuments().cend(); aSubIter++) {
171 if (!Model_Application::getApplication()->isLoadByDemand(*aSubIter)) {
172 aResult.push_back(Model_Application::getApplication()->getDocument(*aSubIter));
180 boost::shared_ptr<ModelAPI_Document> Model_Session::copy(
181 boost::shared_ptr<ModelAPI_Document> theSource, std::string theID)
183 // create a new document
184 boost::shared_ptr<Model_Document> aNew = boost::dynamic_pointer_cast<Model_Document>(
185 Model_Application::getApplication()->getDocument(theID));
186 // make a copy of all labels
187 TDF_Label aSourceRoot = boost::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main()
189 TDF_Label aTargetRoot = aNew->document()->Main().Father();
190 Handle(TDF_DataSet) aDS = new TDF_DataSet;
191 aDS->AddLabel(aSourceRoot);
192 TDF_ClosureTool::Closure(aDS);
193 Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
194 aRT->SetRelocation(aSourceRoot, aTargetRoot);
195 TDF_CopyTool::Copy(aDS, aRT);
197 aNew->synchronizeFeatures(false, true);
201 Model_Session::Model_Session()
203 myPluginsInfoLoaded = false;
204 myCheckTransactions = true;
205 ModelAPI_Session::setSession(boost::shared_ptr<ModelAPI_Session>(this));
206 // register the configuration reading listener
207 Events_Loop* aLoop = Events_Loop::loop();
208 static const Events_ID kFeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
209 aLoop->registerListener(this, kFeatureEvent);
210 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED), 0, true);
211 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED), 0, true);
212 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED), 0, true);
213 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_VALIDATOR_LOADED));
216 void Model_Session::processEvent(const boost::shared_ptr<Events_Message>& theMessage)
218 static const Events_ID kFeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
219 static const Events_ID kValidatorEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
220 if (theMessage->eventID() == kFeatureEvent) {
221 const boost::shared_ptr<Config_FeatureMessage> aMsg =
222 boost::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
224 // proccess the plugin info, load plugin
225 if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
226 myPlugins[aMsg->id()] = std::pair<std::string, std::string>(
227 aMsg->pluginLibrary(), aMsg->documentKind());
230 // plugins information was started to load, so, it will be loaded
231 myPluginsInfoLoaded = true;
232 } else if (theMessage->eventID() == kValidatorEvent) {
233 boost::shared_ptr<Config_ValidatorMessage> aMsg =
234 boost::dynamic_pointer_cast<Config_ValidatorMessage>(theMessage);
236 if (aMsg->attributeId().empty()) { // feature validator
237 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->parameters());
238 } else { // attribute validator
239 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->attributeId(),
243 } else { // create/update/delete
244 if (myCheckTransactions && !isOperation())
245 Events_Error::send("Modification of data structure outside of the transaction");
249 void Model_Session::LoadPluginsInfo()
251 if (myPluginsInfoLoaded) // nothing to do
254 // Read plugins information from XML files
255 Config_ModuleReader aXMLReader("FeatureRegisterEvent");
256 aXMLReader.readAll();
259 void Model_Session::registerPlugin(ModelAPI_Plugin* thePlugin)
261 myPluginObjs[myCurrentPluginName] = thePlugin;
262 static Events_ID EVENT_LOAD = Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED);
263 ModelAPI_EventCreator::get()->sendUpdated(ObjectPtr(), EVENT_LOAD);
264 Events_Loop::loop()->flush(EVENT_LOAD);
267 ModelAPI_ValidatorsFactory* Model_Session::validators()
269 static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;