Salome HOME
0022869: EDF 7482 GEOM: Automatically create groups with the generation operations
[modules/geom.git] / src / GEOMImpl / GEOMImpl_PipeDriver.cxx
index 029595cbf609971a3ee76a6b2b36984798bddb95..4768d35f11720613e68e82d826d7692f15b35db7 100644 (file)
 #include <TopoDS_Shell.hxx>
 #include <TopoDS_Face.hxx>
 #include <TopoDS_Compound.hxx>
+#include <TopTools_DataMapOfShapeSequenceOfShape.hxx>
 #include <TopTools_SequenceOfShape.hxx>
 #include <TopTools_HSequenceOfShape.hxx>
 #include <TopTools_IndexedDataMapOfShapeShape.hxx>
 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
 #include <TopTools_ListIteratorOfListOfShape.hxx>
+#include <TopTools_MapIteratorOfMapOfShape.hxx>
 
 #include <GProp_GProps.hxx>
 
 
 #include "utilities.h"
 
+#define GROUP_DOWN  0
+#define GROUP_UP    1
+#define GROUP_SIDE1 2
+#define GROUP_SIDE2 3
+#define GROUP_OTHER 4
+
+static bool FillGroups(const TopTools_SequenceOfShape         *theGroups,
+                       const TopTools_IndexedMapOfShape       &theIndices,
+                             Handle(TColStd_HArray1OfInteger) *theGroupIds);
+
+static void StoreGroups(GEOMImpl_IPipe                   *theCI,
+                        Handle(TColStd_HArray1OfInteger) *theGroups);
+
+static bool DoGroups(BRepOffsetAPI_MakePipeShell &theSweep,
+                     TopTools_SequenceOfShape    *theGroups);
+
+static bool CreateGroups(BRepOffsetAPI_MakePipeShell &theSweep,
+                         GEOMImpl_IPipe              *theCI);
 
 //=======================================================================
 //function : GetID
