0027282: [Regression to 6.9.1] smesh/bugs_00/A6: Cut produces an empty shape
[occt.git] / src / IntPatch / IntPatch_Intersection.cxx
index d3da070..719b406 100644 (file)
 // Alternatively, this file may be used under the terms of Open CASCADE
 // commercial license or contractual agreement.
 
-#include <IntPatch_Intersection.ixx>
+#include <IntPatch_Intersection.hxx>
 
+#include <Adaptor3d_HSurface.hxx>
+#include <Adaptor3d_TopolTool.hxx>
+#include <IntPatch_ALine.hxx>
 #include <IntPatch_ALineToWLine.hxx>
 #include <IntPatch_GLine.hxx>
-#include <IntPatch_ALine.hxx>
-#include <IntPatch_WLine.hxx>
-#include <IntPatch_RLine.hxx>
-#include <IntPatch_PrmPrmIntersection.hxx>
-#include <IntPatch_ImpPrmIntersection.hxx>
 #include <IntPatch_ImpImpIntersection.hxx>
+#include <IntPatch_ImpPrmIntersection.hxx>
+#include <IntPatch_Line.hxx>
+#include <IntPatch_Point.hxx>
+#include <IntPatch_PrmPrmIntersection.hxx>
+#include <IntPatch_RLine.hxx>
+#include <IntPatch_WLine.hxx>
+#include <IntPatch_WLineTool.hxx>
 #include <IntSurf_Quadric.hxx>
+#include <Standard_ConstructionError.hxx>
+#include <Standard_DomainError.hxx>
+#include <Standard_OutOfRange.hxx>
+#include <StdFail_NotDone.hxx>
+#include <IntPatch_LineConstructor.hxx>
 
 #include <stdio.h>
-
 #define DEBUG 0 
 static const Standard_Integer aNbPointsInALine = 200;
 
