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 myDoc->CommitCommand();
137 myTransactionsAfterSave++;
139 if( theName.Length() != 0 )
141 const TDF_DeltaList& aList = GetUndos();
142 if( !aList.IsEmpty() )
144 Handle(TDF_Delta) aDelta = aList.Last();
145 if( !aDelta.IsNull() )
146 aDelta->SetName( theName );
151 void HYDROData_Document::AbortOperation()
153 myDoc->AbortCommand();
156 bool HYDROData_Document::IsOperation()
158 return myDoc->HasOpenCommand() != 0;
161 bool HYDROData_Document::IsModified()
163 return myTransactionsAfterSave != 0;
166 bool HYDROData_Document::CanUndo()
168 return myDoc->GetAvailableUndos() > 0;
171 const TDF_DeltaList& HYDROData_Document::GetUndos()
173 return myDoc->GetUndos();
176 void HYDROData_Document::ClearUndos()
178 return myDoc->ClearUndos();
181 void HYDROData_Document::Undo()
184 myTransactionsAfterSave--;
187 bool HYDROData_Document::CanRedo()
189 return myDoc->GetAvailableRedos() > 0;
192 const TDF_DeltaList& HYDROData_Document::GetRedos()
194 return myDoc->GetRedos();
197 void HYDROData_Document::ClearRedos()
199 return myDoc->ClearRedos();
202 void HYDROData_Document::Redo()
205 myTransactionsAfterSave++;
208 Handle_HYDROData_Object HYDROData_Document::CreateObject(const ObjectKind theKind)
210 return HYDROData_Iterator::CreateObject(this, theKind);
213 HYDROData_Document::HYDROData_Document()
215 HYDROData_Application::GetApplication()->NewDocument("BinOcaf", myDoc);
216 myDoc->SetUndoLimit(UNDO_LIMIT);
217 NewID(); // needed to have at least one attribute in initial document to avoid errors
218 myTransactionsAfterSave = 0;
221 HYDROData_Document::HYDROData_Document(const Handle(TDocStd_Document)& theDoc)
224 myTransactionsAfterSave = 0;
227 HYDROData_Document::~HYDROData_Document()
231 int HYDROData_Document::NewID()
233 TDF_Label anIDLab = myDoc->Main().FindChild(TAG_PROPS).
234 FindChild(TAG_PROPS_NEW_ID);
235 Handle(TDataStd_Integer) anInt;
236 if (!anIDLab.FindAttribute(TDataStd_Integer::GetID(), anInt)) {
237 anInt = TDataStd_Integer::Set(anIDLab, 0);
239 // just increment value and return
240 anInt->Set(anInt->Get() + 1);
244 TDF_Label HYDROData_Document::LabelOfObjects()
246 return myDoc->Main().FindChild(TAG_OBJECTS);