0028691: Storage of Ocaf documents in XML file format in old document version
[occt.git] / src / DDocStd / DDocStd_ApplicationCommands.cxx
CommitLineData
b311480e 1// Created on: 2000-03-01
2// Created by: Denis PASCAL
973c2be1 3// Copyright (c) 2000-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.
7fd59977 15
16#include <DDocStd.hxx>
17#include <Draw.hxx>
18#include <Draw_Interpretor.hxx>
19#include <Draw_Viewer.hxx>
20#include <DDocStd_DrawDocument.hxx>
21#include <TDocStd_Application.hxx>
22#include <TDocStd_Document.hxx>
23#include <TDataStd_Name.hxx>
24#include <Draw.hxx>
25#include <Standard_GUID.hxx>
26#include <Standard_ExtString.hxx>
27#include <TCollection_AsciiString.hxx>
28#include <TCollection_ExtendedString.hxx>
29#include <TDF.hxx>
30#include <TDF_Data.hxx>
31#include <TDF_ChildIterator.hxx>
32#include <TDF_Tool.hxx>
33
34#include <OSD_Path.hxx>
4ff92abe 35#include <OSD_OpenFile.hxx>
7fd59977 36#include <TDocStd_PathParser.hxx>
c2f5b821 37#include <XmlLDrivers.hxx>
7fd59977 38
0e93d9e5 39#include <AIS_InteractiveContext.hxx>
40#include <TPrsStd_AISViewer.hxx>
41#include <ViewerTest.hxx>
42#include <V3d_Viewer.hxx>
43
44#ifndef _WIN32
7fd59977 45extern Draw_Viewer dout;
46#else
47Standard_IMPORT Draw_Viewer dout;
48#endif
49
7fd59977 50//=======================================================================
51//function : ListDocuments
52//purpose :
53//=======================================================================
54
55static Standard_Integer DDocStd_ListDocuments (Draw_Interpretor& di,
56 Standard_Integer nb,
57 const char** /*a*/)
58{
59 if (nb == 1) {
6fe96f84 60 Handle(TDocStd_Application) A = DDocStd::GetApplication();
7fd59977 61 Handle(TDocStd_Document) D;
62 Standard_Integer nbdoc = A->NbDocuments();
63 for (Standard_Integer i = 1; i <= nbdoc; i++) {
64 A->GetDocument(i,D);
65 di <<"document " << i;
66 if (D->IsSaved()) {
67 TCollection_AsciiString GetNameAsciiString(D->GetName().ToExtString(),'?');
68 TCollection_AsciiString GetPathAsciiString(D->GetPath().ToExtString(),'?');
69 //cout << " name : " << D->GetName();
70 //cout << " path : " << D->GetPath();
71 di << " name : " << GetNameAsciiString.ToCString();
72 di << " path : " << GetPathAsciiString.ToCString();
73 }
74 else di << " not saved";
75 di << "\n";
76 }
77 return 0;
78 }
586db386 79 di << "DDocStd_ListDocuments : Error\n";
7fd59977 80 return 1;
81}
82
83
84//=======================================================================
85//function : NewDocument
86//purpose :
87//=======================================================================
88
89static Standard_Integer DDocStd_NewDocument (Draw_Interpretor& di,
90 Standard_Integer nb,
91 const char** a)
92{
93 Handle(TDocStd_Document) D;
94 Handle(DDocStd_DrawDocument) DD;
95 if (nb == 2) {
96 if (!DDocStd::GetDocument(a[1],D,Standard_False)) {
97 D = new TDocStd_Document("dummy");
98 DD = new DDocStd_DrawDocument(D);
99 Draw::Set(a[1],DD);
586db386 100 di << "document (not handled by application) " << a[1] << " created\n";
7fd59977 101 DDocStd::ReturnLabel(di,D->Main());
102 }
586db386 103 else di << a[1] << " is already a document\n";
7fd59977 104 return 0;
105 }
106 if (nb == 3) {
107 if (!DDocStd::GetDocument(a[1],D,Standard_False)) {
6fe96f84 108 Handle(TDocStd_Application) A = DDocStd::GetApplication();
7fd59977 109 A->NewDocument(a[2],D);
110 DD = new DDocStd_DrawDocument(D);
111 TDataStd_Name::Set(D->GetData()->Root(),a[1]);
112 Draw::Set(a[1],DD);
586db386 113 di << "document " << a[1] << " created\n";
7fd59977 114 DDocStd::ReturnLabel(di,D->Main());
115 }
586db386 116 else di << a[1] << " is already a document\n";
7fd59977 117 return 0;
118 }
586db386 119 di << "DDocStd_NewDocument : Error\n";
7fd59977 120 return 1;
121}
122
123//=======================================================================
7fd59977 124//function : Open
125//purpose :
126//=======================================================================
127
128static Standard_Integer DDocStd_Open (Draw_Interpretor& di,
129 Standard_Integer nb,
130 const char** a)
131{
4ff92abe 132 if (nb >= 3) {
7fd59977 133 TCollection_ExtendedString path (a[1]);
6fe96f84 134 Handle(TDocStd_Application) A = DDocStd::GetApplication();
7fd59977 135 Handle(TDocStd_Document) D;
136 Standard_Integer insession = A->IsInSession(path);
137 if (insession > 0) {
586db386 138 di <<"document " << insession << " is already in session\n";
7fd59977 139 return 0;
140 }
4ff92abe 141 PCDM_ReaderStatus theStatus;
142
143 Standard_Boolean anUseStream = Standard_False;
144 for ( Standard_Integer i = 3; i < nb; i++ )
145 {
146 if (!strcmp (a[i], "-stream"))
147 {
148 di << "standard SEEKABLE stream is used\n";
149 anUseStream = Standard_True;
150 break;
151 }
152 }
153
154 if (anUseStream)
155 {
156 std::ifstream aFileStream;
157 OSD_OpenStream (aFileStream, path, std::ios::in | std::ios::binary);
158
159 theStatus = A->Open (aFileStream, D);
160 }
161 else
162 {
163 theStatus = A->Open(path,D);
164 }
15e8b082 165 if (theStatus == PCDM_RS_OK && !D.IsNull()) {
7fd59977 166 Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(D);
167 TDataStd_Name::Set(D->GetData()->Root(),a[2]);
168 Draw::Set(a[2],DD);
169 return 0;
170 } else {
171 switch ( theStatus ) {
15e8b082
M
172 case PCDM_RS_AlreadyRetrieved:
173 case PCDM_RS_AlreadyRetrievedAndModified: {
586db386 174 di << " already retrieved \n" ;
7fd59977 175 break;
176 }
15e8b082 177 case PCDM_RS_NoDriver: {
586db386 178 di << " could not retrieve , no Driver to make it \n" ;
7fd59977 179 break ;
180 }
15e8b082
M
181 case PCDM_RS_UnknownDocument:
182 case PCDM_RS_NoModel: {
586db386 183 di << " could not retrieve , Unknown Document or No Model \n";
7fd59977 184 break ;
185 }
15e8b082
M
186 case PCDM_RS_TypeNotFoundInSchema:
187 case PCDM_RS_UnrecognizedFileFormat: {
586db386 188 di << " could not retrieve , Type not found or Unrecognized File Format\n";
7fd59977 189 break ;
190 }
15e8b082 191 case PCDM_RS_PermissionDenied: {
586db386 192 di << " could not retrieve , permission denied \n" ;
7fd59977 193 break;
194 }
15e8b082 195 default:
586db386 196 di << " could not retrieve \n" ;
15e8b082 197 break;
7fd59977 198 }
586db386 199 di << "DDocStd_Open : Error\n";
7fd59977 200 }
201 }
202 return 1;
203}
204
205//=======================================================================
206//function : Save
207//purpose :
208//=======================================================================
209
210static Standard_Integer DDocStd_Save (Draw_Interpretor& di,
211 Standard_Integer nb,
212 const char** a)
213{
214 if (nb == 2) {
215 Handle(TDocStd_Document) D;
216 if (!DDocStd::GetDocument(a[1],D)) return 1;
6fe96f84 217 Handle(TDocStd_Application) A = DDocStd::GetApplication();
7fd59977 218 if (!D->IsSaved()) {
586db386 219 di << "this document has never been saved\n";
7fd59977 220 return 0;
221 }
222 A->Save(D);
223 return 0;
224 }
586db386 225 di << "DDocStd_Save : Error\n";
7fd59977 226 return 1;
227}
228
229//=======================================================================
230//function : SaveAs
231//purpose :
232//=======================================================================
233
234static Standard_Integer DDocStd_SaveAs (Draw_Interpretor& di,
235 Standard_Integer nb,
236 const char** a)
237{
4ff92abe 238 if (nb >= 3) {
7fd59977 239 Handle(TDocStd_Document) D;
240 if (!DDocStd::GetDocument(a[1],D)) return 1;
241 TCollection_ExtendedString path (a[2]);
6fe96f84 242 Handle(TDocStd_Application) A = DDocStd::GetApplication();
4ff92abe 243 PCDM_StoreStatus theStatus;
244
245 Standard_Boolean anUseStream = Standard_False;
246 for ( Standard_Integer i = 3; i < nb; i++ )
247 {
248 if (!strcmp (a[i], "-stream"))
249 {
250 di << "standard SEEKABLE stream is used\n";
251 anUseStream = Standard_True;
252 break;
253 }
254 }
255
256 if (anUseStream)
257 {
258 std::ofstream aFileStream;
259 OSD_OpenStream (aFileStream, path, std::ios::out | std::ios::binary);
260 theStatus = A->SaveAs (D, aFileStream);
261 }
262 else
263 {
264 theStatus = A->SaveAs(D,path);
265 }
266
15e8b082 267 if (theStatus != PCDM_SS_OK ) {
7fd59977 268 switch ( theStatus ) {
566f8441 269 case PCDM_SS_DriverFailure: {
586db386 270 di << "Error saving document: Could not store , no driver found to make it\n";
566f8441 271 break ;
272 }
273 case PCDM_SS_WriteFailure: {
586db386 274 di << "Error saving document: Write access failure\n";
566f8441 275 break;
276 }
277 case PCDM_SS_Failure: {
586db386 278 di << "Error saving document: Write failure\n" ;
566f8441 279 break;
280 }
281 case PCDM_SS_Doc_IsNull: {
586db386 282 di << "Error saving document: No document to save\n";
566f8441 283 break ;
284 }
285 case PCDM_SS_No_Obj: {
586db386 286 di << "Error saving document: No objects written\n";
566f8441 287 break;
288 }
289 case PCDM_SS_Info_Section_Error: {
586db386 290 di << "Error saving document: Write info section failure\n" ;
566f8441 291 break;
292 }
293 default:
294 break;
7fd59977 295 }
296 return 1;
297 } else {
298 return 0;
299 }
300 }
586db386 301 di << "DDocStd_SaveAs : Error not enough argument\n";
7fd59977 302 return 1;
303}
304
305//=======================================================================
306//function : Close
307//purpose :
308//=======================================================================
309
0e93d9e5 310static Standard_Integer DDocStd_Close (Draw_Interpretor& /*theDI*/,
311 Standard_Integer theArgNb,
312 const char** theArgVec)
7fd59977 313{
0e93d9e5 314 if (theArgNb != 2)
315 {
316 std::cout << "DDocStd_Close : Error\n";
317 return 1;
318 }
24de79c3 319
0e93d9e5 320 Handle(TDocStd_Document) aDoc;
321 Standard_CString aDocName = theArgVec[1];
322 if (!DDocStd::GetDocument (aDocName, aDoc))
323 {
324 return 1;
7fd59977 325 }
0e93d9e5 326
327 TDF_Label aRoot = aDoc->GetData()->Root();
328 Handle(TPrsStd_AISViewer) aDocViewer;
329 if (TPrsStd_AISViewer::Find (aRoot, aDocViewer)
330 && !aDocViewer->GetInteractiveContext().IsNull())
331 {
332 Handle(V3d_Viewer) aViewer = aDocViewer->GetInteractiveContext()->CurrentViewer();
6a24c6de 333 V3d_ListOfView aViews;
334 for (V3d_ListOfViewIterator aViewIter (aDocViewer->GetInteractiveContext()->CurrentViewer()->DefinedViewIterator()); aViewIter.More(); aViewIter.Next())
0e93d9e5 335 {
6a24c6de 336 aViews.Append (aViewIter.Value());
337 }
338 for (V3d_ListOfViewIterator aViewIter (aViews); aViewIter.More(); aViewIter.Next())
339 {
340 Handle(V3d_View) aView = aViewIter.Value();
0e93d9e5 341 ViewerTest::RemoveView (aView);
342 }
343 }
344
6fe96f84 345 Handle(TDocStd_Application) aDocApp = DDocStd::GetApplication();
0e93d9e5 346
347 aDocApp->Close (aDoc);
348
349 Handle(Draw_Drawable3D) aDrawable = Draw::Get (aDocName, Standard_False);
350 dout.RemoveDrawable (aDrawable);
351 Draw::Set (theArgVec[1], Handle(Draw_Drawable3D)());
352 return 0;
7fd59977 353}
354
355//=======================================================================
356//function : IsInSession
357//purpose :
358//=======================================================================
359
360static Standard_Integer DDocStd_IsInSession (Draw_Interpretor& di,
361 Standard_Integer nb,
362 const char** a)
363{
364 if (nb == 2) {
6fe96f84 365 Handle(TDocStd_Application) A = DDocStd::GetApplication();
7fd59977 366 di << A->IsInSession(a[1]);
367 return 0;
368 }
586db386 369 di << "DDocStd_IsInSession : Error\n";
7fd59977 370 return 1;
371}
372
373
374//=======================================================================
375//function : OSDPath
376//purpose :
377//=======================================================================
378
379static Standard_Integer DDocStd_OSDPath (Draw_Interpretor& di,
380 Standard_Integer nb,
381 const char** a)
382{
383 if (nb == 2) {
384 OSD_Path path (a[1]);
385 di << "Node : " << path.Node().ToCString() << "\n";
386 di << "UserName : " << path.UserName().ToCString() << "\n";
387 di << "Password : " << path.Password().ToCString() << "\n";
388 di << "Disk : " << path.Disk().ToCString() << "\n";
389 di << "Trek : " << path.Trek().ToCString() << "\n";
390 di << "Name : " << path.Name().ToCString() << "\n";
391 di << "Extension : " << path.Extension().ToCString() << "\n";
392 return 0;
393 }
586db386 394 di << "DDocStd_OSDPath : Error\n";
7fd59977 395 return 1;
396}
397
398
399//=======================================================================
400//function : Path
401//purpose :
402//=======================================================================
403
404static Standard_Integer DDocStd_Path (Draw_Interpretor& di,
405 Standard_Integer nb,
406 const char** a)
407{
408 if (nb == 2) {
409 TDocStd_PathParser path (a[1]);
410 //cout << "Trek : " << path.Trek() << endl;
411 //cout << "Name : " << path.Name() << endl;
412 //cout << "Extension : " << path.Extension() << endl;
413 //cout << "Path : " << path.Path() << endl;
414 TCollection_AsciiString TrekAsciiString(path.Trek().ToExtString(),'?');
415 TCollection_AsciiString NameAsciiString(path.Name().ToExtString(),'?');
416 TCollection_AsciiString ExtensionAsciiString(path.Extension().ToExtString(),'?');
417 TCollection_AsciiString PathAsciiString(path.Path().ToExtString(),'?');
418 di << "Trek : " << TrekAsciiString.ToCString() << "\n";
419 di << "Name : " << NameAsciiString.ToCString() << "\n";
420 di << "Extension : " << ExtensionAsciiString.ToCString() << "\n";
421 di << "Path : " << PathAsciiString.ToCString() << "\n";
422 return 0;
423 }
586db386 424 di << "DDocStd_Path : Error\n";
7fd59977 425 return 1;
426}
427
428//=======================================================================
429//function : AddComment
430//purpose :
431//=======================================================================
432static Standard_Integer DDocStd_AddComment (Draw_Interpretor& di,
433 Standard_Integer nb,
434 const char** a)
435{
436 if (nb == 3) {
437 Handle(TDocStd_Document) D;
438 if (!DDocStd::GetDocument(a[1],D)) return 1;
439 TCollection_ExtendedString comment (a[2]);
6fe96f84 440// Handle(TDocStd_Application) A = DDocStd::GetApplication();
7fd59977 441// A->AddComment(D,comment);
442 D->AddComment(comment);
443 return 0;
444 }
586db386 445 di << "DDocStd_AddComment : Wrong arguments number\n";
7fd59977 446 return 1;
447}
448
449//=======================================================================
450//function : PrintComments
451//purpose :
452//=======================================================================
453static Standard_Integer DDocStd_PrintComments (Draw_Interpretor& di,
454 Standard_Integer nb,
455 const char** a)
456{
457 if (nb == 2) {
458 Handle(TDocStd_Document) D;
459 if (!DDocStd::GetDocument(a[1],D)) return 1;
460
461 TColStd_SequenceOfExtendedString comments;
462 D->Comments(comments);
463
464 for (int i = 1; i <= comments.Length(); i++)
465 {
466 //cout << comments(i) << endl;
467 TCollection_AsciiString commentAsciiString(comments(i).ToExtString(),'?');
468 di << commentAsciiString.ToCString() << "\n";
469 }
470
471 return 0;
472 }
586db386 473 di << "DDocStd_PrintComments : Wrong arguments number\n";
7fd59977 474 return 1;
475}
476
477//=======================================================================
c2f5b821 478//function : SetStorageVerison
479//purpose :
480//=======================================================================
481static Standard_Integer DDocStd_SetStorageVersion (Draw_Interpretor& ,
482 Standard_Integer nb,
483 const char** a)
484{
485 if (nb == 2)
486 {
487 const int version = atoi(a[1]);
488 XmlLDrivers::SetStorageVersion(version);
489 return 0;
490 }
491 return 1;
492}
493
494//=======================================================================
495//function : GetStorageVerison
496//purpose :
497//=======================================================================
498static Standard_Integer DDocStd_GetStorageVersion (Draw_Interpretor& di,
499 Standard_Integer ,
500 const char** )
501{
502 di << XmlLDrivers::StorageVersion() << "\n" ;
503 return 0;
504}
505
506//=======================================================================
7fd59977 507//function : ApplicationCommands
508//purpose :
509//=======================================================================
510
511void DDocStd::ApplicationCommands(Draw_Interpretor& theCommands)
512{
513
514 static Standard_Boolean done = Standard_False;
515 if (done) return;
516 done = Standard_True;
517
518 const char* g = "DDocStd application commands";
519
520 // user application commands
7fd59977 521 theCommands.Add("ListDocuments",
522 "ListDocuments",
523 __FILE__, DDocStd_ListDocuments, g);
524
525 theCommands.Add("NewDocument",
526 "NewDocument docname format",
527 __FILE__, DDocStd_NewDocument, g);
528
7fd59977 529 theCommands.Add("Open",
4ff92abe 530 "Open path docname [-stream]",
7fd59977 531 __FILE__, DDocStd_Open, g);
532
533 theCommands.Add("SaveAs",
4ff92abe 534 "SaveAs DOC path [-stream]",
7fd59977 535 __FILE__, DDocStd_SaveAs, g);
536
537 theCommands.Add("Save",
538 "Save",
539 __FILE__, DDocStd_Save, g);
540
541 theCommands.Add("Close",
542 "Close DOC",
543 __FILE__, DDocStd_Close, g);
544
545 theCommands.Add("IsInSession",
546 "IsInSession path",
547 __FILE__, DDocStd_IsInSession, g);
548
549 theCommands.Add("OSDPath",
550 "OSDPath string",
551 __FILE__, DDocStd_OSDPath, g);
552
553 theCommands.Add("Path",
554 "Path string",
555 __FILE__, DDocStd_Path, g);
556
557 theCommands.Add("AddComment",
558 "AddComment Doc string",
559 __FILE__, DDocStd_AddComment, g);
560
561 theCommands.Add("PrintComments",
562 "PrintComments Doc",
563 __FILE__, DDocStd_PrintComments, g);
c2f5b821 564
565 theCommands.Add("GetStorageVersion",
566 "GetStorageVersion",
567 __FILE__, DDocStd_GetStorageVersion, g);
568 theCommands.Add("SetStorageVersion",
569 "SetStorageVersion Version",
570 __FILE__, DDocStd_SetStorageVersion, g);
7fd59977 571}