0031668: Visualization - WebGL sample doesn't work on Emscripten 1.39
[occt.git] / src / DDocStd / DDocStd_ApplicationCommands.cxx
index c0a9f06..955338a 100644 (file)
@@ -32,7 +32,9 @@
 #include <TDF_Tool.hxx> 
 
 #include <OSD_Path.hxx>
+#include <OSD_OpenFile.hxx>
 #include <TDocStd_PathParser.hxx>
+#include <XmlLDrivers.hxx>
 
 #include <AIS_InteractiveContext.hxx>
 #include <TPrsStd_AISViewer.hxx>
@@ -55,8 +57,7 @@ static Standard_Integer DDocStd_ListDocuments (Draw_Interpretor& di,
                                               const char** /*a*/)
 {  
   if (nb == 1) {
-    Handle(TDocStd_Application) A;
-    if (!DDocStd::Find(A)) return 1;
+    Handle(TDocStd_Application) A = DDocStd::GetApplication();
     Handle(TDocStd_Document) D;
     Standard_Integer nbdoc = A->NbDocuments();
     for (Standard_Integer i = 1; i <= nbdoc; i++) {
@@ -65,8 +66,8 @@ static Standard_Integer DDocStd_ListDocuments (Draw_Interpretor& di,
       if (D->IsSaved()) {
        TCollection_AsciiString GetNameAsciiString(D->GetName().ToExtString(),'?');
        TCollection_AsciiString GetPathAsciiString(D->GetPath().ToExtString(),'?');
-       //cout << " name : " << D->GetName();
-       //cout << " path : " << D->GetPath();
+       //std::cout << " name : " << D->GetName();
+       //std::cout << " path : " << D->GetPath();
        di << " name : " << GetNameAsciiString.ToCString();
        di << " path : " << GetPathAsciiString.ToCString();
       }
@@ -75,7 +76,7 @@ static Standard_Integer DDocStd_ListDocuments (Draw_Interpretor& di,
     }
     return 0;
   }
-  di << "DDocStd_ListDocuments : Error" << "\n";
+  di << "DDocStd_ListDocuments : Error\n";
   return 1; 
 }
 
@@ -96,27 +97,26 @@ static Standard_Integer DDocStd_NewDocument (Draw_Interpretor& di,
       D = new TDocStd_Document("dummy");  
       DD = new DDocStd_DrawDocument(D);  
       Draw::Set(a[1],DD);       
-      di << "document (not handled by application)  " << a[1] << " created" << "\n";  
+      di << "document (not handled by application)  " << a[1] << " created\n";  
       DDocStd::ReturnLabel(di,D->Main()); 
     }    
-    else di << a[1] << " is already a document" << "\n";
+    else di << a[1] << " is already a document\n";
     return 0;
   }
   if (nb == 3) {   
     if (!DDocStd::GetDocument(a[1],D,Standard_False)) {  
-      Handle(TDocStd_Application) A;
-      if (!DDocStd::Find(A)) return 1;
+      Handle(TDocStd_Application) A = DDocStd::GetApplication();
       A->NewDocument(a[2],D);  
       DD = new DDocStd_DrawDocument(D);  
       TDataStd_Name::Set(D->GetData()->Root(),a[1]);  
       Draw::Set(a[1],DD);    
-      di << "document " << a[1] << " created" << "\n";    
+      di << "document " << a[1] << " created\n";    
       DDocStd::ReturnLabel(di,D->Main()); 
     }
-    else di << a[1] << " is already a document" << "\n";
+    else di << a[1] << " is already a document\n";
     return 0;
   }   
-  di << "DDocStd_NewDocument : Error" << "\n";
+  di << "DDocStd_NewDocument : Error\n";
   return 1;
 }
 
