Commit | Line | Data |
---|---|---|
b311480e | 1 | // Created on: 1999-06-30 |
2 | // Created by: Denis PASCAL | |
3 | // Copyright (c) 1999-1999 Matra Datavision | |
973c2be1 | 4 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e | 5 | // |
973c2be1 | 6 | // This file is part of Open CASCADE Technology software library. |
b311480e | 7 | // |
d5f74e42 | 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 | |
973c2be1 | 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. | |
b311480e | 13 | // |
973c2be1 | 14 | // Alternatively, this file may be used under the terms of Open CASCADE |
15 | // commercial license or contractual agreement. | |
7fd59977 | 16 | |
17 | #include <TDocStd_Application.ixx> | |
18 | ||
19 | #include <CDF_Session.hxx> | |
20 | #include <CDF_DirectoryIterator.hxx> | |
21 | #include <CDF_Directory.hxx> | |
22 | ||
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> | |
30 | ||
31 | #include <CDM_MessageDriver.hxx> | |
32 | ||
7fd59977 | 33 | // TDocStd_Owner attribute have pointer of closed TDocStd_Document |
7fd59977 | 34 | |
7fd59977 | 35 | #include <TDocStd_Owner.hxx> |
ec357c5c | 36 | #include <TDocStd_Document.hxx> |
7fd59977 | 37 | |
38 | //======================================================================= | |
39 | //function : TDocStd_Application | |
40 | //purpose : | |
41 | //======================================================================= | |
42 | ||
43 | TDocStd_Application::TDocStd_Application() | |
44 | : myIsDriverLoaded (Standard_True) | |
45 | { | |
46 | Handle(CDF_Session) S; | |
47 | if (!CDF_Session::Exists()) S = new CDF_Session(); | |
48 | else S = CDF_Session::CurrentSession(); | |
49 | S->SetCurrentApplication(this); | |
50 | try | |
51 | { | |
52 | OCC_CATCH_SIGNALS | |
53 | S->LoadDriver(); | |
54 | } | |
55 | catch (Plugin_Failure) | |
56 | { | |
57 | myIsDriverLoaded = Standard_False; | |
58 | } | |
59 | } | |
60 | ||
61 | ||
62 | //======================================================================= | |
63 | //function : IsDriverLoaded | |
64 | //purpose : | |
65 | //======================================================================= | |
66 | Standard_Boolean TDocStd_Application::IsDriverLoaded() const | |
67 | { | |
68 | return myIsDriverLoaded; | |
69 | } | |
70 | ||
71 | //======================================================================= | |
72 | //function : Resources | |
73 | //purpose : | |
74 | //======================================================================= | |
75 | ||
76 | Handle(Resource_Manager) TDocStd_Application::Resources() { | |
77 | if (myResources.IsNull()) { | |
78 | myResources = new Resource_Manager(ResourcesName()); | |
79 | } | |
80 | return myResources; | |
81 | } | |
82 | ||
83 | ||
84 | //======================================================================= | |
85 | //function : NbDocuments | |
86 | //purpose : | |
87 | //======================================================================= | |
88 | ||
89 | Standard_Integer TDocStd_Application::NbDocuments() const | |
90 | { | |
91 | if (!CDF_Session::Exists()) | |
92 | Standard_DomainError::Raise("TDocStd_Application::NbDocuments"); | |
93 | Handle(CDF_Session) S = CDF_Session::CurrentSession(); | |
94 | return S->Directory()->Length(); | |
95 | } | |
96 | ||
97 | //======================================================================= | |
98 | //function : GetDocument | |
99 | //purpose : | |
100 | //======================================================================= | |
101 | ||
102 | void TDocStd_Application::GetDocument(const Standard_Integer index,Handle(TDocStd_Document)& aDoc) const | |
103 | { | |
104 | if (!CDF_Session::Exists()) | |
105 | Standard_DomainError::Raise("TDocStd_Application::NbDocuments"); | |
106 | Handle(CDF_Session) S = CDF_Session::CurrentSession(); | |
107 | CDF_DirectoryIterator it (S->Directory()); | |
108 | Standard_Integer current = 0; | |
109 | for (;it.MoreDocument();it.NextDocument()) { | |
110 | current++; | |
111 | if (index == current) { | |
112 | Handle(TDocStd_Document) D = | |
113 | Handle(TDocStd_Document)::DownCast(it.Document()); | |
114 | aDoc = D; | |
115 | return; | |
116 | } | |
117 | } | |
118 | } | |
119 | ||
120 | //======================================================================= | |
121 | //function : NewDocument | |
122 | //purpose : | |
123 | //======================================================================= | |
124 | ||
125 | void TDocStd_Application::NewDocument(const TCollection_ExtendedString& format,Handle(TDocStd_Document)& aDoc) | |
126 | { | |
127 | Handle(TDocStd_Document) D = new TDocStd_Document(format); | |
128 | InitDocument (D); | |
129 | CDF_Application::Open(D); // add the document in the session | |
130 | aDoc = D; | |
131 | } | |
132 | ||
133 | //======================================================================= | |
134 | //function : InitDocument | |
135 | //purpose : do nothing | |
136 | //======================================================================= | |
137 | ||
138 | void TDocStd_Application::InitDocument(const Handle(TDocStd_Document)& /*aDoc*/) const | |
139 | { | |
140 | } | |
141 | ||
7fd59977 | 142 | //======================================================================= |
143 | //function : Close | |
144 | //purpose : | |
145 | //======================================================================= | |
146 | ||
147 | void TDocStd_Application::Close(const Handle(TDocStd_Document)& aDoc) | |
148 | { | |
b6c0b841 RL |
149 | if (aDoc.IsNull()) |
150 | { | |
151 | return; | |
152 | } | |
153 | ||
7fd59977 | 154 | Handle(TDocStd_Owner) Owner; |
155 | if (aDoc->Main().Root().FindAttribute(TDocStd_Owner::GetID(),Owner)) { | |
156 | Handle(TDocStd_Document) emptyDoc; | |
157 | Owner->SetDocument(emptyDoc); | |
158 | } | |
1c9cffdb | 159 | aDoc->BeforeClose(); |
7fd59977 | 160 | CDF_Application::Close(aDoc); |
161 | } | |
7fd59977 | 162 | |
163 | //======================================================================= | |
164 | //function : IsInSession | |
165 | //purpose : | |
166 | //======================================================================= | |
167 | ||
168 | Standard_Integer TDocStd_Application::IsInSession (const TCollection_ExtendedString& path) const | |
169 | { | |
bcb0fd43 V |
170 | TCollection_ExtendedString unifiedPath(path); |
171 | unifiedPath.ChangeAll('/', '|'); | |
172 | unifiedPath.ChangeAll('\\', '|'); | |
173 | ||
174 | Standard_Integer nbdoc = NbDocuments(); | |
175 | Handle(TDocStd_Document) D; | |
176 | for (Standard_Integer i = 1; i <= nbdoc; i++) | |
177 | { | |
178 | GetDocument(i,D); | |
179 | if (D->IsSaved()) | |
180 | { | |
181 | TCollection_ExtendedString unifiedDocPath(D->GetPath()); | |
182 | unifiedDocPath.ChangeAll('/', '|'); | |
183 | unifiedDocPath.ChangeAll('\\', '|'); | |
184 | ||
185 | if (unifiedPath == unifiedDocPath) | |
186 | return i; | |
187 | } | |
7fd59977 | 188 | } |
bcb0fd43 | 189 | return 0; |
7fd59977 | 190 | } |
191 | ||
192 | //======================================================================= | |
193 | //function : Open | |
194 | //purpose : | |
195 | //======================================================================= | |
196 | ||
15e8b082 M |
197 | PCDM_ReaderStatus TDocStd_Application::Open(const TCollection_ExtendedString& path,Handle(TDocStd_Document)& aDoc) { |
198 | PCDM_ReaderStatus status = PCDM_RS_DriverFailure; | |
7fd59977 | 199 | TDocStd_PathParser tool (path); |
200 | TCollection_ExtendedString directory = tool.Trek(); | |
201 | TCollection_ExtendedString file = tool.Name(); | |
202 | file+="."; | |
203 | file+=tool.Extension(); | |
7fd59977 | 204 | status = CanRetrieve(directory,file); |
15e8b082 | 205 | if (status != PCDM_RS_OK) return status; |
7fd59977 | 206 | try { |
207 | OCC_CATCH_SIGNALS | |
208 | Handle(TDocStd_Document) D = | |
209 | Handle(TDocStd_Document)::DownCast(Retrieve(directory,file)); | |
210 | CDF_Application::Open(D); | |
211 | aDoc = D; | |
212 | } | |
213 | catch (Standard_Failure) { | |
214 | // status = GetRetrieveStatus(); | |
215 | Handle(Standard_Failure) F = Standard_Failure::Caught(); | |
216 | if (!F.IsNull() && !MessageDriver().IsNull()) { | |
217 | // Standard_SStream aMsg; | |
218 | // aMsg << Standard_Failure::Caught() << endl; | |
219 | // cout << "TDocStd_Application::Open(): " << aMsg.rdbuf()->str() << endl; | |
220 | TCollection_ExtendedString aString (F->GetMessageString()); | |
221 | MessageDriver()->Write(aString.ToExtString()); | |
222 | } | |
223 | } | |
224 | status = GetRetrieveStatus(); | |
0797d9d3 | 225 | #ifdef OCCT_DEBUG |
7fd59977 | 226 | cout<<"TDocStd_Application::Open(): The status = "<<status<<endl; |
227 | #endif | |
228 | return status; | |
229 | } | |
230 | ||
231 | //======================================================================= | |
232 | //function : SaveAs | |
233 | //purpose : | |
234 | //======================================================================= | |
235 | ||
15e8b082 | 236 | PCDM_StoreStatus TDocStd_Application::SaveAs(const Handle(TDocStd_Document)& D,const TCollection_ExtendedString& path) { |
7fd59977 | 237 | TDocStd_PathParser tool (path); |
238 | TCollection_ExtendedString directory = tool.Trek(); | |
239 | TCollection_ExtendedString file = tool.Name(); | |
240 | file+="."; | |
241 | file+=tool.Extension(); | |
242 | D->Open(this); | |
243 | CDF_Store storer (D); | |
7fd59977 | 244 | if (!storer.SetFolder(directory)) |
245 | { | |
246 | TCollection_ExtendedString aMsg ("TDocStd_Application::SaveAs() - folder "); | |
247 | aMsg += directory; | |
248 | aMsg += " does not exist"; | |
249 | if(!MessageDriver().IsNull()) | |
250 | MessageDriver()->Write(aMsg.ToExtString()); | |
251 | return storer.StoreStatus(); //CDF_SS_Failure; | |
252 | } | |
7fd59977 | 253 | storer.SetName (file); |
254 | try { | |
255 | OCC_CATCH_SIGNALS | |
256 | storer.Realize(); | |
257 | } | |
258 | catch (Standard_Failure) { | |
259 | Handle(Standard_Failure) F = Standard_Failure::Caught(); | |
260 | if (!F.IsNull() && !MessageDriver().IsNull()) { | |
261 | TCollection_ExtendedString aString (F->GetMessageString()); | |
262 | MessageDriver()->Write(aString.ToExtString()); | |
263 | } | |
264 | } | |
15e8b082 | 265 | if(storer.StoreStatus() == PCDM_SS_OK) |
7fd59977 | 266 | D->SetSaved(); |
0797d9d3 | 267 | #ifdef OCCT_DEBUG |
7fd59977 | 268 | cout<<"TDocStd_Application::SaveAs(): The status = "<<storer.StoreStatus()<<endl; |
269 | #endif | |
270 | return storer.StoreStatus(); | |
7fd59977 | 271 | } |
272 | ||
273 | //======================================================================= | |
274 | //function : Save | |
275 | //purpose : | |
276 | //======================================================================= | |
277 | ||
15e8b082 | 278 | PCDM_StoreStatus TDocStd_Application::Save (const Handle(TDocStd_Document)& D) { |
15e8b082 | 279 | PCDM_StoreStatus status = PCDM_SS_OK; |
7fd59977 | 280 | if (D->IsSaved()) { |
281 | CDF_Store storer (D); | |
282 | try{ | |
283 | OCC_CATCH_SIGNALS | |
284 | storer.Realize(); | |
285 | } | |
286 | catch (Standard_Failure) { | |
287 | Handle(Standard_Failure) F = Standard_Failure::Caught(); | |
288 | if (!F.IsNull() && !MessageDriver().IsNull()) { | |
289 | TCollection_ExtendedString aString (F->GetMessageString()); | |
290 | MessageDriver()->Write(aString.ToExtString()); | |
291 | } | |
292 | } | |
15e8b082 | 293 | if(storer.StoreStatus() == PCDM_SS_OK) |
7fd59977 | 294 | D->SetSaved(); |
7fd59977 | 295 | status = storer.StoreStatus(); |
7fd59977 | 296 | } else { |
7fd59977 | 297 | if(!MessageDriver().IsNull()) { |
15e8b082 | 298 | TCollection_ExtendedString aMsg("Document has not been saved yet"); |
7fd59977 | 299 | MessageDriver()->Write(aMsg.ToExtString()); |
300 | } | |
15e8b082 | 301 | status = PCDM_SS_Failure; |
7fd59977 | 302 | } |
0797d9d3 | 303 | #ifdef OCCT_DEBUG |
7fd59977 | 304 | cout<<"TDocStd_Application::Save(): The status = "<<status<<endl; |
305 | #endif | |
306 | return status; | |
7fd59977 | 307 | } |
308 | ||
309 | //======================================================================= | |
310 | //function : SetViewer | |
311 | //purpose : | |
312 | //======================================================================= | |
313 | ||
314 | // void TDocStd_Application::SetViewer(const Handle(TDocStd_Document)& D, | |
315 | // const Handle(V3d_Viewer)& viewer) | |
316 | // { | |
317 | // TPrsStd_AISViewer::New (D->Main(),viewer); | |
318 | // InitViewer(D); | |
319 | // } | |
320 | ||
321 | ||
322 | //======================================================================= | |
323 | //function : SetViewer | |
324 | //purpose : | |
325 | //======================================================================= | |
326 | ||
327 | // void TDocStd_Application::SetViewer(const Handle(TDocStd_Document)& D, | |
328 | // const Handle(AIS_InteractiveContext)& IC) | |
329 | // { | |
330 | // TPrsStd_AISViewer::New (D->Main(),IC); | |
331 | // InitViewer(D); | |
332 | // } | |
333 | ||
334 | ||
335 | //======================================================================= | |
336 | //function : SaveAs | |
337 | //purpose : | |
338 | //======================================================================= | |
339 | ||
15e8b082 | 340 | PCDM_StoreStatus TDocStd_Application::SaveAs(const Handle(TDocStd_Document)& D, |
7fd59977 | 341 | const TCollection_ExtendedString& path, |
342 | TCollection_ExtendedString& theStatusMessage) | |
343 | { | |
344 | TDocStd_PathParser tool (path); | |
15e8b082 | 345 | PCDM_StoreStatus aStatus = PCDM_SS_Failure; |
7fd59977 | 346 | TCollection_ExtendedString directory = tool.Trek(); |
347 | TCollection_ExtendedString file = tool.Name(); | |
348 | file+="."; | |
349 | file+=tool.Extension(); | |
350 | D->Open(this); | |
351 | CDF_Store storer (D); | |
352 | if (storer.SetFolder(directory)) { | |
353 | storer.SetName (file); | |
354 | try { | |
355 | OCC_CATCH_SIGNALS | |
356 | storer.Realize(); | |
357 | } | |
358 | catch (Standard_Failure) { | |
359 | Handle(Standard_Failure) F = Standard_Failure::Caught(); | |
360 | if (!F.IsNull() && !MessageDriver().IsNull()) { | |
361 | TCollection_ExtendedString aString (F->GetMessageString()); | |
362 | MessageDriver()->Write(aString.ToExtString()); | |
363 | } | |
364 | } | |
15e8b082 | 365 | if(storer.StoreStatus() == PCDM_SS_OK) |
7fd59977 | 366 | D->SetSaved(); |
367 | theStatusMessage = storer.AssociatedStatusText(); | |
368 | aStatus = storer.StoreStatus(); | |
369 | } else { | |
370 | theStatusMessage = | |
371 | TCollection_ExtendedString("TDocStd_Application::SaveAs" | |
372 | ": No such directory ") + directory; | |
15e8b082 | 373 | aStatus = PCDM_SS_Failure; |
7fd59977 | 374 | } |
375 | return aStatus; | |
376 | } | |
377 | ||
378 | //======================================================================= | |
379 | //function : Save | |
380 | //purpose : | |
381 | //======================================================================= | |
382 | ||
15e8b082 | 383 | PCDM_StoreStatus TDocStd_Application::Save (const Handle(TDocStd_Document)& D, |
7fd59977 | 384 | TCollection_ExtendedString& theStatusMessage) |
385 | { | |
15e8b082 | 386 | PCDM_StoreStatus status = PCDM_SS_OK; |
7fd59977 | 387 | if (D->IsSaved()) { |
388 | CDF_Store storer (D); | |
389 | try { | |
390 | OCC_CATCH_SIGNALS | |
391 | storer.Realize(); | |
392 | } | |
393 | catch (Standard_Failure) { | |
394 | Handle(Standard_Failure) F = Standard_Failure::Caught(); | |
395 | if (!F.IsNull() && !MessageDriver().IsNull()) { | |
396 | TCollection_ExtendedString aString (F->GetMessageString()); | |
397 | MessageDriver()->Write(aString.ToExtString()); | |
398 | } | |
399 | } | |
15e8b082 | 400 | if(storer.StoreStatus() == PCDM_SS_OK) |
7fd59977 | 401 | D->SetSaved(); |
402 | status = storer.StoreStatus(); | |
403 | theStatusMessage = storer.AssociatedStatusText(); | |
404 | } else { | |
15e8b082 M |
405 | theStatusMessage = "TDocStd_Application::the document has not been saved yet"; |
406 | status = PCDM_SS_Failure; | |
7fd59977 | 407 | } |
408 | return status; | |
409 | } | |
410 | ||
411 | ||
412 | //======================================================================= | |
413 | //function : OnOpenTransaction | |
414 | //purpose : | |
415 | //======================================================================= | |
416 | ||
417 | void TDocStd_Application::OnOpenTransaction (const Handle(TDocStd_Document)&) | |
418 | { | |
419 | // nothing to do on this level | |
420 | } | |
421 | ||
422 | //======================================================================= | |
423 | //function : OnAbortTransaction | |
424 | //purpose : | |
425 | //======================================================================= | |
426 | ||
427 | void TDocStd_Application::OnAbortTransaction (const Handle(TDocStd_Document)&) | |
428 | { | |
429 | // nothing to do on this level | |
430 | } | |
431 | ||
432 | //======================================================================= | |
433 | //function : OnCommitTransaction | |
434 | //purpose : | |
435 | //======================================================================= | |
436 | ||
437 | void TDocStd_Application::OnCommitTransaction (const Handle(TDocStd_Document)&) | |
438 | { | |
439 | // nothing to do on this level | |
440 | } |