]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMImpl/GEOMImpl_PipeDriver.cxx
Salome HOME
NPAL14856: Get The Normal of a Face.
[modules/geom.git] / src / GEOMImpl / GEOMImpl_PipeDriver.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20
21 #include <Standard_Stream.hxx>
22
23 #include <GEOMImpl_PipeDriver.hxx>
24
25 #include <GEOMImpl_IShapesOperations.hxx>
26 #include <GEOMImpl_IPipeDiffSect.hxx>
27 #include <GEOMImpl_IPipeShellSect.hxx>
28 #include <GEOMImpl_IPipe.hxx>
29 #include <GEOMImpl_Types.hxx>
30 #include <GEOM_Function.hxx>
31
32 #include <ShapeAnalysis_FreeBounds.hxx>
33 #include <ShapeAnalysis_Edge.hxx>
34 #include <ShapeFix_Face.hxx>
35 #include <ShapeFix_Shell.hxx>
36
37 #include <BRep_Tool.hxx>
38 #include <BRep_Builder.hxx>
39 #include <BRepBuilderAPI_MakeWire.hxx>
40 #include <BRepBuilderAPI_Sewing.hxx>
41 #include <BRepCheck_Analyzer.hxx>
42 #include <BRepOffsetAPI_MakePipe.hxx>
43 #include <BRepOffsetAPI_MakePipeShell.hxx>
44 #include <GProp_GProps.hxx>
45 #include <BRepGProp.hxx>
46 #include <BRepBuilderAPI_MakeFace.hxx>
47
48 #include <TopAbs.hxx>
49 #include <TopExp.hxx>
50 #include <TopExp_Explorer.hxx>
51 #include <TopoDS.hxx>
52 #include <TopoDS_Wire.hxx>
53 #include <TopoDS_Edge.hxx>
54 #include <TopoDS_Shape.hxx>
55 #include <TopoDS_Solid.hxx>
56 #include <TopoDS_Shell.hxx>
57 #include <TopoDS_Face.hxx>
58 #include <TopoDS_Compound.hxx>
59 #include <TopTools_SequenceOfShape.hxx>
60 #include <TopTools_IndexedDataMapOfShapeShape.hxx>
61 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
62 #include <TopTools_ListIteratorOfListOfShape.hxx>
63
64 #include <GeomAPI_ProjectPointOnCurve.hxx>
65 #include <GeomAPI_Interpolate.hxx>
66 #include <Geom_TrimmedCurve.hxx>
67 #include <Geom_Plane.hxx>
68 #include <Geom_RectangularTrimmedSurface.hxx>
69 #include <Geom_BezierSurface.hxx>
70 #include <Geom_Line.hxx>
71 #include <Geom_Conic.hxx>
72 #include <Geom_BSplineCurve.hxx>
73 #include <Geom_BSplineSurface.hxx>
74 #include <GeomFill_BSplineCurves.hxx>
75 #include <GeomConvert_ApproxCurve.hxx>
76 #include <GeomConvert.hxx>
77
78 #include <TColgp_SequenceOfPnt.hxx>
79 #include <TColgp_HArray1OfPnt.hxx>
80 #include <TColgp_Array2OfPnt.hxx>
81 #include <TColStd_HSequenceOfTransient.hxx>
82
83 #include <Precision.hxx>
84 #include <Standard_NullObject.hxx>
85 #include <Standard_TypeMismatch.hxx>
86 #include <Standard_ConstructionError.hxx>
87
88 #include "utilities.h"
89
90 //#include "BRepTools.hxx"
91 //#include "GeomTools.hxx"
92 #include <GEOMAlgo_GlueAnalyser.hxx>
93
94
95 //=======================================================================
96 //function : GetID
97 //purpose  :
98 //=======================================================================
99 const Standard_GUID& GEOMImpl_PipeDriver::GetID()
100 {
101   static Standard_GUID aPipeDriver("FF1BBB19-5D14-4df2-980B-3A668264EA16");
102   return aPipeDriver;
103 }
104
105
106 //=======================================================================
107 //function : GEOMImpl_PipeDriver
108 //purpose  :
109 //=======================================================================
110 GEOMImpl_PipeDriver::GEOMImpl_PipeDriver()
111 {
112 }
113
114
115 //=======================================================================
116 //function : FillForOtherEdges
117 //purpose  : auxilary for CreatePipeForShellSections()
118 //=======================================================================
119 static bool FillForOtherEdges(const TopoDS_Shape& F1,
120                               const TopoDS_Shape& E1,
121                               const TopoDS_Shape& V1,
122                               TopTools_IndexedDataMapOfShapeShape& FF)
123 {
124   //cout<<"FillForOtherEdges"<<endl;
125   // find other pairs for vertexes and edges
126   // creating map of vertex edges for both faces
127   TopTools_IndexedDataMapOfShapeListOfShape aMapVertEdge1;
128   TopExp::MapShapesAndAncestors(F1, TopAbs_VERTEX, TopAbs_EDGE, aMapVertEdge1);
129   if(!FF.Contains(F1))
130     cout<<"    FillForOtherEdges: map FF not contains key F1"<<endl;
131   if(!FF.Contains(E1))
132     cout<<"    FillForOtherEdges: map FF not contains key E1"<<endl;
133   if(!FF.Contains(V1))
134     cout<<"    FillForOtherEdges: map FF not contains key V1"<<endl;
135   const TopoDS_Shape& F2 = FF.FindFromKey(F1);
136   const TopoDS_Shape& E2 = FF.FindFromKey(E1);
137   const TopoDS_Shape& V2 = FF.FindFromKey(V1);
138   TopTools_IndexedDataMapOfShapeListOfShape aMapVertEdge2;
139   TopExp::MapShapesAndAncestors(F2, TopAbs_VERTEX, TopAbs_EDGE, aMapVertEdge2);
140
141   TopoDS_Edge ES1 = TopoDS::Edge(E1);
142   TopoDS_Edge ES2 = TopoDS::Edge(E2);
143   TopoDS_Shape VS1 = V1;
144   TopoDS_Shape VS2 = V2;
145
146   ShapeAnalysis_Edge sae;
147   while(1) {
148     if(!aMapVertEdge1.Contains(VS1))
149       cout<<"    FillForOtherEdges: map aMapVertEdge1 not contains key VS1"<<endl;
150     const TopTools_ListOfShape& aList1 = aMapVertEdge1.FindFromKey(VS1);
151     //TopoDS_Shape E1next;
152     TopTools_ListIteratorOfListOfShape anIter1(aList1);
153     if(anIter1.Value().IsSame(ES1)) {
154       anIter1.Next();
155     }
156     //E1next = anIter1.Value();
157     if(!aMapVertEdge2.Contains(VS2))
158       cout<<"    FillForOtherEdges: map aMapVertEdge2 not contains key VS2"<<endl;
159     const TopTools_ListOfShape& aList2 = aMapVertEdge2.FindFromKey(VS2);
160     //TopoDS_Shape E2next;
161     TopTools_ListIteratorOfListOfShape anIter2(aList2);
162     if(anIter2.Value().IsSame(ES2)) {
163       anIter2.Next();
164     }
165     //E2next = anIter2.Value();
166     //ES1 = TopoDS::Edge(E1next);
167     //ES2 = TopoDS::Edge(E2next);
168     ES1 = TopoDS::Edge(anIter1.Value());
169     ES2 = TopoDS::Edge(anIter2.Value());
170     if(!FF.Contains(ES1)) {
171       FF.Add(ES1,ES2);
172     }
173     if(VS1.IsSame(sae.FirstVertex(ES1)))
174       VS1 = sae.LastVertex(ES1);
175     else
176       VS1 = sae.FirstVertex(ES1);
177     if(VS2.IsSame(sae.FirstVertex(ES2)))
178       VS2 = sae.LastVertex(ES2);
179     else
180       VS2 = sae.FirstVertex(ES2);
181     if(VS1.IsSame(V1))
182       break;
183     if(!FF.Contains(VS1)) {
184       FF.Add(VS1,VS2);
185     }
186   }
187
188   return true;
189 }
190
191
192 //=======================================================================
193 //function : FillCorrespondingEdges
194 //purpose  : auxilary for CreatePipeForShellSections()
195 //=======================================================================
196 static bool FillCorrespondingEdges(const TopoDS_Shape& FS1,
197                                    const TopoDS_Shape& FS2,
198                                    const TopoDS_Vertex& aLoc1,
199                                    const TopoDS_Vertex& aLoc2,
200                                    const TopoDS_Wire& aWirePath,
201                                    TopTools_IndexedDataMapOfShapeShape& FF)
202 {
203   //cout<<"FillCorrespondingEdges"<<endl;
204   // find corresponding edges
205   TopExp_Explorer expw1(FS1,TopAbs_WIRE);
206   TopoDS_Wire aWire1 = TopoDS::Wire(expw1.Current());
207   //exp = TopExp_Explorer(FS2,TopAbs_WIRE);
208   TopExp_Explorer expw2(FS2,TopAbs_WIRE);
209   TopoDS_Wire aWire2 = TopoDS::Wire(expw2.Current());
210   BRepOffsetAPI_MakePipeShell aBuilder(aWirePath);
211   aBuilder.Add(aWire1, aLoc1);
212   aBuilder.Add(aWire2, aLoc2);
213   if(!aBuilder.IsReady()) {
214     return false;
215   }
216   aBuilder.Build();
217   TopoDS_Shape aShape = aBuilder.Shape();
218   /*
219   TopoDS_Compound C;
220   BRep_Builder B;
221   B.MakeCompound(C);
222   B.Add(C,aShape);
223   B.Add(C,FS1);
224   B.Add(C,FS2);
225   BRepTools::Write(C,"/dn02/users_Linux/skl/work/Bugs/14857/comp.brep");
226   */
227   ShapeAnalysis_Edge sae;
228   double tol = Max( BRep_Tool::Tolerance(TopoDS::Face(FS1)),
229                     BRep_Tool::Tolerance(TopoDS::Face(FS2)) );
230   TopTools_MapOfShape Vs1,Vs2;
231   TopExp_Explorer exp;
232   exp.Init( FS1, TopAbs_EDGE );
233   TopoDS_Edge E1 = TopoDS::Edge(exp.Current());
234   TopoDS_Vertex V11 = sae.FirstVertex(E1);
235   TopoDS_Vertex V21 = sae.LastVertex(E1);
236   gp_Pnt P11 = BRep_Tool::Pnt(V11);
237   gp_Pnt P21 = BRep_Tool::Pnt(V21);
238   //cout<<"P11("<<P11.X()<<","<<P11.Y()<<","<<P11.Z()<<")"<<endl;
239   //cout<<"P21("<<P21.X()<<","<<P21.Y()<<","<<P21.Z()<<")"<<endl;
240   // find corresponding vertexes from created shape
241   TopoDS_Vertex VN11,VN21;
242   for( exp.Init( aShape, TopAbs_VERTEX ); exp.More(); exp.Next() ) {
243     TopoDS_Vertex V = TopoDS::Vertex(exp.Current());
244     gp_Pnt P = BRep_Tool::Pnt(V);
245     if(P.Distance(P11)<tol) {
246       VN11 = V;
247     }
248     if(P.Distance(P21)<tol) {
249       VN21 = V;
250     }
251   }
252   // find edge contains VN11 and VN21 and corresponding vertexes
253   TopoDS_Vertex VN12,VN22;
254   for( exp.Init( aShape, TopAbs_FACE ); exp.More(); exp.Next() ) {
255     TopoDS_Shape F = exp.Current();
256     TopExp_Explorer expe;
257     bool IsFind = false;
258     for( expe.Init( F, TopAbs_EDGE ); expe.More(); expe.Next() ) {
259       TopoDS_Edge E = TopoDS::Edge(expe.Current());
260       TopoDS_Vertex VF = sae.FirstVertex(E);
261       TopoDS_Vertex VL = sae.LastVertex(E);
262       if( (VF.IsSame(VN11) && VL.IsSame(VN21)) || (VF.IsSame(VN21) && VL.IsSame(VN11)) ) {
263         IsFind = true;
264         break;
265       }
266     }
267     if(IsFind) {
268       for( expe.Init( F, TopAbs_EDGE ); expe.More(); expe.Next() ) {
269         TopoDS_Edge E = TopoDS::Edge(expe.Current());
270         TopoDS_Vertex VF = sae.FirstVertex(E);
271         TopoDS_Vertex VL = sae.LastVertex(E);
272         if( VF.IsSame(VN11) && !VL.IsSame(VN21) )
273           VN12 = VL;
274         if( VL.IsSame(VN11) && !VF.IsSame(VN21) )
275           VN12 = VF;
276         if( VF.IsSame(VN21) && !VL.IsSame(VN11) )
277           VN22 = VL;
278         if( VL.IsSame(VN21) && !VF.IsSame(VN11) )
279           VN22 = VF;
280       }
281       break;
282     }
283   }
284   // find vertexes from FS2 corresponded to VN12 and VN22
285   // and find edge from FS2 contains V12 and V22,
286   // this edge will be corresponded to edge E1
287   TopoDS_Vertex V12,V22;
288   gp_Pnt PN12 = BRep_Tool::Pnt(VN12);
289   gp_Pnt PN22 = BRep_Tool::Pnt(VN22);
290   //cout<<"PN12("<<PN12.X()<<","<<PN12.Y()<<","<<PN12.Z()<<")"<<endl;
291   //cout<<"PN22("<<PN22.X()<<","<<PN22.Y()<<","<<PN22.Z()<<")"<<endl;
292   TopoDS_Edge E2;
293   TopExp_Explorer expe;
294   for( expe.Init( FS2, TopAbs_EDGE ); expe.More(); expe.Next() ) {
295     TopoDS_Edge E = TopoDS::Edge(expe.Current());
296     TopoDS_Vertex VF = sae.FirstVertex(E);
297     TopoDS_Vertex VL = sae.LastVertex(E);
298     gp_Pnt PF = BRep_Tool::Pnt(VF);
299     gp_Pnt PL = BRep_Tool::Pnt(VL);
300     if( PF.Distance(PN12)<tol && PL.Distance(PN22)<tol ) {
301       V12 = VF;
302       V22 = VL;
303       E2 = E;
304       break;
305     }
306     if( PF.Distance(PN22)<tol && PL.Distance(PN12)<tol ) {
307       V12 = VL;
308       V22 = VF;
309       E2 = E;
310       break;
311     }
312   }
313   FF.Add(V11,V12);
314   FF.Add(V21,V22);
315   FF.Add(E1,E2);
316
317   // find other pairs for vertexes and edges
318   // creating map of vertex edges for both faces
319   return FillForOtherEdges(FS1,E1,V21,FF);
320
321   //return true;
322 }
323
324
325 //=======================================================================
326 //function : FillCorrespondingEdges
327 //purpose  : auxilary for CreatePipeShellsWithoutPath()
328 //=======================================================================
329 static bool FillCorrespondingEdges(const TopoDS_Shape& FS1,
330                                    const TopoDS_Shape& FS2,
331                                    const TopoDS_Vertex& aLoc1,
332                                    const TopoDS_Vertex& aLoc2,
333                                    TopTools_IndexedDataMapOfShapeShape& FF)
334 {
335   //cout<<"FillCorrespondingEdges"<<endl;
336
337   gp_Pnt P1 = BRep_Tool::Pnt(aLoc1);
338   gp_Pnt P2 = BRep_Tool::Pnt(aLoc2);
339   gp_Vec aDir(P1,P2);
340
341   ShapeAnalysis_Edge sae;
342   double tol = Max( BRep_Tool::Tolerance(TopoDS::Face(FS1)),
343                     BRep_Tool::Tolerance(TopoDS::Face(FS2)) );
344   TopTools_MapOfShape Vs1,Vs2;
345
346   TopoDS_Vertex V11=aLoc1, V12=aLoc2, V21, V22;
347   TopoDS_Edge E1,E2;
348
349   TopExp_Explorer exp1;
350   for( exp1.Init(FS1,TopAbs_EDGE); exp1.More(); exp1.Next() ) {
351     E1 = TopoDS::Edge(exp1.Current());
352     TopoDS_Vertex V1 = sae.FirstVertex(E1);
353     TopoDS_Vertex V2 = sae.LastVertex(E1);
354     gp_Pnt Ptmp1 = BRep_Tool::Pnt(V1);
355     gp_Pnt Ptmp2 = BRep_Tool::Pnt(V2);
356     //cout<<"P11("<<P11.X()<<","<<P11.Y()<<","<<P11.Z()<<")"<<endl;
357     //cout<<"P21("<<P21.X()<<","<<P21.Y()<<","<<P21.Z()<<")"<<endl;
358     if(P1.Distance(Ptmp1)<tol) {
359       V21 = V2;
360       break;
361     }
362     if(P1.Distance(Ptmp2)<tol) {
363       V21 = V1;
364       break;
365     }
366   }
367
368   TopoDS_Edge E21,E22;
369   TopoDS_Vertex VE21,VE22;
370   int nbe=0;
371   for( exp1.Init(FS2,TopAbs_EDGE); exp1.More() && nbe<2; exp1.Next() ) {
372     TopoDS_Edge E = TopoDS::Edge(exp1.Current());
373     TopoDS_Vertex V1 = sae.FirstVertex(E);
374     TopoDS_Vertex V2 = sae.LastVertex(E);
375     gp_Pnt Ptmp1 = BRep_Tool::Pnt(V1);
376     gp_Pnt Ptmp2 = BRep_Tool::Pnt(V2);
377     if(P2.Distance(Ptmp1)<tol) {
378       if(nbe==0) {
379         E21 = E;
380         VE21 = V2;
381         nbe++;
382       }
383       else if(nbe==1) {
384         E22 = E;
385         VE22 = V2;
386         nbe++;
387       }
388     }
389     if(P2.Distance(Ptmp2)<tol) {
390       if(nbe==0) {
391         E21 = E;
392         VE21 = V1;
393         nbe++;
394       }
395       else if(nbe==1) {
396         E22 = E;
397         VE22 = V1;
398         nbe++;
399       }
400     }
401   }
402
403   gp_Pnt PV21 = BRep_Tool::Pnt(V21);
404   gp_Pnt PE21 = BRep_Tool::Pnt(VE21);
405   gp_Pnt PE22 = BRep_Tool::Pnt(VE22);
406   gp_Vec aDir1(PV21,PE21);
407   gp_Vec aDir2(PV21,PE22);
408   double ang1 = aDir.Angle(aDir1);
409   double ang2 = aDir.Angle(aDir2);
410   if(fabs(ang1)<fabs(ang2)) {
411     E2 = E21;
412     V22 = VE21;
413   }
414   else {
415     E2 = E22;
416     V22 = VE22;
417   }
418
419   FF.Add(V11,V12);
420   FF.Add(V21,V22);
421   FF.Add(E1,E2);
422
423   // find other pairs for vertexes and edges
424   return FillForOtherEdges(FS1,E1,V21,FF);
425 }
426
427
428 //=======================================================================
429 //function : FindNextPairOfFaces
430 //purpose  : auxilary for CreatePipeForShellSections()
431 //=======================================================================
432 static void FindNextPairOfFaces(const TopoDS_Shape& aCurFace,
433                                 TopTools_IndexedDataMapOfShapeListOfShape& aMapEdgeFaces1,
434                                 TopTools_IndexedDataMapOfShapeListOfShape& aMapEdgeFaces2,
435                                 TopTools_IndexedDataMapOfShapeShape& FF,
436                                 GEOMImpl_IPipe* aCI)
437 {
438   //cout<<"FindNextPairOfFaces"<<endl;
439   TopExp_Explorer anExp;
440   for ( anExp.Init( aCurFace, TopAbs_EDGE ); anExp.More(); anExp.Next() ) {
441     TopoDS_Shape E1 = anExp.Current();
442     if(!FF.Contains(E1)) {
443       if(aCI) delete aCI;
444       Standard_ConstructionError::Raise("FindNextPairOfFaces: Can not find edge in map");
445     }
446     if(!FF.Contains(E1))
447       cout<<"    FindNextPairOfFaces: map FF not contains key E1"<<endl;
448     const TopoDS_Shape& E2 = FF.FindFromKey(E1);
449     TopExp_Explorer anExpV;
450     anExpV.Init( E1, TopAbs_VERTEX );
451     TopoDS_Shape V1 = anExpV.Current();
452     if(!FF.Contains(V1)) {
453       if(aCI) delete aCI;
454       Standard_ConstructionError::Raise("FindNextPairOfFaces: Can not find vertex in map");
455     }
456
457     if(!aMapEdgeFaces1.Contains(E1))
458       cout<<"    FindNextPairOfFaces: map aMapEdgeFaces1 not contains key E1"<<endl;
459     const TopTools_ListOfShape& aList1 = aMapEdgeFaces1.FindFromKey(E1);
460     if(aList1.Extent()<2)
461       continue;
462     TopTools_ListIteratorOfListOfShape anIter(aList1);
463     if(anIter.Value().IsEqual(aCurFace)) {
464       anIter.Next();
465     }
466     TopoDS_Shape F1other = anIter.Value();
467     if(FF.Contains(F1other))
468       continue;
469
470     if(!FF.Contains(aCurFace))
471       cout<<"    FindNextPairOfFaces: map FF not contains key aCurFace"<<endl;
472     const TopoDS_Shape& F2 = FF.FindFromKey(aCurFace);
473     if(!aMapEdgeFaces2.Contains(E2))
474       cout<<"    FindNextPairOfFaces: map aMapEdgeFaces2 not contains key E2"<<endl;
475     const TopTools_ListOfShape& aList2 = aMapEdgeFaces2.FindFromKey(E2);
476     if(aList2.Extent()<2) {
477       if(aCI) delete aCI;
478       Standard_ConstructionError::Raise("FindNextPairOfFaces: Can not find corresponding face");
479     }
480     TopTools_ListIteratorOfListOfShape anIter2(aList2);
481     if(anIter2.Value().IsEqual(F2)) {
482       anIter2.Next();
483     }
484     TopoDS_Shape F2other = anIter2.Value();
485     FF.Add(F1other,F2other);
486
487     // add pairs of edges to FF
488     bool stat =  FillForOtherEdges(F1other,E1,V1,FF);
489     if( !stat ) {
490       if(aCI) delete aCI;
491       Standard_ConstructionError::Raise("FindNextPairOfFaces: Can not mapping other egdes");
492     }
493
494     FindNextPairOfFaces(F1other, aMapEdgeFaces1, aMapEdgeFaces2, FF, aCI);
495   }
496 }
497
498
499 //=======================================================================
500 //function : FindFirstPairFaces
501 //purpose  : auxilary for Execute()
502 //=======================================================================
503 static void FindFirstPairFaces(const TopoDS_Shape& S1, const TopoDS_Shape& S2,
504                                TopoDS_Vertex& V1, TopoDS_Vertex& V2,
505                                TopoDS_Shape& FS1, TopoDS_Shape& FS2)
506 {
507   //cout<<"FindFirstPairFaces"<<endl;
508
509   // check if vertexes are subshapes of sections
510   gp_Pnt P1 = BRep_Tool::Pnt(V1);
511   gp_Pnt P2 = BRep_Tool::Pnt(V2);
512   TopoDS_Vertex V1new,V2new;
513   TopExp_Explorer exp;
514   double mindist = 1.e10;
515   for( exp.Init( S1, TopAbs_VERTEX ); exp.More(); exp.Next() ) {
516     TopoDS_Vertex V = TopoDS::Vertex(exp.Current());
517     gp_Pnt P = BRep_Tool::Pnt(V);
518     double dist = P1.Distance(P);
519     if(dist<mindist) {
520       mindist = dist;
521       V1new = V;
522     }
523   }
524   mindist = 1.e10;
525   for( exp.Init( S2, TopAbs_VERTEX ); exp.More(); exp.Next() ) {
526     TopoDS_Vertex V = TopoDS::Vertex(exp.Current());
527     gp_Pnt P = BRep_Tool::Pnt(V);
528     double dist = P2.Distance(P);
529     if(dist<mindist) {
530       mindist = dist;
531       V2new = V;
532     }
533   }
534
535   //gp_Pnt P1new = BRep_Tool::Pnt(V1new);
536   //gp_Pnt P2new = BRep_Tool::Pnt(V2new);
537   //cout<<"  P1("<<P1.X()<<","<<P1.Y()<<","<<P1.Z()<<")"<<endl;
538   //cout<<"  P2("<<P2.X()<<","<<P2.Y()<<","<<P2.Z()<<")"<<endl;
539   //cout<<"  P1new("<<P1new.X()<<","<<P1new.Y()<<","<<P1new.Z()<<")"<<endl;
540   //cout<<"  P2new("<<P2new.X()<<","<<P2new.Y()<<","<<P2new.Z()<<")"<<endl;
541
542   // replace vertexes if it is needed
543   if(!V1.IsSame(V1new)) {
544     V1 = V1new;
545     P1 = BRep_Tool::Pnt(V1);
546     cout<<"  replace V1"<<endl;
547   }
548   else
549     cout<<"  not replace V1"<<endl;
550   if(!V2.IsSame(V2new)) {
551     V2 = V2new;
552     P2 = BRep_Tool::Pnt(V2);
553     cout<<"  replace V2"<<endl;
554   }
555   else
556     cout<<"  not replace V2"<<endl;
557
558   TopTools_IndexedDataMapOfShapeListOfShape aMapVertFaces1;
559   TopExp::MapShapesAndAncestors(S1, TopAbs_VERTEX, TopAbs_FACE, aMapVertFaces1);
560   TopTools_IndexedDataMapOfShapeListOfShape aMapVertFaces2;
561   TopExp::MapShapesAndAncestors(S2, TopAbs_VERTEX, TopAbs_FACE, aMapVertFaces2);
562
563   if(!aMapVertFaces1.Contains(V1))
564     cout<<"    FindFirstPairFaces: map aMapVertFaces1 not contains key V1"<<endl;
565   const TopTools_ListOfShape& aList1 = aMapVertFaces1.FindFromKey(V1);
566   TopTools_ListIteratorOfListOfShape anIter1(aList1);
567   FS1 = anIter1.Value();
568   // find middle point
569   double x1=0., y1=0., z1=0.;
570   int nbv1=0;
571   for( exp.Init( FS1, TopAbs_VERTEX ); exp.More(); exp.Next() ) {
572     TopoDS_Vertex V = TopoDS::Vertex(exp.Current());
573     gp_Pnt P = BRep_Tool::Pnt(V);
574     x1 += P.X();
575     y1 += P.Y();
576     z1 += P.Z();
577     nbv1++;
578   }
579   gp_Pnt PM1(x1/nbv1, y1/nbv1, z1/nbv1);
580
581   TColgp_SequenceOfPnt Ps;
582   TopTools_SequenceOfShape Fs;
583   if(!aMapVertFaces2.Contains(V2))
584     cout<<"    FindFirstPairFaces: map aMapVertFaces2 not contains key V2"<<endl;
585   const TopTools_ListOfShape& aList2 = aMapVertFaces2.FindFromKey(V2);
586   TopTools_ListIteratorOfListOfShape anIter2(aList2);
587   for(; anIter2.More(); anIter2.Next()) {
588     TopoDS_Shape F = anIter2.Value();
589     double x2=0., y2=0., z2=0.;
590     int nbv2=0;
591     for( exp.Init( F, TopAbs_VERTEX ); exp.More(); exp.Next() ) {
592       TopoDS_Vertex V = TopoDS::Vertex(exp.Current());
593       gp_Pnt P = BRep_Tool::Pnt(V);
594       x2 += P.X();
595       y2 += P.Y();
596       z2 += P.Z();
597       nbv2++;
598     }
599     gp_Pnt PM(x2/nbv1, y2/nbv1, z2/nbv1);
600     Fs.Append(F);
601     Ps.Append(PM);
602   }
603
604   gp_Vec aDir(P1,P2);
605   int i=1;
606   double MinAng = PI;
607   int numface = 0;
608   for(; i<=Fs.Length(); i++) {
609     gp_Vec tmpDir(PM1,Ps(i));
610     double ang = fabs(aDir.Angle(tmpDir));
611     if(ang<MinAng) {
612       MinAng = ang;
613       numface = i;
614     }
615   }
616   FS2 = Fs(numface);
617 }
618
619
620 //=======================================================================
621 //function : CreatePipeForShellSections
622 //purpose  : auxilary for Execute()
623 //=======================================================================
624 static TopoDS_Shape CreatePipeForShellSections(const TopoDS_Wire& aWirePath,
625                                                GEOMImpl_IPipe* aCI)
626 {
627   //cout<<"CreatePipeForShellSections"<<endl;
628   //TopoDS_Shape res;
629   int i,j;
630   BRep_Builder B;
631
632   GEOMImpl_IPipeShellSect* aCIDS = (GEOMImpl_IPipeShellSect*)aCI;
633   Handle(TColStd_HSequenceOfTransient) aBasesObjs = aCIDS->GetBases();
634   Handle(TColStd_HSequenceOfTransient) aSubBasesObjs = aCIDS->GetSubBases();
635   Handle(TColStd_HSequenceOfTransient) aLocObjs = aCIDS->GetLocations();
636   Standard_Boolean aWithContact = (aCIDS->GetWithContactMode());
637   Standard_Boolean aWithCorrect = (aCIDS->GetWithCorrectionMode());
638
639   Standard_Integer nbBases = aBasesObjs->Length(), 
640     nbSubBases = (aSubBasesObjs.IsNull() ? 0 :aSubBasesObjs->Length()),
641     nbLocs = (aLocObjs.IsNull() ? 0 :aLocObjs->Length());
642     
643   if( nbLocs != nbBases) {
644     if(aCI) delete aCI;
645     Standard_ConstructionError::Raise("Number of sections is not equal to number of locations ");
646   }
647   if( nbSubBases && nbSubBases != nbBases) {
648     if(aCI) delete aCI;
649     Standard_ConstructionError::Raise("Number of sections is not equal to number of subsections ");
650   }
651
652   //BRepOffsetAPI_MakePipeShell aBuilder(aWirePath);
653
654   TopTools_SequenceOfShape VLocs;
655   for(i=1; i<=nbBases; i++) {
656     Handle(Standard_Transient) anItemLoc = aLocObjs->Value(i);
657     if(anItemLoc.IsNull())
658       continue;
659     Handle(GEOM_Function) aRefLoc = Handle(GEOM_Function)::DownCast(anItemLoc);
660     TopoDS_Shape aShapeLoc = aRefLoc->GetValue();
661     if(aShapeLoc.IsNull() || aShapeLoc.ShapeType() != TopAbs_VERTEX)
662       continue;
663     VLocs.Append(aShapeLoc);
664   }
665   nbLocs = VLocs.Length();
666   if( nbLocs != nbBases) {
667     if(aCI) delete aCI;
668     Standard_ConstructionError::Raise("One of location shapes is not a vertex");
669   }
670   // split wire path by location points
671   TColgp_SequenceOfPnt PLocs;
672   for(i=1; i<=nbLocs; i++) {
673     TopoDS_Vertex V = TopoDS::Vertex(VLocs.Value(i));
674     PLocs.Append(BRep_Tool::Pnt(V));
675   }
676
677   TopTools_SequenceOfShape Edges;
678   TopTools_SequenceOfShape Wires;
679   ShapeAnalysis_Edge sae;
680
681   if(nbLocs==2) {
682     TopExp_Explorer anExp;
683     for ( anExp.Init( aWirePath, TopAbs_EDGE ); anExp.More(); anExp.Next() ) {
684       Edges.Append(anExp.Current());
685     }
686     Standard_Integer Num1 = 0;
687     Standard_Integer Num2 = 0;
688     for(i=1; i<=Edges.Length(); i++) {
689       TopoDS_Edge E = TopoDS::Edge(Edges.Value(i));
690       double tol = BRep_Tool::Tolerance(E);
691       TopoDS_Vertex V1 = sae.FirstVertex(E);
692       TopoDS_Vertex V2 = sae.LastVertex(E);
693       gp_Pnt P1 = BRep_Tool::Pnt(V1);
694       gp_Pnt P2 = BRep_Tool::Pnt(V2);
695       if( P1.Distance(PLocs.First()) < tol ) {
696         Num1 = i;
697       }
698       if( P2.Distance(PLocs.Last()) < tol ) {
699         Num2 = i;
700       }
701     }
702     if( Num1>0 && Num2>0 ) {
703       TopoDS_Wire W;
704       B.MakeWire(W);
705       for(i=Num1; i<=Num2; i++) {
706         B.Add(W,Edges.Value(i));
707       }
708       Wires.Append(W);
709     }
710     else {
711       Wires.Append(aWirePath);
712     }
713   }
714   else {
715     TopExp_Explorer anExp;
716     for ( anExp.Init( aWirePath, TopAbs_EDGE ); anExp.More(); anExp.Next() ) {
717       Edges.Append(anExp.Current());
718     }
719     TopoDS_Edge edge = TopoDS::Edge(Edges.First());
720     double tol = BRep_Tool::Tolerance(edge);
721     TopoDS_Vertex VF = sae.FirstVertex(edge);
722     gp_Pnt PF = BRep_Tool::Pnt(VF);
723     //cout<<"PF("<<PF.X()<<","<<PF.Y()<<","<<PF.Z()<<")"<<endl;
724     if( PF.Distance(PLocs.First()) > tol ) {
725       if(aCI) delete aCI;
726       Standard_ConstructionError::Raise
727         ("First location shapes is not coincided with first vertex of aWirePath");
728     }
729     VLocs.ChangeValue(1) = VF;
730     edge = TopoDS::Edge(Edges.Last());
731     tol = BRep_Tool::Tolerance(edge);
732     TopoDS_Vertex VL = sae.LastVertex(edge);
733     gp_Pnt PL = BRep_Tool::Pnt(VL);
734     if( PL.Distance(PLocs.Last()) > tol ) {
735       if(aCI) delete aCI;
736       Standard_ConstructionError::Raise
737         ("Last location shapes is not coincided with last vertex of aWirePath");
738     }
739     VLocs.ChangeValue(nbLocs) = VL;
740     int jcurr = 2;
741     TopTools_SequenceOfShape tmpEdges;
742     for(i=1; i<=Edges.Length() && jcurr<nbLocs; i++) {
743       TopoDS_Edge E = TopoDS::Edge(Edges.Value(i));
744       tol = BRep_Tool::Tolerance(E);
745       TopoDS_Vertex V1 = sae.FirstVertex(E);
746       TopoDS_Vertex V2 = sae.LastVertex(E);
747       gp_Pnt P1 = BRep_Tool::Pnt(V1);
748       gp_Pnt P2 = BRep_Tool::Pnt(V2);
749       if( P2.Distance(PLocs.Value(jcurr)) < tol ) {
750         // make wire from current edge and add created
751         // wire to Wires
752         TopoDS_Wire W;
753         B.MakeWire(W);
754         for(j=1; j<=tmpEdges.Length(); j++)
755           B.Add(W,tmpEdges.Value(j));
756         B.Add(W,E);
757         Wires.Append(W);
758         VLocs.ChangeValue(jcurr) = V2;
759         jcurr++;
760         tmpEdges.Clear();
761       }
762       else {
763         // find distance between E and aLocs(jcurr)
764         double fp,lp;
765         Handle(Geom_Curve) C = BRep_Tool::Curve(E,fp,lp);
766         GeomAPI_ProjectPointOnCurve PPC (PLocs.Value(jcurr),C);
767         if( PPC.NbPoints()>0 &&
768             PLocs.Value(jcurr).Distance(PPC.Point(1)) < tol ) {
769           double param = PPC.Parameter(1);
770           gp_Pnt PC1;
771           C->D0(param,PC1);
772           // split current edge
773           Handle(Geom_TrimmedCurve) tc1 = new Geom_TrimmedCurve(C,fp,param);
774           Handle(Geom_TrimmedCurve) tc2 = new Geom_TrimmedCurve(C,param,lp);
775           TopoDS_Edge E1,E2;
776           gp_Pnt Pfp;
777           C->D0(fp,Pfp);
778           if(Pfp.Distance(P1)<tol) {
779             B.MakeEdge(E1,tc1,tol);
780             B.Add(E1,V1);
781             TopoDS_Shape tmpV = VLocs.Value(jcurr).Oriented(TopAbs_REVERSED);
782             B.Add(E1,TopoDS::Vertex(tmpV));
783             tmpEdges.Append(E1);
784             B.MakeEdge(E2,tc2,tol);
785             tmpV = VLocs.Value(jcurr).Oriented(TopAbs_FORWARD);
786             B.Add(E2,TopoDS::Vertex(tmpV));
787             B.Add(E2,V2);
788           }
789           else {
790             B.MakeEdge(E1,tc2,tol);
791             TopoDS_Shape tmpV = VLocs.Value(jcurr).Oriented(TopAbs_FORWARD);
792             B.Add(E1,TopoDS::Vertex(tmpV));
793             B.Add(E1,V1);
794             E1.Reverse();
795             tmpEdges.Append(E1);
796             B.MakeEdge(E2,tc1,tol);
797             B.Add(E2,V2);
798             tmpV = VLocs.Value(jcurr).Oriented(TopAbs_REVERSED);
799             B.Add(E2,TopoDS::Vertex(tmpV));
800             E2.Reverse();
801           }
802           // create wire from tmpEdges
803           TopoDS_Wire W;
804           B.MakeWire(W);
805           for(j=1; j<=tmpEdges.Length(); j++)
806             B.Add(W,tmpEdges.Value(j));
807           Wires.Append(W);
808           jcurr++;
809           tmpEdges.Clear();
810           Edges.Remove(i);
811           Edges.InsertAfter(i-1,E1);
812           Edges.InsertAfter(i,E2);
813         }
814         else {
815           tmpEdges.Append(E);
816         }
817       }
818     }
819     // create wire from other edges
820     TopoDS_Wire W;
821     B.MakeWire(W);
822     for(; i<=Edges.Length(); i++)
823       B.Add(W,Edges.Value(i));
824     Wires.Append(W);
825     //cout<<"Wires.Length()="<<Wires.Length()<<endl;
826   }
827
828   if( Wires.Length() != nbLocs-1 ) {
829     if(aCI) delete aCI;
830     Standard_ConstructionError::Raise
831       ("One of location shapes is not lied on the path");
832   }
833
834   //TopTools_SequenceOfShape aSeqBases;
835   //TopTools_SequenceOfShape aSeqSubBases;
836   //TopTools_SequenceOfShape aSeqFaces;
837   TopoDS_Compound aComp;
838   B.MakeCompound(aComp);
839   for(i=1 ; i<nbBases; i++) {
840     TopoDS_Wire WPath = TopoDS::Wire(Wires.Value(i));
841     // 1 section
842     Handle(Standard_Transient) anItem1 = aBasesObjs->Value(i);
843     if(anItem1.IsNull())
844       continue;
845     Handle(GEOM_Function) aRefBase1 = Handle(GEOM_Function)::DownCast(anItem1);
846     if(aRefBase1.IsNull())
847       continue;
848     TopoDS_Shape aShBase1 = aRefBase1->GetValue();
849     if(aShBase1.IsNull())
850       continue;
851     TopAbs_ShapeEnum aType1 = aShBase1.ShapeType();
852     // 2 section
853     Handle(Standard_Transient) anItem2 = aBasesObjs->Value(i+1);
854     if(anItem2.IsNull())
855       continue;
856     Handle(GEOM_Function) aRefBase2 = Handle(GEOM_Function)::DownCast(anItem2);
857     if(aRefBase2.IsNull())
858       continue;
859     TopoDS_Shape aShBase2 = aRefBase2->GetValue();
860     if(aShBase2.IsNull())
861       continue;
862     TopAbs_ShapeEnum aType2 = aShBase2.ShapeType();
863     
864     //BRepTools::Write(aShBase1,"/dn02/users_Linux/skl/work/Bugs/14857/base1.brep");
865
866     bool OkSec = ( aType1==TopAbs_SHELL || aType1==TopAbs_FACE ) &&
867                  ( aType2==TopAbs_SHELL || aType2==TopAbs_FACE );
868     if( !OkSec ) {
869       if(aCI) delete aCI;
870       Standard_ConstructionError::Raise("One of section shapes has invalid type");
871     }
872
873     bool CreateFewSolids = false;
874     // compare sections
875     TopExp_Explorer anExp;
876     Standard_Integer nbf1 = 0;
877     for ( anExp.Init( aShBase1, TopAbs_FACE ); anExp.More(); anExp.Next() ) {
878       nbf1++;
879     }
880     Standard_Integer nbf2 = 0;
881     for ( anExp.Init( aShBase2, TopAbs_FACE ); anExp.More(); anExp.Next() ) {
882       nbf2++;
883     }
884     if(nbf1==nbf2) {
885       CreateFewSolids = true;
886     }
887
888     /*
889     // check orientation of sections
890     bool NeedReverse = false;
891     {
892       // first section
893       anExp.Init( aShBase1, TopAbs_FACE );
894       TopoDS_Shape aFace = anExp.Current();
895       TColgp_SequenceOfPnt aPnts;
896       double xc=0, yc=0, zc=0;
897       for ( anExp.Init( aFace, TopAbs_VERTEX ); anExp.More(); anExp.Next() ) {
898         TopoDS_Vertex V = TopoDS::Vertex(anExp.Current());
899         aPnts.Append(BRep_Tool::Pnt(V));
900         xc += aPnts.Last().X();
901         yc += aPnts.Last().Y();
902         zc += aPnts.Last().Z();
903       }
904       gp_Pnt PC( xc/aPnts.Length(), yc/aPnts.Length(), zc/aPnts.Length() );
905       gp_Vec V1(PC,aPnts.Value(1));
906       gp_Vec V2(PC,aPnts.Value(2));
907       gp_Vec VN = V1.Crossed(V2);
908       for(int ip=2; ip<aPnts.Length(); ip++) {
909         V1 = gp_Vec(PC,aPnts.Value(ip));
910         V2 = gp_Vec(PC,aPnts.Value(ip+1));
911         VN.Add(V1.Crossed(V2));
912       }
913       gp_Vec PathNorm;
914       gp_Pnt PLoc = BRep_Tool::Pnt(TopoDS::Vertex(VLocs(i)));
915       TopExp_Explorer WE;
916       for ( WE.Init( WPath, TopAbs_EDGE ); WE.More(); WE.Next() ) {
917         TopoDS_Edge edge = TopoDS::Edge(WE.Current());
918         double tol = BRep_Tool::Tolerance(edge);
919         TopoDS_Vertex VF = sae.FirstVertex(edge);
920         gp_Pnt PF = BRep_Tool::Pnt(VF);
921         if( PF.Distance(PLoc) < tol ) {
922           double fp,lp;
923           Handle(Geom_Curve) C = BRep_Tool::Curve(edge,fp,lp);
924           gp_Pnt P1,P2;
925           C->D0(fp,P1);
926           if( P1.Distance(PLoc) < tol ) {
927             C->D0(fp+(lp-fp)/100,P2);
928           }
929           else {
930             C->D0(lp,P1);
931             C->D0(lp+(fp-lp)/100,P2);
932           }
933           PathNorm = gp_Vec(P1,P2);
934           break;
935         }
936         else {
937           TopoDS_Vertex VL = sae.LastVertex(edge);
938           gp_Pnt PL = BRep_Tool::Pnt(VL);
939           if( PL.Distance(PLoc) < tol ) {
940             double fp,lp;
941             Handle(Geom_Curve) C = BRep_Tool::Curve(edge,fp,lp);
942             gp_Pnt P1,P2;
943             C->D0(fp,P1);
944             if( P1.Distance(PLoc) < tol ) {
945               C->D0(fp+(lp-fp)/100,P2);
946             }
947             else {
948               C->D0(lp,P1);
949               C->D0(lp+(fp-lp)/100,P2);
950             }
951             PathNorm = gp_Vec(P2,P1);
952             break;
953           }
954         }
955       }
956       cout<<"VN("<<VN.X()<<","<<VN.Y()<<","<<VN.Z()<<")"<<endl;
957       cout<<"PathNorm("<<PathNorm.X()<<","<<PathNorm.Y()<<","<<PathNorm.Z()<<")"<<endl;
958       if(fabs(VN.Angle(PathNorm))>PI/2.) {
959         NeedReverse = true;
960         aShBase1.Reverse();
961       }
962     }
963     {
964       // second section
965       anExp.Init( aShBase2, TopAbs_FACE );
966       TopoDS_Shape aFace = anExp.Current();
967       TColgp_SequenceOfPnt aPnts;
968       double xc=0, yc=0, zc=0;
969       for ( anExp.Init( aFace, TopAbs_VERTEX ); anExp.More(); anExp.Next() ) {
970         TopoDS_Vertex V = TopoDS::Vertex(anExp.Current());
971         aPnts.Append(BRep_Tool::Pnt(V));
972         xc += aPnts.Last().X();
973         yc += aPnts.Last().Y();
974         zc += aPnts.Last().Z();
975       }
976       gp_Pnt PC( xc/aPnts.Length(), yc/aPnts.Length(), zc/aPnts.Length() );
977       gp_Vec V1(PC,aPnts.Value(1));
978       gp_Vec V2(PC,aPnts.Value(2));
979       gp_Vec VN = V1.Crossed(V2);
980       for(int ip=2; ip<aPnts.Length(); ip++) {
981         V1 = gp_Vec(PC,aPnts.Value(ip));
982         V2 = gp_Vec(PC,aPnts.Value(ip+1));
983         VN.Add(V1.Crossed(V2));
984       }
985       gp_Vec PathNorm;
986       gp_Pnt PLoc = BRep_Tool::Pnt(TopoDS::Vertex(VLocs(i+1)));
987       TopExp_Explorer WE;
988       for ( WE.Init( WPath, TopAbs_EDGE ); WE.More(); WE.Next() ) {
989         TopoDS_Edge edge = TopoDS::Edge(WE.Current());
990         double tol = BRep_Tool::Tolerance(edge);
991         TopoDS_Vertex VF = sae.FirstVertex(edge);
992         gp_Pnt PF = BRep_Tool::Pnt(VF);
993         if( PF.Distance(PLoc) < tol ) {
994           double fp,lp;
995           Handle(Geom_Curve) C = BRep_Tool::Curve(edge,fp,lp);
996           gp_Pnt P1,P2;
997           C->D0(fp,P1);
998           if( P1.Distance(PLoc) < tol ) {
999             C->D0(fp+(lp-fp)/100,P2);
1000           }
1001           else {
1002             C->D0(lp,P1);
1003             C->D0(lp+(fp-lp)/100,P2);
1004           }
1005           PathNorm = gp_Vec(P2,P1);
1006           break;
1007         }
1008         else {
1009           TopoDS_Vertex VL = sae.LastVertex(edge);
1010           gp_Pnt PL = BRep_Tool::Pnt(VL);
1011           if( PL.Distance(PLoc) < tol ) {
1012             double fp,lp;
1013             Handle(Geom_Curve) C = BRep_Tool::Curve(edge,fp,lp);
1014             gp_Pnt P1,P2;
1015             C->D0(fp,P1);
1016             if( P1.Distance(PLoc) < tol ) {
1017               C->D0(fp+(lp-fp)/100,P2);
1018             }
1019             else {
1020               C->D0(lp,P1);
1021               C->D0(lp+(fp-lp)/100,P2);
1022             }
1023             PathNorm = gp_Vec(P2,P1);
1024             break;
1025           }
1026         }
1027       }
1028       //cout<<"VN("<<VN.X()<<","<<VN.Y()<<","<<VN.Z()<<")"<<endl;
1029       //cout<<"PathNorm("<<PathNorm.X()<<","<<PathNorm.Y()<<","<<PathNorm.Z()<<")"<<endl;
1030       if(fabs(VN.Angle(PathNorm))>PI/2.)
1031         aShBase2.Reverse();
1032     }
1033     */
1034
1035     if(!CreateFewSolids) {
1036       // we can create only one solid
1037       TopoDS_Shape aWire1, aWire2;
1038       // prepare aWire1
1039       if(aType1==TopAbs_SHELL) {
1040         // create wire as boundary contour if shell is no closed
1041         // get free boundary shapes
1042         ShapeAnalysis_FreeBounds anAnalizer( aShBase1 );
1043         TopoDS_Compound aClosed = anAnalizer.GetClosedWires();
1044         //TopExp_Explorer anExp;
1045         Standard_Integer NbWires = 0;
1046         for ( anExp.Init( aClosed, TopAbs_WIRE ); anExp.More(); anExp.Next() ) {
1047           NbWires++;
1048           aWire1 = anExp.Current();
1049         }
1050         if(NbWires!=1) {
1051           // bad case
1052           if(aCI) delete aCI;
1053           Standard_ConstructionError::Raise("Bad shell is used as section ");
1054         }
1055       }
1056       else { // aType1==TopAbs_FACE
1057         TopExp_Explorer aExpW(aShBase1,TopAbs_WIRE);
1058         aWire1 = aExpW.Current();
1059       }
1060       // prepare aWire2
1061       if(aType2==TopAbs_SHELL) {
1062         // create wire as boundary contour if shell is no closed
1063         // get free boundary shapes
1064         ShapeAnalysis_FreeBounds anAnalizer( aShBase2 );
1065         TopoDS_Compound aClosed = anAnalizer.GetClosedWires();
1066         //TopExp_Explorer anExp;
1067         Standard_Integer NbWires = 0;
1068         for ( anExp.Init( aClosed, TopAbs_WIRE ); anExp.More(); anExp.Next() ) {
1069           NbWires++;
1070           aWire2 = anExp.Current();
1071         }
1072         if(NbWires!=1) {
1073           // bad case
1074           if(aCI) delete aCI;
1075           Standard_ConstructionError::Raise("Bad shell is used as section ");
1076         }
1077       }
1078       else { // aType2==TopAbs_FACE
1079         TopExp_Explorer aExpW(aShBase2,TopAbs_WIRE);
1080         aWire2 = aExpW.Current();
1081       }
1082       // make pipe using aWire1 and aWire2
1083       if( !aWire1.IsNull() && !aWire2.IsNull() ) {
1084         //BRepOffsetAPI_MakePipeShell aBuilder(aWirePath);
1085         BRepOffsetAPI_MakePipeShell aBuilder(WPath);
1086         aBuilder.Add(aWire1, TopoDS::Vertex(VLocs(i)),
1087                      aWithContact, aWithCorrect);
1088         aBuilder.Add(aWire2, TopoDS::Vertex(VLocs(i+1)),
1089                      aWithContact, aWithCorrect);
1090         if(!aBuilder.IsReady()) {
1091           if(aCI) delete aCI;
1092           Standard_ConstructionError::Raise("Invalid input data for building PIPE: bases are invalid");
1093         }
1094         aBuilder.Build();
1095         TopoDS_Shape aShape = aBuilder.Shape();
1096         TopoDS_Shell aShell;
1097         B.MakeShell(aShell);
1098         for ( anExp.Init( aShape, TopAbs_FACE ); anExp.More(); anExp.Next() ) {
1099           B.Add(aShell,anExp.Current());
1100         }
1101         for ( anExp.Init( aShBase1, TopAbs_FACE ); anExp.More(); anExp.Next() ) {
1102           B.Add(aShell,anExp.Current());
1103         }
1104         for ( anExp.Init( aShBase2, TopAbs_FACE ); anExp.More(); anExp.Next() ) {
1105           B.Add(aShell,anExp.Current());
1106         }
1107         // make sewing for this shell
1108         Handle(BRepBuilderAPI_Sewing) aSewing = new BRepBuilderAPI_Sewing;
1109         aSewing->SetTolerance(Precision::Confusion());
1110         aSewing->SetFaceMode(Standard_True);
1111         aSewing->SetFloatingEdgesMode(Standard_False);
1112         aSewing->SetNonManifoldMode(Standard_False);
1113         for ( anExp.Init( aShell, TopAbs_FACE ); anExp.More(); anExp.Next() ) {
1114           aSewing->Add(anExp.Current());
1115         }
1116         aSewing->Perform();
1117         const TopoDS_Shape aSewShape = aSewing->SewedShape();
1118         if( aSewShape.ShapeType() == TopAbs_SHELL ) {
1119           aShell = TopoDS::Shell(aSewShape);
1120           GProp_GProps aSystem;
1121           BRepGProp::VolumeProperties(aShell, aSystem);
1122           if(aSystem.Mass()<0) {
1123             aShell.Reverse();
1124           }
1125           if(BRep_Tool::IsClosed(aShell)) {
1126             TopoDS_Solid aSolid;
1127             B.MakeSolid(aSolid);
1128             B.Add(aSolid,aShell);
1129             B.Add(aComp,aSolid);
1130           }
1131           else {
1132             B.Add(aComp,aShell);
1133           }
1134         }
1135         else {
1136           B.Add(aComp,aShell);
1137         }
1138       }
1139     }
1140     else {
1141       // main block - creation few solids (for each pair of faces)
1142       TopTools_MapOfShape aFaces1,aFaces2;
1143       for ( anExp.Init( aShBase1, TopAbs_FACE ); anExp.More(); anExp.Next() ) {
1144         aFaces1.Add(anExp.Current());
1145       }
1146       for ( anExp.Init( aShBase2, TopAbs_FACE ); anExp.More(); anExp.Next() ) {
1147         aFaces2.Add(anExp.Current());
1148       }
1149       // creating map of edge faces
1150       TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces1;
1151       TopExp::MapShapesAndAncestors(aShBase1, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces1);
1152       TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces2;
1153       TopExp::MapShapesAndAncestors(aShBase2, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces2);
1154   
1155       // constuct map face->face
1156       TopTools_IndexedDataMapOfShapeShape FF;
1157       TopoDS_Shape FS1,FS2;
1158       if(nbSubBases==0) {
1159         // find edge the most distant from location point
1160         // (this edge is not shared by two faces)
1161         double maxdist = 0.;
1162         TopoDS_Shape E1;
1163         TopoDS_Vertex V11,V21;
1164         for(j=1; j<=aMapEdgeFaces1.Extent(); j++) {
1165           TopoDS_Shape tmp = aMapEdgeFaces1.FindKey(j);
1166           const TopTools_ListOfShape& aList = aMapEdgeFaces1.FindFromKey(tmp);
1167           if(aList.Extent()>1)
1168             continue;
1169           TopExp_Explorer expv;
1170           expv.Init( tmp, TopAbs_VERTEX );
1171           TopoDS_Vertex V1 = TopoDS::Vertex(expv.Current());
1172           expv.Next();
1173           TopoDS_Vertex V2 = TopoDS::Vertex(expv.Current());
1174           gp_Pnt P1 = BRep_Tool::Pnt(V1);
1175           gp_Pnt P2 = BRep_Tool::Pnt(V2);
1176           double dist = PLocs.Value(i).Distance(P1) + PLocs.Value(i).Distance(P2);
1177           if(dist>maxdist) {
1178             E1 = tmp;
1179             V11 = V1;
1180             V21 = V2;
1181             TopTools_ListIteratorOfListOfShape anIter(aList);
1182             FS1 = anIter.Value();
1183             maxdist = dist;
1184           }
1185         }
1186         // main direction for comparing
1187         gp_Vec VM(PLocs.Value(i),PLocs.Value(i+1));
1188         // find corresponding edge from next section
1189         double minang = PI;
1190         gp_Pnt P11 = BRep_Tool::Pnt(V11);
1191         gp_Pnt P21 = BRep_Tool::Pnt(V21);
1192         TopoDS_Shape E2;
1193         TopoDS_Vertex V12,V22;
1194         for(j=1; j<=aMapEdgeFaces2.Extent(); j++) {
1195           TopoDS_Shape tmp = aMapEdgeFaces2.FindKey(j);
1196           const TopTools_ListOfShape& aList = aMapEdgeFaces2.FindFromKey(tmp);
1197           if(aList.Extent()>1)
1198             continue;
1199           TopExp_Explorer expv;
1200           expv.Init( tmp, TopAbs_VERTEX );
1201           TopoDS_Vertex V1tmp = TopoDS::Vertex(expv.Current());
1202           expv.Next();
1203           TopoDS_Vertex V2tmp = TopoDS::Vertex(expv.Current());
1204           gp_Pnt P1tmp = BRep_Tool::Pnt(V1tmp);
1205           gp_Pnt P2tmp = BRep_Tool::Pnt(V2tmp);
1206           double d1 = P1tmp.Distance(P11) + P2tmp.Distance(P21);
1207           double d2 = P1tmp.Distance(P21) + P2tmp.Distance(P11);
1208           TopoDS_Vertex V1,V2;
1209           gp_Pnt P1,P2;
1210           if(d1>d2) {
1211             V1 = V2tmp; P1 = P2tmp;
1212             V2 = V1tmp; P2 = P1tmp;
1213           }
1214           else {
1215             V1 = V1tmp; P1 = P1tmp;
1216             V2 = V2tmp; P2 = P2tmp;
1217           }
1218           gp_Vec Vec1(P11,P1);
1219           gp_Vec Vec2(P21,P2);
1220           double ang = fabs(Vec1.Angle(VM)) + fabs(Vec2.Angle(VM));
1221           if(ang<minang) {
1222             E2 = tmp;
1223             V12 = V1;
1224             V22 = V2;
1225             TopTools_ListIteratorOfListOfShape anIter(aList);
1226             FS2 = anIter.Value();
1227             minang = ang;
1228           }
1229         }
1230         // put all pairs to map FF
1231         FF.Add(FS1,FS2);
1232         FF.Add(E1,E2);
1233         FF.Add(V11,V12);
1234         FF.Add(V21,V22);
1235
1236         // add pairs of edges to FF
1237         bool stat =  FillForOtherEdges(FS1,E1,V11,FF);
1238         if( !stat ) {
1239           if(aCI) delete aCI;
1240           Standard_ConstructionError::Raise("FindForOtherEdges: Can not mapping other egdes");
1241         }
1242
1243       }
1244       else {
1245         { // 1 section
1246           Handle(Standard_Transient) anItem = aSubBasesObjs->Value(i);
1247           if(anItem.IsNull()) {
1248             if(aCI) delete aCI;
1249             Standard_ConstructionError::Raise("Invalid subbase shape");
1250           }
1251           Handle(GEOM_Function) aRefBase = Handle(GEOM_Function)::DownCast(anItem);
1252           if(aRefBase.IsNull()) {
1253             if(aCI) delete aCI;
1254             Standard_ConstructionError::Raise("Invalid subbase shape");
1255           }
1256           TopoDS_Shape aSh = aRefBase->GetValue();
1257           if(aSh.IsNull()) {
1258             if(aCI) delete aCI;
1259             Standard_ConstructionError::Raise("Invalid subbase shape");
1260           }
1261           if(aSh.ShapeType()!=TopAbs_FACE) {
1262             if(aCI) delete aCI;
1263             Standard_ConstructionError::Raise("Invalid subbase shape");
1264           }
1265           FS1 = aSh;
1266         }       
1267         { // 2 section
1268           Handle(Standard_Transient) anItem = aSubBasesObjs->Value(i+1);
1269           if(anItem.IsNull()) {
1270             if(aCI) delete aCI;
1271             Standard_ConstructionError::Raise("Invalid subbase shape");
1272           }
1273           Handle(GEOM_Function) aRefBase = Handle(GEOM_Function)::DownCast(anItem);
1274           if(aRefBase.IsNull()) {
1275             if(aCI) delete aCI;
1276             Standard_ConstructionError::Raise("Invalid subbase shape");
1277           }
1278           TopoDS_Shape aSh = aRefBase->GetValue();
1279           if(aSh.IsNull()) {
1280             if(aCI) delete aCI;
1281             Standard_ConstructionError::Raise("Invalid subbase shape");
1282           }
1283           if(aSh.ShapeType()!=TopAbs_FACE) {
1284             if(aCI) delete aCI;
1285             Standard_ConstructionError::Raise("Invalid subbase shape");
1286           }
1287           FS2 = aSh;
1288         }
1289
1290         if( !aFaces1.Contains(FS1) || !aFaces2.Contains(FS2) ) {
1291           if(aCI) delete aCI;
1292           Standard_ConstructionError::Raise("Invalid subbase shape");
1293         }
1294       
1295         FF.Add(FS1,FS2);
1296
1297         // add pairs of edges to FF
1298         bool stat =  FillCorrespondingEdges(FS1, FS2, TopoDS::Vertex(VLocs(i)),
1299                                             TopoDS::Vertex(VLocs(i+1)), WPath, FF);
1300         if( !stat ) {
1301           if(aCI) delete aCI;
1302           Standard_ConstructionError::Raise("Can not create correct pipe");
1303         }
1304       }
1305
1306       FindNextPairOfFaces(FS1, aMapEdgeFaces1, aMapEdgeFaces2, FF, aCI);
1307
1308
1309       // make pipe for each pair of faces
1310       for(j=1; j<=FF.Extent(); j++) {
1311         TopoDS_Shape F1 = FF.FindKey(j);
1312         if( F1.ShapeType() != TopAbs_FACE )
1313           continue;
1314         TopoDS_Shape F2 = FF.FindFromIndex(j);
1315         TopExp_Explorer aExpW1(F1,TopAbs_WIRE);
1316         TopoDS_Wire aWire1 = TopoDS::Wire(aExpW1.Current());
1317         TopExp_Explorer aExpW2(F2,TopAbs_WIRE);
1318         TopoDS_Wire aWire2 = TopoDS::Wire(aExpW2.Current());
1319         // make pipe using aWire1 and aWire2
1320         if( !aWire1.IsNull() && !aWire2.IsNull() ) {
1321           BRepOffsetAPI_MakePipeShell aBuilder(WPath);
1322           aBuilder.Add(aWire1, TopoDS::Vertex(VLocs(i)),
1323                        aWithContact, aWithCorrect);
1324           aBuilder.Add(aWire2, TopoDS::Vertex(VLocs(i+1)),
1325                        aWithContact, aWithCorrect);
1326           if(!aBuilder.IsReady()) {
1327             if(aCI) delete aCI;
1328             Standard_ConstructionError::Raise("Invalid input data for building PIPE: bases are invalid");
1329           }
1330           aBuilder.Build();
1331           TopoDS_Shape aShape = aBuilder.Shape();
1332           TopoDS_Shell aShell;
1333           B.MakeShell(aShell);
1334           for ( anExp.Init( aShape, TopAbs_FACE ); anExp.More(); anExp.Next() ) {
1335             B.Add(aShell,anExp.Current());
1336           }
1337
1338           B.Add(aShell,F1);
1339           B.Add(aShell,F2);
1340           // make sewing for this shell
1341           Handle(BRepBuilderAPI_Sewing) aSewing = new BRepBuilderAPI_Sewing;
1342           aSewing->SetTolerance(Precision::Confusion());
1343           aSewing->SetFaceMode(Standard_True);
1344           aSewing->SetFloatingEdgesMode(Standard_False);
1345           aSewing->SetNonManifoldMode(Standard_False);
1346           for ( anExp.Init( aShell, TopAbs_FACE ); anExp.More(); anExp.Next() ) {
1347             aSewing->Add(anExp.Current());
1348           }
1349           aSewing->Perform();
1350           const TopoDS_Shape aSewShape = aSewing->SewedShape();
1351           if( aSewShape.ShapeType() == TopAbs_SHELL ) {
1352             aShell = TopoDS::Shell(aSewShape);
1353             GProp_GProps aSystem;
1354             BRepGProp::VolumeProperties(aShell, aSystem);
1355             if(aSystem.Mass()<0) {
1356               //cout<<"aSewShape is reversed"<<endl;
1357               aShell.Reverse();
1358             }
1359             if(BRep_Tool::IsClosed(aShell)) {
1360               TopoDS_Solid aSolid;
1361               B.MakeSolid(aSolid);
1362               B.Add(aSolid,aShell);
1363               B.Add(aComp,aSolid);
1364             }
1365             else {
1366               B.Add(aComp,aShell);
1367             }
1368           }
1369           else {
1370             B.Add(aComp,aShell);
1371           }
1372         }
1373       }
1374
1375     }
1376   }
1377
1378   //BRepTools::Write(aComp,"/dn02/users_Linux/skl/work/Bugs/14857/comp.brep");
1379   return aComp;
1380 }
1381
1382
1383 //=======================================================================
1384 //function : CreatePipeShellsWithoutPath
1385 //purpose  : auxilary for Execute()
1386 //=======================================================================
1387 static TopoDS_Shape CreatePipeShellsWithoutPath(GEOMImpl_IPipe* aCI)
1388 {
1389   //cout<<"CreatePipeShellsWithoutPath"<<endl;
1390   int i,j;
1391   BRep_Builder B;
1392
1393   GEOMImpl_IPipeShellSect* aCIDS = (GEOMImpl_IPipeShellSect*)aCI;
1394   // shell sections
1395   Handle(TColStd_HSequenceOfTransient) aBasesObjs = aCIDS->GetBases();
1396   // vertex for recognition
1397   Handle(TColStd_HSequenceOfTransient) VObjs = aCIDS->GetLocations();
1398
1399   Standard_Integer nbBases = aBasesObjs->Length(), 
1400     nbv = (VObjs.IsNull() ? 0 :VObjs->Length());
1401     
1402   if( nbv != nbBases ) {
1403     if(aCI) delete aCI;
1404     Standard_ConstructionError::Raise("Number of shapes for recognition is invalid");
1405   }
1406
1407
1408   TopTools_SequenceOfShape SecVs,Bases;
1409   for(i=1; i<=nbBases; i++) {
1410     // vertex
1411     Handle(Standard_Transient) anItem = VObjs->Value(i);
1412     if(anItem.IsNull())
1413       continue;
1414     Handle(GEOM_Function) aRef = Handle(GEOM_Function)::DownCast(anItem);
1415     TopoDS_Shape V = aRef->GetValue();
1416     if(V.IsNull() || V.ShapeType() != TopAbs_VERTEX)
1417       continue;
1418     SecVs.Append(V);
1419     // section
1420     anItem = aBasesObjs->Value(i);
1421     if(anItem.IsNull())
1422       continue;
1423     aRef = Handle(GEOM_Function)::DownCast(anItem);
1424     TopoDS_Shape aSh = aRef->GetValue();
1425     if(aSh.IsNull())
1426       continue;
1427     Bases.Append(aSh);
1428   }
1429   nbv = SecVs.Length();
1430   nbBases = Bases.Length();
1431   if( nbv != nbBases ) {
1432     if(aCI) delete aCI;
1433     Standard_ConstructionError::Raise("One of shapes for recognition is not a vertex");
1434   }
1435
1436   TopoDS_Compound aComp;
1437   B.MakeCompound(aComp);
1438
1439   for(i=1 ; i<nbBases; i++) {
1440     cout<<"Make pipe between sections "<<i<<" and "<<i+1<<endl;
1441     TopoDS_Shape aShBase1 = Bases.Value(i);
1442     TopoDS_Shape aShBase2 = Bases.Value(i+1);
1443     TopExp_Explorer anExp;
1444     Standard_Integer nbf1 = 0;
1445     for ( anExp.Init( aShBase1, TopAbs_FACE ); anExp.More(); anExp.Next() ) {
1446       nbf1++;
1447     }
1448     Standard_Integer nbf2 = 0;
1449     for ( anExp.Init( aShBase2, TopAbs_FACE ); anExp.More(); anExp.Next() ) {
1450       nbf2++;
1451     }
1452     //cout<<"nbf1="<<nbf1<<" nbf2="<<nbf2<<endl;
1453     if(nbf1!=nbf2) {
1454       if(aCI) delete aCI;
1455       Standard_ConstructionError::Raise("Different number of faces in the sections");
1456     }
1457
1458     TopTools_MapOfShape aFaces1,aFaces2;
1459     for ( anExp.Init( aShBase1, TopAbs_FACE ); anExp.More(); anExp.Next() ) {
1460       aFaces1.Add(anExp.Current());
1461     }
1462     for ( anExp.Init( aShBase2, TopAbs_FACE ); anExp.More(); anExp.Next() ) {
1463       aFaces2.Add(anExp.Current());
1464     }
1465
1466     // creating map of edge faces
1467     TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces1;
1468     TopExp::MapShapesAndAncestors(aShBase1, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces1);
1469     TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces2;
1470     TopExp::MapShapesAndAncestors(aShBase2, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces2);
1471
1472     // constuct map face->face (and subshapes)
1473     TopTools_IndexedDataMapOfShapeShape FF;
1474     //TopoDS_Shape FS1 = SecFs.Value(i), FS2 = SecFs.Value(i+1);
1475     TopoDS_Shape FS1, FS2;
1476     TopoDS_Vertex V1 = TopoDS::Vertex(SecVs(i));
1477     TopoDS_Vertex V2 = TopoDS::Vertex(SecVs(i+1));
1478     FindFirstPairFaces(aShBase1, aShBase2, V1, V2, FS1, FS2);
1479
1480     FF.Add(FS1,FS2);
1481     cout<<"  first pair of corresponding faces is found"<<endl;
1482
1483     // add pairs of edges and vertexes to FF
1484     bool stat =  FillCorrespondingEdges(FS1, FS2, V1, V2, FF);
1485     if( !stat ) {
1486       if(aCI) delete aCI;
1487       Standard_ConstructionError::Raise("Can not create correct pipe");
1488     }
1489     cout<<"  correspondences for subshapes of first pair of faces is found"<<endl;
1490
1491     FindNextPairOfFaces(FS1, aMapEdgeFaces1, aMapEdgeFaces2, FF, aCI);
1492     cout<<"  other correspondences is found, make pipe for all pairs of faces"<<endl;
1493
1494     // make pipe for each pair of faces
1495     // auxilary map vertex->edge for created pipe edges
1496     TopTools_IndexedDataMapOfShapeShape VPE;
1497     ShapeAnalysis_Edge sae;
1498     //cout<<"FF.Extent()="<<FF.Extent()<<endl;
1499     int nbff = 0;
1500     for(j=1; j<=FF.Extent(); j++) {
1501       TopoDS_Shape F1 = FF.FindKey(j);
1502       if( F1.ShapeType() != TopAbs_FACE )
1503         continue;
1504       TopoDS_Shape F2 = FF.FindFromIndex(j);
1505       nbff++;
1506
1507       //if(nbff!=3) continue;
1508
1509       cout<<"    make pipe for "<<nbff<<" face"<<endl;
1510
1511       Handle(Geom_Surface) S1 = BRep_Tool::Surface(TopoDS::Face(F1));
1512       if(S1->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
1513         Handle(Geom_RectangularTrimmedSurface) RTS = 
1514           Handle(Geom_RectangularTrimmedSurface)::DownCast(S1);
1515         S1 = RTS->BasisSurface();
1516       }
1517       Handle(Geom_Plane) Pln1 = 
1518           Handle(Geom_Plane)::DownCast(S1);
1519       if( Pln1.IsNull() ) {
1520         if(aCI) delete aCI;
1521         Standard_ConstructionError::Raise("Surface from face is not plane");
1522       }
1523       gp_Vec aDir1(Pln1->Axis().Direction());
1524
1525       Handle(Geom_Surface) S2 = BRep_Tool::Surface(TopoDS::Face(F2));
1526       if(S2->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
1527         Handle(Geom_RectangularTrimmedSurface) RTS = 
1528           Handle(Geom_RectangularTrimmedSurface)::DownCast(S2);
1529         S2 = RTS->BasisSurface();
1530       }
1531       Handle(Geom_Plane) Pln2 = 
1532           Handle(Geom_Plane)::DownCast(S2);
1533       if( Pln2.IsNull() ) {
1534         if(aCI) delete aCI;
1535         Standard_ConstructionError::Raise("Surface from face is not plane");
1536       }
1537       gp_Vec aDir2(Pln2->Axis().Direction());
1538
1539       gp_Pnt P1 = BRep_Tool::Pnt(TopoDS::Vertex(SecVs(i)));
1540       gp_Pnt P2 = BRep_Tool::Pnt(TopoDS::Vertex(SecVs(i+1)));
1541       gp_Vec aDir(P1,P2);
1542       if(fabs(aDir.Angle(aDir1))>PI/2.)
1543         aDir1.Reverse();
1544       if(fabs(aDir.Angle(aDir2))>PI/2.)
1545         aDir2.Reverse();
1546
1547       TopExp_Explorer anExpE(F1,TopAbs_EDGE);
1548       TopTools_SequenceOfShape aNewFs;
1549       //int nbee=0;
1550       for(; anExpE.More(); anExpE.Next()) {
1551         TopoDS_Edge E1 = TopoDS::Edge(anExpE.Current());
1552         //nbee++;
1553         if(!FF.Contains(E1))
1554           cout<<"map FF not contains key E1"<<endl;
1555
1556         if(VPE.Contains(E1)) {
1557           aNewFs.Append(VPE.FindFromKey(E1));
1558 #ifdef _DEBUG_
1559           cout<<"    using existed face"<<endl;
1560 #endif
1561           continue;
1562         }
1563
1564         TopoDS_Edge E3 = TopoDS::Edge(FF.FindFromKey(E1));
1565         TopoDS_Vertex V1 = sae.FirstVertex(E1);
1566         TopoDS_Vertex V2 = sae.LastVertex(E1);
1567         if(!FF.Contains(V1))
1568           cout<<"map FF not contains key V1"<<endl;
1569         if(!FF.Contains(V2))
1570           cout<<"map FF not contains key V2"<<endl;
1571         TopoDS_Vertex V3 = TopoDS::Vertex(FF.FindFromKey(V2));
1572         TopoDS_Vertex V4 = TopoDS::Vertex(FF.FindFromKey(V1));
1573         TopoDS_Vertex Vtmp = sae.FirstVertex(E3);
1574         if(Vtmp.IsSame(V4))
1575           E3.Reverse();
1576         gp_Pnt P1 = BRep_Tool::Pnt(V1);
1577         gp_Pnt P2 = BRep_Tool::Pnt(V2);
1578         gp_Pnt P3 = BRep_Tool::Pnt(V3);
1579         gp_Pnt P4 = BRep_Tool::Pnt(V4);
1580         // make E2
1581         TopoDS_Edge E2;
1582         Handle(Geom_BSplineCurve) C2;
1583         if(VPE.Contains(V2)) {
1584           E2 = TopoDS::Edge(VPE.FindFromKey(V2));
1585           double fp,lp;
1586           C2 = Handle(Geom_BSplineCurve)::DownCast(BRep_Tool::Curve(E2,fp,lp));
1587         }
1588         else {
1589           Handle(TColgp_HArray1OfPnt) HAP = new TColgp_HArray1OfPnt(1,2);
1590           HAP->SetValue(1,P2);
1591           HAP->SetValue(2,P3);
1592           GeomAPI_Interpolate anInt(HAP,Standard_False,1.e-7);
1593           anInt.Load(aDir1,aDir2);
1594           anInt.Perform();
1595           C2 = anInt.Curve();
1596           B.MakeEdge(E2,C2,1.e-7);
1597           B.Add(E2,TopoDS::Vertex(V2.Oriented(TopAbs_FORWARD)));
1598           B.Add(E2,TopoDS::Vertex(V3.Oriented(TopAbs_REVERSED)));
1599           VPE.Add(V2,E2);
1600         }
1601         // make E4
1602         TopoDS_Edge E4;
1603         Handle(Geom_BSplineCurve) C4;
1604         if(VPE.Contains(V1)) {
1605           E4 = TopoDS::Edge(VPE.FindFromKey(V1));
1606           double fp,lp;
1607           C4 = Handle(Geom_BSplineCurve)::DownCast(BRep_Tool::Curve(E4,fp,lp));
1608         }
1609         else {
1610           Handle(TColgp_HArray1OfPnt) HAP = new TColgp_HArray1OfPnt(1,2);
1611           HAP->SetValue(1,P1);
1612           HAP->SetValue(2,P4);
1613           GeomAPI_Interpolate anInt(HAP,Standard_False,1.e-7);
1614           anInt.Load(aDir1,aDir2);
1615           anInt.Perform();
1616           C4 = anInt.Curve();
1617           B.MakeEdge(E4,anInt.Curve(),1.e-7);
1618           B.Add(E4,TopoDS::Vertex(V1.Oriented(TopAbs_FORWARD)));
1619           B.Add(E4,TopoDS::Vertex(V4.Oriented(TopAbs_REVERSED)));
1620           VPE.Add(V1,E4);
1621         }
1622
1623         TopoDS_Wire W;
1624         B.MakeWire(W);
1625         B.Add(W,E1);
1626         B.Add(W,E2);
1627         B.Add(W,E3);
1628         B.Add(W,E4.Reversed());
1629         //cout<<"      wire for edge "<<nbee<<" is created"<<endl;
1630         //BRepTools::Write(W,"/dn02/users_Linux/skl/work/Bugs/14857/w.brep");
1631         
1632         // make surface
1633         
1634         double fp,lp;
1635         Handle(Geom_Curve) C1 = BRep_Tool::Curve(E1,fp,lp);
1636         //bool IsConicC1 = false;
1637         //if( C1->IsKind(STANDARD_TYPE(Geom_Conic)) ) {
1638         //  IsConicC1 = true;
1639         //  cout<<"C1 - Geom_Conic"<<endl;
1640         //}
1641         if( C1->IsKind(STANDARD_TYPE(Geom_Line)) || C1->IsKind(STANDARD_TYPE(Geom_Conic)) ) {
1642           C1 = new Geom_TrimmedCurve(C1,fp,lp);
1643         }
1644         //if(IsConicC1) {
1645         //  double tol = BRep_Tool::Tolerance(E1);
1646         //  GeomConvert_ApproxCurve ApxC1(C1,tol,GeomAbs_C1,10,5);
1647         //  C1 = ApxC1.Curve();
1648         //}
1649         Handle(Geom_Curve) C3 = BRep_Tool::Curve(E3,fp,lp);
1650         if( C3->IsKind(STANDARD_TYPE(Geom_Line)) || C3->IsKind(STANDARD_TYPE(Geom_Conic)) ) {
1651           C3 = new Geom_TrimmedCurve(C3,fp,lp);
1652         }
1653         //filebuf fic;
1654         //ostream os(&fic);
1655         //os.precision(15);
1656         Handle(Geom_BSplineCurve) CE1 =
1657           GeomConvert::CurveToBSplineCurve(C1,Convert_RationalC1);
1658         if(CE1->Degree()<3)
1659           CE1->IncreaseDegree(3);
1660         Handle(Geom_BSplineCurve) CE2 =
1661           GeomConvert::CurveToBSplineCurve(C2,Convert_RationalC1);
1662         if(CE2->Degree()<3)
1663           CE2->IncreaseDegree(3);
1664         Handle(Geom_BSplineCurve) CE3 =
1665           GeomConvert::CurveToBSplineCurve(C3,Convert_RationalC1);
1666         if(CE3->Degree()<3)
1667           CE3->IncreaseDegree(3);
1668         Handle(Geom_BSplineCurve) CE4 =
1669           GeomConvert::CurveToBSplineCurve(C4,Convert_RationalC1);
1670         if(CE4->Degree()<3)
1671           CE4->IncreaseDegree(3);
1672         //cout<<"CE1->Degree()="<<CE1->Degree()<<" CE2->Degree()="<<CE2->Degree()
1673         //    <<" CE3->Degree()="<<CE3->Degree()<<" CE4->Degree()="<<CE4->Degree()<<endl;
1674         //if(fic.open("/dn02/users_Linux/skl/work/Bugs/14857/ce1.brep",ios::out)) {
1675         //  os<<"DrawTrSurf_BSplineCurve"<<endl;
1676         //  GeomTools::Write(CE1,os);
1677         //  fic.close();
1678         //}
1679
1680         Handle(Geom_Surface) BS;
1681         try {
1682           GeomFill_BSplineCurves GF(CE1,CE2,CE3,CE4,GeomFill_CoonsStyle);
1683           //GeomFill_BSplineCurves GF(CE1,CE2,CE3,CE4,GeomFill_StretchStyle);
1684           BS = GF.Surface();
1685         }
1686         catch(...) {
1687           cout<<"      can not create BSplineSurface - create Bezier"<<endl;
1688           int NbP=26;
1689           TColgp_Array2OfPnt Points(1,NbP,1,NbP);
1690           double fp1,lp1,fp2,lp2;
1691           Handle(Geom_Curve) C1 = BRep_Tool::Curve(E1,fp1,lp1);
1692           Handle(Geom_Curve) C3 = BRep_Tool::Curve(E3,fp2,lp2);
1693           gp_Pnt P1C1,P2C1;
1694           C1->D0(fp1,P1C1);
1695           C1->D0(lp1,P2C1);
1696           gp_Pnt P1C3,P2C3;
1697           C3->D0(fp2,P1C3);
1698           C3->D0(lp2,P2C3);
1699           int n1,n2;
1700           double fp,lp;
1701           // get points from C1
1702           if(P1.Distance(P1C1)<1.e-6) {
1703             fp = fp1;
1704             lp = lp1;
1705           }
1706           else {
1707             fp = lp1;
1708             lp = fp1;
1709           }
1710           double step = (lp-fp)/(NbP-1);
1711           Points.SetValue(1,1,P1);
1712           double par = fp;
1713           for(n1=2; n1<NbP; n1++) {
1714             gp_Pnt P;
1715             par += step;
1716             C1->D0(par,P);
1717             Points.SetValue(1,n1,P);
1718           }
1719           Points.SetValue(1,NbP,P2);
1720           // get points from C3
1721           if(P4.Distance(P1C3)<1.e-6) {
1722             fp = fp2;
1723             lp = lp2;
1724           }
1725           else {
1726             fp = lp2;
1727             lp = fp2;
1728           }
1729           step = (lp-fp)/(NbP-1);
1730           Points.SetValue(NbP,1,P4);
1731           par = fp;
1732           for(n1=2; n1<NbP; n1++) {
1733             gp_Pnt P;
1734             par += step;
1735             C3->D0(par,P);
1736             Points.SetValue(NbP,n1,P);
1737           }
1738           Points.SetValue(NbP,NbP,P3);
1739           // create isolines and get points from them
1740           for(n1=1; n1<=NbP; n1++) {
1741             gp_Pnt PI1 = Points.Value(1,n1);
1742             gp_Pnt PI2 = Points.Value(NbP,n1);
1743             Handle(TColgp_HArray1OfPnt) HAP = new TColgp_HArray1OfPnt(1,2);
1744             HAP->SetValue(1,PI1);
1745             HAP->SetValue(2,PI2);
1746             GeomAPI_Interpolate anInt(HAP,Standard_False,1.e-7);
1747             anInt.Load(aDir1,aDir2);
1748             anInt.Perform();
1749             Handle(Geom_Curve) iso = anInt.Curve();
1750             fp = iso->FirstParameter();
1751             lp = iso->LastParameter();
1752             step = (lp-fp)/(NbP-1);
1753             par = fp;
1754             TopoDS_Compound VComp;
1755             B.MakeCompound(VComp);
1756             for(n2=2; n2<NbP; n2++) {
1757               gp_Pnt P;
1758               par += step;
1759               iso->D0(par,P);
1760               Points.SetValue(n2,n1,P);
1761             }
1762           }
1763           // create surface and face
1764           //Handle(Geom_BezierSurface) BS = new Geom_BezierSurface(Points);
1765           BS = new Geom_BezierSurface(Points);
1766         }
1767
1768         BRepBuilderAPI_MakeFace BB(BS,W);
1769         TopoDS_Face NewF = BB.Face();
1770         Handle(ShapeFix_Face) sff = new ShapeFix_Face(NewF);
1771         sff->Perform();
1772         sff->FixOrientation();
1773         TopoDS_Face FixedFace = sff->Face();
1774         aNewFs.Append(FixedFace);
1775         VPE.Add(E1,FixedFace);
1776         //cout<<"      face for edge "<<nbee<<" is created"<<endl;
1777         //BRepTools::Write(FixedFace,"/dn02/users_Linux/skl/work/Bugs/14857/f.brep");
1778       }
1779       // make shell
1780       TopoDS_Shell aShell;
1781       B.MakeShell(aShell);
1782       for(int nf=1; nf<=aNewFs.Length(); nf++) {
1783         B.Add(aShell,aNewFs(nf));
1784       }
1785       B.Add(aShell,F1);
1786       B.Add(aShell,F2);
1787       
1788       // make sewing for this shell
1789       Handle(BRepBuilderAPI_Sewing) aSewing = new BRepBuilderAPI_Sewing;
1790       aSewing->SetTolerance(Precision::Confusion());
1791       aSewing->SetFaceMode(Standard_True);
1792       aSewing->SetFloatingEdgesMode(Standard_False);
1793       aSewing->SetNonManifoldMode(Standard_False);
1794       for ( anExp.Init( aShell, TopAbs_FACE ); anExp.More(); anExp.Next() ) {
1795         aSewing->Add(anExp.Current());
1796       }
1797       aSewing->Perform();
1798       cout<<"    shell for face "<<nbff<<" is created"<<endl;
1799       const TopoDS_Shape aSewShape = aSewing->SewedShape();
1800       //BRepTools::Write(aSewShape,"/dn02/users_Linux/skl/work/Bugs/14857/sew.brep");
1801       if( aSewShape.ShapeType() == TopAbs_SHELL ) {
1802         aShell = TopoDS::Shell(aSewShape);
1803         GProp_GProps aSystem;
1804         BRepGProp::VolumeProperties(aShell, aSystem);
1805         if(aSystem.Mass()<0) {
1806           //cout<<"aSewShape is reversed"<<endl;
1807           aShell.Reverse();
1808         }
1809         if(BRep_Tool::IsClosed(aShell)) {
1810           TopoDS_Solid aSolid;
1811           B.MakeSolid(aSolid);
1812           B.Add(aSolid,aShell);
1813           B.Add(aComp,aSolid);
1814           cout<<"    solid for face "<<nbff<<" is created"<<endl;
1815         }
1816         else {
1817           B.Add(aComp,aShell);
1818           cout<<"    solid for face "<<nbff<<" is not created"<<endl;
1819         }
1820       }
1821       else {
1822         B.Add(aComp,aShell);
1823         cout<<"    solid for face "<<nbff<<" is not created"<<endl;
1824       }
1825       //cout<<"    solid for face "<<nbff<<" is created"<<endl;
1826       
1827       //Handle(ShapeFix_Shell) sfs = new ShapeFix_Shell(aShell);
1828       //sfs->Perform();
1829       //TopoDS_Shell FixedShell = sfs->Shell();
1830       /*
1831       GProp_GProps aSystem;
1832       BRepGProp::VolumeProperties(FixedShell, aSystem);
1833       if(aSystem.Mass()<0) {
1834         //cout<<"aSewShape is reversed"<<endl;
1835         FixedShell.Reverse();
1836       }
1837       if(BRep_Tool::IsClosed(FixedShell)) {
1838         TopoDS_Solid aSolid;
1839         B.MakeSolid(aSolid);
1840         B.Add(aSolid,aShell);
1841         B.Add(aComp,aSolid);
1842       }
1843       else {
1844         B.Add(aComp,FixedShell);
1845       }
1846       */
1847     }
1848   }
1849
1850   //BRepTools::Write(aComp,"/dn02/users_Linux/skl/work/Bugs/14857/comp.brep");
1851   return aComp;
1852 }
1853
1854
1855 //=======================================================================
1856 //function : Execute
1857 //purpose  :
1858 //=======================================================================
1859 Standard_Integer GEOMImpl_PipeDriver::Execute(TFunction_Logbook& log) const
1860 {
1861   //cout<<"PipeDriver::Execute"<<endl;
1862   if (Label().IsNull()) return 0;
1863   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
1864   GEOMImpl_IPipe* aCI= 0;
1865   Standard_Integer aType = aFunction->GetType();
1866   if(aType == PIPE_BASE_PATH)
1867     aCI = new GEOMImpl_IPipe(aFunction);
1868   else if(aType == PIPE_DIFFERENT_SECTIONS)
1869     aCI = new GEOMImpl_IPipeDiffSect(aFunction);
1870   else if(aType == PIPE_SHELL_SECTIONS)
1871     aCI = new GEOMImpl_IPipeShellSect(aFunction);
1872   else if(aType == PIPE_SHELLS_WITHOUT_PATH)
1873     aCI = new GEOMImpl_IPipeShellSect(aFunction);
1874   else
1875     return 0;
1876
1877   TopoDS_Wire aWirePath;
1878   if(aType != PIPE_SHELLS_WITHOUT_PATH) {
1879     // working with path
1880     Handle(GEOM_Function) aRefPath = aCI->GetPath();
1881     TopoDS_Shape aShapePath = aRefPath->GetValue();
1882
1883     if (aShapePath.IsNull()) {
1884       cout<<"Driver : path is null"<<endl;
1885       if(aCI) delete aCI;
1886       Standard_NullObject::Raise("MakePipe aborted : null path argument");
1887     }
1888   
1889     // Get path contour
1890     if (aShapePath.ShapeType() == TopAbs_WIRE) {
1891       aWirePath = TopoDS::Wire(aShapePath);
1892     } 
1893     else {
1894       if (aShapePath.ShapeType() == TopAbs_EDGE) {
1895         TopoDS_Edge anEdge = TopoDS::Edge(aShapePath);
1896         aWirePath = BRepBuilderAPI_MakeWire(anEdge);
1897       } 
1898       else {
1899         if(aCI) delete aCI;
1900         Standard_TypeMismatch::Raise("MakePipe aborted : path shape is neither a wire nor an edge");
1901       } 
1902     }
1903   }
1904
1905   TopoDS_Shape aShape;
1906
1907   if (aType == PIPE_BASE_PATH) {
1908      
1909     Handle(GEOM_Function) aRefBase = aCI->GetBase();
1910    
1911     TopoDS_Shape aShapeBase = aRefBase->GetValue();
1912     
1913     if (aShapeBase.IsNull()) {
1914       if(aCI) delete aCI;
1915       Standard_NullObject::Raise("MakePipe aborted : null base argument");
1916     }
1917
1918     // Make pipe
1919     aShape = BRepOffsetAPI_MakePipe(aWirePath, aShapeBase);
1920   }
1921
1922   //building pipe with different sections
1923   else if (aType == PIPE_DIFFERENT_SECTIONS) {
1924     GEOMImpl_IPipeDiffSect* aCIDS = (GEOMImpl_IPipeDiffSect*)aCI;
1925     //GEOMImpl_IPipeDiffSect* aCIDS = static_cast<GEOMImpl_IPipeDiffSect*>(aCI);
1926     //BRepOffsetAPI_MakePipeShell aBuilder(aWirePath);
1927     Handle(TColStd_HSequenceOfTransient) aBasesObjs = aCIDS->GetBases ();
1928     Handle(TColStd_HSequenceOfTransient) aLocObjs = aCIDS->GetLocations ();
1929     Standard_Boolean aWithContact = (aCIDS->GetWithContactMode());
1930     Standard_Boolean aWithCorrect = (aCIDS->GetWithCorrectionMode());
1931
1932     Standard_Integer i =1, nbBases = aBasesObjs->Length(), 
1933       nbLocs = (aLocObjs.IsNull() ? 0 :aLocObjs->Length());
1934     
1935     if(nbLocs && nbLocs != nbBases) {
1936       if(aCI) delete aCI;
1937       Standard_ConstructionError::Raise("Number of sections is not equal to number of locations ");
1938     }
1939     TopTools_SequenceOfShape aSeqBases;
1940     TopTools_SequenceOfShape aSeqLocs;
1941     TopTools_SequenceOfShape aSeqFaces;
1942     for( ; i <= nbBases; i++) {
1943       Handle(Standard_Transient) anItem = aBasesObjs->Value(i);
1944       if(anItem.IsNull())
1945         continue;
1946       Handle(GEOM_Function) aRefBase = Handle(GEOM_Function)::DownCast(anItem);
1947       if(aRefBase.IsNull())
1948         continue;
1949       TopoDS_Shape aShapeBase = aRefBase->GetValue();
1950       if(aShapeBase.IsNull())
1951         continue;
1952       TopAbs_ShapeEnum aTypeBase = aShapeBase.ShapeType();
1953
1954       //if for section was specified face with a few wires then a few
1955       //    pipes were build and make solid 
1956       Standard_Boolean NeedCreateSolid = Standard_False;
1957       if(aTypeBase == TopAbs_SHELL) {
1958         // create wire as boundary contour if shell is no closed
1959         // get free boundary shapes
1960         ShapeAnalysis_FreeBounds anAnalizer( aShapeBase );
1961         TopoDS_Compound aClosed = anAnalizer.GetClosedWires();
1962         TopExp_Explorer anExp;
1963         TopoDS_Shape aWire;
1964         Standard_Integer NbWires = 0;
1965         for ( anExp.Init( aClosed, TopAbs_WIRE ); anExp.More(); anExp.Next() ) {
1966           NbWires++;
1967           aWire = anExp.Current();
1968         }
1969         if(NbWires!=1) {
1970           // bad case
1971           if(aCI) delete aCI;
1972           Standard_ConstructionError::Raise("Bad shell is used as section ");
1973         }
1974         NeedCreateSolid = Standard_True;
1975         aSeqFaces.Append(aShapeBase);
1976         aSeqBases.Append(aWire);
1977       }
1978       else if(aTypeBase == TopAbs_FACE) {
1979         NeedCreateSolid = Standard_True;
1980         //for case one path should be used other type function
1981         aSeqFaces.Append(aShapeBase);
1982         TopExp_Explorer aExpW(aShapeBase,TopAbs_WIRE);
1983         for( ; aExpW.More(); aExpW.Next())
1984         {
1985           TopoDS_Shape aWireProf = aExpW.Current();
1986           aSeqBases.Append(aWireProf);
1987         }
1988       }
1989       else if(aTypeBase == TopAbs_WIRE || aTypeBase == TopAbs_VERTEX) {
1990         aSeqBases.Append(aShapeBase);
1991       }
1992       else if(aTypeBase == TopAbs_EDGE) {
1993         TopoDS_Edge anEdge = TopoDS::Edge(aShapeBase);
1994         TopoDS_Shape aWireProf = BRepBuilderAPI_MakeWire(anEdge);
1995         aSeqBases.Append(aWireProf);
1996       }
1997       if(nbLocs) {
1998         Handle(Standard_Transient) anItemLoc = aLocObjs->Value(i);
1999         if(anItemLoc.IsNull())
2000           continue;
2001         Handle(GEOM_Function) aRefLoc = Handle(GEOM_Function)::DownCast(anItemLoc);
2002         TopoDS_Shape aShapeLoc = aRefLoc->GetValue();
2003         if(aShapeLoc.IsNull() || aShapeLoc.ShapeType() != TopAbs_VERTEX)
2004           continue;
2005         aSeqLocs.Append(aShapeLoc);
2006       }
2007     }
2008     
2009     nbLocs = aSeqLocs.Length();
2010
2011     // skl 02.05.2007
2012     TopTools_SequenceOfShape Edges;
2013     if(nbLocs>0) {
2014       // we have to check that each location shape is a vertex from
2015       // path and update aSeqLocs if it is needed (and possible)
2016       TColgp_SequenceOfPnt PLocs;
2017       for(i=1; i<=nbLocs; i++) {
2018         TopoDS_Vertex V = TopoDS::Vertex(aSeqLocs.Value(i));
2019         PLocs.Append(BRep_Tool::Pnt(V));
2020       }
2021       //TopTools_SequenceOfShape Edges;
2022       TopExp_Explorer anExp;
2023       for ( anExp.Init( aWirePath, TopAbs_EDGE ); anExp.More(); anExp.Next() ) {
2024         Edges.Append(anExp.Current());
2025       }
2026       int nbEdges = Edges.Length();
2027       ShapeAnalysis_Edge sae;
2028       TopoDS_Edge edge = TopoDS::Edge(Edges.First());
2029       double tol = BRep_Tool::Tolerance(edge);
2030       TopoDS_Vertex VF = sae.FirstVertex(edge);
2031       gp_Pnt PF = BRep_Tool::Pnt(VF);
2032       //cout<<"PF("<<PF.X()<<","<<PF.Y()<<","<<PF.Z()<<")"<<endl;
2033       if( PF.Distance(PLocs.First()) > tol ) {
2034         if(aCI) delete aCI;
2035         Standard_ConstructionError::Raise
2036           ("First location shapes is not coincided with first vertex of aWirePath");
2037       }
2038       aSeqLocs.ChangeValue(1) = VF;
2039       edge = TopoDS::Edge(Edges.Last());
2040       tol = BRep_Tool::Tolerance(edge);
2041       TopoDS_Vertex VL = sae.LastVertex(edge);
2042       gp_Pnt PL = BRep_Tool::Pnt(VL);
2043       if( PL.Distance(PLocs.Last()) > tol ) {
2044         if(aCI) delete aCI;
2045         Standard_ConstructionError::Raise
2046           ("Last location shapes is not coincided with last vertex of aWirePath");
2047       }
2048       aSeqLocs.ChangeValue(nbLocs) = VL;
2049       int jcurr = 2;
2050       for(i=1; i<=Edges.Length() && jcurr<nbLocs; i++) {
2051         TopoDS_Edge E = TopoDS::Edge(Edges.Value(i));
2052         tol = BRep_Tool::Tolerance(edge);
2053         TopoDS_Vertex V1 = sae.FirstVertex(E);
2054         TopoDS_Vertex V2 = sae.LastVertex(E);
2055         gp_Pnt P1 = BRep_Tool::Pnt(V1);
2056         gp_Pnt P2 = BRep_Tool::Pnt(V2);
2057         if( P2.Distance(PLocs.Value(jcurr)) < tol ) {
2058           aSeqLocs.ChangeValue(jcurr) = V2;
2059           jcurr++;
2060         }
2061         else {
2062           // find distance between E and aLocs(jcurr)
2063           double fp,lp;
2064           Handle(Geom_Curve) C = BRep_Tool::Curve(E,fp,lp);
2065           GeomAPI_ProjectPointOnCurve PPC (PLocs.Value(jcurr),C);
2066           if( PPC.NbPoints()>0 &&
2067               PLocs.Value(jcurr).Distance(PPC.Point(1)) < tol ) {
2068             double param = PPC.Parameter(1);
2069             gp_Pnt PC1;
2070             C->D0(param,PC1);
2071             // split current edge
2072             Handle(Geom_TrimmedCurve) tc1 = new Geom_TrimmedCurve(C,fp,param);
2073             Handle(Geom_TrimmedCurve) tc2 = new Geom_TrimmedCurve(C,param,lp);
2074             TopoDS_Edge E1,E2;
2075             BRep_Builder B;
2076             gp_Pnt Pfp;
2077             C->D0(fp,Pfp);
2078             if(Pfp.Distance(P1)<tol) {
2079               B.MakeEdge(E1,tc1,tol);
2080               B.Add(E1,V1);
2081               TopoDS_Shape tmpV = aSeqLocs.Value(jcurr).Oriented(TopAbs_REVERSED);
2082               B.Add(E1,TopoDS::Vertex(tmpV));
2083               B.MakeEdge(E2,tc2,tol);
2084               tmpV = aSeqLocs.Value(jcurr).Oriented(TopAbs_FORWARD);
2085               B.Add(E2,TopoDS::Vertex(tmpV));
2086               B.Add(E2,V2);
2087             }
2088             else {
2089               B.MakeEdge(E1,tc2,tol);
2090               TopoDS_Shape tmpV = aSeqLocs.Value(jcurr).Oriented(TopAbs_FORWARD);
2091               B.Add(E1,TopoDS::Vertex(tmpV));
2092               B.Add(E1,V1);
2093               E1.Reverse();
2094               B.MakeEdge(E2,tc1,tol);
2095               B.Add(E2,V2);
2096               tmpV = aSeqLocs.Value(jcurr).Oriented(TopAbs_REVERSED);
2097               B.Add(E2,TopoDS::Vertex(tmpV));
2098               E2.Reverse();
2099             }
2100             jcurr++;
2101             Edges.Remove(i);
2102             Edges.InsertAfter(i-1,E1);
2103             Edges.InsertAfter(i,E2);
2104           }
2105         }
2106       }
2107       if(nbEdges<Edges.Length()) {
2108         // one of edges was splitted => we have to update WirePath
2109         BRep_Builder B;
2110         TopoDS_Wire W;
2111         B.MakeWire(W);
2112         for(i=1; i<=Edges.Length(); i++) {
2113           B.Add(W,TopoDS::Edge(Edges.Value(i)));
2114         }
2115         aWirePath = W;
2116       }
2117     }
2118
2119     // check curvature of wire for condition that
2120     // max summary angle between directions along
2121     // wire path must be < 4*PI. If not - split wire
2122     // and seguences of shapes, perform pipe for each
2123     // and make sewing after that
2124     double fp,lp;
2125     Handle(Geom_Curve) C = BRep_Tool::Curve(TopoDS::Edge(Edges.Value(1)),fp,lp);
2126     gp_Pnt P1,P2;
2127     gp_Vec Vec1,Vec2;
2128     C->D1(fp,P1,Vec1);
2129     C->D1(lp,P2,Vec2);
2130     double SumAng = fabs(Vec1.Angle(Vec2));
2131     Vec1 = Vec2;
2132     P1 = P2;
2133     TColStd_SequenceOfInteger SplitEdgeNums,SplitLocNums;
2134     int LastLoc = 1;
2135     //cout<<"Edges.Length()="<<Edges.Length()<<endl;
2136     for(i=2; i<=Edges.Length(); i++) {
2137       TopoDS_Edge edge = TopoDS::Edge(Edges.Value(i));
2138       double tol = BRep_Tool::Tolerance(edge);
2139       Handle(Geom_Curve) C = BRep_Tool::Curve(edge,fp,lp);
2140       C->D1(lp,P2,Vec2);
2141       double ang = fabs(Vec1.Angle(Vec2));
2142       SumAng += ang;
2143       if(SumAng>4*PI) {
2144         SumAng = ang;
2145         SplitEdgeNums.Append(i-1);
2146         int j;
2147         for(j=LastLoc+1; j<=aSeqLocs.Length(); j++) {
2148           TopoDS_Vertex aVert = TopoDS::Vertex(aSeqLocs.Value(j));
2149           gp_Pnt P = BRep_Tool::Pnt(aVert);
2150           if( P1.Distance(P) < tol ) {
2151             SplitLocNums.Append(j);
2152             LastLoc = j;
2153             break;
2154           }
2155         }
2156       }
2157       Vec1 = Vec2;
2158       P1 = P2;
2159     }
2160
2161     //cout<<"SplitEdgeNums.Length()="<<SplitEdgeNums.Length()<<endl;
2162     //cout<<"SplitLocNums.Length()="<<SplitLocNums.Length()<<endl;
2163     if( SplitLocNums.Length()==SplitEdgeNums.Length() && SplitEdgeNums.Length()>0 ) {
2164       TopTools_SequenceOfShape aSeqRes;
2165       int nn, num1 = 1, num2 = 1;
2166       for(nn=1; nn<=SplitEdgeNums.Length(); nn++) {
2167         // create wirepath and sequences of shapes
2168         BRep_Builder B;
2169         TopoDS_Wire tmpW;
2170         B.MakeWire(tmpW);
2171         for(i=num1; i<=SplitEdgeNums.Value(nn); i++) {
2172           B.Add(tmpW,TopoDS::Edge(Edges.Value(i)));
2173         }
2174         num1 = SplitEdgeNums.Value(nn) + 1;
2175         TopTools_SequenceOfShape aTmpSeqBases;
2176         TopTools_SequenceOfShape aTmpSeqLocs;
2177         for(i=num2; i<=SplitLocNums.Value(nn); i++) {
2178           aTmpSeqBases.Append(aSeqBases.Value(i));
2179           aTmpSeqLocs.Append(aSeqLocs.Value(i));
2180         }
2181         num2 = SplitLocNums.Value(nn);
2182         // make pipe
2183         BRepOffsetAPI_MakePipeShell aBuilder(tmpW);
2184         Standard_Integer nbShapes = aTmpSeqBases.Length();
2185         for(i=1; i<=nbShapes; i++) {
2186           TopoDS_Shape aShapeLoc = aTmpSeqLocs.Value(i);
2187           TopoDS_Vertex aVert = TopoDS::Vertex(aShapeLoc);
2188           aBuilder.Add(aTmpSeqBases.Value(i), aVert, aWithContact, aWithCorrect);
2189         }
2190         if(!aBuilder.IsReady()) {
2191           if(aCI) delete aCI;
2192           Standard_ConstructionError::Raise("Invalid input data for building PIPE: bases are invalid");
2193         }
2194         aBuilder.Build();
2195         TopoDS_Shape resShape = aBuilder.Shape();
2196         aSeqRes.Append(resShape);       
2197       }
2198       // create wirepath and sequences of shapes for last part
2199       BRep_Builder B;
2200       TopoDS_Wire tmpW;
2201       B.MakeWire(tmpW);
2202       for(i=num1; i<=Edges.Length(); i++) {
2203         B.Add(tmpW,TopoDS::Edge(Edges.Value(i)));
2204       }
2205       TopTools_SequenceOfShape aTmpSeqBases;
2206       TopTools_SequenceOfShape aTmpSeqLocs;
2207       for(i=num2; i<=aSeqLocs.Length(); i++) {
2208         aTmpSeqBases.Append(aSeqBases.Value(i));
2209         aTmpSeqLocs.Append(aSeqLocs.Value(i));
2210       }
2211       // make pipe for last part
2212       BRepOffsetAPI_MakePipeShell aBuilder(tmpW);
2213       Standard_Integer nbShapes = aTmpSeqBases.Length();
2214       for(i=1; i<=nbShapes; i++) {
2215         TopoDS_Shape aShapeLoc = aTmpSeqLocs.Value(i);
2216         TopoDS_Vertex aVert = TopoDS::Vertex(aShapeLoc);
2217         aBuilder.Add(aTmpSeqBases.Value(i), aVert, aWithContact, aWithCorrect);
2218       }
2219       if(!aBuilder.IsReady()) {
2220         if(aCI) delete aCI;
2221         Standard_ConstructionError::Raise("Invalid input data for building PIPE: bases are invalid");
2222       }
2223       aBuilder.Build();
2224       TopoDS_Shape resShape = aBuilder.Shape();
2225       aSeqRes.Append(resShape);
2226       // make sewing for result
2227       Handle(BRepBuilderAPI_Sewing) aSewing = new BRepBuilderAPI_Sewing;
2228       aSewing->SetTolerance(Precision::Confusion());
2229       aSewing->SetFaceMode(Standard_True);
2230       aSewing->SetFloatingEdgesMode(Standard_False);
2231       aSewing->SetNonManifoldMode(Standard_False);
2232       for(i=1; i<=aSeqRes.Length(); i++) {
2233         aSewing->Add(aSeqRes.Value(i));
2234       }
2235       aSewing->Perform();
2236       aShape = aSewing->SewedShape();
2237     }
2238     else {
2239       // old implementation without splitting
2240       BRepOffsetAPI_MakePipeShell aBuilder(aWirePath);
2241
2242       Standard_Integer nbShapes = aSeqBases.Length();
2243       Standard_Integer step = nbShapes/nbBases;
2244     
2245       if(nbShapes < nbBases || fmod((double)nbShapes, (double)nbBases)) {
2246         if(aCI) delete aCI;
2247         Standard_ConstructionError::Raise("Invalid sections were specified for building pipe");
2248       }
2249       Standard_Integer ind =0;   
2250       for( i=1; i  <= nbShapes && ind < nbShapes; i++) { //i+nbBases <= nbShapes
2251         TopTools_SequenceOfShape usedBases;
2252         Standard_Integer j = 1;
2253         for( ; j <= nbBases ; j++) {
2254           ind = i + (j-1)*step;
2255           TopoDS_Shape aWireProf = aSeqBases.Value(ind);
2256           usedBases.Append(aWireProf);
2257           if(nbLocs) {
2258             TopoDS_Shape aShapeLoc = aSeqLocs.Value(j);
2259             TopoDS_Vertex aVert = TopoDS::Vertex(aShapeLoc);
2260             aBuilder.Add(aWireProf,aVert,aWithContact,aWithCorrect);
2261           }
2262           else
2263             aBuilder.Add(aWireProf,aWithContact,aWithCorrect);
2264         }
2265       if(!aBuilder.IsReady()) {
2266         if(aCI) delete aCI;
2267         Standard_ConstructionError::Raise("Invalid input data for building PIPE: bases are invalid");
2268       }
2269       aBuilder.Build();
2270       aShape = aBuilder.Shape();
2271       aSeqFaces.Append(aShape); 
2272       for( j = 1; j <=usedBases.Length(); j++)
2273         aBuilder.Delete(usedBases.Value(j));
2274       }
2275         
2276       //for case if section is face 
2277       if(aSeqFaces.Length() >1) {
2278         BRep_Builder aB;
2279         TopoDS_Compound aComp;
2280         aB.MakeCompound(aComp);
2281         for( i = 1; i <= aSeqFaces.Length(); i++)
2282           aB.Add(aComp,aSeqFaces.Value(i));
2283         aShape = aComp;
2284       }
2285     }
2286   }
2287
2288   //building pipe with shell sections
2289   else if (aType == PIPE_SHELL_SECTIONS) {
2290     aShape = CreatePipeForShellSections(aWirePath,aCI);
2291   }
2292
2293   //building pipe shell sections without path
2294   else if (aType == PIPE_SHELLS_WITHOUT_PATH) {
2295     aShape = CreatePipeShellsWithoutPath(aCI);
2296   }
2297
2298   if (aShape.IsNull()) return 0;
2299
2300   BRepCheck_Analyzer ana (aShape, Standard_False);
2301   if (!ana.IsValid()) {
2302     if(aCI) delete aCI;
2303     Standard_ConstructionError::Raise("Algorithm have produced an invalid shape result");
2304   }
2305
2306   TopoDS_Shape aRes = GEOMImpl_IShapesOperations::CompsolidToCompound(aShape);
2307   aFunction->SetValue(aRes);
2308
2309   log.SetTouched(Label());
2310   if(aCI) delete aCI;
2311   return 1;
2312 }
2313
2314
2315 //=======================================================================
2316 //function :  GEOMImpl_PipeDriver_Type_
2317 //purpose  :
2318 //=======================================================================
2319 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PipeDriver_Type_()
2320 {
2321
2322   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
2323   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
2324   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
2325   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
2326   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
2327   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
2328
2329
2330   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
2331   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PipeDriver",
2332                                                          sizeof(GEOMImpl_PipeDriver),
2333                                                          1,
2334                                                          (Standard_Address)_Ancestors,
2335                                                          (Standard_Address)NULL);
2336
2337   return _aType;
2338 }
2339
2340 //=======================================================================
2341 //function : DownCast
2342 //purpose  :
2343 //=======================================================================
2344 const Handle(GEOMImpl_PipeDriver) Handle(GEOMImpl_PipeDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
2345 {
2346   Handle(GEOMImpl_PipeDriver) _anOtherObject;
2347
2348   if (!AnObject.IsNull()) {
2349      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PipeDriver))) {
2350        _anOtherObject = Handle(GEOMImpl_PipeDriver)((Handle(GEOMImpl_PipeDriver)&)AnObject);
2351      }
2352   }
2353
2354   return _anOtherObject ;
2355 }
2356