1 #include <HYDROData_Document.h>
2 #include <HYDROData_Application.h>
3 #include <HYDROData_Iterator.h>
5 #include <TDataStd_Integer.hxx>
7 IMPLEMENT_STANDARD_HANDLE(HYDROData_Document,MMgt_TShared)
8 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Document,MMgt_TShared)
10 static const int UNDO_LIMIT = 10; // number of possible undo operations in the module
12 static const int TAG_PROPS = 1; // general properties tag
13 static const int TAG_PROPS_NEW_ID = 1; // general properties: tag for storage of the new object ID
14 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree
15 static const int TAG_HISTORY = 3; // tag of the history sub-tree (Root for History)
19 Handle(HYDROData_Document) HYDROData_Document::Document(const int theStudyID)
21 Handle(HYDROData_Document) aResult =
22 HYDROData_Application::GetApplication()->GetDocument(theStudyID);
23 if (aResult.IsNull()) {
24 aResult = new HYDROData_Document();
25 HYDROData_Application::GetApplication()->AddDocument(theStudyID, aResult);
30 bool HYDROData_Document::HasDocument(const int theStudyID)
32 Handle(HYDROData_Document) aResult =
33 HYDROData_Application::GetApplication()->GetDocument(theStudyID);
34 return !aResult.IsNull();
37 Data_DocError HYDROData_Document::Load(const char* theFileName, const int theStudyID)
39 Handle(TDocStd_Document) aResult;
40 TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
41 PCDM_ReaderStatus aStatus = (PCDM_ReaderStatus) -1;
44 aStatus = HYDROData_Application::GetApplication()->Open (aPath, aResult);
46 catch (Standard_Failure)
48 if (!aResult.IsNull()) {
49 aResult->SetUndoLimit(UNDO_LIMIT);
50 HYDROData_Application::GetApplication()->AddDocument(theStudyID, new HYDROData_Document(aResult));
53 Data_DocError anError;
56 anError = DocError_OK;
58 case PCDM_RS_NoDriver:
59 case PCDM_RS_UnknownFileDriver:
60 case PCDM_RS_NoSchema:
61 case PCDM_RS_DriverFailure:
62 case PCDM_RS_WrongResource:
63 anError = DocError_ResourcesProblem;
65 case PCDM_RS_OpenError:
66 case PCDM_RS_NoDocument:
67 case PCDM_RS_WrongStreamMode:
68 case PCDM_RS_PermissionDenied:
69 anError = DocError_CanNotOpen;
71 case PCDM_RS_NoVersion:
72 anError = DocError_InvalidVersion;
74 case PCDM_RS_ExtensionFailure:
75 case PCDM_RS_FormatFailure:
76 case PCDM_RS_TypeFailure:
77 case PCDM_RS_TypeNotFoundInSchema:
78 case PCDM_RS_UnrecognizedFileFormat:
79 anError = DocError_InvalidFormat;
81 case PCDM_RS_MakeFailure:
83 anError = DocError_UnknownProblem;
89 Data_DocError HYDROData_Document::Save(const char* theFileName)
91 TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
92 PCDM_StoreStatus aStatus;
94 aStatus = HYDROData_Application::GetApplication()->SaveAs (myDoc, aPath);
96 catch (Standard_Failure) {}
97 myTransactionsAfterSave = 0;
98 Standard::Purge(); // Release free memory
101 Data_DocError anError;
104 anError = DocError_OK;
106 case PCDM_SS_DriverFailure:
107 anError = DocError_ResourcesProblem;
109 case PCDM_SS_WriteFailure:
110 case PCDM_SS_DiskWritingFailure:
111 case PCDM_SS_UserRightsFailure:
112 anError = DocError_CanNotOpen;
115 anError = DocError_UnknownProblem;
121 void HYDROData_Document::Close()
124 HYDROData_Application::GetApplication()->RemoveDocument(this);
127 void HYDROData_Document::StartOperation()
132 void HYDROData_Document::CommitOperation()
134 myDoc->CommitCommand();
135 myTransactionsAfterSave++;
138 void HYDROData_Document::AbortOperation()
140 myDoc->AbortCommand();
143 bool HYDROData_Document::IsOperation()
145 return myDoc->HasOpenCommand() != 0;
148 bool HYDROData_Document::IsModified()
150 return myTransactionsAfterSave != 0;
153 bool HYDROData_Document::CanUndo()
155 return myDoc->GetAvailableUndos() > 0;
158 void HYDROData_Document::Undo()
161 myTransactionsAfterSave--;
164 bool HYDROData_Document::CanRedo()
166 return myDoc->GetAvailableRedos() > 0;
169 void HYDROData_Document::Redo()
172 myTransactionsAfterSave++;
175 Handle_HYDROData_Object HYDROData_Document::CreateObject(const ObjectKind theKind)
177 return HYDROData_Iterator::CreateObject(this, theKind);
180 HYDROData_Document::HYDROData_Document()
182 HYDROData_Application::GetApplication()->NewDocument("BinOcaf", myDoc);
183 myDoc->SetUndoLimit(UNDO_LIMIT);
184 NewID(); // needed to have at least one attribute in initial document to avoid errors
185 myTransactionsAfterSave = 0;
188 HYDROData_Document::HYDROData_Document(const Handle(TDocStd_Document)& theDoc)
191 myTransactionsAfterSave = 0;
194 HYDROData_Document::~HYDROData_Document()
198 int HYDROData_Document::NewID()
200 TDF_Label anIDLab = myDoc->Main().FindChild(TAG_PROPS).
201 FindChild(TAG_PROPS_NEW_ID);
202 Handle(TDataStd_Integer) anInt;
203 if (!anIDLab.FindAttribute(TDataStd_Integer::GetID(), anInt)) {
204 anInt = TDataStd_Integer::Set(anIDLab, 0);
206 // just increment value and return
207 anInt->Set(anInt->Get() + 1);
211 TDF_Label HYDROData_Document::LabelOfObjects()
213 return myDoc->Main().FindChild(TAG_OBJECTS);