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.
18 #include <CDF_Directory.hxx>
19 #include <CDF_DirectoryIterator.hxx>
20 #include <CDF_Session.hxx>
21 #include <CDF_Store.hxx>
22 #include <CDM_MessageDriver.hxx>
23 #include <Plugin_Failure.hxx>
24 #include <Resource_Manager.hxx>
25 #include <Standard_DomainError.hxx>
26 #include <Standard_ErrorHandler.hxx>
27 #include <Standard_NoSuchObject.hxx>
28 #include <Standard_NotImplemented.hxx>
29 #include <Standard_Type.hxx>
30 #include <TCollection_ExtendedString.hxx>
31 #include <TDF_Label.hxx>
32 #include <TDocStd_Application.hxx>
33 #include <TDocStd_Document.hxx>
34 #include <TDocStd_Owner.hxx>
35 #include <TDocStd_PathParser.hxx>
37 IMPLEMENT_STANDARD_RTTIEXT(TDocStd_Application,CDF_Application)
39 // TDocStd_Owner attribute have pointer of closed TDocStd_Document
40 //=======================================================================
41 //function : TDocStd_Application
43 //=======================================================================
44 TDocStd_Application::TDocStd_Application()
45 : myIsDriverLoaded (Standard_True)
47 Handle(CDF_Session) S;
48 if (!CDF_Session::Exists()) S = new CDF_Session();
49 else S = CDF_Session::CurrentSession();
50 S->SetCurrentApplication(this);
56 catch (Plugin_Failure)
58 myIsDriverLoaded = Standard_False;
63 //=======================================================================
64 //function : IsDriverLoaded
66 //=======================================================================
67 Standard_Boolean TDocStd_Application::IsDriverLoaded() const
69 return myIsDriverLoaded;
72 //=======================================================================
73 //function : Resources
75 //=======================================================================
77 Handle(Resource_Manager) TDocStd_Application::Resources() {
78 if (myResources.IsNull()) {
79 myResources = new Resource_Manager(ResourcesName());
85 //=======================================================================
86 //function : NbDocuments
88 //=======================================================================
90 Standard_Integer TDocStd_Application::NbDocuments() const
92 if (!CDF_Session::Exists())
93 Standard_DomainError::Raise("TDocStd_Application::NbDocuments");
94 Handle(CDF_Session) S = CDF_Session::CurrentSession();
95 return S->Directory()->Length();
98 //=======================================================================
99 //function : GetDocument
101 //=======================================================================
103 void TDocStd_Application::GetDocument(const Standard_Integer index,Handle(TDocStd_Document)& aDoc) const
105 if (!CDF_Session::Exists())
106 Standard_DomainError::Raise("TDocStd_Application::NbDocuments");
107 Handle(CDF_Session) S = CDF_Session::CurrentSession();
108 CDF_DirectoryIterator it (S->Directory());
109 Standard_Integer current = 0;
110 for (;it.MoreDocument();it.NextDocument()) {
112 if (index == current) {
113 Handle(TDocStd_Document) D =
114 Handle(TDocStd_Document)::DownCast(it.Document());
121 //=======================================================================
122 //function : NewDocument
124 //=======================================================================
126 void TDocStd_Application::NewDocument(const TCollection_ExtendedString& format,Handle(TDocStd_Document)& aDoc)
128 Handle(TDocStd_Document) D = new TDocStd_Document(format);
130 CDF_Application::Open(D); // add the document in the session
134 //=======================================================================
135 //function : InitDocument
136 //purpose : do nothing
137 //=======================================================================
139 void TDocStd_Application::InitDocument(const Handle(TDocStd_Document)& /*aDoc*/) const
143 //=======================================================================
146 //=======================================================================
148 void TDocStd_Application::Close(const Handle(TDocStd_Document)& aDoc)
155 Handle(TDocStd_Owner) Owner;
156 if (aDoc->Main().Root().FindAttribute(TDocStd_Owner::GetID(),Owner)) {
157 Handle(TDocStd_Document) emptyDoc;
158 Owner->SetDocument(emptyDoc);
161 CDF_Application::Close(aDoc);
164 //=======================================================================
165 //function : IsInSession
167 //=======================================================================
169 Standard_Integer TDocStd_Application::IsInSession (const TCollection_ExtendedString& path) const
171 TCollection_ExtendedString unifiedPath(path);
172 unifiedPath.ChangeAll('/', '|');
173 unifiedPath.ChangeAll('\\', '|');
175 Standard_Integer nbdoc = NbDocuments();
176 Handle(TDocStd_Document) D;
177 for (Standard_Integer i = 1; i <= nbdoc; i++)
182 TCollection_ExtendedString unifiedDocPath(D->GetPath());
183 unifiedDocPath.ChangeAll('/', '|');
184 unifiedDocPath.ChangeAll('\\', '|');
186 if (unifiedPath == unifiedDocPath)
193 //=======================================================================
196 //=======================================================================
198 PCDM_ReaderStatus TDocStd_Application::Open(const TCollection_ExtendedString& path,Handle(TDocStd_Document)& aDoc) {
199 PCDM_ReaderStatus status = PCDM_RS_DriverFailure;
200 TDocStd_PathParser tool (path);
201 TCollection_ExtendedString directory = tool.Trek();
202 TCollection_ExtendedString file = tool.Name();
204 file+=tool.Extension();
205 status = CanRetrieve(directory,file);
206 if (status != PCDM_RS_OK) return status;
209 Handle(TDocStd_Document) D =
210 Handle(TDocStd_Document)::DownCast(Retrieve(directory,file));
211 CDF_Application::Open(D);
214 catch (Standard_Failure) {
215 // status = GetRetrieveStatus();
216 Handle(Standard_Failure) F = Standard_Failure::Caught();
217 if (!F.IsNull() && !MessageDriver().IsNull()) {
218 // Standard_SStream aMsg;
219 // aMsg << Standard_Failure::Caught() << endl;
220 // cout << "TDocStd_Application::Open(): " << aMsg.rdbuf()->str() << endl;
221 TCollection_ExtendedString aString (F->GetMessageString());
222 MessageDriver()->Write(aString.ToExtString());
225 status = GetRetrieveStatus();
227 cout<<"TDocStd_Application::Open(): The status = "<<status<<endl;
232 //=======================================================================
235 //=======================================================================
237 PCDM_StoreStatus TDocStd_Application::SaveAs(const Handle(TDocStd_Document)& D,const TCollection_ExtendedString& path) {
238 TDocStd_PathParser tool (path);
239 TCollection_ExtendedString directory = tool.Trek();
240 TCollection_ExtendedString file = tool.Name();
242 file+=tool.Extension();
244 CDF_Store storer (D);
245 if (!storer.SetFolder(directory))
247 TCollection_ExtendedString aMsg ("TDocStd_Application::SaveAs() - folder ");
249 aMsg += " does not exist";
250 if(!MessageDriver().IsNull())
251 MessageDriver()->Write(aMsg.ToExtString());
252 return storer.StoreStatus(); //CDF_SS_Failure;
254 storer.SetName (file);
259 catch (Standard_Failure) {
260 Handle(Standard_Failure) F = Standard_Failure::Caught();
261 if (!F.IsNull() && !MessageDriver().IsNull()) {
262 TCollection_ExtendedString aString (F->GetMessageString());
263 MessageDriver()->Write(aString.ToExtString());
266 if(storer.StoreStatus() == PCDM_SS_OK)
269 cout<<"TDocStd_Application::SaveAs(): The status = "<<storer.StoreStatus()<<endl;
271 return storer.StoreStatus();
274 //=======================================================================
277 //=======================================================================
279 PCDM_StoreStatus TDocStd_Application::Save (const Handle(TDocStd_Document)& D) {
280 PCDM_StoreStatus status = PCDM_SS_OK;
282 CDF_Store storer (D);
287 catch (Standard_Failure) {
288 Handle(Standard_Failure) F = Standard_Failure::Caught();
289 if (!F.IsNull() && !MessageDriver().IsNull()) {
290 TCollection_ExtendedString aString (F->GetMessageString());
291 MessageDriver()->Write(aString.ToExtString());
294 if(storer.StoreStatus() == PCDM_SS_OK)
296 status = storer.StoreStatus();
298 if(!MessageDriver().IsNull()) {
299 TCollection_ExtendedString aMsg("Document has not been saved yet");
300 MessageDriver()->Write(aMsg.ToExtString());
302 status = PCDM_SS_Failure;
305 cout<<"TDocStd_Application::Save(): The status = "<<status<<endl;
310 //=======================================================================
311 //function : SetViewer
313 //=======================================================================
315 // void TDocStd_Application::SetViewer(const Handle(TDocStd_Document)& D,
316 // const Handle(V3d_Viewer)& viewer)
318 // TPrsStd_AISViewer::New (D->Main(),viewer);
323 //=======================================================================
324 //function : SetViewer
326 //=======================================================================
328 // void TDocStd_Application::SetViewer(const Handle(TDocStd_Document)& D,
329 // const Handle(AIS_InteractiveContext)& IC)
331 // TPrsStd_AISViewer::New (D->Main(),IC);
336 //=======================================================================
339 //=======================================================================
341 PCDM_StoreStatus TDocStd_Application::SaveAs(const Handle(TDocStd_Document)& D,
342 const TCollection_ExtendedString& path,
343 TCollection_ExtendedString& theStatusMessage)
345 TDocStd_PathParser tool (path);
346 PCDM_StoreStatus aStatus = PCDM_SS_Failure;
347 TCollection_ExtendedString directory = tool.Trek();
348 TCollection_ExtendedString file = tool.Name();
350 file+=tool.Extension();
352 CDF_Store storer (D);
353 if (storer.SetFolder(directory)) {
354 storer.SetName (file);
359 catch (Standard_Failure) {
360 Handle(Standard_Failure) F = Standard_Failure::Caught();
361 if (!F.IsNull() && !MessageDriver().IsNull()) {
362 TCollection_ExtendedString aString (F->GetMessageString());
363 MessageDriver()->Write(aString.ToExtString());
366 if(storer.StoreStatus() == PCDM_SS_OK)
368 theStatusMessage = storer.AssociatedStatusText();
369 aStatus = storer.StoreStatus();
372 TCollection_ExtendedString("TDocStd_Application::SaveAs"
373 ": No such directory ") + directory;
374 aStatus = PCDM_SS_Failure;
379 //=======================================================================
382 //=======================================================================
384 PCDM_StoreStatus TDocStd_Application::Save (const Handle(TDocStd_Document)& D,
385 TCollection_ExtendedString& theStatusMessage)
387 PCDM_StoreStatus status = PCDM_SS_OK;
389 CDF_Store storer (D);
394 catch (Standard_Failure) {
395 Handle(Standard_Failure) F = Standard_Failure::Caught();
396 if (!F.IsNull() && !MessageDriver().IsNull()) {
397 TCollection_ExtendedString aString (F->GetMessageString());
398 MessageDriver()->Write(aString.ToExtString());
401 if(storer.StoreStatus() == PCDM_SS_OK)
403 status = storer.StoreStatus();
404 theStatusMessage = storer.AssociatedStatusText();
406 theStatusMessage = "TDocStd_Application::the document has not been saved yet";
407 status = PCDM_SS_Failure;
413 //=======================================================================
414 //function : OnOpenTransaction
416 //=======================================================================
418 void TDocStd_Application::OnOpenTransaction (const Handle(TDocStd_Document)&)
420 // nothing to do on this level
423 //=======================================================================
424 //function : OnAbortTransaction
426 //=======================================================================
428 void TDocStd_Application::OnAbortTransaction (const Handle(TDocStd_Document)&)
430 // nothing to do on this level
433 //=======================================================================
434 //function : OnCommitTransaction
436 //=======================================================================
438 void TDocStd_Application::OnCommitTransaction (const Handle(TDocStd_Document)&)
440 // nothing to do on this level