0024023: Revamp the OCCT Handle -- ambiguity
[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
268   fila.Multiply(myTrsf);
269   TopLoc_Location LocFirst(fila);
270   myFirst = myProfile;
271   if ( ! LocFirst.IsIdentity()) {
272     //myFirst.Location( LocFirst.Multiplied(myProfile.Location()) );
273     myFirst = BRepBuilderAPI_Transform(myProfile, fila, Standard_True); //copy
274   }
275
276   ShapeUpgrade_RemoveLocations RemLoc;
277   RemLoc.SetRemoveLevel(TopAbs_COMPOUND);
278   RemLoc.Remove(myFirst);
279   myFirst = RemLoc.GetResult();
280   
281   myLoc->Law(myLoc->NbLaw())->GetDomain(first, last);
282   myLoc->Law(myLoc->NbLaw())->D0(last,M, V);
283 //    try { // Not good, but there are no other means to test SetValues
284   fila.SetValues(M(1,1), M(1,2), M(1,3), V.X(),
285                  M(2,1), M(2,2), M(2,3), V.Y(),
286                  M(3,1), M(3,2), M(3,3), V.Z());
287   fila.Multiply(myTrsf);
288   TopLoc_Location LocLast(fila);
289   if (! myLoc->IsClosed() || LocFirst != LocLast) {
290     myLast = myProfile;
291     if ( ! LocLast.IsIdentity()) {
292       //myLast.Location(LocLast.Multiplied(myProfile.Location()) );
293       myLast = BRepBuilderAPI_Transform(myProfile, fila, Standard_True); //copy
294     }
295   }
296   else {
297     myLast = myFirst;
298   }
299
300   RemLoc.Remove(myLast);
301   myLast = RemLoc.GetResult();
302   
303 #if DRAW
304   if (Affich) {
305     DBRep::Set("theprof",  TheProf);
306     DBRep::Set("thefirst", myFirst);
307     DBRep::Set("thelast" , myLast);
308  }
309 #endif
310  
311   myShape = MakeShape(TheProf, myFirst, myLast);
312 }
313
314
315 //=======================================================================
316 //function : Spine
317 //purpose  : 
318 //=======================================================================
319
320 const TopoDS_Shape& BRepFill_Pipe::Spine() const 
321 {
322   return mySpine;
323 }
324
325 //=======================================================================
326 //function : Profile
327 //purpose  : 
328 //=======================================================================
329
330 const TopoDS_Shape& BRepFill_Pipe::Profile() const 
331 {
332   return myProfile;
333 }
334
335 //=======================================================================
336 //function : Shape
337 //purpose  : 
338 //=======================================================================
339
340 const TopoDS_Shape& BRepFill_Pipe::Shape() const 
341 {
342   return myShape;
343 }
344
345 //=======================================================================
346 //function : ErrorOnSurface
347 //purpose  : 
348 //=======================================================================
349
350 Standard_Real BRepFill_Pipe::ErrorOnSurface() const 
351 {
352   return myErrorOnSurf;
353 }
354
355
356 //=======================================================================
357 //function : FirstShape
358 //purpose  : 
359 //=======================================================================
360
361 const TopoDS_Shape& BRepFill_Pipe::FirstShape() const 
362 {
363   return myFirst;
364 }
365
366
367 //=======================================================================
368 //function : LastShape
369 //purpose  : 
370 //=======================================================================
371
372 const TopoDS_Shape& BRepFill_Pipe::LastShape() const 
373 {
374   return myLast;
375 }
376
377
378 //=======================================================================
379 //function : Face
380 //purpose  : 
381 //=======================================================================
382
383 TopoDS_Face BRepFill_Pipe::Face(const TopoDS_Edge& ESpine,
384                                 const TopoDS_Edge& EProfile)
385 {
386   TopoDS_Face theFace;
387
388   if ( BRep_Tool::Degenerated(EProfile))
389     return theFace;
390
391   Standard_Integer ii, ispin = 0, iprof = 0, count = 0;
392
393   // *************************************************
394   // Search if EProfile is an edge of myProfile
395   // *************************************************
396   iprof = FindEdge(myProfile, EProfile, count);
397
398   if (!iprof) Standard_DomainError::Raise(
399               "BRepFill_Pipe::Face : Edge not in the Profile");
400
401
402   // *************************************************
403   // Search if ESpine  is an edge of mySpine and find 
404   // the index of the corresponding Filler
405   // *************************************************
406  for (ii=1; ii<=myLoc->NbLaw() && (!ispin); ii++) 
407     if  (ESpine.IsSame(myLoc->Edge(ii))) ispin = ii;
408
409   if (!ispin) Standard_DomainError::Raise(
410     "BRepFill_Pipe::Edge  : Edge not in the Spine");
411
412   theFace = TopoDS::Face(myFaces->Value(iprof, ispin));
413   return theFace;
414
415 }
416
417 //=======================================================================
418 //function : Edge
419 //purpose  : 
420 //=======================================================================
421 TopoDS_Edge BRepFill_Pipe::Edge(const TopoDS_Edge&   ESpine,
422                                 const TopoDS_Vertex& VProfile)
423 {
424   Standard_Integer ii, ispin = 0, iprof = 0, count = 0;;
425
426   // *************************************************
427   // Search if VProfile is a Vertex of myProfile
428   // *************************************************
429   iprof = FindVertex(myProfile, VProfile, count);
430   if (!iprof) Standard_DomainError::Raise(
431         "BRepFill_Pipe::Edge : Vertex not in the Profile");
432
433
434   // *************************************************
435   // Search if ESpine  is an edge of mySpine and find 
436   // the index of the corresponding Filler
437   // *************************************************
438    
439   for (ii=1; ii<=myLoc->NbLaw() && (!ispin); ii++) 
440     if  (ESpine.IsSame(myLoc->Edge(ii))) ispin = ii;
441
442   if (!ispin) Standard_DomainError::Raise(
443     "BRepFill_Pipe::Edge  : Edge not in the Spine");
444
445
446   // *************************************************
447   // Generate the corresponding Shape
448   // *************************************************  
449   TopoDS_Edge theEdge;
450   theEdge = TopoDS::Edge(myEdges->Value(iprof, ispin));
451
452   return theEdge;
453
454 }
455
456
457 //=======================================================================
458 //function : Section
459 //purpose  : 
460 //=======================================================================
461
462 TopoDS_Shape BRepFill_Pipe::Section(const TopoDS_Vertex& VSpine) const 
463 {
464   TopoDS_Iterator it, itv;
465
466   Standard_Integer ii, ispin = 0;
467
468   TopoDS_Shape curSect = myProfile;
469
470   // *************************************************
471   // Search if ESpine  is an edge of mySpine and find 
472   // the index of the corresponding Filler
473   // *************************************************
474
475   // iterate on all the edges of mySpine
476  for (ii=1; ii<=myLoc->NbLaw()+1 && (!ispin); ii++) 
477     if  (VSpine.IsSame(myLoc->Vertex(ii))) ispin = ii;
478
479   if (!ispin) Standard_DomainError::Raise(
480     "BRepFill_Pipe::Section  : Vertex not in the Spine");
481
482   BRep_Builder B;
483   TopoDS_Compound Comp; 
484   B.MakeCompound(Comp);
485   for (ii=1; ii<=mySections->ColLength(); ii++)
486     B.Add(Comp, mySections->Value(ii, ispin));
487   
488   return Comp;
489 }
490
491 //=======================================================================
492 //function : PipeLine
493 //purpose  : Construct a wire by sweeping of a point
494 //=======================================================================
495
496 TopoDS_Wire BRepFill_Pipe::PipeLine(const gp_Pnt& Point)
497 {
498  // Postioning 
499  gp_Pnt P;
500  P = Point;
501  P.Transform(myTrsf);
502
503  BRepLib_MakeVertex MkV(P); 
504  Handle(BRepFill_ShapeLaw) Section = 
505         new (BRepFill_ShapeLaw) (MkV.Vertex());
506
507  // Sweeping
508  BRepFill_Sweep MkSw(Section, myLoc, Standard_True);
509  MkSw.SetForceApproxC1(myForceApproxC1);
510  MkSw.Build( myReversedEdges, myTapes, myRails,
511              BRepFill_Modified, myContinuity, GeomFill_Location, myDegmax, mySegmax );
512  TopoDS_Shape aLocalShape = MkSw.Shape();
513  myErrorOnSurf = MkSw.ErrorOnSurface();
514  return TopoDS::Wire(aLocalShape);
515 // return TopoDS::Wire(MkSw.Shape());
516 }
517
518 //=======================================================================
519 //function : MakeShape
520 //purpose  : 
521 //=======================================================================
522
523 TopoDS_Shape BRepFill_Pipe::MakeShape(const TopoDS_Shape& S,
524                                       const TopoDS_Shape& FirstShape,
525                                       const TopoDS_Shape& LastShape)
526 {
527   TopoDS_Shape result;
528   BRep_Builder B;
529   Standard_Boolean explode = Standard_False; 
530   TopoDS_Shape TheS, TheFirst, TheLast;
531   Standard_Integer InitialLength = 0;
532   TheS = S;
533   TheFirst = FirstShape;
534   TheLast = LastShape;
535   if (! myFaces.IsNull()) InitialLength = myFaces->ColLength();
536
537   // there are two kinds of generation
538   //  1. generate with S from each Filler (Vertex, Edge)
539   //  2. call MakeShape recursively on the subshapes of S
540   //
541   // explode is True in the second case
542
543   // create the result empty
544
545   switch (S.ShapeType()) {
546
547   case TopAbs_VERTEX :
548     {
549       B.MakeWire(TopoDS::Wire(result));
550       break;
551     }
552           
553   case TopAbs_EDGE :
554     {
555       TopoDS_Wire W;
556       B.MakeShell(TopoDS::Shell(result));
557       B.MakeWire(W);
558       B.Add(W, S);
559       W.Closed(BRep_Tool::IsClosed(S));
560       TheS = W;
561       if (!FirstShape.IsNull()) {
562         B.MakeWire(W);
563         B.Add(W, FirstShape);
564         W.Closed(BRep_Tool::IsClosed(FirstShape));
565         TheFirst = W;
566       }
567       if (!LastShape.IsNull()) {
568         B.MakeWire(W);
569         B.Add(W, LastShape);
570         W.Closed(BRep_Tool::IsClosed(LastShape));
571         TheLast = W;
572       }
573       result.Closed (BRep_Tool::IsClosed (result));
574       break;
575     }
576           
577   case TopAbs_WIRE :
578     B.MakeShell(TopoDS::Shell(result));
579     break;
580
581   case TopAbs_FACE :
582     {
583       B.MakeShell(TopoDS::Shell(result));
584       explode = Standard_True;
585       if ( !mySpine.Closed() && !TheFirst.IsNull()) {
586          B.Add(result, TheFirst.Reversed());
587       }
588       result.Closed (BRep_Tool::IsClosed (result));
589       break;
590     }
591
592   case TopAbs_SHELL :
593     {
594       B.MakeCompSolid(TopoDS::CompSolid(result));
595       explode = Standard_True;
596       break;
597     }
598
599   case TopAbs_SOLID :
600   case TopAbs_COMPSOLID :
601     Standard_DomainError::Raise("BRepFill_Pipe::profile contains solids");
602     break;
603
604   case TopAbs_COMPOUND :
605     {
606       B.MakeCompound(TopoDS::Compound(result));
607       explode = Standard_True;
608       break;
609     }
610   default:
611     break;
612   }
613
614   if (explode) {
615     // add the subshapes
616     TopoDS_Iterator itFirst, itLast;
617     TopoDS_Shape first, last;
618     if (!TheFirst.IsNull()) itFirst.Initialize(TheFirst);
619     if (!TheLast.IsNull()) itLast.Initialize(TheLast);
620
621     for (TopoDS_Iterator it(S); it.More(); it.Next()) {
622       if (!TheFirst.IsNull()) first = itFirst.Value();
623       if (!TheLast.IsNull())  last = itLast.Value();
624       if (TheS.ShapeType() == TopAbs_FACE ) 
625         MakeShape(it.Value(), first, last);
626       else
627         B.Add(result,MakeShape(it.Value(), first, last));
628
629       if (!TheFirst.IsNull()) itFirst.Next();
630       if (!TheLast.IsNull())  itLast.Next();
631     }
632   }
633   
634   else {
635     if (TheS.ShapeType() == TopAbs_VERTEX ) {
636      Handle(BRepFill_ShapeLaw) Section = 
637         new (BRepFill_ShapeLaw) (TopoDS::Vertex(TheS));
638       BRepFill_Sweep MkSw(Section, myLoc, Standard_True);
639       MkSw.SetForceApproxC1(myForceApproxC1);
640       MkSw.Build( myReversedEdges, myTapes, myRails,
641                   BRepFill_Modified, myContinuity, GeomFill_Location, myDegmax, mySegmax );
642       result = MkSw.Shape();
643       myErrorOnSurf = MkSw.ErrorOnSurface();
644
645       Handle(TopTools_HArray2OfShape) aSections = MkSw.Sections();
646
647       if (aSections.IsNull() == Standard_False) {
648         const Standard_Integer aVLast = aSections->UpperCol();
649
650         myFirst = aSections->Value(1, 1);
651         myLast  = aSections->Value(1, aVLast);
652       }
653     }
654
655     if (TheS.ShapeType() == TopAbs_WIRE ) {
656       Handle(BRepFill_ShapeLaw) Section = 
657         new (BRepFill_ShapeLaw) (TopoDS::Wire(TheS));
658       BRepFill_Sweep MkSw(Section, myLoc, Standard_True);
659       MkSw.SetBounds(TopoDS::Wire(TheFirst), 
660                      TopoDS::Wire(TheLast));
661       MkSw.SetForceApproxC1(myForceApproxC1);
662       MkSw.Build( myReversedEdges, myTapes, myRails,
663                   BRepFill_Modified, myContinuity, GeomFill_Location, myDegmax, mySegmax );
664       result = MkSw.Shape();
665       myErrorOnSurf = MkSw.ErrorOnSurface();
666       //Correct <myFirst> and <myLast>
667       ReverseModifiedEdges(myFirst, myReversedEdges);
668       ReverseModifiedEdges(myLast, myReversedEdges);
669
670       // Labeling of elements
671       if (mySections.IsNull()) {
672         myFaces    = MkSw.SubShape();
673         mySections = MkSw.Sections();
674         myEdges    = MkSw.InterFaces();
675       }
676       else {
677         Handle(TopTools_HArray2OfShape) Aux, Somme;
678         Standard_Integer length;
679         Standard_Integer ii, jj, kk;
680
681         Aux = MkSw.SubShape();
682         length = Aux->ColLength() + myFaces->ColLength(); 
683         Somme = new (TopTools_HArray2OfShape) (1, length, 1, 
684                                                Aux->RowLength());
685         for (jj=1; jj<=myFaces->RowLength(); jj++) {
686           for (ii=1; ii<=myFaces->ColLength(); ii++)
687             Somme->SetValue(ii, jj, myFaces->Value(ii, jj));
688  
689           for (kk=1, ii=myFaces->ColLength()+1; 
690                kk <=Aux->ColLength(); kk++, ii++)
691             Somme->SetValue(ii, jj, Aux->Value(kk, jj));
692         }
693         myFaces = Somme;    
694
695         Aux = MkSw.Sections();
696         length = Aux->ColLength() + mySections->ColLength(); 
697         Somme = new (TopTools_HArray2OfShape) (1, length, 1, 
698                                                Aux->RowLength());
699         for (jj=1; jj<=mySections->RowLength(); jj++) {
700           for (ii=1; ii<=mySections->ColLength(); ii++)
701             Somme->SetValue(ii, jj, mySections->Value(ii, jj));
702
703           myCurIndexOfSectionEdge = mySections->ColLength()+1;
704           
705           for (kk=1, ii=mySections->ColLength()+1; 
706                kk <=Aux->ColLength(); kk++, ii++)
707             Somme->SetValue(ii, jj, Aux->Value(kk, jj));   
708         }
709         mySections = Somme;
710
711         Aux = MkSw.InterFaces();
712         length = Aux->ColLength() + myEdges->ColLength(); 
713         Somme = new (TopTools_HArray2OfShape) (1, length, 1, 
714                                                Aux->RowLength());
715         for (jj=1; jj<=myEdges->RowLength(); jj++) {
716           for (ii=1; ii<=myEdges->ColLength(); ii++)
717             Somme->SetValue(ii, jj, myEdges->Value(ii, jj));
718  
719           for (kk=1, ii=myEdges->ColLength()+1; 
720                kk <=Aux->ColLength(); kk++, ii++)
721             Somme->SetValue(ii, jj, Aux->Value(kk, jj));   
722         }
723
724         myEdges = Somme;
725       }
726     }
727   }
728       
729   if ( TheS.ShapeType() == TopAbs_FACE ) {
730     Standard_Integer ii, jj;
731     //jgv
732     TopExp_Explorer Explo(result, TopAbs_FACE);
733     for (; Explo.More(); Explo.Next())
734     {
735       TopoDS_Shape aFace = Explo.Current();
736       RebuildTopOrBottomFace(aFace.Reversed(), Standard_True); //top face was reversed
737     }
738     /////
739     TopoDS_Face F;
740     for (ii=InitialLength+1; ii<=myFaces->ColLength(); ii++) {
741       for (jj=1; jj<=myFaces->RowLength(); jj++) {
742         F = TopoDS::Face(myFaces->Value(ii, jj));
743         if (!F.IsNull()) B.Add(result, F);
744       }
745     }
746
747     if ( !mySpine.Closed()) {
748       // if Spine is not closed 
749       // add the last face of the solid
750
751       //jgv
752       RebuildTopOrBottomFace(TheLast, Standard_False); //bottom face
753       /////
754       B.Add(result, TopoDS::Face(TheLast));
755     }
756
757     TopoDS_Solid solid;
758     BRep_Builder BS;
759     BS.MakeSolid(solid);
760
761     result.Closed(Standard_True);
762     BS.Add(solid,TopoDS::Shell(result));
763
764     BRepClass3d_SolidClassifier SC(solid);
765     SC.PerformInfinitePoint(Precision::Confusion());
766     if ( SC.State() == TopAbs_IN) {
767       BS.MakeSolid(solid);
768       TopoDS_Shape aLocalShape = result.Reversed();
769       BS.Add(solid,TopoDS::Shell(aLocalShape));
770 //      BS.Add(solid,TopoDS::Shell(result.Reversed()));
771     }
772     return solid;
773   }
774   else {
775     return result;
776   }
777 }
778
779 //============================================================================
780 //function : FindEdge
781 //purpose  : Find the number of edge corresponding to the edge of the profile.
782 //============================================================================
783
784 Standard_Integer BRepFill_Pipe::FindEdge(const TopoDS_Shape& S,
785                                          const TopoDS_Edge& E,
786                                          Standard_Integer& InitialLength) const
787 {
788   Standard_Integer result = 0;
789
790   switch (S.ShapeType()) {
791           
792   case TopAbs_EDGE :
793     {
794       InitialLength++;
795       if (S.IsSame(E)) result = InitialLength;
796       break;
797     }
798           
799   case TopAbs_WIRE :
800     {
801       Standard_Integer ii = InitialLength+1;
802       Handle(BRepFill_ShapeLaw) Section = 
803         new (BRepFill_ShapeLaw) (TopoDS::Wire(S), Standard_False);
804       InitialLength += Section->NbLaw();
805      
806       for (; (ii<=InitialLength) && (!result); ii++) {
807         if (E.IsSame(Section->Edge(ii)) ) result = ii;
808       }
809       break;
810     }
811
812   case TopAbs_FACE :
813   case TopAbs_SHELL :
814   case TopAbs_COMPOUND :
815     {
816      for (TopoDS_Iterator it(S); it.More() && (!result); it.Next())
817        result = FindEdge(it.Value(), E, InitialLength );
818      break;
819     }
820
821   case TopAbs_SOLID :
822   case TopAbs_COMPSOLID :
823     Standard_DomainError::Raise("BRepFill_Pipe::SOLID or COMPSOLID");
824     break;
825   default:
826     break;
827   }
828
829   return result; 
830 }
831
832 //=======================================================================
833 //function : FindVertex
834 //purpose  : Find the number of edge corresponding to an edge of the profile.
835 //=======================================================================
836
837 Standard_Integer BRepFill_Pipe::FindVertex(const TopoDS_Shape& S,
838                                            const TopoDS_Vertex& V,
839                                            Standard_Integer& InitialLength) const
840 {
841   Standard_Integer result = 0;
842
843   switch (S.ShapeType()) {
844   case TopAbs_VERTEX :
845     {
846       InitialLength++;
847       if (S.IsSame(V)) result = InitialLength;
848       break;
849     }
850           
851   case TopAbs_EDGE :
852     {
853       TopoDS_Vertex VF, VL;
854       TopExp::Vertices(TopoDS::Edge(S), VF, VL);
855       if (S.Orientation() == TopAbs_REVERSED) {
856         TopoDS_Vertex aux;
857         aux = VF; VF = VL; VL = aux;
858       }
859       if (VF.IsSame(V)) result = InitialLength+1;
860       else if (VL.IsSame(V)) result = InitialLength+2;
861       InitialLength += 2;
862       break;
863     }
864           
865   case TopAbs_WIRE :
866     {
867       Standard_Integer ii = InitialLength+1;
868       Handle(BRepFill_ShapeLaw) Section = 
869         new (BRepFill_ShapeLaw) (TopoDS::Wire(S), Standard_False);
870       InitialLength += Section->NbLaw()+1;
871      
872       for (; (ii<=InitialLength) && (!result); ii++) {
873         if (V.IsSame(Section->Vertex(ii, 0.)) ) result = ii;
874       }
875       break;
876     }
877
878   case TopAbs_FACE :
879   case TopAbs_SHELL :
880   case TopAbs_COMPOUND :
881     {
882      for (TopoDS_Iterator it(S); it.More() && (!result); it.Next())
883        result = FindVertex(it.Value(), V, InitialLength);
884      break;
885     }
886
887   case TopAbs_SOLID :
888   case TopAbs_COMPSOLID :
889     Standard_DomainError::Raise("BRepFill_Pipe::SOLID or COMPSOLID");
890     break;
891   default:
892     break;
893   }
894
895   return result; 
896 }
897
898 //=======================================================================
899 //function : DefineRealSegmax
900 //purpose  : Defines the real number of segments
901 //           required in the case of bspline spine
902 //=======================================================================
903
904 void BRepFill_Pipe::DefineRealSegmax()
905 {
906   Standard_Integer RealSegmax = 0;
907
908   TopoDS_Iterator iter(mySpine);
909   for (; iter.More(); iter.Next())
910     {
911       TopoDS_Edge E = TopoDS::Edge(iter.Value());
912       Standard_Real first, last;
913       Handle(Geom_Curve) C = BRep_Tool::Curve( E, first, last );
914       if (C.IsNull())
915         continue;
916       while (C->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve) ||
917              C->DynamicType() == STANDARD_TYPE(Geom_OffsetCurve))
918         {
919           if (C->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve))
920             C = Handle(Geom_TrimmedCurve)::DownCast (C)->BasisCurve();
921           if (C->DynamicType() == STANDARD_TYPE(Geom_OffsetCurve))
922             C = Handle(Geom_OffsetCurve)::DownCast (C)->BasisCurve();
923         }
924       if (C->DynamicType() == STANDARD_TYPE(Geom_BSplineCurve))
925         {
926           Handle(Geom_BSplineCurve) BC (Handle(Geom_BSplineCurve)::DownCast (C));
927           Standard_Integer NbKnots = BC->NbKnots();
928           Standard_Integer RealNbKnots = NbKnots;
929           if (first > BC->FirstParameter())
930             {
931               Standard_Integer I1, I2;
932               BC->LocateU( first, Precision::PConfusion(), I1, I2 );
933               RealNbKnots -= I1-1;
934             }
935           if (last < BC->LastParameter())
936             {
937               Standard_Integer I1, I2;
938               BC->LocateU( last, Precision::PConfusion(), I1, I2 );
939               RealNbKnots -= NbKnots-I2;
940             }
941           RealSegmax += RealNbKnots-1;
942         }
943     }
944
945   if (mySegmax < RealSegmax)
946     mySegmax = RealSegmax;
947 }
948
949 //=======================================================================
950 //function : RebuildTopOrBottomFace
951 //purpose  : Correct orientation of v-iso edges
952 //           according to new 3d and 2d curves taken from swept surfaces
953 //=======================================================================
954
955 void BRepFill_Pipe::RebuildTopOrBottomFace(const TopoDS_Shape& aFace,
956                                            const Standard_Boolean IsTop) const
957 {
958   Standard_Integer IndexOfSection =
959     (IsTop)? 1 : mySections->RowLength();
960
961   Standard_Integer ii;
962   BRep_Builder BB;
963   TopoDS_Iterator itf(aFace);
964   for (; itf.More(); itf.Next())
965   {
966     TopoDS_Shape aWire = itf.Value();
967     TopTools_SequenceOfShape InitEdges;
968     TopTools_SequenceOfShape ResEdges;
969     TopoDS_Iterator itw(aWire);
970     for (; itw.More(); itw.Next())
971     {
972       TopoDS_Shape anEdge = itw.Value();
973       for (ii = myCurIndexOfSectionEdge; ii <= mySections->ColLength(); ii++)
974       {
975         TopoDS_Shape aVisoEdge = mySections->Value(ii, IndexOfSection);
976         if (anEdge.IsSame(aVisoEdge))
977         {
978           InitEdges.Append(anEdge);
979           ResEdges.Append(aVisoEdge);
980           break;
981         }
982       }
983     }
984     aWire.Free(Standard_True);
985     for (ii = 1; ii <= InitEdges.Length(); ii++)
986     {
987       BB.Remove(aWire, InitEdges(ii));
988       UpdateTolFromTopOrBottomPCurve(TopoDS::Face(aFace), TopoDS::Edge(ResEdges(ii)));
989       BB.Add(aWire, ResEdges(ii));
990     }
991   }
992 }