1 // Created on: 1999-06-30
2 // Created by: Denis PASCAL
3 // Copyright (c) 1999-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <TDocStd_Application.ixx>
19 #include <CDF_Session.hxx>
20 #include <CDF_DirectoryIterator.hxx>
21 #include <CDF_Directory.hxx>
23 #include <Standard_ErrorHandler.hxx>
24 #include <Standard_NotImplemented.hxx>
25 #include <Standard_DomainError.hxx>
26 #include <Plugin_Failure.hxx>
27 #include <TDF_Label.hxx>
28 #include <CDF_Store.hxx>
29 #include <TDocStd_PathParser.hxx>
31 #include <CDM_MessageDriver.hxx>
33 // The improvement concerns returning value of the methods Open(), Save() and SaveAs():
36 // TDocStd_Owner attribute have pointer of closed TDocStd_Document
40 #include <TDocStd_Owner.hxx>
43 //=======================================================================
44 //function : TDocStd_Application
46 //=======================================================================
48 TDocStd_Application::TDocStd_Application()
49 : myIsDriverLoaded (Standard_True)
51 Handle(CDF_Session) S;
52 if (!CDF_Session::Exists()) S = new CDF_Session();
53 else S = CDF_Session::CurrentSession();
54 S->SetCurrentApplication(this);
60 catch (Plugin_Failure)
62 myIsDriverLoaded = Standard_False;
67 //=======================================================================
68 //function : IsDriverLoaded
70 //=======================================================================
71 Standard_Boolean TDocStd_Application::IsDriverLoaded() const
73 return myIsDriverLoaded;
76 //=======================================================================
77 //function : Resources
79 //=======================================================================
81 Handle(Resource_Manager) TDocStd_Application::Resources() {
82 if (myResources.IsNull()) {
83 myResources = new Resource_Manager(ResourcesName());
89 //=======================================================================
90 //function : NbDocuments
92 //=======================================================================
94 Standard_Integer TDocStd_Application::NbDocuments() const
96 if (!CDF_Session::Exists())
97 Standard_DomainError::Raise("TDocStd_Application::NbDocuments");
98 Handle(CDF_Session) S = CDF_Session::CurrentSession();
99 return S->Directory()->Length();
102 //=======================================================================
103 //function : GetDocument
105 //=======================================================================
107 void TDocStd_Application::GetDocument(const Standard_Integer index,Handle(TDocStd_Document)& aDoc) const
109 if (!CDF_Session::Exists())
110 Standard_DomainError::Raise("TDocStd_Application::NbDocuments");
111 Handle(CDF_Session) S = CDF_Session::CurrentSession();
112 CDF_DirectoryIterator it (S->Directory());
113 Standard_Integer current = 0;
114 for (;it.MoreDocument();it.NextDocument()) {
116 if (index == current) {
117 Handle(TDocStd_Document) D =
118 Handle(TDocStd_Document)::DownCast(it.Document());
125 //=======================================================================
126 //function : NewDocument
128 //=======================================================================
130 void TDocStd_Application::NewDocument(const TCollection_ExtendedString& format,Handle(TDocStd_Document)& aDoc)
132 Handle(TDocStd_Document) D = new TDocStd_Document(format);
134 CDF_Application::Open(D); // add the document in the session
138 //=======================================================================
139 //function : InitDocument
140 //purpose : do nothing
141 //=======================================================================
143 void TDocStd_Application::InitDocument(const Handle(TDocStd_Document)& /*aDoc*/) const
148 //=======================================================================
151 //=======================================================================
153 void TDocStd_Application::Close(const Handle(TDocStd_Document)& aDoc)
160 Handle(TDocStd_Owner) Owner;
161 if (aDoc->Main().Root().FindAttribute(TDocStd_Owner::GetID(),Owner)) {
162 Handle(TDocStd_Document) emptyDoc;
163 Owner->SetDocument(emptyDoc);
166 CDF_Application::Close(aDoc);
170 //=======================================================================
171 //function : IsInSession
173 //=======================================================================
175 Standard_Integer TDocStd_Application::IsInSession (const TCollection_ExtendedString& path) const
177 TCollection_ExtendedString unifiedPath(path);
178 unifiedPath.ChangeAll('/', '|');
179 unifiedPath.ChangeAll('\\', '|');
181 Standard_Integer nbdoc = NbDocuments();
182 Handle(TDocStd_Document) D;
183 for (Standard_Integer i = 1; i <= nbdoc; i++)
188 TCollection_ExtendedString unifiedDocPath(D->GetPath());
189 unifiedDocPath.ChangeAll('/', '|');
190 unifiedDocPath.ChangeAll('\\', '|');
192 if (unifiedPath == unifiedDocPath)
199 //=======================================================================
202 //=======================================================================
204 PCDM_ReaderStatus TDocStd_Application::Open(const TCollection_ExtendedString& path,Handle(TDocStd_Document)& aDoc) {
205 PCDM_ReaderStatus status = PCDM_RS_DriverFailure;
206 TDocStd_PathParser tool (path);
207 TCollection_ExtendedString directory = tool.Trek();
208 TCollection_ExtendedString file = tool.Name();
210 file+=tool.Extension();
212 status = CanRetrieve(directory,file);
213 if (status != PCDM_RS_OK) return status;
217 Handle(TDocStd_Document) D =
218 Handle(TDocStd_Document)::DownCast(Retrieve(directory,file));
219 CDF_Application::Open(D);
222 catch (Standard_Failure) {
223 // status = GetRetrieveStatus();
224 Handle(Standard_Failure) F = Standard_Failure::Caught();
225 if (!F.IsNull() && !MessageDriver().IsNull()) {
226 // Standard_SStream aMsg;
227 // aMsg << Standard_Failure::Caught() << endl;
228 // cout << "TDocStd_Application::Open(): " << aMsg.rdbuf()->str() << endl;
229 TCollection_ExtendedString aString (F->GetMessageString());
230 MessageDriver()->Write(aString.ToExtString());
233 status = GetRetrieveStatus();
235 cout<<"TDocStd_Application::Open(): The status = "<<status<<endl;
240 //=======================================================================
243 //=======================================================================
245 PCDM_StoreStatus TDocStd_Application::SaveAs(const Handle(TDocStd_Document)& D,const TCollection_ExtendedString& path) {
246 TDocStd_PathParser tool (path);
247 TCollection_ExtendedString directory = tool.Trek();
248 TCollection_ExtendedString file = tool.Name();
250 file+=tool.Extension();
252 CDF_Store storer (D);
254 if (!storer.SetFolder(directory))
256 TCollection_ExtendedString aMsg ("TDocStd_Application::SaveAs() - folder ");
258 aMsg += " does not exist";
259 if(!MessageDriver().IsNull())
260 MessageDriver()->Write(aMsg.ToExtString());
261 return storer.StoreStatus(); //CDF_SS_Failure;
264 storer.SetName (file);
269 catch (Standard_Failure) {
270 Handle(Standard_Failure) F = Standard_Failure::Caught();
271 if (!F.IsNull() && !MessageDriver().IsNull()) {
272 TCollection_ExtendedString aString (F->GetMessageString());
273 MessageDriver()->Write(aString.ToExtString());
276 if(storer.StoreStatus() == PCDM_SS_OK)
280 cout<<"TDocStd_Application::SaveAs(): The status = "<<storer.StoreStatus()<<endl;
282 return storer.StoreStatus();
286 //=======================================================================
289 //=======================================================================
291 PCDM_StoreStatus TDocStd_Application::Save (const Handle(TDocStd_Document)& D) {
293 PCDM_StoreStatus status = PCDM_SS_OK;
296 CDF_Store storer (D);
301 catch (Standard_Failure) {
302 Handle(Standard_Failure) F = Standard_Failure::Caught();
303 if (!F.IsNull() && !MessageDriver().IsNull()) {
304 TCollection_ExtendedString aString (F->GetMessageString());
305 MessageDriver()->Write(aString.ToExtString());
308 if(storer.StoreStatus() == PCDM_SS_OK)
311 status = storer.StoreStatus();
314 if(!MessageDriver().IsNull()) {
315 TCollection_ExtendedString aMsg("Document has not been saved yet");
316 MessageDriver()->Write(aMsg.ToExtString());
318 status = PCDM_SS_Failure;
322 cout<<"TDocStd_Application::Save(): The status = "<<status<<endl;
328 //=======================================================================
329 //function : SetViewer
331 //=======================================================================
333 // void TDocStd_Application::SetViewer(const Handle(TDocStd_Document)& D,
334 // const Handle(V3d_Viewer)& viewer)
336 // TPrsStd_AISViewer::New (D->Main(),viewer);
341 //=======================================================================
342 //function : SetViewer
344 //=======================================================================
346 // void TDocStd_Application::SetViewer(const Handle(TDocStd_Document)& D,
347 // const Handle(AIS_InteractiveContext)& IC)
349 // TPrsStd_AISViewer::New (D->Main(),IC);
354 //=======================================================================
357 //=======================================================================
359 PCDM_StoreStatus TDocStd_Application::SaveAs(const Handle(TDocStd_Document)& D,
360 const TCollection_ExtendedString& path,
361 TCollection_ExtendedString& theStatusMessage)
363 TDocStd_PathParser tool (path);
364 PCDM_StoreStatus aStatus = PCDM_SS_Failure;
365 TCollection_ExtendedString directory = tool.Trek();
366 TCollection_ExtendedString file = tool.Name();
368 file+=tool.Extension();
370 CDF_Store storer (D);
371 if (storer.SetFolder(directory)) {
372 storer.SetName (file);
377 catch (Standard_Failure) {
378 Handle(Standard_Failure) F = Standard_Failure::Caught();
379 if (!F.IsNull() && !MessageDriver().IsNull()) {
380 TCollection_ExtendedString aString (F->GetMessageString());
381 MessageDriver()->Write(aString.ToExtString());
384 if(storer.StoreStatus() == PCDM_SS_OK)
386 theStatusMessage = storer.AssociatedStatusText();
387 aStatus = storer.StoreStatus();
390 TCollection_ExtendedString("TDocStd_Application::SaveAs"
391 ": No such directory ") + directory;
392 aStatus = PCDM_SS_Failure;
397 //=======================================================================
400 //=======================================================================
402 PCDM_StoreStatus TDocStd_Application::Save (const Handle(TDocStd_Document)& D,
403 TCollection_ExtendedString& theStatusMessage)
405 PCDM_StoreStatus status = PCDM_SS_OK;
407 CDF_Store storer (D);
412 catch (Standard_Failure) {
413 Handle(Standard_Failure) F = Standard_Failure::Caught();
414 if (!F.IsNull() && !MessageDriver().IsNull()) {
415 TCollection_ExtendedString aString (F->GetMessageString());
416 MessageDriver()->Write(aString.ToExtString());
419 if(storer.StoreStatus() == PCDM_SS_OK)
421 status = storer.StoreStatus();
422 theStatusMessage = storer.AssociatedStatusText();
424 theStatusMessage = "TDocStd_Application::the document has not been saved yet";
425 status = PCDM_SS_Failure;
431 //=======================================================================
432 //function : OnOpenTransaction
434 //=======================================================================
436 void TDocStd_Application::OnOpenTransaction (const Handle(TDocStd_Document)&)
438 // nothing to do on this level
441 //=======================================================================
442 //function : OnAbortTransaction
444 //=======================================================================
446 void TDocStd_Application::OnAbortTransaction (const Handle(TDocStd_Document)&)
448 // nothing to do on this level
451 //=======================================================================
452 //function : OnCommitTransaction
454 //=======================================================================
456 void TDocStd_Application::OnCommitTransaction (const Handle(TDocStd_Document)&)
458 // nothing to do on this level