Salome HOME
Merge V8_4_BR branch.
[modules/geom.git] / src / GEOMImpl / GEOMImpl_BlockDriver.cxx
index c6527f70ea3e1e3444bf0160bb698cee68139c16..797495faca97e3ae02d5d5e85b6d2ddd25013627 100644 (file)
@@ -1,3 +1,24 @@
+// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
+//
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
 #include <Standard_Stream.hxx>
 
 #include <GEOM_Function.hxx>
 
 #include <ShHealOper_Sewing.hxx>
-#include <NMTAlgo_Splitter1.hxx>
+#include <ShHealOper_ShapeProcess.hxx>
+//#include <GEOMAlgo_Gluer.hxx>
 #include <BlockFix_BlockFixAPI.hxx>
+#include <BlockFix_UnionFaces.hxx>
 
 #include "utilities.h"
 
 #include <TNaming_CopyShape.hxx>
 
+#include <BRepAdaptor_Curve.hxx>
 #include <BRepLib.hxx>
 #include <BRep_Tool.hxx>
 #include <BRepTools.hxx>
@@ -33,6 +57,7 @@
 #include <BRepBuilderAPI_MakeWire.hxx>
 #include <BRepBuilderAPI_MakePolygon.hxx>
 #include <BRepBuilderAPI_Transform.hxx>
+#include <BRepCheck_Analyzer.hxx>
 #include <BRepClass_FaceClassifier.hxx>
 #include <BRepClass3d_SolidClassifier.hxx>
 #include <BRepExtrema_ExtPF.hxx>
@@ -60,6 +85,9 @@
 
 #include <GProp_GProps.hxx>
 
+#include <Geom_Line.hxx>
+#include <GC_MakeLine.hxx>
+
 #include <gp.hxx>
 #include <gp_Pnt.hxx>
 #include <gp_Ax3.hxx>
@@ -95,7 +123,7 @@ GEOMImpl_BlockDriver::GEOMImpl_BlockDriver()
 //function : Execute
 //purpose  :
 //=======================================================================
-Standard_Integer GEOMImpl_BlockDriver::Execute(TFunction_Logbook& log) const
+Standard_Integer GEOMImpl_BlockDriver::Execute(Handle(TFunction_Logbook)& log) const
 {
   if (Label().IsNull()) return 0;
   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
@@ -212,7 +240,14 @@ Standard_Integer GEOMImpl_BlockDriver::Execute(TFunction_Logbook& log) const
           ("Impossible to build a connected wire from the given edges");
       }
       TopoDS_Wire aWire = *MW;
