0031303: Different calculation of offset direction in Adaptor2d_OffsetCurve and Geom2...
[occt.git] / src / BRepFill / BRepFill_OffsetWire.cxx
index 465f214..7e48817 100644 (file)
@@ -17,7 +17,7 @@
 //  Modified by skv - Fri Jul  8 11:21:38 2005 OCC9145
 
 #include <Adaptor3d_Curve.hxx>
-#include <Adaptor3d_OffsetCurve.hxx>
+#include <Adaptor2d_OffsetCurve.hxx>
 #include <Bisector_Bisec.hxx>
 #include <BRep_Builder.hxx>
 #include <BRep_CurveRepresentation.hxx>
@@ -222,6 +222,8 @@ static void MakeOffset
  const GeomAbs_JoinType                      theJoinType,
   const TopoDS_Vertex *                       Ends);
 
+Standard_Boolean CheckSmallParamOnEdge(const TopoDS_Edge& anEdge);
+
 //=======================================================================
 //function : KPartCircle
 //purpose  : 
@@ -236,19 +238,27 @@ static Standard_Boolean KPartCircle
   BRepFill_IndexedDataMapOfOrientedShapeListOfShape& myMap,
   Standard_Boolean&    myIsDone)
 {
-  TopExp_Explorer exp(mySpine,TopAbs_EDGE);
-  Standard_Integer NbEdges = 0;
-  TopoDS_Edge      E;
-
-  for (; exp.More(); exp.Next()) {
-    NbEdges++;
-    E = TopoDS::Edge(exp.Current());
-    if (NbEdges > 1) return Standard_False;
+  TopoDS_Edge E;
+  for (TopExp_Explorer anEdgeIter (mySpine, TopAbs_EDGE); anEdgeIter.More(); anEdgeIter.Next())
+  {
+    if (!E.IsNull())
+    {
+      return Standard_False;
+    }
+    E = TopoDS::Edge (anEdgeIter.Current());
+  }
+  if (E.IsNull())
+  {
+    return Standard_False;
   }
 
   Standard_Real      f,l;
   TopLoc_Location    L;
   Handle(Geom_Curve) C =  BRep_Tool::Curve(E,L,f,l);
+  if (C.IsNull())
+  {
+    return Standard_False;
+  }
 
   if (C->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
     Handle(Geom_TrimmedCurve) Ct = Handle(Geom_TrimmedCurve)::DownCast(C);
@@ -265,9 +275,9 @@ static Standard_Boolean KPartCircle
     Handle(Geom2d_Curve) OC;
     if (AHC->GetType() == GeomAbs_Line)
     {
-      if (E.Orientation() == TopAbs_REVERSED)
+      if (E.Orientation() == TopAbs_FORWARD)
         anOffset *= -1;
-      Adaptor3d_OffsetCurve Off(AHC,anOffset);
+      Adaptor2d_OffsetCurve Off(AHC, anOffset);
       OC = new Geom2d_Line(Off.Line());
     }
     else if (AHC->GetType() == GeomAbs_Circle)
@@ -288,7 +298,7 @@ static Standard_Boolean KPartCircle
       if (E.Orientation() == TopAbs_FORWARD)
         anOffset *= -1;
       Handle(Geom2d_TrimmedCurve) G2dT = new Geom2d_TrimmedCurve(aPCurve, f, l);
-      OC = new Geom2d_OffsetCurve( G2dT, anOffset);
+      OC = new Geom2d_OffsetCurve(G2dT, anOffset);
     }
     Handle(Geom_Surface) aSurf = BRep_Tool::Surface(mySpine);
     Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(aSurf);
@@ -297,7 +307,7 @@ static Standard_Boolean KPartCircle
     
     myShape.Orientation(E.Orientation());
     myShape.Location(L);
-    if (Alt != 0.) {
+    if (fabs(Alt) > gp::Resolution()) {
       BRepAdaptor_Surface S(mySpine,0);
       gp_Ax1 Nor = S.Plane().Axis();
       gp_Trsf T;
@@ -450,13 +460,7 @@ const TopTools_ListOfShape& BRepFill_OffsetWire::GeneratedShapes
           }
           if ( !it.Value().IsSame(it.Key())) {
             myMap.ChangeFromKey(it.Value()).Append(myMap.ChangeFromKey(it.Key()));
-            //myMap.UnBind(it.Key());
-            TopoDS_Shape LastShape = myMap.FindKey(myMap.Extent());
-            TopTools_ListOfShape LastList;
-            LastList.Append(myMap(myMap.Extent()));
-            myMap.RemoveLast();
-            if (myMap.FindIndex(it.Key()) != 0)
-              myMap.Substitute(myMap.FindIndex(it.Key()), LastShape, LastList);
+            myMap.RemoveKey(it.Key());
           }
         }
         if (myMap.Contains(it.Key().Reversed())) {
@@ -466,13 +470,7 @@ const TopTools_ListOfShape& BRepFill_OffsetWire::GeneratedShapes
           }
           if ( !it.Value().IsSame(it.Key())) {
             myMap.ChangeFromKey(it.Value().Reversed()).Append(myMap.ChangeFromKey(it.Key().Reversed()));
-            //myMap.UnBind(it.Key().Reversed());
-            TopoDS_Shape LastShape = myMap.FindKey(myMap.Extent());
-            TopTools_ListOfShape LastList;
-            LastList.Append(myMap(myMap.Extent()));
-            myMap.RemoveLast();
-            if (myMap.FindIndex(it.Key().Reversed()) != 0)
-              myMap.Substitute(myMap.FindIndex(it.Key().Reversed()), LastShape, LastList);
+            myMap.RemoveKey(it.Key().Reversed());
           }
         }
       }
