0023948: Wrong intersection between a surface of revolution and a plane.
[occt.git] / src / BRepTools / BRepTools_ShapeSet.cxx
1 // Created on: 1993-07-19
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 // Modifed:     Portage NT 7-5-97 DPF (strcasecmp)
18
19 #include <Standard_Stream.hxx>
20 #include <Precision.hxx>
21 #include <BRepTools_ShapeSet.ixx>
22
23 #include <BRepTools.hxx>
24 #include <Poly.hxx>
25 #include <TopoDS.hxx>
26 #include <TColStd_HArray1OfInteger.hxx>
27 #include <BRep_TFace.hxx>
28 #include <BRep_TEdge.hxx>
29 #include <BRep_TVertex.hxx>
30 #include <BRep_Tool.hxx>
31 #include <BRep_CurveRepresentation.hxx>
32 #include <Poly_Polygon3D.hxx>
33 #include <BRep_Polygon3D.hxx>
34 #include <BRep_PolygonOnSurface.hxx>
35 #include <BRep_PolygonOnClosedSurface.hxx>
36 #include <BRep_PolygonOnTriangulation.hxx>
37 #include <BRep_PolygonOnClosedTriangulation.hxx>
38 #include <BRep_CurveOnSurface.hxx>
39 #include <BRep_CurveOnClosedSurface.hxx>
40 #include <BRep_ListOfCurveRepresentation.hxx>
41 #include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
42 #include <BRep_PointOnCurve.hxx>
43 #include <BRep_PointOnCurveOnSurface.hxx>
44 #include <BRep_PointOnSurface.hxx>
45 #include <BRep_ListIteratorOfListOfPointRepresentation.hxx>
46
47 #include <TopoDS_Vertex.hxx>
48
49 #include <TColgp_HArray1OfPnt.hxx>
50 #include <TColgp_HArray1OfPnt2d.hxx>
51 #include <TColStd_HArray1OfReal.hxx>
52 #include <Poly_Triangulation.hxx>
53 #include <Poly_PolygonOnTriangulation.hxx>
54
55 #include <Message_ProgressIndicator.hxx>
56 #include <Message_ProgressSentry.hxx>
57
58 #ifdef MacOS
59 #define strcasecmp(p,q) strcmp(p,q)
60 #elseif WNT
61 #define strcasecmp strcmp
62 #elseif AIX
63 #include <string.h>
64 #endif
65
66 // Modified:    02 Nov 2000: BUC60769. JMB, PTV.  In order to be able to read BRep 
67 //              files that came from a platform different from where CasCade 
68 //              is run, we need the following modifications.
69 //
70 //              On NT platforms (in BRepTools_ShapeSet):
71 //              ----------------
72 //                In Visual C++ 5 (or higher) the fstream::tellg method is not 
73 //                conform to Standard C++ because it modifies the file pointer
74 //                position and returns a wrong position. After that the next
75 //                readings are shifted and the reading process stop with errors.
76 //                
77 //                Workaround is following: Now we don`t use tellg for get position in stream.
78 //                Now able to read file (when reading TopAbs_FACE) without tellg. 
79 //                We simple check the next string if there are value that equal 2 
80 //               (It means a parametr for triangulation).
81
82
83 //=======================================================================
84 //function : BRepTools_ShapeSet
85 //purpose  : 
86 //=======================================================================
87
88 BRepTools_ShapeSet::BRepTools_ShapeSet(const Standard_Boolean isWithTriangles)
89      :myWithTriangles(isWithTriangles)
90 {
91 }
92
93 //=======================================================================
94 //function : BRepTools_ShapeSet
95 //purpose  : 
96 //=======================================================================
97
98 BRepTools_ShapeSet::BRepTools_ShapeSet (const BRep_Builder& B,
99                                         const Standard_Boolean isWithTriangles) :
100        myBuilder(B), myWithTriangles(isWithTriangles)
101 {
102 }
103
104
105 //=======================================================================
106 //function : Clear
107 //purpose  : 
108 //=======================================================================
109
110 void  BRepTools_ShapeSet::Clear()
111 {
112   mySurfaces.Clear();
113   myCurves.Clear();
114   myCurves2d.Clear();
115   myPolygons3D.Clear();
116   myPolygons2D.Clear();
117   myNodes.Clear();
118   myTriangulations.Clear();
119   TopTools_ShapeSet::Clear();
120 }
121
122
123 //=======================================================================
124 //function : AddGeometry
125 //purpose  : 
126 //=======================================================================
127
128 void BRepTools_ShapeSet::AddGeometry(const TopoDS_Shape& S)
129 {
130   // Add the geometry
131   
132   if (S.ShapeType() == TopAbs_VERTEX) {
133     
134     Handle(BRep_TVertex) TV = Handle(BRep_TVertex)::DownCast(S.TShape());
135     BRep_ListIteratorOfListOfPointRepresentation itrp(TV->Points());
136     
137     while (itrp.More()) {
138       const Handle(BRep_PointRepresentation)& PR = itrp.Value();
139
140       if (PR->IsPointOnCurve()) {
141         myCurves.Add(PR->Curve());
142       }
143
144       else if (PR->IsPointOnCurveOnSurface()) {
145         myCurves2d.Add(PR->PCurve());
146         mySurfaces.Add(PR->Surface());
147       }
148
149       else if (PR->IsPointOnSurface()) {
150         mySurfaces.Add(PR->Surface());
151       }
152
153       ChangeLocations().Add(PR->Location());
154       itrp.Next();
155     }
156
157   }
158   else if (S.ShapeType() == TopAbs_EDGE) {
159
160     // Add the curve geometry
161     Handle(BRep_TEdge) TE = Handle(BRep_TEdge)::DownCast(S.TShape());
162     BRep_ListIteratorOfListOfCurveRepresentation itrc(TE->Curves());
163
164     while (itrc.More()) {
165       const Handle(BRep_CurveRepresentation)& CR = itrc.Value();
166       if (CR->IsCurve3D()) {
167         if (!CR->Curve3D().IsNull()) {
168           myCurves.Add(CR->Curve3D());
169           ChangeLocations().Add(CR->Location());
170         }
171       }
172       else if (CR->IsCurveOnSurface()) {
173         mySurfaces.Add(CR->Surface());
174         myCurves2d.Add(CR->PCurve());
175         ChangeLocations().Add(CR->Location());
176         if (CR->IsCurveOnClosedSurface())
177           myCurves2d.Add(CR->PCurve2());
178       }
179       else if (CR->IsRegularity()) {
180         mySurfaces.Add(CR->Surface());
181         ChangeLocations().Add(CR->Location());
182         mySurfaces.Add(CR->Surface2());
183         ChangeLocations().Add(CR->Location2());
184       }
185       else if (myWithTriangles) { // for XML Persistence
186         if (CR->IsPolygon3D()) {
187           if (!CR->Polygon3D().IsNull()) {
188             myPolygons3D.Add(CR->Polygon3D());
189             ChangeLocations().Add(CR->Location());
190           }
191         }
192         else if (CR->IsPolygonOnTriangulation()) {
193           myTriangulations.Add(CR->Triangulation());
194           myNodes.Add(CR->PolygonOnTriangulation());
195           ChangeLocations().Add(CR->Location());
196           if (CR->IsPolygonOnClosedTriangulation())
197             myNodes.Add(CR->PolygonOnTriangulation2());
198         }
199         else if (CR->IsPolygonOnSurface()) {
200           mySurfaces.Add(CR->Surface());
201           myPolygons2D.Add(CR->Polygon());
202           ChangeLocations().Add(CR->Location());
203           if (CR->IsPolygonOnClosedSurface())
204           myPolygons2D.Add(CR->Polygon2());
205         }
206       }
207       itrc.Next();
208     }
209   }
210
211   else if (S.ShapeType() == TopAbs_FACE) {
212
213     // Add the surface geometry
214     Handle(BRep_TFace) TF = Handle(BRep_TFace)::DownCast(S.TShape());
215     if (!TF->Surface().IsNull())  mySurfaces.Add(TF->Surface());
216
217     if (myWithTriangles) { // for XML Persistence
218       Handle(Poly_Triangulation) Tr = TF->Triangulation();
219       if (!Tr.IsNull()) myTriangulations.Add(Tr);
220     }
221
222     ChangeLocations().Add(TF->Location());
223   }
224 }
225
226
227 //=======================================================================
228 //function : DumpGeometry
229 //purpose  : 
230 //=======================================================================
231
232 void  BRepTools_ShapeSet::DumpGeometry(Standard_OStream& OS)const 
233 {
234   myCurves2d.Dump(OS);
235   myCurves.Dump(OS);
236   DumpPolygon3D(OS);
237   DumpPolygonOnTriangulation(OS);
238   mySurfaces.Dump(OS);
239   DumpTriangulation(OS);
240 }
241
242
243 //=======================================================================
244 //function : WriteGeometry
245 //purpose  : 
246 //=======================================================================
247
248 void  BRepTools_ShapeSet::WriteGeometry(Standard_OStream& OS)
249 {
250   //OCC19559
251   myCurves2d.SetProgress(GetProgress());
252   myCurves.SetProgress(GetProgress());
253   mySurfaces.SetProgress(GetProgress());
254
255   if ( !GetProgress().IsNull()) {
256     if(GetProgress()->UserBreak() ) return;
257     GetProgress()->NewScope ( 15, "2D Curves" );
258   }
259   myCurves2d.Write(OS);
260   
261   if ( !GetProgress().IsNull()) {
262     if( GetProgress()->UserBreak() ) return;
263     GetProgress()->EndScope();
264     GetProgress()->Show();
265     
266     GetProgress()->NewScope ( 15, "3D Curves" );
267   }
268   myCurves.Write(OS);
269   
270   if ( !GetProgress().IsNull()) {
271     if( GetProgress()->UserBreak() ) return;
272     GetProgress()->EndScope();
273     GetProgress()->Show();
274   
275     GetProgress()->NewScope ( 10, "3D Polygons" );
276   }
277   WritePolygon3D(OS);
278   if ( !GetProgress().IsNull()) {
279     if( GetProgress()->UserBreak() ) return;
280     GetProgress()->EndScope();
281     GetProgress()->Show();
282
283     GetProgress()->NewScope ( 10, "Polygons On Triangulation" );
284   }
285   WritePolygonOnTriangulation(OS);
286   if ( !GetProgress().IsNull()) {
287     if( GetProgress()->UserBreak() ) return;
288     GetProgress()->EndScope();
289     GetProgress()->Show();
290     
291     GetProgress()->NewScope ( 10, "Surfaces" );
292   }
293   mySurfaces.Write(OS);
294   if ( !GetProgress().IsNull()) {
295     if( GetProgress()->UserBreak() ) return;
296     GetProgress()->EndScope();
297     GetProgress()->Show();
298     
299     GetProgress()->NewScope ( 15, "Triangulations" );
300   }
301   WriteTriangulation(OS);
302   if ( !GetProgress().IsNull()) {
303     if( GetProgress()->UserBreak() ) return;
304     GetProgress()->EndScope();
305     GetProgress()->Show();
306   }
307 }
308
309
310 //=======================================================================
311 //function : ReadGeometry
312 //purpose  : 
313 //=======================================================================
314
315 void  BRepTools_ShapeSet::ReadGeometry(Standard_IStream& IS)
316 {
317   //OCC19559
318   myCurves2d.SetProgress(GetProgress());
319   myCurves.SetProgress(GetProgress());
320   mySurfaces.SetProgress(GetProgress());
321
322   if ( !GetProgress().IsNull()) {
323     if( GetProgress()->UserBreak() ) return;
324     GetProgress()->NewScope ( 15, "2D Curves" );
325   }
326   myCurves2d.Read(IS);
327
328   if ( !GetProgress().IsNull()) {
329     if( GetProgress()->UserBreak() ) return;
330     GetProgress()->EndScope();
331     GetProgress()->Show();
332     
333     GetProgress()->NewScope ( 15, "3D Curves" );
334   }
335   myCurves.Read(IS);
336
337   if ( !GetProgress().IsNull()) {
338     if( GetProgress()->UserBreak() ) return;
339     GetProgress()->EndScope();
340     GetProgress()->Show();
341     
342     GetProgress()->NewScope ( 10, "3D Polygons" );
343   }
344   ReadPolygon3D(IS);
345   if ( !GetProgress().IsNull() ) {
346     if( GetProgress()->UserBreak() ) return;
347     GetProgress()->EndScope();
348     GetProgress()->Show();
349
350     GetProgress()->NewScope ( 10, "Polygons On Triangulation" );
351   }
352   ReadPolygonOnTriangulation(IS);
353   if ( !GetProgress().IsNull()) {
354     if( GetProgress()->UserBreak() ) return;
355     GetProgress()->EndScope();
356     GetProgress()->Show();
357     
358     GetProgress()->NewScope ( 10, "Surfaces" );
359   }
360   mySurfaces.Read(IS);
361   if ( !GetProgress().IsNull() ) {
362     if( GetProgress()->UserBreak() ) return;
363     GetProgress()->EndScope();
364     GetProgress()->Show();
365
366     GetProgress()->NewScope ( 15, "Triangulations" );
367   }
368   ReadTriangulation(IS);
369   if ( !GetProgress().IsNull()) {
370     if( GetProgress()->UserBreak() ) return;
371     GetProgress()->EndScope();
372     GetProgress()->Show();
373   }
374 }
375
376 //=======================================================================
377 //function : PrintRegularity
378 //purpose  : 
379 //=======================================================================
380
381 static void PrintRegularity(const GeomAbs_Shape C,
382                             Standard_OStream& OS)
383 {
384   switch (C) {
385
386   case GeomAbs_C0 :
387     OS << "C0";
388     break;
389
390   case GeomAbs_G1 :
391     OS << "G1";
392     break;
393
394   case GeomAbs_C1 :
395     OS << "C1";
396     break;
397
398   case GeomAbs_G2 :
399     OS << "G2";
400     break;
401
402   case GeomAbs_C2 :
403     OS << "C2";
404     break;
405
406   case GeomAbs_C3 :
407     OS << "C3";
408     break;
409
410   case GeomAbs_CN :
411     OS << "CN";
412     break;
413
414   }
415 }
416
417 //=======================================================================
418 //function : DumpGeometry
419 //purpose  : 
420 //=======================================================================
421
422 void  BRepTools_ShapeSet::DumpGeometry(const TopoDS_Shape& S, 
423                                        Standard_OStream&   OS)const 
424 {
425   // Dump the geometry
426   
427   if (S.ShapeType() == TopAbs_VERTEX) {
428
429     // Dump the point geometry
430     TopoDS_Vertex V = TopoDS::Vertex(S);
431     OS << "    Tolerance : " << BRep_Tool::Tolerance(V) << "\n";
432     gp_Pnt p = BRep_Tool::Pnt(V);
433     OS << "    - Point 3D : "<<p.X()<<", "<<p.Y()<<", "<<p.Z()<<"\n";
434
435     Handle(BRep_TVertex) TV = Handle(BRep_TVertex)::DownCast(S.TShape());
436     BRep_ListIteratorOfListOfPointRepresentation itrp(TV->Points());
437     
438     while (itrp.More()) {
439       const Handle(BRep_PointRepresentation)& PR = itrp.Value();
440
441       OS << "    - Parameter : "<< PR->Parameter();
442       if (PR->IsPointOnCurve()) {
443         OS << " on curve " << myCurves.Index(PR->Curve());
444       }
445
446       else if (PR->IsPointOnCurveOnSurface()) {
447         OS << " on pcurve " <<  myCurves2d.Index(PR->PCurve());
448         OS << " on surface " << mySurfaces.Index(PR->Surface());
449       }
450
451       else if (PR->IsPointOnSurface()) {
452         OS << ", " << PR->Parameter2() << " on surface ";
453         OS << mySurfaces.Index(PR->Surface());
454       }
455
456       if (!PR->Location().IsIdentity())
457         OS << " location " << Locations().Index(PR->Location());
458       OS << "\n";
459
460       itrp.Next();
461     }
462
463   }
464
465   else if (S.ShapeType() == TopAbs_EDGE) {
466
467     Handle(BRep_TEdge) TE = Handle(BRep_TEdge)::DownCast(S.TShape());
468     gp_Pnt2d Pf,Pl;
469
470     // Dump the curve geometry 
471     OS << "    Tolerance : " << TE->Tolerance() << "\n";
472     if (TE->SameParameter()) OS << "     same parametrisation of curves\n";
473     if (TE->SameRange())     OS << "     same range on curves\n";
474     if (TE->Degenerated())   OS << "     degenerated\n";
475     
476     Standard_Real first, last;
477     BRep_ListIteratorOfListOfCurveRepresentation itrc = TE->Curves();
478     while (itrc.More()) {
479       const Handle(BRep_CurveRepresentation)& CR = itrc.Value();
480       if (CR->IsCurve3D()) {
481         Handle(BRep_GCurve) GC = Handle(BRep_GCurve)::DownCast(itrc.Value());
482         GC->Range(first, last);
483         if (!CR->Curve3D().IsNull()) {
484           OS << "    - Curve 3D : "<<myCurves.Index(CR->Curve3D());
485           if (!CR->Location().IsIdentity())
486             OS << " location "<<Locations().Index(CR->Location());
487           OS <<", range : " << first << " " << last <<"\n";
488         }
489       }
490       else if (CR->IsCurveOnSurface()) {
491         Handle(BRep_GCurve) GC = Handle(BRep_GCurve)::DownCast(itrc.Value());
492         GC->Range(first, last);
493         OS <<"    - PCurve : "<<myCurves2d.Index(CR->PCurve());
494         if (CR->IsCurveOnClosedSurface()) {
495           OS <<", " << myCurves2d.Index(CR->PCurve2());
496           OS << " (";
497           PrintRegularity(CR->Continuity(),OS);
498           OS <<")";
499         }
500         OS << " on surface "<<mySurfaces.Index(CR->Surface());
501         if (!CR->Location().IsIdentity())
502           OS << " location "<<Locations().Index(CR->Location());
503         OS <<", range : " << first << " " << last <<"\n";
504         
505         Handle(BRep_CurveOnSurface) COS = 
506           Handle(BRep_CurveOnSurface)::DownCast(CR);
507         COS->UVPoints(Pf,Pl);
508         OS << "  UV Points : " <<Pf.X()<<", "<<Pf.Y()<<" ";
509         OS << Pl.X()<<", "<<Pl.Y()<<"\n";
510         if (CR->IsCurveOnClosedSurface()) {
511           Handle(BRep_CurveOnClosedSurface) COCS = 
512             Handle(BRep_CurveOnClosedSurface)::DownCast(CR);
513           COCS->UVPoints2(Pf,Pl);
514           OS << "  UV Points : " <<Pf.X()<<", "<<Pf.Y()<<" ";
515           OS << Pl.X()<<", "<<Pl.Y()<<"\n";
516         }
517       }
518       else if (CR->IsRegularity()) {
519         OS << "    - Regularity ";
520         PrintRegularity(CR->Continuity(),OS);
521         OS << "   on surfaces : "<<mySurfaces.Index(CR->Surface());
522         if (!CR->Location().IsIdentity())
523           OS << " location "<<Locations().Index(CR->Location());
524         OS << ", "<<mySurfaces.Index(CR->Surface2());
525         if (!CR->Location2().IsIdentity())
526           OS << " location "<<Locations().Index(CR->Location2());
527         OS << "\n";
528       }
529       else if (CR->IsPolygon3D()) {
530         Handle(BRep_Polygon3D) GC = Handle(BRep_Polygon3D)::DownCast(itrc.Value());
531         if (!GC->Polygon3D().IsNull()) {
532           OS << "    - Polygon 3D : "<<myPolygons3D.FindIndex(CR->Polygon3D());
533           if (!CR->Location().IsIdentity())
534             OS << " location "<<Locations().Index(CR->Location());
535         }
536       }
537       else if (CR->IsPolygonOnTriangulation()) {
538         OS << "    - PolygonOnTriangulation " << myNodes.FindIndex(CR->PolygonOnTriangulation());
539         if (CR->IsPolygonOnClosedTriangulation()) {
540           OS << " " << myNodes.FindIndex(CR->PolygonOnTriangulation2());
541         }
542         OS << " on triangulation " << myTriangulations.FindIndex(CR->Triangulation());
543         if (!CR->Location().IsIdentity())
544           OS << " location "<<Locations().Index(CR->Location());
545         OS << endl;
546       }
547       itrc.Next();
548     }
549   }
550
551   else if (S.ShapeType() == TopAbs_FACE) {
552
553     const TopoDS_Face& F = TopoDS::Face(S);
554     if (BRep_Tool::NaturalRestriction(F))
555       OS << "NaturalRestriction\n";
556
557     // Dump the surface geometry
558     Handle(BRep_TFace) TF = Handle(BRep_TFace)::DownCast(S.TShape());
559     if (!TF->Surface().IsNull()) {
560       OS << "    Tolerance : " <<TF->Tolerance() << "\n";
561       OS << "    - Surface : "<<mySurfaces.Index(TF->Surface());
562       if (!S.Location().IsIdentity())
563         OS << " location "<<Locations().Index(S.Location());
564       OS << "\n";
565     }
566     if (!(TF->Triangulation()).IsNull()) {
567       // Dump the triangulation
568       OS << "    - Triangulation : " <<myTriangulations.FindIndex(TF->Triangulation());
569       if (!S.Location().IsIdentity())
570         OS << " location " <<Locations().Index(TF->Location());
571       OS << "\n";
572     }
573   }
574  
575   OS << "\n";
576 }
577
578
579 //=======================================================================
580 //function : WriteGeometry
581 //purpose  : 
582 //=======================================================================
583
584 void  BRepTools_ShapeSet::WriteGeometry(const TopoDS_Shape& S, 
585                                         Standard_OStream&   OS)const 
586 {
587   // Write the geometry
588   
589   if (S.ShapeType() == TopAbs_VERTEX) {
590
591     // Write the point geometry
592     TopoDS_Vertex V = TopoDS::Vertex(S);
593     OS << BRep_Tool::Tolerance(V) << "\n";
594     gp_Pnt p = BRep_Tool::Pnt(V);
595     OS<<p.X()<<" "<<p.Y()<<" "<<p.Z()<<"\n";
596
597     Handle(BRep_TVertex) TV = Handle(BRep_TVertex)::DownCast(S.TShape());
598     BRep_ListIteratorOfListOfPointRepresentation itrp(TV->Points());
599     
600     while (itrp.More()) {
601       const Handle(BRep_PointRepresentation)& PR = itrp.Value();
602
603       OS << PR->Parameter();
604       if (PR->IsPointOnCurve()) {
605         OS << " 1 " << myCurves.Index(PR->Curve());
606       }
607
608       else if (PR->IsPointOnCurveOnSurface()) {
609         OS << " 2 " <<  myCurves2d.Index(PR->PCurve());
610         OS << " " << mySurfaces.Index(PR->Surface());
611       }
612
613       else if (PR->IsPointOnSurface()) {
614         OS << " 3 " << PR->Parameter2() << " ";
615         OS << mySurfaces.Index(PR->Surface());
616       }
617
618       OS << " " << Locations().Index(PR->Location());
619       OS << "\n";
620       
621       itrp.Next();
622     }
623     
624     OS << "0 0\n"; // end representations
625
626   }
627
628   else if (S.ShapeType() == TopAbs_EDGE) {
629
630     // Write the curve geometry 
631
632     Handle(BRep_TEdge) TE = Handle(BRep_TEdge)::DownCast(S.TShape());
633
634     OS << " " << TE->Tolerance() << " ";
635     OS << ((TE->SameParameter()) ? 1 : 0) << " ";
636     OS << ((TE->SameRange())     ? 1 : 0) << " ";
637     OS << ((TE->Degenerated())   ? 1 : 0) << "\n";
638     
639     Standard_Real first, last;
640     BRep_ListIteratorOfListOfCurveRepresentation itrc = TE->Curves();
641     while (itrc.More()) {
642       const Handle(BRep_CurveRepresentation)& CR = itrc.Value();
643       if (CR->IsCurve3D()) {
644         if (!CR->Curve3D().IsNull()) {
645           Handle(BRep_GCurve) GC = Handle(BRep_GCurve)::DownCast(itrc.Value());
646           GC->Range(first, last);
647           OS << "1 ";                               // -1- Curve 3D
648           OS << " "<<myCurves.Index(CR->Curve3D());
649           OS << " "<<Locations().Index(CR->Location());
650           OS << " "<<first<<" "<<last;
651           OS << "\n";
652         }
653       }
654       else if (CR->IsCurveOnSurface()) {
655         Handle(BRep_GCurve) GC = Handle(BRep_GCurve)::DownCast(itrc.Value());
656         GC->Range(first, last);
657         if (!CR->IsCurveOnClosedSurface())
658           OS << "2 ";                             // -2- Curve on surf
659         else
660           OS << "3 ";                             // -3- Curve on closed surf
661         OS <<" "<<myCurves2d.Index(CR->PCurve());
662         if (CR->IsCurveOnClosedSurface()) {
663           OS <<" " << myCurves2d.Index(CR->PCurve2());
664           PrintRegularity(CR->Continuity(),OS);
665         }
666         OS << " " << mySurfaces.Index(CR->Surface());
667         OS << " " << Locations().Index(CR->Location());
668         OS << " "<<first<<" "<<last;
669         OS << "\n";
670
671         // Write UV Points // for XML Persistence higher performance
672         if (FormatNb() == 2)
673         {
674           gp_Pnt2d Pf,Pl;
675           if (CR->IsCurveOnClosedSurface()) {
676             Handle(BRep_CurveOnClosedSurface) COCS = 
677               Handle(BRep_CurveOnClosedSurface)::DownCast(CR);
678             COCS->UVPoints2(Pf,Pl);
679           }
680           else {
681             Handle(BRep_CurveOnSurface) COS = 
682               Handle(BRep_CurveOnSurface)::DownCast(CR);
683             COS->UVPoints(Pf,Pl);
684           }
685           OS << Pf.X() << " " << Pf.Y() << " " << Pl.X() << " " << Pl.Y() << "\n";
686         }
687       }
688       else if (CR->IsRegularity()) {
689         OS << "4 ";                              // -4- Regularity
690         PrintRegularity(CR->Continuity(),OS);
691         OS << " "<<mySurfaces.Index(CR->Surface());
692         OS << " "<<Locations().Index(CR->Location());
693         OS << " "<<mySurfaces.Index(CR->Surface2());
694         OS << " "<<Locations().Index(CR->Location2());
695         OS << "\n";
696       }
697
698       else if (myWithTriangles) { // for XML Persistence
699         if (CR->IsPolygon3D()) {
700           Handle(BRep_Polygon3D) GC = Handle(BRep_Polygon3D)::DownCast(itrc.Value());
701           if (!GC->Polygon3D().IsNull()) {
702             OS << "5 ";                            // -5- Polygon3D
703             OS << " "<<myPolygons3D.FindIndex(CR->Polygon3D());
704             OS << " "<<Locations().Index(CR->Location());
705             OS << "\n";
706           }
707         }
708         else if (CR->IsPolygonOnTriangulation()) {
709           Handle(BRep_PolygonOnTriangulation) PT = 
710             Handle(BRep_PolygonOnTriangulation)::DownCast(itrc.Value());
711           if (!CR->IsPolygonOnClosedTriangulation())
712             OS << "6 ";                            // -6- Polygon on triangulation
713           else
714             OS << "7 ";                            // -7- Polygon on closed triangulation
715           OS << " " <<  myNodes.FindIndex(PT->PolygonOnTriangulation());
716           if (CR->IsPolygonOnClosedTriangulation()) {
717             OS << " " << myNodes.FindIndex(PT->PolygonOnTriangulation2());
718           }
719           OS << " " << myTriangulations.FindIndex(PT->Triangulation());
720           OS << " "<<Locations().Index(CR->Location());
721           OS << "\n";
722         }
723       }
724       
725       itrc.Next();
726     }
727     OS << "0\n"; // end of the list of representations
728   }
729   
730   else if (S.ShapeType() == TopAbs_FACE) {
731
732     Handle(BRep_TFace) TF = Handle(BRep_TFace)::DownCast(S.TShape());
733     const TopoDS_Face& F = TopoDS::Face(S);
734
735     if (!(TF->Surface()).IsNull()) {
736       OS << ((BRep_Tool::NaturalRestriction(F)) ? 1 : 0);
737       OS << " ";
738       // Write the surface geometry
739       OS << " " <<TF->Tolerance();
740       OS << " " <<mySurfaces.Index(TF->Surface());
741       OS << " " <<Locations().Index(TF->Location());
742       OS << "\n";
743     }
744     else //For correct reading of null face
745       {
746         OS << 0;
747         OS << " ";
748         OS << " " <<TF->Tolerance();
749         OS << " " << 0;
750         OS << " " << 0;
751         OS << "\n";
752       }
753     if (myWithTriangles) { // for XML Persistence
754       if (!(TF->Triangulation()).IsNull()) {
755         OS << 2;
756         OS << " ";
757         // Write the triangulation
758         OS << " " <<myTriangulations.FindIndex(TF->Triangulation());
759       }
760     }
761   }
762   
763 }
764
765 //=======================================================================
766 //function : ReadRegularity
767 //purpose  : 
768 //=======================================================================
769
770 static GeomAbs_Shape ReadRegularity(Standard_IStream& IS)
771 {
772   char buffer[255];
773   IS >> buffer;
774   switch (buffer[0]) {
775
776   case 'C' :
777     switch (buffer[1]) {
778       
779     case '0' :
780       return GeomAbs_C0;
781       
782     case '1' :
783       return GeomAbs_C1;
784       
785     case '2' :
786       return GeomAbs_C2;
787       
788     case '3' :
789       return GeomAbs_C3;
790       
791     case 'N' :
792       return GeomAbs_CN;
793     }
794     break;
795
796   case 'G' :
797     switch (buffer[1]) {
798       
799     case '1' :
800       return GeomAbs_G1;
801       
802     case '2' :
803       return GeomAbs_G2;
804       
805     }
806     break;
807   }
808   return GeomAbs_C0;
809 }
810
811 //=======================================================================
812 //function : ReadGeometry
813 //purpose  : 
814 //=======================================================================
815
816 void  BRepTools_ShapeSet::ReadGeometry(const TopAbs_ShapeEnum T, 
817                                        Standard_IStream&      IS, 
818                                        TopoDS_Shape&          S)
819 {
820   // Read the geometry
821
822   Standard_Integer val,c,pc,pc2 = 0,s,s2,l,l2,t, pt, pt2 = 0;
823   Standard_Real tol,X,Y,Z,first,last,p1,p2;
824   Standard_Real PfX,PfY,PlX,PlY;
825   gp_Pnt2d aPf, aPl;
826   Standard_Boolean closed;
827   GeomAbs_Shape reg = GeomAbs_C0;
828   switch (T) {
829
830
831     //---------
832     // vertex
833     //---------
834
835   case TopAbs_VERTEX :
836     {
837       TopoDS_Vertex& V = TopoDS::Vertex(S);
838       
839       // Read the point geometry
840       IS >> tol;
841       IS >> X >> Y >> Z;
842       myBuilder.MakeVertex(V,gp_Pnt(X,Y,Z),tol);
843       Handle(BRep_TVertex) TV = Handle(BRep_TVertex)::DownCast(V.TShape());
844
845       BRep_ListOfPointRepresentation& lpr = TV->ChangePoints();
846       TopLoc_Location L;
847
848       do {
849         IS >> p1 >> val;
850         
851         Handle(BRep_PointRepresentation) PR;
852         switch (val) {
853
854         case 1 :
855           {
856             IS >> c;
857
858 //  Modified by Sergey KHROMOV - Wed Apr 24 13:59:09 2002 Begin
859             if (myCurves.Curve(c).IsNull())
860               break;
861 //  Modified by Sergey KHROMOV - Wed Apr 24 13:59:13 2002 End
862
863             Handle(BRep_PointOnCurve) POC =
864               new BRep_PointOnCurve(p1,
865                                     myCurves.Curve(c),
866                                     L);
867             PR = POC;
868           }
869           break;
870
871         case 2 :
872           {
873             IS >> pc >> s;
874
875 //  Modified by Sergey KHROMOV - Wed Apr 24 13:59:09 2002 Begin
876             if (myCurves2d.Curve2d(pc).IsNull() ||
877                 mySurfaces.Surface(s).IsNull())
878               break;
879 //  Modified by Sergey KHROMOV - Wed Apr 24 13:59:13 2002 End
880
881             Handle(BRep_PointOnCurveOnSurface) POC =
882               new BRep_PointOnCurveOnSurface(p1,
883                                              myCurves2d.Curve2d(pc),
884                                              mySurfaces.Surface(s),
885                                              L);
886             PR = POC;
887           }
888           break;
889
890         case 3 :
891           {
892             IS >> p2 >> s;
893
894 //  Modified by Sergey KHROMOV - Wed Apr 24 13:59:09 2002 Begin
895             if (mySurfaces.Surface(s).IsNull())
896               break;
897 //  Modified by Sergey KHROMOV - Wed Apr 24 13:59:13 2002 End
898
899             Handle(BRep_PointOnSurface) POC =
900               new BRep_PointOnSurface(p1,p2,
901                                       mySurfaces.Surface(s),
902                                       L);
903             PR = POC;
904           }
905           break;
906         }
907
908         if (val > 0) {
909           IS >> l;
910           if (!PR.IsNull()) {
911             PR->Location(Locations().Location(l));
912             lpr.Append(PR);
913           }
914         }
915       } while (val > 0);
916     }
917     break;
918       
919
920     //---------
921     // edge
922     //---------
923
924
925     case TopAbs_EDGE :
926
927       // Create an edge
928       {
929         TopoDS_Edge& E = TopoDS::Edge(S);
930         
931         myBuilder.MakeEdge(E);
932         
933         // Read the curve geometry 
934         IS >> tol;
935         IS >> val;
936         myBuilder.SameParameter(E,(val == 1));
937         IS >> val;
938         myBuilder.SameRange(E,(val == 1));
939         IS >> val;
940         myBuilder.Degenerated(E,(val == 1));
941         
942         do {
943           IS >> val;
944           switch (val) {
945             
946           case 1 :                               // -1- Curve 3D
947             IS >> c >> l;
948             if (!myCurves.Curve(c).IsNull()) {
949               myBuilder.UpdateEdge(E,myCurves.Curve(c),
950                                    Locations().Location(l),tol);
951             }
952             IS >> first >> last;
953             if (!myCurves.Curve(c).IsNull()) {
954               Standard_Boolean Only3d = Standard_True;
955               myBuilder.Range(E,first,last,Only3d);
956             }
957             break;
958             
959             
960           case 2 :                               // -2- Curve on surf
961           case 3 :                               // -3- Curve on closed surf
962             closed = (val == 3);
963             IS >> pc;
964             if (closed) {
965               IS >> pc2;
966               reg = ReadRegularity(IS);
967             }
968
969             // surface, location
970             IS >> s >> l;
971
972             // range
973             IS >> first >> last;
974
975             // read UV Points // for XML Persistence higher performance
976             if (FormatNb() == 2)
977             {
978               IS >> PfX >> PfY >> PlX >> PlY;
979               aPf = gp_Pnt2d(PfX,PfY);
980               aPl = gp_Pnt2d(PlX,PlY);
981             }
982
983 //  Modified by Sergey KHROMOV - Wed Apr 24 12:11:16 2002 Begin
984             if (myCurves2d.Curve2d(pc).IsNull() ||
985                 (closed && myCurves2d.Curve2d(pc2).IsNull()) ||
986                 mySurfaces.Surface(s).IsNull())
987               break;
988 //  Modified by Sergey KHROMOV - Wed Apr 24 12:11:17 2002 End
989
990             if (closed) {
991               if (FormatNb() == 2)
992                 myBuilder.UpdateEdge(E,myCurves2d.Curve2d(pc),
993                                      myCurves2d.Curve2d(pc2),
994                                      mySurfaces.Surface(s),
995                                      Locations().Location(l),tol,
996                                      aPf, aPl);
997               else
998                 myBuilder.UpdateEdge(E,myCurves2d.Curve2d(pc),
999                                      myCurves2d.Curve2d(pc2),
1000                                      mySurfaces.Surface(s),
1001                                      Locations().Location(l),tol);
1002
1003               myBuilder.Continuity(E,
1004                                    mySurfaces.Surface(s),
1005                                    mySurfaces.Surface(s),
1006                                    Locations().Location(l),
1007                                    Locations().Location(l),
1008                                    reg);
1009             }
1010             else
1011             {
1012               if (FormatNb() == 2)
1013                 myBuilder.UpdateEdge(E,myCurves2d.Curve2d(pc),
1014                                      mySurfaces.Surface(s),
1015                                      Locations().Location(l),tol,
1016                                      aPf, aPl);
1017               else
1018                 myBuilder.UpdateEdge(E,myCurves2d.Curve2d(pc),
1019                                      mySurfaces.Surface(s),
1020                                      Locations().Location(l),tol);
1021             }
1022             myBuilder.Range(E,
1023                             mySurfaces.Surface(s),
1024                             Locations().Location(l),
1025                             first,last);
1026             break;
1027             
1028           case 4 :                               // -4- Regularity
1029             reg = ReadRegularity(IS);
1030             IS >> s >> l >> s2 >> l2;
1031 //  Modified by Sergey KHROMOV - Wed Apr 24 12:39:13 2002 Begin
1032             if (mySurfaces.Surface(s).IsNull() ||
1033                 mySurfaces.Surface(s2).IsNull())
1034               break;
1035 //  Modified by Sergey KHROMOV - Wed Apr 24 12:39:14 2002 End
1036             myBuilder.Continuity(E,
1037                                  mySurfaces.Surface(s),
1038                                  mySurfaces.Surface(s2),
1039                                  Locations().Location(l),
1040                                  Locations().Location(l2),
1041                                  reg);
1042             break;
1043
1044           case 5 :   // -5- Polygon3D                            
1045             IS >> c >> l;
1046 //szy-02.05.2004            myBuilder.UpdateEdge(E,Handle(Poly_Polygon3D)::DownCast(myPolygons3D(c)));
1047                         if (c > 0 && c <= myPolygons3D.Extent())
1048                   myBuilder.UpdateEdge(E,Handle(Poly_Polygon3D)::DownCast(myPolygons3D(c)), Locations().Location(l));
1049             break;
1050
1051           case 6 :
1052           case 7 :
1053             closed = (val == 7);
1054             IS >> pt;
1055             if (closed) {
1056               IS >> pt2;
1057             }
1058             IS >> t >> l;
1059             if (closed) {
1060                       if (t > 0 && t <= myTriangulations.Extent() &&
1061                                   pt > 0 && pt <= myNodes.Extent() &&
1062                                   pt2 > 0 && pt2 <= myNodes.Extent())
1063                 myBuilder.UpdateEdge
1064                   (E, Handle(Poly_PolygonOnTriangulation)::DownCast(myNodes(pt)),
1065                    Handle(Poly_PolygonOnTriangulation)::DownCast(myNodes(pt2)),
1066                    Handle(Poly_Triangulation)::DownCast(myTriangulations(t)),
1067                    Locations().Location(l));
1068             }
1069             else {
1070                       if (t > 0 && t <= myTriangulations.Extent() &&
1071                                   pt > 0 && pt <= myNodes.Extent())
1072                 myBuilder.UpdateEdge
1073                   (E,Handle(Poly_PolygonOnTriangulation)::DownCast(myNodes(pt)),
1074                    Handle(Poly_Triangulation)::DownCast(myTriangulations(t)),
1075                    Locations().Location(l));
1076             }
1077             // range
1078             
1079             break;
1080             
1081           }
1082         } while (val > 0);
1083       }
1084     break;
1085
1086
1087     //---------
1088     // wire
1089     //---------
1090
1091   case TopAbs_WIRE :
1092     myBuilder.MakeWire(TopoDS::Wire(S));
1093     break;
1094
1095
1096     //---------
1097     // face
1098     //---------
1099
1100   case TopAbs_FACE :
1101     {
1102     // create a face :
1103     TopoDS_Face& F = TopoDS::Face(S);
1104 //    streampos pos;
1105     myBuilder.MakeFace(F);
1106
1107     IS >> val; // natural restriction
1108     if (val == 0 || val == 1) {
1109       IS >> tol >> s >> l;
1110 //  Modified by Sergey KHROMOV - Wed Apr 24 12:39:13 2002 Begin
1111       if (!mySurfaces.Surface(s).IsNull()) {
1112 //  Modified by Sergey KHROMOV - Wed Apr 24 12:39:14 2002 End
1113         myBuilder.UpdateFace(TopoDS::Face(S),
1114                              mySurfaces.Surface(s),
1115                              Locations().Location(l),tol);
1116         myBuilder.NaturalRestriction(TopoDS::Face(S),(val == 1));
1117       }
1118 //      pos = IS.tellg();
1119 //      IS >> val;
1120     }
1121     else if(val == 2) {
1122       //only triangulation
1123       IS >> s;
1124       myBuilder.UpdateFace(TopoDS::Face(S),
1125                            Handle(Poly_Triangulation)::DownCast(myTriangulations(s)));
1126     }
1127 //    else pos = IS.tellg();
1128     
1129     // BUC60769
1130
1131     if(val == 2) break;
1132
1133     char string[260];
1134     IS.getline ( string, 256, '\n' );
1135     IS.getline ( string, 256, '\n' );
1136
1137     if (string[0] == '2') {
1138       // cas triangulation
1139       s = atoi ( &string[2] );
1140           if (s > 0 && s <= myTriangulations.Extent())
1141         myBuilder.UpdateFace(TopoDS::Face(S),
1142                              Handle(Poly_Triangulation)::DownCast(myTriangulations(s)));
1143     }
1144 //    else IS.seekg(pos);
1145     }
1146     break;
1147
1148
1149     //---------
1150     // shell
1151     //---------
1152
1153   case TopAbs_SHELL :
1154     myBuilder.MakeShell(TopoDS::Shell(S));
1155     break;
1156
1157
1158     //---------
1159     // solid
1160     //---------
1161
1162   case TopAbs_SOLID :
1163     myBuilder.MakeSolid(TopoDS::Solid(S));
1164     break;
1165
1166
1167     //---------
1168     // compsolid
1169     //---------
1170
1171   case TopAbs_COMPSOLID :
1172     myBuilder.MakeCompSolid(TopoDS::CompSolid(S));
1173     break;
1174
1175
1176     //---------
1177     // compound
1178     //---------
1179
1180   case TopAbs_COMPOUND :
1181     myBuilder.MakeCompound(TopoDS::Compound(S));
1182     break;
1183
1184   default:
1185     break;
1186   }
1187   
1188 }
1189
1190
1191
1192 //=======================================================================
1193 //function : AddShapes
1194 //purpose  : 
1195 //=======================================================================
1196
1197 void  BRepTools_ShapeSet::AddShapes(TopoDS_Shape&       S1, 
1198                                     const TopoDS_Shape& S2)
1199 {
1200   myBuilder.Add(S1,S2);
1201 }
1202
1203 //=======================================================================
1204 //function : Check
1205 //purpose  : 
1206 //=======================================================================
1207
1208 void BRepTools_ShapeSet::Check(const TopAbs_ShapeEnum T,
1209                                TopoDS_Shape&          S)
1210 {
1211   if (T == TopAbs_FACE) {
1212     const TopoDS_Face& F = TopoDS::Face(S);
1213     BRepTools::Update(F);
1214   }
1215 }
1216
1217
1218
1219 //=======================================================================
1220 //function : WritePolygonOnTriangulation
1221 //purpose  : 
1222 //=======================================================================
1223
1224 void BRepTools_ShapeSet::WritePolygonOnTriangulation(Standard_OStream&      OS,
1225                                                      const Standard_Boolean Compact)const
1226 {
1227   Standard_Integer i, j, nbpOntri = myNodes.Extent();
1228
1229   Handle(Message_ProgressIndicator) progress = GetProgress();
1230   Message_ProgressSentry PS(progress, "Polygons On Triangulation", 0, nbpOntri, 1);
1231   if (Compact)
1232     OS << "PolygonOnTriangulations " << nbpOntri << endl;
1233   else {
1234     OS << " -------\n";
1235     OS <<"Dump of " << nbpOntri << " PolygonOnTriangulations\n";
1236     OS << " -------\n";
1237   }
1238
1239   Handle(Poly_PolygonOnTriangulation) Poly;
1240   Handle(TColStd_HArray1OfReal) Param;
1241   for (i=1; i<=nbpOntri && PS.More(); i++, PS.Next()) {
1242     Poly = Handle(Poly_PolygonOnTriangulation)::DownCast(myNodes(i));
1243     const TColStd_Array1OfInteger& Nodes = Poly->Nodes();
1244     if (!Compact) {
1245       OS << "  "<< i << " : PolygonOnTriangulation with " << Nodes.Length() << " Nodes\n";
1246     }
1247     else OS << Nodes.Length()<<" ";
1248     if (!Compact) OS <<"  ";
1249     for (j=1; j <= Nodes.Length(); j++) OS << Nodes.Value(j) << " ";
1250     OS << "\n";
1251
1252     // writing parameters:
1253     Param = Poly->Parameters();
1254     if (Compact) OS <<"p ";
1255
1256     // write the deflection
1257     if (!Compact) OS << "  Deflection : ";
1258     OS <<Poly->Deflection() << " ";
1259     if (!Compact) OS << "\n";
1260     
1261     if (!Param.IsNull()) {
1262       if (!Compact) {
1263         OS << "  "<< "Parameters :";
1264       }
1265       else OS << "1 " ;
1266       if (!Compact) OS <<"  ";
1267       for (j=1; j <= Param->Length(); j++) OS << Param->Value(j) << " ";
1268       OS << "\n";
1269     }
1270     else OS <<"0 \n";
1271   }
1272 }
1273
1274 //=======================================================================
1275 //function : DumpPolygonOnTriangulation
1276 //purpose  : 
1277 //=======================================================================
1278
1279 void BRepTools_ShapeSet::DumpPolygonOnTriangulation(Standard_OStream& OS)const
1280 {
1281   WritePolygonOnTriangulation(OS, Standard_False);
1282 }
1283
1284 //=======================================================================
1285 //function : ReadPolygonOnTriangulation
1286 //purpose  : 
1287 //=======================================================================
1288
1289 void BRepTools_ShapeSet::ReadPolygonOnTriangulation(Standard_IStream& IS)
1290 {
1291   char buffer[255];
1292   IS >> buffer;
1293   if (strstr(buffer,"PolygonOnTriangulations") == NULL) return;
1294   Standard_Integer i, j, val, nbpol = 0, nbnodes =0;
1295   Standard_Integer hasparameters;
1296   Standard_Real par;
1297   Handle(TColStd_HArray1OfReal) Param;
1298   Handle(Poly_PolygonOnTriangulation) Poly;
1299   IS >> nbpol;
1300   //OCC19559
1301   Handle(Message_ProgressIndicator) progress = GetProgress();
1302   Message_ProgressSentry PS(progress, "Polygons On Triangulation", 0, nbpol, 1);
1303   for (i=1; i<=nbpol&& PS.More(); i++, PS.Next()) {
1304     IS >> nbnodes;
1305     TColStd_Array1OfInteger Nodes(1, nbnodes);
1306     for (j = 1; j <= nbnodes; j++) {
1307       IS >> val;
1308       Nodes(j) = val;
1309     }
1310     IS >> buffer;
1311 //      if (!strcasecmp(buffer, "p")) {
1312       Standard_Real def;
1313       IS >> def;
1314       IS >> hasparameters;
1315       if (hasparameters) {
1316         TColStd_Array1OfReal Param1(1, nbnodes);
1317         for (j = 1; j <= nbnodes; j++) {
1318           IS >> par;
1319           Param1(j) = par;
1320         }
1321         Poly = new Poly_PolygonOnTriangulation(Nodes, Param1);
1322       }
1323       else Poly = new Poly_PolygonOnTriangulation(Nodes);
1324       Poly->Deflection(def);
1325 //      }
1326 //      else {
1327 //      IS.seekg(ppp);
1328 //      Poly = new Poly_PolygonOnTriangulation(Nodes);
1329 //      }
1330     myNodes.Add(Poly);
1331   }
1332 //  }
1333 //  else IS.seekg(pos);
1334 }
1335
1336
1337
1338 //=======================================================================
1339 //function : WritePolygon3D
1340 //purpose  : 
1341 //=======================================================================
1342
1343 void BRepTools_ShapeSet::WritePolygon3D(Standard_OStream&      OS,
1344                                         const Standard_Boolean Compact)const
1345 {
1346   Standard_Integer i, j, nbpol = myPolygons3D.Extent();
1347   
1348   Handle(Message_ProgressIndicator) progress = GetProgress();
1349   Message_ProgressSentry PS(progress, "3D Poligons", 0, nbpol, 1);
1350
1351   if (Compact)
1352     OS << "Polygon3D " << nbpol << endl;
1353   else {
1354     OS << " -------\n";
1355     OS <<"Dump of " << nbpol << " Polygon3Ds\n";
1356     OS << " -------\n";
1357   }
1358   
1359   Handle(Poly_Polygon3D) P;
1360   for (i = 1; i <= nbpol && PS.More(); i++, PS.Next()) {
1361     P = Handle(Poly_Polygon3D)::DownCast(myPolygons3D(i));
1362     if (Compact) {
1363       OS << P->NbNodes() << " ";
1364       OS << ((P->HasParameters()) ? "1" : "0") << "\n";
1365     }
1366     else {
1367       OS << "  "<< i << " : Polygon3D with " << P->NbNodes() << " Nodes\n";
1368       OS << ((P->HasParameters()) ? "with" : "without") << " parameters\n";
1369     }
1370     
1371
1372     // write the deflection
1373     if (!Compact) OS << "Deflection : ";
1374     OS << P->Deflection() << "\n";
1375     
1376     // write the nodes
1377     if (!Compact) OS << "\nNodes :\n";
1378     
1379     Standard_Integer i1, nbNodes = P->NbNodes();
1380     const TColgp_Array1OfPnt& Nodes = P->Nodes();
1381     for (j = 1; j <= nbNodes; j++) {
1382       if (!Compact) OS << setw(10) << j << " : ";
1383       if (!Compact) OS << setw(17);
1384       OS << Nodes(j).X() << " ";
1385       if (!Compact) OS << setw(17);
1386       OS << Nodes(j).Y() << " ";
1387       if (!Compact) OS << setw(17);
1388       OS << Nodes(j).Z();
1389       if (!Compact) OS << "\n";
1390       else OS << " ";
1391     }
1392     OS <<"\n";
1393   
1394     if (P->HasParameters()) {
1395       if (!Compact) OS << "\nParameters :\n";
1396       const TColStd_Array1OfReal& Param = P->Parameters();
1397       for ( i1 = 1; i1 <= nbNodes; i1++ ) {
1398         OS << Param(i1) << " ";
1399       }
1400       OS <<"\n";
1401     }
1402   }
1403 }
1404
1405 //=======================================================================
1406 //function : DumpPolygon3D
1407 //purpose  : 
1408 //=======================================================================
1409
1410 void BRepTools_ShapeSet::DumpPolygon3D(Standard_OStream& OS)const
1411 {
1412   WritePolygon3D(OS, Standard_False);
1413 }
1414
1415
1416 //=======================================================================
1417 //function : ReadPolygon3D
1418 //purpose  : 
1419 //=======================================================================
1420
1421 void BRepTools_ShapeSet::ReadPolygon3D(Standard_IStream& IS)
1422 {
1423   char buffer[255];
1424   //  Standard_Integer i, j, p, val, nbpol, nbnodes, hasparameters;
1425   Standard_Integer i, j, p, nbpol=0, nbnodes =0, hasparameters = Standard_False;
1426   Standard_Real d, x, y, z;
1427
1428   IS >> buffer;
1429   if (strstr(buffer,"Polygon3D") == NULL) return;
1430   Handle(Poly_Polygon3D) P;
1431   IS >> nbpol;
1432   //OCC19559
1433   Handle(Message_ProgressIndicator) progress = GetProgress();
1434   Message_ProgressSentry PS(progress, "3D Polygons", 0, nbpol, 1);
1435   for (i=1; i<=nbpol && PS.More(); i++, PS.Next()) {
1436     IS >> nbnodes;
1437     IS >> hasparameters;
1438     TColgp_Array1OfPnt Nodes(1, nbnodes);
1439     IS >> d;
1440     for (j = 1; j <= nbnodes; j++) {
1441       IS >> x >> y >> z;
1442       Nodes(j).SetCoord(x,y,z);
1443     }
1444     if (hasparameters) {
1445       TColStd_Array1OfReal Param(1,nbnodes);
1446       for (p = 1; p <= nbnodes; p++) {
1447         IS >> Param(p);
1448       }
1449       P = new Poly_Polygon3D(Nodes, Param);
1450     }
1451     else P = new Poly_Polygon3D(Nodes);
1452     P->Deflection(d);
1453     myPolygons3D.Add(P);
1454   }
1455 }
1456
1457
1458
1459 //=======================================================================
1460 //function : WriteTriangulation
1461 //purpose  : 
1462 //=======================================================================
1463
1464 void BRepTools_ShapeSet::WriteTriangulation(Standard_OStream&      OS,
1465                                         const Standard_Boolean Compact)const
1466 {
1467   Standard_Integer i, j, nbNodes, nbtri = myTriangulations.Extent();
1468   Standard_Integer nbTriangles = 0, n1, n2, n3;
1469   
1470   Handle(Message_ProgressIndicator) progress = GetProgress();
1471   Message_ProgressSentry PS(progress, "Triangulations", 0, nbtri, 1);
1472
1473   if (Compact)
1474     OS << "Triangulations " << nbtri << endl;
1475   else {
1476     OS << " -------\n";
1477     OS <<"Dump of " << nbtri << " Triangulations\n";
1478     OS << " -------\n";
1479   }
1480
1481   Handle(Poly_Triangulation) T;
1482   for (i = 1; i <= nbtri && PS.More(); i++, PS.Next()) {
1483
1484     T = Handle(Poly_Triangulation)::DownCast(myTriangulations(i));
1485     if (Compact) {
1486       OS << T->NbNodes() << " " << T->NbTriangles() << " ";
1487       OS << ((T->HasUVNodes()) ? "1" : "0") << " ";
1488     }
1489     else {
1490       OS << "  "<< i << " : Triangulation with " << T->NbNodes() << " Nodes and "
1491          << T->NbTriangles() <<" Triangles\n";
1492       OS << "      "<<((T->HasUVNodes()) ? "with" : "without") << " UV nodes\n";
1493
1494     }
1495     
1496     // write the deflection
1497     
1498     if (!Compact) OS << "  Deflection : ";
1499     OS <<T->Deflection() << "\n";
1500     
1501     // write the 3d nodes
1502     
1503     if (!Compact) OS << "\n3D Nodes :\n";
1504     
1505     nbNodes = T->NbNodes();
1506     const TColgp_Array1OfPnt& Nodes = T->Nodes();
1507     for (j = 1; j <= nbNodes; j++) {
1508       if (!Compact) OS << setw(10) << j << " : ";
1509       if (!Compact) OS << setw(17);
1510       OS << Nodes(j).X() << " ";
1511       if (!Compact) OS << setw(17);
1512       OS << Nodes(j).Y() << " ";
1513       if (!Compact) OS << setw(17);
1514       OS << Nodes(j).Z();
1515       if (!Compact) OS << "\n";
1516       else OS << " ";
1517     }
1518     
1519     if (T->HasUVNodes()) {
1520       if (!Compact) OS << "\nUV Nodes :\n";
1521       const TColgp_Array1OfPnt2d& UVNodes = T->UVNodes();
1522       for (j = 1; j <= nbNodes; j++) {
1523         if (!Compact) OS << setw(10) << j << " : ";
1524         if (!Compact) OS << setw(17);
1525         OS << UVNodes(j).X() << " ";
1526         if (!Compact) OS << setw(17);
1527         OS << UVNodes(j).Y();
1528         if (!Compact) OS << "\n";
1529         else OS << " ";
1530       }
1531     }
1532     
1533     if (!Compact) OS << "\nTriangles :\n";
1534     nbTriangles = T->NbTriangles();
1535     const Poly_Array1OfTriangle& Triangles = T->Triangles();
1536     for (j = 1; j <= nbTriangles; j++) {
1537       if (!Compact) OS << setw(10) << j << " : ";
1538       Triangles(j).Get(n1, n2, n3);
1539       if (!Compact) OS << setw(10);
1540       OS << n1 << " ";
1541       if (!Compact) OS << setw(10);
1542       OS << n2 << " ";
1543       if (!Compact) OS << setw(10);
1544       OS << n3;
1545       if (!Compact) OS << "\n";
1546       else OS << " ";
1547     }
1548     OS << "\n";
1549   }
1550 }
1551
1552 //=======================================================================
1553 //function : DumpTriangulation
1554 //purpose  : 
1555 //=======================================================================
1556
1557 void BRepTools_ShapeSet::DumpTriangulation(Standard_OStream& OS)const
1558 {
1559   WriteTriangulation(OS, Standard_False);
1560 }
1561
1562
1563 //=======================================================================
1564 //function : ReadTriangulation
1565 //purpose  : 
1566 //=======================================================================
1567
1568 void BRepTools_ShapeSet::ReadTriangulation(Standard_IStream& IS)
1569 {
1570   char buffer[255];
1571   //  Standard_Integer i, j, val, nbtri;
1572   Standard_Integer i, j, nbtri =0;
1573   Standard_Real d, x, y, z;
1574   Standard_Integer nbNodes =0, nbTriangles=0;
1575   Standard_Boolean hasUV= Standard_False;
1576
1577   Handle(Poly_Triangulation) T;
1578
1579   IS >> buffer;
1580   if (strstr(buffer,"Triangulations") == NULL) return;
1581
1582   IS >> nbtri;
1583   //OCC19559
1584   Handle(Message_ProgressIndicator) progress = GetProgress();
1585   Message_ProgressSentry PS(progress, "Triangulations", 0, nbtri, 1);
1586   for (i=1; i<=nbtri && PS.More();i++, PS.Next()) {
1587
1588     IS >> nbNodes >> nbTriangles >> hasUV;
1589     IS >> d;
1590
1591     TColgp_Array1OfPnt Nodes(1, nbNodes);
1592     TColgp_Array1OfPnt2d UVNodes(1, nbNodes);
1593
1594     for (j = 1; j <= nbNodes; j++) {
1595       IS >> x >> y >> z;
1596       Nodes(j).SetCoord(x,y,z);
1597     }
1598
1599     if (hasUV) {
1600       for (j = 1; j <= nbNodes; j++) {
1601         IS >> x >> y;
1602         UVNodes(j).SetCoord(x,y);
1603       }
1604     }
1605       
1606     // read the triangles
1607     Standard_Integer n1,n2,n3;
1608     Poly_Array1OfTriangle Triangles(1, nbTriangles);
1609     for (j = 1; j <= nbTriangles; j++) {
1610       IS >> n1 >> n2 >> n3;
1611       Triangles(j).Set(n1,n2,n3);
1612     }
1613       
1614     if (hasUV) T =  new Poly_Triangulation(Nodes,UVNodes,Triangles);
1615     else T = new Poly_Triangulation(Nodes,Triangles);
1616       
1617     T->Deflection(d);
1618       
1619     myTriangulations.Add(T);
1620   }
1621 }