Salome HOME
Preparation of intermediate revision
[modules/geom.git] / src / GEOMAlgo / BlockFix_UnionEdges.cxx
index c3b7baf15fd396b330b0124c0d226310d5b421d3..6fe2f8fe8d5e7851e2c707c48a3b399e5c018dba 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>
@@ -37,6 +57,7 @@
 #include <TopoDS_Vertex.hxx>
 #include <TopoDS_Iterator.hxx>
 
+#include "utilities.h"
 
 //=======================================================================
 //function : BlockFix_UnionEdges()
@@ -90,7 +111,7 @@ 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
@@ -148,7 +169,7 @@ static Standard_Boolean MergeEdges(const TopTools_SequenceOfShape& SeqEdges,
       Handle(Geom_Circle) C2 = Handle(Geom_Circle)::DownCast(c3d2);
       gp_Pnt P01 = C1->Location();
       gp_Pnt P02 = C2->Location();
-      if(P01.Distance(P02)>Precision::Confusion()) continue;
+      if (P01.Distance(P02) > Precision::Confusion()) continue;
       // can union circles => create new edge
       TopoDS_Vertex V1 = sae.FirstVertex(edge1);
       gp_Pnt PV1 = BRep_Tool::Pnt(V1);
@@ -156,19 +177,46 @@ static Standard_Boolean MergeEdges(const TopTools_SequenceOfShape& SeqEdges,
       gp_Pnt PV2 = BRep_Tool::Pnt(V2);
       TopoDS_Vertex VM = sae.LastVertex(edge1);
       gp_Pnt PVM = BRep_Tool::Pnt(VM);
-      GC_MakeCircle MC(PV1,PVM,PV2);
+      GC_MakeCircle MC (PV1,PVM,PV2);
       Handle(Geom_Circle) C = MC.Value();
-      gp_Pnt P0 = C->Location();
-      gp_Dir D1(gp_Vec(P0,PV1));
-      gp_Dir D2(gp_Vec(P0,PV2));
-      Standard_Real fpar = C->XAxis().Direction().Angle(D1);
-      Standard_Real lpar = C->XAxis().Direction().Angle(D2);
-      Handle(Geom_TrimmedCurve) tc = new Geom_TrimmedCurve(C,fpar,lpar);
       TopoDS_Edge E;
-      B.MakeEdge (E,tc,Precision::Confusion());
-      B.Add (E,V1);  B.Add (E,V2);
-      B.UpdateVertex(V1, fpar, E, 0.);
-      B.UpdateVertex(V2, lpar, E, 0.);
+      if (!MC.IsDone() || C.IsNull()) {
+        // jfa for Mantis issue 0020228
+        if (PV1.Distance(PV2) > Precision::Confusion()) continue;
+        // closed chain
+        C = C1;
+        B.MakeEdge (E,C,Precision::Confusion());
+        B.Add(E,V1);
+        B.Add(E,V2);
+      }
+      else {
+        gp_Pnt P0 = C->Location();
+        gp_Dir D1(gp_Vec(P0,PV1));
+        gp_Dir D2(gp_Vec(P0,PV2));
+        Standard_Real fpar = C->XAxis().Direction().Angle(D1);
+        if(fabs(fpar)>Precision::Confusion()) {
+          // check orientation
+          gp_Dir ND =  C->XAxis().Direction().Crossed(D1);
+          if(ND.IsOpposite(C->Axis().Direction(),Precision::Confusion())) {
+            fpar = -fpar;
+          }
+        }
+        Standard_Real lpar = C->XAxis().Direction().Angle(D2);
+        if(fabs(lpar)>Precision::Confusion()) {
+          // check orientation
+          gp_Dir ND =  C->XAxis().Direction().Crossed(D2);
+          if(ND.IsOpposite(C->Axis().Direction(),Precision::Confusion())) {
+            lpar = -lpar;
+          }
+        }
+        if(lpar<fpar) lpar += 2*PI;
+        Handle(Geom_TrimmedCurve) tc = new Geom_TrimmedCurve(C,fpar,lpar);
+        B.MakeEdge (E,tc,Precision::Confusion());
+        B.Add(E,V1);
+        B.Add(E,V2);
+        B.UpdateVertex(V1, fpar, E, 0.);
+        B.UpdateVertex(V2, lpar, E, 0.);
+      }
       ShapeFix_Edge sfe;
       sfe.FixAddPCurve(E,aFace,Standard_False);
       sfe.FixSameParameter(E);
@@ -177,27 +225,50 @@ static Standard_Boolean MergeEdges(const TopTools_SequenceOfShape& SeqEdges,
       j--;
     }
   }
-  if(j<aChain.Length()) {
-    cout<<"null curve3d in edge..."<<endl;
+  if (j < aChain.Length()) {
+    MESSAGE ("null curve3d in edge...");
     return Standard_False;
   }
-  if(aChain.Length()>1) {
+  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);
-    aChain.SetValue(1,E);
   }
 
   anEdge = TopoDS::Edge(aChain.Value(1));
@@ -207,7 +278,7 @@ static Standard_Boolean MergeEdges(const TopTools_SequenceOfShape& SeqEdges,
 
 //=======================================================================
 //function : Perform
-//purpose  : 
+//purpose  :
 //=======================================================================
 
 TopoDS_Shape BlockFix_UnionEdges::Perform(const TopoDS_Shape& Shape,
@@ -216,18 +287,18 @@ TopoDS_Shape BlockFix_UnionEdges::Perform(const TopoDS_Shape& Shape,
   myContext = new ShapeBuild_ReShape;
   myTolerance = Tol;
   TopoDS_Shape aResult = myContext->Apply(Shape);
-  
+
   // 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);
@@ -258,7 +329,7 @@ TopoDS_Shape BlockFix_UnionEdges::Perform(const TopoDS_Shape& Shape,
           }
         }
       }
-      
+
       for(Standard_Integer i=1; i<=aMapFacesEdges.Extent(); i++) {
         const TopTools_ListOfShape& ListEdges = aMapFacesEdges.FindFromIndex(i);
         TopTools_SequenceOfShape SeqEdges;
@@ -283,9 +354,9 @@ TopoDS_Shape BlockFix_UnionEdges::Perform(const TopoDS_Shape& Shape,
             ChangedFaces.Add(tmpF);
         }
       }
-      
+
     } // end processing each face
-    
+
     // 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)));