0025418: Debug output to be limited to OCC development environment
[occt.git] / src / TObj / TObj_Application.cxx
1 // Created on: 2004-11-23
2 // Created by: Pavel TELKOV
3 // Copyright (c) 2004-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
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>
22 #include <TCollection_ExtendedString.hxx>
23 #include <TColStd_SequenceOfExtendedString.hxx>
24 #include <CDM_COutMessageDriver.hxx>
25 #include <Message_Msg.hxx>
26 #include <stdio.h>
27
28 IMPLEMENT_STANDARD_HANDLE(TObj_Application,TDocStd_Application)
29 IMPLEMENT_STANDARD_RTTIEXT(TObj_Application,TDocStd_Application)
30
31 //=======================================================================
32 //function : GetInstance
33 //purpose  :
34 //=======================================================================
35
36 Handle(TObj_Application) TObj_Application::GetInstance()
37 {
38   static Handle(TObj_Application) anInstance = new TObj_Application;
39   return anInstance;
40 }
41
42 //=======================================================================
43 //function : TObj_Application
44 //purpose  : 
45 //=======================================================================
46
47 TObj_Application::TObj_Application () : myIsError(Standard_False)
48 {
49   myMessenger = new Message_Messenger;
50   myMessageDriver = new CDM_COutMessageDriver;
51   myIsVerbose = Standard_False;
52 }
53
54 //=======================================================================
55 //function : Formats
56 //purpose  : 
57 //=======================================================================
58
59 void TObj_Application::Formats(TColStd_SequenceOfExtendedString& theFormats) 
60 {
61   theFormats.Append(TCollection_ExtendedString ("TObjXml"));
62   theFormats.Append(TCollection_ExtendedString ("TObjBin"));
63 }
64
65 //=======================================================================
66 //function : ResourcesName
67 //purpose  : 
68 //=======================================================================
69
70 Standard_CString TObj_Application::ResourcesName()
71 {
72   return Standard_CString("TObj");
73 }
74
75 //=======================================================================
76 //function : SaveDocument
77 //purpose  : Saving the OCAF document
78 //=======================================================================
79
80 Standard_Boolean TObj_Application::SaveDocument
81                         (const Handle(TDocStd_Document)&   theSourceDoc,
82                          const char*                       theTargetFile)
83 {
84   myIsError = Standard_False;
85   TCollection_ExtendedString aPath ((const Standard_CString)theTargetFile);
86
87   PCDM_StoreStatus aStatus = SaveAs (theSourceDoc, aPath);
88   myIsError = aStatus != PCDM_SS_OK;
89   if (myIsError)
90   {
91     switch (aStatus)
92     {
93     case PCDM_SS_DriverFailure:
94       ErrorMessage (Message_Msg("TObj_Appl_SDriverFailure") << aPath);
95       break;
96     case PCDM_SS_WriteFailure:
97       ErrorMessage (Message_Msg("TObj_Appl_SWriteFailure") << aPath);
98       break;
99     case PCDM_SS_Failure:
100       ErrorMessage (Message_Msg("TObj_Appl_SFailure") << aPath);
101       break;
102     case PCDM_SS_Doc_IsNull:
103       ErrorMessage (Message_Msg("TObj_Appl_SDocIsNull") << aPath);
104       break;
105     case PCDM_SS_No_Obj:
106       ErrorMessage (Message_Msg("TObj_Appl_SNoObj") << aPath);
107       break;
108     case PCDM_SS_Info_Section_Error:
109       ErrorMessage (Message_Msg("TObj_Appl_SInfoSectionError") << aPath);
110       break;
111     default:
112       ErrorMessage (Message_Msg("TObj_Appl_SUnknownFailure") << aPath);
113       break;
114     }
115   }
116
117   // Release free memory
118   Standard::Purge();
119   return myIsError ? Standard_False : Standard_True;
120 }
121
122 //=======================================================================
123 //function : LoadDocument
124 //purpose  : Loading the OCAF document
125 //=======================================================================
126
127 Standard_Boolean TObj_Application::LoadDocument
128                         (const char*                       theSourceFile,
129                          Handle(TDocStd_Document)&         theTargetDoc)
130 {
131   myIsError = Standard_False;
132   TCollection_ExtendedString aPath ((const Standard_CString)theSourceFile);
133
134   PCDM_ReaderStatus aStatus = PCDM_RS_ReaderException;
135   {
136     try
137     {
138       aStatus = Open (aPath, theTargetDoc);
139     }
140     catch (Standard_Failure)
141     {
142 #ifdef OCCT_DEBUG
143       ErrorMessage (Message_Msg("TObj_Appl_Exception") << 
144                     Standard_Failure::Caught()->GetMessageString());
145 #endif
146     }
147   }
148   myIsError = aStatus != PCDM_RS_OK;
149   if (myIsError)
150   {
151     switch (aStatus)
152     {
153     case PCDM_RS_UnknownDocument:
154       ErrorMessage (Message_Msg("TObj_Appl_RUnknownDocument") << aPath);
155       break;
156     case PCDM_RS_AlreadyRetrieved:
157       ErrorMessage (Message_Msg("TObj_Appl_RAlreadyRetrieved") << aPath);
158       break;
159     case PCDM_RS_AlreadyRetrievedAndModified:
160       ErrorMessage (Message_Msg("TObj_Appl_RAlreadyRetrievedAndModified") << aPath);
161       break;
162     case PCDM_RS_NoDriver:
163       ErrorMessage (Message_Msg("TObj_Appl_RNoDriver") << aPath);
164       break;
165     case PCDM_RS_UnknownFileDriver:
166       ErrorMessage (Message_Msg("TObj_Appl_RNoDriver") << aPath);
167       break;
168     case PCDM_RS_OpenError:
169       ErrorMessage (Message_Msg("TObj_Appl_ROpenError") << aPath);
170       break;
171     case PCDM_RS_NoVersion:
172       ErrorMessage (Message_Msg("TObj_Appl_RNoVersion") << aPath);
173       break;
174     case PCDM_RS_NoModel:
175       ErrorMessage (Message_Msg("TObj_Appl_RNoModel") << aPath);
176       break;
177     case PCDM_RS_NoDocument:
178       ErrorMessage (Message_Msg("TObj_Appl_RNoDocument") << aPath);
179       break;
180     case PCDM_RS_FormatFailure:
181       ErrorMessage (Message_Msg("TObj_Appl_RFormatFailure") << aPath);
182       break;
183     case PCDM_RS_TypeNotFoundInSchema:
184       ErrorMessage (Message_Msg("TObj_Appl_RTypeNotFound") << aPath);
185       break;
186     case PCDM_RS_UnrecognizedFileFormat:
187       ErrorMessage (Message_Msg("TObj_Appl_RBadFileFormat") << aPath);
188       break;
189     case PCDM_RS_MakeFailure:
190       ErrorMessage (Message_Msg("TObj_Appl_RMakeFailure") << aPath);
191       break;
192     case PCDM_RS_PermissionDenied:
193       ErrorMessage (Message_Msg("TObj_Appl_RPermissionDenied") << aPath);
194       break;
195     case PCDM_RS_DriverFailure:
196       ErrorMessage (Message_Msg("TObj_Appl_RDriverFailure") << aPath);
197       break;
198     case PCDM_RS_ReaderException:
199       ErrorMessage (Message_Msg("TObj_Appl_RException") << aPath);
200       break;
201     default:
202       ErrorMessage (Message_Msg("TObj_Appl_RUnknownFail") << aPath);
203       break;
204     }
205   }
206
207   // Release free memory
208   Standard::Purge();
209   return myIsError ? Standard_False : Standard_True;
210 }
211
212 //=======================================================================
213 //function : CreateNewDocument
214 //purpose  : 
215 //=======================================================================
216
217 Standard_Boolean TObj_Application::CreateNewDocument
218                         (Handle(TDocStd_Document)&         theDoc,
219                          const TCollection_ExtendedString& theFormat)
220 {
221   myIsError = Standard_False;
222
223   // Create the Document
224   NewDocument (theFormat, theDoc);
225
226   return myIsError ? Standard_False : Standard_True;
227 }
228
229 //=======================================================================
230 //function : ErrorMessage
231 //purpose  : 
232 //=======================================================================
233
234 void TObj_Application::ErrorMessage (const TCollection_ExtendedString &theMsg,
235                                          const Message_Gravity theLevel)
236 {
237   myMessenger->Send ( theMsg, theLevel );
238 }
239