Salome HOME
Bug 0020052: EDF 867 GEOM: Non removable extra edges are created with fuse operation...
[modules/geom.git] / src / GEOMAlgo / BlockFix_UnionEdges.cxx
index f36e8c7e1b5a9bcc62c50452bb5705051f255cd5..4ac917101bab090ee3e36b9d9e6012b8a642072c 100644 (file)
@@ -1,8 +1,28 @@
+//  Copyright (C) 2007-2008  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.
+//
+//  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
+//
 // File:      BlockFix_UnionEdges.cxx
 // Created:   07.12.04 15:27:30
 // Author:    Sergey KUUL
-
-
+//
 #include <BlockFix_UnionEdges.ixx>
 
 #include <Approx_Curve3d.hxx>
@@ -18,6 +38,7 @@
 #include <ShapeAnalysis_Edge.hxx>
 #include <ShapeFix_Edge.hxx>
 #include <ShapeFix_Face.hxx>
+#include <ShapeFix_Shell.hxx>
 #include <TColgp_SequenceOfPnt.hxx>
 #include <TColStd_MapOfInteger.hxx>
 #include <TopExp.hxx>
 #include <TopoDS.hxx>
 #include <TopoDS_Edge.hxx>
 #include <TopoDS_Face.hxx>
+#include <TopoDS_Shell.hxx>
 #include <TopoDS_Solid.hxx>
 #include <TopoDS_Vertex.hxx>
 #include <TopoDS_Iterator.hxx>
 
+#include "utilities.h"
 
 //=======================================================================
 //function : BlockFix_UnionEdges()
