Salome HOME
Fix pb in non-regression scripts, caused by previous integration
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ShapeDriver.cxx
index 1ea14d97ba87aac8cd6b7ba8327f6c7275a8ab3d..a54d8e0be71daf1b363fd42361f7434714b8e444 100644 (file)
@@ -44,6 +44,7 @@
 #include <BRepBuilderAPI_Sewing.hxx>
 #include <BRepBuilderAPI_MakeWire.hxx>
 #include <BRepBuilderAPI_MakeEdge.hxx>
+#include <BRepBuilderAPI_MakeSolid.hxx>
 #include <BRepCheck.hxx>
 #include <BRepCheck_Analyzer.hxx>
 #include <BRepCheck_Shell.hxx>
@@ -133,10 +134,13 @@ Standard_Integer GEOMImpl_ShapeDriver::Execute(TFunction_Logbook& log) const
 
   TopoDS_Shape aShape;
   TCollection_AsciiString aWarning;
+  TopAbs_ShapeEnum anExpectedType = TopAbs_SHAPE;
 
   BRep_Builder B;
 
   if (aType == WIRE_EDGES) {
+    anExpectedType = TopAbs_WIRE;
+
     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
 
     Standard_Real aTolerance = aCI.GetTolerance();
@@ -146,6 +150,8 @@ Standard_Integer GEOMImpl_ShapeDriver::Execute(TFunction_Logbook& log) const
     aShape = MakeWireFromEdges(aShapes, aTolerance);
   }
   else if (aType == FACE_WIRE) {
+    anExpectedType = TopAbs_FACE;
+
     Handle(GEOM_Function) aRefBase = aCI.GetBase();
     TopoDS_Shape aShapeBase = aRefBase->GetValue();
     if (aShapeBase.IsNull()) Standard_NullObject::Raise("Argument Shape is null");
@@ -179,6 +185,8 @@ Standard_Integer GEOMImpl_ShapeDriver::Execute(TFunction_Logbook& log) const
     }
   }
   else if (aType == FACE_WIRES) {
+    anExpectedType = TopAbs_FACE;
+
     // Try to build a face from a set of wires and edges
     int ind;
 
@@ -204,6 +212,10 @@ Standard_Integer GEOMImpl_ShapeDriver::Execute(TFunction_Logbook& log) const
       }
     }
 
+    if (aSeqEdgesIn->IsEmpty()) {
+      Standard_ConstructionError::Raise("No edges given");
+    }
+
     // 2. Connect edges to wires of maximum length
     Handle(TopTools_HSequenceOfShape) aSeqWiresOut;
     ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdgesIn, Precision::Confusion(),
@@ -295,6 +307,8 @@ Standard_Integer GEOMImpl_ShapeDriver::Execute(TFunction_Logbook& log) const
     }
   }
   else if (aType == SHELL_FACES) {
+    anExpectedType = TopAbs_SHELL;
+
     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
     unsigned int ind, nbshapes = aShapes->Length();
 
@@ -350,40 +364,14 @@ Standard_Integer GEOMImpl_ShapeDriver::Execute(TFunction_Logbook& log) const
       }
     }
 
-  }
-  else if (aType == SOLID_SHELL) {
-    Handle(GEOM_Function) aRefShell = aCI.GetBase();
-    TopoDS_Shape aShapeShell = aRefShell->GetValue();
-    if (!aShapeShell.IsNull() && aShapeShell.ShapeType() == TopAbs_COMPOUND) {
-      TopoDS_Iterator It (aShapeShell, Standard_True, Standard_True);
-      if (It.More()) aShapeShell = It.Value();
-    }
-    if (aShapeShell.IsNull() || aShapeShell.ShapeType() != TopAbs_SHELL) {
-      Standard_NullObject::Raise("Shape for solid construction is null or not a shell");
-    }
-
-    BRepCheck_Shell chkShell(TopoDS::Shell(aShapeShell));
-    if (chkShell.Closed() == BRepCheck_NotClosed) return 0;
-
-    TopoDS_Solid Sol;
-    B.MakeSolid(Sol);
-    B.Add(Sol, aShapeShell);
-    BRepClass3d_SolidClassifier SC (Sol);
-    SC.PerformInfinitePoint(Precision::Confusion());
-    if (SC.State() == TopAbs_IN) {
-      B.MakeSolid(Sol);
-      B.Add(Sol, aShapeShell.Reversed());
-    }
-
-    aShape = Sol;
-
   }
   else if (aType == SOLID_SHELLS) {
+    anExpectedType = TopAbs_SOLID;
+
     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
     unsigned int ind, nbshapes = aShapes->Length();
     Standard_Integer ish = 0;
-    TopoDS_Solid Sol;
-    B.MakeSolid(Sol);
+    BRepBuilderAPI_MakeSolid aMkSolid;
 
     // add shapes
     for (ind = 1; ind <= nbshapes; ind++) {
@@ -397,11 +385,13 @@ Standard_Integer GEOMImpl_ShapeDriver::Execute(TFunction_Logbook& log) const
         if (It.More()) aShapeShell = It.Value();
       }
       if (aShapeShell.ShapeType() == TopAbs_SHELL) {
-        B.Add(Sol, aShapeShell);
+        aMkSolid.Add(TopoDS::Shell(aShapeShell));
         ish++;
       }
     }
