0024924: ShapeFix_SplitTool doesn't verify the new range after cutting an edge
[occt.git] / src / BRepFill / BRepFill_Pipe.cxx
1 // Created on: 1994-06-07
2 // Created by: Bruno DUMORTIER
3 // Copyright (c) 1994-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 <BRepFill_Pipe.ixx>
18
19 #include <Standard_ErrorHandler.hxx>
20
21 #include <BRep_Tool.hxx>
22 #include <BRep_Builder.hxx>
23 #include <BRepClass3d_SolidClassifier.hxx>
24 #include <BRepLib_MakeVertex.hxx>
25 #include <BRepTools_Substitution.hxx>
26
27 #include <GeomFill_CorrectedFrenet.hxx>
28 #include <GeomFill_Frenet.hxx>
29 #include <GeomFill_DiscreteTrihedron.hxx>
30 #include <GeomFill_CurveAndTrihedron.hxx>
31
32 #include <BRepFill_SectionPlacement.hxx>
33 #include <BRepFill_ShapeLaw.hxx>
34 #include <BRepFill_Edge3DLaw.hxx>
35 #include <BRepFill_Sweep.hxx>
36
37 #include <GeomAbs_Shape.hxx>
38 #include <TopExp.hxx>
39 #include <TopAbs_ShapeEnum.hxx>
40 #include <TopoDS.hxx>
41 #include <TopoDS_Shell.hxx>
42 #include <TopoDS_Solid.hxx>
43 #include <TopoDS_Compound.hxx>
44 #include <TopoDS_Iterator.hxx>
45 #include <TopTools_DataMapOfShapeInteger.hxx>
46 #include <TColStd_DataMapOfIntegerInteger.hxx>
47 #include <TColStd_DataMapIteratorOfDataMapOfIntegerInteger.hxx>
48
49 #include <Precision.hxx>
50 #include <Standard_NotImplemented.hxx>
51
52 #include <Geom_TrimmedCurve.hxx>
53 #include <Geom_OffsetCurve.hxx>
54 #include <Geom_BSplineCurve.hxx>
55 #include <BRepBuilderAPI_Transform.hxx>
56 #include <BRepBuilderAPI_Copy.hxx>
57 #include <TopTools_SequenceOfShape.hxx>
58 #include <TopTools_ListIteratorOfListOfShape.hxx>
59 #include <BRepLib.hxx>
60
61 #include <Geom2dAdaptor_HCurve.hxx>
62 #include <GeomAdaptor_HSurface.hxx>
63 #include <Adaptor3d_CurveOnSurface.hxx>
64
65 #include <ShapeUpgrade_RemoveLocations.hxx>
66
67 #ifdef DRAW
68 #include <DBRep.hxx>
69 static Standard_Boolean Affich = 0;
70 #endif
71
72 static void ReverseModifiedEdges(TopoDS_Shape& aShape,
73                                  TopTools_MapOfShape& Emap)
74 {
75   TopExp_Explorer Explo(aShape, TopAbs_FACE);
76   BRep_Builder BB;
77   
78   for (; Explo.More(); Explo.Next())
79   {
80     TopoDS_Shape aFace = Explo.Current();
81     TopoDS_Iterator itf(aFace);
82     for (; itf.More(); itf.Next())
83     {
84       TopoDS_Shape aWire = itf.Value();
85       TopTools_ListOfShape Ledges;
86       TopoDS_Iterator itw(aWire);
87       for (; itw.More(); itw.Next())
88         Ledges.Append(itw.Value());
89
90       aWire.Free(Standard_True);
91       TopTools_ListIteratorOfListOfShape itl(Ledges);
92       for (; itl.More(); itl.Next())
93         BB.Remove(aWire, itl.Value());
94         
95       for (itl.Initialize(Ledges); itl.More(); itl.Next())
96       {
97         TopoDS_Shape anEdge = itl.Value();
98         if (Emap.Contains(anEdge))
99           anEdge.Reverse();
100         BB.Add(aWire, anEdge);
101       }
102     }
103   }
104 }
105
106 static void UpdateTolFromTopOrBottomPCurve(const TopoDS_Face& aFace,
107                                            TopoDS_Edge& anEdge)
108 {
109   Standard_Real fpar, lpar;
110   Handle(Geom2d_Curve) aPCurve = BRep_Tool::CurveOnSurface(anEdge, aFace, fpar, lpar);
111   if (aPCurve.IsNull())
112     return;
113
114   Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, fpar, lpar);
115   if (aCurve.IsNull())
116     return;
117
118   Handle(Geom2dAdaptor_HCurve) GAHC2d = new Geom2dAdaptor_HCurve(aPCurve, fpar, lpar);
119   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
120   Handle(GeomAdaptor_HSurface) GAHS = new GeomAdaptor_HSurface(aSurf);
121   Adaptor3d_CurveOnSurface ConS(GAHC2d, GAHS);
122
123   Standard_Real Tol = BRep_Tool::Tolerance(anEdge);
124   Standard_Real InitTol = Tol;
125   Standard_Real TolTol = Tol*Tol;
126   const Standard_Integer NCONTROL = 22;
127   Standard_Real delta = (lpar - fpar)/NCONTROL;
128   for (Standard_Integer i = 0; i <= NCONTROL; i++)
129   {
130     Standard_Real par = fpar + i*delta;
131     gp_Pnt pnt = aCurve->Value(par);
132     gp_Pnt prj = ConS.Value(par);
133     Standard_Real sqdist = pnt.SquareDistance(prj);
134     if (sqdist > TolTol)
135       TolTol = sqdist;
136   }
137   Tol = 1.00005 * Sqrt(TolTol);
138   if (Tol >= InitTol)
139   {
140     BRep_Builder BB;
141     BB.UpdateEdge(anEdge, Tol);
142     TopoDS_Iterator itv(anEdge);
143     for (; itv.More(); itv.Next())
144     {
145       TopoDS_Vertex aVertex = TopoDS::Vertex(itv.Value());
146       BB.UpdateVertex(aVertex, Tol);
147     }
148   }
149 }
150
151 //=======================================================================
152 //function : BRepFill_Pipe
153 //purpose  : 
154 //=======================================================================
155
156 BRepFill_Pipe::BRepFill_Pipe()
157 {
158   myDegmax = 11;
159   mySegmax = 100;
160   myContinuity = GeomAbs_C2;
161   myMode = GeomFill_IsCorrectedFrenet;
162   myForceApproxC1 = Standard_False;
163
164   myCurIndexOfSectionEdge = 1;
165 }
166
167
168 //=======================================================================
169 //function : BRepFill_Pipe
170 //purpose  : 
171 //=======================================================================
172
173 BRepFill_Pipe::BRepFill_Pipe(const TopoDS_Wire&  Spine,
174                              const TopoDS_Shape& Profile,
175                              const GeomFill_Trihedron aMode,
176                              const Standard_Boolean ForceApproxC1,
177                              const Standard_Boolean KPart)
178                              
179 {
180   myDegmax = 11;
181   mySegmax = 100;
182   
183   myMode = GeomFill_IsCorrectedFrenet;
184   if (aMode == GeomFill_IsFrenet ||
185       aMode == GeomFill_IsCorrectedFrenet ||
186       aMode == GeomFill_IsDiscreteTrihedron)
187     myMode = aMode;
188
189   myContinuity = GeomAbs_C2;
190   if (myMode == GeomFill_IsDiscreteTrihedron)
191     myContinuity = GeomAbs_C0;
192   
193   myForceApproxC1 = ForceApproxC1;
194
195   myCurIndexOfSectionEdge = 1;
196   
197   Perform(Spine, Profile, KPart);
198 }
199
200
201 //=======================================================================
202 //function : Perform
203 //purpose  : 
204 //=======================================================================
205
206 void BRepFill_Pipe::Perform(const TopoDS_Wire&  Spine,
207                             const TopoDS_Shape& Profile,
208                             const Standard_Boolean /*KPart*/)
209
210 {
211   mySections.Nullify();
212   myFaces.Nullify();
213   myEdges.Nullify();
214
215   mySpine   = Spine;
216   myProfile = Profile;
217
218   DefineRealSegmax();
219
220   BRepTools_WireExplorer wexp;
221   TopoDS_Shape TheProf; 
222
223   Handle(GeomFill_TrihedronLaw) TLaw;
224   switch (myMode)
225   {
226   case GeomFill_IsFrenet:
227     TLaw = new GeomFill_Frenet();
228     break;
229   case GeomFill_IsCorrectedFrenet:
230     TLaw = new GeomFill_CorrectedFrenet();
231     break;
232   case GeomFill_IsDiscreteTrihedron:
233     TLaw = new GeomFill_DiscreteTrihedron();
234     break;
235   default:
236     break;
237   }
238   Handle(GeomFill_CurveAndTrihedron) Loc = 
239     new (GeomFill_CurveAndTrihedron) (TLaw);
240   myLoc = new (BRepFill_Edge3DLaw) (mySpine, Loc);
241   if (myLoc->NbLaw() == 0) {
242     return; // Degenerated case
243   }
244   myLoc->TransformInG0Law(); // Set into continuity
245     
246   BRepFill_SectionPlacement Place(myLoc, Profile);
247   myTrsf = Place.Transformation();
248
249   TopLoc_Location Loc2(myTrsf), Loc1;
250   Loc1 = Profile.Location();
251   TopoDS_Shape aux;
252   TheProf =  myProfile;
253   TheProf.Location(Loc2.Multiplied(Loc1));
254  
255   // Construct First && Last Shape
256   Handle(GeomFill_LocationLaw) law;
257
258   gp_Mat M;
259   gp_Vec V;
260   gp_Trsf fila;
261   Standard_Real first, last;
262   myLoc->Law(1)->GetDomain(first, last);
263   myLoc->Law(1)->D0(first, M, V);
264     fila.SetValues(M(1,1), M(1,2), M(1,3), V.X(),
265                    M(2,1), M(2,2), M(2,3), V.Y(),
266                    M(3,1), M(3,2), M(3,3), V.Z(),
267                    1.e-12, 1.e-14);
268
269   fila.Multiply(myTrsf);
270   TopLoc_Location LocFirst(fila);
271   myFirst = myProfile;
272   if ( ! LocFirst.IsIdentity()) {
273     //myFirst.Location( LocFirst.Multiplied(myProfile.Location()) );
274     myFirst = BRepBuilderAPI_Transform(myProfile, fila, Standard_True); //copy
275   }
276
277   ShapeUpgrade_RemoveLocations RemLoc;
278   RemLoc.Remove(myFirst);
279   myFirst = RemLoc.GetResult();
280   TopLoc_Location theLoc = myFirst.Location();
281   if (!theLoc.IsIdentity())
282   {
283     TopoDS_Shape NewMyFirst = BRepBuilderAPI_Copy(myFirst);
284     RemLoc.Remove(NewMyFirst);
285     NewMyFirst = RemLoc.GetResult();
286     TopLoc_Location theIdentity;
287     NewMyFirst.Location(theIdentity);
288     myFirst = BRepBuilderAPI_Transform(NewMyFirst, theLoc.Transformation(), Standard_True);
289   }
290   
291   myLoc->Law(myLoc->NbLaw())->GetDomain(first, last);
292   myLoc->Law(myLoc->NbLaw())->D0(last,M, V);
293 //    try { // Not good, but there are no other means to test SetValues
294   fila.SetValues(M(1,1), M(1,2), M(1,3), V.X(),
295                  M(2,1), M(2,2), M(2,3), V.Y(),
296                  M(3,1), M(3,2), M(3,3), V.Z(),
297                  1.e-12, 1.e-14);
298   fila.Multiply(myTrsf);
299   TopLoc_Location LocLast(fila);
300   if (! myLoc->IsClosed() || LocFirst != LocLast) {
301     myLast = myProfile;
302     if ( ! LocLast.IsIdentity()) {
303       //myLast.Location(LocLast.Multiplied(myProfile.Location()) );
304       myLast = BRepBuilderAPI_Transform(myProfile, fila, Standard_True); //copy
305     }
306   }
307   else {
308     myLast = myFirst;
309   }
310
311   RemLoc.Remove(myLast);
312   myLast = RemLoc.GetResult();
313   theLoc = myLast.Location();
314   if (!theLoc.IsIdentity())
315   {
316     TopoDS_Shape NewMyLast = BRepBuilderAPI_Copy(myLast);
317     RemLoc.Remove(NewMyLast);
318     NewMyLast = RemLoc.GetResult();
319     TopLoc_Location theIdentity;
320     NewMyLast.Location(theIdentity);
321     myLast = BRepBuilderAPI_Transform(NewMyLast, theLoc.Transformation(), Standard_True);
322   }
323   
324 #if DRAW
325   if (Affich) {
326     DBRep::Set("theprof",  TheProf);
327     DBRep::Set("thefirst", myFirst);
328     DBRep::Set("thelast" , myLast);
329  }
330 #endif
331  
332   myShape = MakeShape(TheProf, myFirst, myLast);
333 }
334
335
336 //=======================================================================
337 //function : Spine
338 //purpose  : 
339 //=======================================================================
340
341 const TopoDS_Shape& BRepFill_Pipe::Spine() const 
342 {
343   return mySpine;
344 }
345
346 //=======================================================================
347 //function : Profile
348 //purpose  : 
349 //=======================================================================
350
351 const TopoDS_Shape& BRepFill_Pipe::Profile() const 
352 {
353   return myProfile;
354 }
355
356 //=======================================================================
357 //function : Shape
358 //purpose  : 
359 //=======================================================================
360
361 const TopoDS_Shape& BRepFill_Pipe::Shape() const 
362 {
363   return myShape;
364 }
365
366
367 //=======================================================================
368 //function : FirstShape
369 //purpose  : 
370 //=======================================================================
371
372 const TopoDS_Shape& BRepFill_Pipe::FirstShape() const 
373 {
374   return myFirst;
375 }
376
377
378 //=======================================================================
379 //function : LastShape
380 //purpose  : 
381 //=======================================================================
382
383 const TopoDS_Shape& BRepFill_Pipe::LastShape() const 
384 {
385   return myLast;
386 }
387
388
389 //=======================================================================
390 //function : Face
391 //purpose  : 
392 //=======================================================================
393
394 TopoDS_Face BRepFill_Pipe::Face(const TopoDS_Edge& ESpine,
395                                 const TopoDS_Edge& EProfile)
396 {
397   TopoDS_Face theFace;
398
399   if ( BRep_Tool::Degenerated(EProfile))
400     return theFace;
401
402   Standard_Integer ii, ispin = 0, iprof = 0, count = 0;
403
404   // *************************************************
405   // Search if EProfile is an edge of myProfile
406   // *************************************************
407   iprof = FindEdge(myProfile, EProfile, count);
408
409   if (!iprof) Standard_DomainError::Raise(
410               "BRepFill_Pipe::Face : Edge not in the Profile");
411
412
413   // *************************************************
414   // Search if ESpine  is an edge of mySpine and find 
415   // the index of the corresponding Filler
416   // *************************************************
417  for (ii=1; ii<=myLoc->NbLaw() && (!ispin); ii++) 
418     if  (ESpine.IsSame(myLoc->Edge(ii))) ispin = ii;
419
420   if (!ispin) Standard_DomainError::Raise(
421     "BRepFill_Pipe::Edge  : Edge not in the Spine");
422
423   theFace = TopoDS::Face(myFaces->Value(iprof, ispin));
424   return theFace;
425
426 }
427
428 //=======================================================================
429 //function : Edge
430 //purpose  : 
431 //=======================================================================
432 TopoDS_Edge BRepFill_Pipe::Edge(const TopoDS_Edge&   ESpine,
433                                 const TopoDS_Vertex& VProfile)
434 {
435   Standard_Integer ii, ispin = 0, iprof = 0, count = 0;;
436
437   // *************************************************
438   // Search if VProfile is a Vertex of myProfile
439   // *************************************************
440   iprof = FindVertex(myProfile, VProfile, count);
441   if (!iprof) Standard_DomainError::Raise(
442         "BRepFill_Pipe::Edge : Vertex not in the Profile");
443
444
445   // *************************************************
446   // Search if ESpine  is an edge of mySpine and find 
447   // the index of the corresponding Filler
448   // *************************************************
449    
450   for (ii=1; ii<=myLoc->NbLaw() && (!ispin); ii++) 
451     if  (ESpine.IsSame(myLoc->Edge(ii))) ispin = ii;
452
453   if (!ispin) Standard_DomainError::Raise(
454     "BRepFill_Pipe::Edge  : Edge not in the Spine");
455
456
457   // *************************************************
458   // Generate the corresponding Shape
459   // *************************************************  
460   TopoDS_Edge theEdge;
461   theEdge = TopoDS::Edge(myEdges->Value(iprof, ispin));
462
463   return theEdge;
464
465 }
466
467
468 //=======================================================================
469 //function : Section
470 //purpose  : 
471 //=======================================================================
472
473 TopoDS_Shape BRepFill_Pipe::Section(const TopoDS_Vertex& VSpine) const 
474 {
475   TopoDS_Iterator it, itv;
476
477   Standard_Integer ii, ispin = 0;
478
479   TopoDS_Shape curSect = myProfile;
480
481   // *************************************************
482   // Search if ESpine  is an edge of mySpine and find 
483   // the index of the corresponding Filler
484   // *************************************************
485
486   // iterate on all the edges of mySpine
487  for (ii=1; ii<=myLoc->NbLaw()+1 && (!ispin); ii++) 
488     if  (VSpine.IsSame(myLoc->Vertex(ii))) ispin = ii;
489
490   if (!ispin) Standard_DomainError::Raise(
491     "BRepFill_Pipe::Section  : Vertex not in the Spine");
492
493   BRep_Builder B;
494   TopoDS_Compound Comp; 
495   B.MakeCompound(Comp);
496   for (ii=1; ii<=mySections->ColLength(); ii++)
497     B.Add(Comp, mySections->Value(ii, ispin));
498   
499   return Comp;
500 }
501
502 //=======================================================================
503 //function : PipeLine
504 //purpose  : Construct a wire by sweeping of a point
505 //=======================================================================
506
507 TopoDS_Wire BRepFill_Pipe::PipeLine(const gp_Pnt& Point)
508 {
509  // Postioning 
510  gp_Pnt P;
511  P = Point;
512  P.Transform(myTrsf);
513
514  BRepLib_MakeVertex MkV(P); 
515  Handle(BRepFill_ShapeLaw) Section = 
516         new (BRepFill_ShapeLaw) (MkV.Vertex());
517
518  // Sweeping
519  BRepFill_Sweep MkSw(Section, myLoc, Standard_True);
520  MkSw.SetForceApproxC1(myForceApproxC1);
521  MkSw.Build( myReversedEdges, myTapes,
522              BRepFill_Modified, myContinuity, GeomFill_Location, myDegmax, mySegmax );
523  TopoDS_Shape aLocalShape = MkSw.Shape();
524  return TopoDS::Wire(aLocalShape);
525 // return TopoDS::Wire(MkSw.Shape());
526 }
527
528 //=======================================================================
529 //function : MakeShape
530 //purpose  : 
531 //=======================================================================
532
533 TopoDS_Shape BRepFill_Pipe::MakeShape(const TopoDS_Shape& S,
534                                       const TopoDS_Shape& FirstShape,
535                                       const TopoDS_Shape& LastShape)
536 {
537   TopoDS_Shape result;
538   BRep_Builder B;
539   Standard_Boolean explode = Standard_False; 
540   TopoDS_Shape TheS, TheFirst, TheLast;
541   Standard_Integer InitialLength = 0;
542   TheS = S;
543   TheFirst = FirstShape;
544   TheLast = LastShape;
545   if (! myFaces.IsNull()) InitialLength = myFaces->ColLength();
546
547   // there are two kinds of generation
548   //  1. generate with S from each Filler (Vertex, Edge)
549   //  2. call MakeShape recursively on the subshapes of S
550   //
551   // explode is True in the second case
552
553   // create the result empty
554
555   switch (S.ShapeType()) {
556
557   case TopAbs_VERTEX :
558     {
559       B.MakeWire(TopoDS::Wire(result));
560       break;
561     }
562           
563   case TopAbs_EDGE :
564     {
565       TopoDS_Wire W;
566       B.MakeShell(TopoDS::Shell(result));
567       B.MakeWire(W);
568       B.Add(W, S);
569       W.Closed(S.Closed());
570       TheS = W;
571       if (!FirstShape.IsNull()) {
572         B.MakeWire(W);
573         B.Add(W, FirstShape);
574         W.Closed(FirstShape.Closed());
575         TheFirst = W;
576       }
577       if (!LastShape.IsNull()) {
578         B.MakeWire(W);
579         B.Add(W, LastShape);
580         W.Closed(LastShape.Closed());
581         TheLast = W;
582       }
583       break;
584     }
585           
586   case TopAbs_WIRE :
587     B.MakeShell(TopoDS::Shell(result));
588     break;
589
590   case TopAbs_FACE :
591     {
592       B.MakeShell(TopoDS::Shell(result));
593       explode = Standard_True;
594       if ( !mySpine.Closed() && !TheFirst.IsNull()) {
595          B.Add(result, TheFirst.Reversed());
596       }
597       break;
598     }
599
600   case TopAbs_SHELL :
601     {
602       B.MakeCompSolid(TopoDS::CompSolid(result));
603       explode = Standard_True;
604       break;
605     }
606
607   case TopAbs_SOLID :
608   case TopAbs_COMPSOLID :
609     Standard_DomainError::Raise("BRepFill_Pipe::profile contains solids");
610     break;
611
612   case TopAbs_COMPOUND :
613     {
614       B.MakeCompound(TopoDS::Compound(result));
615       explode = Standard_True;
616       break;
617     }
618   default:
619     break;
620   }
621
622   if (explode) {
623     // add the subshapes
624     TopoDS_Iterator itFirst, itLast;
625     TopoDS_Shape first, last;
626     if (!TheFirst.IsNull()) itFirst.Initialize(TheFirst);
627     if (!TheLast.IsNull()) itLast.Initialize(TheLast);
628
629     for (TopoDS_Iterator it(S); it.More(); it.Next()) {
630       if (!TheFirst.IsNull()) first = itFirst.Value();
631       if (!TheLast.IsNull())  last = itLast.Value();
632       if (TheS.ShapeType() == TopAbs_FACE ) 
633         MakeShape(it.Value(), first, last);
634       else
635         B.Add(result,MakeShape(it.Value(), first, last));
636
637       if (!TheFirst.IsNull()) itFirst.Next();
638       if (!TheLast.IsNull())  itLast.Next();
639     }
640   }
641   
642   else {
643     if (TheS.ShapeType() == TopAbs_VERTEX ) {
644      Handle(BRepFill_ShapeLaw) Section = 
645         new (BRepFill_ShapeLaw) (TopoDS::Vertex(TheS));
646       BRepFill_Sweep MkSw(Section, myLoc, Standard_True);
647       MkSw.SetForceApproxC1(myForceApproxC1);
648       MkSw.Build( myReversedEdges, myTapes,
649                   BRepFill_Modified, myContinuity, GeomFill_Location, myDegmax, mySegmax );
650       result = MkSw.Shape();
651
652       Handle(TopTools_HArray2OfShape) aSections = MkSw.Sections();
653
654       if (aSections.IsNull() == Standard_False) {
655         const Standard_Integer aVLast = aSections->UpperCol();
656
657         myFirst = aSections->Value(1, 1);
658         myLast  = aSections->Value(1, aVLast);
659       }
660     }
661
662     if (TheS.ShapeType() == TopAbs_WIRE ) {
663       Handle(BRepFill_ShapeLaw) Section = 
664         new (BRepFill_ShapeLaw) (TopoDS::Wire(TheS));
665       BRepFill_Sweep MkSw(Section, myLoc, Standard_True);
666       MkSw.SetBounds(TopoDS::Wire(TheFirst), 
667                      TopoDS::Wire(TheLast));
668       MkSw.SetForceApproxC1(myForceApproxC1);
669       MkSw.Build( myReversedEdges, myTapes,
670                   BRepFill_Modified, myContinuity, GeomFill_Location, myDegmax, mySegmax );
671       result = MkSw.Shape();
672       //Correct <myFirst> and <myLast>
673       ReverseModifiedEdges(myFirst, myReversedEdges);
674       ReverseModifiedEdges(myLast, myReversedEdges);
675
676       // Labeling of elements
677       if (mySections.IsNull()) {
678         myFaces    = MkSw.SubShape();
679         mySections = MkSw.Sections();
680         myEdges    = MkSw.InterFaces();
681       }
682       else {
683         Handle(TopTools_HArray2OfShape) Aux, Somme;
684         Standard_Integer length;
685         Standard_Integer ii, jj, kk;
686
687         Aux = MkSw.SubShape();
688         length = Aux->ColLength() + myFaces->ColLength(); 
689         Somme = new (TopTools_HArray2OfShape) (1, length, 1, 
690                                                Aux->RowLength());
691         for (jj=1; jj<=myFaces->RowLength(); jj++) {
692           for (ii=1; ii<=myFaces->ColLength(); ii++)
693             Somme->SetValue(ii, jj, myFaces->Value(ii, jj));
694  
695           for (kk=1, ii=myFaces->ColLength()+1; 
696                kk <=Aux->ColLength(); kk++, ii++)
697             Somme->SetValue(ii, jj, Aux->Value(kk, jj));
698         }
699         myFaces = Somme;    
700
701         Aux = MkSw.Sections();
702         length = Aux->ColLength() + mySections->ColLength(); 
703         Somme = new (TopTools_HArray2OfShape) (1, length, 1, 
704                                                Aux->RowLength());
705         for (jj=1; jj<=mySections->RowLength(); jj++) {
706           for (ii=1; ii<=mySections->ColLength(); ii++)
707             Somme->SetValue(ii, jj, mySections->Value(ii, jj));
708
709           myCurIndexOfSectionEdge = mySections->ColLength()+1;
710           
711           for (kk=1, ii=mySections->ColLength()+1; 
712                kk <=Aux->ColLength(); kk++, ii++)
713             Somme->SetValue(ii, jj, Aux->Value(kk, jj));   
714         }
715         mySections = Somme;
716
717         Aux = MkSw.InterFaces();
718         length = Aux->ColLength() + myEdges->ColLength(); 
719         Somme = new (TopTools_HArray2OfShape) (1, length, 1, 
720                                                Aux->RowLength());
721         for (jj=1; jj<=myEdges->RowLength(); jj++) {
722           for (ii=1; ii<=myEdges->ColLength(); ii++)
723             Somme->SetValue(ii, jj, myEdges->Value(ii, jj));
724  
725           for (kk=1, ii=myEdges->ColLength()+1; 
726                kk <=Aux->ColLength(); kk++, ii++)
727             Somme->SetValue(ii, jj, Aux->Value(kk, jj));   
728         }
729
730         myEdges = Somme;
731       }
732     }
733   }
734       
735   if ( TheS.ShapeType() == TopAbs_FACE ) {
736     Standard_Integer ii, jj;
737     //jgv
738     TopExp_Explorer Explo(result, TopAbs_FACE);
739     for (; Explo.More(); Explo.Next())
740     {
741       TopoDS_Shape aFace = Explo.Current();
742       RebuildTopOrBottomFace(aFace.Reversed(), Standard_True); //top face was reversed
743     }
744     /////
745     TopoDS_Face F;
746     for (ii=InitialLength+1; ii<=myFaces->ColLength(); ii++) {
747       for (jj=1; jj<=myFaces->RowLength(); jj++) {
748         F = TopoDS::Face(myFaces->Value(ii, jj));
749         if (!F.IsNull()) B.Add(result, F);
750       }
751     }
752
753     if ( !mySpine.Closed()) {
754       // if Spine is not closed 
755       // add the last face of the solid
756
757       //jgv
758       RebuildTopOrBottomFace(TheLast, Standard_False); //bottom face
759       /////
760       B.Add(result, TopoDS::Face(TheLast));
761     }
762
763     TopoDS_Solid solid;
764     BRep_Builder BS;
765     BS.MakeSolid(solid);
766
767     result.Closed(Standard_True);
768     BS.Add(solid,TopoDS::Shell(result));
769
770     BRepClass3d_SolidClassifier SC(solid);
771     SC.PerformInfinitePoint(Precision::Confusion());
772     if ( SC.State() == TopAbs_IN) {
773       BS.MakeSolid(solid);
774       TopoDS_Shape aLocalShape = result.Reversed();
775       BS.Add(solid,TopoDS::Shell(aLocalShape));
776 //      BS.Add(solid,TopoDS::Shell(result.Reversed()));
777     }
778     return solid;
779   }
780   else {
781     return result;
782   }
783 }
784
785 //============================================================================
786 //function : FindEdge
787 //purpose  : Find the number of edge corresponding to the edge of the profile.
788 //============================================================================
789
790 Standard_Integer BRepFill_Pipe::FindEdge(const TopoDS_Shape& S,
791                                          const TopoDS_Edge& E,
792                                          Standard_Integer& InitialLength) const
793 {
794   Standard_Integer result = 0;
795
796   switch (S.ShapeType()) {
797           
798   case TopAbs_EDGE :
799     {
800       InitialLength++;
801       if (S.IsSame(E)) result = InitialLength;
802       break;
803     }
804           
805   case TopAbs_WIRE :
806     {
807       Standard_Integer ii = InitialLength+1;
808       Handle(BRepFill_ShapeLaw) Section = 
809         new (BRepFill_ShapeLaw) (TopoDS::Wire(S), Standard_False);
810       InitialLength += Section->NbLaw();
811      
812       for (; (ii<=InitialLength) && (!result); ii++) {
813         if (E.IsSame(Section->Edge(ii)) ) result = ii;
814       }
815       break;
816     }
817
818   case TopAbs_FACE :
819   case TopAbs_SHELL :
820   case TopAbs_COMPOUND :
821     {
822      for (TopoDS_Iterator it(S); it.More() && (!result); it.Next())
823        result = FindEdge(it.Value(), E, InitialLength );
824      break;
825     }
826
827   case TopAbs_SOLID :
828   case TopAbs_COMPSOLID :
829     Standard_DomainError::Raise("BRepFill_Pipe::SOLID or COMPSOLID");
830     break;
831   default:
832     break;
833   }
834
835   return result; 
836 }
837
838 //=======================================================================
839 //function : FindVertex
840 //purpose  : Find the number of edge corresponding to an edge of the profile.
841 //=======================================================================
842
843 Standard_Integer BRepFill_Pipe::FindVertex(const TopoDS_Shape& S,
844                                            const TopoDS_Vertex& V,
845                                            Standard_Integer& InitialLength) const
846 {
847   Standard_Integer result = 0;
848
849   switch (S.ShapeType()) {
850   case TopAbs_VERTEX :
851     {
852       InitialLength++;
853       if (S.IsSame(V)) result = InitialLength;
854       break;
855     }
856           
857   case TopAbs_EDGE :
858     {
859       TopoDS_Vertex VF, VL;
860       TopExp::Vertices(TopoDS::Edge(S), VF, VL);
861       if (S.Orientation() == TopAbs_REVERSED) {
862         TopoDS_Vertex aux;
863         aux = VF; VF = VL; VL = aux;
864       }
865       if (VF.IsSame(V)) result = InitialLength+1;
866       else if (VL.IsSame(V)) result = InitialLength+2;
867       InitialLength += 2;
868       break;
869     }
870           
871   case TopAbs_WIRE :
872     {
873       Standard_Integer ii = InitialLength+1;
874       Handle(BRepFill_ShapeLaw) Section = 
875         new (BRepFill_ShapeLaw) (TopoDS::Wire(S), Standard_False);
876       InitialLength += Section->NbLaw()+1;
877      
878       for (; (ii<=InitialLength) && (!result); ii++) {
879         if (V.IsSame(Section->Vertex(ii, 0.)) ) result = ii;
880       }
881       break;
882     }
883
884   case TopAbs_FACE :
885   case TopAbs_SHELL :
886   case TopAbs_COMPOUND :
887     {
888      for (TopoDS_Iterator it(S); it.More() && (!result); it.Next())
889        result = FindVertex(it.Value(), V, InitialLength);
890      break;
891     }
892
893   case TopAbs_SOLID :
894   case TopAbs_COMPSOLID :
895     Standard_DomainError::Raise("BRepFill_Pipe::SOLID or COMPSOLID");
896     break;
897   default:
898     break;
899   }
900
901   return result; 
902 }
903
904 //=======================================================================
905 //function : DefineRealSegmax
906 //purpose  : Defines the real number of segments
907 //           required in the case of bspline spine
908 //=======================================================================
909
910 void BRepFill_Pipe::DefineRealSegmax()
911 {
912   Standard_Integer RealSegmax = 0;
913
914   TopoDS_Iterator iter(mySpine);
915   for (; iter.More(); iter.Next())
916     {
917       TopoDS_Edge E = TopoDS::Edge(iter.Value());
918       Standard_Real first, last;
919       Handle(Geom_Curve) C = BRep_Tool::Curve( E, first, last );
920       if (C.IsNull())
921         continue;
922       while (C->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve) ||
923              C->DynamicType() == STANDARD_TYPE(Geom_OffsetCurve))
924         {
925           if (C->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve))
926             C = (*((Handle(Geom_TrimmedCurve)*)&C))->BasisCurve();
927           if (C->DynamicType() == STANDARD_TYPE(Geom_OffsetCurve))
928             C = (*((Handle(Geom_OffsetCurve)*)&C))->BasisCurve();
929         }
930       if (C->DynamicType() == STANDARD_TYPE(Geom_BSplineCurve))
931         {
932           const Handle(Geom_BSplineCurve)& BC = *((Handle(Geom_BSplineCurve)*)&C);
933           Standard_Integer NbKnots = BC->NbKnots();
934           Standard_Integer RealNbKnots = NbKnots;
935           if (first > BC->FirstParameter())
936             {
937               Standard_Integer I1, I2;
938               BC->LocateU( first, Precision::PConfusion(), I1, I2 );
939               RealNbKnots -= I1-1;
940             }
941           if (last < BC->LastParameter())
942             {
943               Standard_Integer I1, I2;
944               BC->LocateU( last, Precision::PConfusion(), I1, I2 );
945               RealNbKnots -= NbKnots-I2;
946             }
947           RealSegmax += RealNbKnots-1;
948         }
949     }
950
951   if (mySegmax < RealSegmax)
952     mySegmax = RealSegmax;
953 }
954
955 //=======================================================================
956 //function : RebuildTopOrBottomFace
957 //purpose  : Correct orientation of v-iso edges
958 //           according to new 3d and 2d curves taken from swept surfaces
959 //=======================================================================
960
961 void BRepFill_Pipe::RebuildTopOrBottomFace(const TopoDS_Shape& aFace,
962                                            const Standard_Boolean IsTop) const
963 {
964   Standard_Integer IndexOfSection =
965     (IsTop)? 1 : mySections->RowLength();
966
967   Standard_Integer ii;
968   BRep_Builder BB;
969   TopoDS_Iterator itf(aFace);
970   for (; itf.More(); itf.Next())
971   {
972     TopoDS_Shape aWire = itf.Value();
973     TopTools_SequenceOfShape InitEdges;
974     TopTools_SequenceOfShape ResEdges;
975     TopoDS_Iterator itw(aWire);
976     for (; itw.More(); itw.Next())
977     {
978       TopoDS_Shape anEdge = itw.Value();
979       for (ii = myCurIndexOfSectionEdge; ii <= mySections->ColLength(); ii++)
980       {
981         TopoDS_Shape aVisoEdge = mySections->Value(ii, IndexOfSection);
982         if (anEdge.IsSame(aVisoEdge))
983         {
984           InitEdges.Append(anEdge);
985           ResEdges.Append(aVisoEdge);
986           break;
987         }
988       }
989     }
990     aWire.Free(Standard_True);
991     for (ii = 1; ii <= InitEdges.Length(); ii++)
992     {
993       BB.Remove(aWire, InitEdges(ii));
994       UpdateTolFromTopOrBottomPCurve(TopoDS::Face(aFace), TopoDS::Edge(ResEdges(ii)));
995       BB.Add(aWire, ResEdges(ii));
996     }
997   }
998 }