0023305: One index value verified twice in BRepFill_TrimShellCorner.cxx
[occt.git] / src / BRepFill / BRepFill_Generator.cxx
1 // Created on: 1994-03-07
2 // Created by: Bruno DUMORTIER
3 // Copyright (c) 1994-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21
22 #include <BRepFill_Generator.ixx>
23
24 #include <TopoDS_Face.hxx>
25 #include <TopoDS_Wire.hxx>
26 #include <TopoDS_Edge.hxx>
27 #include <TopoDS_Vertex.hxx>
28 #include <BRep_Builder.hxx>
29 #include <TopLoc_Location.hxx>
30 #include <TopExp_Explorer.hxx>
31 #include <gp_Pnt2d.hxx>
32 #include <gp_Dir2d.hxx>
33 #include <gp_Ax1.hxx>
34 #include <gp_Ax3.hxx>
35 #include <gp_Circ.hxx>
36 #include <gp_Lin.hxx>
37 #include <GeomAbs_Shape.hxx>
38 #include <GeomAdaptor_Curve.hxx>
39 #include <Geom_Circle.hxx>
40 #include <Geom_Line.hxx>
41 #include <Geom_Curve.hxx>
42 #include <Geom_BezierCurve.hxx>
43 #include <Geom_TrimmedCurve.hxx>
44 #include <Geom_Surface.hxx>
45 #include <Geom_Plane.hxx>
46 #include <Geom_CylindricalSurface.hxx>
47 #include <Geom_ConicalSurface.hxx>
48 #include <Geom_RectangularTrimmedSurface.hxx>
49 #include <Geom2d_Line.hxx>
50 #include <Geom2d_BezierCurve.hxx>
51 #include <GeomFill_Generator.hxx>
52
53 #include <TopTools_DataMapOfShapeShape.hxx>
54 #include <GeomFill.hxx>
55 #include <BRep_Tool.hxx>
56 #include <TopoDS.hxx>
57 #include <TopExp.hxx>
58 #include <Precision.hxx>
59 #include <BRepLib.hxx>
60
61 #include <TColgp_Array1OfPnt.hxx> 
62 #include <TColgp_Array1OfPnt2d.hxx> 
63 #include <Geom_BSplineCurve.hxx> 
64 #include <gp_Vec.hxx>
65 #include <GeomConvert.hxx>
66
67 #include <BRepTools_WireExplorer.hxx>
68 #include <BRepTools.hxx>
69
70
71
72 //=======================================================================
73 //function : DetectKPart
74 //purpose  : 
75 //=======================================================================
76
77 Standard_Integer DetectKPart(const TopoDS_Edge& Edge1,
78                              const TopoDS_Edge& Edge2)
79 {
80   // initializations
81   Standard_Integer IType = 0;
82
83   // characteristics of the first edge
84   Standard_Real first1, last1, first2, last2, ff, ll;
85   TopLoc_Location loc;
86   TopoDS_Vertex V1, V2;
87   Handle(Geom_Curve) curv1, curv;
88   GeomAdaptor_Curve AdC1;
89   Standard_Boolean degen1 = BRep_Tool::Degenerated(Edge1);
90
91   // find the particular case
92   gp_Pnt pos1, pos;
93   Standard_Real  dist;
94 #ifndef DEB
95   Standard_Real dist1 =0.;
96 #else
97   Standard_Real dist1;
98 #endif
99   gp_Ax1 axe1, axe;
100
101   if (degen1) {
102     IType = -2;
103     TopExp::Vertices(Edge1,V1,V2);
104     pos1 = BRep_Tool::Pnt(V1);
105   }
106   else {
107     curv1 = BRep_Tool::Curve(Edge1, loc, first1, last1);
108     curv1 = 
109       Handle(Geom_Curve)::DownCast(curv1->Transformed(loc.Transformation()));
110     ff = first1;
111     ll = last1;
112     if (Edge1.Orientation() == TopAbs_REVERSED) {
113       curv1->Reverse();
114       first1 = curv1->ReversedParameter(ll);
115       last1 = curv1->ReversedParameter(ff);
116     }
117     AdC1.Load(curv1);
118     if (AdC1.GetType() == GeomAbs_Circle) {
119       // first circular section 
120       IType = 1;
121       pos1 = AdC1.Circle().Location();
122       dist1 = AdC1.Circle().Radius();
123       axe1 = AdC1.Circle().Axis();
124     }
125     else if (AdC1.GetType() == GeomAbs_Line) {
126       // first straight line section 
127       IType = 4;
128       pos1 = AdC1.Line().Location();
129       dist1 = AdC1.Value(first1).Distance(AdC1.Value(last1));
130       gp_Vec vec(AdC1.Value(first1),AdC1.Value(last1));
131       gp_Dir dir(vec);
132       axe1 = gp_Ax1(AdC1.Value(first1),dir);
133     }
134     else {
135       // first section of any type
136       IType = 0;
137     }
138   }
139
140   if (IType!=0) {
141
142     Standard_Boolean degen2 = BRep_Tool::Degenerated(Edge2);
143     if (degen2) {
144       TopExp::Vertices(Edge2,V1,V2);
145       pos = BRep_Tool::Pnt(V1);
146       if (IType==1) {
147         // the only particular case with degenerated edge at end : the cone
148         if (pos.IsEqual(pos1,Precision::Confusion())) {
149           // the top is mixed with the center of the circle
150           IType = 0;
151         }
152         else {
153           gp_Vec vec(pos1,pos);
154           gp_Dir dir(vec);
155           axe = gp_Ax1(pos1,dir);
156           if (axe.IsParallel(axe1,Precision::Angular())) {
157             // the top is on the axis of the circle
158             IType = 2;
159           }
160           else {
161             // incorrect top --> no particular case
162             IType = 0;
163           }
164         }
165       }
166       else if (IType != 4) { //not a plane
167         // no particular case
168         IType = 0;
169       }
170     }
171     else {
172       curv = BRep_Tool::Curve(Edge2, loc, first2, last2);
173       curv = 
174         Handle(Geom_Curve)::DownCast(curv->Transformed(loc.Transformation()));
175       ff = first2;
176       ll = last2;
177       if (Edge2.Orientation() == TopAbs_REVERSED) {
178         curv->Reverse();
179         first2 = curv->ReversedParameter(ll);
180         last2 = curv->ReversedParameter(ff);
181       }
182       GeomAdaptor_Curve AdC(curv);
183     
184       if (IType>0 && IType<4) {
185         if (AdC.GetType() != GeomAbs_Circle) {
186           // section not circular --> no particular case
187           IType = 0;
188         }
189         else {
190           if (AdC.Circle().Axis()
191               .IsCoaxial(axe1,Precision::Angular(),Precision::Confusion())) {
192             // same axis
193             if (Abs(AdC.Circle().Radius()-dist1)< Precision::Confusion()) {
194               // possibility of cylinder or a piece of cylinder
195               Standard_Real h1 = Abs(last1-first1), h2 = Abs(last2-first2);
196               Standard_Boolean Same, 
197                SameParametricLength = ( Abs(h1-h2) < Precision::PConfusion() );
198               Standard_Real m1=(first1+last1)/2., m2=(first2+last2)/2.;
199               gp_Pnt P1,P2;
200               gp_Vec DU;
201               AdC1.D1(m1,P1,DU);
202               AdC.D0(m2,P2);
203               Same = SameParametricLength 
204                 && ( gp_Vec(P1,P2).IsNormal(DU,Precision::Angular()) ) ;
205               if (Same) {
206                 // cylinder or piece of cylinder
207                 IType = 1;
208               }
209               else {
210                 // the interval of definition is not correct
211                 IType = 0;
212               }
213             }
214             else {
215               // possibility of cone truncation
216               Standard_Real h1 = Abs(last1-first1), h2 = Abs(last2-first2);
217               Standard_Boolean Same, 
218                SameParametricLength = ( Abs(h1-h2) < Precision::PConfusion() );
219               Standard_Real m1=(first1+last1)/2., m2=(first2+last2)/2.;
220               gp_Pnt P1,P2;
221               gp_Vec DU;
222               AdC1.D1(m1,P1,DU);
223               AdC.D0(m2,P2);
224               Same = SameParametricLength 
225                 && ( gp_Vec(P1,P2).IsNormal(DU,Precision::Angular()) ) ;
226               if (Same) {
227                 // truncation of cone
228                 IType = 2;
229               }
230               else {
231                 // the interval of definition is not correct
232                 IType = 0;
233               }
234             }
235             if (AdC.Circle().Location().IsEqual(pos1,Precision::Confusion())) {
236               // the centers are mixed
237               IType = 0;
238             }
239           }
240           else {
241             // different axis
242             if (AdC.Circle().Radius()==dist1) {
243               // torus ?
244               IType = 3;
245             }
246             else {
247               // different radius --> no particular case
248               IType = 0;
249             }
250           }
251         }
252       }
253       else if (IType>=4) {
254         if (AdC.GetType() != GeomAbs_Line) {
255           // not a straight line section --> no particular case
256           IType = 0;
257         }
258         else {
259           pos = AdC.Line().Location();
260           dist = AdC.Value(first2).Distance(AdC.Value(last2));
261           gp_Vec vec(AdC.Value(first2),AdC.Value(last2));
262           gp_Dir dir(vec);
263           axe = gp_Ax1(AdC.Value(first2),dir);
264           if (axe.IsParallel(axe1,Precision::Angular())) {
265             // parallel straight line
266             if (Abs(dist-dist1)<Precision::Confusion()) {
267               gp_Dir dir(gp_Vec(AdC1.Value(first1),AdC.Value(first2)));
268               if (dir.IsNormal(gp_Dir(vec),Precision::Angular())) {
269                 // plane
270                 IType = 4;
271               }
272               else {
273                 // extrusion ?
274                 IType = 5;
275               }
276             }
277             else {
278               // different length --> no particular case
279               IType = 0;
280             }
281           }
282           else {
283             // not parallel straight line --> no particular case
284             IType = 0;
285           }
286         }
287       }
288       else if (IType==-2) {
289         if (AdC.GetType() == GeomAbs_Line)
290           IType = 4; //plane
291         else if (AdC.GetType() == GeomAbs_Circle)
292           {
293             // the only particular case with degenerated edge at the beginning the cone
294             pos = AdC.Circle().Location();
295             axe = AdC.Circle().Axis();
296             if (pos1.IsEqual(pos,Precision::Confusion())) {
297               // the top is mixed with the center of the circle
298               IType = 0;
299             }
300             else {
301               gp_Vec vec(pos1,pos);
302               gp_Dir dir(vec);
303               axe1 = gp_Ax1(pos1,dir);
304               if (axe.IsParallel(axe1,Precision::Angular())) {
305                 // the top is on the axis of the circle
306                 IType = -2;
307               }
308               else {
309                 // incorrect top --> no particular case
310                 IType = 0;
311               }
312             }
313           }
314         else
315           IType = 0;
316       }
317     }
318     
319   }
320   // torus and extrusion are not particular cases.
321   if (IType == 3 || IType == 5) IType = 0;
322   return IType;
323 }
324
325
326 //=======================================================================
327 //function : CreateKPart
328 //purpose  : 
329 //=======================================================================
330
331 void CreateKPart(const TopoDS_Edge& Edge1,const TopoDS_Edge& Edge2,
332                  const Standard_Integer IType, 
333                  Handle(Geom_Surface)& Surf)
334 {
335   // find the dimension
336   TopoDS_Vertex V1, V2;
337
338   TopLoc_Location loc;
339   Standard_Real a1, b1, aa =0., bb =0.;
340   TopoDS_Vertex v1f,v1l,v2f,v2l;
341
342   // find characteristics of the first edge
343   Handle(Geom_Curve) C1;
344   Standard_Boolean degen1 = BRep_Tool::Degenerated(Edge1);
345   if(degen1) {
346     // cone with degenerated edge at the top
347     TopExp::Vertices(Edge1,v1f,v1l);
348   }
349   else {
350     C1 = BRep_Tool::Curve(Edge1, loc, a1, b1);
351     C1 = Handle(Geom_Curve)::DownCast(C1->Transformed(loc.Transformation()));
352     aa = a1;
353     bb = b1;
354     if (Edge1.Orientation() == TopAbs_REVERSED) {
355       C1->Reverse();
356       aa = C1->ReversedParameter(b1);
357       bb = C1->ReversedParameter(a1);
358       TopExp::Vertices(Edge1,v1l,v1f);
359     }
360     else {
361       TopExp::Vertices(Edge1,v1f,v1l);
362     }
363   }
364
365   // find characteristics of the second edge
366   Handle(Geom_Curve) C2;
367   Standard_Boolean degen2 = BRep_Tool::Degenerated(Edge2);
368   if(degen2) {
369     // cone with degenerated edge at the top
370     TopExp::Vertices(Edge2,v2f,v2l);
371   }
372   else {
373     C2 = BRep_Tool::Curve(Edge2, loc, a1, b1);
374     C2 = Handle(Geom_Curve)::DownCast(C2->Transformed(loc.Transformation()));
375     if (Edge2.Orientation() == TopAbs_REVERSED) {
376       C2->Reverse();
377       if (degen1) {
378         aa = a1;
379         bb = b1;
380       }
381       TopExp::Vertices(Edge2,v2l,v2f);
382     }
383     else {
384       if (degen1) {
385         aa = a1; //C2->ReversedParameter(b1);
386         bb = b1; //C2->ReversedParameter(a1);
387       }
388       TopExp::Vertices(Edge2,v2f,v2l);
389     }
390   }
391
392   // create the new surface
393   TopoDS_Shell shell;
394   TopoDS_Face face;
395   TopoDS_Wire W;
396   TopoDS_Edge edge1, edge2, edge3, edge4, couture;
397
398   BRep_Builder B;
399   B.MakeShell(shell);
400
401   TopoDS_Wire newW1, newW2;
402   BRep_Builder BW1, BW2;
403   BW1.MakeWire(newW1);
404   BW2.MakeWire(newW2);
405
406
407   // calculate the surface
408   Handle(Geom_Surface) surface;
409   Standard_Real V, Rad;
410   if (IType==1) {
411     // cylindrical surface
412     gp_Circ c1 = (Handle(Geom_Circle)::DownCast(C1))->Circ();
413     gp_Circ c2 = (Handle(Geom_Circle)::DownCast(C2))->Circ();
414     gp_Ax3 Ac1 = c1.Position();
415     V = gp_Vec( c1.Location(),c2.Location()).Dot(gp_Vec(Ac1.Direction()));
416     if ( V < 0.) {
417       Ac1.ZReverse();
418       V = -V;
419     }
420     Handle(Geom_CylindricalSurface) Cyl = 
421       new Geom_CylindricalSurface( Ac1, c1.Radius());
422     surface = new Geom_RectangularTrimmedSurface
423       ( Cyl, aa, bb, Min(0.,V), Max(0.,V) );
424   }
425   else if (IType==2) {
426     // conical surface
427     gp_Circ k1 = (Handle(Geom_Circle)::DownCast(C1))->Circ();
428     gp_Ax3 Ak1 = k1.Position();
429     if (degen2) {
430       V = gp_Vec( k1.Location(),BRep_Tool::Pnt(v2f))
431         .Dot(gp_Vec(Ak1.Direction()));
432       Rad = - k1.Radius();
433     }
434     else {
435       gp_Circ k2 = (Handle(Geom_Circle)::DownCast(C2))->Circ();
436       V = gp_Vec( k1.Location(),k2.Location()).Dot(gp_Vec(Ak1.Direction()));
437       Rad = k2.Radius() - k1.Radius();
438     }
439       
440     if ( V < 0.) {
441       Ak1.ZReverse();
442       V = -V;
443     }
444     Standard_Real Ang = ATan( Rad / V);
445     Handle(Geom_ConicalSurface) Cone = 
446       new Geom_ConicalSurface( Ak1, Ang, k1.Radius());
447     V /= Cos(Ang);
448     surface = new Geom_RectangularTrimmedSurface
449       ( Cone, aa, bb, Min(0.,V), Max(0.,V) );
450   }
451   else if (IType==-2) {
452     // conical surface with the top at the beginning (degen1 is true)
453     gp_Circ k2 = (Handle(Geom_Circle)::DownCast(C2))->Circ();
454     gp_Ax3 Ak2 = k2.Position();
455     Ak2.SetLocation(BRep_Tool::Pnt(v1f));
456     V = gp_Vec(BRep_Tool::Pnt(v1f),k2.Location())
457                                 .Dot(gp_Vec(Ak2.Direction()));
458     Rad = k2.Radius(); // - k2.Radius();      
459     if ( V < 0.) {
460       Ak2.ZReverse();
461       V = -V;
462     }
463     Standard_Real Ang = ATan( Rad / V);
464     Handle(Geom_ConicalSurface) Cone = 
465       new Geom_ConicalSurface( Ak2, Ang, 0.);
466     V /= Cos(Ang);
467     surface = new Geom_RectangularTrimmedSurface
468       ( Cone, aa, bb, Min(0.,V), Max(0.,V) );
469   }
470   else if (IType==3) {
471     // torus surface ?
472   }
473   else if (IType==4) {
474     // surface plane
475     gp_Lin L1, L2, aLine;
476     if (!degen1)
477       {
478         L1 = (Handle(Geom_Line)::DownCast(C1))->Lin();
479         aLine = L1;
480       }
481     if (!degen2)
482       {
483         L2 = (Handle(Geom_Line)::DownCast(C2))->Lin();
484         aLine = L2;
485       }
486
487     gp_Pnt P1 = (degen1)? BRep_Tool::Pnt(v1f) : L1.Location();
488     gp_Pnt P2 = (degen2)? BRep_Tool::Pnt(v2f) : L2.Location();
489
490     gp_Vec P1P2( P1, P2 ); 
491     gp_Dir D1 = aLine.Direction();
492     gp_Ax3 Ax( aLine.Location(), gp_Dir(D1.Crossed(P1P2)), D1 );
493     Handle(Geom_Plane) Plan = new Geom_Plane(Ax);
494     V = P1P2.Dot( Ax.YDirection());
495     surface = Plan;
496     //surface = new Geom_RectangularTrimmedSurface
497       //( Plan, aa, bb, Min(0.,V), Max(0.,V) );
498   }
499   else if (IType==5) {
500     // surface of extrusion ?
501   }
502   else {
503     // IType incorrect
504   }
505   Surf = surface;
506 }
507
508
509 //=======================================================================
510 //function : BRepFill_Generator
511 //purpose  : 
512 //=======================================================================
513
514 BRepFill_Generator::BRepFill_Generator()
515 {
516 }
517
518
519 //=======================================================================
520 //function : AddWire
521 //purpose  : 
522 //=======================================================================
523
524 void BRepFill_Generator::AddWire(const TopoDS_Wire& Wire)
525 {
526   myWires.Append( Wire);
527 }
528
529
530 //=======================================================================
531 //function : Perform
532 //purpose  : 
533 //=======================================================================
534
535 void BRepFill_Generator::Perform()
536 {
537   TopoDS_Shell Shell;
538   TopoDS_Face  Face;
539   TopoDS_Shape S1, S2;
540   TopoDS_Edge  Edge1, Edge2, Edge3, Edge4, Couture;
541
542   BRep_Builder B;
543   B.MakeShell(myShell);
544
545   Standard_Integer Nb = myWires.Length();
546
547   BRepTools_WireExplorer ex1,ex2;
548
549   Standard_Boolean wPoint1, wPoint2, uClosed, DegenFirst, DegenLast;
550   
551   for ( Standard_Integer i = 1; i <= Nb-1; i++) {
552
553     TopoDS_Wire Wire1 = TopoDS::Wire(myWires( i ));
554     TopoDS_Wire Wire2 = TopoDS::Wire(myWires(i+1));
555
556     wPoint1 = Standard_False;
557     if (i==1) {
558       wPoint1 = Standard_True;
559       for(ex1.Init(Wire1); ex1.More(); ex1.Next()) {
560         wPoint1 = wPoint1 && (BRep_Tool::Degenerated(ex1.Current()));
561       }
562       DegenFirst = wPoint1;
563
564       TopoDS_Vertex V1, V2;
565       TopExp::Vertices(Wire1, V1, V2);
566       uClosed = V1.IsSame(V2);
567     }
568
569     wPoint2 = Standard_False;
570     if (i==Nb-1) {
571       wPoint2 = Standard_True;
572       for(ex2.Init(Wire2); ex2.More(); ex2.Next()) {
573         wPoint2 = wPoint2 && (BRep_Tool::Degenerated(ex2.Current()));
574       }
575       DegenLast = wPoint2;
576     }
577
578     ex1.Init(Wire1);
579     ex2.Init(Wire2);
580
581     TopTools_DataMapOfShapeShape Map;
582     
583     Standard_Boolean tantque = ex1.More() && ex2.More();
584
585     while ( tantque ) { 
586
587       TopoDS_Vertex V1f,V1l,V2f,V2l, Vf_toMap, Vl_toMap;
588
589       Standard_Boolean degen1 
590         = BRep_Tool::Degenerated(TopoDS::Edge(ex1.Current()));
591       Standard_Boolean degen2
592         = BRep_Tool::Degenerated(TopoDS::Edge(ex2.Current()));
593
594       if ( degen1 ) {
595         TopoDS_Shape aLocalShape = ex1.Current().EmptyCopied();
596         Edge1 = TopoDS::Edge(aLocalShape);
597 //      Edge1 = TopoDS::Edge(ex1.Current().EmptyCopied());
598 //      aLocalShape = ex1.Current();
599 //      TopExp::Vertices(TopoDS::Edge(aLocalShape),V1f,V1l);
600         TopExp::Vertices(TopoDS::Edge(ex1.Current()),V1f,V1l);
601         V1f.Orientation(TopAbs_FORWARD);
602         B.Add(Edge1,V1f);
603         V1l.Orientation(TopAbs_REVERSED);
604         B.Add(Edge1,V1l);
605         B.Range(Edge1,0,1);
606       }
607       else {
608         TopoDS_Shape aLocalShape = ex1.Current();
609         Edge1 = TopoDS::Edge(aLocalShape);
610 //      Edge1 = TopoDS::Edge(ex1.Current());
611       }
612
613       if ( degen2 ) {
614         TopoDS_Shape aLocalShape = ex2.Current().EmptyCopied();
615         Edge2 = TopoDS::Edge(aLocalShape);
616 //      Edge2 = TopoDS::Edge(ex2.Current().EmptyCopied());
617         TopExp::Vertices(TopoDS::Edge(ex2.Current()),V2f,V2l);
618         V2f.Orientation(TopAbs_FORWARD);
619         B.Add(Edge2,V2f);
620         V2l.Orientation(TopAbs_REVERSED);
621         B.Add(Edge2,V2l);
622         B.Range(Edge2,0,1);
623       }
624       else {
625         Edge2 = TopoDS::Edge(ex2.Current());
626       }
627
628       Standard_Boolean Periodic
629         = (Edge1.Closed() || degen1) && (Edge2.Closed() || degen2);
630       // ATTENTION : a non-punctual wire should not 
631       //             contain a punctual edge
632       if (!wPoint1) ex1.Next();
633       if (!wPoint2) ex2.Next();
634
635       // initialization of vertices
636       Handle(Geom_Surface) Surf;
637       Standard_Real f1=0, l1=1, f2=0, l2=1;
638       if (Edge1.Orientation() == TopAbs_REVERSED)
639         TopExp::Vertices(Edge1,V1l,V1f);
640       else
641         TopExp::Vertices(Edge1,V1f,V1l);
642       if (Edge2.Orientation() == TopAbs_REVERSED)
643         TopExp::Vertices(Edge2,V2l,V2f);
644       else
645         TopExp::Vertices(Edge2,V2f,V2l);
646       if (degen1)
647         {
648           Vf_toMap = V2f;
649           Vl_toMap = V2l;
650         }
651       else
652         {
653           Vf_toMap = V1f;
654           Vl_toMap = V1l;
655         }
656       
657       if(Periodic) {
658         Standard_Boolean E1IsReallyClosed = BRepTools::Compare(V1f,V1l);
659         Standard_Boolean E2IsReallyClosed = BRepTools::Compare(V2f,V2l);
660         Periodic 
661           = (E1IsReallyClosed || degen1) && (E2IsReallyClosed || degen2);
662       }
663       // processing of KPart
664       Standard_Integer IType = DetectKPart(Edge1,Edge2);
665       if (IType==0) {
666         // no part cases
667         TopLoc_Location L,L1,L2;
668
669         Handle(Geom_Curve) C1, C2;
670         TColgp_Array1OfPnt Extremities(1,2);
671       
672         if (degen1) {
673           Extremities(1) = BRep_Tool::Pnt(V1f);
674           Extremities(2) = BRep_Tool::Pnt(V1l);
675           C1 = new Geom_BezierCurve(Extremities);
676         }
677         else {
678           C1 = BRep_Tool::Curve(Edge1,L1,f1,l1);
679         }
680         if (degen2) {
681           Extremities(1) = BRep_Tool::Pnt(V2l);
682           Extremities(2) = BRep_Tool::Pnt(V2f);
683           C2 = new Geom_BezierCurve(Extremities);
684         }
685         else {
686           C2 = BRep_Tool::Curve(Edge2,L2,f2,l2);
687         }
688         
689         // compute the location
690         Standard_Boolean SameLoc = Standard_False;
691         
692         // transform and trim the curves
693       
694         if (Abs(f1 - C1->FirstParameter()) > Precision::PConfusion() ||
695             Abs(l1 - C1->LastParameter())  > Precision::PConfusion()   ) {
696           C1 = new Geom_TrimmedCurve(C1,f1,l1);
697         }
698         else {
699           C1 = Handle(Geom_Curve)::DownCast(C1->Copy());
700         }
701         if (!SameLoc) C1->Transform(L1.Transformation());
702         if (Edge1.Orientation() == TopAbs_REVERSED) {
703           C1->Reverse();
704         }
705         
706         if (Abs(f2 - C2->FirstParameter()) > Precision::PConfusion() ||
707             Abs(l2 - C2->LastParameter())  > Precision::PConfusion()   ) {
708           C2 = new Geom_TrimmedCurve(C2,f2,l2);
709         }
710         else {
711           C2 = Handle(Geom_Curve)::DownCast(C2->Copy());
712         }
713         if (!SameLoc) C2->Transform(L2.Transformation());
714         if (Edge2.Orientation() == TopAbs_REVERSED) {
715           C2->Reverse();
716         }
717         
718         GeomFill_Generator Generator;
719         Generator.AddCurve( C1);
720         Generator.AddCurve( C2);
721         Generator.Perform( Precision::PConfusion());
722         
723         Surf = Generator.Surface();
724         B.MakeFace(Face,Surf,Precision::Confusion());
725       }
726       else {
727         // particular case
728         CreateKPart(Edge1,Edge2,IType,Surf);
729         B.MakeFace(Face,Surf,Precision::Confusion());
730       }
731       
732       // make the missing edges
733       Standard_Real first,last;
734       Surf->Bounds(f1,l1,f2,l2);
735
736       if ( Map.IsBound(Vf_toMap)) {
737         TopoDS_Shape aLocalShape = Map(Vf_toMap).Reversed();
738         Edge3 = TopoDS::Edge(aLocalShape);
739 //      Edge3 = TopoDS::Edge(Map(V1f).Reversed());
740       }
741       else {
742         Handle(Geom_Curve) CC;
743         TColgp_Array1OfPnt Extremities(1,2);
744         if (IType==0) {
745           // general case : Edge3 corresponds to iso U=f1
746           CC = Surf->UIso(f1);
747           first=f2;
748           last=l2;
749         }
750         else {
751           // particular case : it is required to calculate the curve 3d
752           Extremities(1) = BRep_Tool::Pnt(V1f);
753           Extremities(2) = BRep_Tool::Pnt(V2f);
754           CC = new Geom_BezierCurve(Extremities);
755           first=0.;
756           last=1.;
757         }
758         B.MakeEdge(Edge3,CC,Precision::Confusion());
759         V1f.Orientation(TopAbs_FORWARD);
760         B.Add(Edge3,V1f);
761         V2f.Orientation(TopAbs_REVERSED);
762         B.Add(Edge3,V2f);
763         B.Range(Edge3,first,last);
764         Edge3.Reverse();
765         Map.Bind(Vf_toMap, Edge3);
766       }
767
768       Standard_Boolean CommonEdge = Standard_False;
769       if ( Map.IsBound(Vl_toMap)  ) {
770         TopoDS_Shape aLocalShape = Map(Vl_toMap).Reversed();
771         const TopoDS_Edge CommonE = TopoDS::Edge(aLocalShape);
772 //      const TopoDS_Edge CommonE = TopoDS::Edge(Map(V1l).Reversed());
773         TopoDS_Vertex V1, V2;
774         TopExp::Vertices(CommonE,V1,V2);
775         CommonEdge = V1.IsSame(V1l) && V2.IsSame(V2l);
776       }
777       if ( CommonEdge ) {
778         TopoDS_Shape aLocalShape = Map(Vl_toMap).Reversed();
779         Edge4 = TopoDS::Edge(aLocalShape);
780 //      Edge4 = TopoDS::Edge(Map(V1l).Reversed());
781       }
782       else {
783         Handle(Geom_Curve) CC;
784         TColgp_Array1OfPnt Extremities(1,2);
785         if (IType==0) {
786           // general case : Edge4 corresponds to iso U=l1
787           CC = Surf->UIso(l1);
788           first=f2;
789           last=l2;
790         }
791         else {
792           // particular case : it is required to calculate the curve 3d
793           Extremities(1) = BRep_Tool::Pnt(V1l);
794           Extremities(2) = BRep_Tool::Pnt(V2l);
795           CC = new Geom_BezierCurve(Extremities);
796           first=0.;
797           last=1.;
798         }
799         B.MakeEdge(Edge4,CC,Precision::Confusion());
800         V1l.Orientation(TopAbs_FORWARD);
801         B.Add(Edge4,V1l);
802         V2l.Orientation(TopAbs_REVERSED);
803         B.Add(Edge4,V2l);
804         B.Range(Edge4,first,last);
805         Map.Bind(Vl_toMap, Edge4);
806       }
807
808       // make the wire
809       
810       TopoDS_Wire W;
811       B.MakeWire(W);
812       
813       if (! (degen1 && IType == 4))
814         B.Add(W,Edge1);
815       B.Add(W,Edge4);
816       if (! (degen2 && IType == 4))
817         B.Add(W,Edge2.Reversed());
818       B.Add(W,Edge3);
819       
820       B.Add(Face,W);
821       
822       B.Add(myShell,Face);
823
824     // complete myMap for edge1
825       if (! (degen1 && IType == 4))
826         {
827           TopTools_ListOfShape Empty;
828           if (!myMap.IsBound(Edge1)) myMap.Bind(Edge1,Empty);
829           myMap(Edge1).Append(Face);
830         }
831       
832       // set the pcurves
833       
834       Standard_Real T = Precision::Confusion();
835
836       if (IType != 4) //not plane
837         {
838           if ( Edge1.Orientation() == TopAbs_REVERSED ) {
839             B.UpdateEdge(Edge1,
840                          new Geom2d_Line(gp_Pnt2d(0,f2),gp_Dir2d(-1,0)),
841                          Face,T);
842             B.Range(Edge1,Face,-l1,-f1);
843           }
844           else {
845             B.UpdateEdge(Edge1,
846                          new Geom2d_Line(gp_Pnt2d(0,f2),gp_Dir2d(1,0)),
847                          Face,T);
848             B.Range(Edge1,Face,f1,l1);
849           }
850           
851           if ( Edge2.Orientation() == TopAbs_REVERSED ) {
852             B.UpdateEdge(Edge2,
853                          new Geom2d_Line(gp_Pnt2d(0,l2),gp_Dir2d(-1,0)),
854                          Face,T);
855             B.Range(Edge2,Face,-l1,-f1);
856           }
857           else {
858             B.UpdateEdge(Edge2,
859                          new Geom2d_Line(gp_Pnt2d(0,l2),gp_Dir2d(1,0)),
860                          Face,T);
861             B.Range(Edge2,Face,f1,l1);
862           }
863         }
864
865       if (IType==0) {
866         if ( Periodic) {
867           B.UpdateEdge(Edge3,
868                        new Geom2d_Line(gp_Pnt2d(l1,0),gp_Dir2d(0,1)),
869                        new Geom2d_Line(gp_Pnt2d(f1,0),gp_Dir2d(0,1)),
870                        Face,T);
871         }
872         else {
873           B.UpdateEdge(Edge3,
874                        new Geom2d_Line(gp_Pnt2d(f1,0),gp_Dir2d(0,1)),
875                        Face,T);
876           B.UpdateEdge(Edge4,
877                        new Geom2d_Line(gp_Pnt2d(l1,0),gp_Dir2d(0,1)),
878                        Face,T);
879         }
880       }
881       else {
882         // KPart
883         if ( Periodic) {
884           TColgp_Array1OfPnt2d Extrem1(1,2);
885           Extrem1(1).SetCoord(l1,f2);
886           Extrem1(2).SetCoord(l1,l2);
887           TColgp_Array1OfPnt2d Extrem2(1,2);
888           Extrem2(1).SetCoord(f1,f2);
889           Extrem2(2).SetCoord(f1,l2);
890           B.UpdateEdge(Edge3,
891                        new Geom2d_BezierCurve(Extrem1),
892                        new Geom2d_BezierCurve(Extrem2),
893                        Face,T);
894         }
895         else if (IType != 4) { //not plane
896           TColgp_Array1OfPnt2d Extrem2(1,2);
897           Extrem2(1).SetCoord(f1,f2);
898           Extrem2(2).SetCoord(f1,l2);
899           B.UpdateEdge(Edge3,
900                        new Geom2d_BezierCurve(Extrem2),
901                        Face,T);
902           TColgp_Array1OfPnt2d Extrem1(1,2);
903           Extrem1(1).SetCoord(l1,f2);
904           Extrem1(2).SetCoord(l1,l2);
905           B.UpdateEdge(Edge4,
906                        new Geom2d_BezierCurve(Extrem1),
907                        Face,T);
908         }
909       }
910       // Set the non parameter flag;
911       B.SameParameter(Edge1,Standard_False);
912       B.SameParameter(Edge2,Standard_False);
913       B.SameParameter(Edge3,Standard_False);
914       B.SameParameter(Edge4,Standard_False);
915       B.SameRange(Edge1,Standard_False);
916       B.SameRange(Edge2,Standard_False);
917       B.SameRange(Edge3,Standard_False);
918       B.SameRange(Edge4,Standard_False);
919
920       tantque = ex1.More() && ex2.More();
921       if (wPoint1) tantque = ex2.More();
922       if (wPoint2) tantque = ex1.More();
923     }
924   }
925   BRepLib::SameParameter(myShell);
926
927   if (uClosed && DegenFirst && DegenLast)
928     myShell.Closed(Standard_True);
929 }
930
931
932 //=======================================================================
933 //function : GeneratedShapes
934 //purpose  : 
935 //=======================================================================
936
937 const TopTools_ListOfShape& 
938  BRepFill_Generator::GeneratedShapes (const TopoDS_Shape& SSection) const 
939 {
940   if (myMap.IsBound(SSection)) {
941     return myMap(SSection);
942   }
943   else {
944     static TopTools_ListOfShape Empty;
945     return Empty;
946   }
947 }
948
949 //=======================================================================
950 //function : Generated
951 //purpose  : 
952 //=================================================================== ====
953
954 const TopTools_DataMapOfShapeListOfShape& BRepFill_Generator::Generated() const
955 {
956   return myMap;
957 }
958
959