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_AttributeMessage.h>
17 #include <Config_ValidatorMessage.h>
18 #include <Config_ModuleReader.h>
20 #include <TDF_CopyTool.hxx>
21 #include <TDF_DataSet.hxx>
22 #include <TDF_RelocationTable.hxx>
23 #include <TDF_ClosureTool.hxx>
27 static Model_Session* myImpl = new Model_Session();
29 // t oredirect all calls to the root document
30 #define ROOT_DOC boost::dynamic_pointer_cast<Model_Document>(moduleDocument())
32 bool Model_Session::load(const char* theFileName)
34 return ROOT_DOC->load(theFileName);
37 bool Model_Session::save(const char* theFileName, std::list<std::string>& theResults)
39 return ROOT_DOC->save(theFileName, theResults);
42 void Model_Session::closeAll()
44 ROOT_DOC->close(true);
45 Model_Application::getApplication()->deleteAllDocuments();
48 void Model_Session::startOperation()
50 ROOT_DOC->startOperation();
51 static boost::shared_ptr<Events_Message> aStartedMsg
52 (new Events_Message(Events_Loop::eventByName("StartOperation")));
53 Events_Loop::loop()->send(aStartedMsg);
56 void Model_Session::finishOperation()
58 ROOT_DOC->finishOperation();
61 void Model_Session::abortOperation()
63 ROOT_DOC->abortOperation();
64 static boost::shared_ptr<Events_Message> anAbortMsg
65 (new Events_Message(Events_Loop::eventByName("AbortOperation")));
66 Events_Loop::loop()->send(anAbortMsg);
69 bool Model_Session::isOperation()
71 return ROOT_DOC->isOperation();
74 bool Model_Session::isModified()
76 return ROOT_DOC->isModified();
79 bool Model_Session::canUndo()
81 return ROOT_DOC->canUndo();
84 void Model_Session::undo()
89 bool Model_Session::canRedo()
91 return ROOT_DOC->canRedo();
94 void Model_Session::redo()
99 FeaturePtr Model_Session::createFeature(string theFeatureID)
102 return myImpl->createFeature(theFeatureID);
104 if (myPlugins.find(theFeatureID) != myPlugins.end()) {
105 std::pair<std::string, std::string>& aPlugin = myPlugins[theFeatureID]; // plugin and doc kind
106 if (!aPlugin.second.empty() && aPlugin.second != activeDocument()->kind()) {
108 string("Feature '") + theFeatureID + "' can be created only in document '"
109 + aPlugin.second + "' by the XML definition");
112 myCurrentPluginName = aPlugin.first;
113 if (myPluginObjs.find(myCurrentPluginName) == myPluginObjs.end()) {
114 // load plugin library if not yet done
115 Config_ModuleReader::loadLibrary(myCurrentPluginName);
117 if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
118 FeaturePtr aCreated = myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
121 string("Can not initialize feature '") + theFeatureID + "' in plugin '"
122 + myCurrentPluginName + "'");
126 Events_Error::send(string("Can not load plugin '") + myCurrentPluginName + "'");
129 Events_Error::send(string("Feature '") + theFeatureID + "' not found in any plugin");
132 return FeaturePtr(); // return nothing
135 boost::shared_ptr<ModelAPI_Document> Model_Session::moduleDocument()
137 return boost::shared_ptr<ModelAPI_Document>(
138 Model_Application::getApplication()->getDocument("root"));
141 bool Model_Session::hasModuleDocument()
143 return Model_Application::getApplication()->hasDocument("root");
146 boost::shared_ptr<ModelAPI_Document> Model_Session::activeDocument()
148 if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
149 myCurrentDoc = moduleDocument();
153 void Model_Session::setActiveDocument(
154 boost::shared_ptr<ModelAPI_Document> theDoc, bool theSendSignal)
156 if (myCurrentDoc != theDoc) {
157 myCurrentDoc = theDoc;
159 static boost::shared_ptr<Events_Message> aMsg(new Events_Message(Events_Loop::eventByName("CurrentDocumentChanged")));
160 Events_Loop::loop()->send(aMsg);
165 std::list<boost::shared_ptr<ModelAPI_Document> > Model_Session::allOpenedDocuments()
167 list<boost::shared_ptr<ModelAPI_Document> > aResult;
168 aResult.push_back(moduleDocument());
169 // add subs recursively
170 list<boost::shared_ptr<ModelAPI_Document> >::iterator aDoc = aResult.begin();
171 for(; aDoc != aResult.end(); aDoc++) {
172 DocumentPtr anAPIDoc = *aDoc;
173 boost::shared_ptr<Model_Document> aDoc = boost::dynamic_pointer_cast<Model_Document>(anAPIDoc);
175 std::set<std::string>::const_iterator aSubIter = aDoc->subDocuments().cbegin();
176 for(; aSubIter != aDoc->subDocuments().cend(); aSubIter++) {
177 if (!Model_Application::getApplication()->isLoadByDemand(*aSubIter)) {
178 aResult.push_back(Model_Application::getApplication()->getDocument(*aSubIter));
186 boost::shared_ptr<ModelAPI_Document> Model_Session::copy(
187 boost::shared_ptr<ModelAPI_Document> theSource, std::string theID)
189 // create a new document
190 boost::shared_ptr<Model_Document> aNew = boost::dynamic_pointer_cast<Model_Document>(
191 Model_Application::getApplication()->getDocument(theID));
192 // make a copy of all labels
193 TDF_Label aSourceRoot = boost::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main()
195 TDF_Label aTargetRoot = aNew->document()->Main().Father();
196 Handle(TDF_DataSet) aDS = new TDF_DataSet;
197 aDS->AddLabel(aSourceRoot);
198 TDF_ClosureTool::Closure(aDS);
199 Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
200 aRT->SetRelocation(aSourceRoot, aTargetRoot);
201 TDF_CopyTool::Copy(aDS, aRT);
203 aNew->synchronizeFeatures(false, true);
207 Model_Session::Model_Session()
209 myPluginsInfoLoaded = false;
210 myCheckTransactions = true;
211 ModelAPI_Session::setSession(boost::shared_ptr<ModelAPI_Session>(this));
212 // register the configuration reading listener
213 Events_Loop* aLoop = Events_Loop::loop();
214 static const Events_ID kFeatureEvent = Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
215 aLoop->registerListener(this, kFeatureEvent);
216 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED), 0, true);
217 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED), 0, true);
218 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED), 0, true);
219 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_VALIDATOR_LOADED));
221 // load all information about plugins, features and attributes
225 void Model_Session::processEvent(const boost::shared_ptr<Events_Message>& theMessage)
227 static const Events_ID kFeatureEvent = Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
228 static const Events_ID kValidatorEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
229 if (theMessage->eventID() == kFeatureEvent) {
230 const boost::shared_ptr<Config_FeatureMessage> aMsg =
231 boost::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
233 // proccess the plugin info, load plugin
234 if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
235 myPlugins[aMsg->id()] = std::pair<std::string, std::string>(
236 aMsg->pluginLibrary(), aMsg->documentKind());
239 const boost::shared_ptr<Config_AttributeMessage> aMsgAttr =
240 boost::dynamic_pointer_cast<Config_AttributeMessage>(theMessage);
242 if (!aMsgAttr->isObligatory()) {
243 validators()->registerNotObligatory(aMsgAttr->featureId(), aMsgAttr->attributeId());
245 if(aMsgAttr->isConcealment()) {
246 validators()->registerConcealment(aMsgAttr->featureId(), aMsgAttr->attributeId());
251 // plugins information was started to load, so, it will be loaded
252 myPluginsInfoLoaded = true;
253 } else if (theMessage->eventID() == kValidatorEvent) {
254 boost::shared_ptr<Config_ValidatorMessage> aMsg =
255 boost::dynamic_pointer_cast<Config_ValidatorMessage>(theMessage);
257 if (aMsg->attributeId().empty()) { // feature validator
258 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->parameters());
259 } else { // attribute validator
260 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->attributeId(),
264 } else { // create/update/delete
265 if (myCheckTransactions && !isOperation())
266 Events_Error::send("Modification of data structure outside of the transaction");
270 void Model_Session::LoadPluginsInfo()
272 if (myPluginsInfoLoaded) // nothing to do
275 // Read plugins information from XML files
276 Config_ModuleReader aXMLReader(Config_FeatureMessage::MODEL_EVENT());
277 aXMLReader.readAll();
280 void Model_Session::registerPlugin(ModelAPI_Plugin* thePlugin)
282 myPluginObjs[myCurrentPluginName] = thePlugin;
283 static Events_ID EVENT_LOAD = Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED);
284 ModelAPI_EventCreator::get()->sendUpdated(ObjectPtr(), EVENT_LOAD);
285 Events_Loop::loop()->flush(EVENT_LOAD);
288 ModelAPI_ValidatorsFactory* Model_Session::validators()
290 static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;