@@ -523,7 +521,7 @@ void BRepFill_OffsetWire::Perform (const Standard_Real Offset,
     if(!BadEdges.IsEmpty())
     {
       // Modification of myWorkSpine;
-      //cout << "Modification of myWorkSpine : " << BadEdges.Extent() << endl;
+      //std::cout << "Modification of myWorkSpine : " << BadEdges.Extent() << std::endl;
       BRepTools_Substitution aSubst;
       TopTools_ListIteratorOfListOfShape it(BadEdges);
       TopTools_ListOfShape aL;
@@ -550,7 +548,7 @@ void BRepFill_OffsetWire::Perform (const Standard_Real Offset,
         Standard_Integer NPnts = Points.Length();
         if(NPnts > 2)
         {
-          //cout << NPnts << " points " << endl;
+          //std::cout << NPnts << " points " << std::endl;
           TopoDS_Vertex FV = Vf;
           TopoDS_Vertex LV;
           TopoDS_Edge newE;
@@ -568,7 +566,7 @@ void BRepFill_OffsetWire::Perform (const Standard_Real Offset,
         }
         else
         {
-          //cout << " 2 points " << endl;
+          //std::cout << " 2 points " << std::endl;
           TopoDS_Edge newE = BRepLib_MakeEdge(Vf, Vl);
           aL.Append(newE);
         }
@@ -642,15 +640,15 @@ void BRepFill_OffsetWire::Perform (const Standard_Real Offset,
       PerformWithBiLo(myWorkSpine,Offset,myBilo,myLink,myJoinType,Alt);
     }
   }
-  catch (Standard_Failure)//Every exception was caught.
-  {
-    myShape.Nullify();
-    myIsDone = Standard_False;
+  catch (Standard_Failure const& anException) {
 #ifdef OCCT_DEBUG
-    cout<<"An exception was caught in BRepFill_OffsetWire::Perform : ";
-    Standard_Failure::Caught()->Print(cout);
-    cout<<endl;
+    std::cout<<"An exception was caught in BRepFill_OffsetWire::Perform : ";
+    anException.Print(std::cout);
+    std::cout<<std::endl;
 #endif
+    (void)anException;
+    myShape.Nullify();
+    myIsDone = Standard_False;
 
     return;
   }
@@ -667,7 +665,7 @@ void BRepFill_OffsetWire::Perform (const Standard_Real Offset,
       if (!aWire.Closed()) {
         myShape.Nullify();
         myIsDone = Standard_False;
-        Standard_ConstructionError::Raise("Offset wire is not closed.");
+        throw Standard_ConstructionError("Offset wire is not closed.");
       }
     }
   }
@@ -723,8 +721,8 @@ void BRepFill_OffsetWire::PerformWithBiLo
   const Standard_Real             Alt)
 {
   myIsDone     = Standard_False;
-  TopoDS_Shape aLocalShape = Spine.Oriented(TopAbs_FORWARD);
-  myWorkSpine  = TopoDS::Face(aLocalShape);
+  TopoDS_Shape aLocalShapeWork = Spine.Oriented(TopAbs_FORWARD);
+  myWorkSpine  = TopoDS::Face(aLocalShapeWork);
   //  myWorkSpine  = TopoDS::Face(Spine.Oriented(TopAbs_FORWARD));
   myJoinType   = Join;
   myOffset     = Offset ;
@@ -751,7 +749,6 @@ void BRepFill_OffsetWire::PerformWithBiLo
     return;
 
   BRep_Builder myBuilder;
-  myBuilder.MakeCompound(TopoDS::Compound(myShape));
 
   //---------------------------------------------------------------------
   // MapNodeVertex : associate to each node of the map (key1) and to
@@ -773,12 +770,14 @@ void BRepFill_OffsetWire::PerformWithBiLo
   TopTools_ListOfShape                      EmptyList;
   TColStd_SequenceOfReal                    EmptySeqOfReal;
 
-  Standard_Real ALT = Alt;
   Handle(Geom_Plane) RefPlane = 
     Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(myWorkSpine));
-  if ( myWorkSpine.Orientation() == TopAbs_REVERSED) ALT = -Alt;
-  RefPlane = Handle(Geom_Plane)::DownCast
-    (RefPlane->Translated( ALT * gp_Vec(RefPlane->Axis().Direction() )));
+  if (fabs(Alt) > gp::Resolution()) {
+    Standard_Real anAlt = Alt;
+    if ( myWorkSpine.Orientation() == TopAbs_REVERSED) anAlt = -Alt;
+    RefPlane = Handle(Geom_Plane)::DownCast
+      (RefPlane->Translated( anAlt * gp_Vec(RefPlane->Axis().Direction() )));
+  }
 
   //---------------------------------------------------------------
   // Construction of Circles and OffsetCurves
@@ -793,6 +792,11 @@ void BRepFill_OffsetWire::PerformWithBiLo
     TopExp::Vertices(theWire, Ends[0], Ends[1]);
   }
 
+  if (Locus.NumberOfContours() == 0)
+  {
+    return;
+  }
+
   for (Standard_Integer ic = 1; ic <= Locus.NumberOfContours(); ic++) {
     TopoDS_Shape PEE = Link.GeneratingShape(Locus.BasicElt(ic,Locus.NumberOfElts(ic)));
     TopoDS_Shape& PE = PEE ;      
@@ -811,11 +815,23 @@ void BRepFill_OffsetWire::PerformWithBiLo
     }
   }
 
