]> OCCT Git - occt-copy.git/commitdiff
0027245: DRAW command to test binary persistence for shapes
authoremv <emv@opencascade.com>
Tue, 14 Jun 2016 13:13:39 +0000 (16:13 +0300)
committermsv <msv@opencascade.com>
Tue, 28 Jun 2016 11:42:26 +0000 (14:42 +0300)
- Added new static methods Read and Write in BinTools class to store/retrieve a shape in binary format.
- Added new draw commands "binsave" and "binrestore"
- Updated documantation with description of the new BinTools class methods and corresponding BRepTools class methods.

dox/user_guides/modeling_data/modeling_data.md
src/BinTools/BinTools.cdl
src/BinTools/BinTools.cxx
src/DBRep/DBRep.cxx

index 1cd63326b34a26b5825cf3813b62b47c47ccd5dc..5cb4ab7e8b687dd723f06a1c35d0bb6562a7a9a3 100644 (file)
@@ -1237,4 +1237,19 @@ For example, in the wire in the image we want to recuperate the edges in the ord
   } 
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
+@subsection occt_modat_5_6 Storage of shapes
 
+**BRepTools** and **BinTools** packages contain methods *Read* and *Write* allowing to read and write a Shape to/from a stream or a file.
+The methods provided by **BRepTools** package use ASCII storage format; **BinTools** package use binary format.
+Each of these methods has two arguments:
+- a *TopoDS_Shape* object to be read/written;
+- a stream object or a file name to read from/write to.
+
+The following sample code reads a shape from ASCII file and writes it to a binary one:
+
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
+  TopoDS_Shape aShape;
+  if (BRepTools::Read (aShape, "source_file.txt")) {
+    BinTools::Write (aShape, "result_file.bin");
+  }
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
index 934d73854ae9ada1f342e9bb12551cf4798053e3..ec64d95facf4176c19c7c21e0ce62241b8e98300 100644 (file)
@@ -26,7 +26,8 @@ uses
     TopLoc,
     BRep,
     BRepTools, 
-    TopTools
+    TopTools,
+    TopoDS
 is
     
     class  ShapeSet; 
@@ -66,5 +67,17 @@ is
     GetExtChar (IS : out IStream from Standard; theValue : out ExtCharacter from Standard) returns IStream; 
     ---C++: return &
 
+    Write (theShape : Shape from TopoDS; theStream : out OStream from Standard);
+    ---Purpose: Writes <theShape> on <theStream> in binary format.
+
+    Read (theShape : out Shape from TopoDS; theStream : out IStream from Standard);
+    ---Purpose: Reads a shape from <theStream> and returns it in <theShape>.
+
+    Write (theShape : Shape from TopoDS; theFile : CString from Standard) returns Boolean from Standard;
+    ---Purpose: Writes <theShape> in <theFile>.
+
+    Read (theShape : out Shape from TopoDS; theFile : CString from Standard) returns Boolean from Standard;
+    ---Purpose: Reads a shape from <theFile> and returns it in <theShape>.
+
 end BinTools;
 
index 4ce69f2fbdba2bce374b8d59c1fd946c9f21ed5e..ce368f6b929dd7550bc1f35f0b7cd21555b39e0e 100644 (file)
 // commercial license or contractual agreement.
 
 #include <BinTools.ixx>
+#include <BinTools_ShapeSet.hxx>
 #include <FSD_FileHeader.hxx>
+#include <OSD_OpenFile.hxx>
 #include <Storage_StreamTypeMismatchError.hxx>
+#include <BinTools_ShapeSet.hxx>
+#include <OSD_OpenFile.hxx>
 
 //=======================================================================
 //function : PutBool
@@ -129,3 +133,64 @@ Standard_IStream& BinTools::GetBool(Standard_IStream& IS, Standard_Boolean& aVal
   aValue = (Standard_Boolean)IS.get();
   return IS;
 }
