0022312: Translation of french commentaries in OCCT files
[occt.git] / src / BRep / BRep_Tool.cxx
1 // File:      BRep_Tool.cxx
2 // Created:   Wed Jul  7 15:35:57 1993
3 // Author:    Remi LEQUETTE
4 //            <rle@phylox>
5
6
7 #include <BRep_Tool.ixx>
8 #include <BRep_TFace.hxx>
9 #include <BRep_TEdge.hxx>
10 #include <BRep_TVertex.hxx>
11 #include <BRep_CurveRepresentation.hxx>
12 #include <BRep_CurveOnSurface.hxx>
13 #include <BRep_CurveOnClosedSurface.hxx>
14 #include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
15 #include <BRep_PointRepresentation.hxx>
16 #include <BRep_ListIteratorOfListOfPointRepresentation.hxx>
17 #include <BRep_Curve3D.hxx>
18 #include <BRep_Polygon3D.hxx>
19 #include <BRep_PolygonOnSurface.hxx>
20 #include <BRep_PolygonOnClosedSurface.hxx>
21 #include <BRep_PolygonOnTriangulation.hxx>
22 #include <BRep_PolygonOnClosedTriangulation.hxx>
23 #include <TopoDS.hxx>
24 #include <TopoDS_Iterator.hxx>
25 #include <TopExp_Explorer.hxx>
26 #include <TopExp.hxx>
27 #include <TopTools_MapOfShape.hxx>
28 #include <ElSLib.hxx>
29 #include <Geom_Plane.hxx>
30 #include <Geom_RectangularTrimmedSurface.hxx>
31 #include <Geom_OffsetSurface.hxx>
32 #include <Geom_TrimmedCurve.hxx>
33 #include <Geom2d_TrimmedCurve.hxx>
34 #include <ProjLib_ProjectedCurve.hxx>
35 #include <GeomProjLib.hxx>
36 #include <Geom2dAdaptor.hxx>
37 #include <GeomAdaptor_HCurve.hxx>
38 #include <GeomAdaptor_HSurface.hxx>
39 #include <Precision.hxx>
40 #include <Poly_Triangulation.hxx>
41 #include <Poly_Polygon3D.hxx>
42 #include <Poly_Polygon2D.hxx>
43 #include <Poly_PolygonOnTriangulation.hxx>
44
45 //modified by NIZNHY-PKV Fri Oct 17 14:13:29 2008f
46 static 
47   Standard_Boolean IsPlane(const Handle(Geom_Surface)& aS);
48 //modified by NIZNHY-PKV Fri Oct 17 14:13:33 2008t
49 //
50 //=======================================================================
51 //function : Surface
52 //purpose  : Returns the geometric surface of the face. Returns
53 //           in <L> the location for the surface.
54 //=======================================================================
55
56 const Handle(Geom_Surface)& BRep_Tool::Surface(const TopoDS_Face& F,
57                                                TopLoc_Location& L)
58 {
59   Handle(BRep_TFace)& TF = *((Handle(BRep_TFace)*) &F.TShape());
60   L = F.Location() * TF->Location();
61   return TF->Surface();
62 }
63
64 //=======================================================================
65 //function : Surface
66 //purpose  : Returns the geometric  surface of the face. It can
67 //           be a copy if there is a Location.
68 //=======================================================================
69
70 Handle(Geom_Surface) BRep_Tool::Surface(const TopoDS_Face& F)
71 {
72   Handle(BRep_TFace)& TF = *((Handle(BRep_TFace)*) &F.TShape());
73   TopLoc_Location L = F.Location() * TF->Location();
74   Handle(Geom_Surface) S = TF->Surface();
75
76   if(S.IsNull()) return S;
77
78   Handle(Geom_Geometry) S1;
79   if (!L.IsIdentity()) {
80     S1 = S->Copy();
81     S = *((Handle(Geom_Surface)*)&S1);
82     S->Transform(L.Transformation());
83   }
84   return S;
85 }
86
87 //=======================================================================
88 //function : Triangulation
89 //purpose  : Returns  the Triangulation of  the  face. It  is a
90 //           null handle if there is no triangulation.
91 //=======================================================================
92
93 const Handle(Poly_Triangulation)&
94 BRep_Tool::Triangulation(const TopoDS_Face& F,
95                          TopLoc_Location&   L)
96 {
97   L = F.Location();
98   return (*((Handle(BRep_TFace)*)&F.TShape()))->Triangulation();
99 }
100
101 //=======================================================================
102 //function : Tolerance
103 //purpose  : Returns the tolerance of the face.
104 //=======================================================================
105
106 Standard_Real  BRep_Tool::Tolerance(const TopoDS_Face& F)
107 {
108   Standard_Real p = (*((Handle(BRep_TFace)*)&F.TShape()))->Tolerance();
109   Standard_Real pMin = Precision::Confusion();
110   if (p > pMin) return p;
111   else          return pMin;
112 }
113
114 //=======================================================================
115 //function : NaturalRestriction
116 //purpose  : Returns the  NaturalRestriction  flag of the  face.
117 //=======================================================================
118
119 Standard_Boolean  BRep_Tool::NaturalRestriction(const TopoDS_Face& F)
120 {
121   return (*((Handle(BRep_TFace)*) &F.TShape()))->NaturalRestriction();
122 }
123
124 //=======================================================================
125 //function : Curve
126 //purpose  : Returns the 3D curve  of the edge.  May be  a Null
127 //           handle. Returns in <L> the location for the curve.
128 //           In <First> and <Last> the parameter range.
129 //=======================================================================
130
131 static Handle(Geom_Curve) nullCurve;
132 static Handle(Poly_Polygon3D) nullPolygon3D;
133
134 const Handle(Geom_Curve)&  BRep_Tool::Curve(const TopoDS_Edge& E,
135                                             TopLoc_Location& L,
136                                             Standard_Real& First,
137                                             Standard_Real& Last)
138 {
139   // find the representation
140   BRep_ListIteratorOfListOfCurveRepresentation itcr
141     ((*((Handle(BRep_TEdge)*)&E.TShape()))->ChangeCurves());
142
143   while (itcr.More()) {
144     const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
145     if (cr->IsCurve3D()) {
146       const Handle(BRep_Curve3D)& GC = *((Handle(BRep_Curve3D)*)&cr);
147       L = E.Location() * GC->Location();
148       GC->Range(First,Last);
149       return GC->Curve3D();
150     }
151     itcr.Next();
152   }
153   L.Identity();
154   return nullCurve;
155 }
156
157 //=======================================================================
158 //function : Curve
159 //purpose  : Returns the 3D curve  of the edge. May be a Null handle.
160 //           In <First> and <Last> the parameter range.
161 //           It can be a copy if there is a Location.
162 //=======================================================================
163
164 Handle(Geom_Curve)  BRep_Tool::Curve(const TopoDS_Edge& E,
165                                      Standard_Real& First,
166                                      Standard_Real& Last)
167 {
168   TopLoc_Location L;
169   Handle(Geom_Curve) C = Curve(E,L,First,Last);
170   if ( !C.IsNull() ) {
171     Handle(Geom_Geometry) C1;
172     if ( !L.IsIdentity() ) {
173       C1 = C->Copy();
174       C = *((Handle(Geom_Curve)*)&C1);
175       C->Transform(L.Transformation());
176     }
177   }
178   return C;
179 }
180
181 //=======================================================================
182 //function : IsGeometric
183 //purpose  : Returns True if <E> is a 3d curve or a curve on
184 //           surface.
185 //=======================================================================
186
187 Standard_Boolean  BRep_Tool::IsGeometric(const TopoDS_Edge& E)
188 {
189   // find the representation
190   BRep_ListIteratorOfListOfCurveRepresentation itcr
191     ((*((Handle(BRep_TEdge)*)&E.TShape()))->ChangeCurves());
192
193   while (itcr.More()) {
194     const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
195     if (cr->IsCurve3D()) {
196       Standard_Real first, last;
197       TopLoc_Location L;
198       const Handle(Geom_Curve)&  C = BRep_Tool::Curve(E, L, first, last);
199       if (!C.IsNull()) return Standard_True;
200     }
201     else if (cr->IsCurveOnSurface()) return Standard_True;
202     itcr.Next();
203   }
204   return Standard_False;
205 }
206
207 //=======================================================================
208 //function : Polygon3D
209 //purpose  : Returns the 3D polygon of the edge. May be   a Null
210 //           handle. Returns in <L> the location for the polygon.
211 //=======================================================================
212
213 const Handle(Poly_Polygon3D)& BRep_Tool::Polygon3D(const TopoDS_Edge& E,
214                                                    TopLoc_Location&   L)
215 {
216   // find the representation
217   BRep_ListIteratorOfListOfCurveRepresentation itcr
218     ((*((Handle(BRep_TEdge)*)&E.TShape()))->ChangeCurves());
219
220   while (itcr.More()) {
221     const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
222     if (cr->IsPolygon3D()) {
223       const Handle(BRep_Polygon3D)& GC = *((Handle(BRep_Polygon3D)*)&cr);
224       L = E.Location() * GC->Location();
225       return GC->Polygon3D();
226     }
227     itcr.Next();
228   }
229   L.Identity();
230   return nullPolygon3D;
231 }
232
233 //=======================================================================
234 //function : CurveOnSurface
235 //purpose  : Returns the curve  associated to the  edge in  the
236 //           parametric  space of  the  face.  Returns   a NULL
237 //           handle  if this curve  does not exist.  Returns in
238 //           <First> and <Last> the parameter range.
239 //=======================================================================
240
241 Handle(Geom2d_Curve) BRep_Tool::CurveOnSurface(const TopoDS_Edge& E, 
242                                                const TopoDS_Face& F,
243                                                Standard_Real& First,
244                                                Standard_Real& Last)
245 {
246   TopLoc_Location l;
247   const Handle(Geom_Surface)& S = BRep_Tool::Surface(F,l);
248   TopoDS_Edge aLocalEdge = E;
249   if (F.Orientation() == TopAbs_REVERSED) {
250     aLocalEdge.Reverse();
251 //    return CurveOnSurface(E,S,l,First,Last);
252   }
253 //    return CurveOnSurface(TopoDS::Edge(E.Reversed()),S,l,First,Last);
254 //  else
255 //    return CurveOnSurface(E,S,l,First,Last);
256   return CurveOnSurface(aLocalEdge,S,l,First,Last);
257 }
258
259 //=======================================================================
260 //function : CurveOnSurface
261 //purpose  : Returns the  curve associated to   the edge in the
262 //           parametric  space of the   surface. Returns a NULL
263 //           handle  if this curve does  not exist.  Returns in
264 //           <First> and <Last> the parameter range.
265 //=======================================================================
266
267 static Handle(Geom2d_Curve) nullPCurve;
268
269 Handle(Geom2d_Curve) BRep_Tool::CurveOnSurface(const TopoDS_Edge& E, 
270                                                const Handle(Geom_Surface)& S,
271                                                const TopLoc_Location& L,
272                                                Standard_Real& First,
273                                                Standard_Real& Last)
274 {
275   TopLoc_Location loc = L.Predivided(E.Location());
276   Standard_Boolean Eisreversed = (E.Orientation() == TopAbs_REVERSED);
277
278   // find the representation
279   BRep_ListIteratorOfListOfCurveRepresentation itcr
280     ((*((Handle(BRep_TEdge)*)&E.TShape()))->ChangeCurves());
281
282   while (itcr.More()) {
283     const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
284     if (cr->IsCurveOnSurface(S,loc)) {
285       const Handle(BRep_GCurve)& GC = *((Handle(BRep_GCurve)*)&cr);
286       GC->Range(First,Last);
287       if (GC->IsCurveOnClosedSurface() && Eisreversed)
288         return GC->PCurve2();
289       else
290         return GC->PCurve();
291     }
292     itcr.Next();
293   }
294   
295
296   // for planar surface and 3d curve try a projection
297   // modif 21-05-97 : for RectangularTrimmedSurface, try a projection
298   Handle(Geom_Plane) GP;
299   Handle(Geom_RectangularTrimmedSurface) GRTS;
300   GRTS = Handle(Geom_RectangularTrimmedSurface)::DownCast(S);
301   if(!GRTS.IsNull())
302     GP = Handle(Geom_Plane)::DownCast(GRTS->BasisSurface());
303   else
304     GP = Handle(Geom_Plane)::DownCast(S);
305   //fin modif du 21-05-97
306
307   if (!GP.IsNull()) {
308
309     Handle(GeomAdaptor_HCurve) HC;
310     Handle(GeomAdaptor_HSurface) HS;
311
312     HC = new GeomAdaptor_HCurve();
313     HS = new GeomAdaptor_HSurface();
314
315     TopLoc_Location LC;
316
317     Standard_Real f, l;// for those who call with (u,u).
318     Handle(Geom_Curve) C3d =
319       BRep_Tool::Curve(E,/*LC,*/f,l); // transforming plane instead of curve
320     // we can loose scale factor of Curve transformation (eap 13 May 2002)
321
322     LC = L/*.Predivided(LC)*/;
323
324     if (C3d.IsNull()) return nullPCurve;
325
326     Handle(Geom_Plane) Plane = GP;
327     if (!LC.IsIdentity()) {
328       const gp_Trsf& T = LC.Transformation();
329       Handle(Geom_Geometry) GPT = GP->Transformed(T);
330       Plane = *((Handle(Geom_Plane)*)&GPT);
331     }
332     GeomAdaptor_Surface& GAS = HS->ChangeSurface();
333     GAS.Load(Plane);
334     
335     Handle(Geom_Curve) ProjOnPlane = 
336       GeomProjLib::ProjectOnPlane(new Geom_TrimmedCurve(C3d,f,l),
337                                   Plane,
338                                   Plane->Position().Direction(),
339                                   Standard_True);
340     
341     GeomAdaptor_Curve& GAC = HC->ChangeCurve();
342     GAC.Load(ProjOnPlane);
343
344     ProjLib_ProjectedCurve Proj(HS,HC);
345     Handle(Geom2d_Curve) pc = Geom2dAdaptor::MakeCurve(Proj);
346
347     if (pc->DynamicType() == STANDARD_TYPE(Geom2d_TrimmedCurve)) {
348       Handle(Geom2d_TrimmedCurve) TC = 
349         (*((Handle(Geom2d_TrimmedCurve)*)&pc));
350       pc = TC->BasisCurve();
351     }
352     First = f; Last = l;
353     return pc;
354   }
355   
356   return nullPCurve;
357 }
358
359 //=======================================================================
360 //function : CurveOnSurface
361 //purpose  : 
362 //=======================================================================
363
364 void  BRep_Tool::CurveOnSurface(const TopoDS_Edge& E, 
365                                 Handle(Geom2d_Curve)& C, 
366                                 Handle(Geom_Surface)& S, 
367                                 TopLoc_Location& L,
368                                 Standard_Real& First,
369                                 Standard_Real& Last)
370 {
371   // find the representation
372   BRep_ListIteratorOfListOfCurveRepresentation itcr
373     ((*((Handle(BRep_TEdge)*)&E.TShape()))->ChangeCurves());
374
375   while (itcr.More()) {
376     const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
377     if (cr->IsCurveOnSurface()) {
378       const Handle(BRep_GCurve)& GC = *((Handle(BRep_GCurve)*)&cr);
379       C = GC->PCurve();
380       S = GC->Surface();
381       L = E.Location() * GC->Location();
382       GC->Range(First,Last);
383       return;
384     }
385     itcr.Next();
386   }
387   
388   C = Handle(Geom2d_Curve)();
389   S = Handle(Geom_Surface)();
390   L = TopLoc_Location();
391 }
392
393 //=======================================================================
394 //function : CurveOnSurface
395 //purpose  : 
396 //=======================================================================
397
398 void  BRep_Tool::CurveOnSurface(const TopoDS_Edge& E, 
399                                 Handle(Geom2d_Curve)& C, 
400                                 Handle(Geom_Surface)& S, 
401                                 TopLoc_Location& L,
402                                 Standard_Real& First,
403                                 Standard_Real& Last,
404                                 const Standard_Integer Index)
405 {
406   Standard_Integer i = 0;
407   Standard_Boolean Eisreversed = (E.Orientation() == TopAbs_REVERSED);
408
409   // find the representation
410   BRep_ListIteratorOfListOfCurveRepresentation itcr
411     ((*((Handle(BRep_TEdge)*)&E.TShape()))->ChangeCurves());
412
413   while (itcr.More()) {
414     const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
415     if (cr->IsCurveOnSurface()) {
416       const Handle(BRep_GCurve)& GC = *((Handle(BRep_GCurve)*)&cr);
417       i++;
418       if (i > Index) break;
419       if (i == Index) {
420         // JMB le 21 Mai 1999
421         // it is done as in the other CurveOnSurface methods, ie. take into account
422         // the orientation in case of cut edges (return PCurve2)
423         // otherwise there is a risk to loop curves or to not get the prover one.
424         if (GC->IsCurveOnClosedSurface() && Eisreversed)
425           C = GC->PCurve2();
426         else
427           C = GC->PCurve();
428         S = GC->Surface();
429         L = E.Location() * GC->Location();
430         GC->Range(First,Last);
431         return;
432       }
433     }
434     itcr.Next();
435   }
436   
437   C = Handle(Geom2d_Curve)();
438   S = Handle(Geom_Surface)();
439   L = TopLoc_Location();
440 }
441
442 //=======================================================================
443 //function : PolygonOnSurface
444 //purpose  : Returns the polygon associated to the  edge in  the
445 //           parametric  space of  the  face.  Returns   a NULL
446 //           handle  if this polygon  does not exist.
447 //=======================================================================
448
449 Handle(Poly_Polygon2D) BRep_Tool::PolygonOnSurface(const TopoDS_Edge& E, 
450                                                    const TopoDS_Face& F)
451 {
452   TopLoc_Location l;
453   const Handle(Geom_Surface)& S = BRep_Tool::Surface(F,l);
454   TopoDS_Edge aLocalEdge = E;
455   if (F.Orientation() == TopAbs_REVERSED) {
456     aLocalEdge.Reverse();
457 //    return PolygonOnSurface(E,S,l);
458   }
459   //    return PolygonOnSurface(TopoDS::Edge(E.Reversed()),S,l);
460 //  else
461 //    return PolygonOnSurface(E,S,l);
462   return PolygonOnSurface(aLocalEdge,S,l);
463 }
464
465 //=======================================================================
466 //function : PolygonOnSurface
467 //purpose  : Returns the polygon associated to the  edge in  the
468 //           parametric  space of  the surface. Returns   a NULL
469 //           handle  if this polygon  does not exist.
470 //=======================================================================
471
472 static Handle(Poly_Polygon2D) nullPolygon2D;
473
474 Handle(Poly_Polygon2D) 
475      BRep_Tool::PolygonOnSurface(const TopoDS_Edge& E, 
476                                  const Handle(Geom_Surface)& S,
477                                  const TopLoc_Location& L)
478 {
479   TopLoc_Location l = L.Predivided(E.Location());
480   Standard_Boolean Eisreversed = (E.Orientation() == TopAbs_REVERSED);
481
482   // find the representation
483   BRep_ListIteratorOfListOfCurveRepresentation itcr
484     ((*((Handle(BRep_TEdge)*)&E.TShape()))->ChangeCurves());
485
486   while (itcr.More()) {
487     const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
488     if (cr->IsPolygonOnSurface(S,l)) {
489       if (cr->IsPolygonOnClosedSurface() && Eisreversed )
490         return cr->Polygon2();
491       else
492         return cr->Polygon();
493     }
494     itcr.Next();
495   }
496   
497   return nullPolygon2D;
498 }
499
500 //=======================================================================
501 //function : PolygonOnSurface
502 //purpose  : 
503 //=======================================================================
504
505 void BRep_Tool::PolygonOnSurface(const TopoDS_Edge&      E,
506                                  Handle(Poly_Polygon2D)& P,
507                                  Handle(Geom_Surface)&   S,
508                                  TopLoc_Location&        L)
509 {
510   // find the representation
511   BRep_ListIteratorOfListOfCurveRepresentation itcr
512     ((*((Handle(BRep_TEdge)*)&E.TShape()))->ChangeCurves());
513
514   while (itcr.More()) {
515     const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
516     if (cr->IsPolygonOnSurface()) {
517       const Handle(BRep_PolygonOnSurface)& PS = 
518         *((Handle(BRep_PolygonOnSurface)*)&cr);
519       P = PS->Polygon();
520       S = PS->Surface();
521       L = E.Location() * PS->Location();
522       return;
523     }
524     itcr.Next();
525   }
526   
527   L = TopLoc_Location();
528   P = Handle(Poly_Polygon2D)();
529   S = Handle(Geom_Surface)();
530 }
531
532 //=======================================================================
533 //function : PolygonOnSurface
534 //purpose  : 
535 //=======================================================================
536
537 void BRep_Tool::PolygonOnSurface(const TopoDS_Edge&      E,
538                                  Handle(Poly_Polygon2D)& P,
539                                  Handle(Geom_Surface)&   S,
540                                  TopLoc_Location&        L,
541                                  const Standard_Integer  Index)
542 {
543   Standard_Integer i = 0;
544
545   // find the representation
546   BRep_ListIteratorOfListOfCurveRepresentation itcr
547     ((*((Handle(BRep_TEdge)*)&E.TShape()))->ChangeCurves());
548
549   while (itcr.More()) {
550     const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
551     if (cr->IsPolygonOnSurface()) {
552       const Handle(BRep_PolygonOnSurface)& PS = 
553         *((Handle(BRep_PolygonOnSurface)*)&cr);
554       i++;
555       if (i > Index) break;
556       if (i == Index) {
557         P = PS->Polygon();
558         S = PS->Surface();
559         L = E.Location() * PS->Location();
560         return;
561       }
562     }
563     itcr.Next();
564   }
565   
566   L = TopLoc_Location();
567   P = Handle(Poly_Polygon2D)();
568   S = Handle(Geom_Surface)();
569 }
570
571 //=======================================================================
572 //function : PolygonOnTriangulation
573 //purpose  : Returns the polygon associated to the  edge in  the
574 //           parametric  space of  the  face.  Returns   a NULL
575 //           handle  if this polygon  does not exist.
576 //=======================================================================
577
578 static Handle(Poly_PolygonOnTriangulation) nullArray;
579
580 const Handle(Poly_PolygonOnTriangulation)&
581 BRep_Tool::PolygonOnTriangulation(const TopoDS_Edge&                E, 
582                                   const Handle(Poly_Triangulation)& T,
583                                   const TopLoc_Location&            L)
584 {
585   TopLoc_Location l = L.Predivided(E.Location());
586   Standard_Boolean Eisreversed = (E.Orientation() == TopAbs_REVERSED);
587
588   // find the representation
589   BRep_ListIteratorOfListOfCurveRepresentation itcr
590     ((*((Handle(BRep_TEdge)*)&E.TShape()))->ChangeCurves());
591
592   while (itcr.More()) {
593     const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
594     if ( cr->IsPolygonOnTriangulation(T,l)) {
595       if ( cr->IsPolygonOnClosedTriangulation() && Eisreversed )
596         return cr->PolygonOnTriangulation2();
597       else
598         return cr->PolygonOnTriangulation();
599     }
600     itcr.Next();
601   }
602   
603   return nullArray;
604 }
605
606 //=======================================================================
607 //function : PolygonOnTriangulation
608 //purpose  : 
609 //=======================================================================
610
611 void 
612 BRep_Tool::PolygonOnTriangulation(const TopoDS_Edge&                   E,
613                                   Handle(Poly_PolygonOnTriangulation)& P,
614                                   Handle(Poly_Triangulation)&          T,
615                                   TopLoc_Location&                     L)
616 {
617   // find the representation
618   BRep_ListIteratorOfListOfCurveRepresentation itcr
619     ((*((Handle(BRep_TEdge)*)&E.TShape()))->ChangeCurves());
620
621   while (itcr.More()) {
622     const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
623     if (cr->IsPolygonOnTriangulation()) {
624       const Handle(BRep_PolygonOnTriangulation)& PT = 
625         *((Handle(BRep_PolygonOnTriangulation)*)&cr);
626       P = PT->PolygonOnTriangulation();
627       T = PT->Triangulation();
628       L = E.Location() * PT->Location();
629       return;
630     }
631     itcr.Next();
632   }
633   
634   L = TopLoc_Location();
635   P = Handle(Poly_PolygonOnTriangulation)();
636   T = Handle(Poly_Triangulation)();
637 }
638
639 //=======================================================================
640 //function : PolygonOnTriangulation
641 //purpose  : 
642 //=======================================================================
643
644 void 
645 BRep_Tool::PolygonOnTriangulation(const TopoDS_Edge&                   E,
646                                   Handle(Poly_PolygonOnTriangulation)& P,
647                                   Handle(Poly_Triangulation)&          T,
648                                   TopLoc_Location&                     L,
649                                   const Standard_Integer               Index)
650 {
651   Standard_Integer i = 0;
652
653   // find the representation
654   BRep_ListIteratorOfListOfCurveRepresentation itcr
655     ((*((Handle(BRep_TEdge)*)&E.TShape()))->ChangeCurves());
656
657   while (itcr.More()) {
658     const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
659     if (cr->IsPolygonOnTriangulation()) {
660       const Handle(BRep_PolygonOnTriangulation)& PT = 
661         *((Handle(BRep_PolygonOnTriangulation)*)&cr);
662       i++;
663       if (i > Index) break;
664       if (i == Index) {
665         T = PT->Triangulation();
666         P = PT->PolygonOnTriangulation();
667         L = E.Location() * PT->Location();
668         return;
669       }
670     }
671     itcr.Next();
672   }
673   
674   L = TopLoc_Location();
675   P = Handle(Poly_PolygonOnTriangulation)();
676   T = Handle(Poly_Triangulation)();
677 }
678
679 //=======================================================================
680 //function : IsClosed
681 //purpose  : Returns  True  if  <E>  has  two  PCurves  in  the
682 //           parametric space of <F>. i.e.  <F>  is on a closed
683 //           surface and <E> is on the closing curve.
684 //=======================================================================
685
686 Standard_Boolean BRep_Tool::IsClosed(const TopoDS_Edge& E, 
687                                      const TopoDS_Face& F)
688 {
689   TopLoc_Location l;
690   const Handle(Geom_Surface)& S = BRep_Tool::Surface(F,l);
691   if (IsClosed(E,S,l)) return Standard_True;
692   return IsClosed(E, BRep_Tool::Triangulation(F,l));
693 }
694
695 //=======================================================================
696 //function : IsClosed
697 //purpose  : Returns  True  if  <E>  has  two  PCurves  in  the
698 //           parametric space  of <S>.  i.e.   <S>  is a closed
699 //           surface and <E> is on the closing curve.
700 //=======================================================================
701
702 Standard_Boolean BRep_Tool::IsClosed(const TopoDS_Edge& E,
703                                      const Handle(Geom_Surface)& S,
704                                      const TopLoc_Location& L)
705 {
706   //modified by NIZNHY-PKV Fri Oct 17 12:16:58 2008f
707   if (IsPlane(S)) {
708     return Standard_False;
709   }
710   //modified by NIZNHY-PKV Fri Oct 17 12:16:54 2008t
711   //
712   TopLoc_Location      l = L.Predivided(E.Location());
713
714   // find the representation
715   BRep_ListIteratorOfListOfCurveRepresentation itcr
716     ((*((Handle(BRep_TEdge)*)&E.TShape()))->ChangeCurves());
717
718   while (itcr.More()) {
719     const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
720     if (cr->IsCurveOnSurface(S,l) &&
721         cr->IsCurveOnClosedSurface())
722       return Standard_True;
723     itcr.Next();
724   }
725   return Standard_False;
726 }
727
728 //=======================================================================
729 //function : IsClosed
730 //purpose  : Returns  True  if <E> has two arrays of indices in
731 //           the triangulation <T>.
732 //=======================================================================
733
734 Standard_Boolean BRep_Tool::IsClosed(const TopoDS_Edge&                E, 
735                                      const Handle(Poly_Triangulation)& T)
736 {
737   TopLoc_Location      l = E.Location();
738
739   // find the representation
740   BRep_ListIteratorOfListOfCurveRepresentation itcr
741     ((*((Handle(BRep_TEdge)*)&E.TShape()))->ChangeCurves());
742
743   while (itcr.More()) {
744     const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
745     if (cr->IsPolygonOnTriangulation(T,l) &&
746         cr->IsPolygonOnClosedTriangulation()) 
747       return Standard_True;
748     itcr.Next();
749   }
750   return Standard_False;
751 }
752
753 //=======================================================================
754 //function : Tolerance
755 //purpose  : Returns the tolerance for <E>.
756 //=======================================================================
757
758 Standard_Real  BRep_Tool::Tolerance(const TopoDS_Edge& E)
759 {
760   Standard_Real p = (*((Handle(BRep_TEdge)*)&E.TShape()))->Tolerance();
761   Standard_Real pMin = Precision::Confusion();
762   if (p > pMin) return p;
763   else          return pMin;
764 }
765
766 //=======================================================================
767 //function : SameParameter
768 //purpose  : Returns the SameParameter flag for the edge.
769 //=======================================================================
770
771 Standard_Boolean  BRep_Tool::SameParameter(const TopoDS_Edge& E)
772 {
773   return (*((Handle(BRep_TEdge)*)&E.TShape()))->SameParameter();
774 }
775
776 //=======================================================================
777 //function : SameRange
778 //purpose  : Returns the SameRange flag for the edge.
779 //=======================================================================
780
781 Standard_Boolean  BRep_Tool::SameRange(const TopoDS_Edge& E)
782 {
783   return (*((Handle(BRep_TEdge)*)&E.TShape()))->SameRange();
784 }
785
786 //=======================================================================
787 //function : Degenerated
788 //purpose  : Returns True  if the edge is degenerated.
789 //=======================================================================
790
791 Standard_Boolean  BRep_Tool::Degenerated(const TopoDS_Edge& E)
792 {
793   return (*((Handle(BRep_TEdge)*)&E.TShape()))->Degenerated();
794 }
795
796 //=======================================================================
797 //function : Range
798 //purpose  : 
799 //=======================================================================
800
801 void  BRep_Tool::Range(const TopoDS_Edge& E, 
802                        Standard_Real& First, 
803                        Standard_Real& Last)
804 {
805   //  set the range to all the representations
806   BRep_ListIteratorOfListOfCurveRepresentation itcr
807     ((*((Handle(BRep_TEdge)*)&E.TShape()))->ChangeCurves());
808
809   while (itcr.More()) {
810     const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
811     if (cr->IsCurve3D()) {
812       const Handle(BRep_Curve3D)& CR = *((Handle(BRep_Curve3D)*)&cr);
813       if (!CR->Curve3D().IsNull()) {
814         First = CR->First(); 
815         Last = CR->Last();
816         break;
817       }
818     }
819     else if (cr->IsCurveOnSurface()) {
820       const Handle(BRep_GCurve)& CR = *((Handle(BRep_GCurve)*)&cr);
821       First = CR->First(); 
822       Last = CR->Last();
823       break;
824     }
825     itcr.Next();
826   }
827 }
828
829 //=======================================================================
830 //function : Range
831 //purpose  : 
832 //=======================================================================
833
834 void  BRep_Tool::Range(const TopoDS_Edge& E, 
835                        const Handle(Geom_Surface)& S,
836                        const TopLoc_Location& L,
837                        Standard_Real& First, 
838                        Standard_Real& Last)
839 {
840   TopLoc_Location l = L.Predivided(E.Location());
841   
842   // find the representation
843   BRep_ListIteratorOfListOfCurveRepresentation itcr
844     ((*((Handle(BRep_TEdge)*)&E.TShape()))->ChangeCurves());
845   
846   while (itcr.More()) {
847     const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
848     if (cr->IsCurveOnSurface(S,l)) {
849       (*((Handle(BRep_GCurve)*)&cr))->Range(First,Last);
850       break;
851     }
852     itcr.Next();
853   }
854   if (!itcr.More()) {
855     Range(E,First,Last);
856   }
857   (*((Handle(BRep_TEdge)*)&E.TShape()))->Modified(Standard_True);
858  }
859
860 //=======================================================================
861 //function : Range
862 //purpose  : 
863 //=======================================================================
864
865 void  BRep_Tool::Range(const TopoDS_Edge& E, 
866                        const TopoDS_Face& F, 
867                        Standard_Real& First, 
868                        Standard_Real& Last)
869 {
870   TopLoc_Location L;
871   const Handle(Geom_Surface)& S = BRep_Tool::Surface(F,L);
872   Range(E,S,L,First,Last);
873 }
874
875 //=======================================================================
876 //function : UVPoints
877 //purpose  : 
878 //=======================================================================
879
880 void  BRep_Tool::UVPoints(const TopoDS_Edge& E, 
881                           const Handle(Geom_Surface)& S, 
882                           const TopLoc_Location& L, 
883                           gp_Pnt2d& PFirst, 
884                           gp_Pnt2d& PLast)
885 {
886   TopLoc_Location l = L.Predivided(E.Location());
887   Standard_Boolean Eisreversed = (E.Orientation() == TopAbs_REVERSED);
888
889   // find the representation
890   BRep_ListIteratorOfListOfCurveRepresentation itcr
891     ((*((Handle(BRep_TEdge)*)&E.TShape()))->ChangeCurves());
892
893   while (itcr.More()) {
894     const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
895     if (cr->IsCurveOnSurface(S,l)) {
896       if (cr->IsCurveOnClosedSurface() && Eisreversed)
897         (*((Handle(BRep_CurveOnClosedSurface)*)&cr))->UVPoints2(PFirst,PLast);
898       else
899         (*((Handle(BRep_CurveOnSurface)*)&cr))->UVPoints(PFirst,PLast);
900       return;
901     }
902     itcr.Next();
903   }
904
905   // for planar surface project the vertices
906   // modif 21-05-97 : for RectangularTrimmedSurface, project the vertices
907   Handle(Geom_Plane) GP;
908   Handle(Geom_RectangularTrimmedSurface) GRTS;
909   GRTS = Handle(Geom_RectangularTrimmedSurface)::DownCast(S);
910   if(!GRTS.IsNull())
911     GP = Handle(Geom_Plane)::DownCast(GRTS->BasisSurface());
912   else
913     GP = Handle(Geom_Plane)::DownCast(S);
914   //fin modif du 21-05-97
915   if (!GP.IsNull()) {
916     // get the two vertices
917     TopoDS_Vertex Vf,Vl;
918     TopExp::Vertices(E,Vf,Vl);
919
920     TopLoc_Location Linverted = L.Inverted();
921     Vf.Move(Linverted);
922     Vl.Move(Linverted);
923     Standard_Real u,v;
924     gp_Pln pln = GP->Pln();
925
926     u=v=0.;
927     if (!Vf.IsNull()) {
928       gp_Pnt PF = BRep_Tool::Pnt(Vf);
929       ElSLib::Parameters(pln,PF,u,v);
930     }
931     PFirst.SetCoord(u,v);
932
933     u=v=0.;
934     if (!Vl.IsNull()) {
935       gp_Pnt PL = BRep_Tool::Pnt(Vl);
936       ElSLib::Parameters(pln,PL,u,v);
937     }
938     PLast.SetCoord(u,v);
939   }    
940 }
941
942 //=======================================================================
943 //function : UVPoints
944 //purpose  : 
945 //=======================================================================
946
947 void  BRep_Tool::UVPoints(const TopoDS_Edge& E,
948                           const TopoDS_Face& F, 
949                           gp_Pnt2d& PFirst, 
950                           gp_Pnt2d& PLast)
951 {
952   TopLoc_Location L;
953   const Handle(Geom_Surface)& S = BRep_Tool::Surface(F,L);
954   TopoDS_Edge aLocalEdge = E;
955   if (F.Orientation() == TopAbs_REVERSED) {
956     aLocalEdge.Reverse();
957 //    UVPoints(E,S,L,PFirst,PLast);
958   }
959 //    UVPoints(TopoDS::Edge(E.Reversed()),S,L,PFirst,PLast);
960 //  else
961 //    UVPoints(E,S,L,PFirst,PLast);
962   UVPoints(aLocalEdge,S,L,PFirst,PLast);
963 }
964
965 //=======================================================================
966 //function : SetUVPoints
967 //purpose  : 
968 //=======================================================================
969
970 void  BRep_Tool::SetUVPoints(const TopoDS_Edge& E,
971                              const Handle(Geom_Surface)& S,
972                              const TopLoc_Location& L, 
973                              const gp_Pnt2d& PFirst, 
974                              const gp_Pnt2d& PLast)
975 {
976   TopLoc_Location l = L.Predivided(E.Location());
977   Standard_Boolean Eisreversed = (E.Orientation() == TopAbs_REVERSED);
978
979   // find the representation
980   BRep_ListIteratorOfListOfCurveRepresentation itcr
981     ((*((Handle(BRep_TEdge)*)&E.TShape()))->ChangeCurves());
982
983   while (itcr.More()) {
984     const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
985     if (cr->IsCurveOnSurface(S,l)) {
986       if (cr->IsCurveOnClosedSurface() && Eisreversed)
987         (*((Handle(BRep_CurveOnClosedSurface)*) &cr))->
988           SetUVPoints2(PFirst,PLast);
989       else
990         (*((Handle(BRep_CurveOnSurface)*) &cr))->
991           SetUVPoints(PFirst,PLast);
992     }
993     itcr.Next();
994   }
995 }
996
997 //=======================================================================
998 //function : SetUVPoints
999 //purpose  : 
1000 //=======================================================================
1001
1002 void  BRep_Tool::SetUVPoints(const TopoDS_Edge& E,
1003                              const TopoDS_Face& F, 
1004                              const gp_Pnt2d& PFirst, 
1005                              const gp_Pnt2d& PLast)
1006 {
1007   TopLoc_Location L;
1008   const Handle(Geom_Surface)& S = BRep_Tool::Surface(F,L);
1009   TopoDS_Edge aLocalEdge = E;
1010   if (F.Orientation() == TopAbs_REVERSED) {
1011     aLocalEdge.Reverse();
1012 //    SetUVPoints(TopoDS::Edge(E.Reversed()),S,L,PFirst,PLast);
1013   }
1014 //  else
1015 //    SetUVPoints(E,S,L,PFirst,PLast);
1016   SetUVPoints(aLocalEdge,S,L,PFirst,PLast);
1017 }
1018
1019 //=======================================================================
1020 //function : HasContinuity
1021 //purpose  : Returns True if the edge is on the surfaces of the
1022 //           two faces.
1023 //=======================================================================
1024
1025 Standard_Boolean BRep_Tool::HasContinuity(const TopoDS_Edge& E, 
1026                                           const TopoDS_Face& F1, 
1027                                           const TopoDS_Face& F2)
1028 {
1029   TopLoc_Location l1,l2;
1030   const Handle(Geom_Surface)& S1 = BRep_Tool::Surface(F1,l1);
1031   const Handle(Geom_Surface)& S2 = BRep_Tool::Surface(F2,l2);
1032   return HasContinuity(E,S1,S2,l1,l2);
1033 }
1034
1035 //=======================================================================
1036 //function : Continuity
1037 //purpose  : Returns the continuity.
1038 //=======================================================================
1039
1040 GeomAbs_Shape  BRep_Tool::Continuity(const TopoDS_Edge& E, 
1041                                      const TopoDS_Face& F1, 
1042                                      const TopoDS_Face& F2)
1043 {
1044   TopLoc_Location l1,l2;
1045   const Handle(Geom_Surface)& S1 = BRep_Tool::Surface(F1,l1);
1046   const Handle(Geom_Surface)& S2 = BRep_Tool::Surface(F2,l2);
1047   return Continuity(E,S1,S2,l1,l2);
1048 }
1049
1050 //=======================================================================
1051 //function : HasContinuity
1052 //purpose  : Returns True if the edge is on the surfaces.
1053 //=======================================================================
1054
1055 Standard_Boolean BRep_Tool::HasContinuity(const TopoDS_Edge& E, 
1056                                           const Handle(Geom_Surface)& S1, 
1057                                           const Handle(Geom_Surface)& S2, 
1058                                           const TopLoc_Location& L1, 
1059                                           const TopLoc_Location& L2)
1060 {
1061   const TopLoc_Location& Eloc = E.Location();
1062   TopLoc_Location l1 = L1.Predivided(Eloc);
1063   TopLoc_Location l2 = L2.Predivided(Eloc);
1064
1065   // find the representation
1066   BRep_ListIteratorOfListOfCurveRepresentation itcr
1067     ((*((Handle(BRep_TEdge)*)&E.TShape()))->ChangeCurves());
1068
1069   while (itcr.More()) {
1070     const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
1071     if (cr->IsRegularity(S1,S2,l1,l2))
1072       return Standard_True;
1073     itcr.Next();
1074   }
1075   return Standard_False;
1076 }
1077
1078 //=======================================================================
1079 //function : Continuity
1080 //purpose  : Returns the continuity.
1081 //=======================================================================
1082
1083 GeomAbs_Shape  BRep_Tool::Continuity(const TopoDS_Edge& E, 
1084                                      const Handle(Geom_Surface)& S1, 
1085                                      const Handle(Geom_Surface)& S2, 
1086                                      const TopLoc_Location& L1, 
1087                                      const TopLoc_Location& L2)
1088 {
1089   TopLoc_Location l1 = L1.Predivided(E.Location());
1090   TopLoc_Location l2 = L2.Predivided(E.Location());
1091
1092   // find the representation
1093   BRep_ListIteratorOfListOfCurveRepresentation itcr
1094     ((*((Handle(BRep_TEdge)*)&E.TShape()))->ChangeCurves());
1095
1096   while (itcr.More()) {
1097     const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
1098     if (cr->IsRegularity(S1,S2,l1,l2))
1099       return cr->Continuity();
1100     itcr.Next();
1101   }
1102   return GeomAbs_C0;
1103 }
1104
1105 //=======================================================================
1106 //function : Pnt
1107 //purpose  : Returns the 3d point.
1108 //=======================================================================
1109
1110 gp_Pnt  BRep_Tool::Pnt(const TopoDS_Vertex& V)
1111 {
1112   Handle(BRep_TVertex)& TV = *((Handle(BRep_TVertex)*) &V.TShape());
1113   gp_Pnt P = TV->Pnt();
1114   P.Transform(V.Location().Transformation());
1115   return P;
1116 }
1117
1118 //=======================================================================
1119 //function : Tolerance
1120 //purpose  : Returns the tolerance.
1121 //=======================================================================
1122
1123 Standard_Real  BRep_Tool::Tolerance(const TopoDS_Vertex& V)
1124 {
1125   Standard_Real p = (*((Handle(BRep_TVertex)*)&V.TShape()))->Tolerance();
1126   Standard_Real pMin = Precision::Confusion();
1127   if (p > pMin) return p;
1128   else          return pMin;
1129 }
1130
1131 //=======================================================================
1132 //function : Parameter
1133 //purpose  : Returns the parameter of <V> on <E>.
1134 //=======================================================================
1135
1136 Standard_Real  BRep_Tool::Parameter(const TopoDS_Vertex& V, 
1137                                   const TopoDS_Edge& E)
1138 {
1139   
1140   // Search the vertex in the edge
1141
1142   Standard_Boolean rev = Standard_False;
1143   TopoDS_Shape VF;
1144   TopAbs_Orientation orient = TopAbs_INTERNAL;
1145
1146   TopoDS_Iterator itv(E.Oriented(TopAbs_FORWARD));
1147
1148   // if the edge has no vertices
1149   // and is degenerated use the vertex orientation
1150   // RLE, june 94
1151
1152   if (!itv.More() && Degenerated(E)) {
1153     orient = V.Orientation();
1154   }
1155
1156   while (itv.More()) {
1157     const TopoDS_Shape& Vcur = itv.Value();
1158     if (V.IsSame(Vcur)) {
1159       if (VF.IsNull()) {
1160         VF = Vcur;
1161       }
1162       else {
1163         rev = E.Orientation() == TopAbs_REVERSED;
1164         if (Vcur.Orientation() == V.Orientation()) {
1165           VF = Vcur;
1166         }
1167       }
1168     }
1169     itv.Next();
1170   }
1171   
1172   if (!VF.IsNull()) orient = VF.Orientation();
1173  
1174   Standard_Real f,l;
1175
1176   if (orient ==  TopAbs_FORWARD) {
1177     BRep_Tool::Range(E,f,l);
1178     return (rev) ? l : f;
1179   }
1180  
1181   else if (orient ==  TopAbs_REVERSED) {
1182     BRep_Tool::Range(E,f,l);
1183     return (rev) ? f : l;
1184    }
1185
1186   else {
1187     TopLoc_Location L;
1188     const Handle(Geom_Curve)& C = BRep_Tool::Curve(E,L,f,l);
1189     L = L.Predivided(V.Location());
1190     if (!C.IsNull() || Degenerated(E)) {
1191       BRep_ListIteratorOfListOfPointRepresentation itpr
1192         ((*((Handle(BRep_TVertex)*) &V.TShape()))->Points());
1193
1194       while (itpr.More()) {
1195         const Handle(BRep_PointRepresentation)& pr = itpr.Value();
1196         if (pr->IsPointOnCurve(C,L)) {
1197           Standard_Real p = pr->Parameter();
1198           Standard_Real res = p;// SVV 4 nov 99 - to avoid warnings on Linux
1199           if (!C.IsNull()) {
1200             // Closed curves RLE 16 june 94
1201             if (Precision::IsNegativeInfinite(f)) return pr->Parameter();//p;
1202             if (Precision::IsPositiveInfinite(l)) return pr->Parameter();//p;
1203             gp_Pnt Pf = C->Value(f).Transformed(L.Transformation());
1204             gp_Pnt Pl = C->Value(l).Transformed(L.Transformation());
1205             Standard_Real tol = BRep_Tool::Tolerance(V);
1206             if (Pf.Distance(Pl) < tol) {
1207               if (Pf.Distance(BRep_Tool::Pnt(V)) < tol) {
1208                 if (V.Orientation() == TopAbs_FORWARD) res = f;//p = f;
1209                 else                                   res = l;//p = l;
1210               }
1211             }
1212           }
1213           return res;//p;
1214         }
1215         itpr.Next();
1216       }
1217     }
1218     else {
1219       // no 3d curve !!
1220       // let us try with the first pcurve
1221       Handle(Geom2d_Curve) PC;
1222       Handle(Geom_Surface) S;
1223       BRep_Tool::CurveOnSurface(E,PC,S,L,f,l);
1224       L = L.Predivided(V.Location()); 
1225       BRep_ListIteratorOfListOfPointRepresentation itpr
1226         ((*((Handle(BRep_TVertex)*) &V.TShape()))->Points());
1227
1228       while (itpr.More()) {
1229         const Handle(BRep_PointRepresentation)& pr = itpr.Value();
1230         if (pr->IsPointOnCurveOnSurface(PC,S,L)) {
1231           Standard_Real p = pr->Parameter();
1232           // Closed curves RLE 16 june 94
1233           if (PC->IsClosed()) {
1234             if ((p == PC->FirstParameter()) || 
1235                 (p == PC->LastParameter())) {
1236               if (V.Orientation() == TopAbs_FORWARD) p = PC->FirstParameter();
1237               else                                   p = PC->LastParameter();
1238             }
1239           }
1240           return p;
1241         }
1242         itpr.Next();
1243       }
1244     }
1245   }
1246   
1247   Standard_NoSuchObject::Raise("BRep_Tool:: no parameter on edge");
1248   return 0;
1249 }
1250
1251 //=======================================================================
1252 //function : Parameter
1253 //purpose  : Returns the  parameters  of   the  vertex   on the
1254 //           pcurve of the edge on the face.
1255 //=======================================================================
1256
1257 Standard_Real BRep_Tool::Parameter(const TopoDS_Vertex& V, 
1258                                    const TopoDS_Edge& E, 
1259                                    const TopoDS_Face& F)
1260 {
1261   TopLoc_Location L;
1262   const Handle(Geom_Surface)& S = BRep_Tool::Surface(F,L);
1263   return BRep_Tool::Parameter(V,E,S,L);
1264 }
1265
1266 //=======================================================================
1267 //function : Parameter
1268 //purpose  : Returns the  parameters  of   the  vertex   on the
1269 //           pcurve of the edge on the surface.
1270 //=======================================================================
1271
1272 Standard_Real BRep_Tool::Parameter(const TopoDS_Vertex& V, 
1273                                    const TopoDS_Edge& E, 
1274                                    const Handle(Geom_Surface)& S,
1275                                    const TopLoc_Location& L)
1276 {
1277   // Search the vertex in the edge
1278
1279   Standard_Boolean rev = Standard_False;
1280   TopoDS_Shape VF;
1281   TopoDS_Iterator itv(E.Oriented(TopAbs_FORWARD));
1282
1283   while (itv.More()) {
1284     if (V.IsSame(itv.Value())) {
1285       if (VF.IsNull()) VF = itv.Value();
1286       else {
1287         rev = E.Orientation() == TopAbs_REVERSED;
1288         if (itv.Value().Orientation() == V.Orientation()) 
1289         VF = itv.Value();
1290       }
1291     }
1292     itv.Next();
1293   }
1294
1295  TopAbs_Orientation orient = TopAbs_INTERNAL;
1296   if (!VF.IsNull()) orient = VF.Orientation();
1297  
1298  Standard_Real f,l;
1299
1300  if (orient ==  TopAbs_FORWARD) {
1301    BRep_Tool::Range(E,S,L,f,l);
1302    return (rev) ? l : f;
1303  }
1304  
1305  else if (orient ==  TopAbs_REVERSED) {
1306    BRep_Tool::Range(E,S,L,f,l);
1307    return (rev) ? f : l;
1308  }
1309
1310  else {
1311    Handle(Geom2d_Curve) PC = BRep_Tool::CurveOnSurface(E,S,L,f,l);
1312    BRep_ListIteratorOfListOfPointRepresentation itpr
1313      ((*((Handle(BRep_TVertex)*) &V.TShape()))->Points());
1314
1315    while (itpr.More()) {
1316      if (itpr.Value()->IsPointOnCurveOnSurface(PC,S,L))
1317        return itpr.Value()->Parameter();
1318      itpr.Next();
1319    }
1320  }
1321
1322  //----------------------------------------------------------
1323    
1324   TopLoc_Location L1;
1325   const Handle(Geom_Curve)& C = BRep_Tool::Curve(E,L1,f,l);
1326   L1 = L1.Predivided(V.Location());
1327   if (!C.IsNull() || Degenerated(E)) {
1328     BRep_ListIteratorOfListOfPointRepresentation itpr
1329       ((*((Handle(BRep_TVertex)*) &V.TShape()))->Points());
1330
1331     while (itpr.More()) {
1332       const Handle(BRep_PointRepresentation)& pr = itpr.Value();
1333       if (pr->IsPointOnCurve(C,L1)) {
1334         Standard_Real p = pr->Parameter();
1335         Standard_Real res = p;
1336         if (!C.IsNull()) {
1337           // Closed curves RLE 16 june 94
1338           if (Precision::IsNegativeInfinite(f)) return res;
1339           if (Precision::IsPositiveInfinite(l)) return res;
1340           gp_Pnt Pf = C->Value(f).Transformed(L1.Transformation());
1341           gp_Pnt Pl = C->Value(l).Transformed(L1.Transformation());
1342           Standard_Real tol = BRep_Tool::Tolerance(V);
1343           if (Pf.Distance(Pl) < tol) {
1344             if (Pf.Distance(BRep_Tool::Pnt(V)) < tol) {
1345               if (V.Orientation() == TopAbs_FORWARD) res = f;
1346               else                                   res = l;
1347             }
1348           }
1349         }
1350         return res;
1351       }
1352       itpr.Next();
1353     }
1354   }
1355   
1356 //----------------------------------------------------------   
1357  
1358   Standard_NoSuchObject::Raise("BRep_Tool:: no parameter on edge");
1359   return 0;
1360 }
1361
1362 //=======================================================================
1363 //function : Parameters
1364 //purpose  : Returns the parameters of the vertex on the face.
1365 //=======================================================================
1366
1367 gp_Pnt2d  BRep_Tool::Parameters(const TopoDS_Vertex& V, 
1368                                 const TopoDS_Face& F)
1369 {
1370   TopLoc_Location L;
1371   const Handle(Geom_Surface)& S = BRep_Tool::Surface(F,L);
1372   L = L.Predivided(V.Location());
1373   BRep_ListIteratorOfListOfPointRepresentation itpr
1374     ((*((Handle(BRep_TVertex)*) &V.TShape()))->Points());
1375   // It is checked if there is PointRepresentation (case non Manifold)
1376
1377   while (itpr.More()) {
1378     if (itpr.Value()->IsPointOnSurface(S,L)) {
1379       return gp_Pnt2d(itpr.Value()->Parameter(),
1380                       itpr.Value()->Parameter2());
1381     }
1382     itpr.Next();
1383   }
1384
1385  TopoDS_Vertex Vf,Vl;
1386  TopoDS_Edge E;
1387  // Otherwise the edges are searched (PMN 4/06/97) It is not possible to succeed 999/1000!
1388  // even if often there is a way to make more economically than above...
1389  TopExp_Explorer exp;
1390  for (exp.Init(F, TopAbs_EDGE); exp.More(); exp.Next()) { 
1391     E = TopoDS::Edge(exp.Current());  
1392     TopExp::Vertices(E, Vf, Vl);
1393     if ((V.IsSame(Vf)) || (V.IsSame(Vl))) {
1394       gp_Pnt2d Pf, Pl;
1395       UVPoints(E, F, Pf, Pl);
1396       if (V.IsSame(Vf)) return Pf;
1397       else              return Pl;//Ambiguity (natural) for degenerated edges.
1398     }
1399   }
1400   Standard_NoSuchObject::Raise("BRep_Tool:: no parameters on surface");
1401   return gp_Pnt2d(0,0);
1402 }
1403 //=======================================================================
1404 //function : IsClosed
1405 //purpose  : Returns <True>  if S if flaged Closed, if S is a
1406 //           Solid,Shell or Compound  returns <True> is S has no free boundaries.
1407 //=======================================================================
1408 Standard_Boolean BRep_Tool::IsClosed(const TopoDS_Shape& S)
1409 {
1410   if (S.ShapeType() == TopAbs_SHELL || S.ShapeType() == TopAbs_SOLID ||
1411       S.ShapeType() == TopAbs_COMPOUND) {
1412     TopTools_MapOfShape M;
1413     TopExp_Explorer exp;
1414     for (exp.Init(S,TopAbs_EDGE); exp.More(); exp.Next()) {
1415 //    for (TopExp_Explorer exp(S,TopAbs_EDGE); exp.More(); exp.Next()) {
1416       const TopoDS_Edge& E = TopoDS::Edge(exp.Current());
1417       if (BRep_Tool::Degenerated(E)) continue;
1418       if (!M.Add(E)) M.Remove(E);
1419     }
1420     if ( M.IsEmpty()) return 1;
1421   }
1422   return (S.Closed());
1423 }
1424
1425 //modified by NIZNHY-PKV Fri Oct 17 14:09:58 2008 f 
1426 //=======================================================================
1427 //function : IsPlane
1428 //purpose  : 
1429 //=======================================================================
1430 Standard_Boolean IsPlane(const Handle(Geom_Surface)& aS)
1431 {
1432   Standard_Boolean bRet;
1433   Handle(Geom_Plane) aGP;
1434   Handle(Geom_RectangularTrimmedSurface) aGRTS;
1435   Handle(Geom_OffsetSurface) aGOFS;
1436   //
1437   aGRTS=Handle(Geom_RectangularTrimmedSurface)::DownCast(aS);
1438   aGOFS=Handle(Geom_OffsetSurface)::DownCast(aS);
1439   //
1440   if(!aGOFS.IsNull()) {
1441     aGP=Handle(Geom_Plane)::DownCast(aGOFS->BasisSurface());
1442   }
1443   else if(!aGRTS.IsNull()) {
1444     aGP=Handle(Geom_Plane)::DownCast(aGRTS->BasisSurface());
1445   }
1446   else {
1447     aGP=Handle(Geom_Plane)::DownCast(aS);
1448   }
1449   //
1450   bRet=!aGP.IsNull();
1451   //
1452   return bRet;
1453 }