-      delete MW; 
+      delete MW;
+
+      // check the wire closure
+      TopoDS_Vertex aV1, aV2;
+      TopExp::Vertices(aWire, aV1, aV2);
+      if ( !aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2) )
+        aWire.Closed( true );
+
       if (!aWire.Closed()) {
         Standard_ConstructionError::Raise
           ("Impossible to build a closed wire from the given edges");
@@ -241,16 +276,26 @@ Standard_Integer GEOMImpl_BlockDriver::Execute(TFunction_Logbook& log) const
 
       // create two edges, linking ends of the given edges
       TopoDS_Vertex V11, V12, V21, V22;
-      TopExp::Vertices(anEdge1, V11, V12, Standard_True);
-      TopExp::Vertices(anEdge2, V21, V22, Standard_True);
+      TopExp::Vertices(anEdge1, V11, V12, Standard_False);
+      TopExp::Vertices(anEdge2, V21, V22, Standard_False);
       if (V11.IsNull() || V12.IsNull() ||
           V21.IsNull() || V22.IsNull()) {
         Standard_NullObject::Raise("Bad edge for face construction: vertex is not defined");
       }
-      gp_Pnt P11 = BRep_Tool::Pnt(V11);
-      gp_Pnt P12 = BRep_Tool::Pnt(V12);
-      gp_Pnt P21 = BRep_Tool::Pnt(V21);
-      gp_Pnt P22 = BRep_Tool::Pnt(V22);
+
+      BRepAdaptor_Curve C1 (anEdge1);
+      BRepAdaptor_Curve C2 (anEdge2);
+      gp_Pnt P11, P12, P21, P22;
+
+      // Mantis issue 0020599: Creation of a quadrangle face from 2 edges: SIGSEGV
+      P11 = C1.Value(C1.FirstParameter());
+      P12 = C1.Value(C1.LastParameter());
+      P21 = C2.Value(C2.FirstParameter());
+      P22 = C2.Value(C2.LastParameter());
+      //gp_Pnt P11 = BRep_Tool::Pnt(V11);
+      //gp_Pnt P12 = BRep_Tool::Pnt(V12);
+      //gp_Pnt P21 = BRep_Tool::Pnt(V21);
+      //gp_Pnt P22 = BRep_Tool::Pnt(V22);
 
       if (P11.Distance(P21) < prec || P12.Distance(P22) < prec ||
           P11.Distance(P22) < prec || P12.Distance(P21) < prec) {
@@ -260,24 +305,70 @@ Standard_Integer GEOMImpl_BlockDriver::Execute(TFunction_Logbook& log) const
       Standard_Real per11 = P11.Distance(P21) + P12.Distance(P22);
       Standard_Real per12 = P11.Distance(P22) + P12.Distance(P21);
 
+      BRep_Builder BB;
+
       TopoDS_Edge anEdge3;
       TopoDS_Edge anEdge4;
+
+      // Mantis issue 0020599: Creation of a quadrangle face from 2 edges: SIGSEGV
       if (per11 < per12) {
-        anEdge3 = BRepBuilderAPI_MakeEdge(V11, V21);
-        anEdge4 = BRepBuilderAPI_MakeEdge(V12, V22);
-      } else {
-        anEdge3 = BRepBuilderAPI_MakeEdge(V11, V22);
-        anEdge4 = BRepBuilderAPI_MakeEdge(V12, V21);
+        Handle(Geom_Line) Line1 = GC_MakeLine(P11, P21).Value();
+        Handle(Geom_Line) Line2 = GC_MakeLine(P12, P22).Value();
+
+        BB.MakeEdge(anEdge3, Line1, Precision::Confusion());
+        BB.Range(anEdge3, 0., P11.Distance(P21));
+        BB.Add(anEdge3, V11.Oriented(TopAbs_FORWARD));
+        BB.Add(anEdge3, V21.Oriented(TopAbs_REVERSED));
+
+        BB.MakeEdge(anEdge4, Line2, Precision::Confusion());
+        BB.Range(anEdge4, 0., P12.Distance(P22));
+        BB.Add(anEdge4, V12.Oriented(TopAbs_FORWARD));
+        BB.Add(anEdge4, V22.Oriented(TopAbs_REVERSED));
       }
+      else {
+        Handle(Geom_Line) Line1 = GC_MakeLine(P11, P22).Value();
+        Handle(Geom_Line) Line2 = GC_MakeLine(P12, P21).Value();
+
+        BB.MakeEdge(anEdge3, Line1, Precision::Confusion());
+        BB.Range(anEdge3, 0., P11.Distance(P22));
+        BB.Add(anEdge3, V11.Oriented(TopAbs_FORWARD));
+        BB.Add(anEdge3, V22.Oriented(TopAbs_REVERSED));
+
+        BB.MakeEdge(anEdge4, Line2, Precision::Confusion());
+        BB.Range(anEdge4, 0., P12.Distance(P21));
+        BB.Add(anEdge4, V12.Oriented(TopAbs_FORWARD));
+        BB.Add(anEdge4, V21.Oriented(TopAbs_REVERSED));
+      }
+      //if (per11 < per12) {
+      //  anEdge3 = BRepBuilderAPI_MakeEdge(V11, V21);
+      //  anEdge4 = BRepBuilderAPI_MakeEdge(V12, V22);
+      //} else {
+      //  anEdge3 = BRepBuilderAPI_MakeEdge(V11, V22);
+      //  anEdge4 = BRepBuilderAPI_MakeEdge(V12, V21);
+      //}
 
       // build a wire
-      BRepBuilderAPI_MakeWire MW (anEdge1, anEdge3, anEdge2, anEdge4);
-      if (!MW.IsDone()) {
+      BRepBuilderAPI_MakeWire* MW;
+      MW = new BRepBuilderAPI_MakeWire(anEdge1, anEdge3, anEdge2, anEdge4);
+      if (!MW->IsDone()) {
         Standard_ConstructionError::Raise("Wire construction failed");
       }
 
+      TopoDS_Wire aWire = *MW;
+      delete MW;
+
+      TopoDS_Vertex aV1, aV2;
+      TopExp::Vertices(aWire, aV1, aV2);
+      if ( !aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2) )
+        aWire.Closed( true );
+
+      if (!aWire.Closed()) {
+        Standard_ConstructionError::Raise
+          ("Impossible to build a closed wire from the given edges");
+      }
+
       // try to build face on the wire
-      GEOMImpl_Block6Explorer::MakeFace(MW, Standard_False, aShape);
+      GEOMImpl_Block6Explorer::MakeFace(aWire, Standard_False, aShape);
       if (aShape.IsNull()) {
         Standard_ConstructionError::Raise("Face construction failed");
       }
@@ -335,6 +426,7 @@ Standard_Integer GEOMImpl_BlockDriver::Execute(TFunction_Logbook& log) const
       }
 
       // try to build face on the wire
+      aMkPoly.Close();
       GEOMImpl_Block6Explorer::MakeFace(aMkPoly, Standard_False, aShape);
       if (aShape.IsNull()) {
         Standard_ConstructionError::Raise("Face construction failed");
@@ -345,7 +437,6 @@ Standard_Integer GEOMImpl_BlockDriver::Execute(TFunction_Logbook& log) const
       BRepTools_Quilt Glue;
 
       if (aType == BLOCK_SIX_FACES) {
-
         // Make block (hexahedral solid) from six faces
         for (Standard_Integer ind = 1; ind <= nbshapes; ind++) {
           if (anArgs(ind).ShapeType() != TopAbs_FACE) {
@@ -354,8 +445,8 @@ Standard_Integer GEOMImpl_BlockDriver::Execute(TFunction_Logbook& log) const
           Glue.Add(anArgs(ind));
         }
 
-      } else {
-
+      }
+      else {
         // Make block (hexahedral solid) from two opposite faces
         if (anArgs(1).ShapeType() != TopAbs_FACE ||
             anArgs(2).ShapeType() != TopAbs_FACE) {
@@ -439,9 +530,8 @@ Standard_Integer GEOMImpl_BlockDriver::Execute(TFunction_Logbook& log) const
       }
       aShape = Sol;
       BRepLib::SameParameter(aShape, 1.E-5, Standard_True);
-
-    } else if (aType == BLOCK_COMPOUND_GLUE) {
-
+    }
+    else if (aType == BLOCK_COMPOUND_GLUE) {
       // Make blocks compound from a compound
       if (anArgs(1).ShapeType() != TopAbs_COMPOUND &&
           anArgs(2).ShapeType() != TopAbs_COMPSOLID) {
@@ -450,16 +540,15 @@ Standard_Integer GEOMImpl_BlockDriver::Execute(TFunction_Logbook& log) const
 
       TopoDS_Shape aCompound = anArgs(1);
 
-      // Glue coincident faces and edges (with Partition algorithm).
-      NMTAlgo_Splitter1 PS;
-      PS.AddShape(aCompound);
-      PS.Compute();
-      PS.SetRemoveWebs(Standard_False);
-//      PS.Build(aCompound.ShapeType());
-      PS.Build(TopAbs_SOLID);
-
-      aShape = PS.Shape();
-    } else {
+      // Glue coincident faces and edges
+      aShape = GEOMImpl_GlueDriver::GlueFaces(aCompound, Precision::Confusion(), Standard_True);
+      //GEOMAlgo_Gluer aGluer;
+      //aGluer.SetShape(aCompound);
+      //aGluer.SetCheckGeometry(Standard_True);
+      //aGluer.Perform();
+      //aShape = aGluer.Result();
+    }
+    else {
     }
 
   } else { // Multi-transformations and compound improving
@@ -474,28 +563,47 @@ Standard_Integer GEOMImpl_BlockDriver::Execute(TFunction_Logbook& log) const
         Standard_NullObject::Raise("Null Shape given");
       }
 
+      // Copy shape to avoid problems (Mantis issue 0021683)
+      TopoDS_Shape aShapeCopy;
+      TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
+      TNaming_CopyShape::CopyTool(aBlockOrComp, aMapTShapes, aShapeCopy);
+      aBlockOrComp = aShapeCopy;
+
       // 1. Improve solids with seam and/or degenerated edges
       BlockFix_BlockFixAPI aTool;
       //aTool.Tolerance() = toler;
+      aTool.OptimumNbFaces() = aCI.GetOptimumNbFaces();
       aTool.SetShape(aBlockOrComp);
       aTool.Perform();
 
-      if (aType == BLOCK_REMOVE_EXTRA) {
+      TopoDS_Shape aFixedExtra = aTool.Shape();
+
+      // Repair result
+      BRepCheck_Analyzer ana (aFixedExtra, false);
+      if (!ana.IsValid()) {
+        TopoDS_Shape aFixed;
+        ShHealOper_ShapeProcess aHealer;
+        aHealer.Perform(aFixedExtra, aFixed);
+        if (aHealer.isDone())
+          aFixedExtra = aFixed;
+      }
+
+      if (aType == BLOCK_REMOVE_EXTRA)
+      {
+        aShape = aFixedExtra;
 
-        aShape = aTool.Shape();
         if (aShape == aBlockOrComp) {
           MESSAGE("No modifications have been done");
         }
-
-      } else { // aType == BLOCK_COMPOUND_IMPROVE
-
-        TopoDS_Shape aFixedExtra = aTool.Shape();
-
+      }
+      else // aType == BLOCK_COMPOUND_IMPROVE
+      {
         // 2. Separate non-blocks
         TopTools_ListOfShape BLO; // All blocks from the given compound
         TopTools_ListOfShape NOT; // Not blocks
         TopTools_ListOfShape EXT; // Hexahedral solids, having degenerated and/or seam edges
-        GEOMImpl_IBlocksOperations::AddBlocksFrom(aFixedExtra, BLO, NOT, EXT);
+        TopTools_ListOfShape NOQ; // All non-quadrangular faces
+        GEOMImpl_IBlocksOperations::AddBlocksFrom(aFixedExtra, BLO, NOT, EXT, NOQ);
 
         if (NOT.Extent() > 0) {
           MESSAGE("Some non-blocks have been removed");
@@ -518,7 +626,7 @@ Standard_Integer GEOMImpl_BlockDriver::Execute(TFunction_Logbook& log) const
         }
 
         // 5. Glue Faces
-        aShape = GEOMImpl_GlueDriver::GlueFaces(aComp, Precision::Confusion());
+        aShape = GEOMImpl_GlueDriver::GlueFaces(aComp, Precision::Confusion(), Standard_False);
       }
 
     } else if (aType == BLOCK_MULTI_TRANSFORM_1D ||
@@ -624,8 +732,20 @@ Standard_Integer GEOMImpl_BlockDriver::Execute(TFunction_Logbook& log) const
       }
 
       // Glue faces of the multi-block
-      aShape = GEOMImpl_GlueDriver::GlueFaces(aMulti, aTol);
+      aShape = GEOMImpl_GlueDriver::GlueFaces(aMulti, aTol, Standard_False);
+
+    } else if (aType == BLOCK_UNION_FACES) {
+      GEOMImpl_IBlockTrsf aCI (aFunction);
+      Handle(GEOM_Function) aRefShape = aCI.GetOriginal();
+      TopoDS_Shape aBlockOrComp = aRefShape->GetValue();
+      if (aBlockOrComp.IsNull()) {
+        Standard_NullObject::Raise("Null Shape given");
+      }
 
+      BlockFix_UnionFaces aFaceUnifier;
+  
+      aFaceUnifier.GetOptimumNbFaces() = 0; // To force union faces.
+      aShape = aFaceUnifier.Perform(aBlockOrComp);
     } else { // unknown function type
       return 0;
     }
@@ -635,7 +755,7 @@ Standard_Integer GEOMImpl_BlockDriver::Execute(TFunction_Logbook& log) const
 
   aFunction->SetValue(aShape);
 
-  log.SetTouched(Label());
+  log->SetTouched(Label());
 
   return 1;
 }
@@ -943,44 +1063,97 @@ void GEOMImpl_BlockDriver::MultiTransformate2D (const TopoDS_Shape&    theBlock,
   theResult = aCompound;
 }
 
-//=======================================================================
-//function :  GEOMImpl_BlockDriver_Type_
-//purpose  :
-//=======================================================================
-Standard_EXPORT Handle_Standard_Type& GEOMImpl_BlockDriver_Type_()
-{
+//================================================================================
+/*!
+ * \brief Returns a name of creation operation and names and values of creation parameters
+ */
+//================================================================================
 
-  static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
-  if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
-  static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
-  if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
-  static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
-  if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
-
-
-  static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
-  static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_BlockDriver",
-                                                         sizeof(GEOMImpl_BlockDriver),
-                                                         1,
-                                                         (Standard_Address)_Ancestors,
-                                                         (Standard_Address)NULL);
-
-  return _aType;
-}
-
-//=======================================================================
-//function : DownCast
-//purpose  :
-//=======================================================================
-const Handle(GEOMImpl_BlockDriver) Handle(GEOMImpl_BlockDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
+bool GEOMImpl_BlockDriver::
+GetCreationInformation(std::string&             theOperationName,
+                       std::vector<GEOM_Param>& theParams)
 {
-  Handle(GEOMImpl_BlockDriver) _anOtherObject;
-
-  if (!AnObject.IsNull()) {
-     if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_BlockDriver))) {
-       _anOtherObject = Handle(GEOMImpl_BlockDriver)((Handle(GEOMImpl_BlockDriver)&)AnObject);
-     }
+  if (Label().IsNull()) return 0;
+  Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
+
+  Standard_Integer aType = function->GetType();
+  GEOMImpl_IBlocks    aCI1 (function);
+  GEOMImpl_IBlockTrsf aCI2 (function);
+  Handle(TColStd_HSequenceOfTransient) aShapes = aCI1.GetShapes();
+
+  switch ( aType ) {
+  case BLOCK_FACE_FOUR_EDGES:
+    theOperationName = "Q_FACE";
+    AddParam( theParams, "Edge 1", aShapes->Value(1) );
+    AddParam( theParams, "Edge 2", aShapes->Value(2) );
+    AddParam( theParams, "Edge 3", aShapes->Value(3) );
+    AddParam( theParams, "Edge 4", aShapes->Value(4) );
+    break;
+  case BLOCK_FACE_TWO_EDGES:
+    theOperationName = "Q_FACE";
+    AddParam( theParams, "Edge 1", aShapes->Value(1) );
+    AddParam( theParams, "Edge 2", aShapes->Value(2) );
+    break;
+  case BLOCK_FACE_FOUR_PNT:
+    theOperationName = "Q_FACE";
+    AddParam( theParams, "Point 1", aShapes->Value(1) );
+    AddParam( theParams, "Point 2", aShapes->Value(2) );
+    AddParam( theParams, "Point 3", aShapes->Value(3) );
+    AddParam( theParams, "Point 4", aShapes->Value(4) );
+    break;
+  case BLOCK_SIX_FACES:
+    theOperationName = "HEX_SOLID";
+    AddParam( theParams, "Face 1", aShapes->Value(1) );
+    AddParam( theParams, "Face 2", aShapes->Value(2) );
+    AddParam( theParams, "Face 3", aShapes->Value(3) );
+    AddParam( theParams, "Face 4", aShapes->Value(4) );
+    AddParam( theParams, "Face 5", aShapes->Value(5) );
+    AddParam( theParams, "Face 6", aShapes->Value(6) );
+    break;
+  case BLOCK_TWO_FACES:
+    theOperationName = "HEX_SOLID";
+    AddParam( theParams, "Face 1", aShapes->Value(1) );
+    AddParam( theParams, "Face 2", aShapes->Value(2) );
+    break;
+  case BLOCK_COMPOUND_GLUE:
+    theOperationName = "MakeBlockCompound";
+    AddParam( theParams, "Compound", aShapes->Value(1) );
+    break;
+  case BLOCK_REMOVE_EXTRA:
+    theOperationName = "REMOVE_EXTRA_EDGES";
+    AddParam( theParams, "Selected shape", aCI2.GetOriginal() );
+    AddParam( theParams, "Union faces", aCI2.GetOptimumNbFaces() == 0);
+    break;
+  case BLOCK_COMPOUND_IMPROVE:
+    theOperationName = "CHECK_COMPOUND";
+    AddParam( theParams, "Selected shape", aCI2.GetOriginal() );
+    break;
+  case BLOCK_MULTI_TRANSFORM_1D:
+    theOperationName = "MUL_TRANSFORM";
+    AddParam( theParams, "Main Object", aCI2.GetOriginal() );
+    AddParam( theParams, "Face 1", aCI2.GetFace1U() );
+    AddParam( theParams, "Face 2", aCI2.GetFace2U() );
+    AddParam( theParams, "Nb. Times", aCI2.GetNbIterU() );
+    break;
+  case BLOCK_MULTI_TRANSFORM_2D:
+    theOperationName = "MUL_TRANSFORM";
+    AddParam( theParams, "Main Object", aCI2.GetOriginal() );
+    AddParam( theParams, "Face 1 U", aCI2.GetFace1U() );
+    AddParam( theParams, "Face 2 U", aCI2.GetFace2U() );
+    AddParam( theParams, "Nb. Times V", aCI2.GetNbIterV() );
+    AddParam( theParams, "Face 1 V", aCI2.GetFace1V() );
+    AddParam( theParams, "Face 2 V", aCI2.GetFace2V() );
+    AddParam( theParams, "Nb. Times V", aCI2.GetNbIterV() );
+    break;
+  case BLOCK_UNION_FACES:
+    theOperationName = "UNION_FACES";
+    AddParam( theParams, "Selected shape", aCI2.GetOriginal() );
+    break;
+  default:
+    return false;
   }
 
-  return _anOtherObject ;
+  return true;
 }
+
+IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_BlockDriver,GEOM_BaseDriver);