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 void Model_Document::addFeature(
157 std::shared_ptr<ModelAPI_Feature> theFeature, const std::string theGroupID)
159 TDF_Label aGroupLab = groupLabel(theGroupID);
160 TDF_Label anObjLab = aGroupLab.NewChild();
161 std::shared_ptr<Model_Object> aData(new Model_Object);
162 aData->setLabel(anObjLab);
163 theFeature->setData(aData);
164 setUniqueName(theFeature, theGroupID);
165 theFeature->initAttributes();
166 TDataStd_Comment::Set(anObjLab, theFeature->getKind().c_str());
168 // event: model is updated
169 static Event_ID anEvent = Event_Loop::eventByName(EVENT_MODEL_UPDATED);
170 Event_Message anUpdateMsg(anEvent, this);
171 Event_Loop::loop()->send(anUpdateMsg);
175 std::shared_ptr<ModelAPI_Feature> Model_Document::feature(TDF_Label& theLabel)
177 Handle(TDataStd_Comment) aFeatureID;
178 if (theLabel.FindAttribute(TDataStd_Comment::GetID(), aFeatureID)) {
179 string anID(TCollection_AsciiString(aFeatureID->Get()).ToCString());
180 std::shared_ptr<ModelAPI_Feature> aResult = Model_PluginManager::get()->createFeature(anID);
181 std::shared_ptr<Model_Object> aData(new Model_Object);
182 aData->setLabel(theLabel);
183 aResult->setData(aData);
184 aResult->initAttributes();
187 return std::shared_ptr<ModelAPI_Feature>(); // not found
190 shared_ptr<ModelAPI_Document> Model_Document::subDocument(string theDocID)
192 return Model_Application::getApplication()->getDocument(theDocID);
195 shared_ptr<ModelAPI_Iterator> Model_Document::featuresIterator(const string theGroup)
197 shared_ptr<Model_Document> aThis(Model_Application::getApplication()->getDocument(myID));
198 return shared_ptr<ModelAPI_Iterator>(new Model_Iterator(aThis, groupLabel(theGroup)));
201 shared_ptr<ModelAPI_Feature> Model_Document::feature(const string& theGroupID, const int theIndex)
203 // TODO: optimize this method
204 shared_ptr<ModelAPI_Iterator> anIter = featuresIterator(theGroupID);
205 for(int a = 0; a != theIndex; anIter->next()) a++;
206 return anIter->current();
209 Model_Document::Model_Document(const std::string theID)
210 : myID(theID), myDoc(new TDocStd_Document("BinOcaf")) // binary OCAF format
212 myDoc->SetUndoLimit(UNDO_LIMIT);
213 myTransactionsAfterSave = 0;
216 TDF_Label Model_Document::groupLabel(const string theGroup)
218 if (myGroups.find(theGroup) == myGroups.end()) {
219 myGroups[theGroup] = myDoc->Main().FindChild(TAG_OBJECTS).NewChild();
221 return myGroups[theGroup];
224 void Model_Document::setUniqueName(
225 shared_ptr<ModelAPI_Feature> theFeature, const string theGroupID)
227 // first count all objects of such kind to start with index = count + 1
229 shared_ptr<ModelAPI_Iterator> anIter = featuresIterator(theGroupID);
230 for(; anIter->more(); anIter->next()) {
231 if (anIter->currentKind() == theFeature->getKind())
234 // generate candidate name
235 stringstream aNameStream;
236 aNameStream<<theFeature->getKind()<<"_"<<aNumObjects + 1;
237 string aName = aNameStream.str();
238 // check this is unique, if not, increase index by 1
239 for(anIter = featuresIterator(theGroupID); anIter->more();) {
240 if (anIter->currentName() == aName) {
242 stringstream aNameStream;
243 aNameStream<<theFeature->getKind()<<"_"<<aNumObjects + 1;
244 // reinitialize iterator to make sure a new name is unique
245 anIter = featuresIterator(theGroupID);
246 } else anIter->next();
249 theFeature->data()->setName(aName);