0030895: Coding Rules - specify std namespace explicitly for std::cout and streams
[occt.git] / src / BinTools / BinTools_SurfaceSet.cxx
1 // Created on: 2004-05-20
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_CurveSet.hxx>
19 #include <BinTools_SurfaceSet.hxx>
20 #include <Geom_BezierSurface.hxx>
21 #include <Geom_BSplineSurface.hxx>
22 #include <Geom_ConicalSurface.hxx>
23 #include <Geom_CylindricalSurface.hxx>
24 #include <Geom_OffsetSurface.hxx>
25 #include <Geom_Plane.hxx>
26 #include <Geom_RectangularTrimmedSurface.hxx>
27 #include <Geom_SphericalSurface.hxx>
28 #include <Geom_Surface.hxx>
29 #include <Geom_SurfaceOfLinearExtrusion.hxx>
30 #include <Geom_SurfaceOfRevolution.hxx>
31 #include <Geom_ToroidalSurface.hxx>
32 #include <gp_Cone.hxx>
33 #include <gp_Cylinder.hxx>
34 #include <gp_Pln.hxx>
35 #include <gp_Sphere.hxx>
36 #include <gp_Torus.hxx>
37 #include <Standard_ErrorHandler.hxx>
38 #include <Standard_Failure.hxx>
39 #include <Standard_OutOfRange.hxx>
40 #include <TColgp_Array1OfPnt.hxx>
41 #include <TColgp_Array2OfPnt.hxx>
42 #include <TColStd_Array1OfInteger.hxx>
43 #include <TColStd_Array1OfReal.hxx>
44 #include <TColStd_Array2OfReal.hxx>
45
46 #define PLANE           1
47 #define CYLINDER        2
48 #define CONE            3
49 #define SPHERE          4
50 #define TORUS           5
51 #define LINEAREXTRUSION 6
52 #define REVOLUTION      7
53 #define BEZIER          8
54 #define BSPLINE         9
55 #define RECTANGULAR     10
56 #define OFFSET          11
57
58 //=======================================================================
59 //function : BinTools_SurfaceSet
60 //purpose  : 
61 //=======================================================================
62
63 BinTools_SurfaceSet::BinTools_SurfaceSet() 
64 {
65 }
66
67
68 //=======================================================================
69 //function : Clear
70 //purpose  : 
71 //=======================================================================
72
73 void  BinTools_SurfaceSet::Clear()
74 {
75   myMap.Clear();
76 }
77
78
79 //=======================================================================
80 //function : Add
81 //purpose  : 
82 //=======================================================================
83
84 Standard_Integer  BinTools_SurfaceSet::Add(const Handle(Geom_Surface)& S)
85 {
86   return myMap.Add(S);
87 }
88
89
90 //=======================================================================
91 //function : Surface
92 //purpose  : 
93 //=======================================================================
94
95 Handle(Geom_Surface)  BinTools_SurfaceSet::Surface
96        (const Standard_Integer I)const 
97 {
98   return  Handle(Geom_Surface)::DownCast(myMap(I));
99 }
100
101
102 //=======================================================================
103 //function : Index
104 //purpose  : 
105 //=======================================================================
106
107 Standard_Integer  BinTools_SurfaceSet::Index
108   (const Handle(Geom_Surface)& S)const 
109 {
110   return myMap.FindIndex(S);
111 }
112
113 //=======================================================================
114 //function : operator << (gp_Pnt)
115 //purpose  : 
116 //=======================================================================
117
118 static Standard_OStream& operator <<(Standard_OStream& OS, const gp_Pnt P)
119 {
120   BinTools::PutReal(OS, P.X());
121   BinTools::PutReal(OS, P.Y());
122   BinTools::PutReal(OS, P.Z());
123   return OS;
124 }
125
126 //=======================================================================
127 //function : operator << (gp_Dir)
128 //purpose  : 
129 //=======================================================================
130
131 static Standard_OStream& operator <<(Standard_OStream& OS, const gp_Dir D)
132 {
133   BinTools::PutReal(OS, D.X());
134   BinTools::PutReal(OS, D.Y());
135   BinTools::PutReal(OS, D.Z());
136   return OS;
137 }
138
139
140 //=======================================================================
141 //function : operator <<(Geom_Plane)
142 //purpose  : 
143 //=======================================================================
144
145 static Standard_OStream& operator <<(Standard_OStream& OS, const Handle(Geom_Plane)& S)
146 {
147   OS << (Standard_Byte)PLANE;
148   gp_Pln P = S->Pln();
149   OS << P.Location();//Pnt
150   OS << P.Axis().Direction();
151   OS << P.XAxis().Direction();
152   OS << P.YAxis().Direction();
153   return OS;
154 }
155
156
157 //=======================================================================
158 //function : operator <<(Geom_CylindricalSurface)
159 //purpose  : 
160 //=======================================================================
161
162 static Standard_OStream& operator <<(Standard_OStream& OS, const Handle(Geom_CylindricalSurface)& S)
163 {
164   OS << (Standard_Byte)CYLINDER;
165   gp_Cylinder P = S->Cylinder();
166   OS << P.Location();//Pnt
167   OS << P.Axis().Direction();
168   OS << P.XAxis().Direction();
169   OS << P.YAxis().Direction();
170   BinTools::PutReal(OS, P.Radius());
171   return OS;
172 }
173
174
175 //=======================================================================
176 //function : operator <<(Geom_ConicalSurface)
177 //purpose  : 
178 //=======================================================================
179
180 static Standard_OStream& operator <<(Standard_OStream& OS, const Handle(Geom_ConicalSurface)& S)
181 {
182   OS << (Standard_Byte)CONE;
183   gp_Cone P = S->Cone();
184   OS << P.Location();//Pnt
185   OS << P.Axis().Direction();
186   OS << P.XAxis().Direction();
187   OS << P.YAxis().Direction();
188   BinTools::PutReal(OS, P.RefRadius());
189   BinTools::PutReal(OS, P.SemiAngle());
190   return OS;
191 }
192
193
194 //=======================================================================
195 //function : operator <<(Geom_SphericalSurface)
196 //purpose  : 
197 //=======================================================================
198
199 static Standard_OStream& operator <<(Standard_OStream& OS, const Handle(Geom_SphericalSurface)& S)
200 {
201   OS << (Standard_Byte)SPHERE;
202   gp_Sphere P = S->Sphere();
203   OS << P.Location();//Pnt
204   OS << P.Position().Axis().Direction();
205   OS << P.XAxis().Direction();
206   OS << P.YAxis().Direction();
207   BinTools::PutReal(OS, P.Radius());
208   return OS;
209 }
210
211
212 //=======================================================================
213 //function : operator <<(Geom_ToroidalSurface)
214 //purpose  : 
215 //=======================================================================
216
217 static Standard_OStream& operator <<(Standard_OStream& OS, const Handle(Geom_ToroidalSurface)& S)
218 {
219   OS << (Standard_Byte)TORUS;
220   gp_Torus P = S->Torus();
221   OS << P.Location();//Pnt
222   OS << P.Axis().Direction();
223   OS << P.XAxis().Direction();
224   OS << P.YAxis().Direction();
225   BinTools::PutReal(OS, P.MajorRadius());
226   BinTools::PutReal(OS, P.MinorRadius());
227   return OS;
228 }
229
230
231 //=======================================================================
232 //function : operator <<(Geom_SurfaceOfLinearExtrusion)
233 //purpose  : 
234 //=======================================================================
235
236 static Standard_OStream& operator <<(Standard_OStream& OS, const Handle(Geom_SurfaceOfLinearExtrusion)& S)
237 {
238   OS << (Standard_Byte)LINEAREXTRUSION;
239   OS << S->Direction();
240   BinTools_CurveSet::WriteCurve(S->BasisCurve(),OS);
241   return OS;
242 }
243
244
245 //=======================================================================
246 //function : operator <<(Geom_SurfaceOfRevolution)
247 //purpose  : 
248 //=======================================================================
249
250 static Standard_OStream& operator <<(Standard_OStream& OS, const Handle(Geom_SurfaceOfRevolution)& S)
251 {
252   OS << (Standard_Byte)REVOLUTION;
253   OS << S->Location();
254   OS << S->Direction();
255   BinTools_CurveSet::WriteCurve(S->BasisCurve(),OS);
256   return OS;
257 }
258
259
260 //=======================================================================
261 //function : operator <<(Geom_BezierSurface)
262 //purpose  : 
263 //=======================================================================
264
265 static Standard_OStream& operator <<(Standard_OStream& OS, const Handle(Geom_BezierSurface)& S)
266 {
267   OS << (Standard_Byte)BEZIER;
268   Standard_Boolean urational = S->IsURational() ? 1:0;
269   Standard_Boolean vrational = S->IsVRational() ? 1:0;
270   BinTools::PutBool(OS, urational); //rational
271   BinTools::PutBool(OS, vrational);
272 //  std::cout << "Bezier Surface:"<< std::endl;
273 //  std::cout << "\turational = "<<urational<<" vrational = " <<vrational<<std::endl;
274
275 // poles and weights
276   Standard_Integer i,j,udegree,vdegree;
277   udegree = S->UDegree();
278   vdegree = S->VDegree();
279   BinTools::PutExtChar(OS, (Standard_ExtCharacter)udegree);
280   BinTools::PutExtChar(OS, (Standard_ExtCharacter)vdegree);
281   for (i = 1; i <= udegree+1; i++) {
282     for (j = 1; j <= vdegree+1; j++) {
283       OS << S->Pole(i,j); //Pnt
284       if (urational || vrational) {
285         BinTools::PutReal(OS, S->Weight(i,j));//Real
286       }
287     }
288   }
289   return OS;
290 }
291
292
293 //=======================================================================
294 //function : operator <<(Geom_BSplineSurface)
295 //purpose  : 
296 //=======================================================================
297
298 static Standard_OStream& operator <<(Standard_OStream& OS, const Handle(Geom_BSplineSurface)& S)
299 {
300   OS << (Standard_Byte)BSPLINE;
301   Standard_Boolean urational = S->IsURational() ? 1:0;
302   Standard_Boolean vrational = S->IsVRational() ? 1:0;
303   Standard_Boolean uperiodic = S->IsUPeriodic() ? 1:0;
304   Standard_Boolean vperiodic = S->IsVPeriodic() ? 1:0;
305   BinTools::PutBool(OS, urational); 
306   BinTools::PutBool(OS, vrational); 
307   BinTools::PutBool(OS, uperiodic); 
308   BinTools::PutBool(OS, vperiodic); 
309
310 // poles and weights
311   Standard_Integer i,j,udegree,vdegree,nbupoles,nbvpoles,nbuknots,nbvknots;
312   udegree  = S->UDegree();
313   vdegree  = S->VDegree();
314   nbupoles = S->NbUPoles();
315   nbvpoles = S->NbVPoles();
316   nbuknots = S->NbUKnots();
317   nbvknots = S->NbVKnots();
318   BinTools::PutExtChar(OS, (Standard_ExtCharacter) udegree);
319   BinTools::PutExtChar(OS, (Standard_ExtCharacter) vdegree);
320   BinTools::PutInteger(OS, nbupoles);
321   BinTools::PutInteger(OS, nbvpoles);
322   BinTools::PutInteger(OS, nbuknots);
323   BinTools::PutInteger(OS, nbvknots);
324   for (i = 1; i <= nbupoles; i++) {
325     for (j = 1; j <= nbvpoles; j++) {
326       OS << S->Pole(i,j); //Pnt
327       if (urational || vrational)
328         BinTools::PutReal(OS, S->Weight(i,j));//Real    
329     }
330   }
331
332   for (i = 1; i <= nbuknots; i++) {
333     BinTools::PutReal(OS,S->UKnot(i));
334     BinTools::PutInteger(OS, S->UMultiplicity(i));
335   }
336
337   for (i = 1; i <= nbvknots; i++) {
338     BinTools::PutReal(OS,S->VKnot(i));
339     BinTools::PutInteger(OS, S->VMultiplicity(i));
340   }
341   return OS;
342 }
343
344
345 //=======================================================================
346 //function :  operator <<(Geom_RectangularTrimmedSurface)
347 //purpose  : 
348 //=======================================================================
349
350 static Standard_OStream& operator <<(Standard_OStream& OS, const Handle(Geom_RectangularTrimmedSurface)& S)
351 {
352   OS << (Standard_Byte)RECTANGULAR;
353   Standard_Real U1,U2,V1,V2;
354   S->Bounds(U1,U2,V1,V2);
355   BinTools::PutReal(OS, U1);
356   BinTools::PutReal(OS, U2);
357   BinTools::PutReal(OS, V1);
358   BinTools::PutReal(OS, V2);
359   BinTools_SurfaceSet::WriteSurface(S->BasisSurface(),OS);
360   return OS;
361 }
362
363
364 //=======================================================================
365 //function : operator <<(Geom_OffsetSurface)
366 //purpose  : 
367 //=======================================================================
368
369 static Standard_OStream& operator <<(Standard_OStream& OS, const Handle(Geom_OffsetSurface)& S)
370 {
371   OS << (Standard_Byte)OFFSET;
372   BinTools::PutReal(OS, S->Offset());
373   BinTools_SurfaceSet::WriteSurface(S->BasisSurface(),OS);
374   return OS;
375 }
376
377
378 //=======================================================================
379 //function : WriteSurface
380 //purpose  : 
381 //=======================================================================
382
383 void BinTools_SurfaceSet::WriteSurface(const Handle(Geom_Surface)& S,
384                                         Standard_OStream& OS)
385 {
386   Handle(Standard_Type) TheType = S->DynamicType();
387   try {
388     OCC_CATCH_SIGNALS
389     if ( TheType ==  STANDARD_TYPE(Geom_Plane)) {
390       OS << Handle(Geom_Plane)::DownCast(S);
391     }
392     else if ( TheType ==  STANDARD_TYPE(Geom_CylindricalSurface)) {
393       OS << Handle(Geom_CylindricalSurface)::DownCast(S);
394     }
395     else if ( TheType ==  STANDARD_TYPE(Geom_ConicalSurface)) {
396       OS << Handle(Geom_ConicalSurface)::DownCast(S);
397     }
398     else if ( TheType ==  STANDARD_TYPE(Geom_SphericalSurface)) {
399       OS << Handle(Geom_SphericalSurface)::DownCast(S);
400     }
401     else if ( TheType ==  STANDARD_TYPE(Geom_ToroidalSurface)) {
402       OS << Handle(Geom_ToroidalSurface)::DownCast(S);
403     }
404     else if ( TheType ==  STANDARD_TYPE(Geom_SurfaceOfLinearExtrusion)) {
405       OS << Handle(Geom_SurfaceOfLinearExtrusion)::DownCast(S);
406     }
407     else if ( TheType ==  STANDARD_TYPE(Geom_SurfaceOfRevolution)) {
408       OS << Handle(Geom_SurfaceOfRevolution)::DownCast(S);
409     }
410     else if ( TheType ==  STANDARD_TYPE(Geom_BezierSurface)) {
411       OS << Handle(Geom_BezierSurface)::DownCast(S);
412     }
413     else if ( TheType ==  STANDARD_TYPE(Geom_BSplineSurface)) {
414       OS << Handle(Geom_BSplineSurface)::DownCast(S);
415     }
416     else if ( TheType ==  STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
417       OS << Handle(Geom_RectangularTrimmedSurface)::DownCast(S);
418     }
419     else if ( TheType ==  STANDARD_TYPE(Geom_OffsetSurface)) {
420       OS << Handle(Geom_OffsetSurface)::DownCast(S);
421     }
422     else {
423       throw Standard_Failure("UNKNOWN SURFACE TYPE");
424     }
425   }
426    catch(Standard_Failure const& anException) {
427      Standard_SStream aMsg;
428     aMsg << "EXCEPTION in BinTools_SurfaceSet::WriteSurface(..)" << std::endl;
429     aMsg << anException << std::endl;
430     throw Standard_Failure(aMsg.str().c_str());
431   }  
432 }
433
434 //=======================================================================
435 //function : Write
436 //purpose  : 
437 //=======================================================================
438
439 void  BinTools_SurfaceSet::Write(Standard_OStream& OS)const 
440 {
441
442   Standard_Integer i, nbsurf = myMap.Extent();
443   OS << "Surfaces "<< nbsurf << "\n";
444   for (i = 1; i <= nbsurf; i++) {
445     WriteSurface(Handle(Geom_Surface)::DownCast(myMap(i)),OS);
446   }
447
448 }
449
450
451 //=======================================================================
452 //function : ReadPnt
453 //purpose  : 
454 //=======================================================================
455
456 static Standard_IStream& operator>>(Standard_IStream& IS, gp_Pnt& P)
457 {
458   Standard_Real X=0.,Y=0.,Z=0.;
459   BinTools::GetReal(IS, X);
460   BinTools::GetReal(IS, Y);
461   BinTools::GetReal(IS, Z);
462   P.SetCoord(X,Y,Z);
463   return IS;
464 }
465
466 //=======================================================================
467 //function : ReadDir
468 //purpose  : 
469 //=======================================================================
470
471 static Standard_IStream& operator>>(Standard_IStream& IS, gp_Dir& D)
472 {
473   Standard_Real X=0.,Y=0.,Z=0.;
474   BinTools::GetReal(IS, X);
475   BinTools::GetReal(IS, Y);
476   BinTools::GetReal(IS, Z);
477   D.SetCoord(X,Y,Z);
478   return IS;
479 }
480
481 //=======================================================================
482 //function : ReadAx3
483 //purpose  : 
484 //=======================================================================
485
486 static Standard_IStream& operator>>(Standard_IStream& IS, gp_Ax3& A3)
487 {
488   gp_Pnt P(0.,0.,0.);
489   gp_Dir A(1.,0.,0.),AX(1.,0.,0.),AY(1.,0.,0.);
490   IS >> P >> A >> AX >> AY;
491   gp_Ax3 ax3(P,A,AX);
492   if (AY.DotCross(A,AX) < 0)
493     ax3.YReverse();
494   A3 = ax3;
495   return IS;
496 }
497
498
499 //=======================================================================
500 //function : operator>>
501 //purpose  : 
502 //=======================================================================
503
504 static Standard_IStream& operator>>(Standard_IStream& IS,
505                                     Handle(Geom_Plane)& S)
506 {
507   gp_Ax3 A;
508   IS >> A;
509   S = new Geom_Plane(A);
510   return IS;
511 }
512
513 //=======================================================================
514 //function : operator>>
515 //purpose  : 
516 //=======================================================================
517
518 static Standard_IStream& operator>>(Standard_IStream& IS,
519                                     Handle(Geom_CylindricalSurface)& S)
520 {
521   gp_Ax3 A;
522   Standard_Real R=0.;
523   IS >> A;
524   BinTools::GetReal(IS, R);
525   S = new Geom_CylindricalSurface(A,R);
526   return IS;
527 }
528
529 //=======================================================================
530 //function : operator>>
531 //purpose  : 
532 //=======================================================================
533
534 static Standard_IStream& operator>>(Standard_IStream& IS,
535                                     Handle(Geom_ConicalSurface)& S)
536 {
537   gp_Ax3 A;
538   Standard_Real R=0.,Ang=0.;
539   IS >> A;
540   BinTools::GetReal(IS, R);
541   BinTools::GetReal(IS, Ang);
542   S = new Geom_ConicalSurface(A,Ang,R);
543   return IS;
544 }
545
546 //=======================================================================
547 //function : operator>>
548 //purpose  : 
549 //=======================================================================
550
551 static Standard_IStream& operator>>(Standard_IStream& IS,
552                                     Handle(Geom_SphericalSurface)& S)
553 {
554   gp_Ax3 A;
555   Standard_Real R=0.;
556   IS >> A;
557   BinTools::GetReal(IS, R);
558   S = new Geom_SphericalSurface(A,R);
559   return IS;
560 }
561
562 //=======================================================================
563 //function : operator>>
564 //purpose  : 
565 //=======================================================================
566
567 static Standard_IStream& operator>>(Standard_IStream& IS,
568                                     Handle(Geom_ToroidalSurface)& S)
569 {
570   gp_Ax3 A;
571   Standard_Real R1=0.,R2=0.;
572   IS >> A;
573   BinTools::GetReal(IS, R1);
574   BinTools::GetReal(IS, R2);
575   S = new Geom_ToroidalSurface(A,R1,R2);
576   return IS;
577 }
578
579 //=======================================================================
580 //function : operator>>
581 //purpose  : 
582 //=======================================================================
583
584 static Standard_IStream& operator>>(Standard_IStream& IS,
585                                     Handle(Geom_SurfaceOfLinearExtrusion)& S)
586 {
587   gp_Dir D(1.,0.,0.);
588   Handle(Geom_Curve) C;
589   IS >> D;
590   BinTools_CurveSet::ReadCurve(IS,C);
591   S = new Geom_SurfaceOfLinearExtrusion(C,D);
592   return IS;
593 }
594
595 //=======================================================================
596 //function : operator>>
597 //purpose  : 
598 //=======================================================================
599
600 static Standard_IStream& operator>>(Standard_IStream& IS,
601                                     Handle(Geom_SurfaceOfRevolution)& S)
602 {
603   gp_Pnt P(0.,0.,0.);
604   gp_Dir D(1.,0.,0.);
605   Handle(Geom_Curve) C;
606   IS >> P >> D;
607   BinTools_CurveSet::ReadCurve(IS, C);
608   S = new Geom_SurfaceOfRevolution(C,gp_Ax1(P,D));
609   return IS;
610 }
611
612 //=======================================================================
613 //function : operator>>
614 //purpose  : 
615 //=======================================================================
616
617 static Standard_IStream& operator>>(Standard_IStream& IS,
618                                     Handle(Geom_BezierSurface)& S)
619 {
620 //  std::cout << "BezierSurface:" <<std::endl;
621   Standard_Boolean urational=Standard_False, vrational=Standard_False;
622   BinTools::GetBool(IS, urational);
623   BinTools::GetBool(IS, vrational);
624
625 //  std::cout << "\turational = " << urational << " vrational = " << vrational<<std::endl;
626   Standard_Integer udegree=0, vdegree=0;
627   Standard_ExtCharacter aVal='\0';
628   BinTools::GetExtChar(IS, aVal);
629   
630   udegree = (Standard_Integer)aVal;
631   BinTools::GetExtChar(IS, aVal);
632   vdegree = (Standard_Integer)aVal;
633 //  std::cout << "\ttudegree  = " << udegree << ", vdegree = " << vdegree << std::endl;
634
635   TColgp_Array2OfPnt poles(1,udegree+1,1,vdegree+1);
636   TColStd_Array2OfReal weights(1,udegree+1,1,vdegree+1);
637   
638   Standard_Integer i,j;
639   for (i = 1; i <= udegree+1; i++) {
640     for (j = 1; j <= vdegree+1; j++) {
641       IS >> poles(i,j);//Pnt
642 //      std::cout <<"Pole X = " <<poles(i,j).X()<< " Y = " <<poles(i,j).Y()<< " Z = " << poles(i,j).Z()<<std::endl;
643       if (urational || vrational)
644         BinTools::GetReal(IS, weights(i,j));
645     }
646   }
647
648   if (urational || vrational)
649     S = new Geom_BezierSurface(poles,weights);
650   else
651     S = new Geom_BezierSurface(poles);
652   return IS;
653 }
654
655 //=======================================================================
656 //function : operator>>
657 //purpose  : 
658 //=======================================================================
659
660 static Standard_IStream& operator>>(Standard_IStream& IS,
661                                     Handle(Geom_BSplineSurface)& S)
662 {
663   Standard_Boolean urational=Standard_False, vrational=Standard_False,
664                    uperiodic=Standard_False, vperiodic=Standard_False;
665   BinTools::GetBool(IS, urational); 
666   BinTools::GetBool(IS, vrational); 
667   BinTools::GetBool(IS, uperiodic);
668   BinTools::GetBool(IS, vperiodic);
669
670   Standard_Integer udegree=0, vdegree=0,nbupoles=0,nbvpoles=0,nbuknots=0,nbvknots=0;
671   Standard_ExtCharacter aVal='\0';
672   BinTools::GetExtChar(IS, aVal);
673   udegree = (Standard_Integer)aVal;
674   BinTools::GetExtChar(IS, aVal);
675   vdegree = (Standard_Integer)aVal;
676   BinTools::GetInteger(IS, nbupoles);
677   BinTools::GetInteger(IS, nbvpoles);
678   BinTools::GetInteger(IS, nbuknots);
679   BinTools::GetInteger(IS, nbvknots);
680
681   TColgp_Array2OfPnt poles(1,nbupoles,1,nbvpoles);
682   TColStd_Array2OfReal weights(1,nbupoles,1,nbvpoles);
683   
684   Standard_Integer i,j;
685   for (i = 1; i <= nbupoles; i++) {
686     for (j = 1; j <= nbvpoles; j++) {
687       IS >> poles(i,j);//Pnt
688       if (urational || vrational)
689         BinTools::GetReal(IS, weights(i,j)); 
690     }
691   }
692
693   TColStd_Array1OfReal uknots(1,nbuknots);
694   TColStd_Array1OfInteger umults(1,nbuknots);
695   for (i = 1; i <= nbuknots; i++) {
696     BinTools::GetReal(IS, uknots(i));
697     BinTools::GetInteger(IS, umults(i));
698   }
699
700   TColStd_Array1OfReal vknots(1,nbvknots);
701   TColStd_Array1OfInteger vmults(1,nbvknots);
702   for (i = 1; i <= nbvknots; i++) {
703     BinTools::GetReal(IS, vknots(i));
704     BinTools::GetInteger(IS, vmults(i));
705   }
706
707   if (urational || vrational)
708     S = new Geom_BSplineSurface(poles,weights,uknots,vknots,umults,vmults,
709                                 udegree,vdegree,uperiodic,vperiodic);
710   else
711     S = new Geom_BSplineSurface(poles,uknots,vknots,umults,vmults,
712                                 udegree,vdegree,uperiodic,vperiodic);
713   return IS;
714 }
715
716 //=======================================================================
717 //function : operator>>
718 //purpose  : 
719 //=======================================================================
720
721 static Standard_IStream& operator>>(Standard_IStream& IS,
722                                     Handle(Geom_RectangularTrimmedSurface)& S)
723 {
724   Standard_Real U1=0.,U2=0.,V1=0.,V2=0.;
725   BinTools::GetReal(IS, U1);
726   BinTools::GetReal(IS, U2);
727   BinTools::GetReal(IS, V1);
728   BinTools::GetReal(IS, V2);
729   Handle(Geom_Surface) BS;
730   BinTools_SurfaceSet::ReadSurface(IS, BS);
731   S = new Geom_RectangularTrimmedSurface(BS,U1,U2,V1,V2);
732   return IS;
733 }
734
735 //=======================================================================
736 //function : operator>>
737 //purpose  : 
738 //=======================================================================
739
740 static Standard_IStream& operator>>(Standard_IStream& IS,
741                                     Handle(Geom_OffsetSurface)& S)
742 {
743   Standard_Real O=0.;
744   BinTools::GetReal(IS, O);
745   Handle(Geom_Surface) BS;
746   BinTools_SurfaceSet::ReadSurface(IS, BS);
747   S = new Geom_OffsetSurface(BS,O);
748   return IS;
749 }
750
751
752 //=======================================================================
753 //function : ReadSurface
754 //purpose  : 
755 //=======================================================================
756
757 Standard_IStream& BinTools_SurfaceSet::ReadSurface(Standard_IStream& IS,
758                                                     Handle(Geom_Surface)& S)
759 {
760   try {
761     OCC_CATCH_SIGNALS
762     const Standard_Byte stype = (Standard_Byte) IS.get();
763     switch (stype) {
764
765     case PLANE :
766       {
767         Handle(Geom_Plane) SS;
768         IS >> SS;
769         S = SS;
770       }
771       break;
772
773     case CYLINDER :
774       {
775         Handle(Geom_CylindricalSurface) SS;
776         IS >> SS;
777         S = SS;
778       }
779       break;
780
781     case CONE :
782       {
783         Handle(Geom_ConicalSurface) SS;
784         IS >> SS;
785         S = SS;
786       }
787       break;
788
789     case SPHERE :
790       {
791         Handle(Geom_SphericalSurface) SS;
792         IS >> SS;
793         S = SS;
794       }
795       break;
796
797     case TORUS :
798       {
799         Handle(Geom_ToroidalSurface) SS;
800         IS >> SS;
801         S = SS;
802       }
803       break;
804
805     case LINEAREXTRUSION :
806       {
807         Handle(Geom_SurfaceOfLinearExtrusion) SS;
808         IS >> SS;
809         S = SS;
810       }
811       break;
812
813     case REVOLUTION :
814       {
815         Handle(Geom_SurfaceOfRevolution) SS;
816         IS >> SS;
817         S = SS;
818       }
819       break;
820
821     case BEZIER :
822       {
823         Handle(Geom_BezierSurface) SS;
824         IS >> SS;
825         S = SS;
826       }
827       break;
828
829     case BSPLINE :
830       {
831         Handle(Geom_BSplineSurface) SS;
832         IS >> SS;
833         S = SS;
834       }
835       break;
836
837     case RECTANGULAR :
838       {
839         Handle(Geom_RectangularTrimmedSurface) SS;
840         IS >> SS;
841         S = SS;
842       }
843       break;
844
845     case OFFSET :
846       {
847         Handle(Geom_OffsetSurface) SS;
848         IS >> SS;
849         S = SS;
850       }
851       break;
852       
853     default :
854       {
855         S = NULL;
856         throw Standard_Failure("UNKNOWN SURFACE TYPE");
857       }
858       break;
859     }
860   }
861   catch(Standard_Failure const& anException) {
862     S = NULL;
863     Standard_SStream aMsg;
864     aMsg << "EXCEPTION in BinTools_SurfaceSet::ReadSurface(..)" << std::endl;
865     aMsg << anException << std::endl;
866     throw Standard_Failure(aMsg.str().c_str());
867   }
868   return IS;
869 }
870
871 //=======================================================================
872 //function : Read
873 //purpose  : 
874 //=======================================================================
875
876 void  BinTools_SurfaceSet::Read(Standard_IStream& IS)
877 {
878   char buffer[255];
879   IS >> buffer;
880   if (IS.fail() || strcmp(buffer,"Surfaces")) {
881     Standard_SStream aMsg;
882     aMsg << "BinTools_SurfaceSet::Read:  Not a surface table"<<std::endl;
883 #ifdef OCCT_DEBUG
884     std::cout <<"SurfaceSet buffer: " << buffer << std::endl;
885 #endif
886     throw Standard_Failure(aMsg.str().c_str());
887     return;
888   }
889
890   Handle(Geom_Surface) S;
891   Standard_Integer i, nbsurf;
892   IS >> nbsurf;
893   IS.get ();//remove <lf>
894   for (i = 1; i <= nbsurf; i++) {
895     BinTools_SurfaceSet::ReadSurface(IS,S);
896     myMap.Add(S);
897   }
898 }
899