0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / BinTools / BinTools.cxx
1 // Created on: 2004-05-18
2 // Created by: Sergey ZARITCHNY
3 // Copyright (c) 2004-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
17 #include <BinTools.hxx>
18 #include <BinTools_ShapeSet.hxx>
19 #include <FSD_FileHeader.hxx>
20 #include <OSD_FileSystem.hxx>
21 #include <Storage_StreamTypeMismatchError.hxx>
22
23 //=======================================================================
24 //function : PutBool
25 //purpose  : 
26 //=======================================================================
27 Standard_OStream& BinTools::PutBool(Standard_OStream& OS, const Standard_Boolean aValue)
28 {
29   OS.put((Standard_Byte)(aValue ? 1 : 0));
30   return OS;
31 }
32
33
34 //=======================================================================
35 //function : PutInteger
36 //purpose  : 
37 //=======================================================================
38
39 Standard_OStream& BinTools::PutInteger(Standard_OStream& OS, const Standard_Integer aValue)
40 {
41   Standard_Integer anIntValue = aValue;
42 #ifdef DO_INVERSE
43       anIntValue = InverseInt (aValue);
44 #endif
45   OS.write ((char*)&anIntValue, sizeof (Standard_Integer));  
46   return OS;
47 }
48
49
50 //=======================================================================
51 //function : PutReal
52 //purpose  :
53 //=======================================================================
54 Standard_OStream& BinTools::PutReal (Standard_OStream& theOS,
55                                      const Standard_Real& theValue)
56 {
57 #ifdef DO_INVERSE
58   const Standard_Real aRValue = InverseReal (theValue);
59   theOS.write ((char*)&aRValue, sizeof (Standard_Real));
60 #else
61   theOS.write ((char*)&theValue, sizeof (Standard_Real));
62 #endif
63   return theOS;
64 }
65
66 //=======================================================================
67 //function : PutShortReal
68 //purpose  :
69 //=======================================================================
70 Standard_OStream& BinTools::PutShortReal (Standard_OStream& theOS,
71                                           const Standard_ShortReal& theValue)
72 {
73 #ifdef DO_INVERSE
74   const Standard_ShortReal aValue = InverseShortReal (theValue);
75   theOS.write ((char*)&aValue, sizeof(Standard_ShortReal));
76 #else
77   theOS.write ((char*)&theValue, sizeof(Standard_ShortReal));
78 #endif
79   return theOS;
80 }
81
82 //=======================================================================
83 //function : PutExtChar
84 //purpose  : 
85 //=======================================================================
86
87 Standard_OStream& BinTools::PutExtChar(Standard_OStream& OS, const Standard_ExtCharacter aValue)
88 {
89   Standard_ExtCharacter aSValue = aValue;
90 #ifdef DO_INVERSE
91       aSValue = InverseExtChar (aValue);
92 #endif
93   OS.write((char*)&aSValue, sizeof(Standard_ExtCharacter));  
94   return OS;
95 }
96
97 //=======================================================================
98 //function : GetReal
99 //purpose  :
100 //=======================================================================
101 Standard_IStream& BinTools::GetReal (Standard_IStream& theIS,
102                                      Standard_Real& theValue)
103 {
104   if (!theIS.read ((char*)&theValue, sizeof(Standard_Real)))
105   {
106     throw Storage_StreamTypeMismatchError();
107   }
108 #ifdef DO_INVERSE
109   theValue = InverseReal (theValue);
110 #endif
111   return theIS;
112 }
113
114 //=======================================================================
115 //function : GetShortReal
116 //purpose  :
117 //=======================================================================
118 Standard_IStream& BinTools::GetShortReal (Standard_IStream& theIS,
119                                           Standard_ShortReal& theValue)
120 {
121   if (!theIS.read ((char*)&theValue, sizeof(Standard_ShortReal)))
122   {
123     throw Storage_StreamTypeMismatchError();
124   }
125 #ifdef DO_INVERSE
126   theValue = InverseShortReal (theValue);
127 #endif
128   return theIS;
129 }
130
131 //=======================================================================
132 //function : GetInteger
133 //purpose  : 
134 //=======================================================================
135
136 Standard_IStream& BinTools::GetInteger(Standard_IStream& IS, Standard_Integer& aValue)
137 {
138   if(!IS.read ((char*)&aValue, sizeof(Standard_Integer)))
139     throw Storage_StreamTypeMismatchError();
140 #ifdef DO_INVERSE
141   aValue = InverseInt (aValue);
142 #endif
143   return IS;
144 }
145
146 //=======================================================================
147 //function : GetExtChar
148 //purpose  : 
149 //=======================================================================
150
151 Standard_IStream& BinTools::GetExtChar(Standard_IStream& IS, Standard_ExtCharacter& theValue)
152 {
153   if(!IS.read ((char*)&theValue, sizeof(Standard_ExtCharacter)))
154     throw Storage_StreamTypeMismatchError();
155 #ifdef DO_INVERSE
156   theValue = InverseExtChar (theValue);
157 #endif
158   return IS;
159 }
160
161 //=======================================================================
162 //function : GetBool
163 //purpose  : 
164 //=======================================================================
165
166 Standard_IStream& BinTools::GetBool(Standard_IStream& IS, Standard_Boolean& aValue)
167 {
168   aValue = (IS.get() != 0);
169   return IS;
170 }
171
172 //=======================================================================
173 //function : Write
174 //purpose  :
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)
182 {
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);
190 }
191
192 //=======================================================================
193 //function : Read
194 //purpose  : 
195 //=======================================================================
196
197 void BinTools::Read (TopoDS_Shape& theShape, Standard_IStream& theStream,
198                      const Message_ProgressRange& theRange)
199 {
200   BinTools_ShapeSet aShapeSet;
201   aShapeSet.SetWithTriangles(Standard_True);
202   aShapeSet.Read (theStream, theRange);
203   aShapeSet.ReadSubs (theShape, theStream, aShapeSet.NbShapes());
204 }
205
206 //=======================================================================
207 //function : Write
208 //purpose  :
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)
216 {
217   const Handle(OSD_FileSystem)& aFileSystem = OSD_FileSystem::DefaultFileSystem();
218   std::shared_ptr<std::ostream> aStream = aFileSystem->OpenOStream (theFile, std::ios::out | std::ios::binary);
219   aStream->precision (15);
220   if (aStream.get() == NULL || !aStream->good())
221     return Standard_False;
222
223   Write (theShape, *aStream, theWithTriangles, theWithNormals, theVersion, theRange);
224   aStream->flush();
225   return aStream->good();
226 }
227
228 //=======================================================================
229 //function : Read
230 //purpose  : 
231 //=======================================================================
232
233 Standard_Boolean BinTools::Read (TopoDS_Shape& theShape, const Standard_CString theFile,
234                                  const Message_ProgressRange& theRange)
235 {
236   const Handle(OSD_FileSystem)& aFileSystem = OSD_FileSystem::DefaultFileSystem();
237   std::shared_ptr<std::istream> aStream = aFileSystem->OpenIStream (theFile, std::ios::in | std::ios::binary);
238   if (aStream.get() == NULL)
239   {
240     return Standard_False;
241   }
242
243   Read (theShape, *aStream, theRange);
244   return aStream->good();
245 }