1 // Created on: 2004-05-18
2 // Created by: Sergey ZARITCHNY
3 // Copyright (c) 2004-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
17 #include <BinTools.hxx>
18 #include <BinTools_ShapeSet.hxx>
19 #include <FSD_FileHeader.hxx>
20 #include <OSD_OpenFile.hxx>
21 #include <Storage_StreamTypeMismatchError.hxx>
23 //=======================================================================
26 //=======================================================================
27 Standard_OStream& BinTools::PutBool(Standard_OStream& OS, const Standard_Boolean aValue)
29 OS.put((Standard_Byte)(aValue ? 1 : 0));
34 //=======================================================================
35 //function : PutInteger
37 //=======================================================================
39 Standard_OStream& BinTools::PutInteger(Standard_OStream& OS, const Standard_Integer aValue)
41 Standard_Integer anIntValue = aValue;
43 anIntValue = InverseInt (aValue);
45 OS.write((char*)&anIntValue, sizeof(Standard_Integer));
50 //=======================================================================
53 //=======================================================================
54 Standard_OStream& BinTools::PutReal (Standard_OStream& theOS,
55 const Standard_Real& theValue)
58 const Standard_Real aRValue = InverseReal (theValue);
59 theOS.write((char*)&aRValue, sizeof(Standard_Real));
61 theOS.write((char*)&theValue, sizeof(Standard_Real));
66 //=======================================================================
67 //function : PutShortReal
69 //=======================================================================
70 Standard_OStream& BinTools::PutShortReal (Standard_OStream& theOS,
71 const Standard_ShortReal& theValue)
74 const Standard_ShortReal aValue = InverseShortReal (theValue);
75 theOS.write ((char*)&aValue, sizeof(Standard_ShortReal));
77 theOS.write ((char*)&theValue, sizeof(Standard_ShortReal));
82 //=======================================================================
83 //function : PutExtChar
85 //=======================================================================
87 Standard_OStream& BinTools::PutExtChar(Standard_OStream& OS, const Standard_ExtCharacter aValue)
89 Standard_ExtCharacter aSValue = aValue;
91 aSValue = InverseExtChar (aValue);
93 OS.write((char*)&aSValue, sizeof(Standard_ExtCharacter));
97 //=======================================================================
100 //=======================================================================
101 Standard_IStream& BinTools::GetReal (Standard_IStream& theIS,
102 Standard_Real& theValue)
104 if (!theIS.read ((char*)&theValue, sizeof(Standard_Real)))
106 throw Storage_StreamTypeMismatchError();
109 theValue = InverseReal (theValue);
114 //=======================================================================
115 //function : GetShortReal
117 //=======================================================================
118 Standard_IStream& BinTools::GetShortReal (Standard_IStream& theIS,
119 Standard_ShortReal& theValue)
121 if (!theIS.read ((char*)&theValue, sizeof(Standard_ShortReal)))
123 throw Storage_StreamTypeMismatchError();
126 theValue = InverseShortReal (theValue);
131 //=======================================================================
132 //function : GetInteger
134 //=======================================================================
136 Standard_IStream& BinTools::GetInteger(Standard_IStream& IS, Standard_Integer& aValue)
138 if(!IS.read ((char*)&aValue, sizeof(Standard_Integer)))
139 throw Storage_StreamTypeMismatchError();
141 aValue = InverseInt (aValue);
146 //=======================================================================
147 //function : GetExtChar
149 //=======================================================================
151 Standard_IStream& BinTools::GetExtChar(Standard_IStream& IS, Standard_ExtCharacter& theValue)
153 if(!IS.read ((char*)&theValue, sizeof(Standard_ExtCharacter)))
154 throw Storage_StreamTypeMismatchError();
156 theValue = InverseExtChar (theValue);
161 //=======================================================================
164 //=======================================================================
166 Standard_IStream& BinTools::GetBool(Standard_IStream& IS, Standard_Boolean& aValue)
168 aValue = (IS.get() != 0);
172 //=======================================================================
175 //=======================================================================
176 void BinTools::Write (const TopoDS_Shape& theShape,
177 Standard_OStream& theStream,
178 const Standard_Boolean theWithTriangles,
179 const Standard_Boolean theWithNormals,
180 const BinTools_FormatVersion theVersion,
181 const Message_ProgressRange& theRange)
183 BinTools_ShapeSet aShapeSet;
184 aShapeSet.SetWithTriangles(theWithTriangles);
185 aShapeSet.SetWithNormals(theWithNormals);
186 aShapeSet.SetFormatNb (theVersion);
187 aShapeSet.Add (theShape);
188 aShapeSet.Write (theStream, theRange);
189 aShapeSet.Write (theShape, theStream);
192 //=======================================================================
195 //=======================================================================
197 void BinTools::Read (TopoDS_Shape& theShape, Standard_IStream& theStream,
198 const Message_ProgressRange& theRange)
200 BinTools_ShapeSet aShapeSet;
201 aShapeSet.SetWithTriangles(Standard_True);
202 aShapeSet.Read (theStream, theRange);
203 aShapeSet.Read (theShape, theStream, aShapeSet.NbShapes());
206 //=======================================================================
209 //=======================================================================
210 Standard_Boolean BinTools::Write (const TopoDS_Shape& theShape,
211 const Standard_CString theFile,
212 const Standard_Boolean theWithTriangles,
213 const Standard_Boolean theWithNormals,
214 const BinTools_FormatVersion theVersion,
215 const Message_ProgressRange& theRange)
217 std::ofstream aStream;
218 aStream.precision (15);
219 OSD_OpenStream (aStream, theFile, std::ios::out | std::ios::binary);
221 return Standard_False;
223 Write (theShape, aStream, theWithTriangles, theWithNormals, theVersion, theRange);
225 return aStream.good();
228 //=======================================================================
231 //=======================================================================
233 Standard_Boolean BinTools::Read (TopoDS_Shape& theShape, const Standard_CString theFile,
234 const Message_ProgressRange& theRange)
237 OSD_OpenStream (aBuf, theFile, std::ios::in | std::ios::binary);
239 return Standard_False;
241 Standard_IStream aStream (&aBuf);
242 Read (theShape, aStream, theRange);
243 return aStream.good();