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