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 std::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 std::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 std::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)
101 if (this != myImpl) {
102 return myImpl->createFeature(theFeatureID);
105 // load all information about plugins, features and attributes
108 if (myPlugins.find(theFeatureID) != myPlugins.end()) {
109 std::pair<std::string, std::string>& aPlugin = myPlugins[theFeatureID]; // plugin and doc kind
110 if (!aPlugin.second.empty() && aPlugin.second != activeDocument()->kind()) {
112 string("Feature '") + theFeatureID + "' can be created only in document '"
113 + aPlugin.second + "' by the XML definition");
116 myCurrentPluginName = aPlugin.first;
117 if (myPluginObjs.find(myCurrentPluginName) == myPluginObjs.end()) {
118 // load plugin library if not yet done
119 Config_ModuleReader::loadPlugin(myCurrentPluginName);
121 if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
122 FeaturePtr aCreated = myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
125 string("Can not initialize feature '") + theFeatureID + "' in plugin '"
126 + myCurrentPluginName + "'");
130 Events_Error::send(string("Can not load plugin '") + myCurrentPluginName + "'");
133 Events_Error::send(string("Feature '") + theFeatureID + "' not found in any plugin");
136 return FeaturePtr(); // return nothing
139 std::shared_ptr<ModelAPI_Document> Model_Session::moduleDocument()
141 return std::shared_ptr<ModelAPI_Document>(
142 Model_Application::getApplication()->getDocument("root"));
145 bool Model_Session::hasModuleDocument()
147 return Model_Application::getApplication()->hasDocument("root");
150 std::shared_ptr<ModelAPI_Document> Model_Session::activeDocument()
152 if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
153 myCurrentDoc = moduleDocument();
157 void Model_Session::setActiveDocument(
158 std::shared_ptr<ModelAPI_Document> theDoc, bool theSendSignal)
160 if (myCurrentDoc != theDoc) {
161 myCurrentDoc = theDoc;
163 static std::shared_ptr<Events_Message> aMsg(new Events_Message(Events_Loop::eventByName("CurrentDocumentChanged")));
164 Events_Loop::loop()->send(aMsg);
169 std::list<std::shared_ptr<ModelAPI_Document> > Model_Session::allOpenedDocuments()
171 list<std::shared_ptr<ModelAPI_Document> > aResult;
172 aResult.push_back(moduleDocument());
173 // add subs recursively
174 list<std::shared_ptr<ModelAPI_Document> >::iterator aDoc = aResult.begin();
175 for(; aDoc != aResult.end(); aDoc++) {
176 DocumentPtr anAPIDoc = *aDoc;
177 std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(anAPIDoc);
179 std::set<std::string>::const_iterator aSubIter = aDoc->subDocuments().cbegin();
180 for(; aSubIter != aDoc->subDocuments().cend(); aSubIter++) {
181 if (!Model_Application::getApplication()->isLoadByDemand(*aSubIter)) {
182 aResult.push_back(Model_Application::getApplication()->getDocument(*aSubIter));
190 std::shared_ptr<ModelAPI_Document> Model_Session::copy(
191 std::shared_ptr<ModelAPI_Document> theSource, std::string theID)
193 // create a new document
194 std::shared_ptr<Model_Document> aNew = std::dynamic_pointer_cast<Model_Document>(
195 Model_Application::getApplication()->getDocument(theID));
196 // make a copy of all labels
197 TDF_Label aSourceRoot = std::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main()
199 TDF_Label aTargetRoot = aNew->document()->Main().Father();
200 Handle(TDF_DataSet) aDS = new TDF_DataSet;
201 aDS->AddLabel(aSourceRoot);
202 TDF_ClosureTool::Closure(aDS);
203 Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
204 aRT->SetRelocation(aSourceRoot, aTargetRoot);
205 TDF_CopyTool::Copy(aDS, aRT);
207 aNew->synchronizeFeatures(false, true);
211 Model_Session::Model_Session()
213 myPluginsInfoLoaded = false;
214 myCheckTransactions = true;
215 ModelAPI_Session::setSession(std::shared_ptr<ModelAPI_Session>(this));
216 // register the configuration reading listener
217 Events_Loop* aLoop = Events_Loop::loop();
218 static const Events_ID kFeatureEvent = Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
219 aLoop->registerListener(this, kFeatureEvent);
220 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED), 0, true);
221 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_UPDATED), 0, true);
222 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED), 0, true);
223 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_VALIDATOR_LOADED));
226 void Model_Session::processEvent(const std::shared_ptr<Events_Message>& theMessage)
228 static const Events_ID kFeatureEvent = Events_Loop::eventByName(Config_FeatureMessage::MODEL_EVENT());
229 static const Events_ID kValidatorEvent = Events_Loop::eventByName(EVENT_VALIDATOR_LOADED);
230 if (theMessage->eventID() == kFeatureEvent) {
231 const std::shared_ptr<Config_FeatureMessage> aMsg =
232 std::dynamic_pointer_cast<Config_FeatureMessage>(theMessage);
234 // proccess the plugin info, load plugin
235 if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
236 myPlugins[aMsg->id()] = std::pair<std::string, std::string>(
237 aMsg->pluginLibrary(), aMsg->documentKind());
240 const std::shared_ptr<Config_AttributeMessage> aMsgAttr =
241 std::dynamic_pointer_cast<Config_AttributeMessage>(theMessage);
243 if (!aMsgAttr->isObligatory()) {
244 validators()->registerNotObligatory(aMsgAttr->featureId(), aMsgAttr->attributeId());
246 if(aMsgAttr->isConcealment()) {
247 validators()->registerConcealment(aMsgAttr->featureId(), aMsgAttr->attributeId());
252 // plugins information was started to load, so, it will be loaded
253 myPluginsInfoLoaded = true;
254 } else if (theMessage->eventID() == kValidatorEvent) {
255 std::shared_ptr<Config_ValidatorMessage> aMsg =
256 std::dynamic_pointer_cast<Config_ValidatorMessage>(theMessage);
258 if (aMsg->attributeId().empty()) { // feature validator
259 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->parameters());
260 } else { // attribute validator
261 validators()->assignValidator(aMsg->validatorId(), aMsg->featureId(), aMsg->attributeId(),
265 } else { // create/update/delete
266 if (myCheckTransactions && !isOperation())
267 Events_Error::send("Modification of data structure outside of the transaction");
271 void Model_Session::LoadPluginsInfo()
273 if (myPluginsInfoLoaded) // nothing to do
276 // Read plugins information from XML files
277 Config_ModuleReader aXMLReader(Config_FeatureMessage::MODEL_EVENT());
278 aXMLReader.readAll();
281 void Model_Session::registerPlugin(ModelAPI_Plugin* thePlugin)
283 myPluginObjs[myCurrentPluginName] = thePlugin;
284 static Events_ID EVENT_LOAD = Events_Loop::loop()->eventByName(EVENT_PLUGIN_LOADED);
285 ModelAPI_EventCreator::get()->sendUpdated(ObjectPtr(), EVENT_LOAD);
286 Events_Loop::loop()->flush(EVENT_LOAD);
289 ModelAPI_ValidatorsFactory* Model_Session::validators()
291 static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;