1 // File: Model_Document.cxx
2 // Created: 28 Feb 2014
3 // Author: Mikhail PONIKAROV
5 #include <Model_Document.h>
6 #include <ModelAPI_Feature.h>
7 #include <Model_Object.h>
8 #include <Model_Application.h>
9 #include <Model_PluginManager.h>
10 #include <Model_Iterator.h>
11 #include <Event_Loop.h>
13 #include <TDataStd_Integer.hxx>
14 #include <TDataStd_Comment.hxx>
16 static const int UNDO_LIMIT = 10; // number of possible undo operations
18 static const int TAG_GENERAL = 1; // general properties tag
19 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree (Root for Model_ObjectsMgr)
20 static const int TAG_HISTORY = 3; // tag of the history sub-tree (Root for Model_History)
24 bool Model_Document::load(const char* theFileName)
26 bool myIsError = Standard_False;
28 TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
29 PCDM_ReaderStatus aStatus = (PCDM_ReaderStatus) -1;
32 Handle(TDocStd_Document) aDoc = this;
33 aStatus = Model_Application::GetApplication()->Open(aPath, aDoc);
35 catch (Standard_Failure)
37 myIsError = aStatus != PCDM_RS_OK;
42 case PCDM_RS_UnknownDocument: cout<<"OCAFApp_Appl_RUnknownDocument"<<endl; break;
43 case PCDM_RS_AlreadyRetrieved: cout<<"OCAFApp_Appl_RAlreadyRetrieved"<<endl; break;
44 case PCDM_RS_AlreadyRetrievedAndModified: cout<<"OCAFApp_Appl_RAlreadyRetrievedAndModified"<<endl; break;
45 case PCDM_RS_NoDriver: cout<<"OCAFApp_Appl_RNoDriver"<<endl; break;
46 case PCDM_RS_UnknownFileDriver: cout<<"OCAFApp_Appl_RNoDriver"<<endl; break;
47 case PCDM_RS_OpenError: cout<<"OCAFApp_Appl_ROpenError"<<endl; break;
48 case PCDM_RS_NoVersion: cout<<"OCAFApp_Appl_RNoVersion"<<endl; break;
49 case PCDM_RS_NoModel: cout<<"OCAFApp_Appl_RNoModel"<<endl; break;
50 case PCDM_RS_NoDocument: cout<<"OCAFApp_Appl_RNoDocument"<<endl; break;
51 case PCDM_RS_FormatFailure: cout<<"OCAFApp_Appl_RFormatFailure"<<endl; break;
52 case PCDM_RS_TypeNotFoundInSchema: cout<<"OCAFApp_Appl_RTypeNotFound"<<endl; break;
53 case PCDM_RS_UnrecognizedFileFormat: cout<<"OCAFApp_Appl_RBadFileFormat"<<endl; break;
54 case PCDM_RS_MakeFailure: cout<<"OCAFApp_Appl_RMakeFailure"<<endl; break;
55 case PCDM_RS_PermissionDenied: cout<<"OCAFApp_Appl_RPermissionDenied"<<endl; break;
56 case PCDM_RS_DriverFailure: cout<<"OCAFApp_Appl_RDriverFailure"<<endl; break;
57 default: cout<<"OCAFApp_Appl_RUnknownFail"<<endl; break;
60 SetUndoLimit(UNDO_LIMIT);
65 bool Model_Document::save(const char* theFileName)
67 bool myIsError = true;
69 TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
70 PCDM_StoreStatus aStatus;
72 Handle(TDocStd_Document) aDoc = this;
73 aStatus = Model_Application::GetApplication()->SaveAs (aDoc, aPath);
75 catch (Standard_Failure) {
76 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
77 cout<<"OCAFApp_Engine:save Error: "<<aFail->GetMessageString()<<endl;
80 myIsError = aStatus != PCDM_SS_OK;
85 case PCDM_SS_DriverFailure:
86 cout<<"OCAFApp_Appl_SDriverFailure"<<endl;
88 case PCDM_SS_WriteFailure:
89 cout<<"OCAFApp_Appl_SWriteFailure"<<endl;
93 cout<<"OCAFApp_Appl_SUnknownFailure"<<endl;
97 myTransactionsAfterSave = 0;
98 Standard::Purge(); // Release free memory
103 void Model_Document::close()
108 void Model_Document::startOperation()
113 void Model_Document::finishOperation()
115 myDoc->CommitCommand();
116 myTransactionsAfterSave++;
119 void Model_Document::abortOperation()
121 myDoc->AbortCommand();
124 bool Model_Document::isOperation()
126 return myDoc->HasOpenCommand() == Standard_True ;
129 bool Model_Document::isModified()
131 return myTransactionsAfterSave != 0;
134 bool Model_Document::canUndo()
136 return myDoc->GetAvailableUndos() > 0;
139 void Model_Document::undo()
142 myTransactionsAfterSave--;
145 bool Model_Document::canRedo()
147 return myDoc->GetAvailableRedos() > 0;
150 void Model_Document::redo()
153 myTransactionsAfterSave++;
156 shared_ptr<ModelAPI_Feature> Model_Document::addFeature(string theID)
158 shared_ptr<ModelAPI_Feature> aFeature = ModelAPI_PluginManager::get()->createFeature(theID);
160 dynamic_pointer_cast<Model_Document>(aFeature->documentToAdd())->addFeature(aFeature);
162 // TODO: generate error that feature is not created
167 void Model_Document::addFeature(const std::shared_ptr<ModelAPI_Feature> theFeature)
169 TDF_Label aGroupLab = groupLabel(theFeature->getGroup());
170 TDF_Label anObjLab = aGroupLab.NewChild();
171 std::shared_ptr<Model_Object> aData(new Model_Object);
172 aData->setLabel(anObjLab);
173 aData->setDocument(Model_Application::getApplication()->getDocument(myID));
174 theFeature->setData(aData);
175 setUniqueName(theFeature);
176 theFeature->initAttributes();
177 // keep the feature ID to restore document later correctly
178 TDataStd_Comment::Set(anObjLab, theFeature->getKind().c_str());
180 // event: model is updated
181 static Event_ID anEvent = Event_Loop::eventByName(EVENT_FEATURE_UPDATED);
182 ModelAPI_FeatureUpdatedMessage aMsg(theFeature);
183 Event_Loop::loop()->send(aMsg);
186 shared_ptr<ModelAPI_Feature> Model_Document::feature(TDF_Label& theLabel)
188 Handle(TDataStd_Comment) aFeatureID;
189 if (theLabel.FindAttribute(TDataStd_Comment::GetID(), aFeatureID)) {
190 string anID(TCollection_AsciiString(aFeatureID->Get()).ToCString());
191 std::shared_ptr<ModelAPI_Feature> aResult =
192 Model_PluginManager::get()->createFeature(anID);
193 std::shared_ptr<Model_Object> aData(new Model_Object);
194 aData->setLabel(theLabel);
195 aData->setDocument(Model_Application::getApplication()->getDocument(myID));
196 aResult->setData(aData);
197 aResult->initAttributes();
200 return std::shared_ptr<ModelAPI_Feature>(); // not found
203 int Model_Document::featureIndex(shared_ptr<ModelAPI_Feature> theFeature)
205 if (theFeature->data()->document().get() != this)
206 return theFeature->data()->document()->featureIndex(theFeature);
207 shared_ptr<ModelAPI_Iterator> anIter(featuresIterator(theFeature->getGroup()));
208 for(int anIndex = 0; anIter->more(); anIter->next(), anIndex++)
209 if (anIter->is(theFeature))
211 return -1; // not found
214 shared_ptr<ModelAPI_Document> Model_Document::subDocument(string theDocID)
216 return Model_Application::getApplication()->getDocument(theDocID);
219 shared_ptr<ModelAPI_Iterator> Model_Document::featuresIterator(const string theGroup)
221 shared_ptr<Model_Document> aThis(Model_Application::getApplication()->getDocument(myID));
222 return shared_ptr<ModelAPI_Iterator>(new Model_Iterator(aThis, groupLabel(theGroup)));
225 shared_ptr<ModelAPI_Feature> Model_Document::feature(const string& theGroupID, const int theIndex)
227 // TODO: optimize this method
228 shared_ptr<ModelAPI_Iterator> anIter = featuresIterator(theGroupID);
229 for(int a = 0; a != theIndex && anIter->more(); anIter->next()) a++;
230 return anIter->more() ? anIter->current() : shared_ptr<ModelAPI_Feature>();
233 const vector<string>& Model_Document::getGroups() const
235 return myGroupsNames;
238 Model_Document::Model_Document(const std::string theID)
239 : myID(theID), myDoc(new TDocStd_Document("BinOcaf")) // binary OCAF format
241 myDoc->SetUndoLimit(UNDO_LIMIT);
242 myTransactionsAfterSave = 0;
245 TDF_Label Model_Document::groupLabel(const string theGroup)
247 if (myGroups.find(theGroup) == myGroups.end()) {
248 myGroups[theGroup] = myDoc->Main().FindChild(TAG_OBJECTS).NewChild();
249 myGroupsNames.push_back(theGroup);
251 return myGroups[theGroup];
254 void Model_Document::setUniqueName(
255 shared_ptr<ModelAPI_Feature> theFeature)
257 // first count all objects of such kind to start with index = count + 1
259 shared_ptr<ModelAPI_Iterator> anIter = featuresIterator(theFeature->getGroup());
260 for(; anIter->more(); anIter->next()) {
261 if (anIter->currentKind() == theFeature->getKind())
264 // generate candidate name
265 stringstream aNameStream;
266 aNameStream<<theFeature->getKind()<<"_"<<aNumObjects + 1;
267 string aName = aNameStream.str();
268 // check this is unique, if not, increase index by 1
269 for(anIter = featuresIterator(theFeature->getGroup()); anIter->more();) {
270 if (anIter->currentName() == aName) {
272 stringstream aNameStream;
273 aNameStream<<theFeature->getKind()<<"_"<<aNumObjects + 1;
274 // reinitialize iterator to make sure a new name is unique
275 anIter = featuresIterator(theFeature->getGroup());
276 } else anIter->next();
279 theFeature->data()->setName(aName);
283 ModelAPI_FeatureUpdatedMessage::ModelAPI_FeatureUpdatedMessage(
284 shared_ptr<ModelAPI_Feature> theFeature)
285 : Event_Message(messageId(), 0), myFeature(theFeature)
288 const Event_ID ModelAPI_FeatureUpdatedMessage::messageId()
290 static Event_ID MY_ID = Event_Loop::eventByName("FeatureUpdated");
294 shared_ptr<ModelAPI_Feature> ModelAPI_FeatureUpdatedMessage::feature()