@@ -140,7 +149,7 @@ void IntPatch_Intersection::Perform(const Handle(Adaptor3d_HSurface)&  S1,
     default:
     {
       IntPatch_PrmPrmIntersection interpp;
-      interpp.Perform(S1,D1,TolArc,TolTang,myFleche,myUVMaxStep);
+      interpp.Perform(S1,D1,TolTang,TolArc,myFleche,myUVMaxStep);
       if (interpp.IsDone())
       {
         done = Standard_True;
@@ -174,7 +183,7 @@ void IntPatch_Intersection::Perform(const Handle(Adaptor3d_HSurface)&  S1,
 #include <GeomAdaptor_HCurve.hxx>
 #include <GeomAdaptor_Curve.hxx>
 #include <GeomAdaptor_Surface.hxx>
-#include <Handle_GeomAdaptor_HSurface.hxx>
+#include <GeomAdaptor_HSurface.hxx>
 #include <Geom_Plane.hxx>
 #include <ProjLib_ProjectOnPlane.hxx>
 #include <GeomProjLib.hxx>
@@ -304,7 +313,7 @@ static void FUN_GetUiso(const Handle(Geom_Surface)& GS,
   }
   else//OffsetSurface
   {
-    const Handle(Geom_OffsetSurface) gos = *(Handle_Geom_OffsetSurface*)&GS;
+    const Handle(Geom_OffsetSurface) gos = Handle(Geom_OffsetSurface)::DownCast (GS);
     const Handle(Geom_Surface) bs = gos->BasisSurface();
     Handle(Geom_Curve) gcbs = bs->UIso(U);
     GeomAdaptor_Curve gac(gcbs);
@@ -374,7 +383,7 @@ static void FUN_GetViso(const Handle(Geom_Surface)& GS,
   }
   else//OffsetSurface
   {
-    const Handle(Geom_OffsetSurface) gos = *(Handle_Geom_OffsetSurface*)&GS;
+    const Handle(Geom_OffsetSurface) gos = Handle(Geom_OffsetSurface)::DownCast (GS);
     const Handle(Geom_Surface) bs = gos->BasisSurface();
     Handle(Geom_Curve) gcbs = bs->VIso(V);
     GeomAdaptor_Curve gac(gcbs);
@@ -449,7 +458,7 @@ static void FUN_PL_Intersection(const Handle(Adaptor3d_HSurface)& S1,
   else if(!S1->IsVPeriodic() && !S1->IsVClosed()) {
     if(T1 != GeomAbs_OffsetSurface) C1 = gs1->UIso(MS1[0]);
     else {
-      const Handle(Geom_OffsetSurface) gos = *(Handle_Geom_OffsetSurface*)&gs1;
+      const Handle(Geom_OffsetSurface) gos = Handle(Geom_OffsetSurface)::DownCast (gs1);
       const Handle(Geom_Surface) bs = gos->BasisSurface();
       C1 = bs->UIso(MS1[0]);
     }
@@ -459,7 +468,7 @@ static void FUN_PL_Intersection(const Handle(Adaptor3d_HSurface)& S1,
   if(!S1->IsUPeriodic() && !S1->IsUClosed()) {
     if(T1 != GeomAbs_OffsetSurface) C1 = gs1->VIso(MS1[1]);
     else {
-      const Handle(Geom_OffsetSurface) gos = *(Handle_Geom_OffsetSurface*)&gs1;
+      const Handle(Geom_OffsetSurface) gos = Handle(Geom_OffsetSurface)::DownCast (gs1);
       const Handle(Geom_Surface) bs = gos->BasisSurface();
       C1 = bs->VIso(MS1[1]);
     }
@@ -470,7 +479,7 @@ static void FUN_PL_Intersection(const Handle(Adaptor3d_HSurface)& S1,
   else if(!S2->IsVPeriodic() && !S2->IsVClosed()) {
     if(T2 != GeomAbs_OffsetSurface) C2 = gs2->UIso(MS2[0]);
     else {
-      const Handle(Geom_OffsetSurface) gos = *(Handle_Geom_OffsetSurface*)&gs2;
+      const Handle(Geom_OffsetSurface) gos = Handle(Geom_OffsetSurface)::DownCast (gs2);
       const Handle(Geom_Surface) bs = gos->BasisSurface();
       C2 = bs->UIso(MS2[0]);
     }
@@ -480,7 +489,7 @@ static void FUN_PL_Intersection(const Handle(Adaptor3d_HSurface)& S1,
   if(!S2->IsUPeriodic() && !S2->IsUClosed()) {
     if(T2 != GeomAbs_OffsetSurface) C2 = gs2->VIso(MS2[1]);
     else {
-      const Handle(Geom_OffsetSurface) gos = *(Handle_Geom_OffsetSurface*)&gs2;
+      const Handle(Geom_OffsetSurface) gos = Handle(Geom_OffsetSurface)::DownCast (gs2);
       const Handle(Geom_Surface) bs = gos->BasisSurface();
       C2 = bs->VIso(MS2[1]);
     }
@@ -545,10 +554,8 @@ static void FUN_PL_Intersection(const Handle(Adaptor3d_HSurface)& S1,
   Handle(Geom_Curve) C2Prj =
     GeomProjLib::ProjectOnPlane(C2,GPln,gp_Dir(DV),Standard_True);
   if(C1Prj.IsNull() || C2Prj.IsNull()) return;
-  Handle(Geom2d_Curve) C1Prj2d =
-    GeomProjLib::Curve2d(C1Prj,*(Handle_Geom_Surface *)&GPln);
-  Handle(Geom2d_Curve) C2Prj2d =
-    GeomProjLib::Curve2d(C2Prj,*(Handle_Geom_Surface *)&GPln);
+  Handle(Geom2d_Curve) C1Prj2d = GeomProjLib::Curve2d (C1Prj,GPln);
+  Handle(Geom2d_Curve) C2Prj2d = GeomProjLib::Curve2d (C2Prj,GPln);
   Geom2dAPI_InterCurveCurve ICC(C1Prj2d,C2Prj2d,1.0e-7);
   if(ICC.NbPoints() > 0 )
   {
@@ -650,14 +657,14 @@ static void FUN_TrimBothSurf(const Handle(Adaptor3d_HSurface)& S1,
   if(T1 != GeomAbs_OffsetSurface){ visoS1 = gs1->VIso(VM1); uisoS1 = gs1->UIso(UM1); }
   else
   {
-    const Handle(Geom_OffsetSurface) gos = *(Handle_Geom_OffsetSurface*)&gs1;
+    const Handle(Geom_OffsetSurface) gos = Handle(Geom_OffsetSurface)::DownCast (gs1);
     const Handle(Geom_Surface) bs = gos->BasisSurface();
     visoS1 = bs->VIso(VM1); uisoS1 = bs->UIso(UM1);
   }
   if(T2 != GeomAbs_OffsetSurface){ visoS2 = gs2->VIso(VM2); uisoS2 = gs2->UIso(UM2); }
   else
   {
-    const Handle(Geom_OffsetSurface) gos = *(Handle_Geom_OffsetSurface*)&gs2;
+    const Handle(Geom_OffsetSurface) gos = Handle(Geom_OffsetSurface)::DownCast (gs2);
     const Handle(Geom_Surface) bs = gos->BasisSurface();
     visoS2 = bs->VIso(VM2); uisoS2 = bs->UIso(UM2);
   }
@@ -711,7 +718,9 @@ void IntPatch_Intersection::Perform(const Handle(Adaptor3d_HSurface)&  theS1,
                                     const Handle(Adaptor3d_TopolTool)& theD2,
                                     const Standard_Real TolArc,
                                     const Standard_Real TolTang,
-                                    const Standard_Boolean isGeomInt)
+                                    const Standard_Boolean isGeomInt,
+                                    const Standard_Boolean theIsReqToKeepRLine,
+                                    const Standard_Boolean theIsReqToPostWLProc)
 {
   myTolArc = TolArc;
   myTolTang = TolTang;
@@ -908,11 +917,23 @@ void IntPatch_Intersection::Perform(const Handle(Adaptor3d_HSurface)&  theS1,
     ListOfPnts.Clear();
     if(isGeomInt)
     {
-      GeomGeomPerfom(theS1, theD1, theS2, theD2, TolArc, TolTang, ListOfPnts, RestrictLine, typs1, typs2);
+      if(theD1->DomainIsInfinite() || theD2->DomainIsInfinite())
+      {
+        GeomGeomPerfom( theS1, theD1, theS2, theD2, TolArc, 
+                        TolTang, ListOfPnts, RestrictLine,
+                        typs1, typs2, theIsReqToKeepRLine);
+      }
+      else
+      {
+        GeomGeomPerfomTrimSurf( theS1, theD1, theS2, theD2,
+                                TolArc, TolTang, ListOfPnts, RestrictLine,
+                                typs1, typs2, theIsReqToKeepRLine);
+      }
     }
     else
     {
-      ParamParamPerfom(theS1, theD1, theS2, theD2, TolArc, TolTang, ListOfPnts, RestrictLine, typs1, typs2);
+      ParamParamPerfom(theS1, theD1, theS2, theD2, 
+              TolArc, TolTang, ListOfPnts, RestrictLine, typs1, typs2);
     }
   }
 
@@ -929,7 +950,27 @@ void IntPatch_Intersection::Perform(const Handle(Adaptor3d_HSurface)&  theS1,
     IntSurf_ListOfPntOn2S ListOfPnts;
     ListOfPnts.Clear();
 
-    ParamParamPerfom(theS1, theD1, theS2, theD2, TolArc, TolTang, ListOfPnts, RestrictLine, typs1, typs2);
+    ParamParamPerfom(theS1, theD1, theS2, theD2, TolArc,
+                        TolTang, ListOfPnts, RestrictLine, typs1, typs2);
+  }
+
+  if(!theIsReqToPostWLProc)
+    return;
+
+  for(Standard_Integer i = slin.Lower(); i <= slin.Upper(); i++)
+  {
+    Handle(IntPatch_WLine) aWL = Handle(IntPatch_WLine)::DownCast(slin.Value(i));
+
+    if(aWL.IsNull())
+      continue;
+
+    Handle(IntPatch_WLine) aRW = IntPatch_WLineTool::ComputePurgedWLine(aWL, theS1, theS2, theD1, theD2);
+
+    if(aRW.IsNull())
+      continue;
+
+    slin.InsertAfter(i, aRW);
+    slin.Remove(i);
   }
 }
 
@@ -945,7 +986,9 @@ void IntPatch_Intersection::Perform(const Handle(Adaptor3d_HSurface)&  theS1,
                                     const Standard_Real TolTang,
                                     IntSurf_ListOfPntOn2S& ListOfPnts,
                                     const Standard_Boolean RestrictLine,
-                                    const Standard_Boolean isGeomInt)
+                                    const Standard_Boolean isGeomInt,
+                                    const Standard_Boolean theIsReqToKeepRLine,
+                                    const Standard_Boolean theIsReqToPostWLProc)
 {
   myTolArc = TolArc;
   myTolTang = TolTang;
@@ -1125,7 +1168,8 @@ void IntPatch_Intersection::Perform(const Handle(Adaptor3d_HSurface)&  theS1,
 
   if(!isGeomInt)
   {
-    ParamParamPerfom(theS1, theD1, theS2, theD2, TolArc, TolTang, ListOfPnts, RestrictLine, typs1, typs2);
+    ParamParamPerfom(theS1, theD1, theS2, theD2, 
+                TolArc, TolTang, ListOfPnts, RestrictLine, typs1, typs2);
   }
   else if(ts1 != ts2)
   {
@@ -1133,11 +1177,43 @@ void IntPatch_Intersection::Perform(const Handle(Adaptor3d_HSurface)&  theS1,
   }
   else if (ts1 == 0)
   {
-    ParamParamPerfom(theS1, theD1, theS2, theD2, TolArc, TolTang, ListOfPnts, RestrictLine, typs1, typs2);
+    ParamParamPerfom(theS1, theD1, theS2, theD2,
+                TolArc, TolTang, ListOfPnts, RestrictLine, typs1, typs2);
   }
   else if(ts1 == 1)
   {
-    GeomGeomPerfom(theS1, theD1, theS2, theD2, TolArc, TolTang, ListOfPnts, RestrictLine, typs1, typs2);
+    if(theD1->DomainIsInfinite() || theD2->DomainIsInfinite())
+    {
+      GeomGeomPerfom(theS1, theD1, theS2, theD2, TolArc, 
+                      TolTang, ListOfPnts, RestrictLine, typs1, typs2, theIsReqToKeepRLine);
+    }
+    else
+    {
+      GeomGeomPerfomTrimSurf(theS1, theD1, theS2, theD2,
+              TolArc, TolTang, ListOfPnts, RestrictLine, typs1, typs2, theIsReqToKeepRLine);
+    }
+  }
+
+  if(!theIsReqToPostWLProc)
+    return;
+
+  for(Standard_Integer i = slin.Lower(); i <= slin.Upper(); i++)
+  {
+    Handle(IntPatch_WLine) aWL = Handle(IntPatch_WLine)::DownCast(slin.Value(i));
+
+    if(aWL.IsNull())
+      continue;
+
+    if(!aWL->IsPurgingAllowed())
+      continue;
+
+    Handle(IntPatch_WLine) aRW = IntPatch_WLineTool::ComputePurgedWLine(aWL, theS1, theS2, theD1, theD2);
+
+    if(aRW.IsNull())
+      continue;
+
+    slin.InsertAfter(i, aRW);
+    slin.Remove(i);
   }
 }
 
@@ -1162,10 +1238,10 @@ void IntPatch_Intersection::ParamParamPerfom(const Handle(Adaptor3d_HSurface)&
     Standard_Boolean ClearFlag = Standard_True;
     if(!ListOfPnts.IsEmpty())
     {
-      interpp.Perform(theS1,theD1,theS2,theD2,TolArc,TolTang,myFleche,myUVMaxStep, ListOfPnts, RestrictLine);
+      interpp.Perform(theS1,theD1,theS2,theD2,TolTang,TolArc,myFleche,myUVMaxStep, ListOfPnts, RestrictLine);
       ClearFlag = Standard_False;
     }
-    interpp.Perform(theS1,theD1,theS2,theD2,TolArc,TolTang,myFleche,myUVMaxStep,ClearFlag);   //double call!!!!!!!
+    interpp.Perform(theS1,theD1,theS2,theD2,TolTang,TolArc,myFleche,myUVMaxStep,ClearFlag);   //double call!!!!!!!
   }
   else if((theD1->DomainIsInfinite()) ^ (theD2->DomainIsInfinite()))
   {
@@ -1178,7 +1254,7 @@ void IntPatch_Intersection::ParamParamPerfom(const Handle(Adaptor3d_HSurface)&
       const Standard_Real AP = Max(MU, MV);
       Handle(Adaptor3d_HSurface) SS;
       FUN_TrimInfSurf(pMinXYZ, pMaxXYZ, theS1, AP, SS);
-      interpp.Perform(SS,theD1,theS2,theD2,TolArc,TolTang,myFleche,myUVMaxStep);
+      interpp.Perform(SS,theD1,theS2,theD2,TolTang,TolArc,myFleche,myUVMaxStep);
     }
     else
     {
@@ -1188,7 +1264,7 @@ void IntPatch_Intersection::ParamParamPerfom(const Handle(Adaptor3d_HSurface)&
       const Standard_Real AP = Max(MU, MV);
       Handle(Adaptor3d_HSurface) SS;
       FUN_TrimInfSurf(pMinXYZ, pMaxXYZ, theS2, AP, SS);
-      interpp.Perform(theS1, theD1, SS, theD2,TolArc,TolTang,myFleche,myUVMaxStep);
+      interpp.Perform(theS1, theD1, SS, theD2,TolTang, TolArc,myFleche,myUVMaxStep);
     }
   }//(theD1->DomainIsInfinite()) ^ (theD2->DomainIsInfinite())
   else
@@ -1211,8 +1287,8 @@ void IntPatch_Intersection::ParamParamPerfom(const Handle(Adaptor3d_HSurface)&
         for(Standard_Integer ip = 1; ip <= sop.Length(); ip++)
         {
           gp_Lin lin(sop.Value(ip),gp_Dir(v));
-          Handle(IntPatch_GLine) gl = new IntPatch_GLine(lin,Standard_False);
-          slin.Append(*(Handle_IntPatch_Line *)&gl);
+          Handle(IntPatch_Line) gl = new IntPatch_GLine(lin,Standard_False);
+          slin.Append(gl);
         }
 
         done = Standard_True;
@@ -1227,7 +1303,7 @@ void IntPatch_Intersection::ParamParamPerfom(const Handle(Adaptor3d_HSurface)&
       Handle(Adaptor3d_HSurface) nS1 = theS1;
       Handle(Adaptor3d_HSurface) nS2 = theS2;
       FUN_TrimBothSurf(theS1,typs1,theS2,typs2,1.e+8,nS1,nS2);
-      interpp.Perform(nS1,theD1,nS2,theD2,TolArc,TolTang,myFleche,myUVMaxStep);
+      interpp.Perform(nS1,theD1,nS2,theD2,TolTang,TolArc,myFleche,myUVMaxStep);
     }// 'NON - COLLINEAR LINES'
   }// both domains are infinite
 
@@ -1263,10 +1339,12 @@ void IntPatch_Intersection::GeomGeomPerfom(const Handle(Adaptor3d_HSurface)& the
                                            const Standard_Real TolTang,
                                            IntSurf_ListOfPntOn2S& ListOfPnts,
                                            const Standard_Boolean RestrictLine,
-                                           const GeomAbs_SurfaceType typs1,
-                                           const GeomAbs_SurfaceType typs2)
+                                           const GeomAbs_SurfaceType theTyps1,
+                                           const GeomAbs_SurfaceType theTyps2,
+                                           const Standard_Boolean theIsReqToKeepRLine)
 {
-  IntPatch_ImpImpIntersection interii(theS1,theD1,theS2,theD2,myTolArc,myTolTang);
+  IntPatch_ImpImpIntersection interii(theS1,theD1,theS2,theD2,
+                                      myTolArc,myTolTang, theIsReqToKeepRLine);
   const Standard_Boolean anIS = interii.IsDone();
   if (anIS)
   {
@@ -1278,72 +1356,98 @@ void IntPatch_Intersection::GeomGeomPerfom(const Handle(Adaptor3d_HSurface)& the
       if (tgte)
         oppo = interii.OppositeFaces();
 
+      Standard_Boolean isQuadSet = Standard_False;
+      IntSurf_Quadric Quad1,Quad2;
+
       for (Standard_Integer i = 1; i <= interii.NbLines(); i++)
       {
-        const Handle_IntPatch_Line& line = interii.Line(i);
+        const Handle(IntPatch_Line)& line = interii.Line(i);
         if (line->ArcType() == IntPatch_Analytic)
         {
-          const GeomAbs_SurfaceType typs1 = theS1->GetType();
-          const GeomAbs_SurfaceType typs2 = theS2->GetType();
-          IntSurf_Quadric Quad1,Quad2;
-          
-          switch(typs1)
+          if(!isQuadSet)
           {
-          case GeomAbs_Plane:
-            Quad1.SetValue(theS1->Plane());
-            break;
-
-          case GeomAbs_Cylinder:
-            Quad1.SetValue(theS1->Cylinder());
-            break;
-
-          case GeomAbs_Sphere:
-            Quad1.SetValue(theS1->Sphere());
-            break;
-
-          case GeomAbs_Cone:
-            Quad1.SetValue(theS1->Cone());
-            break;
-
-          case GeomAbs_Torus:
-            Quad1.SetValue(theS1->Torus());
-            break;
+            isQuadSet = Standard_True;
+            
+            const GeomAbs_SurfaceType aTyps1 = theS1->GetType();
+            const GeomAbs_SurfaceType aTyps2 = theS2->GetType();
+
+            switch(aTyps1)
+            {
+            case GeomAbs_Plane:
+              Quad1.SetValue(theS1->Plane());
+              break;
+
+            case GeomAbs_Cylinder:
+              Quad1.SetValue(theS1->Cylinder());
+              break;
+
+            case GeomAbs_Sphere:
+              Quad1.SetValue(theS1->Sphere());
+              break;
+
+            case GeomAbs_Cone:
+              Quad1.SetValue(theS1->Cone());
+              break;
+
+            case GeomAbs_Torus:
+              Quad1.SetValue(theS1->Torus());
+              break;
+
+            default:
+              isQuadSet = Standard_False;
+              break;
+            }
 
-          default:
-            break;
-          }
+            switch(aTyps2)
+            {
+            case GeomAbs_Plane:
+              Quad2.SetValue(theS2->Plane());
+              break;
+            case GeomAbs_Cylinder:
+              Quad2.SetValue(theS2->Cylinder());
+              break;
+
+            case GeomAbs_Sphere:
+              Quad2.SetValue(theS2->Sphere());
+              break;
+
+            case GeomAbs_Cone:
+              Quad2.SetValue(theS2->Cone());
+              break;
+
+            case GeomAbs_Torus:
+              Quad2.SetValue(theS2->Torus());
+              break;
+
+            default:
+              isQuadSet = Standard_False;
+              break;
+            }
 
-          switch(typs2)
-          {
-          case GeomAbs_Plane:
-            Quad2.SetValue(theS2->Plane());
-            break;
-          case GeomAbs_Cylinder:
-            Quad2.SetValue(theS2->Cylinder());
-            break;
-
-          case GeomAbs_Sphere:
-            Quad2.SetValue(theS2->Sphere());
-            break;
-
-          case GeomAbs_Cone:
-            Quad2.SetValue(theS2->Cone());
-            break;
-
-          case GeomAbs_Torus:
-            Quad2.SetValue(theS2->Torus());
-            break;
-
-          default:
-            break;
+            if(!isQuadSet)
+            {
+              break;
+            }
           }
 
           IntPatch_ALineToWLine AToW(Quad1,Quad2,0.01,0.05,aNbPointsInALine);
-          Handle(IntPatch_Line) wlin=AToW.MakeWLine((*((Handle_IntPatch_ALine *)(&line))));
+          Handle(IntPatch_WLine) wlin = 
+                      AToW.MakeWLine(Handle(IntPatch_ALine)::DownCast(line));
+          wlin->EnablePurging(Standard_False);
           slin.Append(wlin);
         }
         else
-          slin.Append(interii.Line(i));
+          slin.Append(line);
+      }
+
+      if(isQuadSet)
+      {
+        IntPatch_WLineTool::
+          ExtendTwoWlinesToEachOther(slin, Quad1, Quad2, TolTang,
+                                     theS1->IsUPeriodic()? theS1->UPeriod() : 0.0,
+                                     theS2->IsUPeriodic()? theS2->UPeriod() : 0.0,
+                                     theS1->IsVPeriodic()? theS1->VPeriod() : 0.0,
+                                     theS2->IsVPeriodic()? theS2->VPeriod() : 0.0);
       }
 
       for (Standard_Integer i = 1; i <= interii.NbPnts(); i++)
@@ -1353,20 +1457,22 @@ void IntPatch_Intersection::GeomGeomPerfom(const Handle(Adaptor3d_HSurface)& the
     }
   }
   else
-    ParamParamPerfom(theS1, theD1, theS2, theD2, TolArc, TolTang, ListOfPnts, RestrictLine, typs1, typs2);
+    ParamParamPerfom(theS1, theD1, theS2, theD2, 
+                TolArc, TolTang, ListOfPnts, RestrictLine, theTyps1, theTyps2);
 }
 
 //=======================================================================
-////function : GeomParamPerfom
+//function : GeomParamPerfom
 //purpose  : 
 //=======================================================================
-void IntPatch_Intersection::GeomParamPerfom(const Handle(Adaptor3d_HSurface)&  theS1,
-                                            const Handle(Adaptor3d_TopolTool)& theD1,
-                                            const Handle(Adaptor3d_HSurface)&  theS2,
-                                            const Handle(Adaptor3d_TopolTool)& theD2,
-                                            const Standard_Boolean isNotAnalitical,
-                                            const GeomAbs_SurfaceType typs1,
-                                            const GeomAbs_SurfaceType typs2)
+void IntPatch_Intersection::
+  GeomParamPerfom(const Handle(Adaptor3d_HSurface)&  theS1,
+                  const Handle(Adaptor3d_TopolTool)& theD1,
+                  const Handle(Adaptor3d_HSurface)&  theS2,
+                  const Handle(Adaptor3d_TopolTool)& theD2,
+                  const Standard_Boolean isNotAnalitical,
+                  const GeomAbs_SurfaceType typs1,
+                  const GeomAbs_SurfaceType typs2)
 {
   IntPatch_ImpPrmIntersection interip;
   if (myIsStartPnt)
@@ -1391,8 +1497,8 @@ void IntPatch_Intersection::GeomParamPerfom(const Handle(Adaptor3d_HSurface)&  t
         for(Standard_Integer ip = 1; ip <= sop.Length(); ip++)
         {
           gp_Lin lin(sop.Value(ip),gp_Dir(v));
-          Handle(IntPatch_GLine) gl = new IntPatch_GLine(lin,Standard_False);
-          slin.Append(*(Handle_IntPatch_Line *)&gl);
+          Handle(IntPatch_Line) gl = new IntPatch_GLine(lin,Standard_False);
+          slin.Append(gl);
         }
 
         done = Standard_True;
@@ -1439,6 +1545,83 @@ void IntPatch_Intersection::GeomParamPerfom(const Handle(Adaptor3d_HSurface)&  t
   }
 }
 
+//=======================================================================
+//function : GeomGeomPerfomTrimSurf
+//purpose  : This function returns ready walking-line (which is not need
+//            in convertation) as an intersection line between two
+//            trimmed surfaces.
+//=======================================================================
+void IntPatch_Intersection::
+  GeomGeomPerfomTrimSurf( const Handle(Adaptor3d_HSurface)& theS1,
+                          const Handle(Adaptor3d_TopolTool)& theD1,
+                          const Handle(Adaptor3d_HSurface)& theS2,
+                          const Handle(Adaptor3d_TopolTool)& theD2,
+                          const Standard_Real theTolArc,
+                          const Standard_Real theTolTang,
+                          IntSurf_ListOfPntOn2S& theListOfPnts,
+                          const Standard_Boolean RestrictLine,
+                          const GeomAbs_SurfaceType theTyps1,
+                          const GeomAbs_SurfaceType theTyps2,
+                          const Standard_Boolean theIsReqToKeepRLine)
+{
+  IntSurf_Quadric Quad1,Quad2;
+
+  if((theTyps1 == GeomAbs_Cylinder) && (theTyps2 == GeomAbs_Cylinder))
+  {
+    IntPatch_ImpImpIntersection anInt;
+    anInt.Perform(theS1, theD1, theS2, theD2, myTolArc,
+                  myTolTang, Standard_True, theIsReqToKeepRLine);
+
+    done = anInt.IsDone();
+
+    if(done)
+    {
+      empt = anInt.IsEmpty();
+      if (!empt)
+      {
+        tgte = anInt.TangentFaces();
+        if (tgte)
+          oppo = anInt.OppositeFaces();
+
+        const Standard_Integer aNbLin = anInt.NbLines();
+        const Standard_Integer aNbPts = anInt.NbPnts();
+
+        for(Standard_Integer aLID = 1; aLID <= aNbLin; aLID++)
+        {
+          const Handle(IntPatch_Line)& aLine = anInt.Line(aLID);
+          slin.Append(aLine);
+        }
+
+        for(Standard_Integer aPID = 1; aPID <= aNbPts; aPID++)
+        {
+          const IntPatch_Point& aPoint = anInt.Point(aPID);
+          spnt.Append(aPoint);
+        }
+
+        IntPatch_WLineTool::JoinWLines( slin, spnt, theTolTang,
+                                        theS1->IsUPeriodic()? theS1->UPeriod() : 0.0,
+                                        theS2->IsUPeriodic()? theS2->UPeriod() : 0.0,
+                                        theS1->IsVPeriodic()? theS1->VPeriod() : 0.0,
+                                        theS2->IsVPeriodic()? theS2->VPeriod() : 0.0,
+                                        theS1->FirstUParameter(),
+                                        theS1->LastUParameter(),
+                                        theS1->FirstVParameter(),
+                                        theS1->LastVParameter(),
+                                        theS2->FirstUParameter(),
+                                        theS2->LastUParameter(),
+                                        theS2->FirstVParameter(),
+                                        theS2->LastVParameter());
+      }
+    }
+  }
+  else
+  {
+    GeomGeomPerfom(theS1, theD1, theS2, theD2,
+            theTolArc, theTolTang, theListOfPnts,
+            RestrictLine, theTyps1, theTyps2, theIsReqToKeepRLine);
+  }
+}
+
 
 void IntPatch_Intersection::Perform(const Handle(Adaptor3d_HSurface)&  S1,
                                     const Handle(Adaptor3d_TopolTool)& D1,
@@ -1496,7 +1679,7 @@ void IntPatch_Intersection::Perform(const Handle(Adaptor3d_HSurface)&  S1,
   else
   {
     IntPatch_PrmPrmIntersection interpp;
-    interpp.Perform(S1,D1,S2,D2,U1,V1,U2,V2,TolArc,TolTang,myFleche,myUVMaxStep);
+    interpp.Perform(S1,D1,S2,D2,U1,V1,U2,V2,TolTang,TolArc,myFleche,myUVMaxStep);
     if (interpp.IsDone())
     {
       done = Standard_True;
@@ -1507,33 +1690,38 @@ void IntPatch_Intersection::Perform(const Handle(Adaptor3d_HSurface)&  S1,
       for (; i<=nblm; i++) slin.Append(interpp.Line(i));
     }
   }
+
+  for(Standard_Integer i = slin.Lower(); i <= slin.Upper(); i++)
+  {
+    Handle(IntPatch_WLine) aWL = Handle(IntPatch_WLine)::DownCast(slin.Value(i));
+
+    if(aWL.IsNull())
+      continue;
+
+    Handle(IntPatch_WLine) aRW = IntPatch_WLineTool::ComputePurgedWLine(aWL, S1, S2, D1, D2);
+
+    if(aRW.IsNull())
+      continue;
+
+    slin.InsertAfter(i, aRW);
+    slin.Remove(i);
+  }
 }
-//======================================================================
-#include <IntPatch_IType.hxx>
-#include <IntPatch_LineConstructor.hxx>
-#include <Handle_Adaptor2d_HCurve2d.hxx>
-#define MAXR 200
-
-
-//void IntPatch_Intersection__MAJ_R(Handle_Adaptor2d_HCurve2d *R1,
-//                                     Handle_Adaptor2d_HCurve2d *R2,
-//                                     int *NR1,
-//                                     int *NR2,
-//                                     Standard_Integer nbR1,
-//                                     Standard_Integer nbR2,
-//                                     const IntPatch_Point& VTX)
-void IntPatch_Intersection__MAJ_R(Handle_Adaptor2d_HCurve2d *,
-                                  Handle_Adaptor2d_HCurve2d *,
-                                  int *,
+
+#ifdef DUMPOFIntPatch_Intersection
+
+void IntPatch_Intersection__MAJ_R(Handle(Adaptor2d_HCurve2d) *R1,
+                                  Handle(Adaptor2d_HCurve2d) *,
+                                  int *NR1,
                                   int *,
+                                  Standard_Integer nbR1,
                                   Standard_Integer ,
-                                  Standard_Integer ,
-                                  const IntPatch_Point& )
+                                  const IntPatch_Point& VTX)
 { 
-  /*
+  
   if(VTX.IsOnDomS1()) { 
     
-    //-- long unsigned ptr= *((long unsigned *)(((Handle_Standard_Transient *)(&(VTX.ArcOnS1())))));
+    //-- long unsigned ptr= *((long unsigned *)(((Handle(Standard_Transient) *)(&(VTX.ArcOnS1())))));
     for(Standard_Integer i=0; i<nbR1;i++) { 
       if(VTX.ArcOnS1()==R1[i]) { 
         NR1[i]++;
@@ -1544,37 +1732,36 @@ void IntPatch_Intersection__MAJ_R(Handle_Adaptor2d_HCurve2d *,
     printf("\n R Pas trouvee  (IntPatch)\n");
     
   }
-  */
 }
+#endif
 
-
-//void IntPatch_Intersection::Dump(const Standard_Integer Mode,
-void IntPatch_Intersection::Dump(const Standard_Integer ,
-                                 const Handle(Adaptor3d_HSurface)&  S1,
-                                 const Handle(Adaptor3d_TopolTool)& D1,
-                                 const Handle(Adaptor3d_HSurface)&  S2,
-                                 const Handle(Adaptor3d_TopolTool)& D2) const 
+void IntPatch_Intersection::Dump(const Standard_Integer /*Mode*/,
+                                 const Handle(Adaptor3d_HSurface)&  /*S1*/,
+                                 const Handle(Adaptor3d_TopolTool)& /*D1*/,
+                                 const Handle(Adaptor3d_HSurface)&  /*S2*/,
+                                 const Handle(Adaptor3d_TopolTool)& /*D2*/) const 
 { 
-  
+#ifdef DUMPOFIntPatch_Intersection
+  const int MAXR = 200;
   //-- ----------------------------------------------------------------------
   //--  construction de la liste des restrictions & vertex 
   //--
   int NR1[MAXR],NR2[MAXR];
-  Handle_Adaptor2d_HCurve2d R1[MAXR],R2[MAXR];
+  Handle(Adaptor2d_HCurve2d) R1[MAXR],R2[MAXR];
   Standard_Integer nbR1=0,nbR2=0;
   for(D1->Init();D1->More() && nbR1<MAXR; D1->Next()) { 
     R1[nbR1]=D1->Value(); 
     NR1[nbR1]=0;
     nbR1++;
   }
-  for(D2->Init();D2->More() && nbR2<MAXR; D2->Next()) { 
+  for(D2->Init();D2->More() && nbR2<MAXR; D2->Next()) {
     R2[nbR2]=D2->Value();
     NR2[nbR2]=0;
     nbR2++;
   }
   
   printf("\nDUMP_INT:  ----empt:%2ud  tgte:%2ud  oppo:%2ud ---------------------------------",empt,tgte,empt);
-  Standard_Integer i,j,nbr1,nbr2,nbgl,nbgc,nbge,nbgp,nbgh,nbl,nbr,nbg,nbw,nba;
+  Standard_Integer i,nbr1,nbr2,nbgl,nbgc,nbge,nbgp,nbgh,nbl,nbr,nbg,nbw,nba;
   nbl=nbr=nbg=nbw=nba=nbgl=nbge=nbr1=nbr2=nbgc=nbgp=nbgh=0;
   nbl=NbLines();
   for(i=1;i<=nbl;i++) { 
@@ -1583,8 +1770,7 @@ void IntPatch_Intersection::Dump(const Standard_Integer ,
     if(IType == IntPatch_Walking) nbw++;
     else     if(IType == IntPatch_Restriction) { 
       nbr++;
-      Handle(IntPatch_RLine)& rlin =
-        *((Handle(IntPatch_RLine) *)&line);
+      Handle(IntPatch_RLine) rlin (Handle(IntPatch_RLine)::DownCast (line));
       if(rlin->IsArcOnS1()) nbr1++;
       if(rlin->IsArcOnS2()) nbr2++;
     }
@@ -1620,8 +1806,7 @@ void IntPatch_Intersection::Dump(const Standard_Integer ,
       nbllc++;
       const Handle(IntPatch_Line)& LineK = LineConstructor.Line(k);
       if (LineK->ArcType() == IntPatch_Analytic) { 
-        Handle(IntPatch_ALine)& alin =
-          *((Handle(IntPatch_ALine) *)&LineK);
+        Handle(IntPatch_ALine) alin (Handle(IntPatch_ALine)::DownCast (LineK));
         nbvtx=alin->NbVertex();
         nbva+=nbvtx;        nba++;
         for(v=1;v<=nbvtx;v++) { 
@@ -1629,8 +1814,7 @@ void IntPatch_Intersection::Dump(const Standard_Integer ,
         }
       }
       else if (LineK->ArcType() == IntPatch_Restriction) {
-        Handle(IntPatch_RLine)& rlin =
-          *((Handle(IntPatch_RLine) *)&LineK);
+        Handle(IntPatch_RLine) rlin (Handle(IntPatch_RLine)::DownCast (LineK));
         nbvtx=rlin->NbVertex();
         nbvr+=nbvtx;        nbr++;
         for(v=1;v<=nbvtx;v++) { 
@@ -1638,8 +1822,7 @@ void IntPatch_Intersection::Dump(const Standard_Integer ,
         }
       }
       else if (LineK->ArcType() == IntPatch_Walking) {
-        Handle(IntPatch_WLine)& wlin =
-          *((Handle(IntPatch_WLine) *)&LineK);
+        Handle(IntPatch_WLine) wlin (Handle(IntPatch_WLine)::DownCast (LineK));
         nbvtx=wlin->NbVertex();
         nbvw+=nbvtx;        nbw++;
         for(v=1;v<=nbvtx;v++) { 
@@ -1647,8 +1830,7 @@ void IntPatch_Intersection::Dump(const Standard_Integer ,
         }
       }
       else { 
-        Handle(IntPatch_GLine)& glin =
-          *((Handle(IntPatch_GLine) *)&LineK);
+        Handle(IntPatch_GLine) glin (Handle(IntPatch_GLine)::DownCast (LineK));
         nbvtx=glin->NbVertex();
         nbvg+=nbvtx;        nbg++;
         for(v=1;v<=nbvtx;v++) { 
@@ -1662,7 +1844,7 @@ void IntPatch_Intersection::Dump(const Standard_Integer ,
   printf("\nDUMP_LC :vtx          :%2d     r:%2d    :%2d     :%2d",
          nbvw,nbvr,nbva,nbvg);
 
+  printf("\n");
 
-
-   printf("\n");
+#endif 
 }