+  //Remove possible hanging arcs on vertices
+  if (myIsOpenResult && myJoinType == GeomAbs_Arc)
+  {
+    if (!myMap.IsEmpty() &&
+        myMap.FindKey(1).ShapeType() == TopAbs_VERTEX)
+    {
+      myMap.RemoveFromIndex(1);
+    }
+    if (!myMap.IsEmpty() &&
+        myMap.FindKey(myMap.Extent()).ShapeType() == TopAbs_VERTEX)
+      myMap.RemoveLast();
+  }
 
 #ifdef OCCT_DEBUG
 #ifdef DRAW
   if (AffichEdge) {
-    cout << " End Construction of geometric primitives "<<endl;
+    std::cout << " End Construction of geometric primitives "<<std::endl;
   }
 #endif
 #endif
@@ -918,7 +934,8 @@ void BRepFill_OffsetWire::PerformWithBiLo
     // Construction of vertices on edges parallel to the spine.
     //-----------------------------------------------------------
 
-    Trim.IntersectWith(E [0], E [1], myJoinType, Params);
+    Trim.IntersectWith(E[0], E[1], S[0], S[1], Ends[0], Ends[1],
+                       myJoinType, myIsOpenResult, Params);
 
     for (Standard_Integer s = 1; s <= Params.Length(); s++) {
       TopoDS_Vertex VC;
@@ -1002,7 +1019,7 @@ void BRepFill_OffsetWire::PerformWithBiLo
 #ifdef OCCT_DEBUG
 #ifdef DRAW
   if (AffichEdge) {
-    cout << " End Construction of vertices on offsets"<<endl;
+    std::cout << " End Construction of vertices on offsets"<<std::endl;
   }
 #endif
 #endif
@@ -1022,7 +1039,7 @@ void BRepFill_OffsetWire::PerformWithBiLo
 
     myMap(j).Clear();
     if (MapBis.IsBound(CurrentEdge)) {
-      TopTools_SequenceOfShape S;
+      TopTools_SequenceOfShape aSeqOfShape;
       if (!MapBis(CurrentEdge).IsEmpty()) {
         Standard_Integer IndOfE = 0;
         if (myIsOpenResult)
@@ -1038,9 +1055,9 @@ void BRepFill_OffsetWire::PerformWithBiLo
           Detromp  (CurrentSpine),
           MapBis   (CurrentEdge) ,  
           MapVerPar(CurrentEdge) ,
-          S, MapVV, IndOfE);
-        for ( k = 1; k <= S.Length(); k++) {
-          myMap(j).Append(S.Value(k));
+          aSeqOfShape, MapVV, IndOfE);
+        for ( k = 1; k <= aSeqOfShape.Length(); k++) {
+          myMap(j).Append(aSeqOfShape.Value(k));
         }
       }
       else {
@@ -1103,15 +1120,15 @@ void BRepFill_OffsetWire::PerformWithBiLo
   TopExp_Explorer Explo( myShape, TopAbs_EDGE );
   for (; Explo.More(); Explo.Next())
   {
-    TopoDS_Edge E = TopoDS::Edge( Explo.Current() );
+    TopoDS_Edge anEdge = TopoDS::Edge( Explo.Current() );
     TopoDS_Vertex V1, V2;
-    TopExp::Vertices( E, V1, V2 );
+    TopExp::Vertices(anEdge, V1, V2 );
     Handle(BRep_TVertex)& TV1 = *((Handle(BRep_TVertex)*) &(V1).TShape());
     Handle(BRep_TVertex)& TV2 = *((Handle(BRep_TVertex)*) &(V2).TShape());
 
     TopLoc_Location loc;
     Standard_Real f, l;
-    Handle( Geom_Curve ) theCurve = BRep_Tool::Curve( E, loc, f, l );
+    Handle( Geom_Curve ) theCurve = BRep_Tool::Curve(anEdge, loc, f, l );
     theCurve = Handle( Geom_Curve )::DownCast( theCurve->Copy() );
     theCurve->Transform( loc.Transformation() );
     gp_Pnt f3d = theCurve->Value( f );
@@ -1271,69 +1288,84 @@ void BRepFill_OffsetWire::UpdateDetromp (BRepFill_DataMapOfOrientedShapeListOfSh
                                          const BRepFill_TrimEdgeTool&    Trim) const
 {
   Standard_Integer ii = 1;
-  Standard_Real    U1,U2;
-  TopoDS_Vertex    V1,V2;
 
-  Handle(Geom2d_Curve) Bis = Bisec.Value();
+  if (myJoinType == GeomAbs_Intersection)
+  {
+    for (; ii <= Vertices.Length(); ii++)
+    {
+      const TopoDS_Vertex& aVertex = TopoDS::Vertex(Vertices.Value(ii));
+      Detromp(Shape1).Append(aVertex);
+      Detromp(Shape2).Append(aVertex);
+    }
+  }
+  else //myJoinType == GeomAbs_Arc
+  {
+    Standard_Real    U1,U2;
+    TopoDS_Vertex    V1,V2;
+    
+    const Handle(Geom2d_Curve)& Bis = Bisec.Value();
+    Standard_Boolean ForceAdd = Standard_False;
+    Handle(Geom2d_TrimmedCurve) aTC = Handle(Geom2d_TrimmedCurve)::DownCast(Bis);
+    if(!aTC.IsNull() && aTC->BasisCurve()->IsPeriodic())
+    {
+      gp_Pnt2d Pf = Bis->Value(Bis->FirstParameter());
+      gp_Pnt2d Pl = Bis->Value(Bis->LastParameter());
+      ForceAdd = Pf.Distance(Pl) <= Precision::Confusion();
+    }
 
-  U1 = Bis->FirstParameter();
-  
-  if (SOnE) { 
-    // the first point of the bissectrice is on the offset
-    V1 = TopoDS::Vertex(Vertices.Value(ii));
-    ii++; 
-  }
-
-  while (ii <= Vertices.Length()) {
-    U2 = Params.Value(ii).X();
-    V2 = TopoDS::Vertex(Vertices.Value(ii));
-
-    gp_Pnt2d P = Bis->Value((U2 + U1)*0.5);
-    Standard_Boolean IsP_inside = Standard_True;
-    if ((myJoinType != GeomAbs_Intersection) || EOnE)
-      IsP_inside = Trim.IsInside(P);
-    if (!IsP_inside) {
-      if (!V1.IsNull()) {
-        Detromp(Shape1).Append(V1);
-        Detromp(Shape2).Append(V1);
-      }
-      Detromp(Shape1).Append(V2);
-      Detromp(Shape2).Append(V2);
-    }
-    U1 = U2;
-    V1 = V2;
-    ii ++;
-  }
-
-  // test medium point between the last parameter and the end of the bissectrice.
-  U2 = Bis->LastParameter();
-  if (!EOnE) {
-    if (!Precision::IsInfinite(U2)) {
-      gp_Pnt2d P = Bis->Value((U2 + U1)*0.5);  
-      Standard_Boolean IsP_inside = Standard_False;
-      if (myJoinType == GeomAbs_Arc)
-        IsP_inside = Trim.IsInside(P);
-      if (!IsP_inside) {
+    U1 = Bis->FirstParameter();
+    
+    if (SOnE) { 
+      // the first point of the bissectrice is on the offset
+      V1 = TopoDS::Vertex(Vertices.Value(ii));
+      ii++; 
+    }
+    
+    while (ii <= Vertices.Length()) {
+      U2 = Params.Value(ii).X();
+      V2 = TopoDS::Vertex(Vertices.Value(ii));
+      
+      gp_Pnt2d P = Bis->Value((U2 + U1)*0.5);
+      if (!Trim.IsInside(P) || ForceAdd) {
         if (!V1.IsNull()) {
           Detromp(Shape1).Append(V1);
           Detromp(Shape2).Append(V1);
         }
+        Detromp(Shape1).Append(V2);
+        Detromp(Shape2).Append(V2);
       }
+      U1 = U2;
+      V1 = V2;
+      ii ++;
     }
-    else {
-      if (!V1.IsNull()) {
-        Detromp(Shape1).Append(V1);
-        Detromp(Shape2).Append(V1);
+    
+    // test medium point between the last parameter and the end of the bissectrice.
+    U2 = Bis->LastParameter();
+    if (!EOnE) {
+      if (!Precision::IsInfinite(U2)) {
+        gp_Pnt2d P = Bis->Value((U2 + U1)*0.5);
+        if (!Trim.IsInside(P) || ForceAdd) {
+          if (!V1.IsNull()) {
+            Detromp(Shape1).Append(V1);
+            Detromp(Shape2).Append(V1);
+          }
+        }
       }
-    }
-  }    
-  //else if(myJoinType != GeomAbs_Arc)
-  //{
-  //  if (!V1.IsNull()) {
-  //    Detromp(Shape1).Append(V1);
-  //    Detromp(Shape2).Append(V1);
-  //  }
-  //}
+      else {
+        if (!V1.IsNull()) {
+          Detromp(Shape1).Append(V1);
+          Detromp(Shape2).Append(V1);
+        }
+      }
+    }    
+    //else if(myJoinType != GeomAbs_Arc)
+    //{
+    //  if (!V1.IsNull()) {
+    //    Detromp(Shape1).Append(V1);
+    //    Detromp(Shape2).Append(V1);
+    //  }
+    //}
+  } //end of else (myJoinType==GeomAbs_Arc)
 }
 
 //=======================================================================
@@ -1361,6 +1393,8 @@ void BRepFill_OffsetWire::MakeWires()
       TopExp::Vertices (E,V1,V2);
       if (V1.IsSame(V2) && IsSmallClosedEdge(E, V1))
         continue; //remove small closed edges
+      if (!CheckSmallParamOnEdge(E))
+        continue;
       if (!MVE.Contains(V1)) {
         TopTools_ListOfShape empty;
         MVE.Add(V1,empty);
@@ -1407,13 +1441,7 @@ void BRepFill_OffsetWire::MakeWires()
 
     if (myIsOpenResult && MVE.FindFromKey(CV).IsEmpty())
     {
-      //MVE.UnBind(CV);
-      TopoDS_Shape LastShape = MVE.FindKey(MVE.Extent());
-      TopTools_ListOfShape LastList;
-      LastList.Append(MVE(MVE.Extent()));
-      MVE.RemoveLast();
-      if (MVE.FindIndex(CV) != 0)
-        MVE.Substitute(MVE.FindIndex(CV), LastShape, LastList);
+      MVE.RemoveKey(CV);
     }
       
 
@@ -1436,18 +1464,12 @@ void BRepFill_OffsetWire::MakeWires()
         isClosed = VF.IsSame(CV);
         //  Modified by Sergey KHROMOV - Thu Mar 14 11:30:15 2002 End
         End = Standard_True;
-        //MVE.UnBind(VF);
-        TopoDS_Shape LastShape = MVE.FindKey(MVE.Extent());
-        TopTools_ListOfShape LastList;
-        LastList.Append(MVE(MVE.Extent()));
-        MVE.RemoveLast();
-        if (MVE.FindIndex(VF) != 0)
-          MVE.Substitute(MVE.FindIndex(VF), LastShape, LastList);
+        MVE.RemoveKey(VF);
       }
 
       if (!End) {
         if (MVE.FindFromKey(CV).Extent() > 2) {
-          //cout <<"vertex on more that 2 edges in a face."<<endl;
+          //std::cout <<"vertex on more that 2 edges in a face."<<std::endl;
         }
         for ( itl.Initialize(MVE.FindFromKey(CV)); itl.More(); itl.Next()) {
           if (itl.Value().IsSame(CE)) {
@@ -1464,13 +1486,7 @@ void BRepFill_OffsetWire::MakeWires()
         
         if (MVE.FindFromKey(CV).IsEmpty())
         {
-          //MVE.UnBind(CV);
-          TopoDS_Shape LastShape = MVE.FindKey(MVE.Extent());
-          TopTools_ListOfShape LastList;
-          LastList.Append(MVE(MVE.Extent()));
-          MVE.RemoveLast();
-          if (MVE.FindIndex(CV) != 0)
-            MVE.Substitute(MVE.FindIndex(CV), LastShape, LastList);
+          MVE.RemoveKey(CV);
         }
       }
     }
@@ -1509,7 +1525,6 @@ void BRepFill_OffsetWire::FixHoles()
   TopTools_SequenceOfShape ClosedWires, UnclosedWires, IsolatedWires;
 
   Standard_Real MaxTol = 0.;
-  Standard_Integer i;
   BRep_Builder BB;
 
   TopExp_Explorer Explo( mySpine, TopAbs_VERTEX );
@@ -1563,16 +1578,13 @@ void BRepFill_OffsetWire::FixHoles()
     TopExp::Vertices( Base, Vf, Vl );
     if(Vf.IsNull() || Vl.IsNull())
     {
-      Standard_Failure::Raise("BRepFill_OffsetWire::FixHoles(): Wrong wire.");
-#ifdef OCCT_DEBUG
-      BRepTools::Write(Base, "Base");
-#endif
+      throw Standard_Failure("BRepFill_OffsetWire::FixHoles(): Wrong wire.");
     }
     gp_Pnt Pf, Pl;
     Pf = BRep_Tool::Pnt(Vf);
     Pl = BRep_Tool::Pnt(Vl);
     Standard_Real DistF = RealLast(), DistL = RealLast();
-    Standard_Integer IndexF = 1, IndexL = 1;
+    Standard_Integer IndexF = 0, IndexL = 0;
     Standard_Boolean IsFirstF = Standard_False, IsFirstL = Standard_False;
     for (Standard_Integer i = 2; i <= UnclosedWires.Length(); i++)
     {
@@ -1582,13 +1594,7 @@ void BRepFill_OffsetWire::FixHoles()
 
       if(V1.IsNull() || V2.IsNull())
       {
-        Standard_Failure::Raise("BRepFill_OffsetWire::FixHoles(): Wrong wire.");
-#ifdef OCCT_DEBUG
-        BRepTools::Write(Base, "Base");
-        char name[128];
-        sprintf(name,"Wire_%d",i);
-        BRepTools::Write(aWire, name);
-#endif
+        throw Standard_Failure("BRepFill_OffsetWire::FixHoles(): Wrong wire.");
       }
 
       gp_Pnt P1, P2;
@@ -1623,6 +1629,10 @@ void BRepFill_OffsetWire::FixHoles()
         IsFirstL = Standard_False;
       }
     }
+    if (DistF > MaxTol)
+      IndexF = 0;
+    if (DistL > MaxTol)
+      IndexL = 0;
     TopoDS_Wire theWire;
     TopoDS_Edge theEdge;
     TopoDS_Vertex theVertex;
@@ -1781,6 +1791,7 @@ void BRepFill_OffsetWire::FixHoles()
   {
     TopoDS_Compound R;
     BB.MakeCompound( R );
+    Standard_Integer i;
     for (i = 1; i <= ClosedWires.Length(); i++)
       BB.Add( R, ClosedWires(i) );
     for (i = 1; i <= IsolatedWires.Length(); i++)
@@ -2021,7 +2032,7 @@ void MakeOffset (const TopoDS_Edge&        E,
   Standard_Real f,l;
   Standard_Real anOffset = Offset;
 
-  if (E.Orientation() == TopAbs_REVERSED) anOffset *= -1;
+  if (E.Orientation() == TopAbs_FORWARD) anOffset *= -1;
 
   Handle(Geom2d_Curve) G2d = BRep_Tool::CurveOnSurface(E,F,f,l);
   Handle(Geom2d_Curve) G2dOC;
@@ -2049,14 +2060,14 @@ void MakeOffset (const TopoDS_Edge&        E,
     gp_Dir2d Xd = axes.XDirection();
     gp_Dir2d Yd = axes.YDirection();
     Standard_Real Crossed = Xd.X()*Yd.Y()-Xd.Y()*Yd.X();
-    Standard_Real Signe = ( Crossed > 0.) ? 1. : -1.;
+    Standard_Real Signe = ( Crossed > 0.) ? -1. : 1.;
 
     if (anOffset*Signe < AC.Circle().Radius() - Precision::Confusion()) {
 
       Handle(Geom2dAdaptor_HCurve) AHC = 
         new Geom2dAdaptor_HCurve(G2d);
-      Adaptor3d_OffsetCurve   Off(AHC,-anOffset);
-      Handle(Geom2d_Circle) CC = new Geom2d_Circle(Off.Circle());      
+      Adaptor2d_OffsetCurve   Off(AHC, anOffset);
+      Handle(Geom2d_Circle) CC = new Geom2d_Circle(Off.Circle());
 
       Standard_Real Delta = 2*M_PI - l + f;
       if (theJoinType == GeomAbs_Arc)
@@ -2090,7 +2101,7 @@ void MakeOffset (const TopoDS_Edge&        E,
   else if (AC.GetType() == GeomAbs_Line) {
     Handle(Geom2dAdaptor_HCurve) AHC = 
       new Geom2dAdaptor_HCurve(G2d);
-    Adaptor3d_OffsetCurve Off(AHC,anOffset);
+    Adaptor2d_OffsetCurve Off(AHC, anOffset);
     Handle(Geom2d_Line)       CC = new Geom2d_Line(Off.Line());
     Standard_Real Delta = (l - f);
     if (ToExtendFirstPar)
@@ -2111,7 +2122,6 @@ void MakeOffset (const TopoDS_Edge&        E,
   }
   else {
 
-    anOffset = -anOffset;
     Handle(Geom2d_TrimmedCurve) G2dT = new Geom2d_TrimmedCurve(G2d,f,l);
     G2dOC = new Geom2d_OffsetCurve( G2dT, anOffset);
 
@@ -2510,7 +2520,7 @@ static void CheckBadEdges(const TopoDS_Face& Spine, const Standard_Real Offset,
 
 
         if (aMap.Contains(SE)) {
-          //cout << "Edge is treated second time" << endl;
+          //std::cout << "Edge is treated second time" << std::endl;
           continue;
         }
 
@@ -2734,3 +2744,18 @@ static void QuasiFleche(const Adaptor3d_Curve& C,
   }
 }
 
+Standard_Boolean CheckSmallParamOnEdge(const TopoDS_Edge& anEdge)
+{  
+  const BRep_ListOfCurveRepresentation& aList = ((Handle(BRep_TEdge)::DownCast(anEdge.TShape()))->Curves());
+  if (!aList.IsEmpty())
+  {
+    Handle( BRep_CurveRepresentation ) CRep = ((Handle(BRep_TEdge)::DownCast(anEdge.TShape()))->Curves()).First();
+    Standard_Real f = (Handle(BRep_GCurve)::DownCast(CRep))->First();
+    Standard_Real l = (Handle(BRep_GCurve)::DownCast(CRep))->Last();
+    if (Abs (l - f) < Precision::PConfusion())
+      return Standard_False;
+  }
+  return Standard_True;
+}
+
+