]> OCCT Git - occt.git/commitdiff
0032350: Data Exchange - STEPControl_Writer.writeStream(std::ostream)
authormahaidong <13501108114@163.com>
Fri, 14 May 2021 06:35:28 +0000 (14:35 +0800)
committersmoskvin <smoskvin@opencascade.com>
Mon, 31 Oct 2022 16:18:08 +0000 (19:18 +0300)
STEPCAFControl_Writer::WriteStream(), STEPControl_Writer::WriteStream() - added interface for writing into stream.
STEPCAFControl_Reader::ReadStream() - added stream reading method (similar to STEPControl_Reader::ReadStream()).

Added option -stream to commands ReadStep, WriteStep, and testwritestep.

src/STEPCAFControl/STEPCAFControl_Reader.cxx
src/STEPCAFControl/STEPCAFControl_Reader.hxx
src/STEPCAFControl/STEPCAFControl_Writer.cxx
src/STEPCAFControl/STEPCAFControl_Writer.hxx
src/STEPControl/STEPControl_Writer.cxx
src/STEPControl/STEPControl_Writer.hxx
src/XDEDRAW/XDEDRAW_Common.cxx
src/XSDRAWSTEP/XSDRAWSTEP.cxx
tests/bugs/step/bug32350 [new file with mode: 0644]

