0029901: Support save to and restore from Stream interface in TObj package
[occt.git] / src / TObj / TObj_Application.cxx
CommitLineData
b311480e 1// Created on: 2004-11-23
2// Created by: Pavel TELKOV
973c2be1 3// Copyright (c) 2004-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
7fd59977 16// The original implementation Copyright: (C) RINA S.p.A
17
18#include <TObj_Application.hxx>
19
20#include <Standard_SStream.hxx>
21#include <Standard_ErrorHandler.hxx>
bc73b006 22#include <Standard_Dump.hxx>
7fd59977 23#include <TCollection_ExtendedString.hxx>
24#include <TColStd_SequenceOfExtendedString.hxx>
7fd59977 25#include <Message_Msg.hxx>
ee5befae 26#include <Message_MsgFile.hxx>
6fe96f84 27#include <Resource_Manager.hxx>
6fe96f84 28#include <stdio.h>
7fd59977 29
ee5befae 30#include "TObj_TObj_msg.pxx"
31
92efcf78 32IMPLEMENT_STANDARD_RTTIEXT(TObj_Application,TDocStd_Application)
33
7fd59977 34//=======================================================================
35//function : GetInstance
36//purpose :
37//=======================================================================
7fd59977 38Handle(TObj_Application) TObj_Application::GetInstance()
39{
8a39adb7 40 static Handle(TObj_Application) THE_TOBJ_APP(new TObj_Application);
41 return THE_TOBJ_APP;
7fd59977 42}
43
44//=======================================================================
45//function : TObj_Application
46//purpose :
47//=======================================================================
48
c24d4017 49TObj_Application::TObj_Application () : myIsError(Standard_False)
7fd59977 50{
ee5befae 51 if (!Message_MsgFile::HasMsg ("TObj_Appl_SUnknownFailure"))
52 {
53 // load messages into global map on first instantiation
54 Message_MsgFile::LoadFromString (TObj_TObj_msg, sizeof(TObj_TObj_msg) - 1);
55 if (!Message_MsgFile::HasMsg ("TObj_Appl_SUnknownFailure"))
56 {
9775fa61 57 throw Standard_ProgramError("Critical Error - message resources for TObj_Application are invalid or undefined!");
ee5befae 58 }
59 }
60
7fd59977 61 myMessenger = new Message_Messenger;
7fd59977 62 myIsVerbose = Standard_False;
63}
64
7fd59977 65//=======================================================================
66//function : ResourcesName
67//purpose :
68//=======================================================================
69
70Standard_CString TObj_Application::ResourcesName()
71{
72 return Standard_CString("TObj");
73}
74
75//=======================================================================
76//function : SaveDocument
77//purpose : Saving the OCAF document
78//=======================================================================
79
80Standard_Boolean TObj_Application::SaveDocument
be70005c 81 (const Handle(TDocStd_Document)& theSourceDoc,
82 const TCollection_ExtendedString& theTargetFile)
7fd59977 83{
a8d3a0b1 84 const PCDM_StoreStatus aStatus = SaveAs (theSourceDoc, theTargetFile);
85 myIsError = (aStatus != PCDM_SS_OK);
7fd59977 86 if (myIsError)
a8d3a0b1 87 SetError (aStatus, theTargetFile);
88
89 // Release free memory
90 Standard::Purge();
91 return myIsError ? Standard_False : Standard_True;
92}
93
94//=======================================================================
95//function : SaveDocument
96//purpose : Saving the OCAF document to a stream
97//=======================================================================
98
99Standard_Boolean TObj_Application::SaveDocument
100 (const Handle(TDocStd_Document)& theSourceDoc,
101 Standard_OStream& theOStream)
102{
103 const PCDM_StoreStatus aStatus = SaveAs (theSourceDoc, theOStream);
104 myIsError = (aStatus != PCDM_SS_OK);
105 if (myIsError)
106 SetError (aStatus, "");
7fd59977 107
108 // Release free memory
109 Standard::Purge();
110 return myIsError ? Standard_False : Standard_True;
111}
112
113//=======================================================================
114//function : LoadDocument
115//purpose : Loading the OCAF document
116//=======================================================================
117
118Standard_Boolean TObj_Application::LoadDocument
be70005c 119 (const TCollection_ExtendedString& theSourceFile,
120 Handle(TDocStd_Document)& theTargetDoc)
7fd59977 121{
498ce76b 122 PCDM_ReaderStatus aStatus = PCDM_RS_ReaderException;
7fd59977 123 {
124 try
125 {
1f9fb707 126 aStatus = Open (theSourceFile, theTargetDoc);
7fd59977 127 }
9775fa61 128 catch (Standard_Failure const& anException) {
0797d9d3 129#ifdef OCCT_DEBUG
7fd59977 130 ErrorMessage (Message_Msg("TObj_Appl_Exception") <<
9775fa61 131 anException.GetMessageString());
7fd59977 132#endif
a8d3a0b1 133 (void) anException;
7fd59977 134 }
135 }
a8d3a0b1 136 myIsError = (aStatus != PCDM_RS_OK);
7fd59977 137 if (myIsError)
a8d3a0b1 138 SetError (aStatus, theSourceFile);
139
140 // Release free memory
141 Standard::Purge();
142 return myIsError ? Standard_False : Standard_True;
143}
144
145//=======================================================================
146//function : LoadDocument
147//purpose : Loading the OCAF document from a stream
148//=======================================================================
149
150Standard_Boolean TObj_Application::LoadDocument
151 (Standard_IStream& theIStream,
152 Handle(TDocStd_Document)& theTargetDoc)
153{
154 PCDM_ReaderStatus aStatus = PCDM_RS_ReaderException;
7fd59977 155 {
a8d3a0b1 156 try
7fd59977 157 {
a8d3a0b1 158 aStatus = Open (theIStream, theTargetDoc);
159 }
160 catch (Standard_Failure const& anException) {
161#ifdef OCCT_DEBUG
162 ErrorMessage(Message_Msg("TObj_Appl_Exception") << anException.GetMessageString());
163#endif
164 (void) anException;
7fd59977 165 }
166 }
a8d3a0b1 167 myIsError = (aStatus != PCDM_RS_OK);
168 if (myIsError)
169 SetError (aStatus, "");
7fd59977 170
171 // Release free memory
172 Standard::Purge();
173 return myIsError ? Standard_False : Standard_True;
174}
175
176//=======================================================================
177//function : CreateNewDocument
178//purpose :
179//=======================================================================
180
181Standard_Boolean TObj_Application::CreateNewDocument
182 (Handle(TDocStd_Document)& theDoc,
183 const TCollection_ExtendedString& theFormat)
184{
185 myIsError = Standard_False;
186
187 // Create the Document
188 NewDocument (theFormat, theDoc);
189
190 return myIsError ? Standard_False : Standard_True;
191}
192
193//=======================================================================
194//function : ErrorMessage
195//purpose :
196//=======================================================================
197
198void TObj_Application::ErrorMessage (const TCollection_ExtendedString &theMsg,
199 const Message_Gravity theLevel)
200{
201 myMessenger->Send ( theMsg, theLevel );
202}
203
bc73b006 204//=======================================================================
205//function : DumpJson
206//purpose :
207//=======================================================================
208void TObj_Application::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
209{
210 OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
211
212 OCCT_DUMP_BASE_CLASS (theOStream, theDepth, TDocStd_Application)
213}
a8d3a0b1 214
215//=======================================================================
216//function : SetError
217//purpose : Sets an error occured on storage of a document.
218//=======================================================================
219
220void TObj_Application::SetError (const PCDM_StoreStatus theStatus, const TCollection_ExtendedString& theInfo)
221{
222 switch (theStatus)
223 {
224 case PCDM_SS_DriverFailure:
225 ErrorMessage(Message_Msg("TObj_Appl_SDriverFailure") << theInfo);
226 break;
227 case PCDM_SS_WriteFailure:
228 ErrorMessage(Message_Msg("TObj_Appl_SWriteFailure") << theInfo);
229 break;
230 case PCDM_SS_Failure:
231 ErrorMessage(Message_Msg("TObj_Appl_SFailure") << theInfo);
232 break;
233 case PCDM_SS_Doc_IsNull:
234 ErrorMessage(Message_Msg("TObj_Appl_SDocIsNull") << theInfo);
235 break;
236 case PCDM_SS_No_Obj:
237 ErrorMessage(Message_Msg("TObj_Appl_SNoObj") << theInfo);
238 break;
239 case PCDM_SS_Info_Section_Error:
240 ErrorMessage(Message_Msg("TObj_Appl_SInfoSectionError") << theInfo);
241 break;
242 default:
243 ErrorMessage(Message_Msg("TObj_Appl_SUnknownFailure") << theInfo);
244 break;
245 }
246}
247
248//=======================================================================
249//function : SetError
250//purpose : Sets an error occured on reading of a document.
251//=======================================================================
252
253void TObj_Application::SetError(const PCDM_ReaderStatus theStatus, const TCollection_ExtendedString& theInfo)
254{
255 switch (theStatus)
256 {
257 case PCDM_RS_UnknownDocument:
258 ErrorMessage(Message_Msg("TObj_Appl_RUnknownDocument") << theInfo);
259 break;
260 case PCDM_RS_AlreadyRetrieved:
261 ErrorMessage(Message_Msg("TObj_Appl_RAlreadyRetrieved") << theInfo);
262 break;
263 case PCDM_RS_AlreadyRetrievedAndModified:
264 ErrorMessage(Message_Msg("TObj_Appl_RAlreadyRetrievedAndModified") << theInfo);
265 break;
266 case PCDM_RS_NoDriver:
267 ErrorMessage(Message_Msg("TObj_Appl_RNoDriver") << theInfo);
268 break;
269 case PCDM_RS_UnknownFileDriver:
270 ErrorMessage(Message_Msg("TObj_Appl_RNoDriver") << theInfo);
271 break;
272 case PCDM_RS_OpenError:
273 ErrorMessage(Message_Msg("TObj_Appl_ROpenError") << theInfo);
274 break;
275 case PCDM_RS_NoVersion:
276 ErrorMessage(Message_Msg("TObj_Appl_RNoVersion") << theInfo);
277 break;
278 case PCDM_RS_NoModel:
279 ErrorMessage(Message_Msg("TObj_Appl_RNoModel") << theInfo);
280 break;
281 case PCDM_RS_NoDocument:
282 ErrorMessage(Message_Msg("TObj_Appl_RNoDocument") << theInfo);
283 break;
284 case PCDM_RS_FormatFailure:
285 ErrorMessage(Message_Msg("TObj_Appl_RFormatFailure") << theInfo);
286 break;
287 case PCDM_RS_TypeNotFoundInSchema:
288 ErrorMessage(Message_Msg("TObj_Appl_RTypeNotFound") << theInfo);
289 break;
290 case PCDM_RS_UnrecognizedFileFormat:
291 ErrorMessage(Message_Msg("TObj_Appl_RBadFileFormat") << theInfo);
292 break;
293 case PCDM_RS_MakeFailure:
294 ErrorMessage(Message_Msg("TObj_Appl_RMakeFailure") << theInfo);
295 break;
296 case PCDM_RS_PermissionDenied:
297 ErrorMessage(Message_Msg("TObj_Appl_RPermissionDenied") << theInfo);
298 break;
299 case PCDM_RS_DriverFailure:
300 ErrorMessage(Message_Msg("TObj_Appl_RDriverFailure") << theInfo);
301 break;
302 case PCDM_RS_ReaderException:
303 ErrorMessage(Message_Msg("TObj_Appl_RException") << theInfo);
304 break;
305 default:
306 ErrorMessage(Message_Msg("TObj_Appl_RUnknownFail") << theInfo);
307 break;
308 }
309}