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