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