+
+//=======================================================================
+//function : Write
+//purpose  : 
+//=======================================================================
+
+void BinTools::Write (const TopoDS_Shape& theShape, Standard_OStream& theStream)
+{
+  BinTools_ShapeSet aShapeSet(Standard_True);
+  aShapeSet.SetFormatNb (3);
+  aShapeSet.Add (theShape);
+  aShapeSet.Write (theStream);
+  aShapeSet.Write (theShape, theStream);
+}
+
+//=======================================================================
+//function : Read
+//purpose  : 
+//=======================================================================
+
+void BinTools::Read (TopoDS_Shape& theShape, Standard_IStream& theStream)
+{
+  BinTools_ShapeSet aShapeSet(Standard_True);
+  aShapeSet.Read (theStream);
+  aShapeSet.Read (theShape, theStream, aShapeSet.NbShapes());
+}
+
+//=======================================================================
+//function : Write
+//purpose  : 
+//=======================================================================
+
+Standard_Boolean BinTools::Write (const TopoDS_Shape& theShape, const Standard_CString theFile)
+{
+  ofstream aStream;
+  aStream.precision (15);
+  OSD_OpenStream (aStream, theFile, ios::out | ios::binary);
+  if (!aStream.good())
+    return Standard_False;
+
+  Write (theShape, aStream);
+  aStream.close();
+  return aStream.good();
+}
+
+//=======================================================================
+//function : Read
+//purpose  : 
+//=======================================================================
+
+Standard_Boolean BinTools::Read (TopoDS_Shape& theShape, const Standard_CString theFile)
+{
+  filebuf aBuf;
+  OSD_OpenFileBuf (aBuf, theFile, ios::in | ios::binary);
+  if (!aBuf.is_open())
+    return Standard_False;
+
+  Standard_IStream aStream (&aBuf);
+  Read (theShape, aStream);
+  return aStream.good();
+}
index 8b19ffc385efd11ac0264102aff568a104cfb636..143d6c1a7fb52042480c0d2456fcdc65dc5bdac6 100644 (file)
@@ -43,6 +43,7 @@
 #include <BRepGProp.hxx>
 #include <TColStd_Array1OfReal.hxx>
 #include <Draw_ProgressIndicator.hxx>
+#include <BinTools.hxx>
 
 // memory management
 #include <Standard.hxx>
@@ -1271,6 +1272,49 @@ static Standard_Integer XProgress (Draw_Interpretor& di, Standard_Integer argc,
   return 0;
 }
 
+//=======================================================================
+// binsave
+//=======================================================================
+static Standard_Integer binsave(Draw_Interpretor& di, Standard_Integer n, const char** a)
+{
+  if (n <= 2) return 1;
+
+  TopoDS_Shape aShape = DBRep::Get (a[1]);
+  if (aShape.IsNull())
+  {
+    di << a[1] << " is not a shape";
+    return 1;
+  }
+
+  if (!BinTools::Write (aShape, a[2]))
+  {
+    di << "Cannot write to the file " << a[2];
+    return 1;
+  }
+
+  di << a[1];
+  return 0;
+}
+
+//=======================================================================
+// binrestore
+//=======================================================================
+static Standard_Integer binrestore(Draw_Interpretor& di, Standard_Integer n, const char** a)
+{
+  if (n <= 2) return 1;
+
+  TopoDS_Shape aShape;
+  if (!BinTools::Read (aShape, a[1]))
+  {
+    di << "Cannot read from the file " << a[1];
+    return 1;
+  }
+
+  DBRep::Set (a[2], aShape);
+  di << a[2];
+  return 0;
+}
+
 //=======================================================================
 //function : BasicCommands
 //purpose  : 
@@ -1323,6 +1367,13 @@ void  DBRep::BasicCommands(Draw_Interpretor& theCommands)
   
   // Add command for DRAW-specific ProgressIndicator
   theCommands.Add ( "XProgress","XProgress [+|-t] [+|-g]: switch on/off textual and graphical mode of Progress Indicator",XProgress,"DE: General");
+
+  theCommands.Add("binsave", "binsave shape filename\n"
+                  "\t\tsave the shape in the binary format file",
+                  __FILE__, binsave, g);
+  theCommands.Add("binrestore", "binrestore filename shape\n"
+                  "\t\trestore the shape from the binary format file",
+                  __FILE__, binrestore, g);
 }
 
 //=======================================================================