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