Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[modules/shaper.git] / src / Model / Model_Document.cxx
1 // File:        Model_Document.cxx
2 // Created:     28 Feb 2014
3 // Author:      Mikhail PONIKAROV
4
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>
12
13 #include <TDataStd_Integer.hxx>
14 #include <TDataStd_Comment.hxx>
15
16 static const int UNDO_LIMIT = 10; // number of possible undo operations
17
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)
21
22 using namespace std;
23
24 bool Model_Document::load(const char* theFileName)
25 {
26   bool myIsError = Standard_False;
27   /*
28    TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
29    PCDM_ReaderStatus aStatus = (PCDM_ReaderStatus) -1;
30    try
31    {
32    Handle(TDocStd_Document) aDoc = this;
33    aStatus = Model_Application::GetApplication()->Open(aPath, aDoc);
34    }
35    catch (Standard_Failure)
36    {}
37    myIsError = aStatus != PCDM_RS_OK;
38    if (myIsError)
39    {
40    switch (aStatus)
41    {
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;
58    }
59    }
60    SetUndoLimit(UNDO_LIMIT);
61    */
62   return !myIsError;
63 }
64
65 bool Model_Document::save(const char* theFileName)
66 {
67   bool myIsError = true;
68   /*
69    TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
70    PCDM_StoreStatus aStatus;
71    try {
72    Handle(TDocStd_Document) aDoc = this;
73    aStatus = Model_Application::GetApplication()->SaveAs (aDoc, aPath);
74    }
75    catch (Standard_Failure) {
76    Handle(Standard_Failure) aFail = Standard_Failure::Caught();
77    cout<<"OCAFApp_Engine:save Error: "<<aFail->GetMessageString()<<endl;
78    return false;
79    }
80    myIsError = aStatus != PCDM_SS_OK;
81    if (myIsError)
82    {
83    switch (aStatus)
84    {
85    case PCDM_SS_DriverFailure:
86    cout<<"OCAFApp_Appl_SDriverFailure"<<endl;
87    break;
88    case PCDM_SS_WriteFailure:
89    cout<<"OCAFApp_Appl_SWriteFailure"<<endl;
90    break;
91    case PCDM_SS_Failure:
92    default:
93    cout<<"OCAFApp_Appl_SUnknownFailure"<<endl;
94    break;
95    }
96    }
97    myTransactionsAfterSave = 0;
98    Standard::Purge(); // Release free memory
99    */
100   return !myIsError;
101 }
102
103 void Model_Document::close()
104 {
105   myDoc->Close();
106 }
107
108 void Model_Document::startOperation()
109 {
110   myDoc->NewCommand();
111 }
112
113 void Model_Document::finishOperation()
114 {
115   myDoc->CommitCommand();
116   myTransactionsAfterSave++;
117 }
118
119 void Model_Document::abortOperation()
120 {
121   myDoc->AbortCommand();
122 }
123
124 bool Model_Document::isOperation()
125 {
126   return myDoc->HasOpenCommand() == Standard_True ;
127 }
128
129 bool Model_Document::isModified()
130 {
131   return myTransactionsAfterSave != 0;
132 }
133
134 bool Model_Document::canUndo()
135 {
136   return myDoc->GetAvailableUndos() > 0;
137 }
138
139 void Model_Document::undo()
140 {
141   myDoc->Undo();
142   myTransactionsAfterSave--;
143 }
144
145 bool Model_Document::canRedo()
146 {
147   return myDoc->GetAvailableRedos() > 0;
148 }
149
150 void Model_Document::redo()
151 {
152   myDoc->Redo();
153   myTransactionsAfterSave++;
154 }
155
156 shared_ptr<ModelAPI_Feature> Model_Document::addFeature(string theID)
157 {
158   shared_ptr<ModelAPI_Feature> aFeature = ModelAPI_PluginManager::get()->createFeature(theID);
159   if (aFeature) {
160     dynamic_pointer_cast<Model_Document>(aFeature->documentToAdd())->addFeature(aFeature);
161   } else {
162     // TODO: generate error that feature is not created
163   }
164   return aFeature;
165 }
166
167 void Model_Document::addFeature(const std::shared_ptr<ModelAPI_Feature> theFeature)
168 {
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());
179
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);
184 }
185
186 shared_ptr<ModelAPI_Feature> Model_Document::feature(TDF_Label& theLabel)
187 {
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();
198     return aResult;
199   }
200   return std::shared_ptr<ModelAPI_Feature>(); // not found
201 }
202
203 int Model_Document::featureIndex(shared_ptr<ModelAPI_Feature> theFeature)
204 {
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)) 
210       return anIndex;
211   return -1; // not found
212 }
213
214 shared_ptr<ModelAPI_Document> Model_Document::subDocument(string theDocID)
215 {
216   return Model_Application::getApplication()->getDocument(theDocID);
217 }
218
219 shared_ptr<ModelAPI_Iterator> Model_Document::featuresIterator(const string theGroup)
220 {
221   shared_ptr<Model_Document> aThis(Model_Application::getApplication()->getDocument(myID));
222   return shared_ptr<ModelAPI_Iterator>(new Model_Iterator(aThis, groupLabel(theGroup)));
223 }
224
225 shared_ptr<ModelAPI_Feature> Model_Document::feature(const string& theGroupID, const int theIndex)
226 {
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>();
231 }
232
233 const vector<string>& Model_Document::getGroups() const
234 {
235   return myGroupsNames;
236 }
237
238 Model_Document::Model_Document(const std::string theID)
239     : myID(theID), myDoc(new TDocStd_Document("BinOcaf")) // binary OCAF format
240 {
241   myDoc->SetUndoLimit(UNDO_LIMIT);
242   myTransactionsAfterSave = 0;
243 }
244
245 TDF_Label Model_Document::groupLabel(const string theGroup)
246 {
247   if (myGroups.find(theGroup) == myGroups.end()) {
248     myGroups[theGroup] = myDoc->Main().FindChild(TAG_OBJECTS).NewChild();
249     myGroupsNames.push_back(theGroup);
250   }
251   return myGroups[theGroup];
252 }
253
254 void Model_Document::setUniqueName(
255   shared_ptr<ModelAPI_Feature> theFeature)
256 {
257   // first count all objects of such kind to start with index = count + 1
258   int aNumObjects = 0;
259   shared_ptr<ModelAPI_Iterator> anIter = featuresIterator(theFeature->getGroup());
260   for(; anIter->more(); anIter->next()) {
261     if (anIter->currentKind() == theFeature->getKind())
262       aNumObjects++;
263   }
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) {
271       aNumObjects++;
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();
277   }
278
279   theFeature->data()->setName(aName);
280 }
281
282
283 ModelAPI_FeatureUpdatedMessage::ModelAPI_FeatureUpdatedMessage(
284   shared_ptr<ModelAPI_Feature> theFeature)
285   : Event_Message(messageId(), 0), myFeature(theFeature)
286 {}
287
288 const Event_ID ModelAPI_FeatureUpdatedMessage::messageId()
289 {
290   static Event_ID MY_ID = Event_Loop::eventByName("FeatureUpdated");
291   return MY_ID;
292 }
293
294 shared_ptr<ModelAPI_Feature> ModelAPI_FeatureUpdatedMessage::feature()
295 {
296   return myFeature;
297 }