0024171: Eliminate CLang compiler warning -Wreorder
[occt.git] / src / BinTools / BinTools_ShapeSet.cxx
1 // Created on: 2004-05-11
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_ShapeSet.ixx>
23 #include <Standard_ErrorHandler.hxx>
24 #include <Precision.hxx>
25 #include <TColStd_HArray1OfReal.hxx>
26 #include <TColStd_HArray1OfInteger.hxx>
27 #include <TColgp_Array1OfPnt2d.hxx>
28 #include <gp_Trsf.hxx>
29 #include <Poly_PolygonOnTriangulation.hxx>
30 #include <Poly_Polygon3D.hxx>
31 #include <Poly_Triangulation.hxx>
32 #include <BRepTools.hxx>
33 #include <BRep_Tool.hxx>
34 #include <BRep_TVertex.hxx>
35 #include <BRep_TEdge.hxx>
36 #include <BRep_ListIteratorOfListOfPointRepresentation.hxx>
37 #include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
38 #include <BRep_PointRepresentation.hxx>
39 #include <BRep_CurveRepresentation.hxx>
40 #include <BRep_CurveOnClosedSurface.hxx>
41 #include <BRep_Polygon3D.hxx>
42 #include <BRep_PolygonOnTriangulation.hxx>
43 #include <BRep_PointOnCurve.hxx>
44 #include <BRep_PointOnCurveOnSurface.hxx>
45 #include <BRep_PointOnSurface.hxx>
46 #include <BRep_GCurve.hxx>
47 #include <BRep_TFace.hxx>
48 #include <TopoDS.hxx>
49 #include <TopoDS_Iterator.hxx>
50 #include <TopoDS_Vertex.hxx>
51 #include <BinTools.hxx>
52 #include <BinTools_Curve2dSet.hxx>
53 #include <BinTools_CurveSet.hxx>
54 #include <BinTools_SurfaceSet.hxx>
55 #include <string.h>
56 //#define MDTV_DEB 1
57 const char* Version_1  = "Open CASCADE Topology V1 (c)";
58 const char* Version_2  = "Open CASCADE Topology V2 (c)";
59 const char* Version_3  = "Open CASCADE Topology V3 (c)";
60 //=======================================================================
61 //function : operator << (gp_Pnt)
62 //purpose  : 
63 //=======================================================================
64
65 static Standard_OStream& operator <<(Standard_OStream& OS, const gp_Pnt P)
66 {
67   BinTools::PutReal(OS, P.X());
68   BinTools::PutReal(OS, P.Y());
69   BinTools::PutReal(OS, P.Z());
70   return OS;
71 }
72 //=======================================================================
73 //function : BinTools_ShapeSet
74 //purpose  : 
75 //=======================================================================
76
77 BinTools_ShapeSet::BinTools_ShapeSet(const Standard_Boolean isWithTriangles)
78      :myFormatNb(3), myWithTriangles(isWithTriangles)
79 {}
80
81 //=======================================================================
82 //function : Delete
83 //purpose  : 
84 //=======================================================================
85
86 void BinTools_ShapeSet::Delete()
87 {}
88
89 //=======================================================================
90 //function : SetFormatNb
91 //purpose  : 
92 //=======================================================================
93 void BinTools_ShapeSet::SetFormatNb(const Standard_Integer theFormatNb)
94 {
95   myFormatNb = theFormatNb;
96 }
97
98 //=======================================================================
99 //function : FormatNb
100 //purpose  : 
101 //=======================================================================
102 Standard_Integer BinTools_ShapeSet::FormatNb() const
103 {
104   return myFormatNb;
105 }
106
107 //=======================================================================
108 //function : Clear
109 //purpose  : 
110 //=======================================================================
111
112 void  BinTools_ShapeSet::Clear()
113 {
114   mySurfaces.Clear();
115   myCurves.Clear();
116   myCurves2d.Clear();
117   myPolygons3D.Clear();
118   myPolygons2D.Clear();
119   myNodes.Clear();
120   myTriangulations.Clear();
121   myShapes.Clear();
122   myLocations.Clear();
123 }
124 //=======================================================================
125 //function : Add
126 //purpose  : 
127 //=======================================================================
128
129 Standard_Integer  BinTools_ShapeSet::Add(const TopoDS_Shape& theShape)
130 {
131   if (theShape.IsNull()) return 0;
132   myLocations.Add(theShape.Location());
133   TopoDS_Shape aS2 = theShape;
134   aS2.Location(TopLoc_Location());
135   Standard_Integer anIndex = myShapes.FindIndex(aS2);
136   if (anIndex == 0) {
137     AddGeometry(aS2);
138     for (TopoDS_Iterator its(aS2,Standard_False,Standard_False);its.More(); its.Next())
139       Add(its.Value());
140     anIndex = myShapes.Add(aS2);
141   }
142   return anIndex;
143 }
144
145
146 //=======================================================================
147 //function : Shape
148 //purpose  : 
149 //=======================================================================
150
151 const TopoDS_Shape&  BinTools_ShapeSet::Shape(const Standard_Integer theIndx)const 
152 {
153   return myShapes(theIndx);
154 }
155
156 //=======================================================================
157 //function : Index
158 //purpose  : 
159 //=======================================================================
160
161 Standard_Integer BinTools_ShapeSet::Index(const TopoDS_Shape& theShape) const
162 {
163   return myShapes.FindIndex(theShape);
164 }
165
166 //=======================================================================
167 //function : Locations
168 //purpose  : 
169 //=======================================================================
170
171 const BinTools_LocationSet&  BinTools_ShapeSet::Locations()const 
172 {
173   return myLocations;
174 }
175
176
177 //=======================================================================
178 //function : ChangeLocations
179 //purpose  : 
180 //=======================================================================
181
182 BinTools_LocationSet&  BinTools_ShapeSet::ChangeLocations()
183 {
184   return myLocations;
185 }
186
187 //=======================================================================
188 //function : AddGeometry
189 //purpose  : 
190 //=======================================================================
191
192 void BinTools_ShapeSet::AddGeometry(const TopoDS_Shape& S)
193 {
194   // Add the geometry
195   
196   if (S.ShapeType() == TopAbs_VERTEX) {
197     
198     Handle(BRep_TVertex) TV = Handle(BRep_TVertex)::DownCast(S.TShape());
199     BRep_ListIteratorOfListOfPointRepresentation itrp(TV->Points());
200     
201     while (itrp.More()) {
202       const Handle(BRep_PointRepresentation)& PR = itrp.Value();
203
204       if (PR->IsPointOnCurve()) {
205         myCurves.Add(PR->Curve());
206       }
207
208       else if (PR->IsPointOnCurveOnSurface()) {
209         myCurves2d.Add(PR->PCurve());
210         mySurfaces.Add(PR->Surface());
211       }
212       
213       else if (PR->IsPointOnSurface()) {
214         mySurfaces.Add(PR->Surface());
215       }
216
217       ChangeLocations().Add(PR->Location());
218       itrp.Next();
219     }
220
221   }
222   else if (S.ShapeType() == TopAbs_EDGE) {
223
224     // Add the curve geometry
225     Handle(BRep_TEdge) TE = Handle(BRep_TEdge)::DownCast(S.TShape());
226     BRep_ListIteratorOfListOfCurveRepresentation itrc(TE->Curves());
227
228     while (itrc.More()) {
229       const Handle(BRep_CurveRepresentation)& CR = itrc.Value();
230       if (CR->IsCurve3D()) {
231         if (!CR->Curve3D().IsNull()) {
232           myCurves.Add(CR->Curve3D());
233           ChangeLocations().Add(CR->Location());
234         }
235       }
236       else if (CR->IsCurveOnSurface()) {
237         mySurfaces.Add(CR->Surface());
238         myCurves2d.Add(CR->PCurve());
239         ChangeLocations().Add(CR->Location());
240         if (CR->IsCurveOnClosedSurface())
241           myCurves2d.Add(CR->PCurve2());
242       }
243       else if (CR->IsRegularity()) {
244         mySurfaces.Add(CR->Surface());
245         ChangeLocations().Add(CR->Location());
246         mySurfaces.Add(CR->Surface2());
247         ChangeLocations().Add(CR->Location2());
248       }
249       else if (myWithTriangles) { 
250         if (CR->IsPolygon3D()) {
251           if (!CR->Polygon3D().IsNull()) {
252             myPolygons3D.Add(CR->Polygon3D());
253             ChangeLocations().Add(CR->Location());
254           }
255         }
256         else if (CR->IsPolygonOnTriangulation()) {
257           myTriangulations.Add(CR->Triangulation());
258           myNodes.Add(CR->PolygonOnTriangulation());
259           ChangeLocations().Add(CR->Location());
260           if (CR->IsPolygonOnClosedTriangulation())
261             myNodes.Add(CR->PolygonOnTriangulation2());
262         }
263         else if (CR->IsPolygonOnSurface()) {
264           mySurfaces.Add(CR->Surface());
265           myPolygons2D.Add(CR->Polygon());
266           ChangeLocations().Add(CR->Location());
267           if (CR->IsPolygonOnClosedSurface())
268           myPolygons2D.Add(CR->Polygon2());
269         }
270       }
271       itrc.Next();
272     }
273   }
274
275   else if (S.ShapeType() == TopAbs_FACE) {
276
277     // Add the surface geometry
278     Handle(BRep_TFace) TF = Handle(BRep_TFace)::DownCast(S.TShape());
279     if (!TF->Surface().IsNull())  mySurfaces.Add(TF->Surface());
280
281     if (myWithTriangles) {
282       Handle(Poly_Triangulation) Tr = TF->Triangulation();
283       if (!Tr.IsNull()) myTriangulations.Add(Tr);
284     }
285
286     ChangeLocations().Add(TF->Location());
287   }
288 }
289
290 //=======================================================================
291 //function : WriteGeometry
292 //purpose  : 
293 //=======================================================================
294
295 void  BinTools_ShapeSet::WriteGeometry(Standard_OStream& OS)const 
296 {
297   myCurves2d.Write(OS); 
298   myCurves.Write(OS);
299   WritePolygon3D(OS);
300   WritePolygonOnTriangulation(OS);
301   mySurfaces.Write(OS);
302   WriteTriangulation(OS);
303 }
304
305 //=======================================================================
306 //function : Write
307 //purpose  : 
308 //=======================================================================
309
310 void  BinTools_ShapeSet::Write(Standard_OStream& OS)const 
311 {
312
313   // write the copyright
314   if (myFormatNb == 3)
315     OS << "\n" << Version_3 << endl;
316   else if (myFormatNb == 2)
317     OS << "\n" << Version_2 << endl;
318   else
319     OS << "\n" << Version_1 << endl;
320
321   //-----------------------------------------
322   // write the locations
323   //-----------------------------------------
324
325   myLocations.Write(OS);
326
327   //-----------------------------------------
328   // write the geometry
329   //-----------------------------------------
330
331   WriteGeometry(OS);
332
333   //-----------------------------------------
334   // write the shapes
335   //-----------------------------------------
336
337   Standard_Integer i, nbShapes = myShapes.Extent();
338   
339   OS << "\nTShapes " << nbShapes << "\n";
340   
341   // subshapes are written first
342   for (i = 1; i <= nbShapes; i++) {
343
344     const TopoDS_Shape& S = myShapes(i);
345     
346     // Type
347     OS << (Standard_Byte)S.ShapeType();
348
349     // Geometry
350     WriteGeometry(S,OS);
351
352     // Flags
353     BinTools::PutBool(OS, S.Free()? 1:0);
354     BinTools::PutBool(OS, S.Modified()? 1:0);
355     BinTools::PutBool(OS, S.Checked()? 1:0);
356     BinTools::PutBool(OS, S.Orientable()? 1:0);
357     BinTools::PutBool(OS, S.Closed()? 1:0);
358     BinTools::PutBool(OS, S.Infinite()? 1:0);
359     BinTools::PutBool(OS, S.Convex()? 1:0);
360
361     // sub-shapes
362
363     TopoDS_Iterator its(S,Standard_False,Standard_False);
364     while (its.More()) {
365       Write(its.Value(),OS);
366       its.Next();
367     }
368     Write(TopoDS_Shape(),OS); // Null shape to end the list
369   }
370   
371 }
372
373 //=======================================================================
374 //function : Read
375 //purpose  : 
376 //=======================================================================
377
378 void  BinTools_ShapeSet::Read(Standard_IStream& IS)
379 {
380
381   Clear();
382
383   // Check the version
384   char vers[101];
385   do {
386     IS.getline(vers,100,'\n');
387     // BUC60769 PTV 18.10.2000: remove possible '\r' at the end of the line
388     
389     for (Standard_Size lv = (strlen(vers)- 1); lv > 1 && (vers[lv] == '\r' || vers[lv] == '\n') ;lv--) 
390       vers[lv] = '\0';
391     
392   } while ( ! IS.fail() && strcmp(vers,Version_1) && strcmp(vers,Version_2) &&
393            strcmp(vers,Version_3));
394   if (IS.fail()) {
395     cout << "BinTools_ShapeSet::Read: File was not written with this version of the topology"<<endl;
396      return;
397   }
398
399   if (strcmp(vers,Version_3) == 0) SetFormatNb(3);
400   else  if (strcmp(vers,Version_2) == 0) SetFormatNb(2);    
401   else SetFormatNb(1);
402
403   //-----------------------------------------
404   // read the locations
405   //-----------------------------------------
406
407   myLocations.Read(IS);
408   //-----------------------------------------
409   // read the geometry
410   //-----------------------------------------
411
412   ReadGeometry(IS);
413
414   //-----------------------------------------
415   // read the shapes
416   //-----------------------------------------
417
418   char buffer[255];
419   IS >> buffer;
420   if (IS.fail() || strcmp(buffer,"TShapes")) {
421     Standard_SStream aMsg;
422     aMsg << "BinTools_ShapeSet::Read: Not a TShape table"<<endl;
423     Standard_Failure::Raise(aMsg);    
424     return;
425   }
426
427   Standard_Integer i, nbShapes;
428   IS >> nbShapes;
429   IS.get();//remove lf 
430
431   for (i = 1; i <= nbShapes; i++) {
432
433     TopoDS_Shape S;
434     
435     //Read type and create empty shape.
436
437     TopAbs_ShapeEnum T = (TopAbs_ShapeEnum) IS.get();
438
439     ReadGeometry(T,IS,S);
440     
441     // Set the flags
442     Standard_Boolean aFree, aMod, aChecked, anOrient, aClosed, anInf, aConv;
443     BinTools::GetBool(IS, aFree);
444     BinTools::GetBool(IS, aMod);
445     BinTools::GetBool(IS, aChecked);
446     BinTools::GetBool(IS, anOrient);
447     BinTools::GetBool(IS, aClosed);
448     BinTools::GetBool(IS, anInf);
449     BinTools::GetBool(IS, aConv);
450
451     // sub-shapes
452     TopoDS_Shape SS;
453     do {
454       Read(SS,IS,nbShapes);
455       if (!SS.IsNull())
456         AddShapes(S,SS);
457     } while(!SS.IsNull());
458
459     S.Free(aFree);
460     S.Modified(aMod);
461      if (myFormatNb >= 2)
462        S.Checked(aChecked);
463      else
464        S.Checked   (Standard_False);     // force check at reading.. 
465     S.Orientable(anOrient);
466     S.Closed    (aClosed);
467     S.Infinite  (anInf);
468     S.Convex    (aConv);
469     // check
470
471     if (myFormatNb == 1)
472       if(T == TopAbs_FACE) {
473         const TopoDS_Face& F = TopoDS::Face(S);
474         BRepTools::Update(F);
475       }
476     myShapes.Add(S);
477   }
478 }
479
480 //=======================================================================
481 //function : Write
482 //purpose  : 
483 //=======================================================================
484
485 void  BinTools_ShapeSet::Write(const TopoDS_Shape& S, Standard_OStream& OS)const 
486 {
487   if (S.IsNull()) 
488
489     OS << '*';
490   else {    
491 // {TopAbs_FORWARD, TopAbs_REVERSED, TopAbs_INTERNAL, TopAbs_EXTERNAL} 
492     OS << (Standard_Byte) S.Orientation();
493     BinTools::PutInteger(OS, myShapes.Extent() - myShapes.FindIndex(S.Located(TopLoc_Location())) + 1);
494     BinTools::PutInteger(OS, Locations().Index(S.Location()));
495   }    
496 }
497
498 //=======================================================================
499 //function : Read
500 //purpose  : 
501 //=======================================================================
502
503 void  BinTools_ShapeSet::Read(TopoDS_Shape& S, Standard_IStream& IS,
504                               const Standard_Integer nbshapes)const 
505 {
506   Standard_Character aChar;
507   IS >> aChar;
508   if(aChar == '*')
509     S = TopoDS_Shape();
510   else {
511     TopAbs_Orientation anOrient;
512     anOrient = (TopAbs_Orientation)aChar;
513     Standard_Integer anIndx;
514     BinTools::GetInteger(IS, anIndx);
515     S = myShapes(nbshapes - anIndx + 1);
516     S.Orientation(anOrient);
517
518     Standard_Integer l;
519     BinTools::GetInteger(IS, l);
520     S.Location(myLocations.Location(l));
521   }
522 }
523
524 //=======================================================================
525 //function : ReadGeometry
526 //purpose  : 
527 //=======================================================================
528
529 void  BinTools_ShapeSet::ReadGeometry(Standard_IStream& IS)
530 {
531   myCurves2d.Read(IS);
532   myCurves.Read(IS);
533   ReadPolygon3D(IS);
534   ReadPolygonOnTriangulation(IS);
535   mySurfaces.Read(IS);
536   ReadTriangulation(IS);
537 }
538
539 //=======================================================================
540 //function : WriteGeometry
541 //purpose  : 
542 //=======================================================================
543
544 void  BinTools_ShapeSet::WriteGeometry(const TopoDS_Shape& S, 
545                                         Standard_OStream&   OS)const 
546 {
547 // Write the geometry
548   try {
549     OCC_CATCH_SIGNALS
550     if (S.ShapeType() == TopAbs_VERTEX) {
551     
552 // Write the point geometry
553       TopoDS_Vertex V = TopoDS::Vertex(S);
554       BinTools::PutReal(OS, BRep_Tool::Tolerance(V));
555       gp_Pnt p = BRep_Tool::Pnt(V);
556       OS << p;
557 #ifdef MDTV_DEB
558       Standard_Integer aPos;
559 #endif
560       Handle(BRep_TVertex) TV = Handle(BRep_TVertex)::DownCast(S.TShape());
561       BRep_ListIteratorOfListOfPointRepresentation itrp(TV->Points());
562       while (itrp.More()) {
563         const Handle(BRep_PointRepresentation)& PR = itrp.Value();
564 //      BinTools::PutReal(OS, PR->Parameter());
565         if (PR->IsPointOnCurve()) {
566 #ifdef MDTV_DEB
567           aPos = OS.tellp();
568 #endif
569           OS << (Standard_Byte)1; // 1
570           BinTools::PutReal(OS, PR->Parameter());
571           BinTools::PutInteger(OS, myCurves.Index(PR->Curve()));
572         }
573
574         else if (PR->IsPointOnCurveOnSurface()) {
575 #ifdef MDTV_DEB
576           aPos = OS.tellp();
577 #endif
578           OS << (Standard_Byte)2;// 2
579           BinTools::PutReal(OS, PR->Parameter());
580           BinTools::PutInteger(OS, myCurves2d.Index(PR->PCurve()));
581           BinTools::PutInteger(OS, mySurfaces.Index(PR->Surface()));
582         }
583
584         else if (PR->IsPointOnSurface()) {
585 #ifdef MDTV_DEB
586           aPos = OS.tellp();
587 #endif
588           OS << (Standard_Byte)3;// 3
589           BinTools::PutReal(OS, PR->Parameter2());
590           BinTools::PutReal(OS, PR->Parameter());
591           BinTools::PutInteger(OS, mySurfaces.Index(PR->Surface()));
592         }
593         BinTools::PutInteger(OS, Locations().Index(PR->Location()));
594         itrp.Next();
595       }
596     
597 //    OS << "0 0\n"; // end representations
598       OS.put((Standard_Byte)0);
599     }
600
601     else if (S.ShapeType() == TopAbs_EDGE) {
602
603     // Write the curve geometry 
604
605       Handle(BRep_TEdge) TE = Handle(BRep_TEdge)::DownCast(S.TShape());
606
607       BinTools::PutReal(OS, TE->Tolerance());
608
609       Standard_Boolean aVal = (TE->SameParameter()) ? Standard_True : Standard_False;
610       BinTools::PutBool(OS, aVal);   
611       aVal = (TE->SameRange()) ? Standard_True : Standard_False;
612       BinTools::PutBool(OS, aVal);
613       aVal = (TE->Degenerated())  ? Standard_True : Standard_False;
614       BinTools::PutBool(OS, aVal);
615       
616       Standard_Real first, last;
617       BRep_ListIteratorOfListOfCurveRepresentation itrc = TE->Curves();
618       while (itrc.More()) {
619         const Handle(BRep_CurveRepresentation)& CR = itrc.Value();
620         if (CR->IsCurve3D()) {
621           if (!CR->Curve3D().IsNull()) {
622             Handle(BRep_GCurve) GC = Handle(BRep_GCurve)::DownCast(itrc.Value());
623             GC->Range(first, last);
624             OS << (Standard_Byte)1;//CURVE_3D;
625             BinTools::PutInteger(OS, myCurves.Index(CR->Curve3D()));
626             BinTools::PutInteger(OS, Locations().Index(CR->Location()));
627             BinTools::PutReal(OS, first);
628             BinTools::PutReal(OS, last);
629           }
630         }
631         else if (CR->IsCurveOnSurface()) {
632           Handle(BRep_GCurve) GC = Handle(BRep_GCurve)::DownCast(itrc.Value());
633           GC->Range(first, last);
634           if (!CR->IsCurveOnClosedSurface())
635 // -2- Curve on surf
636             OS << (Standard_Byte)2;
637           else
638 // -3- Curve on closed surf
639             OS << (Standard_Byte)3;
640           BinTools::PutInteger(OS, myCurves2d.Index(CR->PCurve()));
641           if (CR->IsCurveOnClosedSurface()) {//+ int|char
642             BinTools::PutInteger(OS, myCurves2d.Index(CR->PCurve2()));
643             OS << (Standard_Byte)CR->Continuity();
644           }
645           BinTools::PutInteger(OS, mySurfaces.Index(CR->Surface()));
646           BinTools::PutInteger(OS, Locations().Index(CR->Location()));
647           BinTools::PutReal(OS, first);
648           BinTools::PutReal(OS, last);
649
650         // Write UV Points for higher performance
651           if (FormatNb() >= 2)
652             {
653               gp_Pnt2d Pf,Pl;
654               if (CR->IsCurveOnClosedSurface()) {
655                 Handle(BRep_CurveOnClosedSurface) COCS = 
656                   Handle(BRep_CurveOnClosedSurface)::DownCast(CR);
657                 COCS->UVPoints2(Pf,Pl);
658               }
659               else {
660                 Handle(BRep_CurveOnSurface) COS = 
661                   Handle(BRep_CurveOnSurface)::DownCast(CR);
662                 COS->UVPoints(Pf,Pl);
663               }
664               BinTools::PutReal(OS, Pf.X());
665               BinTools::PutReal(OS, Pf.Y());
666               BinTools::PutReal(OS, Pl.X());
667               BinTools::PutReal(OS, Pl.Y());
668             }
669         }
670         else if (CR->IsRegularity()) {
671 // -4- Regularity
672           OS << (Standard_Byte)4;
673           OS << (Standard_Byte)CR->Continuity();
674           BinTools::PutInteger(OS, mySurfaces.Index(CR->Surface()));
675           BinTools::PutInteger(OS, Locations().Index(CR->Location()));
676           BinTools::PutInteger(OS, mySurfaces.Index(CR->Surface2()));
677           BinTools::PutInteger(OS, Locations().Index(CR->Location2()));
678           
679         }
680
681         else if (myWithTriangles) { 
682           if (CR->IsPolygon3D()) {
683             Handle(BRep_Polygon3D) GC = Handle(BRep_Polygon3D)::DownCast(itrc.Value());
684             if (!GC->Polygon3D().IsNull()) {
685 // -5- Polygon3D
686               OS << (Standard_Byte)5;
687               BinTools::PutInteger(OS, myPolygons3D.FindIndex(CR->Polygon3D()));
688               BinTools::PutInteger(OS, Locations().Index(CR->Location())); 
689             }
690           }
691           else if (CR->IsPolygonOnTriangulation()) {
692             Handle(BRep_PolygonOnTriangulation) PT = 
693               Handle(BRep_PolygonOnTriangulation)::DownCast(itrc.Value());
694             if (!CR->IsPolygonOnClosedTriangulation())
695 // -6- Polygon on triangulation
696               OS << (Standard_Byte)6;
697             else
698 // -7- Polygon on closed triangulation
699               OS << (Standard_Byte)7;
700             BinTools::PutInteger(OS, myNodes.FindIndex(PT->PolygonOnTriangulation()));
701             
702             if (CR->IsPolygonOnClosedTriangulation()) {
703               BinTools::PutInteger(OS, myNodes.FindIndex(PT->PolygonOnTriangulation2()));
704             }
705             BinTools::PutInteger(OS, myTriangulations.FindIndex(PT->Triangulation()));
706             BinTools::PutInteger(OS, Locations().Index(CR->Location()));
707           }
708         }
709         
710         itrc.Next();
711       }
712 //   OS << "0\n"; // end of the list of representations
713
714       OS << (Standard_Byte)0;
715     }
716   
717     else if (S.ShapeType() == TopAbs_FACE) {
718
719       Handle(BRep_TFace) TF = Handle(BRep_TFace)::DownCast(S.TShape());
720       const TopoDS_Face& F = TopoDS::Face(S);
721       
722       if (!(TF->Surface()).IsNull()) {
723         Standard_Boolean aNatRes = (BRep_Tool::NaturalRestriction(F)) ? Standard_True : Standard_False;
724         BinTools::PutBool(OS, aNatRes);
725
726       // Write the surface geometry
727         BinTools::PutReal(OS, TF->Tolerance());
728         BinTools::PutInteger(OS, mySurfaces.Index(TF->Surface()));
729         BinTools::PutInteger(OS, Locations().Index(TF->Location()));
730       }
731       if (myWithTriangles) {
732         if (!(TF->Triangulation()).IsNull()) {
733           OS << (Standard_Byte) 2;
734         // Write the triangulation
735           BinTools::PutInteger(OS, myTriangulations.FindIndex(TF->Triangulation())); 
736         } else
737           OS << (Standard_Byte) 1;
738       } else
739         OS << (Standard_Byte) 0;//without triangulation
740     }
741   }
742   catch(Standard_Failure) {
743     Standard_SStream aMsg;
744     aMsg << "EXCEPTION in BinTools_ShapeSet::WriteGeometry(S,OS)" << endl;
745     Handle(Standard_Failure) anExc = Standard_Failure::Caught();
746     aMsg << anExc << endl;
747     Standard_Failure::Raise(aMsg);
748   }
749 }
750
751 //=======================================================================
752 //function : ReadGeometry
753 //purpose  : 
754 //=======================================================================
755
756 void  BinTools_ShapeSet::ReadGeometry(const TopAbs_ShapeEnum T, 
757                                        Standard_IStream&      IS, 
758                                        TopoDS_Shape&          S)
759 {
760   // Read the geometry
761
762   Standard_Integer val, c,pc,pc2 = 0,s,s2,l,l2,t, pt, pt2 = 0;
763   Standard_Real tol,X,Y,Z,first,last,p1 = 0.,p2;
764   Standard_Real PfX,PfY,PlX,PlY;
765   gp_Pnt2d aPf, aPl;
766   Standard_Boolean closed, bval;
767   Standard_SStream aMsg;
768   GeomAbs_Shape reg = GeomAbs_C0;
769   try {
770     OCC_CATCH_SIGNALS
771     switch (T) {
772
773
774     //---------
775     // vertex
776     //---------
777
778     case TopAbs_VERTEX :
779       {
780 //       Standard_Integer aPos = IS.tellg();      
781 //       cout << "\nPOS = " << aPos << endl;
782         TopoDS_Vertex& V = TopoDS::Vertex(S);
783
784       // Read the point geometry
785         BinTools::GetReal(IS, tol);
786         BinTools::GetReal(IS, X);
787         BinTools::GetReal(IS, Y);
788         BinTools::GetReal(IS, Z);
789         myBuilder.MakeVertex(V,gp_Pnt(X,Y,Z),tol);
790         Handle(BRep_TVertex) TV = Handle(BRep_TVertex)::DownCast(V.TShape());
791
792         BRep_ListOfPointRepresentation& lpr = TV->ChangePoints();
793         TopLoc_Location L;
794         Standard_Boolean aNewF = (myFormatNb > 2) ? Standard_True : Standard_False;
795 #ifdef MDTV_DEB
796         gp_Pnt aPnt = gp_Pnt(X,Y,Z);
797 #endif
798         do {
799           if(aNewF) {
800             val = (Standard_Integer)IS.get();//case {0|1|2|3}
801             if (val > 0 && val <= 3) 
802               BinTools::GetReal(IS, p1); 
803           } else {
804         streampos aPos = IS.tellg();
805             BinTools::GetReal(IS, p1);      
806             val = (Standard_Integer)IS.get();//case {0|1|2|3}
807 #ifdef MDTV_DEB
808             cout << "\nVal = " << val <<endl;   
809 #endif    
810             if(val != 1 && val !=2 && val !=3){
811               IS.seekg(aPos);
812               val = (Standard_Integer)IS.get();
813               if (val > 0 && val <= 3) 
814                 BinTools::GetReal(IS, p1);
815             }
816           }
817           Handle(BRep_PointRepresentation) PR;
818           switch (val) {
819           case 0 :
820             break;
821             
822           case 1 :
823             {
824               BinTools::GetInteger(IS, c);
825               if (myCurves.Curve(c).IsNull())
826                 break;
827               Handle(BRep_PointOnCurve) POC =
828                 new BRep_PointOnCurve(p1,
829                                       myCurves.Curve(c),
830                                       L);
831               PR = POC;
832             }
833             break;
834             
835           case 2 :
836             {
837               BinTools::GetInteger(IS, pc);
838               BinTools::GetInteger(IS, s);
839               if (myCurves2d.Curve2d(pc).IsNull() ||
840                   mySurfaces.Surface(s).IsNull())
841                 break;
842               
843               Handle(BRep_PointOnCurveOnSurface) POC =
844                 new BRep_PointOnCurveOnSurface(p1,
845                                                myCurves2d.Curve2d(pc),
846                                                mySurfaces.Surface(s),
847                                                L);
848               PR = POC;
849             }
850             break;
851             
852           case 3 :
853             {
854               BinTools::GetReal(IS, p2);
855               BinTools::GetInteger(IS, s);
856               if (mySurfaces.Surface(s).IsNull())
857                 break;
858               
859               Handle(BRep_PointOnSurface) POC =
860                 new BRep_PointOnSurface(p1,p2,
861                                         mySurfaces.Surface(s),
862                                         L);
863               PR = POC;
864             }
865             break;
866             
867           default:
868             {
869               aMsg << "BinTools_SurfaceSet::ReadGeometry: UnExpected BRep_PointRepresentation = "<< val <<endl;
870               Standard_Failure::Raise(aMsg);
871               }
872           }
873           
874           if (val > 0) {
875             BinTools::GetInteger(IS, l);//Locations index
876             
877             if (!PR.IsNull()) {
878               PR->Location(Locations().Location(l));
879               lpr.Append(PR);
880             }
881           }
882         } while (val > 0);
883       }
884       break;
885       
886
887       //---------
888       // edge
889       //---------
890
891
892     case TopAbs_EDGE :
893
894       // Create an edge
895       {
896         TopoDS_Edge& E = TopoDS::Edge(S);
897         
898         myBuilder.MakeEdge(E);
899         
900         // Read the curve geometry 
901         BinTools::GetReal(IS, tol);
902         BinTools::GetBool(IS, bval);
903         myBuilder.SameParameter(E, bval);
904
905         BinTools::GetBool(IS, bval);
906         myBuilder.SameRange(E,bval);
907
908         BinTools::GetBool(IS, bval);
909         myBuilder.Degenerated(E,bval);
910         
911         do {
912           val = (Standard_Integer)IS.get();//{0|1|2|3|4|5|6|7}
913           // -0- no representation
914           // -1- Curve 3D
915           // -2- Curve on surf
916           // -3- Curve on closed surf
917           // -4- Regularity
918           // -5- Polygon3D
919           // -6- Polygon on triangulation
920           // -7- Polygon on closed triangulation
921
922           switch (val) {
923           case 0:
924             break;
925
926           case 1 :                               // -1- Curve 3D
927             BinTools::GetInteger(IS, c);
928             BinTools::GetInteger(IS, l);
929             if (!myCurves.Curve(c).IsNull()) {
930               myBuilder.UpdateEdge(E,myCurves.Curve(c),
931                                    Locations().Location(l),tol);
932             }
933             BinTools::GetReal(IS, first);
934             BinTools::GetReal(IS, last);
935             if (!myCurves.Curve(c).IsNull()) {
936               Standard_Boolean Only3d = Standard_True;
937               myBuilder.Range(E,first,last,Only3d);
938             }
939             break;
940             
941             
942           case 2 : // -2- Curve on surf
943           case 3 : // -3- Curve on closed surf
944             closed = (val == 3);
945              BinTools::GetInteger(IS, pc);
946             if (closed) {
947               BinTools::GetInteger(IS, pc2);
948               reg = (GeomAbs_Shape)IS.get();
949             }
950
951             // surface, location
952             BinTools::GetInteger(IS, s);
953             BinTools::GetInteger(IS, l);
954
955             // range
956             BinTools::GetReal(IS, first);
957             BinTools::GetReal(IS, last);
958
959             // read UV Points // for XML Persistence higher performance
960             if (FormatNb() >= 2)
961             {
962               BinTools::GetReal(IS, PfX);
963               BinTools::GetReal(IS, PfY);
964               BinTools::GetReal(IS, PlX);
965               BinTools::GetReal(IS, PlY);
966               aPf = gp_Pnt2d(PfX,PfY);
967               aPl = gp_Pnt2d(PlX,PlY);
968             }
969
970             if (myCurves2d.Curve2d(pc).IsNull() ||
971                 (closed && myCurves2d.Curve2d(pc2).IsNull()) ||
972                 mySurfaces.Surface(s).IsNull())
973               break;
974             
975             if (closed) {
976               if (FormatNb() >= 2)
977                 myBuilder.UpdateEdge(E,myCurves2d.Curve2d(pc),
978                                      myCurves2d.Curve2d(pc2),
979                                      mySurfaces.Surface(s),
980                                      Locations().Location(l),tol,
981                                      aPf, aPl);
982               else
983                 myBuilder.UpdateEdge(E,myCurves2d.Curve2d(pc),
984                                      myCurves2d.Curve2d(pc2),
985                                      mySurfaces.Surface(s),
986                                      Locations().Location(l),tol);
987
988               myBuilder.Continuity(E,
989                                    mySurfaces.Surface(s),
990                                    mySurfaces.Surface(s),
991                                    Locations().Location(l),
992                                    Locations().Location(l),
993                                    reg);
994             }
995             else
996             {
997               if (FormatNb() >= 2)
998                 myBuilder.UpdateEdge(E,myCurves2d.Curve2d(pc),
999                                      mySurfaces.Surface(s),
1000                                      Locations().Location(l),tol,
1001                                      aPf, aPl);
1002               else
1003                 myBuilder.UpdateEdge(E,myCurves2d.Curve2d(pc),
1004                                      mySurfaces.Surface(s),
1005                                      Locations().Location(l),tol);
1006             }
1007             myBuilder.Range(E,
1008                             mySurfaces.Surface(s),
1009                             Locations().Location(l),
1010                             first,last);
1011             break;
1012             
1013           case 4 : // -4- Regularity
1014             reg = (GeomAbs_Shape)IS.get();
1015             BinTools::GetInteger(IS, s);
1016             BinTools::GetInteger(IS, l);
1017             BinTools::GetInteger(IS, s2);
1018             BinTools::GetInteger(IS, l2);
1019             if (mySurfaces.Surface(s).IsNull() ||
1020                 mySurfaces.Surface(s2).IsNull())
1021               break;
1022             myBuilder.Continuity(E,
1023                                  mySurfaces.Surface(s),
1024                                  mySurfaces.Surface(s2),
1025                                  Locations().Location(l),
1026                                  Locations().Location(l2),
1027                                  reg);
1028             break;
1029             
1030           case 5 : // -5- Polygon3D                     
1031             BinTools::GetInteger(IS, c);
1032             BinTools::GetInteger(IS, l);
1033 //??? Bug?  myBuilder.UpdateEdge(E,Handle(Poly_Polygon3D)::DownCast(myPolygons3D(c)));
1034             myBuilder.UpdateEdge(E,Handle(Poly_Polygon3D)::DownCast(myPolygons3D(c)), Locations().Location(l));
1035             break;
1036
1037           case 6 : // -6- Polygon on triangulation
1038           case 7 : // -7- Polygon on closed triangulation
1039             closed = (val == 7);
1040             BinTools::GetInteger(IS, pt);
1041             if (closed) 
1042               BinTools::GetInteger(IS, pt2);
1043
1044             BinTools::GetInteger(IS, t);
1045             BinTools::GetInteger(IS, l);
1046             if (closed) {
1047               myBuilder.UpdateEdge
1048                 (E, Handle(Poly_PolygonOnTriangulation)::DownCast(myNodes(pt)),
1049                  Handle(Poly_PolygonOnTriangulation)::DownCast(myNodes(pt2)),
1050                  Handle(Poly_Triangulation)::DownCast(myTriangulations(t)),
1051                  Locations().Location(l));
1052             }
1053             else {
1054               myBuilder.UpdateEdge
1055                 (E,Handle(Poly_PolygonOnTriangulation)::DownCast(myNodes(pt)),
1056                  Handle(Poly_Triangulation)::DownCast(myTriangulations(t)),
1057                  Locations().Location(l));
1058             }
1059             // range            
1060             break;
1061           default:
1062             {
1063               aMsg <<"Unexpected Curve Representation ="<< val << endl;
1064               Standard_Failure::Raise(aMsg);
1065             }
1066             
1067           }
1068         } while (val > 0);
1069       }
1070       break;
1071
1072
1073     //---------
1074     // wire
1075     //---------
1076
1077     case TopAbs_WIRE :
1078       myBuilder.MakeWire(TopoDS::Wire(S));
1079       break;
1080
1081
1082     //---------
1083     // face
1084     //---------
1085
1086     case TopAbs_FACE :
1087       {
1088     // create a face :
1089         TopoDS_Face& F = TopoDS::Face(S);
1090         myBuilder.MakeFace(F);
1091         BinTools::GetBool(IS, bval); //NaturalRestriction flag
1092         BinTools::GetReal(IS, tol);
1093         BinTools::GetInteger(IS, s); //surface indx
1094         BinTools::GetInteger(IS, l); //location indx
1095         if (!mySurfaces.Surface(s).IsNull()) {
1096           myBuilder.UpdateFace(TopoDS::Face(S),
1097                                mySurfaces.Surface(s),
1098                                Locations().Location(l),tol);
1099           myBuilder.NaturalRestriction(TopoDS::Face(S),bval );
1100         }
1101     
1102         Standard_Byte aByte = (Standard_Byte)IS.get();
1103       // cas triangulation
1104         if(aByte == 2) {
1105           BinTools::GetInteger(IS, s);
1106           myBuilder.UpdateFace(TopoDS::Face(S),
1107                                Handle(Poly_Triangulation)::DownCast(myTriangulations(s)));
1108         }
1109       }
1110       break;
1111
1112
1113     //---------
1114     // shell
1115     //---------
1116
1117     case TopAbs_SHELL :
1118       myBuilder.MakeShell(TopoDS::Shell(S));
1119       break;
1120
1121
1122     //---------
1123     // solid
1124     //---------
1125
1126     case TopAbs_SOLID :
1127       myBuilder.MakeSolid(TopoDS::Solid(S));
1128       break;
1129
1130
1131     //---------
1132     // compsolid
1133     //---------
1134
1135     case TopAbs_COMPSOLID :
1136       myBuilder.MakeCompSolid(TopoDS::CompSolid(S));
1137       break;
1138
1139
1140     //---------
1141     // compound
1142     //---------
1143
1144     case TopAbs_COMPOUND :
1145       myBuilder.MakeCompound(TopoDS::Compound(S));
1146       break;
1147
1148     default:
1149       aMsg << "Unexpected topology type = "<< T <<endl;
1150       Standard_Failure::Raise(aMsg);
1151       break;
1152     }
1153   }
1154   catch(Standard_Failure) {
1155     aMsg << "EXCEPTION in BinTools_ShapeSet::ReadGeometry(S,OS)" << endl;
1156     Handle(Standard_Failure) anExc = Standard_Failure::Caught();
1157     aMsg << anExc << endl;
1158     Standard_Failure::Raise(aMsg);
1159   }
1160 }
1161
1162
1163
1164 //=======================================================================
1165 //function : AddShapes
1166 //purpose  : 
1167 //=======================================================================
1168
1169 void  BinTools_ShapeSet::AddShapes(TopoDS_Shape&       S1, 
1170                                     const TopoDS_Shape& S2)
1171 {
1172   myBuilder.Add(S1,S2);
1173 }
1174
1175
1176 //=======================================================================
1177 //function : WritePolygonOnTriangulation
1178 //purpose  : 
1179 //=======================================================================
1180
1181 void BinTools_ShapeSet::WritePolygonOnTriangulation(Standard_OStream& OS) const
1182 {
1183   Standard_Integer i, j, nbpOntri = myNodes.Extent();
1184
1185   OS << "PolygonOnTriangulations " << nbpOntri << endl;
1186   Handle(Poly_PolygonOnTriangulation) Poly;
1187   Handle(TColStd_HArray1OfReal) Param;
1188   try {
1189     OCC_CATCH_SIGNALS
1190     for (i=1; i<=nbpOntri; i++) {
1191       Poly = Handle(Poly_PolygonOnTriangulation)::DownCast(myNodes(i));
1192       const TColStd_Array1OfInteger& Nodes = Poly->Nodes();
1193       BinTools::PutInteger(OS, Nodes.Length());
1194       for (j=1; j <= Nodes.Length(); j++) 
1195         BinTools::PutInteger(OS,  Nodes.Value(j));
1196
1197     // writing parameters:
1198       Param = Poly->Parameters();
1199
1200     // write the deflection
1201       BinTools::PutReal(OS, Poly->Deflection());
1202       if (!Param.IsNull()) {
1203         BinTools::PutBool(OS, Standard_True);
1204         for (j=1; j <= Param->Length(); j++) 
1205           BinTools::PutReal(OS, Param->Value(j)); 
1206       }
1207       else 
1208         BinTools::PutBool(OS, Standard_False);
1209     }
1210   }
1211   catch(Standard_Failure) {
1212     Standard_SStream aMsg;
1213     aMsg << "EXCEPTION in BinTools_ShapeSet::WritePolygonOnTriangulation(..)" << endl;
1214     Handle(Standard_Failure) anExc = Standard_Failure::Caught();
1215     aMsg << anExc << endl;
1216     Standard_Failure::Raise(aMsg);
1217   }
1218 }
1219
1220 //=======================================================================
1221 //function : ReadPolygonOnTriangulation
1222 //purpose  : 
1223 //=======================================================================
1224
1225 void BinTools_ShapeSet::ReadPolygonOnTriangulation(Standard_IStream& IS)
1226 {
1227   char buffer[255];
1228   IS >> buffer;
1229   Standard_SStream aMsg;
1230   if (IS.fail() || (strstr(buffer,"PolygonOnTriangulations") == NULL)) {
1231     aMsg << "BinTools_ShapeSet::ReadPolygonOnTriangulation: Not a PolygonOnTriangulation section" <<endl;
1232     Standard_Failure::Raise(aMsg);
1233   }
1234   Standard_Integer i, j, val, nbpol = 0, nbnodes =0;
1235   Standard_Boolean hasparameters;
1236   Standard_Real par;
1237   Handle(TColStd_HArray1OfReal) Param;
1238   Handle(Poly_PolygonOnTriangulation) Poly;
1239   IS >> nbpol;
1240   IS.get();//remove LF 
1241 #ifdef DEB
1242 //  cout << "ReadPolygonOnTriangulation: NbPoles = "<< nbpol<< endl;
1243 #endif
1244   try {
1245     OCC_CATCH_SIGNALS
1246     for (i=1; i<=nbpol; i++) {
1247 #ifdef DEB
1248 //    streampos pos = IS.tellg();
1249 //    cout << "ReadPolygonOnTriangulation: Pos = "<< pos << endl;
1250 #endif
1251       BinTools::GetInteger(IS, nbnodes);
1252
1253 #ifdef DEB
1254 //    cout << "ReadPolygonOnTriangulation: PoleIndx = "<< i << " NbOfNodes = "<< nbnodes <<endl;
1255 #endif
1256
1257       TColStd_Array1OfInteger Nodes(1, nbnodes);
1258       for (j = 1; j <= nbnodes; j++) {
1259         BinTools::GetInteger(IS, val);
1260         Nodes(j) = val;
1261       }
1262       Standard_Real def;
1263       BinTools::GetReal(IS, def);
1264       BinTools::GetBool(IS, hasparameters);
1265       if (hasparameters) {
1266         TColStd_Array1OfReal Param1(1, nbnodes);
1267         for (j = 1; j <= nbnodes; j++) {
1268           BinTools::GetReal(IS, par);
1269           Param1(j) = par;
1270         }
1271         Poly = new Poly_PolygonOnTriangulation(Nodes, Param1);
1272       }
1273       else Poly = new Poly_PolygonOnTriangulation(Nodes);
1274       Poly->Deflection(def);
1275       myNodes.Add(Poly);
1276     }
1277   }
1278   catch(Standard_Failure) {
1279     aMsg << "EXCEPTION in BinTools_ShapeSet::ReadPolygonOnTriangulation(..)" << endl;
1280     Handle(Standard_Failure) anExc = Standard_Failure::Caught();
1281     aMsg << anExc << endl;
1282     Standard_Failure::Raise(aMsg);
1283   }
1284 }
1285
1286
1287
1288 //=======================================================================
1289 //function : WritePolygon3D
1290 //purpose  : 
1291 //=======================================================================
1292
1293 void BinTools_ShapeSet::WritePolygon3D(Standard_OStream& OS)const
1294 {
1295   Standard_Integer i, j, nbpol = myPolygons3D.Extent();
1296   OS << "Polygon3D " << nbpol << endl;
1297   Handle(Poly_Polygon3D) P;
1298   try {
1299     OCC_CATCH_SIGNALS
1300     for (i = 1; i <= nbpol; i++) {
1301       P = Handle(Poly_Polygon3D)::DownCast(myPolygons3D(i));
1302       BinTools::PutInteger(OS, P->NbNodes());
1303       BinTools::PutBool(OS, P->HasParameters()? 1:0);
1304
1305     // write the deflection
1306       BinTools::PutReal(OS, P->Deflection());
1307
1308     // write the nodes
1309       Standard_Integer i1, nbNodes = P->NbNodes();
1310       const TColgp_Array1OfPnt& Nodes = P->Nodes();
1311       for (j = 1; j <= nbNodes; j++) {
1312         BinTools::PutReal(OS, Nodes(j).X());
1313         BinTools::PutReal(OS, Nodes(j).Y());
1314         BinTools::PutReal(OS, Nodes(j).Z());
1315       }
1316       if (P->HasParameters()) {
1317         const TColStd_Array1OfReal& Param = P->Parameters();
1318         for ( i1 = 1; i1 <= nbNodes; i1++ ) {
1319           BinTools::PutReal(OS, Param(i1));
1320         }
1321       }
1322     }
1323   }
1324   catch(Standard_Failure) {
1325     Standard_SStream aMsg;
1326     aMsg << "EXCEPTION in BinTools_ShapeSet::WritePolygon3D(..)" << endl;
1327     Handle(Standard_Failure) anExc = Standard_Failure::Caught();
1328     aMsg << anExc << endl;
1329     Standard_Failure::Raise(aMsg);
1330   }
1331 }
1332 //=======================================================================
1333 //function : ReadPolygon3D
1334 //purpose  : 
1335 //=======================================================================
1336
1337 void BinTools_ShapeSet::ReadPolygon3D(Standard_IStream& IS)
1338 {
1339   char buffer[255];
1340   Standard_Integer i, j, p, nbpol=0, nbnodes =0;
1341   Standard_Boolean hasparameters = Standard_False;
1342   Standard_Real d, x, y, z;
1343   IS >> buffer;
1344   Standard_SStream aMsg;
1345
1346   if (IS.fail() || strstr(buffer,"Polygon3D") == NULL) {
1347     aMsg << "BinTools_ShapeSet::ReadPolygon3D: Not a Polygon3D section" <<endl;
1348 #ifdef DEB
1349     cout <<"Buffer: " << buffer << endl;
1350 #endif
1351     Standard_Failure::Raise(aMsg);
1352   }
1353   Handle(Poly_Polygon3D) P;
1354   IS >> nbpol;
1355   IS.get();//remove LF 
1356
1357   try {
1358     OCC_CATCH_SIGNALS
1359     for (i=1; i<=nbpol; i++) {
1360       BinTools::GetInteger(IS, nbnodes);
1361       BinTools::GetBool(IS, hasparameters);
1362       TColgp_Array1OfPnt Nodes(1, nbnodes);
1363       BinTools::GetReal(IS, d);
1364       for (j = 1; j <= nbnodes; j++) {
1365         BinTools::GetReal(IS, x);
1366         BinTools::GetReal(IS, y);
1367         BinTools::GetReal(IS, z);
1368         Nodes(j).SetCoord(x,y,z);
1369       }
1370       if (hasparameters) {
1371         TColStd_Array1OfReal Param(1,nbnodes);
1372         for (p = 1; p <= nbnodes; p++) 
1373           BinTools::GetReal(IS, Param(p));
1374
1375         P = new Poly_Polygon3D(Nodes, Param);
1376       }
1377       else P = new Poly_Polygon3D(Nodes);
1378       P->Deflection(d);
1379       myPolygons3D.Add(P);
1380     }
1381   }
1382   catch(Standard_Failure) {
1383     aMsg << "EXCEPTION in BinTools_ShapeSet::ReadPolygon3D(..)" << endl;
1384     Handle(Standard_Failure) anExc = Standard_Failure::Caught();
1385     aMsg << anExc << endl;
1386     Standard_Failure::Raise(aMsg);
1387   }
1388 }
1389
1390
1391 //=======================================================================
1392 //function : WriteTriangulation
1393 //purpose  : 
1394 //=======================================================================
1395
1396 void BinTools_ShapeSet::WriteTriangulation(Standard_OStream& OS) const
1397 {
1398   Standard_Integer i, j, nbNodes, nbtri = myTriangulations.Extent();
1399   Standard_Integer nbTriangles = 0, n1, n2, n3;
1400     OS << "Triangulations " << nbtri << endl;
1401   Handle(Poly_Triangulation) T;
1402   try {
1403     OCC_CATCH_SIGNALS
1404     for (i = 1; i <= nbtri; i++) {
1405       T = Handle(Poly_Triangulation)::DownCast(myTriangulations(i));
1406       BinTools::PutInteger(OS, T->NbNodes());
1407       BinTools::PutInteger(OS, T->NbTriangles());
1408       BinTools::PutBool(OS, T->HasUVNodes()? 1:0);
1409     // write the deflection
1410       BinTools::PutReal(OS, T->Deflection());
1411
1412     // write the 3d nodes
1413       nbNodes = T->NbNodes();
1414       const TColgp_Array1OfPnt& Nodes = T->Nodes();
1415       for (j = 1; j <= nbNodes; j++) {
1416         BinTools::PutReal(OS, Nodes(j).X());
1417         BinTools::PutReal(OS, Nodes(j).Y());
1418         BinTools::PutReal(OS, Nodes(j).Z());
1419       }
1420     
1421       if (T->HasUVNodes()) {
1422         const TColgp_Array1OfPnt2d& UVNodes = T->UVNodes();
1423         for (j = 1; j <= nbNodes; j++) {
1424           BinTools::PutReal(OS, UVNodes(j).X());
1425           BinTools::PutReal(OS, UVNodes(j).Y());
1426         }
1427       }
1428       nbTriangles = T->NbTriangles();
1429       const Poly_Array1OfTriangle& Triangles = T->Triangles();
1430       for (j = 1; j <= nbTriangles; j++) {
1431         Triangles(j).Get(n1, n2, n3);
1432         BinTools::PutInteger(OS, n1);
1433         BinTools::PutInteger(OS, n2);
1434         BinTools::PutInteger(OS, n3);
1435       }
1436     }
1437   }
1438   catch(Standard_Failure) {
1439     Standard_SStream aMsg;
1440     aMsg << "EXCEPTION in BinTools_ShapeSet::WriteTriangulation(..)" << endl;
1441     Handle(Standard_Failure) anExc = Standard_Failure::Caught();
1442     aMsg << anExc << endl;
1443     Standard_Failure::Raise(aMsg);
1444   }
1445 }
1446
1447 //=======================================================================
1448 //function : ReadTriangulation
1449 //purpose  : 
1450 //=======================================================================
1451
1452 void BinTools_ShapeSet::ReadTriangulation(Standard_IStream& IS)
1453 {
1454   char buffer[255];
1455   Standard_Integer i, j, nbtri =0;
1456   Standard_Real d, x, y, z;
1457   Standard_Integer nbNodes =0, nbTriangles=0;
1458   Standard_Boolean hasUV = Standard_False;
1459
1460   Handle(Poly_Triangulation) T;
1461   IS >> buffer;
1462
1463   Standard_SStream aMsg;
1464   if (IS.fail() || (strstr(buffer,"Triangulations") == NULL)) {
1465     aMsg << "BinTools_ShapeSet::Triangulation: Not a Triangulation section" <<endl;
1466     Standard_Failure::Raise(aMsg);
1467   }
1468   IS >> nbtri;
1469   IS.get();// remove LF 
1470
1471   try {
1472     OCC_CATCH_SIGNALS
1473     for (i=1; i<=nbtri; i++) {
1474       BinTools::GetInteger(IS, nbNodes);
1475       BinTools::GetInteger(IS, nbTriangles);
1476       TColgp_Array1OfPnt Nodes(1, nbNodes);
1477       BinTools::GetBool(IS, hasUV);
1478       TColgp_Array1OfPnt2d UVNodes(1, nbNodes);
1479       BinTools::GetReal(IS, d); //deflection
1480       for (j = 1; j <= nbNodes; j++) {
1481         BinTools::GetReal(IS, x);
1482         BinTools::GetReal(IS, y);
1483         BinTools::GetReal(IS, z);
1484         Nodes(j).SetCoord(x,y,z);
1485       }
1486       
1487       if (hasUV) {
1488         for (j = 1; j <= nbNodes; j++) {
1489           BinTools::GetReal(IS, x);
1490           BinTools::GetReal(IS, y);
1491           UVNodes(j).SetCoord(x,y);
1492         }
1493       }
1494       
1495       // read the triangles
1496       Standard_Integer n1,n2,n3;
1497       Poly_Array1OfTriangle Triangles(1, nbTriangles);
1498       for (j = 1; j <= nbTriangles; j++) {
1499         BinTools::GetInteger(IS, n1);
1500         BinTools::GetInteger(IS, n2);
1501         BinTools::GetInteger(IS, n3);
1502         Triangles(j).Set(n1,n2,n3);
1503       }
1504       
1505       if (hasUV) T =  new Poly_Triangulation(Nodes,UVNodes,Triangles);
1506       else T = new Poly_Triangulation(Nodes,Triangles);      
1507       T->Deflection(d);      
1508       myTriangulations.Add(T);
1509     }
1510   }
1511   catch(Standard_Failure) {
1512     aMsg << "EXCEPTION in BinTools_ShapeSet::ReadTriangulation(..)" << endl;
1513     Handle(Standard_Failure) anExc = Standard_Failure::Caught();
1514     aMsg << anExc << endl;
1515     Standard_Failure::Raise(aMsg);
1516   }
1517 }
1518
1519 //=======================================================================
1520 //function : NbShapes
1521 //purpose  : 
1522 //=======================================================================
1523
1524 Standard_Integer  BinTools_ShapeSet::NbShapes() const
1525 {
1526   return myShapes.Extent();
1527 }