@@ -67,7 +90,7 @@ static Standard_Boolean MergeEdges(const TopTools_SequenceOfShape& SeqEdges,
   TColStd_MapOfInteger IndUsedEdges;
   IndUsedEdges.Add(1);
   Standard_Integer j;
-  for (j = 2; j <= SeqEdges.Length(); j++) {
+  for(j=2; j<=SeqEdges.Length(); j++) {
     for(Standard_Integer k=2; k<=SeqEdges.Length(); k++) {
       if(IndUsedEdges.Contains(k)) continue;
       TopoDS_Edge edge = TopoDS::Edge(SeqEdges.Value(k));
@@ -88,14 +111,14 @@ static Standard_Boolean MergeEdges(const TopTools_SequenceOfShape& SeqEdges,
     }
   }
   if(aChain.Length()<SeqEdges.Length()) {
-    cout<<"can not create correct chain..."<<endl;
+    MESSAGE ("can not create correct chain...");
     return Standard_False;
   }
   // union edges in chain
   // first step: union lines and circles
   TopLoc_Location Loc;
   Standard_Real fp1,lp1,fp2,lp2;
-  for (j = 1; j < aChain.Length(); j++) {
+  for(j=1; j<aChain.Length(); j++) {
     TopoDS_Edge edge1 = TopoDS::Edge(aChain.Value(j));
     Handle(Geom_Curve) c3d1 = BRep_Tool::Curve(edge1,Loc,fp1,lp1);
     if(c3d1.IsNull()) break;
@@ -113,6 +136,7 @@ static Standard_Boolean MergeEdges(const TopTools_SequenceOfShape& SeqEdges,
       c3d2 = tc->BasisCurve();
     }
     if( c3d1->IsKind(STANDARD_TYPE(Geom_Line)) && c3d2->IsKind(STANDARD_TYPE(Geom_Line)) ) {
+      // union lines
       Handle(Geom_Line) L1 = Handle(Geom_Line)::DownCast(c3d1);
       Handle(Geom_Line) L2 = Handle(Geom_Line)::DownCast(c3d2);
       gp_Dir Dir1 = L1->Position().Direction();
@@ -140,6 +164,7 @@ static Standard_Boolean MergeEdges(const TopTools_SequenceOfShape& SeqEdges,
       j--;
     }
     if( c3d1->IsKind(STANDARD_TYPE(Geom_Circle)) && c3d2->IsKind(STANDARD_TYPE(Geom_Circle)) ) {
+      // union circles
       Handle(Geom_Circle) C1 = Handle(Geom_Circle)::DownCast(c3d1);
       Handle(Geom_Circle) C2 = Handle(Geom_Circle)::DownCast(c3d2);
       gp_Pnt P01 = C1->Location();
@@ -174,45 +199,49 @@ static Standard_Boolean MergeEdges(const TopTools_SequenceOfShape& SeqEdges,
     }
   }
   if(j<aChain.Length()) {
-    cout<<"null curve3d in edge..."<<endl;
+    MESSAGE ("null curve3d in edge...");
     return Standard_False;
   }
   if(aChain.Length()>1) {
     // second step: union edges with various curves
-    cout<<"can not make analitical union => make approximation"<<endl;
-    TopoDS_Wire W;
-    B.MakeWire(W);
+    // skl for bug 0020052 from Mantis: perform such unions
+    // only if curves are bspline or bezier
+    bool NeedUnion = true;
     for(j=1; j<=aChain.Length(); j++) {
       TopoDS_Edge edge = TopoDS::Edge(aChain.Value(j));
-      B.Add(W,edge);
+      Handle(Geom_Curve) c3d = BRep_Tool::Curve(edge,Loc,fp1,lp1);
+      if(c3d.IsNull()) continue;
+      while(c3d->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
+        Handle(Geom_TrimmedCurve) tc =
+          Handle(Geom_TrimmedCurve)::DownCast(c3d);
+        c3d = tc->BasisCurve();
+      }
+      if( ( c3d->IsKind(STANDARD_TYPE(Geom_BSplineCurve)) ||
+            c3d->IsKind(STANDARD_TYPE(Geom_BezierCurve)) ) ) continue;
+      NeedUnion = false;
+      break;
+    }
+    if(NeedUnion) {
+      MESSAGE ("can not make analitical union => make approximation");
+      TopoDS_Wire W;
+      B.MakeWire(W);
+      for(j=1; j<=aChain.Length(); j++) {
+        TopoDS_Edge edge = TopoDS::Edge(aChain.Value(j));
+        B.Add(W,edge);
+      }
+      Handle(BRepAdaptor_HCompCurve) Adapt = new BRepAdaptor_HCompCurve(W);
+      Approx_Curve3d Conv(Adapt,Tol,GeomAbs_C1,9,1000);
+      Handle(Geom_BSplineCurve) bc = Conv.Curve();
+      TopoDS_Edge E;
+      B.MakeEdge (E,bc,Precision::Confusion());
+      B.Add (E,VF);
+      B.Add (E,VL);
+      aChain.SetValue(1,E);
+    }
+    else {
+      MESSAGE ("can not make approximation for such types of curves");
+      return Standard_False;
     }
-    Handle(BRepAdaptor_HCompCurve) Adapt = new BRepAdaptor_HCompCurve(W);
-    Approx_Curve3d Conv(Adapt,Tol,GeomAbs_C1,9,1000);
-    Handle(Geom_BSplineCurve) bc = Conv.Curve();
-    TopoDS_Edge E;
-    B.MakeEdge (E,bc,Precision::Confusion());
-    B.Add (E,VF);
-    B.Add (E,VL);
-    //TopLoc_Location L;
-    //Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace,L);
-    //ShapeFix_Edge sfe;
-    //if(!L.IsIdentity()) {
-    //  TopoDS_Edge aPCEdge = TopoDS::Edge(E.Moved(L.Inverted()));
-    //  sfe.FixAddPCurve(aPCEdge,aFace,Standard_False);
-    //  Handle(Geom2d_Curve) c2d;
-    //  Standard_Real fp,lp;
-    //  sae.PCurve(aPCEdge,aFace,c2d,fp,lp);
-    //  B.UpdateEdge(E,c2d,aFace,0.);
-    //  B.Range(E,aFace,fp,lp);
-    //  c2d.Nullify();
-    //  B.UpdateEdge(aPCEdge,c2d,aFace,0.);
-    //  E = aPCEdge;
-    //}
-    //else {
-    //  sfe.FixAddPCurve(E,aFace,Standard_False);
-    //}
-    //sfe.FixSameParameter(E);
-    aChain.SetValue(1,E);
   }
 
   anEdge = TopoDS::Edge(aChain.Value(1));
@@ -232,21 +261,26 @@ TopoDS_Shape BlockFix_UnionEdges::Perform(const TopoDS_Shape& Shape,
   myTolerance = Tol;
   TopoDS_Shape aResult = myContext->Apply(Shape);
   
-  TopTools_IndexedMapOfShape ChangedFaces;
-
   // processing each solid
   TopExp_Explorer exps;
   for(exps.Init(Shape, TopAbs_SOLID); exps.More(); exps.Next()) {
     TopoDS_Solid aSolid = TopoDS::Solid(exps.Current());
 
+    TopTools_IndexedMapOfShape ChangedFaces;
+    
     // creating map of edge faces
     TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
     TopExp::MapShapesAndAncestors(aSolid, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
   
+    Handle(ShapeBuild_ReShape) aContext = new ShapeBuild_ReShape;
+    TopoDS_Shape aRes = aSolid;
+    aRes = aContext->Apply(aSolid);
+
     // processing each face
     TopExp_Explorer exp;
-    for(exp.Init(aSolid, TopAbs_FACE); exp.More(); exp.Next()) {
-      TopoDS_Face aFace = TopoDS::Face(myContext->Apply(exp.Current().Oriented(TopAbs_FORWARD)));
+    for(exp.Init(aRes, TopAbs_FACE); exp.More(); exp.Next()) {
+      TopoDS_Face aFace =
+        TopoDS::Face(aContext->Apply(exp.Current().Oriented(TopAbs_FORWARD)));
       TopTools_IndexedDataMapOfShapeListOfShape aMapFacesEdges;
 
       for(TopExp_Explorer expe(aFace,TopAbs_EDGE); expe.More(); expe.Next()) {
@@ -256,7 +290,7 @@ TopoDS_Shape BlockFix_UnionEdges::Perform(const TopoDS_Shape& Shape,
         TopTools_ListIteratorOfListOfShape anIter(aList);
         for( ; anIter.More(); anIter.Next()) {
           TopoDS_Face face = TopoDS::Face(anIter.Value());
-          TopoDS_Face face1 = TopoDS::Face(myContext->Apply(anIter.Value()));
+          TopoDS_Face face1 = TopoDS::Face(aContext->Apply(anIter.Value()));
           if(face1.IsSame(aFace)) continue;
           if(aMapFacesEdges.Contains(face)) {
             aMapFacesEdges.ChangeFromKey(face).Append(edge);
@@ -281,9 +315,9 @@ TopoDS_Shape BlockFix_UnionEdges::Perform(const TopoDS_Shape& Shape,
         if( MergeEdges(SeqEdges,aFace,Tol,E) ) {
           // now we have only one edge - aChain.Value(1)
           // we have to replace old ListEdges with this new edge
-          myContext->Replace(SeqEdges(1),E);
+          aContext->Replace(SeqEdges(1),E);
           for(Standard_Integer j=2; j<=SeqEdges.Length(); j++) {
-            myContext->Remove(SeqEdges(j));
+            aContext->Remove(SeqEdges(j));
           }
           TopoDS_Face tmpF = TopoDS::Face(exp.Current());
           if( !ChangedFaces.Contains(tmpF) )
@@ -296,29 +330,34 @@ TopoDS_Shape BlockFix_UnionEdges::Perform(const TopoDS_Shape& Shape,
       
     } // end processing each face
     
-  } // end processing each solid
+    // fix changed faces and replace them in the local context
+    for(Standard_Integer i=1; i<=ChangedFaces.Extent(); i++) {
+      TopoDS_Face aFace = TopoDS::Face(aContext->Apply(ChangedFaces.FindKey(i)));
+      Handle(ShapeFix_Face) sff = new ShapeFix_Face(aFace);
+      sff->SetContext(myContext);
+      sff->SetPrecision(myTolerance);
+      sff->SetMinTolerance(myTolerance);
+      sff->SetMaxTolerance(Max(1.,myTolerance*1000.));
+      sff->Perform();
+      aContext->Replace(aFace,sff->Face());
+    }
 
-  for(Standard_Integer i=1; i<=ChangedFaces.Extent(); i++) {
-    TopoDS_Face aFace = TopoDS::Face(myContext->Apply(ChangedFaces.FindKey(i)));
-    Handle(ShapeFix_Face) sff = new ShapeFix_Face(aFace);
-    sff->SetContext(myContext);
-    sff->SetPrecision(myTolerance);
-    sff->SetMinTolerance(myTolerance);
-    sff->SetMaxTolerance(Max(1.,myTolerance*1000.));
-    sff->Perform();
-    //Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire;
-    //sfw->SetContext(myContext);
-    //sfw->SetPrecision(myTolerance);
-    //sfw->SetMinTolerance(myTolerance);
-    //sfw->SetMaxTolerance(Max(1.,myTolerance*1000.));
-    //sfw->SetFace(aFace);
-    //for ( TopoDS_Iterator iter(aFace,Standard_False); iter.More(); iter.Next()) { 
-    //  TopoDS_Wire wire = TopoDS::Wire ( iter.Value() );
-    //  sfw->Load(wire);
-    //  sfw->FixReorder();
-    //  sfw->FixShifted();
-    //}
-  }
+    if(ChangedFaces.Extent()>0) {
+      // fix changed shell and replace it in the local context
+      TopoDS_Shape aRes1 = aContext->Apply(aRes);
+      TopExp_Explorer expsh;
+      for(expsh.Init(aRes1, TopAbs_SHELL); expsh.More(); expsh.Next()) {
+        TopoDS_Shell aShell = TopoDS::Shell(expsh.Current());
+        Handle(ShapeFix_Shell) sfsh = new ShapeFix_Shell;
+        sfsh->FixFaceOrientation(aShell);
+        aContext->Replace(aShell,sfsh->Shell());
+      }
+      TopoDS_Shape aRes2 = aContext->Apply(aRes1);
+      // put new solid into global context
+      myContext->Replace(aSolid,aRes2);
+    }
+
+  } // end processing each solid
 
   aResult = myContext->Apply(Shape);
   return aResult;