-    if (ish == 0) return 0;
+    if (ish == 0 || !aMkSolid.IsDone()) return 0;
+
+    TopoDS_Solid Sol = aMkSolid.Solid();
     BRepClass3d_SolidClassifier SC (Sol);
     SC.PerformInfinitePoint(Precision::Confusion());
     if (SC.State() == TopAbs_IN)
@@ -410,6 +400,8 @@ Standard_Integer GEOMImpl_ShapeDriver::Execute(TFunction_Logbook& log) const
       aShape = Sol;
   }
   else if (aType == COMPOUND_SHAPES) {
+    anExpectedType = TopAbs_COMPOUND;
+
     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
     unsigned int ind, nbshapes = aShapes->Length();
 
@@ -428,31 +420,9 @@ Standard_Integer GEOMImpl_ShapeDriver::Execute(TFunction_Logbook& log) const
     aShape = C;
 
   }
-  /*
-  else if (aType == REVERSE_ORIENTATION) {
-    Handle(GEOM_Function) aRefShape = aCI.GetBase();
-    TopoDS_Shape aShape_i = aRefShape->GetValue();
-    if (aShape_i.IsNull()) {
-       Standard_NullObject::Raise("Shape for reverse is null");
-    }
-
-    BRepBuilderAPI_Copy Copy(aShape_i);
-    if( Copy.IsDone() ) {
-      TopoDS_Shape tds = Copy.Shape();
-      if( tds.IsNull() ) {
-        Standard_ConstructionError::Raise("Orientation aborted : Can not reverse the shape");
-      }
-
-      if( tds.Orientation() == TopAbs_FORWARD)
-        tds.Orientation(TopAbs_REVERSED);
-      else
-        tds.Orientation(TopAbs_FORWARD);
-
-      aShape = tds;
-    }
-  }
-  */
   else if (aType == EDGE_WIRE) {
+    anExpectedType = TopAbs_EDGE;
+
     Handle(GEOM_Function) aRefBase = aCI.GetBase();
     TopoDS_Shape aWire = aRefBase->GetValue();
     Standard_Real LinTol = aCI.GetTolerance();
@@ -462,6 +432,8 @@ Standard_Integer GEOMImpl_ShapeDriver::Execute(TFunction_Logbook& log) const
     aShape = MakeEdgeFromWire(aWire, LinTol, AngTol);
   }
   else if (aType == EDGE_CURVE_LENGTH) {
+    anExpectedType = TopAbs_EDGE;
+
     GEOMImpl_IVector aVI (aFunction);
 
     // RefCurve
@@ -577,6 +549,13 @@ Standard_Integer GEOMImpl_ShapeDriver::Execute(TFunction_Logbook& log) const
     aShape = aSfs->Shape();
   }
 
+  // Check if the result shape type is compatible with the expected.
+  const TopAbs_ShapeEnum aShType = aShape.ShapeType();
+
+  if (anExpectedType != TopAbs_SHAPE && anExpectedType != aShType) {
+    Standard_ConstructionError::Raise("Result type check failed");
+  }
+
   aFunction->SetValue(aShape);
 
   log.SetTouched(Label());
