1 #include <HYDROData_Document.h>
2 #include <HYDROData_Application.h>
3 #include <HYDROData_Iterator.h>
5 #include <TDataStd_Integer.hxx>
7 #include <TDF_Delta.hxx>
9 IMPLEMENT_STANDARD_HANDLE(HYDROData_Document,MMgt_TShared)
10 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Document,MMgt_TShared)
12 static const int UNDO_LIMIT = 10; // number of possible undo operations in the module
14 static const int TAG_PROPS = 1; // general properties tag
15 static const int TAG_PROPS_NEW_ID = 1; // general properties: tag for storage of the new object ID
16 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree
17 static const int TAG_HISTORY = 3; // tag of the history sub-tree (Root for History)
21 Handle(HYDROData_Document) HYDROData_Document::Document(const int theStudyID)
23 Handle(HYDROData_Document) aResult =
24 HYDROData_Application::GetApplication()->GetDocument(theStudyID);
25 if (aResult.IsNull()) {
26 aResult = new HYDROData_Document();
27 HYDROData_Application::GetApplication()->AddDocument(theStudyID, aResult);
32 bool HYDROData_Document::HasDocument(const int theStudyID)
34 Handle(HYDROData_Document) aResult =
35 HYDROData_Application::GetApplication()->GetDocument(theStudyID);
36 return !aResult.IsNull();
39 Data_DocError HYDROData_Document::Load(const char* theFileName, const int theStudyID)
41 Handle(TDocStd_Document) aResult;
42 TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
43 PCDM_ReaderStatus aStatus = (PCDM_ReaderStatus) -1;
46 aStatus = HYDROData_Application::GetApplication()->Open (aPath, aResult);
48 catch (Standard_Failure)
50 if (!aResult.IsNull()) {
51 aResult->SetUndoLimit(UNDO_LIMIT);
52 HYDROData_Application::GetApplication()->AddDocument(theStudyID, new HYDROData_Document(aResult));
55 Data_DocError anError;
58 anError = DocError_OK;
60 case PCDM_RS_NoDriver:
61 case PCDM_RS_UnknownFileDriver:
62 case PCDM_RS_NoSchema:
63 case PCDM_RS_DriverFailure:
64 case PCDM_RS_WrongResource:
65 anError = DocError_ResourcesProblem;
67 case PCDM_RS_OpenError:
68 case PCDM_RS_NoDocument:
69 case PCDM_RS_WrongStreamMode:
70 case PCDM_RS_PermissionDenied:
71 anError = DocError_CanNotOpen;
73 case PCDM_RS_NoVersion:
74 anError = DocError_InvalidVersion;
76 case PCDM_RS_ExtensionFailure:
77 case PCDM_RS_FormatFailure:
78 case PCDM_RS_TypeFailure:
79 case PCDM_RS_TypeNotFoundInSchema:
80 case PCDM_RS_UnrecognizedFileFormat:
81 anError = DocError_InvalidFormat;
83 case PCDM_RS_MakeFailure:
85 anError = DocError_UnknownProblem;
91 Data_DocError HYDROData_Document::Save(const char* theFileName)
93 TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
94 PCDM_StoreStatus aStatus;
96 aStatus = HYDROData_Application::GetApplication()->SaveAs (myDoc, aPath);
98 catch (Standard_Failure) {}
99 myTransactionsAfterSave = 0;
100 Standard::Purge(); // Release free memory
103 Data_DocError anError;
106 anError = DocError_OK;
108 case PCDM_SS_DriverFailure:
109 anError = DocError_ResourcesProblem;
111 case PCDM_SS_WriteFailure:
112 case PCDM_SS_DiskWritingFailure:
113 case PCDM_SS_UserRightsFailure:
114 anError = DocError_CanNotOpen;
117 anError = DocError_UnknownProblem;
123 void HYDROData_Document::Close()
126 HYDROData_Application::GetApplication()->RemoveDocument(this);
129 void HYDROData_Document::StartOperation()
134 void HYDROData_Document::CommitOperation(const TCollection_ExtendedString& theName)
136 if( !myDoc->CommitCommand() ) // it means that there were no modifications done
139 NewID(); // workaround: do something just to modify the document
140 myDoc->CommitCommand();
142 myTransactionsAfterSave++;
144 if( theName.Length() != 0 )
146 const TDF_DeltaList& aList = GetUndos();
147 if( !aList.IsEmpty() )
149 Handle(TDF_Delta) aDelta = aList.Last();
150 if( !aDelta.IsNull() )
151 aDelta->SetName( theName );
156 void HYDROData_Document::AbortOperation()
158 myDoc->AbortCommand();
161 bool HYDROData_Document::IsOperation()
163 return myDoc->HasOpenCommand() != 0;
166 bool HYDROData_Document::IsModified()
168 return myTransactionsAfterSave != 0;
171 bool HYDROData_Document::CanUndo()
173 return myDoc->GetAvailableUndos() > 0;
176 const TDF_DeltaList& HYDROData_Document::GetUndos()
178 return myDoc->GetUndos();
181 void HYDROData_Document::ClearUndos()
183 return myDoc->ClearUndos();
186 void HYDROData_Document::Undo()
189 myTransactionsAfterSave--;
192 bool HYDROData_Document::CanRedo()
194 return myDoc->GetAvailableRedos() > 0;
197 const TDF_DeltaList& HYDROData_Document::GetRedos()
199 return myDoc->GetRedos();
202 void HYDROData_Document::ClearRedos()
204 return myDoc->ClearRedos();
207 void HYDROData_Document::Redo()
210 myTransactionsAfterSave++;
213 Handle_HYDROData_Object HYDROData_Document::CreateObject(const ObjectKind theKind)
215 return HYDROData_Iterator::CreateObject(this, theKind);
218 HYDROData_Document::HYDROData_Document()
220 HYDROData_Application::GetApplication()->NewDocument("BinOcaf", myDoc);
221 myDoc->SetUndoLimit(UNDO_LIMIT);
222 NewID(); // needed to have at least one attribute in initial document to avoid errors
223 myTransactionsAfterSave = 0;
226 HYDROData_Document::HYDROData_Document(const Handle(TDocStd_Document)& theDoc)
229 myTransactionsAfterSave = 0;
232 HYDROData_Document::~HYDROData_Document()
236 int HYDROData_Document::NewID()
238 TDF_Label anIDLab = myDoc->Main().FindChild(TAG_PROPS).
239 FindChild(TAG_PROPS_NEW_ID);
240 Handle(TDataStd_Integer) anInt;
241 if (!anIDLab.FindAttribute(TDataStd_Integer::GetID(), anInt)) {
242 anInt = TDataStd_Integer::Set(anIDLab, 0);
244 // just increment value and return
245 anInt->Set(anInt->Get() + 1);
249 TDF_Label HYDROData_Document::LabelOfObjects()
251 return myDoc->Main().FindChild(TAG_OBJECTS);