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