index 04fc8af7d34a6e4440af5928fc4ec0ff7be2110f..6c2ccfa8b49df6394a90f056ad4e87dadb1e0297 100644 (file)
@@ -336,14 +336,22 @@ TCollection_ExtendedString STEPCAFControl_Reader::convertName (const TCollection
 
 //=======================================================================
 //function : ReadFile
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-IFSelect_ReturnStatus STEPCAFControl_Reader::ReadFile(const Standard_CString filename)
+IFSelect_ReturnStatus STEPCAFControl_Reader::ReadFile (const Standard_CString theFileName)
 {
-  return myReader.ReadFile(filename);
+  return myReader.ReadFile (theFileName);
 }
 
+//=======================================================================
+//function : ReadStream
+//purpose  :
+//=======================================================================
+IFSelect_ReturnStatus STEPCAFControl_Reader::ReadStream (const Standard_CString theName,
+                                                         std::istream& theIStream)
+{
+  return myReader.ReadStream (theName, theIStream);
+}
 
 //=======================================================================
 //function : NbRootsForTransfer
index a32197d168ace85a322794cdede7d70a346b5019..1b1ac0c55a78ab05246aabc6c2ffe9aac3cdd795 100644 (file)
@@ -73,11 +73,20 @@ public:
   //! Clears the internal data structures and attaches to a new session
   //! Clears the session if it was not yet set for STEP
   Standard_EXPORT void Init (const Handle(XSControl_WorkSession)& WS, const Standard_Boolean scratch = Standard_True);
-  
+
   //! Loads a file and returns the read status
-  //! Provided for use like single-file reader
-  Standard_EXPORT IFSelect_ReturnStatus ReadFile (const Standard_CString filename);
-  
+  //! Provided for use like single-file reader.
+  //! @param theFileName [in] file to open
+  //! @return read status
+  Standard_EXPORT IFSelect_ReturnStatus ReadFile (const Standard_CString theFileName);
+
+  //! Loads a file from stream and returns the read status.
+  //! @param theName [in] auxiliary stream name
+  //! @param theIStream [in] stream to read from
+  //! @return read status
+  Standard_EXPORT IFSelect_ReturnStatus ReadStream (const Standard_CString theName,
+                                                    std::istream& theIStream);
+
   //! Returns number of roots recognized for transfer
   //! Shortcut for Reader().NbRootsForTransfer()
   Standard_EXPORT Standard_Integer NbRootsForTransfer();
index b5a617282ab2eb5dbf6896de6f6fd9663ef29990..521d043df42b7943f6ce1ce1499c6432239c169d 100644 (file)
@@ -302,34 +302,51 @@ void STEPCAFControl_Writer::Init (const Handle(XSControl_WorkSession)& WS,
 //function : Write
 //purpose  :
 //=======================================================================
-
-IFSelect_ReturnStatus STEPCAFControl_Writer::Write (const Standard_CString filename)
+IFSelect_ReturnStatus STEPCAFControl_Writer::Write (const Standard_CString theFileName)
 {
-  IFSelect_ReturnStatus status = myWriter.Write ( filename );
+  IFSelect_ReturnStatus aStatus = myWriter.Write (theFileName);
+  if (aStatus != IFSelect_RetDone)
+  {
+    return aStatus;
+  }
 
   // get directory name of the main file
-  OSD_Path mainfile ( filename );
-  mainfile.SetName ( "" );
-  mainfile.SetExtension ( "" );
-  TCollection_AsciiString dpath;
-  mainfile.SystemName ( dpath );
+  TCollection_AsciiString aDirPath;
+  {
+    OSD_Path aMainFile (theFileName);
+    aMainFile.SetName ("");
+    aMainFile.SetExtension ("");
+    aMainFile.SystemName (aDirPath);
+  }
 
-  NCollection_DataMap<TCollection_AsciiString, Handle(STEPCAFControl_ExternFile)>::Iterator it(myFiles);
-  for ( ; it.More(); it.Next() ) {
-    Handle(STEPCAFControl_ExternFile) EF = it.Value();
-    if ( EF->GetWriteStatus() != IFSelect_RetVoid ) continue;
+  for (NCollection_DataMap<TCollection_AsciiString, Handle(STEPCAFControl_ExternFile)>::Iterator anExtFileIter (myFiles);
+       anExtFileIter.More(); anExtFileIter.Next())
+  {
+    Handle(STEPCAFControl_ExternFile) anExtFile = anExtFileIter.Value();
+    if (anExtFile->GetWriteStatus() != IFSelect_RetVoid)
+    {
+      continue;
+    }
 
     // construct extern file name
-    TCollection_AsciiString fname = OSD_Path::AbsolutePath ( dpath, EF->GetName()->String() );
-    if ( fname.Length() <= 0 ) fname = EF->GetName()->String();
+    TCollection_AsciiString aFileName = OSD_Path::AbsolutePath (aDirPath, anExtFile->GetName()->String());
+    if (aFileName.Length() <= 0)
+    {
+      aFileName = anExtFile->GetName()->String();
+    }
 #ifdef OCCT_DEBUG
-    std::cout << "Writing external file: " << fname.ToCString() << std::endl;
+    std::cout << "Writing external file: " << aFileName << std::endl;
 #endif
     
-    EF->SetWriteStatus ( EF->GetWS()->SendAll ( fname.ToCString() ) );
+    const IFSelect_ReturnStatus anExtStatus = anExtFile->GetWS()->SendAll (aFileName.ToCString());
+    anExtFile->SetWriteStatus (anExtStatus);
+    if (anExtStatus != IFSelect_RetDone)
+    {
+      aStatus = anExtStatus;
+    }
   }
 
-  return status;
+  return aStatus;
 }
 
 //=======================================================================
@@ -352,6 +369,21 @@ void STEPCAFControl_Writer::prepareUnit(const TDF_Label& theLabel,
   }
 }
 
+//=======================================================================
+//function : WriteStream
+//purpose  :
+//=======================================================================
+IFSelect_ReturnStatus STEPCAFControl_Writer::WriteStream (std::ostream& theStream)
+{
+  if (!myFiles.IsEmpty())
+  {
+    // writing external files is unsupported via stream interface
+    return IFSelect_RetError;
+  }
+
+  return myWriter.WriteStream (theStream);
+}
+
 //=======================================================================
 //function : Transfer
 //purpose  :
index df6a4545d61ec1577e7e66b23393e0db9e75f66c..ca7f4b5340f51a8e8a994d512acdb137cdd5b9c0 100644 (file)
@@ -70,7 +70,11 @@ public:
   //! filename will be a name of root file, all other files
   //! have names of corresponding parts
   //! Provided for use like single-file writer
-  Standard_EXPORT IFSelect_ReturnStatus Write (const Standard_CString filename);
+  Standard_EXPORT IFSelect_ReturnStatus Write (const Standard_CString theFileName);
+
+  //! Writes all the produced models into the stream.
+  //! Provided for use like single-file writer
+  Standard_EXPORT IFSelect_ReturnStatus WriteStream (std::ostream& theStream);
   
   //! Transfers a document (or single label) to a STEP model
   //! The mode of translation of shape is AsIs
index 5ab4d6bff16d83a19f74d6c6ac96742ead182c7e..96756267ecef40b98ea084f916dbcd9ea9fb88c3 100644 (file)
@@ -18,6 +18,9 @@
 #include <STEPControl_ActorWrite.hxx>
 #include <STEPControl_Controller.hxx>
 #include <StepData_StepModel.hxx>
+#include <StepData_Protocol.hxx>
+#include <StepData_StepWriter.hxx>
+#include <TopExp_Explorer.hxx>
 #include <TopoDS_Shape.hxx>
 #include <XSAlgo.hxx>
 #include <XSAlgo_AlgoContainer.hxx>
@@ -147,14 +150,37 @@ IFSelect_ReturnStatus STEPControl_Writer::Transfer
 
 //=======================================================================
 //function : Write
-//purpose  : 
+//purpose  :
 //=======================================================================
-
-IFSelect_ReturnStatus STEPControl_Writer::Write (const Standard_CString filename)
+IFSelect_ReturnStatus STEPControl_Writer::Write (const Standard_CString theFileName)
 {
-  return thesession->SendAll(filename);
+  return thesession->SendAll (theFileName);
 }
 
+//=======================================================================
+//function : WriteStream
+//purpose  :
+//=======================================================================
+IFSelect_ReturnStatus STEPControl_Writer::WriteStream (std::ostream& theOStream)
+{
+  Handle(StepData_StepModel) aModel = Model();
+  if (aModel.IsNull())
+  {
+    return IFSelect_RetFail;
+  }
+
+  Handle(StepData_Protocol) aProtocol = Handle(StepData_Protocol)::DownCast (aModel->Protocol());
+  if (aProtocol.IsNull())
+  {
+    return IFSelect_RetFail;
+  }
+
+  StepData_StepWriter aWriter (aModel);
+  aWriter.SendModel (aProtocol);
+  return aWriter.Print (theOStream)
+       ? IFSelect_RetDone
+       : IFSelect_RetFail;
+}
 
 //=======================================================================
 //function : PrintStatsTransfer
index 247cd047e6f06bb8fa05402ee13fa83734003950..d179c0f7222b418cd9fdeaeed9cb7134ee23f2f8 100644 (file)
@@ -88,10 +88,13 @@ public:
                     const STEPControl_StepModelType mode,
                     const Standard_Boolean compgraph = Standard_True,
                     const Message_ProgressRange& theProgress = Message_ProgressRange());
-  
+
   //! Writes a STEP model in the file identified by filename.
-  Standard_EXPORT IFSelect_ReturnStatus Write (const Standard_CString filename);
-  
+  Standard_EXPORT IFSelect_ReturnStatus Write (const Standard_CString theFileName);
+
+  //! Writes a STEP model in the std::ostream.
+  Standard_EXPORT IFSelect_ReturnStatus WriteStream (std::ostream& theOStream);
+
   //! Displays the statistics for the
   //! last translation. what defines the kind of statistics that are displayed:
   //! - 0 gives general statistics   (number of translated roots,
index 3da83663125f009db21b63164c1b2a0718268851..509e2b7793bddfbd2bda4c6e8b5adec12ef89b7a 100644 (file)
@@ -26,6 +26,9 @@
 #include <IGESCAFControl_Writer.hxx>
 #include <IGESControl_Controller.hxx>
 #include <Interface_Macros.hxx>
+#include <OSD_OpenFile.hxx>
+#include <OSD_Path.hxx>
+#include <STEPCAFControl_ExternFile.hxx>
 #include <STEPCAFControl_Reader.hxx>
 #include <STEPCAFControl_Writer.hxx>
 #include <STEPControl_Controller.hxx>
@@ -375,19 +378,26 @@ static Standard_Integer WriteIges(Draw_Interpretor& di, Standard_Integer argc, c
 //function : ReadStep
 //purpose  : Read STEP file to DECAF document 
 //=======================================================================
-
 static Standard_Integer ReadStep(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
 {
   DeclareAndCast(STEPControl_Controller, ctl, XSDRAW::Controller());
-  if (ctl.IsNull()) XSDRAW::SetNorm("STEP");
+  if (ctl.IsNull())
+  {
+    XSDRAW::SetNorm ("STEP");
+  }
 
   Standard_CString aDocName = NULL;
   TCollection_AsciiString aFilePath, aModeStr;
+  bool toTestStream = false;
   for (Standard_Integer anArgIter = 1; anArgIter < argc; ++anArgIter)
   {
     TCollection_AsciiString anArgCase(argv[anArgIter]);
     anArgCase.LowerCase();
-    if (aDocName == NULL)
+    if (anArgCase == "-stream")
+    {
+      toTestStream = true;
+    }
+    else if (aDocName == NULL)
     {
       aDocName = argv[anArgIter];
     }
@@ -406,26 +416,26 @@ static Standard_Integer ReadStep(Draw_Interpretor& di, Standard_Integer argc, co
     }
   }
 
-  TCollection_AsciiString fnom, rnom;
-  Standard_Boolean modfic = XSDRAW::FileAndVar(aFilePath.ToCString(), aDocName, "STEP", fnom, rnom);
-  if (modfic) di << " File STEP to read : " << fnom.ToCString() << "\n";
-  else        di << " Model taken from the session : " << fnom.ToCString() << "\n";
+  TCollection_AsciiString aFileName, anOldVarName;
+  Standard_Boolean isFileMode = XSDRAW::FileAndVar (aFilePath.ToCString(), aDocName, "STEP", aFileName, anOldVarName);
+  if (isFileMode) di << " File STEP to read : " << aFileName << "\n";
+  else            di << " Model taken from the session : " << aFileName << "\n";
   //  di<<" -- Names of variables BREP-DRAW prefixed by : "<<rnom<<"\n";
 
-  STEPCAFControl_Reader reader(XSDRAW::Session(), modfic);
+  STEPCAFControl_Reader aReader (XSDRAW::Session(), isFileMode);
   if (!aModeStr.IsEmpty())
   {
-    Standard_Boolean mode = Standard_True;
-    for (Standard_Integer i = 1; aModeStr.Value(i); ++i)
+    Standard_Boolean aMode = Standard_True;
+    for (Standard_Integer i = 1; aModeStr.Value (i); ++i)
     {
-      switch (aModeStr.Value(i))
+      switch (aModeStr.Value (i))
       {
-        case '-': mode = Standard_False; break;
-        case '+': mode = Standard_True; break;
-        case 'c': reader.SetColorMode(mode); break;
-        case 'n': reader.SetNameMode(mode); break;
-        case 'l': reader.SetLayerMode(mode); break;
-        case 'v': reader.SetPropsMode(mode); break;
+        case '-' : aMode = Standard_False; break;
+        case '+' : aMode = Standard_True;  break;
+        case 'c' : aReader.SetColorMode (aMode); break;
+        case 'n' : aReader.SetNameMode  (aMode); break;
+        case 'l' : aReader.SetLayerMode (aMode); break;
+        case 'v' : aReader.SetPropsMode (aMode); break;
         default:
         {
           Message::SendFail() << "Syntax error at '" << aModeStr << "'\n";
@@ -434,26 +444,37 @@ static Standard_Integer ReadStep(Draw_Interpretor& di, Standard_Integer argc, co
       }
     }
   }
+  
+  Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator (di);
+  Message_ProgressScope aRootScope (aProgress->Start(), "STEP import", isFileMode ? 2 : 1);
 
-  Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator(di);
-  Message_ProgressScope aRootScope(aProgress->Start(), "STEP import", modfic ? 2 : 1);
-
-  IFSelect_ReturnStatus readstat = IFSelect_RetVoid;
-  if (modfic)
+  IFSelect_ReturnStatus aReadStat = IFSelect_RetVoid;
+  if (isFileMode)
   {
-    Message_ProgressScope aReadScope(aRootScope.Next(), "File reading", 1);
+    Message_ProgressScope aReadScope (aRootScope.Next(), "File reading", 1);
     aReadScope.Show();
-    readstat = reader.ReadFile(fnom.ToCString());
+    if (toTestStream)
+    {
+      std::ifstream aStream;
+      OSD_OpenStream (aStream, aFileName.ToCString(), std::ios::in | std::ios::binary);
+      TCollection_AsciiString aFolder, aFileNameShort;
+      OSD_Path::FolderAndFileFromPath (aFileName, aFolder, aFileNameShort);
+      aReadStat = aReader.ReadStream (aFileNameShort.ToCString(), aStream);
+    }
+    else
+    {
+      aReadStat = aReader.ReadFile (aFileName.ToCString());
+    }
   }
   else if (XSDRAW::Session()->NbStartingEntities() > 0)
   {
-    readstat = IFSelect_RetDone;
+    aReadStat = IFSelect_RetDone;
   }
-  if (readstat != IFSelect_RetDone)
+  if (aReadStat != IFSelect_RetDone)
   {
-    if (modfic)
+    if (isFileMode)
     {
-      di << "Could not read file " << fnom << " , abandon\n";
+      di << "Could not read file " << aFileName << " , abandon\n";
     }
     else
     {
@@ -462,30 +483,29 @@ static Standard_Integer ReadStep(Draw_Interpretor& di, Standard_Integer argc, co
     return 1;
   }
 
-  Handle(TDocStd_Document) doc;
-  if (!DDocStd::GetDocument(aDocName, doc, Standard_False))
+  Handle(TDocStd_Document) aDoc;
+  if (!DDocStd::GetDocument (aDocName, aDoc, Standard_False))
   {
-    Handle(TDocStd_Application) A = DDocStd::GetApplication();
-    A->NewDocument("BinXCAF", doc);
-    TDataStd_Name::Set(doc->GetData()->Root(), aDocName);
-    Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(doc);
-    Draw::Set(aDocName, DD);
+    Handle(TDocStd_Application) anApp = DDocStd::GetApplication();
+    anApp->NewDocument("BinXCAF", aDoc);
+    TDataStd_Name::Set (aDoc->GetData()->Root(), aDocName);
+    Handle(DDocStd_DrawDocument) aDrawDoc = new DDocStd_DrawDocument (aDoc);
+    Draw::Set (aDocName, aDrawDoc);
     //     di << "Document saved with name " << aDocName;
   }
-  if (!reader.Transfer(doc, aRootScope.Next()))
+  if (!aReader.Transfer (aDoc, aRootScope.Next()))
   {
     di << "Cannot read any relevant data from the STEP file\n";
     return 1;
   }
 
-  Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(doc);
-  Draw::Set(aDocName, DD);
+  Handle(DDocStd_DrawDocument) aDrawDoc = new DDocStd_DrawDocument (aDoc);
+  Draw::Set (aDocName, aDrawDoc);
   di << "Document saved with name " << aDocName;
 
-  NCollection_DataMap<TCollection_AsciiString, Handle(STEPCAFControl_ExternFile)> DicFile = reader.ExternFiles();
-  FillDicWS(DicFile);
-  AddWS(fnom, XSDRAW::Session());
-
+  NCollection_DataMap<TCollection_AsciiString, Handle(STEPCAFControl_ExternFile)> aDicFile = aReader.ExternFiles();
+  FillDicWS (aDicFile);
+  AddWS (aFileName, XSDRAW::Session());
   return 0;
 }
 
@@ -493,131 +513,182 @@ static Standard_Integer ReadStep(Draw_Interpretor& di, Standard_Integer argc, co
 //function : WriteStep
 //purpose  : Write DECAF document to STEP
 //=======================================================================
-
 static Standard_Integer WriteStep(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
 {
-  if (argc < 3)
+  DeclareAndCast(STEPControl_Controller,ctl,XSDRAW::Controller());
+  if (ctl.IsNull())
   {
-    di << "Use: " << argv[0] << " Doc filename [mode [multifile_prefix [label]]]: write document to the STEP file\n";
-    di << "mode can be: a or 0 : AsIs (default)\n";
-    di << "             f or 1 : FacettedBRep        s or 2 : ShellBasedSurfaceModel\n";
-    di << "             m or 3 : ManifoldSolidBrep   w or 4 : GeometricCurveSet/WireFrame\n";
-    di << "multifile_prefix: triggers writing assembly components as separate files,\n";
-    di << "                  and defines common prefix for their names\n";
-    di << "label: tag of the sub-assembly label to save only that sub-assembly\n";
-    return 0;
+    XSDRAW::SetNorm ("STEP");
   }
+  STEPCAFControl_Writer aWriter (XSDRAW::Session(), Standard_True);
 
-  Handle(TDocStd_Document) Doc;
-  DDocStd::GetDocument(argv[1], Doc);
-  if (Doc.IsNull())
-  {
-    di << argv[1] << " is not a document\n";
-    return 1;
-  }
-  Standard_CString multifile = 0;
-
-  Standard_Integer k = 3;
-  DeclareAndCast(STEPControl_Controller, ctl, XSDRAW::Controller());
-  if (ctl.IsNull()) XSDRAW::SetNorm("STEP");
-  STEPCAFControl_Writer writer(XSDRAW::Session(), Standard_True);
-
-  STEPControl_StepModelType mode = STEPControl_AsIs;
-  if (argc > k)
+  Handle(TDocStd_Document) aDoc;
+  TCollection_AsciiString aDocName, aFilePath;
+  STEPControl_StepModelType aMode = STEPControl_AsIs;
+  bool hasModeArg = false, toTestStream = false;
+  TCollection_AsciiString aMultiFilePrefix, aLabelName;
+  TDF_Label aLabel;
+  for (Standard_Integer anArgIter = 1; anArgIter < argc; ++anArgIter)
   {
-    switch (argv[k][0])
+    TCollection_AsciiString anArgCase (argv[anArgIter]);
+    anArgCase.LowerCase();
+    if (anArgCase == "-stream")
     {
-      case 'a':
-      case '0': mode = STEPControl_AsIs;                    break;
-      case 'f':
-      case '1': mode = STEPControl_FacetedBrep;             break;
-      case 's':
-      case '2': mode = STEPControl_ShellBasedSurfaceModel;  break;
-      case 'm':
-      case '3': mode = STEPControl_ManifoldSolidBrep;       break;
-      case 'w':
-      case '4': mode = STEPControl_GeometricCurveSet;       break;
-      default:  di << "3rd arg = mode, incorrect [give fsmw]\n"; return 1;
+      toTestStream = true;
     }
-    Standard_Boolean wrmode = Standard_True;
-    for (Standard_Integer i = 0; argv[k][i]; i++)
-      switch (argv[3][i])
+    else if (aDocName.IsEmpty())
+    {
+      Standard_CString aDocNameStr = argv[anArgIter];
+      DDocStd::GetDocument (aDocNameStr, aDoc);
+      if (aDoc.IsNull())
       {
-        case '-': wrmode = Standard_False; break;
-        case '+': wrmode = Standard_True; break;
-        case 'c': writer.SetColorMode(wrmode); break;
-        case 'n': writer.SetNameMode(wrmode); break;
-        case 'l': writer.SetLayerMode(wrmode); break;
-        case 'v': writer.SetPropsMode(wrmode); break;
+        di << "Syntax error: '" << argv[anArgIter] << "' is not a document";
+        return 1;
       }
-    k++;
-  }
-  TDF_Label label;
-  if (argc > k)
-  {
-    TCollection_AsciiString aStr(argv[k]);
-    if (aStr.Search(":") == -1)
-      multifile = argv[k++];
-
-  }
-  if (argc > k)
-  {
-
-    if (!DDF::FindLabel(Doc->Main().Data(), argv[k], label) || label.IsNull())
+      aDocName = aDocNameStr;
+    }
+    else if (aFilePath.IsEmpty())
+    {
+      aFilePath = argv[anArgIter];
+    }
+    else if (!hasModeArg)
     {
-      di << "No label for entry" << "\n";
+      hasModeArg = true;
+      switch (anArgCase.Value (1))
+      {
+        case 'a':
+        case '0': aMode = STEPControl_AsIs;                    break;
+        case 'f':
+        case '1': aMode = STEPControl_FacetedBrep;             break;
+        case 's':
+        case '2': aMode = STEPControl_ShellBasedSurfaceModel;  break;
+        case 'm':
+        case '3': aMode = STEPControl_ManifoldSolidBrep;       break;
+        case 'w':
+        case '4': aMode = STEPControl_GeometricCurveSet;       break;
+        default:
+        {
+          di << "Syntax error: mode '" << argv[anArgIter] << "' is incorrect [give fsmw]";
+          return 1;
+        }
+      }
+      Standard_Boolean wrmode = Standard_True;
+      for (Standard_Integer i = 1; i <= anArgCase.Length(); ++i)
+      {
+        switch (anArgCase.Value (i))
+        {
+          case '-' : wrmode = Standard_False; break;
+          case '+' : wrmode = Standard_True;  break;
+          case 'c' : aWriter.SetColorMode (wrmode); break;
+          case 'n' : aWriter.SetNameMode  (wrmode); break;
+          case 'l' : aWriter.SetLayerMode (wrmode); break;
+          case 'v' : aWriter.SetPropsMode (wrmode); break;
+        }
+      }
+    }
+    else if (aMultiFilePrefix.IsEmpty()
+          && anArgCase.Search (":") == -1)
+    {
+      aMultiFilePrefix = argv[anArgIter];
+    }
+    else if (aLabel.IsNull())
+    {
+      if (!DDF::FindLabel (aDoc->Main().Data(), argv[anArgIter], aLabel)
+       || aLabel.IsNull())
+      {
+        di << "Syntax error: No label for entry '" << argv[anArgIter] << "'";
+        return 1;
+      }
+      aLabelName = argv[anArgIter];
+    }
+    else
+    {
+      di << "Syntax error: unknown argument '" << argv[anArgIter] << "'";
       return 1;
-
     }
   }
+  if (aFilePath.IsEmpty())
+  {
+    di << "Syntax error: wrong number of arguments";
+    return 1;
+  }
 
-  TCollection_AsciiString fnom, rnom;
-  const Standard_Boolean modfic = XSDRAW::FileAndVar(argv[2], argv[1], "STEP", fnom, rnom);
+  TCollection_AsciiString aFileName, anOldVarName;
+  const Standard_Boolean isFileMode = XSDRAW::FileAndVar (aFilePath.ToCString(), aDocName.ToCString(), "STEP", aFileName, anOldVarName);
 
-  Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator(di);
-  Message_ProgressScope aRootScope(aProgress->Start(), "STEP export", modfic ? 2 : 1);
-  if (!label.IsNull())
-  {
-    di << "Translating label " << argv[k] << " of document " << argv[1] << " to STEP\n";
-    if (!writer.Transfer(label, mode, multifile, aRootScope.Next()))
+  Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator (di);
+  Message_ProgressScope aRootScope (aProgress->Start(), "STEP export", isFileMode ? 2 : 1);
+  if (!aLabel.IsNull())
+  {  
+    di << "Translating label " << aLabelName << " of document " << aDocName << " to STEP\n";
+    if (!aWriter.Transfer (aLabel, aMode,
+                           !aMultiFilePrefix.IsEmpty() ? aMultiFilePrefix.ToCString() : NULL,
+                           aRootScope.Next()))
     {
-      di << "The label of document cannot be translated or gives no result\n";
+      di << "Error: the label of document cannot be translated or gives no result";
       return 1;
     }
   }
   else
   {
-    di << "Translating document " << argv[1] << " to STEP\n";
-    if (!writer.Transfer(Doc, mode, multifile, aRootScope.Next()))
+    di << "Translating document " << aDocName << " to STEP\n";
+    if (!aWriter.Transfer (aDoc, aMode,
+                           !aMultiFilePrefix.IsEmpty() ? aMultiFilePrefix.ToCString() : NULL,
+                           aRootScope.Next()))
     {
-      di << "The document cannot be translated or gives no result\n";
+      di << "Error: The document cannot be translated or gives no result\n";
     }
   }
 
-  if (modfic)
+  if (!isFileMode)
   {
-    Message_ProgressScope aWriteScope(aRootScope.Next(), "File writing", 1);
-    aWriteScope.Show();
-    di << "Writing STEP file " << argv[2] << "\n";
-    IFSelect_ReturnStatus stat = writer.Write(argv[2]);
-    switch (stat)
-    {
-      case IFSelect_RetVoid: di << "No file written\n"; break;
-      case IFSelect_RetDone:
-      {
-        di << "File " << argv[2] << " written\n";
+    di << "Document has been translated into the session";
+    return 0;
+  }
 
-        NCollection_DataMap<TCollection_AsciiString, Handle(STEPCAFControl_ExternFile)> DicFile = writer.ExternFiles();
-        FillDicWS(DicFile);
-        AddWS(argv[2], XSDRAW::Session());
-        break;
-      }
-      default: di << "Error on writing file\n"; break;
+  Message_ProgressScope aWriteScope (aRootScope.Next(), "File writing", 1);
+  aWriteScope.Show();
+  di << "Writing STEP file " << aFilePath << "\n";
+
+  IFSelect_ReturnStatus aStat = IFSelect_RetVoid;
+  if (toTestStream)
+  {
+    std::ofstream aStream;
+    OSD_OpenStream (aStream, aFilePath, std::ios::out | std::ios::binary);
+    aStat = aWriter.WriteStream (aStream);
+    aStream.close();
+    if (!aStream.good()
+      && aStat == IFSelect_RetDone)
+    {
+      aStat = IFSelect_RetFail;
     }
   }
   else
   {
-    di << "Document has been translated into the session";
+    aStat = aWriter.Write (aFilePath.ToCString());
+  }
+
+  switch (aStat)
+  {
+    case IFSelect_RetVoid:
+    {
+      di << "Error: no file written";
+      break;
+    }
+    case IFSelect_RetDone:
+    {
+      di << "File " << aFilePath << " written\n";
+
+      NCollection_DataMap<TCollection_AsciiString, Handle(STEPCAFControl_ExternFile)> aDicFile = aWriter.ExternFiles();
+      FillDicWS (aDicFile);
+      AddWS (aFilePath, XSDRAW::Session());
+      break;
+    }
+    default:
+    {
+      di << "Error on writing file";
+      break;
+    }
   }
   return 0;
 }
@@ -1271,10 +1342,21 @@ void XDEDRAW_Common::InitCommands(Draw_Interpretor& di)
   di.Add("ReadIges", "Doc filename: Read IGES file to DECAF document", __FILE__, ReadIges, g);
   di.Add("WriteIges", "Doc filename: Write DECAF document to IGES file", __FILE__, WriteIges, g);
   di.Add("ReadStep",
-         "Doc filename [mode]"
-         "\n\t\t: Read STEP file to a document.",
+         "Doc filename [mode] [-stream]"
+         "\n\t\t: Read STEP file to a document."
+         "\n\t\t:  -stream read using istream reading interface (testing)",
          __FILE__, ReadStep, g);
-  di.Add("WriteStep", "Doc filename [mode=a [multifile_prefix] [label]]: Write DECAF document to STEP file", __FILE__, WriteStep, g);
+  di.Add("WriteStep" ,
+         "Doc filename [mode=a [multifile_prefix] [label]] [-stream]"
+         "\n\t\t: Write DECAF document to STEP file"
+         "\n\t\t:   mode can be: a or 0 : AsIs (default)"
+         "\n\t\t:                f or 1 : FacettedBRep        s or 2 : ShellBasedSurfaceModel"
+         "\n\t\t:                m or 3 : ManifoldSolidBrep   w or 4 : GeometricCurveSet/WireFrame"
+         "\n\t\t:   multifile_prefix: triggers writing assembly components as separate files,"
+         "\n\t\t:                     and defines common prefix for their names"
+         "\n\t\t:   label  tag of the sub-assembly label to save only that sub-assembly"
+         "\n\t\t:  -stream read using ostream writing interface (testing)",
+         __FILE__, WriteStep, g);
 
   di.Add("XFileList", "Print list of files that was transferred by the last transfer", __FILE__, GetDicWSList, g);
   di.Add("XFileCur", ": returns name of file which is set as current", __FILE__, GetCurWS, g);
index 3eaf3df394bea2181efb3297259848230af85521..7ceff7987b5b1faca2cdfd15673d668a35751b38 100644 (file)
@@ -22,6 +22,8 @@
 #include <Interface_Static.hxx>
 #include <Message.hxx>
 #include <Message_ProgressScope.hxx>
+#include <OSD_OpenFile.hxx>
+#include <OSD_Path.hxx>
 #include <STEPControl_ActorWrite.hxx>
 #include <STEPControl_Controller.hxx>
 #include <STEPControl_Reader.hxx>
@@ -282,8 +284,11 @@ static Standard_Integer testreadstep (Draw_Interpretor& di, Standard_Integer arg
   IFSelect_ReturnStatus readstat;
   if (useStream)
   {
-    std::ifstream aStream (filename);
-    readstat = Reader.ReadStream(filename, aStream);
+    std::ifstream aStream;
+    OSD_OpenStream (aStream, filename, std::ios::in | std::ios::binary);
+    TCollection_AsciiString aFolder, aFileNameShort;
+    OSD_Path::FolderAndFileFromPath (filename, aFolder, aFileNameShort);
+    readstat = Reader.ReadStream (aFileNameShort.ToCString(), aStream);
   }
   else
   {
@@ -435,22 +440,72 @@ static Standard_Integer stepwrite (Draw_Interpretor& di, Standard_Integer argc,
 //=======================================================================
 static Standard_Integer testwrite (Draw_Interpretor& di, Standard_Integer argc, const char** argv) 
 {
-  if (argc != 3)                                                                                      
-    {                                                                                             
-      di << "ERROR in " << argv[0] << "Wrong Number of Arguments.\n";                     
-      di << " Usage : " << argv[0] <<" file_name shape_name \n"; 
-      return 1;                                                                                 
+  TCollection_AsciiString aFilePath;
+  TopoDS_Shape aShape;
+  bool toTestStream = false;
+  for (Standard_Integer anArgIter = 1; anArgIter < argc; ++anArgIter)
+  {
+    TCollection_AsciiString anArgCase (argv[anArgIter]);
+    anArgCase.LowerCase();
+    if (anArgCase == "-stream")
+    {
+      toTestStream = true;
     }
-  STEPControl_Writer Writer;
-  Standard_CString filename = argv[1];
-  TopoDS_Shape shape = DBRep::Get(argv[2]); 
-  IFSelect_ReturnStatus stat = Writer.Transfer(shape,STEPControl_AsIs);
-  stat = Writer.Write(filename);
-  if(stat != IFSelect_RetDone){
-    di<<"Error on writing file\n";                                                               
+    else if (aFilePath.IsEmpty())
+    {
+      aFilePath = argv[anArgIter];
+    }
+    else if (aShape.IsNull())
+    {
+      aShape = DBRep::Get (argv[anArgIter]);
+      if (aShape.IsNull())
+      {
+        di << "Syntax error: '" << argv[anArgIter] << "' is not a shape";
+        return 1;
+      }
+    }
+    else
+    {
+      di << "Syntax error: unknown argument '" << argv[anArgIter] << "'";
+      return 1;
+    }
+  }
+  if (aShape.IsNull())
+  {
+    di << "Syntax error: wrong number of arguments";
+    return 1;
+  }
+
+  STEPControl_Writer aWriter;
+  IFSelect_ReturnStatus aStat = aWriter.Transfer (aShape, STEPControl_AsIs);
+  if (aStat != IFSelect_RetDone)
+  {
+    di << "Error on transferring shape";
+    return 1;
+  }
+
+  if (toTestStream)
+  {
+    std::ofstream aStream;
+    OSD_OpenStream (aStream, aFilePath, std::ios::out | std::ios::binary);
+    aStat = aWriter.WriteStream (aStream);
+    aStream.close();
+    if (!aStream.good()
+      && aStat == IFSelect_RetDone)
+    {
+      aStat = IFSelect_RetFail;
+    }
+  }
+  else
+  {
+    aStat = aWriter.Write (aFilePath.ToCString());
+  }
+  if (aStat != IFSelect_RetDone)
+  {
+    di << "Error on writing file";
     return 1; 
   }
-  di<<"File Is Written\n";
+  di << "File Is Written";
   return 0;
 }
 
@@ -550,7 +605,8 @@ void XSDRAWSTEP::InitCommands (Draw_Interpretor& theCommands)
   XSDRAWSTEP::Init();
   XSDRAW::LoadDraw(theCommands);
   theCommands.Add("stepwrite" ,    "stepwrite mode[0-4 afsmw] shape",  __FILE__, stepwrite,     g);
-  theCommands.Add("testwritestep", "testwritestep filename.stp shape", __FILE__, testwrite,     g);
+  theCommands.Add("testwritestep", "testwritestep filename.stp shape [-stream]",
+                   __FILE__, testwrite, g);
   theCommands.Add("stepread",      "stepread  [file] [f or r (type of model full or reduced)]",__FILE__, stepread,      g);
   theCommands.Add("testreadstep",  "testreadstep file shape [-stream]",__FILE__, testreadstep,  g);
   theCommands.Add("steptrans",     "steptrans shape stepax1 stepax2",  __FILE__, steptrans,     g);
diff --git a/tests/bugs/step/bug32350 b/tests/bugs/step/bug32350
new file mode 100644 (file)
index 0000000..4fe9624
--- /dev/null
@@ -0,0 +1,36 @@
+puts "==================================================="
+puts "0032350: Data Exchange - STEPControl_Writer.writeStream(std::ostream)"
+puts "==================================================="
+puts ""
+
+pload XDE OCAF MODELING VISUALIZATION
+
+Close D -silent
+
+set aTmpFile1 "$imagedir/${casename}_1.stp"
+set aTmpFile2 "$imagedir/${casename}_2.stp"
+
+ReadStep D [locate_data_file "as1-oc-214-mat.stp"] -stream
+
+WriteStep D "$aTmpFile1"
+WriteStep D "$aTmpFile2" -stream
+
+ReadStep D1 "$aTmpFile1"
+ReadStep D2 "$aTmpFile2"
+
+file delete "$aTmpFile1"
+file delete "$aTmpFile2"
+
+vclear
+vinit View1
+XDisplay -dispMode 1 D
+vfit
+vdump "$imagedir/${casename}_src.png"
+
+vclear
+XDisplay -dispMode 1 D1
+vdump "$imagedir/${casename}_out1.png"
+
+vclear
+XDisplay -dispMode 1 D2
+vdump "$imagedir/${casename}_out2.png"