]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_Document.cxx
Salome HOME
Merge branch 'master' of newgeom:newgeom
[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 void Model_Document::addFeature(
157   std::shared_ptr<ModelAPI_Feature> theFeature, const std::string theGroupID)
158 {
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());
167
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);
172
173 }
174
175 std::shared_ptr<ModelAPI_Feature> Model_Document::feature(TDF_Label& theLabel)
176 {
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();
185     return aResult;
186   }
187   return std::shared_ptr<ModelAPI_Feature>(); // not found
188 }
189
190 shared_ptr<ModelAPI_Document> Model_Document::subDocument(string theDocID)
191 {
192   return Model_Application::getApplication()->getDocument(theDocID);
193 }
194
195 shared_ptr<ModelAPI_Iterator> Model_Document::featuresIterator(const string theGroup)
196 {
197   shared_ptr<Model_Document> aThis(Model_Application::getApplication()->getDocument(myID));
198   return shared_ptr<ModelAPI_Iterator>(new Model_Iterator(aThis, groupLabel(theGroup)));
199 }
200
201 shared_ptr<ModelAPI_Feature> Model_Document::feature(const string& theGroupID, const int theIndex)
202 {
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();
207 }
208
209 Model_Document::Model_Document(const std::string theID)
210     : myID(theID), myDoc(new TDocStd_Document("BinOcaf")) // binary OCAF format
211 {
212   myDoc->SetUndoLimit(UNDO_LIMIT);
213   myTransactionsAfterSave = 0;
214 }
215
216 TDF_Label Model_Document::groupLabel(const string theGroup)
217 {
218   if (myGroups.find(theGroup) == myGroups.end()) {
219     myGroups[theGroup] = myDoc->Main().FindChild(TAG_OBJECTS).NewChild();
220   }
221   return myGroups[theGroup];
222 }
223
224 void Model_Document::setUniqueName(
225   shared_ptr<ModelAPI_Feature> theFeature, const string theGroupID)
226 {
227   // first count all objects of such kind to start with index = count + 1
228   int aNumObjects = 0;
229   shared_ptr<ModelAPI_Iterator> anIter = featuresIterator(theGroupID);
230   for(; anIter->more(); anIter->next()) {
231     if (anIter->currentKind() == theFeature->getKind())
232       aNumObjects++;
233   }
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) {
241       aNumObjects++;
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();
247   }
248
249   theFeature->data()->setName(aName);
250 }