@@ -129,17 +129,39 @@ static Standard_Integer DDocStd_Open (Draw_Interpretor& di,
                                      Standard_Integer nb,
                                      const char** a)
 {   
-  if (nb == 3) {
+  if (nb >= 3) {
     TCollection_ExtendedString path (a[1]); 
-    Handle(TDocStd_Application) A;
-    if (!DDocStd::Find(A)) return 1;
+    Handle(TDocStd_Application) A = DDocStd::GetApplication();
     Handle(TDocStd_Document) D;
     Standard_Integer insession = A->IsInSession(path);
     if (insession > 0) {  
-      di <<"document " << insession << "  is already in session" << "\n";
+      di <<"document " << insession << "  is already in session\n";
       return 0;
     }
-    PCDM_ReaderStatus theStatus = A->Open(path,D);
+    PCDM_ReaderStatus theStatus;
+
+    Standard_Boolean anUseStream = Standard_False;
+    for ( Standard_Integer i = 3; i < nb; i++ )
+    {
+      if (!strcmp (a[i], "-stream"))
+      {
+        di << "standard SEEKABLE stream is used\n";
+        anUseStream = Standard_True;
+        break;
+      }
+    }
+
+    if (anUseStream)
+    {
+      std::ifstream aFileStream;
+      OSD_OpenStream (aFileStream, path, std::ios::in | std::ios::binary);
+
+      theStatus = A->Open (aFileStream, D);
+    }
+    else
+    {
+      theStatus = A->Open(path,D);
+    }
     if (theStatus == PCDM_RS_OK && !D.IsNull()) {
       Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(D);
       TDataStd_Name::Set(D->GetData()->Root(),a[2]);
@@ -149,32 +171,32 @@ static Standard_Integer DDocStd_Open (Draw_Interpretor& di,
       switch ( theStatus ) {
       case PCDM_RS_AlreadyRetrieved: 
       case PCDM_RS_AlreadyRetrievedAndModified: {
-       di << " already retrieved " << "\n" ;  
+       di << " already retrieved \n" ;  
        break;
       }
       case PCDM_RS_NoDriver: {
-       di << " could not retrieve , no Driver to make it " <<"\n" ;
+       di << " could not retrieve , no Driver to make it \n" ;
        break ;
       }
       case PCDM_RS_UnknownDocument:
       case PCDM_RS_NoModel: {
-       di << " could not retrieve , Unknown Document or No Model " <<"\n";
+       di << " could not retrieve , Unknown Document or No Model \n";
        break ; 
       }
       case PCDM_RS_TypeNotFoundInSchema:
       case PCDM_RS_UnrecognizedFileFormat: {
-       di << " could not retrieve , Type not found or Unrecognized File Format" <<"\n";
+       di << " could not retrieve , Type not found or Unrecognized File Format\n";
        break ;
       }
       case PCDM_RS_PermissionDenied: {
-       di << " could not retrieve , permission denied " << "\n" ;  
+       di << " could not retrieve , permission denied \n" ;  
        break;
       }
       default:
-       di << " could not retrieve " << "\n" ;  
+       di << " could not retrieve \n" ;  
        break;
       }
-      di << "DDocStd_Open : Error" << "\n";
+      di << "DDocStd_Open : Error\n";
     }  
   }
   return 1;
@@ -192,16 +214,15 @@ static Standard_Integer DDocStd_Save (Draw_Interpretor& di,
   if (nb == 2) {
     Handle(TDocStd_Document) D;    
     if (!DDocStd::GetDocument(a[1],D)) return 1;
-    Handle(TDocStd_Application) A;
-    if (!DDocStd::Find(A)) return 1;  
+    Handle(TDocStd_Application) A = DDocStd::GetApplication();
     if (!D->IsSaved()) {
-      di << "this document has never been saved" << "\n";
+      di << "this document has never been saved\n";
       return 0;
     }
     A->Save(D);
     return 0; 
   }
-  di << "DDocStd_Save : Error" << "\n";
+  di << "DDocStd_Save : Error\n";
   return 1;
 }
 
@@ -211,51 +232,75 @@ static Standard_Integer DDocStd_Save (Draw_Interpretor& di,
 //=======================================================================
 
 static Standard_Integer DDocStd_SaveAs (Draw_Interpretor& di,
-                                       Standard_Integer nb,
-                                       const char** a)
-{  
-  if (nb == 3) {
+                                        Standard_Integer nb,
+                                        const char** a)
+{
+  if (nb >= 3) {
     Handle(TDocStd_Document) D;    
     if (!DDocStd::GetDocument(a[1],D)) return 1;  
     TCollection_ExtendedString path (a[2]); 
-    Handle(TDocStd_Application) A;
-    if (!DDocStd::Find(A)) return 1;
-    PCDM_StoreStatus theStatus = A->SaveAs(D,path);
+    Handle(TDocStd_Application) A = DDocStd::GetApplication();
+    PCDM_StoreStatus theStatus;
+
+    Standard_Boolean anUseStream(Standard_False), isSaveEmptyLabels(Standard_False);
+    for ( Standard_Integer i = 3; i < nb; i++ )
+    {
+      if (!strcmp (a[i], "-stream"))
+      {
+        di << "standard SEEKABLE stream is used\n";
+        anUseStream = Standard_True;
+        break;
+      } else {
+        isSaveEmptyLabels =  ((atoi (a[3])) != 0);
+        D->SetEmptyLabelsSavingMode(isSaveEmptyLabels);
+      }
+    }
+    if (anUseStream)
+    {
+      std::ofstream aFileStream;
+      OSD_OpenStream (aFileStream, path, std::ios::out | std::ios::binary);
+      theStatus = A->SaveAs (D, aFileStream);
+    }
+    else
+    {
+      theStatus = A->SaveAs(D,path);
+    }
+
     if (theStatus != PCDM_SS_OK ) {
       switch ( theStatus ) {
-        case PCDM_SS_DriverFailure: {
-          di << "Error saving document: Could not store , no driver found to make it" << "\n";
-          break ;
-        }
-        case PCDM_SS_WriteFailure: {
-          di << "Error saving document: Write access failure" << "\n";
-          break;
-        }
-        case PCDM_SS_Failure: {
-          di << "Error saving document: Write failure" << "\n" ;
-          break;
-        }
-        case PCDM_SS_Doc_IsNull: {
-          di << "Error saving document: No document to save" << "\n";
-          break ;
-        }
-        case PCDM_SS_No_Obj: {
-          di << "Error saving document: No objects written" << "\n";
-          break;
-        }
-        case PCDM_SS_Info_Section_Error: {
-          di << "Error saving document: Write info section failure" << "\n" ;
-          break;
-        }
-        default:
-          break;
+      case PCDM_SS_DriverFailure: {
+        di << "Error saving document: Could not store , no driver found to make it\n";
+        break ;
+                                  }
+      case PCDM_SS_WriteFailure: {
+        di << "Error saving document: Write access failure\n";
+        break;
+                                 }
+      case PCDM_SS_Failure: {
+        di << "Error saving document: Write failure\n" ;
+        break;
+                            }
+      case PCDM_SS_Doc_IsNull: {
+        di << "Error saving document: No document to save\n";
+        break ;
+                               }
+      case PCDM_SS_No_Obj: {
+        di << "Error saving document: No objects written\n";
+        break;
+                           }
+      case PCDM_SS_Info_Section_Error: {
+        di << "Error saving document: Write info section failure\n" ;
+        break;
+                                       }
+      default:
+        break;
       }
       return 1;
     } else {
       return 0; 
     }
   }
-  di << "DDocStd_SaveAs : Error not enough argument" << "\n";
+  di << "DDocStd_SaveAs : Error not enough argument\n";
   return 1;
 }
 
@@ -287,23 +332,26 @@ static Standard_Integer DDocStd_Close (Draw_Interpretor& /*theDI*/,
    && !aDocViewer->GetInteractiveContext().IsNull())
   {
     Handle(V3d_Viewer) aViewer = aDocViewer->GetInteractiveContext()->CurrentViewer();
-    for (aViewer->InitDefinedViews(); aViewer->MoreDefinedViews(); aViewer->NextDefinedViews())
+    V3d_ListOfView aViews;
+    for (V3d_ListOfViewIterator aViewIter (aDocViewer->GetInteractiveContext()->CurrentViewer()->DefinedViewIterator()); aViewIter.More(); aViewIter.Next())
     {
-      Handle(V3d_View) aView = aViewer->DefinedView();
+      aViews.Append (aViewIter.Value());
+    }
+    for (V3d_ListOfViewIterator aViewIter (aViews); aViewIter.More(); aViewIter.Next())
+    {
+      Handle(V3d_View) aView = aViewIter.Value();
       ViewerTest::RemoveView (aView);
     }
   }
 
-  Handle(TDocStd_Application) aDocApp;
-  if (!DDocStd::Find (aDocApp))
-  {
-    return 1;
-  }
+  Handle(TDocStd_Application) aDocApp = DDocStd::GetApplication();
 
   aDocApp->Close (aDoc);
 
-  Handle(Draw_Drawable3D) aDrawable = Draw::Get (aDocName, Standard_False);
-  dout.RemoveDrawable (aDrawable);
+  if (Handle(Draw_Drawable3D) aDrawable = Draw::GetExisting (aDocName))
+  {
+    dout.RemoveDrawable (aDrawable);
+  }
   Draw::Set (theArgVec[1], Handle(Draw_Drawable3D)());
   return 0;
 }
@@ -318,12 +366,11 @@ static Standard_Integer DDocStd_IsInSession (Draw_Interpretor& di,
                                             const char** a)
 {   
   if (nb == 2) {   
-    Handle(TDocStd_Application) A;
-    if (!DDocStd::Find(A)) return 1;
+    Handle(TDocStd_Application) A = DDocStd::GetApplication();
     di << A->IsInSession(a[1]);
     return 0;
   }  
-  di << "DDocStd_IsInSession : Error" << "\n";
+  di << "DDocStd_IsInSession : Error\n";
   return 1;
 }
 
@@ -348,7 +395,7 @@ static Standard_Integer DDocStd_OSDPath (Draw_Interpretor& di,
     di << "Extension : " << path.Extension().ToCString() << "\n";
     return 0;
   }
-  di << "DDocStd_OSDPath : Error" << "\n";
+  di << "DDocStd_OSDPath : Error\n";
   return 1;
 }
 
@@ -364,10 +411,10 @@ static Standard_Integer DDocStd_Path (Draw_Interpretor& di,
 {   
   if (nb == 2) { 
     TDocStd_PathParser path (a[1]);
-    //cout << "Trek      : " << path.Trek() << endl;  
-    //cout << "Name      : " << path.Name() << endl; 
-    //cout << "Extension : " << path.Extension() << endl;
-    //cout << "Path      : " << path.Path() << endl;
+    //std::cout << "Trek      : " << path.Trek() << std::endl;  
+    //std::cout << "Name      : " << path.Name() << std::endl; 
+    //std::cout << "Extension : " << path.Extension() << std::endl;
+    //std::cout << "Path      : " << path.Path() << std::endl;
     TCollection_AsciiString TrekAsciiString(path.Trek().ToExtString(),'?');
     TCollection_AsciiString NameAsciiString(path.Name().ToExtString(),'?');
     TCollection_AsciiString ExtensionAsciiString(path.Extension().ToExtString(),'?');
@@ -378,7 +425,7 @@ static Standard_Integer DDocStd_Path (Draw_Interpretor& di,
     di << "Path      : " << PathAsciiString.ToCString() << "\n";
     return 0;
   }
-  di << "DDocStd_Path : Error" << "\n";
+  di << "DDocStd_Path : Error\n";
   return 1;
 }
 
@@ -394,13 +441,12 @@ static Standard_Integer DDocStd_AddComment (Draw_Interpretor& di,
     Handle(TDocStd_Document) D;    
     if (!DDocStd::GetDocument(a[1],D)) return 1;  
     TCollection_ExtendedString comment (a[2]); 
-//    Handle(TDocStd_Application) A;
-//    if (!DDocStd::Find(A)) return 1;
+//    Handle(TDocStd_Application) A = DDocStd::GetApplication();
 //    A->AddComment(D,comment);
     D->AddComment(comment);
     return 0; 
   }
-  di << "DDocStd_AddComment : Wrong arguments number" << "\n";
+  di << "DDocStd_AddComment : Wrong arguments number\n";
   return 1;
 }
 
@@ -421,14 +467,50 @@ static Standard_Integer DDocStd_PrintComments (Draw_Interpretor& di,
 
     for (int i = 1; i <= comments.Length(); i++)
     {
-      //cout << comments(i) << endl;
+      //std::cout << comments(i) << std::endl;
       TCollection_AsciiString commentAsciiString(comments(i).ToExtString(),'?');
       di << commentAsciiString.ToCString() << "\n";
     }
 
     return 0; 
   }
-  di << "DDocStd_PrintComments : Wrong arguments number" << "\n";
+  di << "DDocStd_PrintComments : Wrong arguments number\n";
+  return 1;
+}
+
+//=======================================================================
+//function : SetStorageVerison
+//purpose  : 
+//=======================================================================
+static Standard_Integer DDocStd_SetStorageVersion (Draw_Interpretor& ,
+                                                   Standard_Integer nb,
+                                                   const char** a)
+{  
+  if (nb == 3)
+  {
+    Handle(TDocStd_Document) D;
+    if (!DDocStd::GetDocument(a[1], D)) return 1;
+    const int version = atoi(a[2]);
+    D->ChangeStorageFormatVersion(version);
+    return 0;
+  }
+  return 1;
+}
+
+//=======================================================================
+//function : GetStorageVerison
+//purpose  : 
+//=======================================================================
+static Standard_Integer DDocStd_GetStorageVersion (Draw_Interpretor& di,
+                                                   Standard_Integer nb,
+                                                   const char** a)
+{ 
+  if (nb == 2) {
+    Handle(TDocStd_Document) D;
+    if (!DDocStd::GetDocument(a[1], D)) return 1;
+    di << D->StorageFormatVersion() << "\n";
+    return 0;
+  }
   return 1;
 }
 
@@ -456,11 +538,11 @@ void DDocStd::ApplicationCommands(Draw_Interpretor& theCommands)
                  __FILE__, DDocStd_NewDocument, g);  
 
   theCommands.Add("Open",
-                 "Open path docname",
+                 "Open path docname [-stream]",
                  __FILE__, DDocStd_Open, g);   
 
   theCommands.Add("SaveAs",
-                 "SaveAs DOC path",
+                 "SaveAs DOC path [saveEmptyLabels: 0|1] [-stream]",
                  __FILE__, DDocStd_SaveAs, g);  
 
   theCommands.Add("Save",
@@ -490,4 +572,11 @@ void DDocStd::ApplicationCommands(Draw_Interpretor& theCommands)
   theCommands.Add("PrintComments",
                  "PrintComments Doc",
                  __FILE__, DDocStd_PrintComments, g);
+
+  theCommands.Add("GetStorageVersion",
+                 "GetStorageVersion Doc",
+                 __FILE__, DDocStd_GetStorageVersion, g);
+  theCommands.Add("SetStorageVersion",
+                 "SetStorageVersion Doc Version",
+                 __FILE__, DDocStd_SetStorageVersion, g);
 }