@@ -277,15 +297,6 @@ static bool FillCorrespondingEdges(const TopoDS_Shape& FS1,
   BuildPipeShell(aBuilder);
 
   TopoDS_Shape aShape = aBuilder.Shape();
-  /*
-  TopoDS_Compound C;
-  BRep_Builder B;
-  B.MakeCompound(C);
-  B.Add(C,aShape);
-  B.Add(C,FS1);
-  B.Add(C,FS2);
-  BRepTools::Write(C,"/dn02/users_Linux/skl/work/Bugs/14857/comp.brep");
-  */
   ShapeAnalysis_Edge sae;
   double tol = Max(BRep_Tool::Tolerance(TopoDS::Face(FS1)),
                     BRep_Tool::Tolerance(TopoDS::Face(FS2)));
@@ -680,11 +691,12 @@ static void FindFirstPairFaces(const TopoDS_Shape& S1, const TopoDS_Shape& S2,
 //purpose  :
 //=======================================================================
 TopoDS_Shape GEOMImpl_PipeDriver::CreatePipeWithDifferentSections
-                                  (const TopoDS_Wire& theWirePath,
-                                   const Handle(TopTools_HSequenceOfShape) theHSeqBases,
-                                   const Handle(TopTools_HSequenceOfShape) theHSeqLocs,
-                                   const Standard_Boolean theWithContact,
-                                   const Standard_Boolean theWithCorrect)
+                  (const TopoDS_Wire                       &theWirePath,
+                   const Handle(TopTools_HSequenceOfShape)  theHSeqBases,
+                   const Handle(TopTools_HSequenceOfShape)  theHSeqLocs,
+                   const Standard_Boolean                   theWithContact,
+                   const Standard_Boolean                   theWithCorrect,
+                         Handle(TColStd_HArray1OfInteger)  *theGroups)
 {
   TopoDS_Shape aShape;
 
@@ -916,8 +928,12 @@ TopoDS_Shape GEOMImpl_PipeDriver::CreatePipeWithDifferentSections
       P1 = P2;
   }
 
+  bool isCreateGroups = (theGroups != NULL);
+
   if (SplitLocNums.Length()==SplitEdgeNums.Length() && SplitEdgeNums.Length()>0) {
-      TopTools_SequenceOfShape aSeqRes;
+      TopTools_SequenceOfShape               aSeqRes;
+      TopTools_DataMapOfShapeSequenceOfShape aMapResGroups[5];
+      Standard_Integer                       iGrp;
       int nn, num1 = 1, num2 = 1;
       for (nn=1; nn<=SplitEdgeNums.Length(); nn++) {
         // create wirepath and sequences of shapes
@@ -954,6 +970,21 @@ TopoDS_Shape GEOMImpl_PipeDriver::CreatePipeWithDifferentSections
 
         TopoDS_Shape resShape = aBuilder.Shape();
         aSeqRes.Append(resShape);
+
+        // Create groups.
+        if (isCreateGroups) {
+          // Make groups.
+          TopTools_SequenceOfShape aGroups[5];
+
+          if (!DoGroups(aBuilder, aGroups)) {
+            Standard_ConstructionError::Raise("Generate groups failure");
+          }
+
+          // Get shapes from all groups.
+          for (iGrp = 0; iGrp < 5; ++iGrp) {
+            aMapResGroups[iGrp].Bind(resShape, aGroups[iGrp]);
+          }
+        }
       }
       // create wirepath and sequences of shapes for last part
       BRep_Builder B;
@@ -987,6 +1018,22 @@ TopoDS_Shape GEOMImpl_PipeDriver::CreatePipeWithDifferentSections
 
       TopoDS_Shape resShape = aBuilder.Shape();
       aSeqRes.Append(resShape);
+
+      // Create groups.
+      if (isCreateGroups) {
+        // Make groups.
+        TopTools_SequenceOfShape aGroups[5];
+
+        if (!DoGroups(aBuilder, aGroups)) {
+          Standard_ConstructionError::Raise("Generate groups failure");
+        }
+
+        // Get shapes from all groups.
+        for (iGrp = 0; iGrp < 5; ++iGrp) {
+          aMapResGroups[iGrp].Bind(resShape, aGroups[iGrp]);
+        }
+      }
+
       // make sewing for result
       Handle(BRepBuilderAPI_Sewing) aSewing = new BRepBuilderAPI_Sewing;
       aSewing->SetTolerance(Precision::Confusion());
@@ -998,6 +1045,56 @@ TopoDS_Shape GEOMImpl_PipeDriver::CreatePipeWithDifferentSections
       }
       aSewing->Perform();
       aShape = aSewing->SewedShape();
+
+      if (isCreateGroups) {
+        // Replase Group shapes by modified ones.
+        TopTools_SequenceOfShape aSeqGroups[5];
+
+        // For each group.
+        for (iGrp = 0; iGrp < 5; ++iGrp) {
+          // For each pipe
+          for (i = 1; i <= aSeqRes.Length(); ++i) {
+            if (iGrp == GROUP_DOWN && i > 1) {
+              // For DOWN group we use only the first pipe.
+              continue;
+            }
+
+            if (iGrp == GROUP_UP && i < aSeqRes.Length()) {
+              // For UP group we use only the last pipe.
+              continue;
+            }
+
+            const TopTools_SequenceOfShape &aShapes =
+              aMapResGroups[iGrp].Find(aSeqRes.Value(i));
+            Standard_Integer                j;
+
+            // For each sub-shape of pipe
+            for (j = 1; j <= aShapes.Length(); ++j) {
+              const TopoDS_Shape &aGrpShape = aShapes.Value(j);
+
+              if (aSewing->IsModifiedSubShape(aGrpShape)) {
+                // Use the shape modified by sewing.
+                const TopoDS_Shape &aModifGrpShape =
+                  aSewing->ModifiedSubShape(aGrpShape);
+
+                aSeqGroups[iGrp].Append(aModifGrpShape);
+              } else {
+                // Use the shape as it is.
+                aSeqGroups[iGrp].Append(aGrpShape);
+              }
+            }
+          }
+        }
+
+        // Fill groups
+        TopTools_IndexedMapOfShape anIndices;
+
+        TopExp::MapShapes(aShape, anIndices);
+
+        if (!FillGroups(aSeqGroups, anIndices, theGroups)) {
+          Standard_ConstructionError::Raise("Generate groups failure");
+        }
+      }
   }
   else {
       // old implementation without splitting
@@ -1047,6 +1144,26 @@ TopoDS_Shape GEOMImpl_PipeDriver::CreatePipeWithDifferentSections
           Standard_ConstructionError::Raise("Pipe construction failure");
         }
         aShape = aBuilder.Shape();
+
+        if (isCreateGroups) {
+          // Make groups.
+          TopTools_SequenceOfShape aSeqGroups[5];
+
+          if (!DoGroups(aBuilder, aSeqGroups)) {
+            Standard_ConstructionError::Raise("Generate groups failure");
+          }
+
+          // Fill the groups.
+          Handle(TColStd_HArray1OfInteger) aGroupIds[5];
+          TopTools_IndexedMapOfShape       anIndices;
+          const TopoDS_Shape               aResult = aBuilder.Shape();
+
+          TopExp::MapShapes(aResult, anIndices);
+
+          if (!FillGroups(aSeqGroups, anIndices, theGroups)) {
+            Standard_ConstructionError::Raise("Generate groups failure");
+          }
+        }
         aSeqFaces.Append(aShape);
         for (j = 1; j <=usedBases.Length(); j++)
           aBuilder.Delete(usedBases.Value(j));
@@ -1063,8 +1180,6 @@ TopoDS_Shape GEOMImpl_PipeDriver::CreatePipeWithDifferentSections
 static TopoDS_Shape CreatePipeForShellSections(const TopoDS_Wire& aWirePath,
                                                GEOMImpl_IPipe* aCI)
 {
-  //cout<<"CreatePipeForShellSections"<<endl;
-  //TopoDS_Shape res;
   int i,j;
   BRep_Builder B;
 
@@ -1074,6 +1189,7 @@ static TopoDS_Shape CreatePipeForShellSections(const TopoDS_Wire& aWirePath,
   Handle(TColStd_HSequenceOfTransient) aLocObjs = aCIDS->GetLocations();
   Standard_Boolean aWithContact = (aCIDS->GetWithContactMode());
   Standard_Boolean aWithCorrect = (aCIDS->GetWithCorrectionMode());
+  Standard_Boolean isGenerateGroups = aCIDS->GetGenerateGroups();
 
   Standard_Integer nbBases = aBasesObjs->Length(),
     nbSubBases = (aSubBasesObjs.IsNull() ? 0 :aSubBasesObjs->Length()),
@@ -1088,8 +1204,6 @@ static TopoDS_Shape CreatePipeForShellSections(const TopoDS_Wire& aWirePath,
     Standard_ConstructionError::Raise("Number of sections is not equal to number of subsections ");
   }
 
-  //BRepOffsetAPI_MakePipeShell aBuilder(aWirePath);
-
   TopTools_SequenceOfShape VLocs;
   for (i=1; i<=nbBases; i++) {
     Handle(Standard_Transient) anItemLoc = aLocObjs->Value(i);
@@ -1261,7 +1375,6 @@ static TopoDS_Shape CreatePipeForShellSections(const TopoDS_Wire& aWirePath,
     for (; i<=Edges.Length(); i++)
       B.Add(W,Edges.Value(i));
     Wires.Append(W);
-    //cout<<"Wires.Length()="<<Wires.Length()<<endl;
   }
 
   if (Wires.Length() != nbLocs-1) {
@@ -1270,10 +1383,8 @@ static TopoDS_Shape CreatePipeForShellSections(const TopoDS_Wire& aWirePath,
       ("One of location shapes is not lied on the path");
   }
 
-  //TopTools_SequenceOfShape aSeqBases;
-  //TopTools_SequenceOfShape aSeqSubBases;
-  //TopTools_SequenceOfShape aSeqFaces;
-  TopoDS_Compound aComp;
+  TopTools_SequenceOfShape aGroups[5];
+  TopoDS_Compound          aComp;
   B.MakeCompound(aComp);
   for (i = 1; i < nbBases; i++) {
     TopoDS_Wire WPath = TopoDS::Wire(Wires.Value(i));
@@ -1300,8 +1411,6 @@ static TopoDS_Shape CreatePipeForShellSections(const TopoDS_Wire& aWirePath,
       continue;
     TopAbs_ShapeEnum aType2 = aShBase2.ShapeType();
 
-    //BRepTools::Write(aShBase1,"/dn02/users_Linux/skl/work/Bugs/14857/base1.brep");
-
     bool OkSec = (aType1==TopAbs_SHELL || aType1==TopAbs_FACE) &&
                  (aType2==TopAbs_SHELL || aType2==TopAbs_FACE);
     if (!OkSec) {
@@ -1324,153 +1433,6 @@ static TopoDS_Shape CreatePipeForShellSections(const TopoDS_Wire& aWirePath,
       CreateFewSolids = true;
     }
 
-    /*
-    // check orientation of sections
-    bool NeedReverse = false;
-    {
-      // first section
-      anExp.Init(aShBase1, TopAbs_FACE);
-      TopoDS_Shape aFace = anExp.Current();
-      TColgp_SequenceOfPnt aPnts;
-      double xc=0, yc=0, zc=0;
-      for (anExp.Init(aFace, TopAbs_VERTEX); anExp.More(); anExp.Next()) {
-        TopoDS_Vertex V = TopoDS::Vertex(anExp.Current());
-        aPnts.Append(BRep_Tool::Pnt(V));
-        xc += aPnts.Last().X();
-        yc += aPnts.Last().Y();
-        zc += aPnts.Last().Z();
-      }
-      gp_Pnt PC(xc/aPnts.Length(), yc/aPnts.Length(), zc/aPnts.Length());
-      gp_Vec V1(PC,aPnts.Value(1));
-      gp_Vec V2(PC,aPnts.Value(2));
-      gp_Vec VN = V1.Crossed(V2);
-      for (int ip=2; ip<aPnts.Length(); ip++) {
-        V1 = gp_Vec(PC,aPnts.Value(ip));
-        V2 = gp_Vec(PC,aPnts.Value(ip+1));
-        VN.Add(V1.Crossed(V2));
-      }
-      gp_Vec PathNorm;
-      gp_Pnt PLoc = BRep_Tool::Pnt(TopoDS::Vertex(VLocs(i)));
-      TopExp_Explorer WE;
-      for (WE.Init(WPath, TopAbs_EDGE); WE.More(); WE.Next()) {
-        TopoDS_Edge edge = TopoDS::Edge(WE.Current());
-        double tol = BRep_Tool::Tolerance(edge);
-        TopoDS_Vertex VF = sae.FirstVertex(edge);
-        gp_Pnt PF = BRep_Tool::Pnt(VF);
-        if (PF.Distance(PLoc) < tol) {
-          double fp,lp;
-          Handle(Geom_Curve) C = BRep_Tool::Curve(edge,fp,lp);
-          gp_Pnt P1,P2;
-          C->D0(fp,P1);
-          if (P1.Distance(PLoc) < tol) {
-            C->D0(fp+(lp-fp)/100,P2);
-          }
-          else {
-            C->D0(lp,P1);
-            C->D0(lp+(fp-lp)/100,P2);
-          }
-          PathNorm = gp_Vec(P1,P2);
-          break;
-        }
-        else {
-          TopoDS_Vertex VL = sae.LastVertex(edge);
-          gp_Pnt PL = BRep_Tool::Pnt(VL);
-          if (PL.Distance(PLoc) < tol) {
-            double fp,lp;
-            Handle(Geom_Curve) C = BRep_Tool::Curve(edge,fp,lp);
-            gp_Pnt P1,P2;
-            C->D0(fp,P1);
-            if (P1.Distance(PLoc) < tol) {
-              C->D0(fp+(lp-fp)/100,P2);
-            }
-            else {
-              C->D0(lp,P1);
-              C->D0(lp+(fp-lp)/100,P2);
-            }
-            PathNorm = gp_Vec(P2,P1);
-            break;
-          }
-        }
-      }
-      cout<<"VN("<<VN.X()<<","<<VN.Y()<<","<<VN.Z()<<")"<<endl;
-      cout<<"PathNorm("<<PathNorm.X()<<","<<PathNorm.Y()<<","<<PathNorm.Z()<<")"<<endl;
-      if (fabs(VN.Angle(PathNorm))>PI/2.) {
-        NeedReverse = true;
-        aShBase1.Reverse();
-      }
-    }
-    {
-      // second section
-      anExp.Init(aShBase2, TopAbs_FACE);
-      TopoDS_Shape aFace = anExp.Current();
-      TColgp_SequenceOfPnt aPnts;
-      double xc=0, yc=0, zc=0;
-      for (anExp.Init(aFace, TopAbs_VERTEX); anExp.More(); anExp.Next()) {
-        TopoDS_Vertex V = TopoDS::Vertex(anExp.Current());
-        aPnts.Append(BRep_Tool::Pnt(V));
-        xc += aPnts.Last().X();
-        yc += aPnts.Last().Y();
-        zc += aPnts.Last().Z();
-      }
-      gp_Pnt PC(xc/aPnts.Length(), yc/aPnts.Length(), zc/aPnts.Length());
-      gp_Vec V1(PC,aPnts.Value(1));
-      gp_Vec V2(PC,aPnts.Value(2));
-      gp_Vec VN = V1.Crossed(V2);
-      for (int ip=2; ip<aPnts.Length(); ip++) {
-        V1 = gp_Vec(PC,aPnts.Value(ip));
-        V2 = gp_Vec(PC,aPnts.Value(ip+1));
-        VN.Add(V1.Crossed(V2));
-      }
-      gp_Vec PathNorm;
-      gp_Pnt PLoc = BRep_Tool::Pnt(TopoDS::Vertex(VLocs(i+1)));
-      TopExp_Explorer WE;
-      for (WE.Init(WPath, TopAbs_EDGE); WE.More(); WE.Next()) {
-        TopoDS_Edge edge = TopoDS::Edge(WE.Current());
-        double tol = BRep_Tool::Tolerance(edge);
-        TopoDS_Vertex VF = sae.FirstVertex(edge);
-        gp_Pnt PF = BRep_Tool::Pnt(VF);
-        if (PF.Distance(PLoc) < tol) {
-          double fp,lp;
-          Handle(Geom_Curve) C = BRep_Tool::Curve(edge,fp,lp);
-          gp_Pnt P1,P2;
-          C->D0(fp,P1);
-          if (P1.Distance(PLoc) < tol) {
-            C->D0(fp+(lp-fp)/100,P2);
-          }
-          else {
-            C->D0(lp,P1);
-            C->D0(lp+(fp-lp)/100,P2);
-          }
-          PathNorm = gp_Vec(P2,P1);
-          break;
-        }
-        else {
-          TopoDS_Vertex VL = sae.LastVertex(edge);
-          gp_Pnt PL = BRep_Tool::Pnt(VL);
-          if (PL.Distance(PLoc) < tol) {
-            double fp,lp;
-            Handle(Geom_Curve) C = BRep_Tool::Curve(edge,fp,lp);
-            gp_Pnt P1,P2;
-            C->D0(fp,P1);
-            if (P1.Distance(PLoc) < tol) {
-              C->D0(fp+(lp-fp)/100,P2);
-            }
-            else {
-              C->D0(lp,P1);
-              C->D0(lp+(fp-lp)/100,P2);
-            }
-            PathNorm = gp_Vec(P2,P1);
-            break;
-          }
-        }
-      }
-      //cout<<"VN("<<VN.X()<<","<<VN.Y()<<","<<VN.Z()<<")"<<endl;
-      //cout<<"PathNorm("<<PathNorm.X()<<","<<PathNorm.Y()<<","<<PathNorm.Z()<<")"<<endl;
-      if (fabs(VN.Angle(PathNorm))>PI/2.)
-        aShBase2.Reverse();
-    }
-    */
-
     if (!CreateFewSolids) {
       // we can create only one solid
       TopoDS_Shape aWire1, aWire2;
@@ -1536,7 +1498,22 @@ static TopoDS_Shape CreatePipeForShellSections(const TopoDS_Wire& aWirePath,
 
         BuildPipeShell(aBuilder);
 
-        TopoDS_Shape aShape = aBuilder.Shape();
+        TopoDS_Shape             aShape = aBuilder.Shape();
+        TopTools_SequenceOfShape aLocalGroups[5];
+
+        // Create groups.
+        if (isGenerateGroups) {
+          // Make groups.
+          if (!DoGroups(aBuilder, aLocalGroups)) {
+            if (aCI) delete aCI;
+            Standard_ConstructionError::Raise("Generate groups failure");
+          }
+
+          // Clear the groups Down and Up.
+          aLocalGroups[GROUP_DOWN].Clear();
+          aLocalGroups[GROUP_UP].Clear();
+        }
+
         TopoDS_Shell aShell;
         B.MakeShell(aShell);
         for (anExp.Init(aShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
@@ -1544,9 +1521,17 @@ static TopoDS_Shape CreatePipeForShellSections(const TopoDS_Wire& aWirePath,
         }
         for (anExp.Init(aShBase1, TopAbs_FACE); anExp.More(); anExp.Next()) {
           B.Add(aShell,anExp.Current());
+
+          if (isGenerateGroups && i == 1) {
+            aLocalGroups[GROUP_DOWN].Append(anExp.Current());
+          }
         }
         for (anExp.Init(aShBase2, TopAbs_FACE); anExp.More(); anExp.Next()) {
           B.Add(aShell,anExp.Current());
+
+          if (isGenerateGroups && i == nbBases - 1) {
+            aLocalGroups[GROUP_UP].Append(anExp.Current());
+          }
         }
         // make sewing for this shell
         Handle(BRepBuilderAPI_Sewing) aSewing = new BRepBuilderAPI_Sewing;
@@ -1579,6 +1564,30 @@ static TopoDS_Shape CreatePipeForShellSections(const TopoDS_Wire& aWirePath,
         else {
           B.Add(aComp,aShell);
         }
+
+        if (isGenerateGroups) {
+          Standard_Integer iGrp;
+
+          for (iGrp = 0; iGrp < 5; ++iGrp) {
+            Standard_Integer j;
+
+            // For each sub-shape of pipe
+            for (j = 1; j <= aLocalGroups[iGrp].Length(); ++j) {
+              const TopoDS_Shape &aGrpShape = aLocalGroups[iGrp].Value(j);
+
+              if (aSewing->IsModifiedSubShape(aGrpShape)) {
+                // Use the shape modified by sewing.
+                const TopoDS_Shape &aModifGrpShape =
+                  aSewing->ModifiedSubShape(aGrpShape);
+
+                aGroups[iGrp].Append(aModifGrpShape);
+              } else {
+                // Use the shape as it is.
+                aGroups[iGrp].Append(aGrpShape);
+              }
+            }
+          }
+        }
       }
     }
     else {
@@ -1776,7 +1785,30 @@ static TopoDS_Shape CreatePipeForShellSections(const TopoDS_Wire& aWirePath,
 
           BuildPipeShell(aBuilder);
 
-          TopoDS_Shape aShape = aBuilder.Shape();
+          TopoDS_Shape             aShape = aBuilder.Shape();
+          TopTools_SequenceOfShape aLocalGroups[5];
+
+          // Create groups.
+          if (isGenerateGroups) {
+            // Make groups.
+            if (!DoGroups(aBuilder, aLocalGroups)) {
+              if (aCI) delete aCI;
+              Standard_ConstructionError::Raise("Generate groups failure");
+            }
+
+            // Clear the groups Down and Up.
+            aLocalGroups[GROUP_DOWN].Clear();
+            aLocalGroups[GROUP_UP].Clear();
+
+            if (i == 1) {
+              aLocalGroups[GROUP_DOWN].Append(F1);
+            }
+
+            if (i == nbBases - 1) {
+              aLocalGroups[GROUP_UP].Append(F2);
+            }
+          }
+
           TopoDS_Shell aShell;
           B.MakeShell(aShell);
           for (anExp.Init(aShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
@@ -1817,13 +1849,52 @@ static TopoDS_Shape CreatePipeForShellSections(const TopoDS_Wire& aWirePath,
           else {
             B.Add(aComp,aShell);
           }
+
+          if (isGenerateGroups) {
+            // Replase Group shapes by modified ones.
+            Standard_Integer iGrp;
+
+            // For each group.
+            for (iGrp = 0; iGrp < 5; ++iGrp) {
+              Standard_Integer j;
+
+              // For each sub-shape of pipe
+              for (j = 1; j <= aLocalGroups[iGrp].Length(); ++j) {
+                const TopoDS_Shape &aGrpShape = aLocalGroups[iGrp].Value(j);
+
+                if (aSewing->IsModifiedSubShape(aGrpShape)) {
+                  // Use the shape modified by sewing.
+                  const TopoDS_Shape &aModifGrpShape =
+                    aSewing->ModifiedSubShape(aGrpShape);
+
+                  aGroups[iGrp].Append(aModifGrpShape);
+                } else {
+                  // Use the shape as it is.
+                  aGroups[iGrp].Append(aGrpShape);
+                }
+              }
+            }
+          }
         }
       }
+    }
+  }
+
+  if (isGenerateGroups) {
+    // Fill the groups.
+    Handle(TColStd_HArray1OfInteger) aGroupIds[5];
+    TopTools_IndexedMapOfShape       anIndices;
+
+    TopExp::MapShapes(aComp, anIndices);
 
+    if (!FillGroups(aGroups, anIndices, aGroupIds)) {
+      if (aCI) delete aCI;
+      Standard_ConstructionError::Raise("Generate groups failure");
     }
+
+    StoreGroups(aCI, aGroupIds);
   }
 
-  //BRepTools::Write(aComp,"/dn02/users_Linux/skl/work/Bugs/14857/comp.brep");
   return aComp;
 }
 
@@ -1842,6 +1913,7 @@ static TopoDS_Shape CreatePipeShellsWithoutPath(GEOMImpl_IPipe* aCI)
   Handle(TColStd_HSequenceOfTransient) aBasesObjs = aCIDS->GetBases();
   // vertex for recognition
   Handle(TColStd_HSequenceOfTransient) VObjs = aCIDS->GetLocations();
+  Standard_Boolean isGenerateGroups = aCIDS->GetGenerateGroups();
 
   Standard_Integer nbBases = aBasesObjs->Length(),
     nbv = (VObjs.IsNull() ? 0 :VObjs->Length());
@@ -1851,6 +1923,7 @@ static TopoDS_Shape CreatePipeShellsWithoutPath(GEOMImpl_IPipe* aCI)
     Standard_ConstructionError::Raise("Number of shapes for recognition is invalid");
   }
 
+  TopTools_SequenceOfShape aGroups[5];
   TopTools_SequenceOfShape SecVs,Bases;
   for (i=1; i<=nbBases; i++) {
     // vertex
@@ -1902,8 +1975,23 @@ static TopoDS_Shape CreatePipeShellsWithoutPath(GEOMImpl_IPipe* aCI)
     }
 
     TopTools_MapOfShape aFaces1,aFaces2;
+    TopTools_MapOfShape aBndEdges1;
+
     for (anExp.Init(aShBase1, TopAbs_FACE); anExp.More(); anExp.Next()) {
-      aFaces1.Add(anExp.Current());
+      const TopoDS_Shape &aBaseFace1 = anExp.Current();
+
+      if (aFaces1.Add(aBaseFace1)) {
+        // Get boundary edges.
+        TopExp_Explorer anExpE(aBaseFace1, TopAbs_EDGE);
+
+        for (; anExpE.More(); anExpE.Next()) {
+          const TopoDS_Shape &aBaseEdge1 = anExpE.Current();
+
+          if (!aBndEdges1.Add(aBaseEdge1)) {
+            aBndEdges1.Remove(aBaseEdge1);
+          }
+        }
+      }
     }
     for (anExp.Init(aShBase2, TopAbs_FACE); anExp.More(); anExp.Next()) {
       aFaces2.Add(anExp.Current());
@@ -1991,6 +2079,8 @@ static TopoDS_Shape CreatePipeShellsWithoutPath(GEOMImpl_IPipe* aCI)
 
       TopExp_Explorer anExpE(F1,TopAbs_EDGE);
       TopTools_SequenceOfShape aNewFs;
+      TopTools_SequenceOfShape aLocalGroups[5];
+
       //int nbee=0;
       for (; anExpE.More(); anExpE.Next()) {
         TopoDS_Edge E1 = TopoDS::Edge(anExpE.Current());
@@ -2071,8 +2161,6 @@ static TopoDS_Shape CreatePipeShellsWithoutPath(GEOMImpl_IPipe* aCI)
         B.Add(W,E2);
         B.Add(W,E3);
         B.Add(W,E4.Reversed());
-        //cout<<"      wire for edge "<<nbee<<" is created"<<endl;
-        //BRepTools::Write(W,"/dn02/users_Linux/skl/work/Bugs/14857/w.brep");
 
         // make surface
 
@@ -2114,13 +2202,6 @@ static TopoDS_Shape CreatePipeShellsWithoutPath(GEOMImpl_IPipe* aCI)
           GeomConvert::CurveToBSplineCurve(C4,Convert_RationalC1);
         if (CE4->Degree()<3)
           CE4->IncreaseDegree(3);
-        //cout<<"CE1->Degree()="<<CE1->Degree()<<" CE2->Degree()="<<CE2->Degree()
-        //    <<" CE3->Degree()="<<CE3->Degree()<<" CE4->Degree()="<<CE4->Degree()<<endl;
-        //if (fic.open("/dn02/users_Linux/skl/work/Bugs/14857/ce1.brep",ios::out)) {
-        //  os<<"DrawTrSurf_BSplineCurve"<<endl;
-        //  GeomTools::Write(CE1,os);
-        //  fic.close();
-        //}
 
         Handle(Geom_Surface) BS;
         try {
@@ -2218,8 +2299,13 @@ static TopoDS_Shape CreatePipeShellsWithoutPath(GEOMImpl_IPipe* aCI)
         TopoDS_Face FixedFace = sff->Face();
         aNewFs.Append(FixedFace);
         VPE.Add(E1,FixedFace);
-        //cout<<"      face for edge "<<nbee<<" is created"<<endl;
-        //BRepTools::Write(FixedFace,"/dn02/users_Linux/skl/work/Bugs/14857/f.brep");
+
+        if (isGenerateGroups) {
+          if (aBndEdges1.Contains(E1)) {
+            // This is a boundary face.
+            aLocalGroups[GROUP_OTHER].Append(FixedFace);
+          }
+        }
       }
       // make shell
       TopoDS_Shell aShell;
@@ -2230,6 +2316,15 @@ static TopoDS_Shape CreatePipeShellsWithoutPath(GEOMImpl_IPipe* aCI)
       B.Add(aShell,F1);
       B.Add(aShell,F2);
 
+      // Create groups.
+      if (isGenerateGroups && i == 1) {
+        aLocalGroups[GROUP_DOWN].Append(F1);
+      }
+
+      if (isGenerateGroups && i == nbBases - 1) {
+        aLocalGroups[GROUP_UP].Append(F2);
+      }
+
       // make sewing for this shell
       Handle(BRepBuilderAPI_Sewing) aSewing = new BRepBuilderAPI_Sewing;
       aSewing->SetTolerance(Precision::Confusion());
@@ -2242,7 +2337,6 @@ static TopoDS_Shape CreatePipeShellsWithoutPath(GEOMImpl_IPipe* aCI)
       aSewing->Perform();
       MESSAGE ("    shell for face "<<nbff<<" is created");
       const TopoDS_Shape aSewShape = aSewing->SewedShape();
-      //BRepTools::Write(aSewShape,"/dn02/users_Linux/skl/work/Bugs/14857/sew.brep");
       if (aSewShape.ShapeType() == TopAbs_SHELL) {
         aShell = TopoDS::Shell(aSewShape);
         GProp_GProps aSystem;
@@ -2267,32 +2361,48 @@ static TopoDS_Shape CreatePipeShellsWithoutPath(GEOMImpl_IPipe* aCI)
         B.Add(aComp,aShell);
         MESSAGE ("    solid for face "<<nbff<<" is not created");
       }
-      //cout<<"    solid for face "<<nbff<<" is created"<<endl;
 
-      //Handle(ShapeFix_Shell) sfs = new ShapeFix_Shell(aShell);
-      //sfs->Perform();
-      //TopoDS_Shell FixedShell = sfs->Shell();
-      /*
-      GProp_GProps aSystem;
-      BRepGProp::VolumeProperties(FixedShell, aSystem);
-      if (aSystem.Mass()<0) {
-        //cout<<"aSewShape is reversed"<<endl;
-        FixedShell.Reverse();
-      }
-      if (BRep_Tool::IsClosed(FixedShell)) {
-        TopoDS_Solid aSolid;
-        B.MakeSolid(aSolid);
-        B.Add(aSolid,aShell);
-        B.Add(aComp,aSolid);
-      }
-      else {
-        B.Add(aComp,FixedShell);
+      if (isGenerateGroups) {
+        Standard_Integer iGrp;
+
+        for (iGrp = 0; iGrp < 5; ++iGrp) {
+          Standard_Integer j;
+
+          // For each sub-shape of pipe
+          for (j = 1; j <= aLocalGroups[iGrp].Length(); ++j) {
+            const TopoDS_Shape &aGrpShape = aLocalGroups[iGrp].Value(j);
+
+            if (aSewing->IsModifiedSubShape(aGrpShape)) {
+              // Use the shape modified by sewing.
+              const TopoDS_Shape &aModifGrpShape =
+                aSewing->ModifiedSubShape(aGrpShape);
+
+              aGroups[iGrp].Append(aModifGrpShape);
+            } else {
+              // Use the shape as it is.
+              aGroups[iGrp].Append(aGrpShape);
+            }
+          }
+        }
       }
-      */
     }
   }
 
-  //BRepTools::Write(aComp,"/dn02/users_Linux/skl/work/Bugs/14857/comp.brep");
+  if (isGenerateGroups) {
+    // Fill the groups.
+    Handle(TColStd_HArray1OfInteger) aGroupIds[5];
+    TopTools_IndexedMapOfShape       anIndices;
+
+    TopExp::MapShapes(aComp, anIndices);
+
+    if (!FillGroups(aGroups, anIndices, aGroupIds)) {
+      if (aCI) delete aCI;
+      Standard_ConstructionError::Raise("Generate groups failure");
+    }
+
+    StoreGroups(aCI, aGroupIds);
+  }
+
   return aComp;
 }
 
@@ -2365,9 +2475,397 @@ static TopoDS_Shape CreatePipeBiNormalAlongVector(const TopoDS_Wire& aWirePath,
       PipeBuilder.MakeSolid();
   }
 
+  if (!CreateGroups(PipeBuilder, aCIBN)) {
+    if (aCIBN) delete aCIBN;
+    Standard_ConstructionError::Raise("Generate groups failure");
+  }
+
   return PipeBuilder.Shape();
 }
 
+//=======================================================================
+//function : FillGroups
+//purpose  : auxilary for DoGroups()
+//=======================================================================
+bool FillGroups(const TopTools_SequenceOfShape         *theGroups,
+                const TopTools_IndexedMapOfShape       &theIndices,
+                      Handle(TColStd_HArray1OfInteger) *theGroupIds)
+{
+  Standard_Integer i;
+
+  for (i = 0; i < 5; ++i) {
+    if (!theGroups[i].IsEmpty()) {
+      const Standard_Integer aNbShapes = theGroups[i].Length();
+      Standard_Integer       j;
+
+      theGroupIds[i] = new TColStd_HArray1OfInteger(1, aNbShapes);
+
+      for (j = 1; j <= aNbShapes; ++j) {
+        const TopoDS_Shape     &aShape  = theGroups[i].Value(j);
+        const Standard_Integer  anIndex = theIndices.FindIndex(aShape);
+
+        if (anIndex == 0) {
+          return false;
+        }
+
+        theGroupIds[i]->SetValue(j, anIndex);
+      }
+    }
+  }
+
+  return true;
+}
+
+//=======================================================================
+//function : StoreGroups
+//purpose  : auxilary for CreateGroups()
+//=======================================================================
+void StoreGroups(GEOMImpl_IPipe                   *theCI,
+                 Handle(TColStd_HArray1OfInteger) *theGroups)
+{
+  if (theGroups[GROUP_DOWN].IsNull() == Standard_False) {
+    theCI->SetGroupDown(theGroups[GROUP_DOWN]);
+  }
+
+  if (theGroups[GROUP_UP].IsNull() == Standard_False) {
+    theCI->SetGroupUp(theGroups[GROUP_UP]);
+  }
+
+  if (theGroups[GROUP_SIDE1].IsNull() == Standard_False) {
+    theCI->SetGroupSide1(theGroups[GROUP_SIDE1]);
+  }
+
+  if (theGroups[GROUP_SIDE2].IsNull() == Standard_False) {
+    theCI->SetGroupSide2(theGroups[GROUP_SIDE2]);
+  }
+
+  if (theGroups[GROUP_OTHER].IsNull() == Standard_False) {
+    theCI->SetGroupOther(theGroups[GROUP_OTHER]);
+  }
+}
+
+//=======================================================================
+//function : CreateDownUpGroups
+//purpose  : auxilary for DoGroups()
+//=======================================================================
+static bool CreateDownUpGroups(BRepPrimAPI_MakeSweep    *theSweep,
+                               TopTools_SequenceOfShape *theGroups,
+                               Standard_Boolean         &IsDoSides)
+{
+  const TopoDS_Shape     aDownShape   = theSweep->FirstShape();
+  const TopAbs_ShapeEnum aType        = aDownShape.ShapeType();
+  TopAbs_ShapeEnum       anUpDownType = TopAbs_SHAPE;
+
+  IsDoSides  = Standard_False;
+
+  switch (aType) {
+    case TopAbs_EDGE:
+    case TopAbs_WIRE:
+      anUpDownType = TopAbs_EDGE;
+
+      if (GEOMUtils::IsOpenPath(aDownShape)) {
+        IsDoSides = Standard_True;
+      }
+      break;
+    case TopAbs_FACE:
+    case TopAbs_SHELL:
+      anUpDownType = TopAbs_FACE;
+      break;
+    default:
+      break;
+  }
+
+  if (anUpDownType == TopAbs_SHAPE) {
+    // Invalid Up and Down group type.
+    return false;
+  }
+
+  TopExp_Explorer     anExp(aDownShape, anUpDownType);
+  TopTools_MapOfShape aMapFence;
+
+  // Create Down group.
+  for (; anExp.More(); anExp.Next()) {
+    const TopoDS_Shape &aShape = anExp.Current();
+
+    if (aMapFence.Add(aShape)) {
+      theGroups[GROUP_DOWN].Append(aShape);
+    }
+  }
+
+  // Create Up group
+  const TopoDS_Shape anUpShape = theSweep->LastShape();
+
+  aMapFence.Clear();
+  anExp.Init(anUpShape, anUpDownType);
+
+  for (; anExp.More(); anExp.Next()) {
+    const TopoDS_Shape &aShape = anExp.Current();
+
+    if (aMapFence.Add(aShape)) {
+      theGroups[GROUP_UP].Append(aShape);
+    }
+  }
+
+  return true;
+}
+
+//=======================================================================
+//function : DoGroups
+//purpose  : auxilary for CreateGroups()
+//=======================================================================
+bool DoGroups(BRepOffsetAPI_MakePipeShell &theSweep,
+              TopTools_SequenceOfShape    *theGroups)
+{
+  Standard_Boolean isDoSides = Standard_False;
+
+  if (!CreateDownUpGroups(&theSweep, theGroups, isDoSides)) {
+    // Up and Down groups creation failure
+    return false;
+  }
+
+  const TopoDS_Shape aDownShape = theSweep.FirstShape();
+
+  if (isDoSides) {
+    // Create Side1 and Side2 groups.
+    const TopAbs_ShapeEnum aType = aDownShape.ShapeType();
+    TopoDS_Vertex          aV[2];
+    Standard_Integer       i;
+
+    if (aType == TopAbs_EDGE) {
+      TopExp::Vertices(TopoDS::Edge(aDownShape), aV[0], aV[1], Standard_True);
+    } else { // aType == TopAbs_WIRE
+      TopExp::Vertices(TopoDS::Wire(aDownShape), aV[0], aV[1]);
+    }
+
+    for (i = 0; i < 2; ++i) {
+      if (aV[i].IsNull() == Standard_False) {
+        const TopTools_ListOfShape &aLstSide = theSweep.Generated(aV[i]);
+
+        if (!aLstSide.IsEmpty()) {
+          TopTools_ListIteratorOfListOfShape aSideIt(aLstSide);
+          TopTools_MapOfShape                aMapFence;
+          const Standard_Integer             anIdSide =
+                      (i == 0 ? GROUP_SIDE1 : GROUP_SIDE2);
+
+          for (; aSideIt.More(); aSideIt.Next()) {
+            const TopoDS_Shape &aSideShape = aSideIt.Value();
+
+            if (aSideShape.ShapeType() == TopAbs_EDGE) {
+              if (aMapFence.Add(aSideShape)) {
+                theGroups[anIdSide].Append(aSideShape);
+              }
+            } else {
+              // Only edges can be is Side1 and Side2 groups.
+              return false;
+            }
+          }
+        }
+      }
+    }
+  } else {
+    // Create Other group. Get boudnary edges of the profile.
+    TopTools_MapOfShape aMapBndEdges;
+    TopExp_Explorer     anExp(aDownShape, TopAbs_EDGE);
+
+    for (; anExp.More(); anExp.Next()) {
+      const TopoDS_Shape &anEdge = anExp.Current();
+
+      if (!aMapBndEdges.Add(anEdge)) {
+        aMapBndEdges.Remove(anEdge);
+      }
+    }
+
+    // Fill the map of faces generated from profile's boundary edges.
+    TopTools_MapIteratorOfMapOfShape anIter(aMapBndEdges);
+    TopTools_MapOfShape              aMapFence;
+
+    for (; anIter.More(); anIter.Next()) {
+      const TopTools_ListOfShape &aLstOther = theSweep.Generated(anIter.Key());
+
+      if (!aLstOther.IsEmpty()) {
+        TopTools_ListIteratorOfListOfShape anOtherIt(aLstOther);
+
+        for (; anOtherIt.More(); anOtherIt.Next()) {
+          const TopoDS_Shape &anOtherShape = anOtherIt.Value();
+
+          if (anOtherShape.ShapeType() == TopAbs_FACE) {
+            if (aMapFence.Add(anOtherShape)) {
+              theGroups[GROUP_OTHER].Append(anOtherShape);
+            }
+          } else {
+            // Only faces can be in Other group.
+            return false;
+          }
+        }
+      }
+    }
+  }
+
+  return true;
+}
+
+//=======================================================================
+//function : CreateGroups
+//purpose  : auxilary for Execute()
+//=======================================================================
+bool CreateGroups(BRepOffsetAPI_MakePipeShell &theSweep,
+                  GEOMImpl_IPipe              *theCI)
+{
+  if (!theCI->GetGenerateGroups()) {
+    // Nothing to do.
+    return true;
+  }
+
+  // Make groups.
+  TopTools_SequenceOfShape aGroups[5];
+
+  if (!DoGroups(theSweep, aGroups)) {
+    return false;
+  }
+
+  // Fill the groups.
+  Handle(TColStd_HArray1OfInteger) aGroupIds[5];
+  TopTools_IndexedMapOfShape       anIndices;
+  const TopoDS_Shape               aResult = theSweep.Shape();
+
+  TopExp::MapShapes(aResult, anIndices);
+
+  if (!FillGroups(aGroups, anIndices, aGroupIds)) {
+    return false;
+  }
+
+  // Store groups.
+  StoreGroups(theCI, aGroupIds);
+
+  return true;
+}
+
+//=======================================================================
+//function : DoGroups
+//purpose  : auxilary for CreateGroups()
+//=======================================================================
+static bool DoGroups(const TopoDS_Shape             &theProfile,
+                     const TopoDS_Shape             &thePath,
+                           BRepOffsetAPI_MakePipe   &theSweep,
+                           TopTools_SequenceOfShape *theGroups)
+{
+  Standard_Boolean isDoSides = Standard_False;
+
+  if (!CreateDownUpGroups(&theSweep, theGroups, isDoSides)) {
+    // Up and Down groups creation failure
+    return false;
+  }
+
+  if (isDoSides) {
+    // Create Side1 and Side2 groups.
+    const TopAbs_ShapeEnum aType = theProfile.ShapeType();
+    TopoDS_Vertex          aV[2];
+    Standard_Integer       i;
+
+    if (aType == TopAbs_EDGE) {
+      TopExp::Vertices(TopoDS::Edge(theProfile), aV[0], aV[1], Standard_True);
+    } else { // aType == TopAbs_WIRE
+      TopExp::Vertices(TopoDS::Wire(theProfile), aV[0], aV[1]);
+    }
+
+    for (i = 0; i < 2; ++i) {
+      if (aV[i].IsNull() == Standard_False) {
+        TopExp_Explorer        anExpP(thePath, TopAbs_EDGE);
+        TopTools_MapOfShape    aMapFence;
+        const Standard_Integer anIdSide =
+                      (i == 0 ? GROUP_SIDE1 : GROUP_SIDE2);
+
+        for (; anExpP.More(); anExpP.Next()) {
+          const TopoDS_Shape aSideShape =
+            theSweep.Generated(anExpP.Current(), aV[i]);
+
+          if (aSideShape.ShapeType() == TopAbs_EDGE) {
+            if (aMapFence.Add(aSideShape)) {
+              theGroups[anIdSide].Append(aSideShape);
+            }
+          } else {
+            // Only edges can be is Side1 and Side2 groups.
+            return false;
+          }
+        }
+      }
+    }
+  } else {
+    // Create Other group. Get boudnary edges of the profile.
+    TopTools_MapOfShape aMapBndEdges;
+    TopExp_Explorer     anExp(theProfile, TopAbs_EDGE);
+
+    for (; anExp.More(); anExp.Next()) {
+      const TopoDS_Shape &anEdge = anExp.Current();
+
+      if (!aMapBndEdges.Add(anEdge)) {
+        aMapBndEdges.Remove(anEdge);
+      }
+    }
+
+    TopExp_Explorer        anExpP(thePath, TopAbs_EDGE);
+    TopTools_MapOfShape    aMapFence;
+
+    for (; anExpP.More(); anExpP.Next()) {
+      TopTools_MapIteratorOfMapOfShape anIter(aMapBndEdges);
+
+      for (; anIter.More(); anIter.Next()) {
+        const TopoDS_Shape anOtherShape =
+          theSweep.Generated(anExpP.Current(), anIter.Key());
+
+        if (anOtherShape.ShapeType() == TopAbs_FACE) {
+          if (aMapFence.Add(anOtherShape)) {
+            theGroups[GROUP_OTHER].Append(anOtherShape);
+          }
+        } else {
+          // Only faces can be in Other group.
+          return false;
+        }
+      }
+    }
+  }
+
+  return true;
+}
+
+//=======================================================================
+//function : CreateGroups
+//purpose  : auxilary for Execute()
+//=======================================================================
+static bool CreateGroups(const TopoDS_Shape     &theProfile,
+                         const TopoDS_Shape     &thePath,
+                         BRepOffsetAPI_MakePipe &theSweep,
+                         GEOMImpl_IPipe         *theCI)
+{
+  if (!theCI->GetGenerateGroups()) {
+    // Nothing to do.
+    return true;
+  }
+
+  // Make groups.
+  TopTools_SequenceOfShape aGroups[5];
+
+  if (!DoGroups(theProfile, thePath, theSweep, aGroups)) {
+    return false;
+  }
+
+  // Fill the groups.
+  Handle(TColStd_HArray1OfInteger) aGroupIds[5];
+  TopTools_IndexedMapOfShape       anIndices;
+  const TopoDS_Shape               aResult = theSweep.Shape();
+
+  TopExp::MapShapes(aResult, anIndices);
+
+  if (!FillGroups(aGroups, anIndices, aGroupIds)) {
+    return false;
+  }
+
+  // Store groups.
+  StoreGroups(theCI, aGroupIds);
+
+  return true;
+}
+
 //=======================================================================
 //function : Execute
 //purpose  :
@@ -2434,6 +2932,16 @@ Standard_Integer GEOMImpl_PipeDriver::Execute (TFunction_Logbook& log) const
       if (aCI) delete aCI;
       Standard_TypeMismatch::Raise("MakePipe aborted : path shape is neither a wire nor an edge");
     }
+
+    // Check if it is possible to create groups.
+    if (aCI->GetGenerateGroups() && !GEOMUtils::IsOpenPath(aWirePath)) {
+      if (aCI) {
+        delete aCI;
+      }
+
+      Standard_ConstructionError::Raise
+        ("Can't create groups if the path is closed");
+    }
   }
 
   TopoDS_Shape aShape;
@@ -2481,7 +2989,11 @@ Standard_Integer GEOMImpl_PipeDriver::Execute (TFunction_Logbook& log) const
       }
       else
         aShape = Sweep.Shape(); //result is good
-      
+
+      if (!CreateGroups(Sweep, aCI)) {
+        if (aCI) delete aCI;
+        Standard_ConstructionError::Raise("Generate groups failure");
+      }
     }
     else
     {
@@ -2490,6 +3002,11 @@ Standard_Integer GEOMImpl_PipeDriver::Execute (TFunction_Logbook& log) const
 
       if (aMkPipe.IsDone()) {
         aShape = aMkPipe.Shape();
+
+        if (!CreateGroups(aShapeBase, aWirePath, aMkPipe, aCI)) {
+          if (aCI) delete aCI;
+          Standard_ConstructionError::Raise("Generate groups failure");
+        }
       } else if (theBestMode != GeomFill_IsDiscreteTrihedron) {
         // Try to use Descrete Trihedron mode.
         BRepOffsetAPI_MakePipe aMkPipeDescrete
@@ -2497,6 +3014,11 @@ Standard_Integer GEOMImpl_PipeDriver::Execute (TFunction_Logbook& log) const
 
         if (aMkPipeDescrete.IsDone()) {
           aShape = aMkPipeDescrete.Shape();
+
+          if (!CreateGroups(aShapeBase, aWirePath, aMkPipeDescrete, aCI)) {
+            if (aCI) delete aCI;
+            Standard_ConstructionError::Raise("Generate groups failure");
+          }
         }
       }
     }
@@ -2509,6 +3031,8 @@ Standard_Integer GEOMImpl_PipeDriver::Execute (TFunction_Logbook& log) const
     Handle(TColStd_HSequenceOfTransient) aLocObjs = aCIDS->GetLocations ();
     Standard_Boolean aWithContact = (aCIDS->GetWithContactMode());
     Standard_Boolean aWithCorrect = (aCIDS->GetWithCorrectionMode());
+    Standard_Boolean isGenerateGroups = aCIDS->GetGenerateGroups();
+
     if (aCI) {
       delete aCI;
       aCI = 0;
@@ -2545,7 +3069,24 @@ Standard_Integer GEOMImpl_PipeDriver::Execute (TFunction_Logbook& log) const
       aHSeqLocs->Append(aShapeLoc);
     }
 
-    aShape = CreatePipeWithDifferentSections(aWirePath, aHSeqBases, aHSeqLocs, aWithContact, aWithCorrect);
+
+    Handle(TColStd_HArray1OfInteger) *pGroups = NULL;
+    Handle(TColStd_HArray1OfInteger)  aGroups[5];
+
+    if (isGenerateGroups) {
+      pGroups = aGroups;
+    }
+
+    aShape = CreatePipeWithDifferentSections
+              (aWirePath, aHSeqBases, aHSeqLocs,
+               aWithContact, aWithCorrect, pGroups);
+
+    if (isGenerateGroups) {
+      // Store created groups.
+      GEOMImpl_IPipeDiffSect aPipeDS(aFunction);
+
+      StoreGroups(&aPipeDS, aGroups);
+    }
   }
 
   //building pipe with shell sections
@@ -2563,6 +3104,8 @@ Standard_Integer GEOMImpl_PipeDriver::Execute (TFunction_Logbook& log) const
     aShape = CreatePipeBiNormalAlongVector(aWirePath, aCI);
   }
 
+  const Standard_Boolean isGenerateGroups = aCI->GetGenerateGroups();
+
   if (aCI) {
     delete aCI;
     aCI = 0;
@@ -2585,11 +3128,99 @@ Standard_Integer GEOMImpl_PipeDriver::Execute (TFunction_Logbook& log) const
           aVertMaxTol = aTol;
       }
       aVertMaxTol += Precision::Confusion();
-      aShape = GEOMImpl_GlueDriver::GlueFaces(aShape, aVertMaxTol, Standard_True);
-      //aShape = GEOMImpl_GlueDriver::GlueFaces(aShape, Precision::Confusion(), Standard_True);
+
+      TopTools_DataMapOfShapeListOfShape  aMapModif;
+      TopTools_DataMapOfShapeListOfShape *pMapModif = NULL;
+
+      if (isGenerateGroups) {
+        pMapModif = &aMapModif;
+      }
+
+      TopoDS_Shape aNewShape = GEOMImpl_GlueDriver::GlueFaces
+        (aShape, aVertMaxTol, Standard_True, pMapModif);
+
+      if (isGenerateGroups && !aMapModif.IsEmpty()) {
+        // Update groups.
+        GEOMImpl_IPipe                   aCI(aFunction);
+        Handle(TColStd_HArray1OfInteger) aGroupIDs[5] =
+          { aCI.GetGroupDown(), aCI.GetGroupUp(), aCI.GetGroupSide1(),
+            aCI.GetGroupSide2(), aCI.GetGroupOther() };
+        TopTools_IndexedMapOfShape       anIndices;
+        TopTools_IndexedMapOfShape       aNewIndices;
+        TopTools_SequenceOfShape         aNewShapes[5];
+        TopTools_MapOfShape              aMapReplaced;
+        TopTools_MapOfShape              aMapGlued;
+        Standard_Integer                 iGrp;
+        Standard_Integer                 i;
+
+        TopExp::MapShapes(aShape,    anIndices);
+        TopExp::MapShapes(aNewShape, aNewIndices);
+
+        for (iGrp = 0; iGrp < 5; ++iGrp) {
+          if (aGroupIDs[iGrp].IsNull() == Standard_False) {
+            const Standard_Integer aLower  = aGroupIDs[iGrp]->Lower();
+            const Standard_Integer anUpper = aGroupIDs[iGrp]->Upper();
+
+            for (i = aLower; i <= anUpper; ++i) {
+              const Standard_Integer  anIndex   = aGroupIDs[iGrp]->Value(i);
+              const TopoDS_Shape     &aSubShape = anIndices.FindKey(anIndex);
+
+              if (aMapModif.IsBound(aSubShape)) {
+                const TopTools_ListOfShape         &aListModif =
+                  aMapModif.Find(aSubShape);
+                TopTools_ListIteratorOfListOfShape  anIter(aListModif);
+
+                for (; anIter.More(); anIter.Next()) {
+                  const TopoDS_Shape &aNewShape = anIter.Value();
+
+                  if (aMapReplaced.Add(aNewShape)) {
+                    aNewShapes[iGrp].Append(aNewShape);
+                  } else {
+                    // This is a glued shape. It means that it is internal
+                    // one and should be removed from groups later.
+                    aMapGlued.Add(aNewShape);
+                  }
+                }
+              } else {
+                // Shape is not modified.
+                aNewShapes[iGrp].Append(aSubShape);
+              }
+            }
+          }
+        }
+
+        if (!aMapGlued.IsEmpty()) {
+          // Remove glued (internal) shapes from groups.
+          for (iGrp = 0; iGrp < 5; ++iGrp) {
+            Standard_Integer aNbShapes = aNewShapes[iGrp].Length();
+
+            for (i = 1; i < aNbShapes; ++i) {
+              const TopoDS_Shape &aNewShape = aNewShapes[iGrp].Value(i);
+
+              if (aMapGlued.Contains(aNewShape)) {
+                aNewShapes[iGrp].Remove(i);
+                --i;
+                --aNbShapes;
+              }
+            }
+          }
+        }
+
+        // Store modified groups.
+        Handle(TColStd_HArray1OfInteger) aNewGroupIDs[5];
+
+        if (!FillGroups(aNewShapes, aNewIndices, aNewGroupIDs)) {
+          Standard_ConstructionError::Raise("Generate groups failure");
+        }
+
+        StoreGroups(&aCI, aNewGroupIDs);
+      }
+
+      aShape = aNewShape;
     }
   }
 
+  // Note: group indices should not be changed after the next call.
   TopoDS_Shape aRes = GEOMUtils::CompsolidToCompound(aShape);
   aFunction->SetValue(aRes);