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