@@ -674,7 +653,7 @@ TopoDS_Edge GEOMImpl_ShapeDriver::MakeEdgeFromWire(const TopoDS_Shape& aWire,
     TColStd_SequenceOfReal TolSeq;
     GeomAbs_CurveType CurType;
     TopoDS_Vertex FirstVertex, LastVertex;
-    Standard_Boolean FinalReverse = Standard_False;
+    Standard_Real aPntShiftDist = 0.;
 
     BRepTools_WireExplorer wexp(theWire) ;
     for (; wexp.More(); wexp.Next())
@@ -706,8 +685,6 @@ TopoDS_Edge GEOMImpl_ShapeDriver::MakeEdgeFromWire(const TopoDS_Shape& aWire,
         LparSeq.Append(lpar);
         CurType = aType;
         FirstVertex = wexp.CurrentVertex();
-        if (anEdge.Orientation() == TopAbs_REVERSED)
-          FinalReverse = Standard_True;
       }
       else
       {
@@ -754,6 +731,18 @@ TopoDS_Edge GEOMImpl_ShapeDriver::MakeEdgeFromWire(const TopoDS_Shape& aWire,
                 gp_Pnt P2 = ElCLib::Value(lpar, aLine);
                 NewFpar = ElCLib::Parameter(PrevLine, P1);
                 NewLpar = ElCLib::Parameter(PrevLine, P2);
+
+                // Compute shift
+                if (ConnectByOrigin == TopAbs_FORWARD) {
+                  gp_Pnt aNewP2 = ElCLib::Value(NewLpar, PrevLine);
+
+                  aPntShiftDist += P2.Distance(aNewP2);
+                } else {
+                  gp_Pnt aNewP1 = ElCLib::Value(NewFpar, PrevLine);
+
+                  aPntShiftDist += P1.Distance(aNewP1);
+                }
+
                 if (NewLpar < NewFpar)
                 {
                   Standard_Real MemNewFpar = NewFpar;
@@ -773,6 +762,8 @@ TopoDS_Edge GEOMImpl_ShapeDriver::MakeEdgeFromWire(const TopoDS_Shape& aWire,
                   Abs(aCircle.Radius() - PrevCircle.Radius()) <= LinTol &&
                   aCircle.Axis().IsParallel(PrevCircle.Axis(), AngTol))
               {
+                const Standard_Boolean isFwd = ConnectByOrigin == TopAbs_FORWARD;
+
                 if (aCircle.Axis().Direction() * PrevCircle.Axis().Direction() < 0.)
                 {
                   Standard_Real memfpar = fpar;
@@ -784,6 +775,18 @@ TopoDS_Edge GEOMImpl_ShapeDriver::MakeEdgeFromWire(const TopoDS_Shape& aWire,
                 gp_Pnt P2 = ElCLib::Value(lpar, aCircle);
                 NewFpar = ElCLib::Parameter(PrevCircle, P1);
                 NewLpar = ElCLib::Parameter(PrevCircle, P2);
+
+                // Compute shift
+                if (isFwd) {
+                  gp_Pnt aNewP2 = ElCLib::Value(NewLpar, PrevCircle);
+
+                  aPntShiftDist += P2.Distance(aNewP2);
+                } else {
+                  gp_Pnt aNewP1 = ElCLib::Value(NewFpar, PrevCircle);
+
+                  aPntShiftDist += P1.Distance(aNewP1);
+                }
+
                 if (NewLpar < NewFpar)
                   NewLpar += 2.*M_PI;
                 //Standard_Real MemNewFpar = NewFpar, MemNewLpar =  NewLpar;
@@ -809,6 +812,8 @@ TopoDS_Edge GEOMImpl_ShapeDriver::MakeEdgeFromWire(const TopoDS_Shape& aWire,
                   Abs(anEllipse.MinorRadius() - PrevEllipse.MinorRadius()) <= LinTol &&
                   anEllipse.Axis().IsParallel(PrevEllipse.Axis(), AngTol))
               {
+                const Standard_Boolean isFwd = ConnectByOrigin == TopAbs_FORWARD;
+
                 if (anEllipse.Axis().Direction() * PrevEllipse.Axis().Direction() < 0.)
                 {
                   Standard_Real memfpar = fpar;
@@ -820,6 +825,18 @@ TopoDS_Edge GEOMImpl_ShapeDriver::MakeEdgeFromWire(const TopoDS_Shape& aWire,
                 gp_Pnt P2 = ElCLib::Value(lpar, anEllipse);
                 NewFpar = ElCLib::Parameter(PrevEllipse, P1);
                 NewLpar = ElCLib::Parameter(PrevEllipse, P2);
+
+                // Compute shift
+                if (isFwd) {
+                  gp_Pnt aNewP2 = ElCLib::Value(NewLpar, PrevEllipse);
+
+                  aPntShiftDist += P2.Distance(aNewP2);
+                } else {
+                  gp_Pnt aNewP1 = ElCLib::Value(NewFpar, PrevEllipse);
+
+                  aPntShiftDist += P1.Distance(aNewP1);
+                }
+
                 if (NewLpar < NewFpar)
                   NewLpar += 2.*M_PI;
                 if (ConnectByOrigin == TopAbs_FORWARD)
@@ -848,6 +865,18 @@ TopoDS_Edge GEOMImpl_ShapeDriver::MakeEdgeFromWire(const TopoDS_Shape& aWire,
                 gp_Pnt P2 = ElCLib::Value(lpar, aHypr);
                 NewFpar = ElCLib::Parameter(PrevHypr, P1);
                 NewLpar = ElCLib::Parameter(PrevHypr, P2);
+
+                // Compute shift
+                if (ConnectByOrigin == TopAbs_FORWARD) {
+                  gp_Pnt aNewP2 = ElCLib::Value(NewLpar, PrevHypr);
+
+                  aPntShiftDist += P2.Distance(aNewP2);
+                } else {
+                  gp_Pnt aNewP1 = ElCLib::Value(NewFpar, PrevHypr);
+
+                  aPntShiftDist += P1.Distance(aNewP1);
+                }
+
                 if (NewLpar < NewFpar)
                 {
                   Standard_Real MemNewFpar = NewFpar;
@@ -872,6 +901,18 @@ TopoDS_Edge GEOMImpl_ShapeDriver::MakeEdgeFromWire(const TopoDS_Shape& aWire,
                 gp_Pnt P2 = ElCLib::Value(lpar, aParab);
                 NewFpar = ElCLib::Parameter(PrevParab, P1);
                 NewLpar = ElCLib::Parameter(PrevParab, P2);
+
+                // Compute shift
+                if (ConnectByOrigin == TopAbs_FORWARD) {
+                  gp_Pnt aNewP2 = ElCLib::Value(NewLpar, PrevParab);
+
+                  aPntShiftDist += P2.Distance(aNewP2);
+                } else {
+                  gp_Pnt aNewP1 = ElCLib::Value(NewFpar, PrevParab);
+
+                  aPntShiftDist += P1.Distance(aNewP1);
+                }
+
                 if (NewLpar < NewFpar)
                 {
                   Standard_Real MemNewFpar = NewFpar;
@@ -901,19 +942,18 @@ TopoDS_Edge GEOMImpl_ShapeDriver::MakeEdgeFromWire(const TopoDS_Shape& aWire,
           LocSeq.Append(aLocShape);
           FparSeq.Append(fpar);
           LparSeq.Append(lpar);
-          TolSeq.Append(BRep_Tool::Tolerance(CurVertex));
+          TolSeq.Append(aPntShiftDist + BRep_Tool::Tolerance(CurVertex));
+          aPntShiftDist = 0.;
           CurType = aType;
         }
       } // end of else (CurveSeq.IsEmpty()) -> not first time
     } // end for (; wexp.More(); wexp.Next())
 
     LastVertex = wexp.CurrentVertex();
-    TolSeq.Append(BRep_Tool::Tolerance(LastVertex));
+    TolSeq.Append(aPntShiftDist + BRep_Tool::Tolerance(LastVertex));
 
-    TopoDS_Vertex FirstVtx_final = (FinalReverse)? LastVertex : FirstVertex;
-    FirstVtx_final.Orientation(TopAbs_FORWARD);
-    TopoDS_Vertex LastVtx_final = (FinalReverse)? FirstVertex : LastVertex;
-    LastVtx_final.Orientation(TopAbs_REVERSED);
+    FirstVertex.Orientation(TopAbs_FORWARD);
+    LastVertex.Orientation(TopAbs_REVERSED);
 
     if (!CurveSeq.IsEmpty())
     {
@@ -981,26 +1021,89 @@ TopoDS_Edge GEOMImpl_ShapeDriver::MakeEdgeFromWire(const TopoDS_Shape& aWire,
         if (concatcurve->Value(concatcurve->Lower())->Continuity()==GeomAbs_C0){
           Standard_ConstructionError::Raise("Construction aborted : The given Wire has sharp bends between some Edges, no valid Edge can be built");
         }
-        ResEdge = BRepLib_MakeEdge(concatcurve->Value(concatcurve->Lower()),
-                                   FirstVtx_final, LastVtx_final,
-                                   concatcurve->Value(concatcurve->Lower())->FirstParameter(),
-                                   concatcurve->Value(concatcurve->Lower())->LastParameter());
+
+        Standard_Boolean isValidEndVtx = Standard_True;
+
+        if (closed_flag) {
+          // Check if closed curve is reordered.
+          Handle(Geom_Curve) aCurve  = concatcurve->Value(concatcurve->Lower());
+          Standard_Real      aFPar   = aCurve->FirstParameter();
+          gp_Pnt             aPFirst;
+          gp_Pnt             aPntVtx = BRep_Tool::Pnt(FirstVertex);
+          Standard_Real      aTolVtx = BRep_Tool::Tolerance(FirstVertex);
+
+          aCurve->D0(aFPar, aPFirst);
+
+          if (!aPFirst.IsEqual(aPntVtx, aTolVtx)) {
+            // The curve is reordered. Find the new first and last vertices.
+            TopTools_IndexedMapOfShape aMapVtx;
+            TopExp::MapShapes(theWire, TopAbs_VERTEX, aMapVtx);
+
+            const Standard_Integer aNbVtx = aMapVtx.Extent();
+            Standard_Integer       iVtx;
+
+            for (iVtx = 1; iVtx <= aNbVtx; iVtx++) {
+              const TopoDS_Vertex aVtx = TopoDS::Vertex(aMapVtx.FindKey(iVtx));
+              const gp_Pnt        aPnt = BRep_Tool::Pnt(aVtx);
+              const Standard_Real aTol = BRep_Tool::Tolerance(aVtx);
+
+              if (aPFirst.IsEqual(aPnt, aTol)) {
+                // The coinsident vertex is found.
+                FirstVertex = aVtx;
+                LastVertex  = aVtx;
+                FirstVertex.Orientation(TopAbs_FORWARD);
+                LastVertex.Orientation(TopAbs_REVERSED);
+                break;
+              }
+            }
+
+            if (iVtx > aNbVtx) {
+              // It is necessary to create new vertices.
+              isValidEndVtx = Standard_False;
+            }
+          }
+        }
+
+        if (isValidEndVtx) {
+          ResEdge = BRepLib_MakeEdge(concatcurve->Value(concatcurve->Lower()),
+                                     FirstVertex, LastVertex,
+                                     concatcurve->Value(concatcurve->Lower())->FirstParameter(),
+                                     concatcurve->Value(concatcurve->Lower())->LastParameter());
+        } else {
+          ResEdge = BRepLib_MakeEdge(concatcurve->Value(concatcurve->Lower()),
+                                     concatcurve->Value(concatcurve->Lower())->FirstParameter(),
+                                     concatcurve->Value(concatcurve->Lower())->LastParameter());
+        }
       }
       else
       {
         if (CurveSeq(1)->IsInstance(STANDARD_TYPE(Geom_TrimmedCurve)))
           CurveSeq(1) = (*((Handle(Geom_TrimmedCurve)*)&(CurveSeq(1))))->BasisCurve();
 
-        CurveSeq(1)->Transform(LocSeq(1).Location().Transformation());
-        ResEdge = BRepLib_MakeEdge(CurveSeq(1),
-                                   FirstVtx_final, LastVtx_final,
+        Handle(Geom_Curve) aNewCurve =
+          Handle(Geom_Curve)::DownCast(CurveSeq(1)->Copy());
+
+        aNewCurve->Transform(LocSeq(1).Location().Transformation());
+
+        if (LocSeq(1).Orientation() == TopAbs_REVERSED) {
+          const TopoDS_Vertex aVtxTmp = FirstVertex;
+
+          FirstVertex = LastVertex;
+          LastVertex  = aVtxTmp;
+          FirstVertex.Orientation(TopAbs_FORWARD);
+          LastVertex.Orientation(TopAbs_REVERSED);
+        }
+
+        ResEdge = BRepLib_MakeEdge(aNewCurve,
+                                   FirstVertex, LastVertex,
                                    FparSeq(1), LparSeq(1));
+
+        if (LocSeq(1).Orientation() == TopAbs_REVERSED) {
+          ResEdge.Reverse();
+        }
       }
     }
 
-    if (FinalReverse)
-      ResEdge.Reverse();
-
     return ResEdge;
 }
 
@@ -1016,7 +1119,7 @@ TopoDS_Shape GEOMImpl_ShapeDriver::MakeIsoline
                              const double       theParameter) const
 {
   TopoDS_Shape          aResult;
-  GEOMUtils_Hatcher     aHatcher(theFace);
+  GEOMUtils::Hatcher    aHatcher(theFace);
   const GeomAbs_IsoType aType = (IsUIso ? GeomAbs_IsoU : GeomAbs_IsoV);
 
   aHatcher.Init(aType, theParameter);
@@ -1135,7 +1238,6 @@ GetCreationInformation(std::string&             theOperationName,
     theOperationName = "SHELL";
     AddParam( theParams, "Objects", aCI.GetShapes() );
     break;
-  case SOLID_SHELL:
   case SOLID_SHELLS:
     theOperationName = "SOLID";
     AddParam( theParams, "Objects", aCI.GetShapes() );