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 if (myCurrentDoc != theDoc) {
137 myCurrentDoc = theDoc;
138 static Events_Message aMsg(Events_Loop::eventByName("CurrentDocumentChanged"));
139 Events_Loop::loop()->send(aMsg);
143 boost::shared_ptr<ModelAPI_Document> Model_Session::copy(
144 boost::shared_ptr<ModelAPI_Document> theSource, std::string theID)
146 // create a new document
147 boost::shared_ptr<Model_Document> aNew = boost::dynamic_pointer_cast<Model_Document>(
148 Model_Application::getApplication()->getDocument(theID));
149 // make a copy of all labels
150 TDF_Label aSourceRoot = boost::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main()
152 TDF_Label aTargetRoot = aNew->document()->Main().Father();
153 Handle(TDF_DataSet) aDS = new TDF_DataSet;
154 aDS->AddLabel(aSourceRoot);
155 TDF_ClosureTool::Closure(aDS);
156 Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
157 aRT->SetRelocation(aSourceRoot, aTargetRoot);
158 TDF_CopyTool::Copy(aDS, aRT);
160 aNew->synchronizeFeatures();
164 Model_Session::Model_Session()
166 myPluginsInfoLoaded = false;
167 myCheckTransactions = true;
168 ModelAPI_Session::setSession(boost::shared_ptr<ModelAPI_Session>(this));
169 // register the configuration reading listener
170 Events_Loop* aLoop = Events_Loop::loop();
171 static const Events_ID kFeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
172 aLoop->registerListener(this, kFeatureEvent);
173 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
174 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
175 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
176 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_VALIDATOR_LOADED));
179 void Model_Session::processEvent(const Events_Message* theMessage)
181 static const Events_ID kFeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
182 static const Events_ID kValidatorEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
183 if (theMessage->eventID() == kFeatureEvent) {
184 const Config_FeatureMessage* aMsg = dynamic_cast<const Config_FeatureMessage*>(theMessage);
186 // proccess the plugin info, load plugin
187 if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
188 myPlugins[aMsg->id()] = aMsg->pluginLibrary();
191 // plugins information was started to load, so, it will be loaded
192 myPluginsInfoLoaded = true;
193 } else if (theMessage->eventID() == kValidatorEvent) {
194 const Config_ValidatorMessage* aMsg = dynamic_cast<const Config_ValidatorMessage*>(theMessage);
196 if (aMsg->attributeId().empty()) { // feature validator
197 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->parameters());
198 } else { // attribute validator
199 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->attributeId(),
203 } else { // create/update/delete
204 if (myCheckTransactions && !isOperation())
205 Events_Error::send("Modification of data structure outside of the transaction");
209 void Model_Session::LoadPluginsInfo()
211 if (myPluginsInfoLoaded) // nothing to do
214 // Read plugins information from XML files
215 Config_ModuleReader aXMLReader("FeatureRegisterEvent");
216 aXMLReader.readAll();
219 void Model_Session::registerPlugin(ModelAPI_Plugin* thePlugin)
221 myPluginObjs[myCurrentPluginName] = thePlugin;
222 static Events_ID EVENT_LOAD = Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED);
223 ModelAPI_EventCreator::get()->sendUpdated(ObjectPtr(), EVENT_LOAD);
224 Events_Loop::loop()->flush(EVENT_LOAD);
227 ModelAPI_ValidatorsFactory* Model_Session::validators()
229 static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;