0024624: Lost word in license statement in source files
[occt.git] / src / BRepFill / BRepFill_Sweep.cxx
1 // Created on: 1998-01-07
2 // Created by: Philippe MANGIN
3 // Copyright (c) 1998-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 #include <stdio.h>
18
19 #include <BRepFill_Sweep.ixx>
20
21 #include <BRepFill_SectionLaw.hxx>
22 //#include <BRepFill_TrimCorner.hxx>
23 #include <BRepFill_CurveConstraint.hxx>
24
25 #include <GeomFill_SectionLaw.hxx>
26 #include <GeomFill_LocationLaw.hxx>
27 #include <GeomFill_Sweep.hxx>
28
29 // modified by NIZHNY-MKK  Wed Oct 22 12:25:45 2003
30 #include <BRepFill_TrimShellCorner.hxx>
31
32 //#include <GeomPlate_BuildPlateSurface.hxx>
33 //#include <GeomPlate_Surface.hxx>
34 //#include <GeomPlate_PointConstraint.hxx>
35
36 #include <gp_Pnt2d.hxx>
37 #include <gp_Vec2d.hxx>
38 #include <Bnd_Box.hxx>
39
40 #include <Geom_Surface.hxx>
41 #include <Geom_SurfaceOfRevolution.hxx>
42 #include <Geom_RectangularTrimmedSurface.hxx>
43 #include <Geom_Plane.hxx>
44 #include <Geom_Curve.hxx>
45 #include <Geom_BezierCurve.hxx>
46 #include <Geom_BSplineCurve.hxx>
47 #include <Geom2d_Line.hxx>
48 #include <Geom2d_Curve.hxx>
49 #include <Geom2d_TrimmedCurve.hxx>
50 #include <GeomLib.hxx>
51 #include <GeomLib_IsPlanarSurface.hxx>
52 #include <BRepLib_FindSurface.hxx>
53 #include <GeomConvert_ApproxSurface.hxx>
54
55 #include <BRepAdaptor_HCurve.hxx>
56 #include <BRepAdaptor_HCurve2d.hxx>
57 #include <BRepAdaptor_HSurface.hxx>
58 #include <Adaptor3d_HCurveOnSurface.hxx>
59 #include <GeomAdaptor_HSurface.hxx>
60 #include <GeomAdaptor_HCurve.hxx>
61 #include <Geom2dAdaptor_HCurve.hxx>
62 #include <Approx_CurveOnSurface.hxx>
63 #include <Approx_SameParameter.hxx>
64 #include <GCPnts_AbscissaPoint.hxx>
65
66 #include <BRep_Builder.hxx>
67 #include <BRep_Tool.hxx>
68 #include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
69 #include <BRep_TEdge.hxx>
70 #include <BRep_TVertex.hxx>
71 #include <BRep_CurveRepresentation.hxx>
72 #include <BRep_GCurve.hxx>
73
74 #include <BRepLib.hxx>
75 #include <BRepLib_MakeEdge.hxx>
76 #include <BRepLib_MakeFace.hxx>
77 #include <BRepLib_FaceError.hxx>
78
79 #include <TopoDS.hxx>
80 #include <TopoDS_Edge.hxx>
81 #include <TopoDS_Face.hxx>
82 #include <TopoDS_Compound.hxx>
83 #include <TopoDS_Shell.hxx>
84 #include <TopExp.hxx>
85 #include <TopExp_Explorer.hxx>
86 #include <TopAbs_Orientation.hxx>
87
88 #include <TColStd_HArray1OfInteger.hxx>
89 #include <TColStd_Array2OfInteger.hxx>
90 #include <TColStd_Array1OfReal.hxx>
91 #include <TColStd_Array2OfReal.hxx>
92 #include <TColGeom_Array2OfSurface.hxx>
93 #include <TColgp_Array1OfPnt.hxx>
94 #include <TColStd_Array1OfBoolean.hxx>
95
96 #include <TopTools_Array1OfShape.hxx>
97 #include <TopTools_Array2OfShape.hxx>
98 #include <TopTools_HArray2OfShape.hxx>
99 #include <TopTools_HArray1OfShape.hxx>
100 #include <TopTools_ListIteratorOfListOfShape.hxx> 
101 #include <TopTools_ListOfShape.hxx> 
102 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
103 #include <TopTools_SequenceOfShape.hxx> 
104 #include <BRepTools_WireExplorer.hxx>
105
106 #include <Standard_ConstructionError.hxx>
107 #include <Precision.hxx>
108 #include <BRepBuilderAPI_MakeWire.hxx>
109
110 #include <BRepTools_Substitution.hxx>
111 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
112 #include <TopoDS_Iterator.hxx>
113
114 //OCC500(apo)
115 #include <BRepCheck_Edge.hxx>  
116
117 #ifdef DRAW
118 #include <Draw.hxx>
119 #include <DrawTrSurf.hxx>
120 #include <DBRep.hxx>
121 static Standard_Boolean Affich = 0;
122 #endif
123
124 //=======================================================================
125 //function : NumberOfPoles
126 //purpose  : 
127 //=======================================================================
128 static Standard_Integer NumberOfPoles(const TopoDS_Wire& W)
129 {
130   Standard_Integer NbPoints = 0;
131
132   TopoDS_Iterator iter(W);
133   for (; iter.More(); iter.Next()) 
134   {
135     BRepAdaptor_Curve c(TopoDS::Edge(iter.Value()));
136
137     Standard_Real dfUf = c.FirstParameter();
138     Standard_Real dfUl = c.LastParameter();
139     if (IsEqual(dfUf,dfUl))
140       // Degenerate
141       continue;
142
143     switch (c.GetType()) 
144     {
145     case GeomAbs_BezierCurve:
146       {
147         // Put all poles for bezier
148         Handle(Geom_BezierCurve) GC = c.Bezier();
149         Standard_Integer iNbPol = GC->NbPoles();
150         if ( iNbPol >= 2)
151           NbPoints += iNbPol;
152         break;
153       }
154     case GeomAbs_BSplineCurve:
155       {
156         // Put all poles for bspline
157         Handle(Geom_BSplineCurve) GC = c.BSpline();
158         Standard_Integer iNbPol = GC->NbPoles();
159         if ( iNbPol >= 2)
160           NbPoints += iNbPol;
161         break;
162       }
163     case GeomAbs_Line:
164       {
165         NbPoints += 2;
166         break;
167       }
168     case GeomAbs_Circle:
169     case GeomAbs_Ellipse:
170     case GeomAbs_Hyperbola:
171     case GeomAbs_Parabola:
172       {
173         NbPoints += 4;
174         break;
175       }
176     default:
177       NbPoints += 15 + c.NbIntervals(GeomAbs_C3);
178     } // switch (c.GetType()) ...
179   } // for (; iter.More(); iter.Next())
180
181   return NbPoints;
182 }
183
184 //=======================================================================
185 //function : HasPCurves
186 //purpose  : 
187 //=======================================================================
188 static Standard_Boolean HasPCurves(const TopoDS_Edge& E)
189 {
190   Standard_Boolean haspcurves = Standard_False;
191
192   BRep_ListIteratorOfListOfCurveRepresentation itcr
193     ((*((Handle(BRep_TEdge)*)&E.TShape()))->Curves());
194   for (; itcr.More(); itcr.Next())
195     {
196       const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
197       if (cr->IsCurveOnSurface())
198         {
199           haspcurves = Standard_True;
200           break;
201         }
202     }
203   return haspcurves;
204 }
205
206 //=======================================================================
207 //function : Translate
208 //purpose  : Copy a column from one table to another. 
209 //=======================================================================
210 static void Translate(const Handle(TopTools_HArray2OfShape)& ArrayIn,
211                       const Standard_Integer In,
212                       Handle(TopTools_HArray2OfShape)& ArrayOut,
213                       const Standard_Integer Out)
214 {
215   Standard_Integer ii, Nb;
216   Nb =  ArrayOut->ColLength();
217   for (ii=1; ii<=Nb; ii++) {
218      ArrayOut->SetValue(ii, Out,  ArrayIn->Value(ii, In));
219   } 
220 }
221
222
223 //=======================================================================
224 //function : Box
225 //purpose  : Bounding box of a section.
226 //=======================================================================
227 static void Box(Handle(GeomFill_SectionLaw)& Sec,
228                 const Standard_Real U,
229                 Bnd_Box& Box)
230
231 {
232   Standard_Integer NbPoles, bid;
233   Box.SetVoid();
234   Sec->SectionShape(NbPoles, bid, bid);
235   TColgp_Array1OfPnt Poles(1, NbPoles);
236   TColStd_Array1OfReal W(1, NbPoles);
237   Sec->D0(U, Poles, W);
238   for (Standard_Integer ii=1; ii<=NbPoles; ii++) {
239       Box.Add(Poles(ii));
240     }  
241 }
242
243 //=======================================================================
244 //function : Couture
245 //purpose  : Check if E is an edge of sewing on S
246 //           and make the representation HadHoc
247 //=======================================================================
248 static Handle(Geom2d_Curve) Couture(const TopoDS_Edge& E, 
249                                     const Handle(Geom_Surface)& S,
250                                     const TopLoc_Location& L)
251 {
252   TopLoc_Location l = L.Predivided(E.Location());
253   Standard_Boolean Eisreversed = (E.Orientation() == TopAbs_REVERSED);
254
255   // find the representation
256   BRep_ListIteratorOfListOfCurveRepresentation itcr
257     ((*((Handle(BRep_TEdge)*)&E.TShape()))->ChangeCurves());
258
259   while (itcr.More()) {
260     Handle(BRep_CurveRepresentation)& cr = itcr.Value();
261     if (cr->IsCurveOnSurface(S,l)) {
262       Handle(BRep_GCurve)& GC = *((Handle(BRep_GCurve)*)&cr);
263       if (GC->IsCurveOnClosedSurface() && Eisreversed) 
264         return  GC->PCurve2();
265       else
266         return GC->PCurve();
267     }
268     itcr.Next();
269   }
270   Handle(Geom2d_Curve) pc;
271   pc.Nullify();
272   return pc;
273 }
274
275 //=======================================================================
276 //function : CheckSameParameter
277 //purpose  : Check a posteriori that sameparameter has worked correctly
278 //=======================================================================
279
280 static Standard_Boolean CheckSameParameter 
281 (const Handle(Adaptor3d_HCurve)&   C3d,
282  const Handle(Geom2d_Curve)&           Pcurv,
283  const Handle(Adaptor3d_HSurface)& S,
284  const Standard_Real             tol3d,
285  Standard_Real&                  tolreached)
286 {
287   tolreached = 0.;
288   Standard_Real f = C3d->FirstParameter();
289   Standard_Real l = C3d->LastParameter();
290   Standard_Integer nbp = 45;
291   Standard_Real step = 1./(nbp -1);
292   for(Standard_Integer i = 0; i < nbp; i++){
293     Standard_Real t,u,v;
294     t = step * i;
295     t = (1-t) * f + t * l;
296     Pcurv->Value(t).Coord(u,v);
297     gp_Pnt pS = S->Value(u,v);
298     gp_Pnt pC = C3d->Value(t);
299     Standard_Real d2 = pS.SquareDistance(pC);
300     tolreached = Max(tolreached,d2);
301   }
302   tolreached = sqrt(tolreached);
303   if(tolreached > tol3d){
304     tolreached *= 2.;
305     return Standard_False;
306   }
307   tolreached *= 2.;
308   tolreached = Max(tolreached,Precision::Confusion());
309   return Standard_True;
310 }
311
312 //=======================================================================
313 //function : SameParameter
314 //purpose  : Encapsulation of Sameparameter
315 // Boolean informs if the pcurve was computed or not...
316 // The tolerance is always OK.
317 //=======================================================================
318
319 static Standard_Boolean SameParameter(TopoDS_Edge&    E,
320                                       Handle(Geom2d_Curve)&        Pcurv,
321                                       const Handle(Geom_Surface)&  Surf,
322                                       const Standard_Real          tol3d,
323                                       Standard_Real&               tolreached)
324 {
325   //Handle(BRepAdaptor_HCurve) C3d = new (BRepAdaptor_HCurve)(E);
326   Standard_Real f, l;
327   Handle(Geom_Curve) C3d = BRep_Tool::Curve( E, f, l );
328   GeomAdaptor_Curve GAC3d( C3d, f, l );
329   Handle(GeomAdaptor_HCurve) HC3d = new GeomAdaptor_HCurve( GAC3d );
330
331   Handle(GeomAdaptor_HSurface) S = new (GeomAdaptor_HSurface)(Surf);
332   Standard_Real ResTol;
333
334   if(CheckSameParameter( HC3d, Pcurv, S, tol3d, tolreached )) 
335     return Standard_True;
336
337   if (!HasPCurves(E))
338     {
339       Handle(Geom2dAdaptor_HCurve) HC2d = new Geom2dAdaptor_HCurve( Pcurv );
340       Approx_CurveOnSurface AppCurve(HC2d, S, HC2d->FirstParameter(), HC2d->LastParameter(),
341                                      Precision::Confusion(), GeomAbs_C1, 10, 10, Standard_True);
342       if (AppCurve.IsDone() && AppCurve.HasResult())
343         {
344           C3d = AppCurve.Curve3d();
345           tolreached = AppCurve.MaxError3d();
346           BRep_Builder B;
347           B.UpdateEdge( E, C3d, tolreached );
348           return Standard_True;
349         }
350     }
351
352   Approx_SameParameter sp( HC3d, Pcurv, S, tol3d );
353   if(sp.IsDone() && !sp.IsSameParameter()) Pcurv = sp.Curve2d();
354   else if(!sp.IsDone() && !sp.IsSameParameter()){
355 #ifdef DEB
356     cout<<"echec SameParameter"<<endl;
357 #endif  
358     return Standard_False;
359   }
360
361   ResTol = sp.TolReached();
362   if(ResTol > tolreached ){
363 #ifdef DEB
364     cout<<"SameParameter : Tolerance not reached!"<<endl;
365     cout<<"tol visee : "<<tol3d<<" tol obtained : "<<ResTol<<endl;
366 #endif  
367     return Standard_False;
368   }
369   else {
370     tolreached = 1.1*ResTol;
371     if(sp.IsDone() && !sp.IsSameParameter()) Pcurv = sp.Curve2d();
372   }
373   return Standard_True;
374 }
375
376 //=======================================================================
377 //Objet : Orientate an edge of natural restriction 
378 //      : General
379 //=======================================================================
380 static void Oriente(const Handle(Geom_Surface)& S,
381                     TopoDS_Edge& E)
382 {
383   gp_Pnt2d P;
384   gp_Vec2d D, URef(1, 0), VRef(0, 1);
385   Standard_Boolean isuiso, isfirst, isopposite;
386   Standard_Real UFirst, ULast, VFirst, VLast, f, l;
387   S->Bounds(UFirst, ULast, VFirst, VLast);
388   Handle(Geom2d_Curve) C;
389   TopLoc_Location bid;
390
391   C = BRep_Tool::CurveOnSurface(E, S, bid, f, l);
392   C->D1((f+l)/2, P, D);
393
394   isuiso = D.IsParallel(VRef, 0.1);
395   
396   if ( isuiso ) {
397     isfirst = (Abs (P.X()-UFirst) < Precision::Confusion());
398     isopposite = D.IsOpposite(VRef, 0.1);
399     E.Orientation(TopAbs_REVERSED);
400   }
401   else {
402     isfirst = (Abs (P.Y()-VFirst) < Precision::Confusion());
403     isopposite = D.IsOpposite(URef, 0.1);
404     E.Orientation(TopAbs_FORWARD);
405   }
406
407   if (!isfirst)   E.Reverse();
408   if (isopposite) E.Reverse();
409 }
410 //OCC500(apo)->
411 static void UpdateEdgeOnPlane(const TopoDS_Face& F, const TopoDS_Edge& E,
412                               const BRep_Builder& BB)
413 {
414   Standard_Real f, l;
415   Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(E,F,f,l);
416   Handle(Geom_Surface) S = BRep_Tool::Surface(F);
417   TopLoc_Location Loc;
418   Standard_Real Tol = BRep_Tool::Tolerance(E);
419   BB.UpdateEdge(E, C2d, S, Loc, Tol);
420   BRepCheck_Edge Check(E);
421   Tol = Max(Tol,Check.Tolerance());
422   BB.UpdateEdge(E, Tol);
423   TopoDS_Vertex V; 
424   Tol *= 1.01;
425   V = TopExp::FirstVertex(E);
426   if(BRep_Tool::Tolerance(V) < Tol) BB.UpdateVertex(V, Tol);
427   V = TopExp::LastVertex(E);
428   if(BRep_Tool::Tolerance(V) < Tol) BB.UpdateVertex(V, Tol);
429 }
430 //<-OCC500(apo)
431 //=======================================================================
432 //Function : BuildFace
433 //Objet : Construct a Face via a surface and 4 Edges (natural borders)
434 //      : Only one Hypothesis : isos u and v are switched :
435 //        Edge1/3 are iso u (recp v)
436 //        Edge2/4 are iso v (recp u)
437 //=======================================================================
438 static void BuildFace(const Handle(Geom_Surface)& S,
439                       const TopoDS_Edge& E1,
440                       const TopoDS_Edge& E2,
441                       const TopoDS_Edge& E3,
442                       const TopoDS_Edge& E4,
443                       TopTools_DataMapOfShapeShape& EEmap,
444                       const Standard_Boolean ExchUV,
445                       const Standard_Boolean UReverse,
446                       TopoDS_Face& F)
447 {
448   Standard_Real Tol;
449 // Class BRep_Tool without fields and without Constructor :
450 //  BRep_Tool BT;
451   TopoDS_Edge e1, e2, E;
452   TopoDS_Wire WW;
453   BRep_Builder BB;
454   BRepBuilderAPI_MakeWire B;
455   TopoDS_Iterator Iter;
456   //gp_Pnt2d P;
457
458   //Is the surface planar ?
459   Standard_Real Tol1, Tol2, Tol3, Tol4;
460   Tol1 = BRep_Tool::Tolerance( E1 );
461   Tol2 = BRep_Tool::Tolerance( E2 );
462   Tol3 = BRep_Tool::Tolerance( E3 );
463   Tol4 = BRep_Tool::Tolerance( E4 );
464 //  Tol = Min( BT.Tolerance(E1), BT.Tolerance(E2));
465   Tol = Min( Tol1, Tol2 );
466 //  Tol = Min(Tol, Min(BT.Tolerance(E3),BT.Tolerance(E4)));
467   Tol = Min( Tol, Min( Tol3, Tol4 ) );
468   Standard_Boolean IsPlan = Standard_False;
469   Handle(Geom_Plane) thePlane;
470
471   if (!E1.IsSame(E3) && !E2.IsSame(E4)) //exclude cases with seam edges: they are not planar
472     {
473       GeomLib_IsPlanarSurface IsP(S, Tol);
474       if (IsP.IsPlanar())
475         {
476           IsPlan = Standard_True;
477           thePlane = new Geom_Plane( IsP.Plan() );
478         }
479       else
480         {
481           Handle(BRep_TEdge)& TE1 = *((Handle(BRep_TEdge)*)&E1.TShape());
482           Handle(BRep_TEdge)& TE2 = *((Handle(BRep_TEdge)*)&E2.TShape());
483           Handle(BRep_TEdge)& TE3 = *((Handle(BRep_TEdge)*)&E3.TShape());
484           Handle(BRep_TEdge)& TE4 = *((Handle(BRep_TEdge)*)&E4.TShape());
485           TE1->Tolerance( Precision::Confusion() );
486           TE2->Tolerance( Precision::Confusion() );
487           TE3->Tolerance( Precision::Confusion() );
488           TE4->Tolerance( Precision::Confusion() );
489           
490           TopoDS_Wire theWire = BRepLib_MakeWire( E1, E2, E3, E4 );
491           Standard_Integer NbPoints = NumberOfPoles( theWire );
492           if (NbPoints <= 100) //limitation for CPU
493             {
494               BRepLib_FindSurface FS( theWire, -1, Standard_True );
495               if (FS.Found())
496                 {
497                   IsPlan = Standard_True;
498                   thePlane = Handle(Geom_Plane)::DownCast(FS.Surface());
499                 }
500             }
501           BB.UpdateEdge( E1, Tol1 );
502           BB.UpdateEdge( E2, Tol2 );
503           BB.UpdateEdge( E3, Tol3 );
504           BB.UpdateEdge( E4, Tol4 );
505         }
506     }
507
508   // Construction of the wire
509 //  B.MakeWire(WW);
510   e1 = E1;
511   Oriente(S, e1);
512 //  if (!IsPlan || !BT.Degenerated(e1)) 
513   if (!IsPlan || !BRep_Tool::Degenerated(e1)) 
514     B.Add(e1);
515
516   e2 = E2;
517   Oriente(S, e2);  
518 //  if (!IsPlan || !BT.Degenerated(e2)) 
519   if (!IsPlan || !BRep_Tool::Degenerated(e2))
520     {
521       B.Add(e2);
522       if (!BRep_Tool::Degenerated(e2))
523         {
524           WW = B.Wire();
525           TopoDS_Shape NewEdge;
526           //take the last edge added to WW
527           for (Iter.Initialize( WW ); Iter.More(); Iter.Next())
528             NewEdge = Iter.Value();
529           if (! e2.IsSame(NewEdge))
530             EEmap.Bind( e2, NewEdge );
531         }
532     }
533
534   if (E3.IsSame(E1)) {
535     E = e1;
536     E.Reverse();
537   }
538   else {
539     E = E3;
540     Oriente(S, E);
541   }
542 //  if (!IsPlan || !BT.Degenerated(E))   
543   if (!IsPlan || !BRep_Tool::Degenerated(E))
544     {
545       B.Add(E);
546       if (!BRep_Tool::Degenerated(E))
547         {
548           WW = B.Wire();
549           TopoDS_Shape NewEdge;
550           //take the last edge added to WW
551           for (Iter.Initialize( WW ); Iter.More(); Iter.Next())
552             NewEdge = Iter.Value();
553           if (! E.IsSame(NewEdge))
554             EEmap.Bind( E, NewEdge );
555         }
556     }
557
558   if (E4.IsSame(E2)) {
559     E = e2;
560     E.Reverse();
561   }
562   else {
563     E = E4;
564     Oriente(S, E);
565   }
566 //  if (!IsPlan || !BT.Degenerated(E))  
567   if (!IsPlan || !BRep_Tool::Degenerated(E)) 
568     {
569       B.Add(E);
570       if (!BRep_Tool::Degenerated(E))
571         {
572           WW = B.Wire();
573           TopoDS_Shape NewEdge;
574           //take the last edge added to WW
575           for (Iter.Initialize( WW ); Iter.More(); Iter.Next())
576             NewEdge = Iter.Value();
577           if (! E.IsSame(NewEdge))
578             EEmap.Bind( E, NewEdge );
579         }
580     }
581
582   WW = B.Wire();
583 #if DRAW
584   if (Affich)
585     DBRep::Set("wire-on-face", WW);
586 #endif
587   
588 // Construction of the face.
589   if (IsPlan) { // Suspend representation 2d 
590     // and construct face Plane
591
592     //BRepLib_MakeFace MkF(IsP.Plan(), WW);
593     gp_Pnt aPnt;
594     gp_Vec DU, DV, NS, NP;
595     Standard_Real Ufirst, Ulast, Vfirst, Vlast;
596     S->Bounds( Ufirst, Ulast, Vfirst, Vlast );
597     S->D1( (Ufirst+Ulast)/2., (Vfirst+Vlast)/2., aPnt, DU, DV );
598     NS = DU ^ DV;
599     NP = thePlane->Pln().Axis().Direction();
600     if (NS.Dot(NP) < 0.)
601       thePlane->UReverse();
602     BRepLib_MakeFace MkF( thePlane, WW );
603     if (MkF.Error() != BRepLib_FaceDone) {
604 #if DEB
605       BRepLib_FaceError Err = MkF.Error();
606       cout << "Planar Face Error :" <<   Err << endl;
607 #endif
608     }
609     else {
610       Handle(Geom2d_Curve) NullC2d;
611       TopLoc_Location Loc;
612       BB.UpdateEdge( E1, NullC2d, S, Loc, Tol1 );
613       BB.UpdateEdge( E2, NullC2d, S, Loc, Tol2 );
614       BB.UpdateEdge( E3, NullC2d, S, Loc, Tol3 );
615       BB.UpdateEdge( E4, NullC2d, S, Loc, Tol4 );
616
617       F =  MkF.Face();
618       UpdateEdgeOnPlane(F,E1,BB);
619       UpdateEdgeOnPlane(F,E2,BB);
620       UpdateEdgeOnPlane(F,E3,BB);
621       UpdateEdgeOnPlane(F,E4,BB);
622 /*OCC500(apo)->
623       TopLoc_Location Loc;
624       Handle(Geom2d_Curve) NC;
625       NC.Nullify();
626 //      B.UpdateEdge(E1, NC, S, Loc, BT.Tolerance(E1));
627       BB.UpdateEdge(E1, NC, S, Loc, BRep_Tool::Tolerance(E1));
628 //      B.UpdateEdge(E2, NC, S, Loc, BT.Tolerance(E2));
629       BB.UpdateEdge(E2, NC, S, Loc, BRep_Tool::Tolerance(E2));
630 //      B.UpdateEdge(E3, NC, S, Loc, BT.Tolerance(E3));
631       BB.UpdateEdge(E3, NC, S, Loc, BRep_Tool::Tolerance(E3));
632 //      B.UpdateEdge(E4, NC, S, Loc, BT.Tolerance(E4));
633       BB.UpdateEdge(E4, NC, S, Loc, BRep_Tool::Tolerance(E4));
634 <-OCC500(apo)*/
635     }  
636   }
637
638   if (!IsPlan) {// Cas Standard : Ajout
639     BB.MakeFace(F, S, Precision::Confusion());
640     BB.Add(F, WW);
641   }
642
643   // Reorientation
644   if (ExchUV) F.Reverse();
645   if (UReverse) F.Reverse();
646 }
647
648
649 //=======================================================================
650 //Fonction : BuildEdge
651 //Objet : Construct non-closed Edge 
652 //=======================================================================
653 static TopoDS_Edge BuildEdge(Handle(Geom_Curve)& C3d,
654                              Handle(Geom2d_Curve)& C2d,
655                              Handle(Geom_Surface)& S,
656                              const TopoDS_Vertex& VF,
657                              const TopoDS_Vertex& VL,
658                              const Standard_Real f, 
659                              const Standard_Real l,
660                              const Standard_Real Tol3d)
661 {
662   gp_Pnt P1, P2, P;
663   Standard_Real Tol1, Tol2, Tol, d;
664 // Class BRep_Tool without fields and without Constructor :
665 //  BRep_Tool BT;
666   BRep_Builder B;
667   TopoDS_Edge E;
668
669 //  P1  = BT.Pnt(VF);
670   P1  = BRep_Tool::Pnt(VF);
671 //  Tol1 = BT.Tolerance(VF);
672   Tol1 = BRep_Tool::Tolerance(VF);
673 //  P2  = BT.Pnt(VL);
674   P2  = BRep_Tool::Pnt(VL);
675 //  Tol2 = BT.Tolerance(VF);
676   Tol2 = BRep_Tool::Tolerance(VF);
677   Tol = Max(Tol1, Tol2);
678
679   if (VF.IsSame(VL) || 
680       (P1.Distance(P2) < Tol ) ) { 
681     // Degenerated case
682     gp_Pnt2d P2d;
683     C2d->D0(f, P2d);
684     S->D0(P2d.X(), P2d.Y(), P);
685     d = P1.Distance(P);
686     if (d > Tol) Tol = d;
687     C2d->D0(l, P2d);
688     S->D0(P2d.X(), P2d.Y(), P);
689     d = P2.Distance(P);
690     if (d > Tol) Tol = d;
691
692     B.UpdateVertex(VF, Tol);
693     B.UpdateVertex(VL, Tol);
694
695     B.MakeEdge(E);
696     B.UpdateEdge(E,C2d,S,TopLoc_Location(), Tol);
697     B.Add(E,VF);
698     B.Add(E,VL);
699     B.Range(E,f,l);
700     B.Degenerated(E, Standard_True);
701
702     return E;
703   }
704
705   C3d->D0(f, P);
706   d = P1.Distance(P);
707   if (d > Tol1)
708       B.UpdateVertex(VF, d);
709
710 //  P1 = BT.Pnt(VL);
711   P1 = BRep_Tool::Pnt(VL);
712   C3d->D0(l, P);
713   d = P2.Distance(P);
714   if (d > Tol2)
715       B.UpdateVertex(VL, d); 
716
717   BRepLib_MakeEdge MkE (C3d, VF, VL, f, l);
718   if (!MkE.IsDone()) { // Error of construction !!     
719 #ifdef DRAW    
720     char name[100];
721     sprintf(name,"firstvertex_error");
722     DBRep::Set(name, VF);
723     sprintf(name,"lastvertex_error");
724     DBRep::Set(name, VL);
725     sprintf(name,"curve3d_error");
726     char* Temp = name ;
727     DrawTrSurf::Set(Temp, C3d);
728 //    DrawTrSurf::Set(name, C3d);
729     Standard_ConstructionError::Raise("BRepFill_Sweep::BuildEdge");
730 #endif
731   }
732
733   E = MkE.Edge();
734   TopLoc_Location Loc;
735   B.UpdateEdge(E, C2d, S, Loc, Tol3d);
736
737   return E;
738 }
739
740         
741 //=======================================================================
742 //Fonction : Filling
743 //Objet : Construct the faces of filling
744 //=======================================================================
745 static Standard_Boolean Filling(const TopoDS_Shape& EF,
746                                 const TopoDS_Shape& F1,
747                                 const TopoDS_Shape& EL,
748                                 const TopoDS_Shape& F2,
749                                 TopTools_DataMapOfShapeShape& EEmap,
750                                 const Standard_Real Tol,
751                                 const  gp_Ax2& Axe,
752                                 const  gp_Vec& TangentOnPart1,
753                                 TopoDS_Edge& Aux1,
754                                 TopoDS_Edge& Aux2,
755                                 TopoDS_Face& Result)
756 {
757   BRep_Builder B;
758 // Class BRep_Tool without fields and without Constructor :
759 //  BRep_Tool BT;
760 //  Standard_Integer NbInt =0;
761 //  Standard_Real Tol3d = Tol;
762   Standard_Boolean WithE3, WithE4;
763
764 // Return constraints
765   TopoDS_Vertex V1, V2, Vf, Vl;
766   TopoDS_Edge E1, E2, E3, E4;
767   E1 = TopoDS::Edge(EF);
768   E2 = TopoDS::Edge(EL);
769   
770   TopExp::Vertices(E1, Vf, Vl);
771   Vf.Orientation(TopAbs_FORWARD);
772   Vl.Orientation(TopAbs_FORWARD);
773
774   TopExp::Vertices(E2, V1, V2);
775   V1.Orientation(TopAbs_REVERSED);
776   V2.Orientation(TopAbs_REVERSED);  
777   
778   B.MakeEdge(E3);
779   B.MakeEdge(E4);
780
781   WithE3 = WithE4 = Standard_False;
782
783   if ((!Aux1.IsNull()) && (!Vf.IsSame(V1))) {
784     E3 = Aux1;
785 //    E3 = TopoDS::Edge(Aux1);
786     WithE3 = Standard_True;
787   }
788
789   if (Vf.IsSame(Vl)) {
790     E4 = E3;
791     E4.Reverse();
792     WithE4 = WithE3;
793   }
794   else if (!Aux2.IsNull() && (!Vl.IsSame(V2))) {
795     E4 = Aux2;
796 //    E4 = TopoDS::Edge(Aux2);
797     WithE4 = Standard_True;
798   }
799
800 #if DRAW
801   if (Affich) {
802     DBRep::Set("Fill_Edge1", E1);
803     DBRep::Set("Fill_Edge2", E2);
804     if (!E3.IsNull())
805       DBRep::Set("Fill_Edge3", E3);
806     if (!E4.IsNull())
807       DBRep::Set("Fill_Edge4", E4);
808   }
809 #endif
810
811 // Construction of a surface of revolution
812   Handle(Geom_Curve) Prof1, Prof2;
813   //Standard_Integer ii, jj;//, Nb;
814   Standard_Real f1, f2, l1, l2,/*d1, d2,*/ Angle;//, Eps = 1.e-9;
815 //  Prof1 = BT.Curve(E1, f1, l1);
816   Prof1 = BRep_Tool::Curve(E1, f1, l1);
817 //  Prof2 = BT.Curve(E2, f2, l2);
818   Prof2 = BRep_Tool::Curve(E2, f2, l2);
819   gp_Pnt P1, P2, P;
820   gp_Pnt2d p1, p2;
821   gp_Trsf Tf;
822   Tf.SetTransformation(Axe);
823
824 // Choose the angle of opening
825   P1 = Prof1->Value((f1+l1)/2);
826   P2 = Prof2->Value((f2+l2)/2);
827   P1.Transform(Tf);
828   P2.Transform(Tf);
829   p1.SetCoord(P1.Z(), P1.X());
830   p2.SetCoord(P2.Z(), P2.X());
831   gp_Vec2d v1(gp::Origin2d(), p1);
832   gp_Vec2d v2(gp::Origin2d(), p2);
833   if (v1.Magnitude() <= gp::Resolution() ||
834       v2.Magnitude() <= gp::Resolution())
835     return Standard_False;
836   Angle = v1.Angle(v2);
837
838   gp_Ax1 axe(Axe.Location(), Axe.YDirection());
839
840   if (Angle < 0) {
841     Angle = -Angle;
842     axe.Reverse();
843   }
844
845   Handle(Geom_SurfaceOfRevolution) Rev = 
846     new (Geom_SurfaceOfRevolution) (Prof1, axe);
847   
848   Handle(Geom_Surface) Surf = 
849     new (Geom_RectangularTrimmedSurface) (Rev, 0, Angle, f1, l1);
850
851   // Control the direction of the rotation
852   Standard_Boolean ToReverseResult = Standard_False;
853   gp_Vec d1u;
854   d1u = Surf->DN(0, (f1+l1)/2, 1, 0);
855   if (d1u.Angle(TangentOnPart1) > M_PI/2) { //Invert everything
856     ToReverseResult = Standard_True;
857     /*
858     axe.Reverse();
859     Angle = 2*M_PI - Angle;
860     Rev = new (Geom_SurfaceOfRevolution) (Prof1, axe);
861     Surf = new (Geom_RectangularTrimmedSurface) 
862            (Rev, 0, Angle, f1, l1);
863     */
864   }
865
866 #if DRAW
867   if (Affich) { 
868       char* Temp = "Surf_Init" ;
869       DrawTrSurf::Set(Temp, Surf);
870     }
871 #endif
872
873   Handle(Geom2d_Curve) C1, C2, C3, C4;
874 /*
875 // Deform the surface of revolution.
876   GeomPlate_BuildPlateSurface BPS;
877
878   Handle(BRepAdaptor_HSurface) AS;
879   Handle(BRepAdaptor_HCurve2d) AC2d;
880   Handle(Adaptor3d_HCurveOnSurface) HConS;
881 */
882   Handle(Geom2d_Line) L;
883   gp_Pnt2d P2d(0.,0.);
884
885   L = new (Geom2d_Line) (P2d, gp::DY2d());
886   C1 = new (Geom2d_TrimmedCurve) (L, f1, l1);
887
888   P2d.SetCoord(Angle,0.);
889   L = new (Geom2d_Line) (P2d, gp::DY2d());
890   C2 = new (Geom2d_TrimmedCurve) (L, f1, l1);
891  
892   // It is required to control the direction and the range.
893   C2->D0(f1, P2d);
894   Surf->D0(P2d.X(), P2d.Y(), P1);
895   C2->D0(l1, P2d);
896   Surf->D0(P2d.X(), P2d.Y(), P2);
897 //  P = BT.Pnt(V1);
898   P = BRep_Tool::Pnt(V1);
899   if (P.Distance(P2)+Tol < P.Distance(P1)) {
900     // E2 is parsed in the direction opposite to E1
901     C2->Reverse();
902     TopoDS_Vertex aux;
903     aux = V2;
904     V2 = V1;
905     V1 = aux;
906   }
907   GeomLib::SameRange(Precision::PConfusion(), C2, 
908                      C2->FirstParameter(),
909                      C2->LastParameter(),
910                      f2, l2, C3);
911   C2 = C3;
912
913 //  P1 = BT.Pnt(Vf);
914   P1 = BRep_Tool::Pnt(Vf);
915 //  P2 = BT.Pnt(V1);
916   P2 = BRep_Tool::Pnt(V1);
917 //  pointu_f = Vf.IsSame(V1) || (P1.Distance(P2) < BT.Tolerance(Vf));
918 //  P1 = BT.Pnt(Vl);
919   P1 = BRep_Tool::Pnt(Vl);
920 //  P2 = BT.Pnt(V2);
921   P2 = BRep_Tool::Pnt(V2);
922 //  pointu_l = Vl.IsSame(V2) || (P1.Distance(P2) < BT.Tolerance(Vl));
923
924   P2d.SetCoord(0.,f1);
925   L = new (Geom2d_Line) (P2d, gp::DX2d());
926   C3 = new (Geom2d_TrimmedCurve) (L, 0, Angle);
927
928
929   P2d.SetCoord(0.,l1);
930   L = new (Geom2d_Line) (P2d, gp::DX2d());   
931   C4 = new (Geom2d_TrimmedCurve) (L, 0, Angle);
932 /*
933   // Determine the constraints and  
934   // their parametric localisation.
935   if (!E1.IsNull()) {
936      AS = new BRepAdaptor_HSurface(TopoDS::Face(F1));
937      AC2d = new BRepAdaptor_HCurve2d();
938      AC2d->ChangeCurve2d().Initialize(E1,TopoDS::Face(F1));
939      HConS = new (Adaptor3d_HCurveOnSurface)();
940      HConS->ChangeCurve().Load(AC2d);
941      HConS->ChangeCurve().Load(AS);
942
943      Handle(BRepFill_CurveConstraint) Cont
944       = new BRepFill_CurveConstraint(HConS, 1, 15);
945      Cont->SetCurve2dOnSurf(C1);
946
947      BPS.Add(Cont);
948      NbInt = HConS->NbIntervals(GeomAbs_CN);
949    }
950
951   if (!E2.IsNull()) {
952     AS = new BRepAdaptor_HSurface(TopoDS::Face(F2));
953     AC2d = new BRepAdaptor_HCurve2d();
954     AC2d->ChangeCurve2d().Initialize(E2,TopoDS::Face(F2));
955     HConS = new (Adaptor3d_HCurveOnSurface);
956
957     HConS->ChangeCurve().Load(AC2d);
958     HConS->ChangeCurve().Load(AS);
959
960     Handle(BRepFill_CurveConstraint) Cont
961       = new BRepFill_CurveConstraint(HConS, 1, 15);
962     Cont->SetCurve2dOnSurf(C2);
963     BPS.Add(Cont);
964   }
965
966   if (WithE3) {
967     Handle(BRepAdaptor_HCurve) AC = new (BRepAdaptor_HCurve) (E3);
968     Handle(BRepFill_CurveConstraint) Cont
969       = new BRepFill_CurveConstraint(AC, 0);
970     Cont->SetCurve2dOnSurf(C3);
971     BPS.Add(Cont);
972   } 
973   else if (pointu_f) {
974     Standard_Real delta = Angle / 11;
975 //    P = BT.Pnt(Vf);
976     P = BRep_Tool::Pnt(Vf);
977     Handle(GeomPlate_PointConstraint) PC;
978     for (ii=1; ii<=10; ii++) {
979       C3->D0(ii*delta, P2d);
980       PC = new (GeomPlate_PointConstraint) (P, 0); 
981       PC->SetPnt2dOnSurf(P2d);
982       BPS.Add(PC);
983     }
984   }
985
986
987   if (WithE4) {
988     Handle(BRepAdaptor_HCurve) AC = new (BRepAdaptor_HCurve) (E4);
989     Handle(BRepFill_CurveConstraint) Cont
990       = new BRepFill_CurveConstraint(AC, 0);
991     Cont->SetCurve2dOnSurf(C4);
992     BPS.Add(Cont);
993   }
994   else if (pointu_l) {
995     Standard_Real delta = Angle / 11;
996 //    P = BT.Pnt(Vl);
997     P = BRep_Tool::Pnt(Vl);
998     Handle(GeomPlate_PointConstraint) PC;
999     for (ii=1; ii<=10; ii++) {
1000       C4->D0(ii*delta, P2d);
1001       PC = new (GeomPlate_PointConstraint) (P, 0); 
1002       PC->SetPnt2dOnSurf(P2d);
1003       BPS.Add(PC);
1004     }
1005   }
1006
1007   BPS.LoadInitSurface(Surf);
1008   BPS.Perform();
1009
1010   // Controle s'il y a une deformation effective
1011   Handle(GeomPlate_Surface) plate;
1012   plate = BPS.Surface();
1013   plate->SetBounds(0, Angle, f1, l1);
1014   Standard_Boolean Ok=Standard_True;
1015   Standard_Real u, v;
1016   d1 = (l1-f1)/5;
1017   d2 = Angle/5;
1018   for (ii=0; ii<=5 && Ok; ii++) {
1019     u = f1 + ii*d1;
1020     for (jj=0; jj<=5 && Ok; jj++) {
1021       v = jj*d2;
1022       plate->D0(u, v, P1);
1023       Surf->D0(u, v, P2);
1024       Ok = (P2.IsEqual(P1, Tol)); 
1025     }
1026   }
1027
1028
1029   if (!Ok) {
1030     // Approx de la plate surface
1031     // Bords naturelles => pas besoin de criteres.
1032     GeomConvert_ApproxSurface App(BPS.Surface(), 
1033                                   Tol3d,
1034                                   GeomAbs_C1, GeomAbs_C1,
1035                                   8, 8, 2*NbInt, 0);
1036     if (!App.HasResult()) {
1037 #if DEB
1038       cout << "Filling_Approx : Pas de resultat" << endl;
1039 #endif      
1040       return Standard_False;
1041     }
1042 #if DEB
1043     cout <<  "Filling_Approx Error 3d = " << 
1044       App.MaxError() << endl;
1045 #endif
1046     Surf = App.Surface();
1047     Tol3d = App.MaxError();  
1048   }
1049 */
1050
1051 // Update des Edges
1052   TopLoc_Location Loc;
1053   Handle(Geom_Curve) C3d;
1054   B.UpdateEdge(E1, C1, Surf, Loc, /*Tol3d*/Precision::Confusion());
1055   B.UpdateEdge(E2, C2, Surf, Loc, /*Tol3d*/Precision::Confusion());
1056  
1057   if (E3.IsSame(E4)) {
1058     if (!WithE3)
1059       {
1060         C3d = Surf->VIso(f1);
1061         E3 = BuildEdge(C3d, C3, Surf, Vf, V1, 0, Angle, /*Tol3d*/Precision::Confusion());
1062       }
1063     else
1064       {
1065         BRepAdaptor_Curve aCurve(E3);
1066         Standard_Real AngleOld = aCurve.LastParameter();
1067         if (Angle > AngleOld)
1068           {
1069             B.Range( E3, 0, Angle );
1070             TopoDS_Vertex V (TopExp::LastVertex(E3));
1071             Handle(BRep_TVertex)& TVlast = *((Handle(BRep_TVertex)*) &V.TShape());
1072             TVlast->Tolerance( Precision::Confusion() );
1073           }
1074       }
1075
1076     B.UpdateEdge(E3, C3, C4, Surf, Loc, /*Tol3d*/Precision::Confusion());
1077     E4 = E3;
1078     E4.Reverse();
1079   }
1080   else {
1081     if (!WithE3)
1082       {
1083         C3d = Surf->VIso(f1);
1084         E3 = BuildEdge(C3d, C3, Surf, Vf, V1, 0, Angle, /*Tol3d*/Precision::Confusion());
1085       }
1086     else
1087       {
1088         BRepAdaptor_Curve aCurve(E3);
1089         Standard_Real AngleOld = aCurve.LastParameter();
1090         if (Angle > AngleOld)
1091           {
1092             B.Range( E3, 0, Angle );
1093             TopoDS_Vertex V(TopExp::LastVertex(E3));
1094             Handle(BRep_TVertex)& TVlast = *((Handle(BRep_TVertex)*) &V.TShape());
1095             TVlast->Tolerance( Precision::Confusion() );
1096           }
1097
1098         B.UpdateEdge(E3, C3, Surf, Loc, /*Tol3d*/Precision::Confusion());
1099       }
1100     
1101     if (!WithE4)
1102       {
1103         C3d = Surf->VIso(l1);
1104         E4 = BuildEdge(C3d, C4, Surf, Vl, V2, 0, Angle, /*Tol3d*/Precision::Confusion());
1105       }
1106     else
1107       {
1108         BRepAdaptor_Curve aCurve(E4);
1109         Standard_Real AngleOld = aCurve.LastParameter();
1110         if (Angle > AngleOld)
1111           {
1112             B.Range( E4, 0, Angle );
1113             TopoDS_Vertex V (TopExp::LastVertex(E4));
1114             Handle(BRep_TVertex)& TVlast = *((Handle(BRep_TVertex)*)&V.TShape());
1115             TVlast->Tolerance( Precision::Confusion() );
1116           }
1117
1118         B.UpdateEdge(E4, C4, Surf, Loc, /*Tol3d*/Precision::Confusion());
1119       }
1120   }
1121
1122 //Construct face
1123   BuildFace(Surf,E1, E3, E2, E4, EEmap,
1124             Standard_False, Standard_False, 
1125             Result);
1126
1127 // Set the continuities.
1128   B.Continuity(E1, TopoDS::Face(F1), Result, GeomAbs_G1);
1129   B.Continuity(E2, TopoDS::Face(F2), Result, GeomAbs_G1);
1130
1131 // Render the calculated borders.
1132 //  if (!BT.Degenerated(E3))
1133   if (!BRep_Tool::Degenerated(E3))
1134     Aux1 = E3;
1135   else
1136     B.MakeEdge(Aux1); //Nullify
1137
1138 //  if (!BT.Degenerated(E4))
1139   if (!BRep_Tool::Degenerated(E4))
1140     Aux2 = E4;
1141   else
1142     B.MakeEdge(Aux2);
1143
1144   // Set the orientation
1145   gp_Vec D1U, D1V, N1, N2;
1146   C1->D0( (f1+l1)/2, P2d);
1147   Surf->D1(P2d.X(), P2d.Y(), P, D1U, D1V);
1148   N1 = D1U^D1V;
1149   
1150 //  C1 = BT.CurveOnSurface(E1, TopoDS::Face(F1), f2, l2);
1151   C1 = BRep_Tool::CurveOnSurface(E1, TopoDS::Face(F1), f2, l2);
1152   C1->D0( (f1+l1)/2, P2d);
1153   Handle(BRepAdaptor_HSurface) AS = new BRepAdaptor_HSurface(TopoDS::Face(F1));
1154   AS->D1(P2d.X(), P2d.Y(), P, D1U, D1V);
1155   N2 = D1U^D1V;
1156   
1157   if ( (F1.Orientation() == TopAbs_REVERSED) ^ (N1.Angle(N2)>M_PI/2) )
1158     Result.Orientation(TopAbs_REVERSED);
1159   else  Result.Orientation(TopAbs_FORWARD);
1160
1161   if (ToReverseResult)
1162     Result.Reverse();
1163
1164 #if DRAW
1165   if (Affich) DBRep::Set("BoucheTrou", Result);
1166 #endif
1167
1168   return Standard_True;
1169 }
1170
1171 //=======================================================================
1172 //function : Substitute
1173 //purpose  :
1174 //=======================================================================
1175 static void Substitute(BRepTools_Substitution& aSubstitute,
1176                        const TopoDS_Edge& Old,
1177                        const TopoDS_Edge& New) 
1178 {
1179   TopTools_ListOfShape listShape;
1180
1181   TopoDS_Vertex OldV1, OldV2, NewV1, NewV2;
1182   TopExp::Vertices( Old, OldV1, OldV2 );
1183   TopExp::Vertices( New, NewV1, NewV2 );
1184
1185   if (!aSubstitute.IsCopied( OldV1 ))
1186     {
1187       listShape.Append( NewV1.Oriented(TopAbs_FORWARD) );
1188       aSubstitute.Substitute( OldV1, listShape );
1189       listShape.Clear();
1190     }
1191   if (!aSubstitute.IsCopied( OldV2 ))
1192     {
1193       listShape.Append( NewV2.Oriented(TopAbs_FORWARD) );
1194       aSubstitute.Substitute( OldV2, listShape );
1195       listShape.Clear();
1196     }
1197   if (!aSubstitute.IsCopied( Old ))
1198     {
1199       listShape.Append( New.Oriented(TopAbs_FORWARD) );
1200       aSubstitute.Substitute( Old, listShape );
1201     }
1202 }
1203
1204 //=======================================================================
1205 //Function : SetCommonEdgeInFace
1206 //Purpose  : Replace an edge of the face by the corresponding edge from
1207 //           myUEdges
1208 //=======================================================================
1209 /*
1210 static void SetCommonEdgeInFace(BRepTools_Substitution& aSubstitute,
1211                                 const TopoDS_Shape& Face,
1212                                 const TopoDS_Shape& Edge)
1213 {
1214   if (Edge.IsNull())
1215     return;
1216
1217   Standard_Boolean done = Standard_False;
1218 // Class BRep_Tool without fields and without Constructor :
1219 //  BRep_Tool BT;
1220   Standard_Real f, l;
1221   TopExp_Explorer Exp(Face, TopAbs_EDGE);
1222   Handle(Geom_Curve) Cref, C;
1223   TopLoc_Location Lref, L;
1224 //  Cref = BT.Curve(TopoDS::Edge(Edge), Lref, f, l); 
1225   const TopoDS_Edge& NewEdge = TopoDS::Edge(Edge);
1226   Cref = BRep_Tool::Curve( NewEdge, Lref, f, l ); 
1227
1228   for ( ; Exp.More() && !done; Exp.Next()) {
1229 //    C = BT.Curve(TopoDS::Edge(Exp.Current()), L, f, l);
1230     const TopoDS_Edge& OldEdge = TopoDS::Edge(Exp.Current());
1231     C = BRep_Tool::Curve(OldEdge, L, f, l);
1232     if ((Cref==C) && (Lref == L)) {
1233       done = Standard_True;
1234       Substitute( aSubstitute, OldEdge, NewEdge );
1235     }
1236   }
1237 #if DEB
1238   if (!done) cout << "Substitution of Edge failed" << endl;
1239 #endif  
1240 }
1241 */
1242
1243 //=======================================================================
1244 //Fonction : KeepEdge
1245 //Objet : Find edges of the face supported by the same Curve.
1246 //=======================================================================
1247 static void KeepEdge(const TopoDS_Shape& Face,
1248                      const TopoDS_Shape& Edge,
1249                      TopTools_ListOfShape& List)
1250 {
1251   List.Clear();
1252 // Class BRep_Tool without fields and without Constructor :
1253 //  BRep_Tool BT;
1254   Standard_Real f, l;
1255   TopExp_Explorer Exp(Face, TopAbs_EDGE);
1256   Handle(Geom_Curve) Cref, C;
1257   TopLoc_Location Lref, L;
1258 //  Cref = BT.Curve(TopoDS::Edge(Edge), Lref, f, l); 
1259   Cref = BRep_Tool::Curve(TopoDS::Edge(Edge), Lref, f, l); 
1260
1261   for ( ; Exp.More(); Exp.Next()) {
1262 //    C = BT.Curve(TopoDS::Edge(Exp.Current()), L, f, l);
1263     C = BRep_Tool::Curve(TopoDS::Edge(Exp.Current()), L, f, l);
1264     if ((Cref==C) && (Lref == L)) { 
1265       List.Append(Exp.Current());
1266     }
1267   }
1268 }
1269
1270 //=======================================================================
1271 //Function : 
1272 //Objet : Construct a vertex via an iso
1273 //=======================================================================
1274 static void BuildVertex(const Handle(Geom_Curve)& Iso,
1275                         const Standard_Boolean isfirst,
1276                         const Standard_Real First,
1277                         const Standard_Real Last,
1278                         TopoDS_Shape& Vertex)
1279 {
1280   BRep_Builder B;
1281   Standard_Real val;
1282
1283   if (isfirst) val = First;
1284   else val = Last;
1285   B.MakeVertex(TopoDS::Vertex(Vertex),
1286                Iso->Value(val),
1287                Precision::Confusion());
1288 }
1289
1290 //=======================================================================
1291 //Function : 
1292 //Objet : Construct an empty edge
1293 //=======================================================================
1294 static TopoDS_Edge NullEdge(TopoDS_Shape& Vertex)
1295 {
1296   TopoDS_Edge E;
1297   BRep_Builder B;
1298   B.MakeEdge(E);
1299   Vertex.Orientation(TopAbs_FORWARD);
1300   B.Add(E, Vertex);
1301   B.Add(E, Vertex.Reversed());
1302   B.Degenerated(E, Standard_True);
1303   return E;
1304
1305 }
1306         
1307 //=======================================================================
1308 //Function : 
1309 //Objet : Construct an edge via an iso
1310 //=======================================================================
1311 static TopoDS_Edge BuildEdge(const Handle(Geom_Surface)& S,
1312                              const Standard_Boolean isUiso,
1313                              const Standard_Real ValIso,
1314                              const TopoDS_Shape&  VFirst,
1315                              const TopoDS_Shape&  VLast,
1316                              const Standard_Real  Tol)
1317 {
1318   TopoDS_Edge E;
1319   BRep_Builder B;
1320   Handle(Geom_Curve) Iso;
1321   Standard_Boolean sing = Standard_False;
1322   if (isUiso) {
1323     Iso = S->UIso(ValIso);
1324   }
1325   else {      
1326     Iso = S->VIso(ValIso);
1327   }
1328
1329   if (VFirst.IsSame(VLast)) { // Singular case ?
1330     gp_Pnt P; 
1331 // Class BRep_Tool without fields and without Constructor :
1332 //    BRep_Tool BT;
1333     const TopoDS_Vertex& V = TopoDS::Vertex(VFirst);
1334 //    Standard_Real tol = BT.Tolerance(V);
1335     Standard_Real tol = BRep_Tool::Tolerance(V);
1336     if (Tol > tol) tol = Tol; 
1337     Iso->D0((Iso->FirstParameter()+Iso->LastParameter())/2, P);
1338 //    if (P.Distance(BT.Pnt(V)) < tol) {
1339     if (P.Distance(BRep_Tool::Pnt(V)) < tol) {
1340       GeomAdaptor_Curve AC(Iso);
1341       sing = GCPnts_AbscissaPoint::Length(AC, tol/4) < tol;
1342     }
1343   }
1344
1345
1346   if (sing) { // Singular case
1347     TopoDS_Shape V;
1348     V = VFirst;
1349     E = NullEdge(V);
1350 //    Iso.Nullify();
1351 //    B.UpdateEdge(E, Iso, Precision::Confusion());
1352     B.Degenerated(E, Standard_True);
1353   }
1354   
1355   else {
1356     // Construction Via 3d
1357 //    if (isUiso) {
1358 //      Iso = S->UIso(ValIso);
1359     gp_Pnt P1,P2;
1360     Standard_Real p1, p2, p11, p12, p21, p22;
1361     Standard_Boolean fwd = Standard_False;
1362     p1 = Iso->FirstParameter();
1363     p2 = Iso->LastParameter();
1364     P1 = Iso->Value(p1);
1365     P2 = Iso->Value(p2);
1366
1367     Standard_Real t1 = BRep_Tool::Tolerance(TopoDS::Vertex(VFirst));
1368     Standard_Real t2 = BRep_Tool::Tolerance(TopoDS::Vertex(VLast));
1369
1370     BRep_Builder BB;
1371
1372     p11 = P1.Distance(BRep_Tool::Pnt(TopoDS::Vertex(VFirst)));
1373     p22 = P2.Distance(BRep_Tool::Pnt(TopoDS::Vertex(VLast)));
1374     p12 = P1.Distance(BRep_Tool::Pnt(TopoDS::Vertex(VLast)));
1375     p21 = P2.Distance(BRep_Tool::Pnt(TopoDS::Vertex(VFirst)));
1376     
1377     if(p11 < p12 && p22 < p21) fwd = Standard_True;
1378
1379     if(fwd) { //OCC500(apo)
1380       if (p11 >= t1) BB.UpdateVertex(TopoDS::Vertex(VFirst), 1.01*p11);
1381       if (p22 >= t2) BB.UpdateVertex(TopoDS::Vertex(VLast), 1.01*p22);
1382     }
1383     else {      
1384 //      Iso = S->VIso(ValIso);
1385       if (p12 >= t2) BB.UpdateVertex(TopoDS::Vertex(VLast), 1.01*p12);
1386       if (p21 >= t1) BB.UpdateVertex(TopoDS::Vertex(VFirst), 1.01*p21);
1387     }
1388
1389     BRepLib_MakeEdge MkE;
1390
1391 //    MkE.Init(Iso, 
1392 //           TopoDS::Vertex(VFirst), 
1393 //           TopoDS::Vertex(VLast), 
1394 //           Iso->FirstParameter(),
1395 //           Iso->LastParameter());
1396     if(fwd)
1397       MkE.Init(Iso, 
1398                TopoDS::Vertex(VFirst), 
1399                TopoDS::Vertex(VLast), 
1400                Iso->FirstParameter(),
1401                Iso->LastParameter());
1402     else
1403       MkE.Init(Iso,
1404                TopoDS::Vertex(VLast),
1405                TopoDS::Vertex(VFirst), 
1406                Iso->FirstParameter(),
1407                Iso->LastParameter());
1408
1409 //    if (!MkE.IsDone()) { // Il faut peut etre permuter les Vertex
1410 //      MkE.Init(Iso,
1411 //             TopoDS::Vertex(VLast),
1412 //             TopoDS::Vertex(VFirst), 
1413 //             Iso->FirstParameter(),
1414 //             Iso->LastParameter());
1415 //    }
1416
1417     if (!MkE.IsDone()) { // Erreur de construction !!     
1418 #ifdef DRAW
1419       char name[100];
1420       sprintf(name,"firstvertex_error");
1421       DBRep::Set(name, VFirst);
1422       sprintf(name,"lastvertex_error");
1423       DBRep::Set(name, VLast);
1424       sprintf(name,"curve3d_error");
1425       char* Temp = name ;
1426       DrawTrSurf::Set(Temp,Iso);
1427 //      DrawTrSurf::Set(name,Iso);
1428 #endif
1429       Standard_ConstructionError::Raise("BRepFill_Sweep::BuildEdge");
1430     }
1431
1432     E = MkE.Edge();
1433   }
1434
1435   // Associate 2d
1436   Handle(Geom2d_Line) L;
1437   TopLoc_Location Loc;
1438   if (isUiso) {
1439     gp_Pnt2d P(ValIso, 0);
1440     gp_Vec2d V(0., 1.);
1441     L = new (Geom2d_Line) (P, V);
1442   }
1443   else {
1444     gp_Pnt2d P(0., ValIso);
1445     gp_Vec2d V(1., 0.);
1446     L = new (Geom2d_Line) (P, V);
1447   }
1448
1449   B.UpdateEdge(E, L, S, Loc, Precision::Confusion());
1450   if (sing) B.Range(E, S, Loc, 
1451                     Iso->FirstParameter(), 
1452                     Iso->LastParameter());
1453
1454   Standard_Real MaxTol = 1.e-4;
1455   Standard_Real theTol;
1456   GeomAdaptor_Curve GAiso(Iso);
1457   Handle(GeomAdaptor_HCurve) GAHiso = new GeomAdaptor_HCurve(GAiso);
1458   GeomAdaptor_Surface GAsurf(S);
1459   Handle(GeomAdaptor_HSurface) GAHsurf = new GeomAdaptor_HSurface(GAsurf);
1460   CheckSameParameter( GAHiso, L, GAHsurf, MaxTol, theTol);
1461   B.UpdateEdge(E, theTol);
1462
1463   return E;
1464 }
1465
1466 //=======================================================================
1467 //Function : 
1468 //Objet : Complete an edge via an iso
1469 //=======================================================================
1470 static void UpdateEdge(TopoDS_Edge& E,
1471                        const Handle(Geom_Surface)& S,
1472                        const Standard_Boolean isUiso,
1473                        const Standard_Real ValIso)
1474 {
1475   BRep_Builder B;
1476   Handle(Geom2d_Line) L;
1477   Handle(Geom2d_Curve) PCurve, CL;
1478   TopLoc_Location Loc;
1479   Standard_Real UFirst, ULast, VFirst, VLast, F2d, L2d;
1480   S->Bounds( UFirst, ULast, VFirst, VLast);
1481
1482   Standard_Boolean sing = Standard_False;
1483   Handle(Geom_Curve) Iso;
1484   if (isUiso) {
1485     Iso = S->UIso(ValIso);
1486   }
1487   else {      
1488     Iso = S->VIso(ValIso);
1489   }
1490
1491   TopoDS_Vertex Vf, Vl;
1492   TopExp::Vertices(E, Vf, Vl);
1493   if (Vf.IsSame(Vl)) { // Singular case ?
1494     gp_Pnt Pmid; 
1495     Standard_Real tol = BRep_Tool::Tolerance(Vf);
1496     Iso->D0((Iso->FirstParameter()+Iso->LastParameter())/2, Pmid);
1497     if (Pmid.Distance(BRep_Tool::Pnt(Vf)) < tol) {
1498       GeomAdaptor_Curve AC(Iso);
1499       sing = GCPnts_AbscissaPoint::Length(AC, tol/4) < tol;
1500     }
1501   }
1502
1503   if (isUiso) {
1504     gp_Pnt2d P(ValIso, 0);
1505     gp_Vec2d V(0., 1.);
1506     L = new (Geom2d_Line) (P, V);
1507     F2d = VFirst;
1508     L2d = VLast;
1509   }
1510   else {
1511     gp_Pnt2d P(0., ValIso);
1512     gp_Vec2d V(1., 0.);
1513     L = new (Geom2d_Line) (P, V);
1514     F2d = UFirst;
1515     L2d = ULast;
1516   }
1517   CL = new (Geom2d_TrimmedCurve) (L, F2d, L2d);
1518
1519   // Control direction & Range
1520   Standard_Real R, First, Last, Tol=1.e-4;
1521   Standard_Boolean reverse = Standard_False;;
1522   
1523
1524 // Class BRep_Tool without fields and without Constructor :
1525 //  BRep_Tool BT;
1526   gp_Pnt POnS;
1527   gp_Pnt2d P2d;
1528 //  BT.Range(E, First, Last);
1529   BRep_Tool::Range(E, First, Last);
1530
1531   if (!Vf.IsSame(Vl)) {
1532     // Test distances between "FirstPoint" and "Vertex"
1533     P2d = CL->Value(F2d);
1534     POnS = S->Value(P2d.X(), P2d.Y());
1535 //    reverse = POnS.Distance(BT.Pnt(Vl)) < POnS.Distance(BT.Pnt(Vf));
1536     reverse = POnS.Distance(BRep_Tool::Pnt(Vl)) < POnS.Distance(BRep_Tool::Pnt(Vf));
1537   }
1538   else if (!sing) {
1539     // Test angle between "First Tangente"
1540     gp_Vec2d V2d;
1541     gp_Vec V3d, du, dv, dC3d;
1542     BRepAdaptor_Curve C3d(E);
1543
1544     C3d.D1(First, POnS,  dC3d);
1545     CL->D1(F2d, P2d, V2d);
1546     S->D1(P2d.X(), P2d.Y(), POnS, du, dv);
1547     V3d.SetLinearForm(V2d.X(), du, V2d.Y(), dv);
1548     reverse = ( dC3d.Angle(V3d) > Tol);
1549   }
1550   if (reverse ) { // Return curve 2d
1551     CL = new (Geom2d_TrimmedCurve)(L, F2d, L2d);
1552     CL->Reverse();  
1553     F2d = CL->FirstParameter();
1554     L2d = CL->LastParameter();
1555   }
1556
1557   if (sing)
1558     {
1559       Handle(Geom_Curve) NullCurve;
1560       B.UpdateEdge(E, NullCurve, 0.);
1561       B.Degenerated(E, Standard_True);
1562       B.Range(E, F2d, L2d);
1563       First = F2d;
1564       Last  = L2d;
1565     }
1566
1567   if (First != F2d || Last != L2d) {
1568     Handle(Geom2d_Curve) C2d;
1569     GeomLib::SameRange(Precision::PConfusion(), CL, 
1570                        F2d, L2d, First, Last,
1571                        C2d);
1572     CL = new (Geom2d_TrimmedCurve)(C2d, First, Last);
1573   }
1574
1575   // Update des Vertex
1576
1577   TopoDS_Vertex V;
1578
1579   P2d = CL->Value(First);
1580   POnS = S->Value(P2d.X(), P2d.Y());
1581   V = TopExp::FirstVertex(E); 
1582 //  R = POnS.Distance(BT.Pnt(V));
1583   R = POnS.Distance(BRep_Tool::Pnt(V));
1584   B.UpdateVertex(V, R);
1585
1586   P2d = CL->Value(Last);
1587   POnS = S->Value(P2d.X(), P2d.Y());
1588   V = TopExp::LastVertex(E);
1589 //  R = POnS.Distance(BT.Pnt(V));
1590   R = POnS.Distance(BRep_Tool::Pnt(V));
1591   B.UpdateVertex(V, R);
1592
1593   // Update Edge
1594   if (!sing && SameParameter(E, CL, S, Tol, R)) {
1595     B.UpdateEdge(E, R);
1596   }
1597
1598   PCurve = Couture(E, S, Loc);
1599   if (PCurve.IsNull())
1600     B.UpdateEdge(E, CL, S, Loc, Precision::Confusion());
1601   else { // Sewing edge
1602     TopoDS_Edge e = E;
1603     Oriente(S, e);
1604     if (e.Orientation() == TopAbs_REVERSED)
1605       B.UpdateEdge(E, CL, PCurve, S, Loc, Precision::Confusion());
1606     else         
1607       B.UpdateEdge(E, PCurve, CL, S, Loc, Precision::Confusion());
1608   }
1609
1610   // Attention to case not SameRange on its shapes (PRO13551)
1611 //  if (!BT.SameRange(E)) B.Range(E, S, Loc, First, Last);
1612   if (!BRep_Tool::SameRange(E)) B.Range(E, S, Loc, First, Last);
1613 }
1614
1615 //=======================================================================
1616 // Object : Check if a surface is degenerated
1617 //=======================================================================
1618 static Standard_Boolean IsDegen(const Handle(Geom_Surface)& S,
1619                                 const Standard_Real Tol)
1620 {
1621   Standard_Integer Nb = 5;
1622   Standard_Boolean B = Standard_True;
1623   Standard_Real Umax, Umin, Vmax, Vmin, t, dt, l;
1624   Standard_Integer ii;
1625   Handle(Geom_Curve) Iso;
1626   gp_Pnt P1,P2,P3;
1627   GCPnts_AbscissaPoint GC;
1628
1629   S->Bounds(Umin, Umax, Vmin, Vmax);
1630
1631   // Check the length of Iso-U
1632   t = (Umin + Umax)/2;
1633   S->D0(t, Vmin, P1);
1634   S->D0(t, (Vmin+Vmax)/2, P2);
1635   S->D0(t, Vmax, P3);
1636   B = ((P1.Distance(P2) + P2.Distance(P3)) < Tol);
1637   
1638   for (ii=1, dt = (Umax-Umin)/(Nb+1); B && (ii<=Nb); ii++) {
1639     t =  Umin + ii*dt;
1640     Iso = S->UIso(t);
1641     GeomAdaptor_Curve AC(Iso);
1642     l = GC.Length(AC, Tol/4);
1643     B = (l <= Tol);
1644   }
1645  
1646   if (B) return Standard_True;
1647
1648   // Check the length of Iso-V
1649   t = (Vmin + Vmax)/2;
1650   S->D0(Umin, t, P1);
1651   S->D0((Umin+Umax)/2, t, P2);
1652   S->D0(Umax, t, P3);
1653   B = ((P1.Distance(P2) + P2.Distance(P3)) < Tol); 
1654  
1655  
1656   for (ii=1, dt = (Vmax-Vmin)/(Nb+1); B && (ii<=Nb); ii++) {
1657     t =  Vmin + ii*dt;
1658     Iso = S->VIso(t);
1659     GeomAdaptor_Curve AC(Iso);
1660     l = GC.Length(AC, Tol/4);
1661     B = (l <= Tol);
1662   }  
1663     
1664   return B;
1665 }
1666
1667 //=======================================================================
1668 //function : Constructeur
1669 //purpose  : 
1670 //======================================================================
1671 BRepFill_Sweep::BRepFill_Sweep(const Handle(BRepFill_SectionLaw)& Section,
1672                                const Handle(BRepFill_LocationLaw)& Location,
1673                                const Standard_Boolean WithKPart) : 
1674                                isDone(Standard_False),
1675                                KPart(WithKPart)
1676
1677
1678 {
1679  mySec = Section;
1680  myLoc = Location;
1681  
1682  SetTolerance(1.e-4);
1683  SetAngularControl();
1684  myAuxShape.Clear();
1685
1686  myApproxStyle = GeomFill_Location;
1687  myContinuity  = GeomAbs_C2;
1688  myDegmax      = 11;
1689  mySegmax      = 30;
1690  myForceApproxC1 = Standard_False;
1691 }
1692
1693 //=======================================================================
1694 //function : SetBounds
1695 //purpose  : Define start and end shapes
1696 //======================================================================
1697  void BRepFill_Sweep::SetBounds(const TopoDS_Wire& First,
1698                                 const TopoDS_Wire& Last)
1699
1700   FirstShape = First;
1701   LastShape  = Last; 
1702
1703   // It is necessary to check the SameRange on its (PRO13551)
1704 #ifdef DEB
1705   Standard_Boolean issame = Standard_True;
1706 #endif
1707   BRep_Builder B;
1708   BRepTools_WireExplorer wexp;
1709   if (!FirstShape.IsNull()) {
1710     for (wexp.Init(FirstShape); wexp.More(); wexp.Next()) {
1711       if (!BRepLib::CheckSameRange(wexp.Current())) {
1712         B.SameRange(wexp.Current(), Standard_False);
1713         B.SameParameter(wexp.Current(), Standard_False);
1714 #ifdef DEB
1715         issame = Standard_False;
1716 #endif
1717       }
1718     }
1719   }
1720   
1721   if (!LastShape.IsNull()) {
1722     for (wexp.Init(LastShape); wexp.More(); wexp.Next()) {
1723       if (!BRepLib::CheckSameRange(wexp.Current())) {
1724         B.SameRange(wexp.Current(), Standard_False); 
1725         B.SameParameter(wexp.Current(), Standard_False);
1726 #ifdef DEB
1727         issame = Standard_False;
1728 #endif
1729       }
1730     }
1731   }
1732
1733 #if DEB
1734   if (!issame) 
1735     cout<<"Sweep Warning : Edge not SameRange in the limits"<<endl;
1736 #endif
1737 }
1738
1739 //=======================================================================
1740 //function : SetTolerance
1741 //purpose  :
1742 //======================================================================
1743  void BRepFill_Sweep::SetTolerance(const Standard_Real Tol3d,
1744                                    const Standard_Real BoundTol,
1745                                    const Standard_Real Tol2d, 
1746                                    const Standard_Real TolAngular) 
1747 {
1748   myTol3d = Tol3d;
1749   myBoundTol = BoundTol;
1750   myTol2d =Tol2d;
1751   myTolAngular = TolAngular;
1752 }
1753 //=======================================================================
1754 //function : SetAngularControl
1755 //purpose  :
1756 //======================================================================
1757  void BRepFill_Sweep::SetAngularControl(const Standard_Real MinAngle,
1758                                    const Standard_Real MaxAngle)
1759 {
1760   myAngMin = Max (MinAngle, Precision::Angular());
1761   myAngMax = Min (MaxAngle, 6.28);
1762 }
1763
1764 //=======================================================================
1765 //function : SetForceApproxC1
1766 //purpose  : Set the flag that indicates attempt to approximate
1767 //           a C1-continuous surface if a swept surface proved
1768 //           to be C0.
1769 //=======================================================================
1770  void BRepFill_Sweep::SetForceApproxC1(const Standard_Boolean ForceApproxC1)
1771 {
1772   myForceApproxC1 = ForceApproxC1;
1773 }
1774
1775 ///=======================================================================
1776 //function : CorrectApproxParameters
1777 //purpose  : 
1778 //=======================================================================
1779  Standard_Boolean BRepFill_Sweep::CorrectApproxParameters()
1780 {
1781   TopoDS_Wire thePath = myLoc->Wire();
1782   GeomAbs_Shape    NewCont   = myContinuity;
1783   Standard_Integer NewSegmax = mySegmax;
1784
1785   TopoDS_Iterator iter(thePath);
1786   for (; iter.More(); iter.Next())
1787     {
1788       TopoDS_Edge anEdge = TopoDS::Edge(iter.Value());
1789       BRepAdaptor_Curve aBAcurve(anEdge);
1790       GeomAbs_Shape aContinuity = aBAcurve.Continuity();
1791       Standard_Integer aNbInterv = aBAcurve.NbIntervals(GeomAbs_CN);
1792       if (aContinuity < NewCont)
1793         NewCont = aContinuity;
1794       if (aNbInterv > NewSegmax)
1795         NewSegmax = aNbInterv;
1796     }
1797
1798   Standard_Boolean Corrected = Standard_False;
1799   if (NewCont != myContinuity || NewSegmax != mySegmax)
1800     Corrected = Standard_True;
1801   myContinuity = NewCont;
1802   mySegmax     = NewSegmax;
1803   return Corrected;
1804 }
1805
1806 //=======================================================================
1807 //function : BuildWire
1808 //purpose  : Construit a wire by sweeping
1809 //======================================================================
1810  Standard_Boolean BRepFill_Sweep::
1811  BuildWire(const BRepFill_TransitionStyle /*Transition*/)
1812 {
1813   Standard_Integer ipath, isec = 1;
1814   gp_Pnt P1;//, P2;
1815
1816   BRep_Builder B;
1817 // Class BRep_Tool without fields and without Constructor :
1818 //  BRep_Tool BT;
1819   Standard_Integer NbPath = myLoc->NbLaw();
1820   Standard_Boolean vclose;
1821   vclose = (myLoc->IsClosed() && (myLoc->IsG1(0, myTol3d)>= 0));
1822   Error = 0.;
1823   Handle(Geom_Surface) S;
1824   Handle(Geom_Curve) Iso;
1825   Standard_Real val, bid, First, Last, Tol;
1826
1827  TopoDS_Wire wire;
1828  TopoDS_Edge E;
1829  B.MakeWire(wire);
1830
1831  // (1) Construction of all curves
1832
1833  // (1.1) Construction of Tables
1834  myFaces = new (TopTools_HArray2OfShape) (1, 1, 1, NbPath);
1835  myUEdges = new (TopTools_HArray2OfShape) (1, 2, 1, NbPath);
1836  myVEdges = new (TopTools_HArray2OfShape) (1, 1, 1, NbPath+1);
1837
1838  // (1.2) Calculate curves / vertex / edge
1839   for (ipath=1; ipath <=NbPath; ipath++) { 
1840     // Curve by iso value
1841     GeomFill_Sweep Sweep(myLoc->Law(ipath), KPart);
1842     Sweep.SetTolerance(myTol3d, myBoundTol, myTol2d, myTolAngular);
1843     Sweep.SetForceApproxC1(myForceApproxC1);
1844     Sweep.Build(mySec->Law(isec), myApproxStyle, myContinuity, myDegmax, mySegmax);
1845     if (!Sweep.IsDone())  
1846       return Standard_False;
1847     S = Sweep.Surface();
1848     if (Sweep.ExchangeUV()) {
1849       if  (Sweep.UReversed()) S->Bounds(First, Last, bid, val);
1850       else S->Bounds(First, Last, val, bid);
1851       Iso = S->VIso(val); 
1852     }
1853     else {
1854       if (Sweep.UReversed()) S->Bounds(bid,  val, First, Last);
1855       else  S->Bounds(val, bid, First, Last);
1856       Iso = S->UIso(val); 
1857     }
1858     // Vertex by position
1859     if (ipath < NbPath) 
1860       BuildVertex(Iso, Standard_False, First, Last, 
1861                   myVEdges->ChangeValue(1, ipath+1));
1862     else {
1863       if (vclose) { 
1864         TopoDS_Vertex& V = TopoDS::Vertex(myVEdges->ChangeValue(1, 1));
1865         myVEdges->SetValue(1, ipath+1, V);
1866         Iso->D0(Last, P1);
1867 //      Tol = P1.Distance(BT.Pnt(V));
1868         Tol = P1.Distance(BRep_Tool::Pnt(V));
1869         B.UpdateVertex(V, Tol);
1870       }
1871       else {
1872         if (!LastShape.IsNull()) myVEdges->SetValue(1, NbPath, FirstShape);
1873         else BuildVertex(Iso, Standard_False, First, Last, 
1874                          myVEdges->ChangeValue(1, NbPath+1));
1875       }
1876     }
1877   
1878     if (ipath > 1) {
1879       Iso->D0(First, P1);
1880       TopoDS_Vertex& V = TopoDS::Vertex(myVEdges->ChangeValue(1, ipath));
1881 //      Tol = P1.Distance(BT.Pnt(V));
1882       Tol = P1.Distance(BRep_Tool::Pnt(V));
1883       B.UpdateVertex(V, Tol);
1884     }
1885     if (ipath == 1) {
1886       if (!FirstShape.IsNull()) myVEdges->SetValue(1,1, FirstShape);
1887       else BuildVertex(Iso, Standard_True, First, Last, 
1888                        myVEdges->ChangeValue(1, 1));
1889     }
1890
1891     // Construction of the edge
1892     BRepLib_MakeEdge MkE;
1893     MkE.Init(Iso, 
1894              TopoDS::Vertex(myVEdges->Value(1, ipath)), 
1895              TopoDS::Vertex(myVEdges->Value(1, ipath+1)), 
1896              Iso->FirstParameter(),
1897              Iso->LastParameter());
1898     if (!MkE.IsDone()) { // Error of construction !!     
1899 #ifdef DRAW
1900       char name[100];
1901       sprintf(name,"firstvertex_error");
1902       DBRep::Set(name, myVEdges->Value(1, ipath));
1903       sprintf(name,"lastvertex_error");
1904       DBRep::Set(name, myVEdges->Value(1, ipath+1));
1905       sprintf(name,"curve3d_error");
1906       char* Temp = name ;
1907       DrawTrSurf::Set(Temp,Iso);
1908 //       DrawTrSurf::Set(name,Iso);
1909       Standard_ConstructionError::Raise("BRepFill_Sweep::BuildEdge");
1910 #endif
1911        return Standard_False;  
1912      }
1913     E = MkE.Edge();
1914 #if DRAW
1915     if (Affich) {
1916       sprintf(name,"Surf_%d", ipath);
1917       char* Temp = name;
1918       DrawTrSurf::Set(Temp, S);
1919 //      DrawTrSurf::Set(name, S);
1920       sprintf(name,"Edge_%d", ipath);
1921       DBRep::Set(name, E);
1922     }
1923 #endif
1924     B.UpdateEdge(E, Sweep.ErrorOnSurface());
1925     B.Add(wire, E);
1926     myFaces->SetValue(1, ipath, E);
1927   }  
1928   myShape = wire;
1929   return Standard_True;
1930 }
1931
1932 //=======================================================================
1933 //function : BuildShell
1934 //purpose  : Construct a Shell by sweeping
1935 //======================================================================
1936  Standard_Boolean BRepFill_Sweep::
1937  BuildShell(const BRepFill_TransitionStyle /*Transition*/,
1938             const Standard_Integer IFirst,
1939             const Standard_Integer ILast,
1940             TopTools_MapOfShape& ReversedEdges,
1941             BRepFill_DataMapOfShapeHArray2OfShape& Tapes,
1942             const Standard_Real ExtendFirst,
1943             const Standard_Real ExtendLast) 
1944 {
1945   Standard_Integer ipath, isec, IPath;
1946 #ifdef DRAW
1947   char name[100];
1948 #endif
1949   BRep_Builder B;
1950   Standard_Integer NbPath = ILast - IFirst;
1951   Standard_Integer NbLaw =  mySec->NbLaw();
1952   Standard_Boolean uclose, vclose,  constSection, hasdegen = Standard_False;
1953   constSection = mySec->IsConstant();
1954   uclose = mySec->IsUClosed();
1955   vclose = (mySec->IsVClosed() && myLoc->IsClosed()) && 
1956            (NbPath == myLoc->NbLaw()) && (myLoc->IsG1(0, myTol3d)>= 0);
1957   Error = 0.;
1958
1959   // (1) Construction of all surfaces
1960
1961   // (1.1) Construction of Tables
1962
1963   TColStd_Array2OfInteger ExchUV(1, NbLaw, 1, NbPath);
1964   TColStd_Array2OfInteger UReverse(1, NbLaw, 1, NbPath);
1965   TColStd_Array2OfInteger Degenerated(1, NbLaw, 1, NbPath);
1966   Degenerated.Init(0);
1967   // No VReverse for the moment...
1968   TColStd_Array2OfReal TabErr(1, NbLaw   , 1, NbPath);
1969   TColGeom_Array2OfSurface TabS(1, NbLaw , 1, NbPath);
1970   
1971   TopTools_Array2OfShape UEdge(1, NbLaw+1, 1, NbPath);
1972   TopTools_Array2OfShape VEdge(1, NbLaw  , 1, NbPath+1);
1973   TopTools_Array2OfShape Vertex(1,NbLaw+1, 1, NbPath+1);
1974
1975   TopoDS_Vertex VNULL;
1976   VNULL.Nullify();
1977   Vertex.Init(VNULL);
1978
1979   TopTools_Array1OfShape SecVertex(1, NbLaw+1);
1980   TColStd_Array1OfReal VError(1, NbLaw+1);
1981   TColStd_Array1OfReal Vi(1, NbPath+1);
1982
1983 //Initialization of management of parametric intervals 
1984 //(Case of evolutionary sections)
1985   Standard_Real Length, SecDom, SecDeb;
1986   myLoc->CurvilinearBounds(myLoc->NbLaw(), SecDom, Length);
1987   mySec->Law(1)->GetDomain(SecDeb, SecDom);
1988   SecDom -= SecDeb;
1989   if (IFirst > 1) {
1990     Standard_Real Lf, Ll;
1991     myLoc->CurvilinearBounds(IFirst-1, Lf, Ll);
1992     Vi(1) = SecDeb + (Ll/Length)*SecDom;
1993   }
1994   else 
1995     Vi(1) = SecDeb;
1996
1997   // Error a priori on vertices
1998   if (constSection) {
1999     for (isec=1; isec<=NbLaw+1; isec++) {
2000       VError(isec) = mySec->VertexTol(isec-1, 0.);
2001       SecVertex(isec) = mySec->Vertex(isec, 0.);
2002     }
2003   }
2004  
2005
2006   // (1.2) Calculate surfaces
2007   for (ipath=1, IPath=IFirst; ipath <=NbPath; ipath++, IPath++) {
2008
2009     GeomFill_Sweep Sweep(myLoc->Law(IPath), KPart);
2010     Sweep.SetTolerance(myTol3d, myBoundTol, myTol2d, myTolAngular);
2011     Sweep.SetForceApproxC1(myForceApproxC1);
2012
2013     // Case of evolutionary section, definition of parametric correspondence
2014     if (!constSection) {
2015       Standard_Real lf, ll, Lf, Ll;
2016       myLoc->Law(IPath)->GetDomain(lf, ll);
2017       myLoc->CurvilinearBounds(IPath, Lf, Ll);
2018       Vi(ipath+1) = SecDeb + (Ll/Length)*SecDom;
2019       Sweep.SetDomain(lf, ll, Vi(ipath), Vi(ipath+1));
2020     }
2021     else //section is constant
2022       {
2023         Standard_Real lf, ll, Lf, Ll;
2024         myLoc->Law(IPath)->GetDomain(lf, ll);
2025         myLoc->CurvilinearBounds(IPath, Lf, Ll);
2026         Vi(ipath+1) = SecDeb + (Ll/Length)*SecDom;
2027       }
2028     
2029     for(isec=1; isec<=NbLaw; isec++) {
2030       Sweep.Build(mySec->Law(isec), myApproxStyle, myContinuity, myDegmax, mySegmax);
2031       if (!Sweep.IsDone()) 
2032         return Standard_False;
2033       TabS(isec,ipath) = Sweep.Surface();
2034       TabErr(isec,ipath) = Sweep.ErrorOnSurface();
2035       ExchUV(isec, ipath) =  Sweep.ExchangeUV();
2036       UReverse(isec, ipath) =  Sweep.UReversed();
2037       if (Sweep.ErrorOnSurface()>Error) Error = Sweep.ErrorOnSurface();
2038
2039       if ((ipath==1)&&(ExtendFirst>0)) {
2040         Handle(Geom_BoundedSurface) BndS;
2041         BndS = Handle(Geom_BoundedSurface)::DownCast(TabS(isec,ipath));
2042         GeomLib::ExtendSurfByLength(BndS, ExtendFirst, 1, 
2043                                     Sweep.ExchangeUV(), Standard_False);
2044         TabS(isec,ipath) = BndS;
2045       }
2046       if ((ipath==NbPath)&&(ExtendLast>0)){
2047         Handle(Geom_BoundedSurface) BndS;
2048         BndS = Handle(Geom_BoundedSurface)::DownCast(TabS(isec,ipath));
2049         GeomLib::ExtendSurfByLength(BndS, ExtendLast, 1, 
2050                                     Sweep.ExchangeUV(), Standard_True);
2051         TabS(isec,ipath) = BndS;
2052       }
2053
2054 #ifdef DRAW
2055       if (Affich) {
2056         sprintf(name,"Surf_%d_%d", isec, IPath);
2057         char* Temp = name ;
2058         DrawTrSurf::Set(Temp, TabS(isec,ipath));
2059       }
2060 #endif
2061     }
2062   }
2063
2064   // (2) Construction of Edges
2065   Standard_Real UFirst, ULast, VFirst, VLast;
2066   Standard_Boolean exuv, singu, singv;
2067   Handle(Geom_Surface) S;
2068
2069   // (2.0) return preexisting Edges and vertices
2070   TopoDS_Edge E;
2071   TColStd_Array1OfBoolean IsBuilt(1, NbLaw);
2072   IsBuilt.Init(Standard_False);
2073   TopTools_Array1OfShape StartEdges(1, NbLaw);
2074   if (! FirstShape.IsNull() && (IFirst==1)) {
2075     mySec->Init(FirstShape);
2076     for (isec=1; isec<=NbLaw; isec++) {
2077       E = mySec->CurrentEdge();
2078       VEdge(isec, 1) = E;
2079       if (E.Orientation() == TopAbs_REVERSED)
2080         Vertex(isec+1, 1) = TopExp::FirstVertex(E);
2081       else 
2082         Vertex(isec+1, 1) =  TopExp::LastVertex(E);
2083       UpdateVertex(IFirst-1, isec+1, 
2084                    TabErr(isec, 1), Vi(1),  Vertex(isec+1, 1));
2085
2086       StartEdges(isec) = E;
2087       if (Tapes.IsBound(E))
2088       {
2089         IsBuilt(isec) = Standard_True;
2090         
2091         //Initialize VEdge, UEdge, Vertex and myFaces
2092         Standard_Integer j;
2093         for (j = 1; j <= NbPath+1; j++)
2094         {
2095           VEdge(isec, j) = Tapes(E)->Value(1, j);
2096           VEdge(isec, j).Reverse(); //direction of round is reversed
2097         }
2098         Standard_Integer ifirst = isec+1, ilast = isec; //direction of round is reversed
2099         for (j = 1; j <= NbPath; j++)
2100           UEdge(ifirst, j) = Tapes(E)->Value(2, j);
2101         for (j = 1; j <= NbPath; j++)
2102           UEdge(ilast, j) = Tapes(E)->Value(3, j);
2103         for (j = 1; j <= NbPath+1; j++)
2104           Vertex(ifirst, j) = Tapes(E)->Value(4, j);
2105         for (j = 1; j <= NbPath+1; j++)
2106           Vertex(ilast, j) = Tapes(E)->Value(5, j);
2107         for (j = 1; j <= NbPath; j++)
2108           myFaces->SetValue(isec, j, Tapes(E)->Value(6, j));
2109
2110         if (uclose && isec == 1)
2111         {
2112           for (j = 1; j <= NbPath; j++)
2113             UEdge(NbLaw+1, j) = UEdge(1, j);
2114           for (j = 1; j <= NbPath+1; j++)
2115             Vertex(NbLaw+1, j) = Vertex(1, j);
2116         }
2117         if (uclose && isec == NbLaw)
2118         {
2119           for (j = 1; j <= NbPath; j++)
2120             UEdge(1, j) = UEdge(NbLaw+1, j);
2121           for (j = 1; j <= NbPath+1; j++)
2122             Vertex(1, j) = Vertex(NbLaw+1, j);
2123         }
2124       }
2125       else
2126       {
2127         Handle(TopTools_HArray2OfShape) EmptyArray = new TopTools_HArray2OfShape(1, 6, 1, NbPath+1);
2128         Tapes.Bind(E, EmptyArray);
2129       }
2130     }
2131     
2132     if (VEdge(1, 1).Orientation() == TopAbs_REVERSED)
2133       Vertex(1, 1) =  TopExp::LastVertex(TopoDS::Edge(VEdge(1, 1)));
2134     else
2135       Vertex(1, 1) = TopExp::FirstVertex(TopoDS::Edge(VEdge(1, 1)));
2136     UpdateVertex(IFirst-1, 1, 
2137                  TabErr(1, 1), Vi(1),  Vertex(1, 1));
2138   }
2139   
2140   Standard_Real u, v, aux;
2141   Standard_Boolean ureverse;
2142   for (isec=1; isec<=NbLaw+1; isec++) {
2143     // Return data
2144     if (isec >NbLaw) {
2145       S = TabS(NbLaw, 1);
2146       ureverse = UReverse(NbLaw, 1);
2147       exuv = ExchUV(NbLaw, 1);
2148     }
2149     else {
2150       S = TabS(isec, 1);
2151       ureverse = UReverse(isec, 1);
2152       exuv = ExchUV(isec, 1);
2153     }
2154     S->Bounds(UFirst, ULast, VFirst, VLast);
2155     
2156     // Choice of parameters
2157     if (ureverse) {
2158       if (exuv) {
2159         aux = VFirst; VFirst = VLast; VLast = aux;        
2160       }
2161       else {
2162         aux = UFirst; UFirst = ULast; ULast = aux;
2163       }
2164     }
2165     if (isec!= NbLaw+1) {
2166       u = UFirst;
2167       v = VFirst;
2168     }
2169     else {
2170       if (exuv) {
2171         u = UFirst;
2172           v = VLast;
2173       }
2174       else {
2175         u = ULast;
2176         v = VFirst;
2177       }
2178     }
2179     
2180     // construction of vertices
2181     if (Vertex(isec, 1).IsNull())
2182       B.MakeVertex(TopoDS::Vertex(Vertex(isec, 1)), 
2183                    S->Value(u,v), 
2184                    mySec->VertexTol(isec-1,Vi(1)));
2185     else
2186     {
2187       TopLoc_Location Identity;
2188       Vertex(isec, 1).Location(Identity);
2189       B.UpdateVertex(TopoDS::Vertex(Vertex(isec, 1)),
2190                      S->Value(u,v), 
2191                        mySec->VertexTol(isec-1,Vi(1)));
2192     }
2193   } //end of for (isec=1; isec<=NbLaw+1; isec++)
2194   
2195   if (! LastShape.IsNull() && (ILast==myLoc->NbLaw()+1) ) {
2196     mySec->Init(LastShape);
2197     for (isec=1; isec<=NbLaw; isec++) {
2198       E = mySec->CurrentEdge();
2199       if (VEdge(isec, NbPath+1).IsNull())
2200         VEdge(isec, NbPath+1) = E;
2201
2202       if (Vertex(isec+1, NbPath+1).IsNull())
2203       {
2204         if (VEdge(isec, NbPath+1).Orientation() == TopAbs_REVERSED)
2205           Vertex(isec+1, NbPath+1) = TopExp::FirstVertex(TopoDS::Edge(VEdge(isec, NbPath+1)));
2206         else 
2207           Vertex(isec+1, NbPath+1) = TopExp::LastVertex(TopoDS::Edge(VEdge(isec, NbPath+1)));
2208       }
2209       UpdateVertex(ILast-1, isec+1, TabErr(isec, NbPath), 
2210                    Vi(NbPath+1),  Vertex(isec+1, NbPath+1));
2211     }
2212
2213     if (Vertex(1,  NbPath+1).IsNull())
2214     {
2215       if (VEdge(1,  NbPath+1).Orientation() == TopAbs_REVERSED)
2216         Vertex(1,  NbPath+1) = TopExp::LastVertex(TopoDS::Edge(VEdge(1,  NbPath+1)));
2217       else
2218         Vertex(1,  NbPath+1) = TopExp::FirstVertex(TopoDS::Edge(VEdge(1, NbPath+1)));
2219     }
2220     UpdateVertex(ILast-1, 1, 
2221                  TabErr(1, NbPath), Vi(NbPath+1),  Vertex(1, NbPath+1 ));
2222   }
2223   
2224   for (isec=1; isec<=NbLaw+1; isec++) {
2225     // Return data
2226     if (isec >NbLaw) {
2227         S = TabS(NbLaw, NbPath);
2228         ureverse = UReverse(NbLaw, NbPath);
2229         exuv = ExchUV(NbLaw, NbPath);
2230     }
2231     else {
2232       S = TabS(isec, NbPath);
2233       ureverse = UReverse(isec, NbPath);
2234       exuv = ExchUV(isec, NbPath);
2235     }
2236     S->Bounds(UFirst, ULast, VFirst, VLast);
2237     
2238     // Choice of parametres
2239     if (ureverse) {
2240       if (exuv) {
2241         aux = VFirst; VFirst = VLast; VLast = aux;        
2242       }
2243       else {
2244         aux = UFirst; UFirst = ULast; ULast = aux;
2245       }
2246     }
2247     if (isec == NbLaw+1) {
2248       u = ULast;
2249       v = VLast;
2250     }
2251     else {
2252       if (exuv) {
2253         u = ULast;
2254         v = VFirst;
2255       }
2256       else {
2257         u = UFirst;
2258         v = VLast;
2259       }
2260     }
2261     
2262     // construction of vertex
2263     if (Vertex(isec, NbPath+1).IsNull())
2264       B.MakeVertex(TopoDS::Vertex(Vertex(isec, NbPath+1)), 
2265                    S->Value(u,v), 
2266                    mySec->VertexTol(isec-1, Vi(NbPath+1)));
2267       else
2268       {
2269         TopLoc_Location Identity;
2270         Vertex(isec, NbPath+1).Location(Identity);
2271         B.UpdateVertex(TopoDS::Vertex(Vertex(isec, NbPath+1)), 
2272                        S->Value(u,v), 
2273                        mySec->VertexTol(isec-1, Vi(NbPath+1)));
2274       }
2275   } //end of for (isec=1; isec<=NbLaw+1; isec++)
2276
2277  
2278   // ---------- Creation of Vertex and edge ------------
2279   ReversedEdges.Clear();
2280   for (ipath=1, IPath=IFirst; ipath<=NbPath; 
2281        ipath++, IPath++) {
2282     for (isec=1; isec <=NbLaw; isec++) {
2283       if (IsBuilt(isec))
2284         continue;
2285       
2286       S = TabS(isec, ipath);
2287       exuv = ExchUV(isec, ipath);
2288       S->Bounds(UFirst, ULast, VFirst, VLast);
2289       if (UReverse(isec, ipath)) {
2290         Standard_Real aux;
2291         if (exuv) {
2292           aux = VFirst; VFirst = VLast; VLast = aux;      
2293         }
2294         else {
2295           aux = UFirst; UFirst = ULast; ULast = aux;
2296         }
2297       }
2298
2299       // (2.1) Construction of new vertices
2300       if (isec == 1) {
2301         if (ipath == 1 && Vertex(1, 1).IsNull()) {
2302           // All first
2303           if (constSection)
2304             myLoc->PerformVertex(IPath-1, 
2305                                  TopoDS::Vertex(SecVertex(1)), 
2306                                  VError(1),
2307                                  TopoDS::Vertex(Vertex(1, 1)));
2308           else
2309             myLoc->PerformVertex(IPath-1, 
2310                                  mySec->Vertex(1,Vi(1)), 
2311                                  mySec->VertexTol(0,Vi(1)),
2312                                  TopoDS::Vertex(Vertex(1, 1)));
2313         }
2314         // the first and the next column
2315         if (vclose &&(ipath == NbPath) ) {
2316           Vertex(1, ipath+1) =  Vertex(1, 1);
2317         }
2318         else if (Vertex(1, ipath+1).IsNull()) {
2319           if (constSection)
2320             myLoc->PerformVertex(IPath, 
2321                                  TopoDS::Vertex(SecVertex(1)),
2322                                  TabErr(1,ipath)+VError(1),
2323                                  TopoDS::Vertex(Vertex(1, ipath+1)) );
2324           else
2325             myLoc->PerformVertex(IPath, 
2326                                  mySec->Vertex(1,Vi(ipath+1)), 
2327                                  TabErr(1,ipath) +
2328                                  mySec->VertexTol(0,Vi(ipath+1)),
2329                                  TopoDS::Vertex(Vertex(1, ipath+1)));
2330
2331           if (MergeVertex(Vertex(1,ipath), Vertex(1,ipath+1))) {
2332             UEdge(1, ipath) = NullEdge(Vertex(1,ipath));
2333           }
2334          }
2335        }
2336
2337       if (ipath == 1) {
2338         if (uclose && (isec == NbLaw)) {
2339           Vertex(isec+1, 1) =  Vertex(1, 1);
2340         }  
2341         else if (Vertex(isec+1, 1).IsNull()) {
2342           if (constSection)
2343             myLoc->PerformVertex(IPath-1, 
2344                TopoDS::Vertex(SecVertex(isec+1)),
2345                TabErr(isec,1)+VError(isec+1),
2346                TopoDS::Vertex(Vertex(isec+1, 1)) );
2347           else
2348             myLoc->PerformVertex(IPath-1, 
2349                mySec->Vertex(isec+1,Vi(1)), 
2350                TabErr(isec,1) +
2351                mySec->VertexTol(isec,Vi(1)),
2352                TopoDS::Vertex(Vertex(isec+1, 1)) );
2353                                             
2354           if (MergeVertex(Vertex(isec,1), Vertex(isec+1,1))) {
2355             VEdge(isec, 1) = NullEdge(Vertex(isec, 1)); 
2356           }
2357         }
2358       }
2359
2360       if (uclose && (isec == NbLaw)) {
2361         Vertex(isec+1, ipath+1) = Vertex(1, ipath+1);
2362       }
2363       else if (vclose && (ipath == NbPath)) {
2364         Vertex(isec+1, ipath+1) =  Vertex(isec+1, 1);
2365       }
2366       else if (Vertex(isec+1, ipath+1).IsNull()) {
2367         if (constSection)
2368           myLoc->PerformVertex(IPath, 
2369                                TopoDS::Vertex(SecVertex(isec+1)),
2370                                TabErr(isec, ipath)+ VError(isec+1),
2371                                TopoDS::Vertex(Vertex(isec+1, ipath+1)) );
2372         else
2373          myLoc->PerformVertex(IPath, 
2374                               mySec->Vertex(isec+1,Vi(ipath+1)),
2375                               TabErr(isec, ipath) + 
2376                               mySec->VertexTol(isec, Vi(ipath+1)),
2377                               TopoDS::Vertex(Vertex(isec+1, ipath+1)) ); 
2378       }
2379
2380       // Singular cases
2381       singv = MergeVertex(Vertex(isec,ipath+1), Vertex(isec+1,ipath+1));
2382       singu = MergeVertex(Vertex(isec+1,ipath), Vertex(isec+1,ipath+1));
2383
2384       
2385
2386       if (singu || singv) {
2387         Degenerated(isec, ipath) = IsDegen(TabS(isec,ipath), 
2388                                            Max(myTol3d, TabErr(isec,ipath)));
2389       }
2390       if (Degenerated(isec, ipath)) { 
2391 #if DEB
2392         cout << "Sweep : Degenerated case" << endl;
2393 #endif
2394         hasdegen = Standard_True;
2395         // Particular construction of edges
2396         if (UEdge(isec+1, ipath).IsNull()) {
2397           if (singu) {
2398             // Degenerated edge
2399             UEdge(isec+1, ipath) = NullEdge(Vertex(isec+1,ipath));
2400           }
2401           else { // Copy the previous edge
2402             UEdge(isec+1, ipath) = UEdge(isec, ipath);
2403           }
2404         }
2405         if (VEdge(isec, ipath+1).IsNull()) {
2406           if (singv) {
2407             // Degenerated Edge
2408             VEdge(isec, ipath+1) = NullEdge(Vertex(isec,ipath+1));
2409           }
2410           else { // Copy the previous edge
2411             VEdge(isec, ipath+1) = VEdge(isec, ipath);
2412           }
2413         }
2414       }
2415       else { // Construction of edges by isos
2416         if (exuv) {
2417           Standard_Real UV;
2418           UV = UFirst; UFirst = VFirst; VFirst = UV;
2419           UV = ULast ; ULast = VLast  ; VLast = UV;
2420         }
2421   
2422         // (2.2) Iso-u
2423         if (isec == 1 && UEdge(1, ipath).IsNull()) {
2424           if (!Vertex(1,ipath).IsSame(Vertex(1,ipath+1))) {
2425             gp_Pnt P1 = BRep_Tool::Pnt(TopoDS::Vertex(Vertex(1,ipath)));
2426             gp_Pnt P2 = BRep_Tool::Pnt(TopoDS::Vertex(Vertex(1,ipath+1)));
2427             if (P1.Distance(P2) <= myTol3d)
2428               Vertex(1,ipath+1) = Vertex(1,ipath);
2429           }
2430           UEdge(1, ipath) = BuildEdge(S, !exuv, UFirst, 
2431                                       Vertex(1,ipath), 
2432                                       Vertex(1,ipath+1),
2433                                       myTol3d);
2434         }
2435         else UpdateEdge(TopoDS::Edge(UEdge(isec, ipath)), 
2436                         S, !exuv, UFirst);
2437      
2438         if (uclose && (isec==NbLaw)) {
2439           UpdateEdge(TopoDS::Edge(UEdge(1, ipath)), 
2440                      S, !exuv, ULast);
2441           UEdge(isec+1, ipath) = UEdge(1, ipath);
2442         }
2443         else {
2444           if (UEdge(isec+1, ipath).IsNull())
2445             UEdge(isec+1, ipath) = BuildEdge(S, !exuv, ULast, 
2446                                              Vertex(isec+1, ipath), 
2447                                              Vertex(isec+1, ipath+1),
2448                                              myTol3d);
2449           else
2450             UpdateEdge(TopoDS::Edge(UEdge(isec+1, ipath)), S, !exuv, ULast);
2451         }
2452
2453         // (2.3) Iso-v 
2454         if (ipath == 1)
2455         {
2456           TopoDS_Edge aNewFirstEdge = BuildEdge(S, exuv, VFirst, 
2457                                                 Vertex(isec  , 1), 
2458                                                 Vertex(isec+1, 1),
2459                                                 myTol3d);
2460           if (VEdge(isec, ipath).IsNull())
2461             VEdge(isec, ipath) = aNewFirstEdge;
2462           else //rebuild first edge
2463             RebuildTopOrBottomEdge(aNewFirstEdge,
2464                                    TopoDS::Edge(VEdge(isec, ipath)),
2465                                    ReversedEdges);
2466         }
2467         
2468         else UpdateEdge(TopoDS::Edge(VEdge(isec, ipath)), 
2469                         S, exuv, VFirst);
2470         
2471         if (vclose && (ipath == NbPath)) {
2472           UpdateEdge(TopoDS::Edge(VEdge(isec, 1)), 
2473                      S, exuv, VLast);
2474           VEdge(isec, ipath+1) = VEdge(isec, 1);
2475         }
2476         else if (VEdge(isec, ipath+1).IsNull())
2477           VEdge(isec, ipath+1) = BuildEdge(S, exuv, VLast, 
2478                                            Vertex(isec  , ipath+1), 
2479                                            Vertex(isec+1, ipath+1),
2480                                            myTol3d);
2481         else
2482         {
2483           if (ipath != NbPath || vclose)
2484             UpdateEdge(TopoDS::Edge(VEdge(isec, ipath+1)), 
2485                        S, exuv, VLast);
2486           else //ipath == NbPath && !vclose => rebuild last edge
2487           {
2488             TopoDS_Edge aNewLastEdge = BuildEdge(S, exuv, VLast, 
2489                                                  Vertex(isec  , ipath+1), 
2490                                                  Vertex(isec+1, ipath+1),
2491                                                  myTol3d);
2492             RebuildTopOrBottomEdge(aNewLastEdge,
2493                                    TopoDS::Edge(VEdge(isec, ipath+1)),
2494                                    ReversedEdges);
2495           }
2496         }
2497       }
2498     }// End of construction of edges
2499   }
2500
2501   // (3) Construction of Faces
2502   TopoDS_Face face;
2503
2504 #ifdef DRAW
2505   if (Affich) {
2506     for (ipath=1, IPath=IFirst; ipath<=NbPath; ipath++, IPath++) {
2507       for (isec=1; isec <=NbLaw+1; isec++){
2508         sprintf(name,"uedge_%d_%d", isec, IPath);
2509         DBRep::Set(name,UEdge(isec, ipath));
2510       }
2511     }
2512
2513     for (ipath=1, IPath=IFirst; ipath<=NbPath+1; ipath++, IPath++) {
2514       for (isec=1; isec <=NbLaw; isec++){
2515         sprintf(name,"vedge_%d_%d", isec, IPath);
2516         DBRep::Set(name,VEdge(isec, ipath));
2517       }
2518
2519       for (isec=1; isec <=NbLaw+1; isec++){
2520         sprintf(name,"vertex_%d_%d", isec, IPath);
2521         DBRep::Set(name,Vertex(isec, ipath));
2522       }
2523     }
2524   }
2525 #endif 
2526
2527   for (ipath=1, IPath=IFirst; ipath<=NbPath; ipath++, IPath++) {
2528     for (isec=1; isec <=NbLaw; isec++) {
2529       if (Degenerated(isec, ipath)) {
2530         if (UEdge(isec, ipath).IsSame(UEdge(isec+1, ipath))) 
2531           myFaces->SetValue(isec, IPath, UEdge(isec, ipath));
2532         else  
2533           myFaces->SetValue(isec, IPath, VEdge(isec, ipath));
2534       }
2535       else if (myFaces->Value(isec, IPath).IsNull()) {
2536         BuildFace(TabS(isec,ipath), 
2537                   TopoDS::Edge(UEdge(isec, ipath)),
2538                   TopoDS::Edge(VEdge(isec, ipath)),
2539                   TopoDS::Edge(UEdge(isec+1, ipath)),
2540                   TopoDS::Edge(VEdge(isec, ipath+1)),
2541                   myVEdgesModified,
2542                   ExchUV(isec, ipath),
2543                   UReverse(isec, ipath),
2544                   face);
2545         myFaces->SetValue(isec, IPath, face);
2546       }
2547     }
2548   }
2549
2550   // (3.1) Reverse the faces that have been built ealier
2551   for (ipath = 1; ipath <= NbPath; ipath++)
2552     for (isec = 1; isec <= NbLaw; isec++)
2553       if (IsBuilt(isec))
2554         myFaces->ChangeValue(isec, ipath).Reverse();
2555   
2556
2557   // (4) History and Continuity 
2558
2559   if (hasdegen) {
2560   //(4.1) // Degenerated case => Sledgehammer
2561     TopoDS_Compound Comp;
2562     B.MakeCompound(Comp);
2563     for (isec=1; isec <=  NbLaw+1; isec++) 
2564       for (ipath=1, IPath=IFirst; ipath<=  NbPath+1; ipath++, IPath++) {
2565       if (ipath <= NbPath) myUEdges->SetValue(isec, IPath, UEdge(isec, ipath));
2566       if (isec <= NbLaw) myVEdges->SetValue(isec, IPath, VEdge(isec, ipath)); 
2567       if ((ipath <= NbPath) && (isec <= NbLaw) && 
2568           (myFaces->Value(isec, IPath).ShapeType() == TopAbs_FACE))
2569         B.Add(Comp, myFaces->Value(isec, IPath));
2570     }
2571     BRepLib::EncodeRegularity(Comp, myTolAngular);
2572   }
2573   else {
2574     //(4.2) // General case => Tweezers 
2575     Standard_Boolean isG1;
2576     TopoDS_Face FF;
2577     TopoDS_Edge E;
2578  
2579     for (isec=1; isec <=  NbLaw+1; isec++) {
2580       if (isec>1) isG1 = 
2581         (mySec->Continuity(isec-1, myTolAngular) >= GeomAbs_G1);
2582       else isG1 = Standard_False;
2583       for (ipath=1, IPath=IFirst; ipath<=  NbPath; ipath++, IPath++) {
2584         myUEdges->SetValue(isec, IPath, UEdge(isec, ipath));
2585         if (isG1) {
2586           if (isec == NbLaw+1) FF = TopoDS::Face(myFaces->Value(1, IPath));
2587           else  FF = TopoDS::Face(myFaces->Value(isec, IPath));
2588           B.Continuity(TopoDS::Edge(myUEdges->Value(isec, IPath)),
2589                        TopoDS::Face(myFaces->Value(isec-1, IPath)), 
2590                        FF, GeomAbs_G1);
2591         }
2592       }
2593     }
2594
2595     Standard_Integer nbpath = NbPath;
2596     if (vclose) nbpath++; //Another test G1 
2597     for (ipath=1, IPath=IFirst; ipath<=  NbPath+1; ipath++, IPath++) {
2598       if ((ipath > 1) && (ipath <=nbpath)) 
2599         isG1 = (myLoc->IsG1(IPath-1, myTol3d, myTolAngular) >= 0);
2600       else isG1 = Standard_False;
2601       for (isec=1; isec <=  NbLaw; isec++) {
2602         myVEdges->SetValue(isec, IPath, VEdge(isec, ipath));
2603         if (isG1) { 
2604           if (ipath==NbPath+1) FF = TopoDS::Face(myFaces->Value(isec, 1));
2605           else  FF = TopoDS::Face(myFaces->Value(isec, IPath));
2606           E = TopoDS::Edge(myVEdges->Value(isec, IPath));
2607           BRepLib::EncodeRegularity(E, FF,
2608                                     TopoDS::Face(myFaces->Value(isec, IPath-1)),
2609                                     myTolAngular);
2610         }
2611       } 
2612     }
2613   }
2614
2615   // (5) Update Tapes
2616   Standard_Integer j;
2617   if (IFirst == 1 && !Tapes.IsEmpty()) //works only in case of single shell
2618   {
2619     for (isec = 1; isec <= NbLaw; isec++)
2620     {
2621       for (j = 1; j <= NbPath+1; j++)
2622         Tapes(StartEdges(isec))->SetValue(1, j, myVEdges->Value(isec, j));
2623       for (j = 1; j <= NbPath; j++)
2624         Tapes(StartEdges(isec))->SetValue(2, j, myUEdges->Value(isec, j));
2625       for (j = 1; j <= NbPath; j++)
2626         Tapes(StartEdges(isec))->SetValue(3, j, myUEdges->Value(isec+1, j));
2627       for (j = 1; j <= NbPath+1; j++)
2628         Tapes(StartEdges(isec))->SetValue(4, j, Vertex(isec, j));
2629       for (j = 1; j <= NbPath+1; j++)
2630         Tapes(StartEdges(isec))->SetValue(5, j, Vertex(isec+1, j));
2631       for (j = 1; j <= NbPath; j++)
2632         Tapes(StartEdges(isec))->SetValue(6, j, myFaces->Value(isec, j));
2633     }
2634   }
2635   
2636   return Standard_True;
2637 }
2638
2639 //=======================================================================
2640 //function : Build
2641 //purpose  : Construt the result of sweeping
2642 //======================================================================
2643 void BRepFill_Sweep::Build(TopTools_MapOfShape& ReversedEdges,
2644                            BRepFill_DataMapOfShapeHArray2OfShape& Tapes,
2645                            const BRepFill_TransitionStyle Transition,
2646                            const GeomAbs_Shape Continuity,
2647                            const GeomFill_ApproxStyle Approx,
2648                            const Standard_Integer Degmax,
2649                            const Standard_Integer Segmax) 
2650 {
2651   myContinuity  = Continuity;
2652   myApproxStyle = Approx;
2653   myDegmax = Degmax;
2654   mySegmax = Segmax;
2655
2656   CorrectApproxParameters();
2657
2658   // Wire
2659   if (mySec->IsVertex()) isDone = BuildWire(Transition);
2660
2661   else { // Shell   
2662     Standard_Integer NbTrous = myLoc->NbHoles(myTol3d),
2663                      NbPath   = myLoc->NbLaw(),
2664                      NbLaw    = mySec->NbLaw(), ii, jj, NbPart=1;
2665     Standard_Integer ipath, isec;
2666     BRep_Builder B;
2667     myUEdges = new (TopTools_HArray2OfShape) (1, NbLaw+1, 1, NbPath);
2668     myVEdges = new (TopTools_HArray2OfShape) (1, NbLaw, 1, NbPath+1); 
2669     myFaces = new (TopTools_HArray2OfShape) (1, NbLaw, 1, NbPath);
2670     Handle (TopTools_HArray2OfShape) Bounds =  
2671       new (TopTools_HArray2OfShape) (1, NbLaw, 1, 2);
2672  
2673     Handle(TColStd_HArray1OfInteger) Trous;
2674  
2675     if (NbTrous>0) { // How many sub-parts ?
2676       Trous = new (TColStd_HArray1OfInteger) (1, NbTrous);
2677       myLoc->Holes(Trous->ChangeArray1());
2678       NbPart += NbTrous;
2679       if (Trous->Value(NbTrous) == NbPath+1) NbPart--;  
2680     }
2681     if (NbPart == 1)  { // This is done at once
2682       Standard_Real Extend = 0.0;
2683       if (NbTrous==1)  Extend = EvalExtrapol(1, Transition);
2684       isDone = BuildShell(Transition, 
2685                           1, NbPath+1,
2686                           ReversedEdges,
2687                           Tapes,
2688                           Extend, Extend);
2689     }
2690     else { //  This is done piece by piece
2691       Standard_Integer IFirst = 1, ILast;
2692       for (ii=1, isDone=Standard_True; 
2693            ii<=NbPart && isDone; ii++) {
2694         if (ii > NbTrous) ILast =  NbPath+1;
2695         else ILast = Trous->Value(ii);
2696         isDone = BuildShell(Transition, 
2697                             IFirst, ILast,
2698                             ReversedEdges,
2699                             Tapes,
2700                             EvalExtrapol(IFirst, Transition),
2701                             EvalExtrapol(ILast,  Transition));
2702         if (IFirst>1) {
2703           Translate(myVEdges, IFirst, Bounds, 2);
2704           PerformCorner(IFirst, 
2705                         Transition, Bounds);
2706         }
2707         IFirst = ILast;
2708         Translate(myVEdges, IFirst, Bounds, 1);
2709       }
2710     }
2711     // Management of looping ends
2712     if ( (NbTrous>0) && (myLoc->IsClosed()) &&
2713          (Trous->Value(NbTrous) == NbPath+1) ) {
2714       Translate(myVEdges,  NbPath+1, Bounds, 1);
2715       Translate(myVEdges,  1, Bounds, 2);
2716       PerformCorner(1, Transition, Bounds); 
2717     }
2718
2719     // Construction of the shell
2720     TopoDS_Shell shell;
2721     B.MakeShell(shell);
2722     for (ipath=1; ipath<=NbPath; ipath++) 
2723       for (isec=1; isec <=NbLaw; isec++) {
2724       const TopoDS_Shape& face = myFaces->Value(isec, ipath);
2725         if (!face.IsNull() && 
2726             (face.ShapeType() == TopAbs_FACE) ) B.Add(shell, face);
2727       }
2728
2729     TopTools_ListIteratorOfListOfShape It(myAuxShape);
2730     for (; It.More(); It.Next()) {
2731        const TopoDS_Shape& face = It.Value();
2732        if (!face.IsNull() && 
2733             (face.ShapeType() == TopAbs_FACE) ) B.Add(shell, face);
2734     }
2735     //Set common Uedges to faces
2736     BRepTools_Substitution aSubstitute;
2737     /*
2738     for (ii = 1; ii <= NbLaw; ii++)
2739       for (jj = 1; jj <= NbPath; jj++)
2740         {
2741           SetCommonEdgeInFace(aSubstitute,
2742                               myFaces->Value(ii, jj),
2743                               myUEdges->Value(ii, jj));
2744           SetCommonEdgeInFace(aSubstitute,
2745                               myFaces->Value(ii, jj),
2746                               myUEdges->Value(ii+1, jj));
2747         }
2748     if (mySec->IsUClosed())
2749       for (jj = 1; jj <= NbPath; jj++)
2750         SetCommonEdgeInFace(aSubstitute,
2751                             myFaces->Value( 1, jj ),
2752                             myUEdges->Value( NbLaw+1, jj));
2753     */
2754     TopTools_DataMapIteratorOfDataMapOfShapeShape mapit( myVEdgesModified );
2755     for (; mapit.More(); mapit.Next())
2756       {
2757         const TopoDS_Edge& OldEdge = TopoDS::Edge(mapit.Key());
2758         const TopoDS_Edge& NewEdge = TopoDS::Edge(mapit.Value());
2759         Substitute( aSubstitute, OldEdge, NewEdge );
2760       }
2761     aSubstitute.Build( shell );
2762     if (aSubstitute.IsCopied( shell )) {
2763       const TopTools_ListOfShape& listSh = aSubstitute.Copy( shell );
2764       shell = TopoDS::Shell( listSh.First() );
2765     }
2766
2767     for (ii = myFaces->LowerRow(); ii <= myFaces->UpperRow(); ii++) {
2768       for (jj = myFaces->LowerCol(); jj <= myFaces->UpperCol(); jj++) {
2769         const TopoDS_Shape& aLocalShape = myFaces->Value(ii, jj);
2770
2771         if(!aLocalShape.IsNull() && aSubstitute.IsCopied(aLocalShape)) {
2772           const TopTools_ListOfShape& aList = aSubstitute.Copy(aLocalShape);
2773
2774           if(!aList.IsEmpty())
2775             myFaces->ChangeValue(ii, jj) = aList.First();
2776         }
2777       }
2778     }
2779
2780     for (ii = myVEdges->LowerRow(); ii <= myVEdges->UpperRow(); ii++) {
2781       for (jj = myVEdges->LowerCol(); jj <= myVEdges->UpperCol(); jj++) {
2782         const TopoDS_Shape& aLocalShape = myVEdges->Value(ii, jj);
2783
2784         if(!aLocalShape.IsNull() && aSubstitute.IsCopied(aLocalShape)) {
2785           const TopTools_ListOfShape& aList = aSubstitute.Copy(aLocalShape);
2786
2787           if(!aList.IsEmpty())
2788             myVEdges->ChangeValue(ii, jj) = aList.First();
2789         }
2790       }
2791     }
2792
2793     for (ii = myUEdges->LowerRow(); ii <= myUEdges->UpperRow(); ii++) {
2794       for (jj = myUEdges->LowerCol(); jj <= myUEdges->UpperCol(); jj++) {
2795         const TopoDS_Shape& aLocalShape = myUEdges->Value(ii, jj);
2796
2797         if(!aLocalShape.IsNull() && aSubstitute.IsCopied(aLocalShape)) {
2798           const TopTools_ListOfShape& aList = aSubstitute.Copy(aLocalShape);
2799
2800           if(!aList.IsEmpty())
2801             myUEdges->ChangeValue(ii, jj) = aList.First();
2802         }
2803       }
2804     }
2805
2806     // Is it Closed ?
2807     if (myLoc->IsClosed() && mySec->IsUClosed()) {
2808       //Check
2809       Standard_Boolean closed = Standard_True;
2810       Standard_Integer iedge;
2811       TopTools_IndexedDataMapOfShapeListOfShape EFmap;
2812       TopExp::MapShapesAndAncestors(shell, TopAbs_EDGE, 
2813                                     TopAbs_FACE, EFmap);
2814       
2815       for (iedge = 1; iedge <=EFmap.Extent() && closed; iedge++) {
2816         const TopoDS_Edge& theEdge = TopoDS::Edge(EFmap.FindKey(iedge));
2817         if (BRep_Tool::Degenerated(theEdge)) continue;
2818         closed = (  EFmap(iedge).Extent() > 1);
2819       }
2820       shell.Closed(closed);
2821     }
2822     myShape = shell;
2823   }
2824 }
2825
2826
2827 //=======================================================================
2828 //function : IsDone
2829 //purpose  : 
2830 //=======================================================================
2831  Standard_Boolean BRepFill_Sweep::IsDone() const
2832 {
2833   return isDone;
2834 }
2835
2836 //=======================================================================
2837 //function : Shape
2838 //purpose  : 
2839 //=======================================================================
2840  TopoDS_Shape BRepFill_Sweep::Shape() const
2841 {
2842   return myShape;
2843 }
2844
2845 //=======================================================================
2846 //function : ErrorOnSurface
2847 //purpose  : 
2848 //=======================================================================
2849  Standard_Real BRepFill_Sweep::ErrorOnSurface() const
2850 {
2851   return Error;
2852 }
2853
2854 //=======================================================================
2855 //function : SubShape
2856 //purpose  : Faces obtained by sweeping
2857 //=======================================================================
2858  Handle(TopTools_HArray2OfShape) BRepFill_Sweep::SubShape() const
2859 {
2860   return myFaces;
2861 }
2862
2863 //=======================================================================
2864 //function : InterFaces
2865 //purpose  : Edges obtained by sweeping
2866 //=======================================================================
2867  Handle(TopTools_HArray2OfShape) BRepFill_Sweep::InterFaces() const
2868 {
2869   return myUEdges;
2870 }
2871
2872 //=======================================================================
2873 //function : Sections
2874 //purpose  : Edges or Face (or compound of 2) Transition between 2 sweepings
2875 //=======================================================================
2876  Handle(TopTools_HArray2OfShape) BRepFill_Sweep::Sections() const
2877 {
2878   return myVEdges;
2879 }
2880
2881 //=======================================================================
2882 //function : PerformCorner
2883 //purpose  : Trim and/or loop a corner
2884 //======================================================================
2885  void  BRepFill_Sweep::PerformCorner(const Standard_Integer Index,
2886                                      const BRepFill_TransitionStyle Transition,
2887                                      const Handle(TopTools_HArray2OfShape)& Bounds)
2888 {
2889
2890   if (Transition == BRepFill_Modified) return; // Do nothing.
2891
2892   BRepFill_TransitionStyle TheTransition = Transition;
2893   Standard_Boolean isTangent=Standard_False;
2894   Standard_Real F, L;
2895   Standard_Integer I1, I2, ii; //, jj;
2896   gp_Pnt P1,P2;
2897   gp_Vec T1, T2, Tang, Sortant;
2898 //  gp_Mat M;
2899   //Handle(TopTools_HArray1OfShape) TheShape = 
2900     //new TopTools_HArray1OfShape( 1, mySec->NbLaw() );
2901 //  TopTools_ListIteratorOfListOfShape Iterator;
2902
2903   if (Index > 1) { 
2904     I1 = Index-1;
2905     I2 = Index;
2906   }
2907   else {
2908     I1 = myLoc->NbLaw();
2909     I2 = 1;
2910   }
2911
2912   // Construct an axis supported by the bissectrice
2913   myLoc->Law(I1)->GetDomain(F, L);
2914   myLoc->Law(I1)->GetCurve()->D1(L, P1, T1);
2915   T1.Normalize();
2916
2917   myLoc->Law(I2)->GetDomain(F, L);  
2918   myLoc->Law(I2)->GetCurve()->D1(F, P2, T2);
2919   T2.Normalize();
2920
2921   if (T1.Angle(T2) <  myAngMin) {
2922     isTangent = Standard_True;
2923     gp_Vec t1, t2, V;
2924     gp_Mat M;
2925     myLoc->Law(I1)->GetDomain(F, L);
2926     myLoc->Law(I1)->D0(L, M, V);
2927     t1 = M.Column(3);
2928     myLoc->Law(I2)->GetDomain(F, L);
2929     myLoc->Law(I2)->D0(L, M, V);
2930     t2 = M.Column(3);
2931
2932     if (t1.Angle(t2) < myAngMin) {
2933 #if DEB
2934       cout << "BRepFill_Sweep::PerformCorner : This is not a corner !" << endl;
2935 #endif
2936       return;
2937     }
2938     Sortant = t2 - t1;
2939   }
2940
2941   if ((TheTransition == BRepFill_Right) 
2942       && (T1.Angle(T2) >  myAngMax) ) {
2943     TheTransition =  BRepFill_Round;
2944   }
2945
2946   Tang = T1 + T2; //Average direction
2947   gp_Dir NormalOfBisPlane = Tang;
2948
2949   if (isTangent) {
2950     Sortant -= Tang.Dot(Tang)*Tang;
2951   }
2952   else {
2953     Sortant = T2-T1; //Direction input 
2954     Sortant *= -1; //   "   "   output
2955     Tang -= (Tang.Dot(T2))*T2;
2956   }
2957
2958   P1.BaryCenter(0.5, P2, 0.5);
2959   gp_Dir N(Sortant);
2960   gp_Dir Dx(Tang);
2961     
2962   gp_Ax2 Axe (P1, N, Dx);
2963   gp_Ax2 AxeOfBisPlane( P1, NormalOfBisPlane );
2964
2965   // Construct 2 intersecting Shells
2966   Handle (TopTools_HArray2OfShape) UEdges =
2967     new TopTools_HArray2OfShape( 1, mySec->NbLaw()+1, 1, myLoc->NbLaw() );
2968   UEdges->ChangeArray2() = myUEdges->Array2();
2969
2970 // modified by NIZHNY-MKK  Wed Oct 29 18:31:47 2003.BEGIN
2971   Handle (TopTools_HArray2OfShape) aFaces =
2972     new TopTools_HArray2OfShape(myFaces->LowerRow(), myFaces->UpperRow(), 1, 2);
2973   Translate(myFaces, I1, aFaces, 1);
2974   Translate(myFaces, I2, aFaces, 2);
2975
2976   Handle (TopTools_HArray2OfShape) aUEdges =
2977     new TopTools_HArray2OfShape(myUEdges->LowerRow(), myUEdges->UpperRow(), 1, 2);
2978   Translate(myUEdges, I1, aUEdges, 1);
2979   Translate(myUEdges, I2, aUEdges, 2);
2980
2981   gp_Vec aNormal = T2 + T1;
2982   TopoDS_Face aPlaneF;
2983
2984   if(aNormal.Magnitude() > gp::Resolution()) {
2985     gp_Pln pl(P1, gp_Dir(aNormal));
2986     BRepLib_MakeFace aFMaker(pl);
2987
2988     if(aFMaker.Error() == BRepLib_FaceDone) {
2989       aPlaneF = aFMaker.Face();
2990       BRep_Builder aBB;
2991       aBB.UpdateFace(aPlaneF, Precision::Confusion() * 10.);
2992     }
2993   }
2994
2995   BRepFill_TrimShellCorner aTrim(aFaces, AxeOfBisPlane, aPlaneF);
2996   aTrim.AddBounds(Bounds);
2997   aTrim.AddUEdges(aUEdges);
2998   aTrim.Perform();
2999
3000   if (aTrim.IsDone()) {
3001     TopTools_ListOfShape listmodif;
3002     Standard_Integer iit = 0;
3003
3004     for(iit = 0; iit < 2; iit++) {
3005       Standard_Integer II = (iit == 0) ? I1 : I2;
3006
3007       for (ii = 1; ii <= mySec->NbLaw(); ii++) {
3008         aTrim.Modified(myFaces->Value(ii, II), listmodif);
3009
3010         if(!listmodif.IsEmpty()) {
3011           myFaces->SetValue(ii, II, listmodif.First());
3012         }
3013       }
3014
3015       for (ii = myUEdges->LowerRow(); ii <= myUEdges->UpperRow(); ii++) {
3016         aTrim.Modified(myUEdges->Value(ii, II), listmodif);
3017
3018         if(!listmodif.IsEmpty()) {
3019           myUEdges->SetValue(ii, II, listmodif.First());
3020         }
3021       }
3022     }
3023   }
3024   else if ((TheTransition == BRepFill_Right) ||
3025            aTrim.HasSection() ) { 
3026 #if DEB
3027     cout << "Fail of TrimCorner" << endl;
3028 #endif
3029     return; // Nothing is touched
3030   }
3031
3032   if (mySec->IsUClosed())
3033     {
3034       myUEdges->SetValue( 1, I1, myUEdges->Value(mySec->NbLaw()+1, I1) );
3035       myUEdges->SetValue( 1, I2, myUEdges->Value(mySec->NbLaw()+1, I2) );
3036     }
3037
3038   if (TheTransition == BRepFill_Round) {
3039   // Filling
3040     TopTools_ListOfShape list1, list2;
3041     TopoDS_Edge Bord1, Bord2, BordFirst;
3042     BordFirst.Nullify();
3043     Bord1.Nullify();
3044     Bord2.Nullify();
3045     Standard_Boolean HasFilling = Standard_False;
3046     TopoDS_Face FF;
3047     for (ii=1; ii<=mySec->NbLaw(); ii++) {
3048       KeepEdge(myFaces->Value(ii, I1), Bounds->Value(ii, 1), list1);
3049       KeepEdge(myFaces->Value(ii, I2), Bounds->Value(ii, 2), list2);
3050       if (list1.Extent() == list2.Extent()) {
3051         TopTools_ListIteratorOfListOfShape It1(list1);
3052         TopTools_ListIteratorOfListOfShape It2(list2);
3053         Standard_Boolean B;
3054         for (; It1.More(); It1.Next(), It2.Next()) {
3055           if (HasFilling) { // Transversal choice of constraints
3056             TopoDS_Vertex VF, VL, VC;
3057             TopoDS_Edge E = TopoDS::Edge(It1.Value());
3058             TopoDS_Edge E1, E2;
3059             E1.Nullify();
3060             E2.Nullify();
3061             TopExp::Vertices(E, VF, VL);
3062             if (!Bord1.IsNull() && 
3063                 TopExp::CommonVertex(E, Bord1, VC)) {
3064               if (VC.IsSame(VF)) E1 = Bord1;
3065               else               E2 = Bord1;
3066             }
3067             if (!Bord2.IsNull() && 
3068                 TopExp::CommonVertex(E, Bord2, VC)) {
3069               if (VC.IsSame(VF)) E1 = Bord2;
3070               else               E2 = Bord2;
3071             }
3072             if (!BordFirst.IsNull() && 
3073                 TopExp::CommonVertex(E, BordFirst, VC)) {
3074               if (VC.IsSame(VF)) E1 = BordFirst;
3075               else               E2 = BordFirst;
3076             }
3077             Bord1 = E1;
3078             Bord2 = E2;
3079           }
3080           
3081           // Filling
3082           B = Filling(It1.Value(), myFaces->Value(ii, I1),
3083                       It2.Value(), myFaces->Value(ii, I2),
3084                       myVEdgesModified, myTol3d, Axe, T1, Bord1, Bord2, FF);
3085           
3086           if (B) {
3087             myAuxShape.Append(FF);
3088             myVEdges->ChangeValue(ii, I2) = FF;
3089             HasFilling = Standard_True;
3090           }
3091           if (ii==1) BordFirst = Bord1;
3092         }
3093       }
3094 #if DEB
3095       else cout << "PerformCorner : Unsymmetry of free border" << endl;
3096 #endif
3097     }
3098   }
3099
3100 /*  
3101 #if DRAW
3102   if (Affich) {
3103     Standard_Integer jj;
3104     char name[100];
3105     DBRep::Set("TrimmedShell", TheShape);
3106     for (jj=1; jj <=myFaces->ColLength(); jj++){
3107       sprintf(name,"Tfaces_%d_%d", jj, I1);
3108       DBRep::Set(name, myFaces->Value(jj, I1));
3109       sprintf(name,"Tfaces_%d_%d", jj, I2);
3110       DBRep::Set(name, myFaces->Value(jj, I2));
3111     }
3112   }
3113 #endif
3114 */
3115 }
3116
3117 //=======================================================================
3118 //function : EvalExtrapol
3119 //purpose  : 
3120 //======================================================================
3121 Standard_Real BRepFill_Sweep::
3122   EvalExtrapol(const Standard_Integer Index,
3123                const BRepFill_TransitionStyle Transition) const
3124 {
3125   Standard_Real Extrap = 0.0;
3126   if (Transition == BRepFill_Right) {
3127     Standard_Integer I1, I2;
3128     if ((Index == 1) || (Index ==myLoc->NbLaw()+1) ) {
3129       if (!myLoc->IsClosed() || !mySec->IsVClosed()) return Extrap;
3130       I1 = myLoc->NbLaw();
3131       I2 = 1;
3132     }
3133     else {
3134       I1 = Index-1;
3135       I2 = Index;
3136     }
3137
3138     gp_Vec V1, V2, T1, T2;
3139     gp_Mat M1, M2;
3140     Standard_Real Xmin, Ymin, Zmin, Xmax, Ymax, Zmax, R, f, l;
3141
3142     myLoc->Law(I1)->GetDomain(f, l);
3143     myLoc->Law(I1)->D0(l, M1, V1);
3144     T1.SetXYZ(M1.Column(3));
3145     myLoc->Law(I2)->GetDomain(f, l);
3146     myLoc->Law(I2)->D0(f, M2, V2);
3147     T2.SetXYZ(M2.Column(3));
3148      
3149     Standard_Real alpha = T1.Angle(T2);
3150     if ((alpha >  myAngMax) || (alpha <  myAngMin)) {
3151       //Angle too great => No "straight" connection
3152       //Angle too small => No connection
3153       return Extrap; // = 0.0
3154     }   
3155
3156     Handle(GeomFill_SectionLaw) Sec;
3157     Sec = mySec->ConcatenedLaw();
3158
3159     //Calculating parameter U
3160     Standard_Real U, Length, SecFirst, SecLen, Lf, Ll;
3161     myLoc->CurvilinearBounds( myLoc->NbLaw(), Lf, Length );
3162     mySec->Law(1)->GetDomain( SecFirst, SecLen );
3163     SecLen -= SecFirst;
3164     myLoc->CurvilinearBounds( I1, Lf, Ll );
3165     U = SecFirst + (Ll/Length)*SecLen;
3166
3167     Bnd_Box box;
3168     //Box(Sec, 0., box);
3169     Box(Sec, U, box);
3170     box.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
3171     
3172     R =  Max(Max(Abs(Xmin), Abs(Xmax)),Max(Abs(Ymin), Abs(Ymax)));
3173     //R *= 1.1;
3174     // modified by NIZHNY-MKK  Fri Oct 31 18:57:51 2003
3175     //     Standard_Real coef = 1.2;
3176     Standard_Real coef = 2.;
3177     R *= coef;
3178     Extrap = Max(Abs(Zmin), Abs(Zmax)) + 100*myTol3d; 
3179     Extrap += R*Tan(alpha/2);
3180   }
3181   return Extrap;
3182 }
3183
3184 //=======================================================================
3185 //function : MergeVertex
3186 //purpose  : Make V2 = V1 if V2 is too close to V1
3187 //======================================================================
3188 Standard_Boolean BRepFill_Sweep::MergeVertex(const TopoDS_Shape& V1,
3189                                                    TopoDS_Shape& V2) const
3190 {
3191 // Class BRep_Tool without fields and without Constructor :
3192 //  BRep_Tool BT;
3193   const TopoDS_Vertex& v1 = TopoDS::Vertex(V1);
3194   const TopoDS_Vertex& v2 = TopoDS::Vertex(V2);  
3195   Standard_Real tol;
3196 //  tol = Max(BT.Tolerance(v1), BT.Tolerance(v2));
3197   tol = Max(BRep_Tool::Tolerance(v1), BRep_Tool::Tolerance(v2));
3198   if (tol < myTol3d) tol = myTol3d;
3199 //  if (BT.Pnt(v1).Distance(BT.Pnt(v2)) <= tol ){
3200   if (BRep_Tool::Pnt(v1).Distance(BRep_Tool::Pnt(v2)) <= tol ){
3201     V2 = V1;
3202     return Standard_True;
3203   }                              
3204   return Standard_False;
3205 }
3206
3207         
3208 //=======================================================================
3209 //function : UpdateVertex
3210 //purpose  : Update the Tolerance of Vertices depending on Laws.
3211 //======================================================================
3212 void BRepFill_Sweep::UpdateVertex(const Standard_Integer ipath,
3213                                   const Standard_Integer isec,
3214                                   const Standard_Real ErrApp,
3215                                   const Standard_Real Param,
3216                                   TopoDS_Shape& V) const
3217 {
3218   TopoDS_Vertex vv, TheV;
3219   TheV = TopoDS::Vertex(V);
3220   myLoc->PerformVertex(ipath, 
3221                        mySec->Vertex(isec, Param), 
3222                        ErrApp+mySec->VertexTol(isec-1, Param),
3223                        vv);
3224 // Class BRep_Tool without fields and without Constructor :
3225 //  BRep_Tool BT;
3226   gp_Pnt P1, P2;
3227 //  P1 = BT.Pnt(vv);
3228   P1 = BRep_Tool::Pnt(vv);
3229 //  P2 = BT.Pnt(TheV);
3230   P2 = BRep_Tool::Pnt(TheV);
3231
3232 //  Standard_Real Tol = BT.Tolerance(vv);
3233   Standard_Real Tol = BRep_Tool::Tolerance(vv);
3234   Tol += P1.Distance(P2);
3235   
3236 //  if (Tol >  BT.Tolerance(TheV)) {
3237   if (Tol >  BRep_Tool::Tolerance(TheV)) {
3238     BRep_Builder B;
3239     B.UpdateVertex(TheV, Tol);
3240   }
3241 }
3242
3243 //=======================================================================
3244 //function : RebuildTopOrBottomEdge
3245 //purpose  : Rebuild v-iso edge of top or bottom section
3246 //           inserting new 3d and 2d curves taken from swept surfaces
3247 //======================================================================
3248 void BRepFill_Sweep::RebuildTopOrBottomEdge(const TopoDS_Edge& aNewEdge,
3249                                             TopoDS_Edge& anEdge,
3250                                             TopTools_MapOfShape& ReversedEdges) const
3251 {
3252   Standard_Real fpar, lpar;
3253   Handle(Geom_Curve) aNewCurve = BRep_Tool::Curve(aNewEdge, fpar, lpar);
3254   TopLoc_Location Identity;
3255   
3256   Standard_Boolean ToReverse = Standard_False;
3257   Standard_Boolean IsDegen = BRep_Tool::Degenerated(aNewEdge);
3258   if (IsDegen)
3259     BRep_Tool::Range(aNewEdge, fpar, lpar);
3260   else
3261   {
3262     TopoDS_Vertex V1, V2, NewV1, NewV2;
3263     TopExp::Vertices(anEdge, V1, V2);
3264     if (!V1.IsSame(V2))
3265     {
3266       TopExp::Vertices(aNewEdge, NewV1, NewV2);
3267       V1.Location(Identity);
3268       if (!V1.IsSame(NewV1))
3269       {
3270         if (V1.IsSame(NewV2))
3271           ToReverse = Standard_True;
3272         else
3273         {
3274           gp_Pnt Pnt1 = BRep_Tool::Pnt(V1);
3275           gp_Pnt NewPnt1 = BRep_Tool::Pnt(NewV1);
3276           Standard_Real TolSum = BRep_Tool::Tolerance(V1) + BRep_Tool::Tolerance(NewV1);
3277           if (!Pnt1.IsEqual(NewPnt1, TolSum))
3278             ToReverse = Standard_True;
3279         }
3280       }
3281     }
3282     else
3283     {
3284       Standard_Real OldFirst, OldLast;
3285       Handle(Geom_Curve) OldCurve = BRep_Tool::Curve(anEdge, OldFirst, OldLast);
3286       gp_Vec OldD1, NewD1;
3287       gp_Pnt MidPnt;
3288       OldCurve->D1(0.5*(OldFirst + OldLast), MidPnt, OldD1);
3289       aNewCurve->D1(0.5*(fpar + lpar), MidPnt, NewD1);
3290       if (OldD1 * NewD1 < 0.)
3291         ToReverse = Standard_True;
3292     }
3293   }
3294   
3295   anEdge.Location(Identity);
3296   const Handle(BRep_TEdge)& TEdge = *((Handle(BRep_TEdge)*) &anEdge.TShape());
3297   TEdge->Tolerance(BRep_Tool::Tolerance(aNewEdge));
3298   BRep_Builder BB;
3299   BB.Range(anEdge, fpar, lpar);
3300   BB.UpdateEdge(anEdge, aNewCurve, Precision::Confusion());
3301   const Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*) &aNewEdge.TShape());
3302   const BRep_ListOfCurveRepresentation& lcr = TE->Curves();
3303   BRep_ListIteratorOfListOfCurveRepresentation itrep(lcr);
3304   for (; itrep.More(); itrep.Next())
3305   {
3306     const Handle(BRep_CurveRepresentation)& CurveRep = itrep.Value();
3307     if (CurveRep->IsCurveOnSurface())
3308     {
3309       const Handle(BRep_GCurve)& GC = *((Handle(BRep_GCurve)*)&CurveRep);
3310       Handle(Geom2d_Curve) aPCurve = GC->PCurve();
3311       Handle(Geom_Surface) aSurf = GC->Surface();
3312       TopLoc_Location aLoc = aNewEdge.Location() * GC->Location();
3313       BB.UpdateEdge(anEdge, aPCurve, aSurf, aLoc, Precision::Confusion());
3314     }
3315   }
3316   
3317   anEdge.Free(Standard_True);
3318   TopoDS_Vertex V1, V2;
3319   TopExp::Vertices(anEdge, V1, V2);
3320
3321   TopoDS_Shape anEdgeFORWARD = anEdge.Oriented(TopAbs_FORWARD);
3322   
3323   BB.Remove(anEdgeFORWARD, V1);
3324   BB.Remove(anEdgeFORWARD, V2);
3325  
3326   V1.Location(Identity);
3327   V2.Location(Identity);
3328   if (ToReverse)
3329   {
3330     V2.Orientation(TopAbs_FORWARD);
3331     V1.Orientation(TopAbs_REVERSED);
3332   }
3333   BB.Add(anEdgeFORWARD, V1);
3334   BB.Add(anEdgeFORWARD, V2);
3335   
3336   if (ToReverse)
3337   {
3338     anEdge.Reverse();
3339     ReversedEdges.Add(anEdge);
3340   }
3341
3342   BB.Degenerated(anEdge, IsDegen);
3343 }