Salome HOME
Merge from V6_main 11/02/2013
[modules/geom.git] / src / BlockFix / BlockFix_UnionFaces.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21
22 //  File:    BlockFix_UnionFaces.cxx
23 //  Created: Tue Dec  7 17:15:42 2004
24 //  Author:  Pavel DURANDIN
25
26 #include <BlockFix_UnionFaces.hxx>
27
28 #include <Basics_OCCTVersion.hxx>
29
30 #include <ShapeAnalysis_WireOrder.hxx>
31 #include <ShapeAnalysis_Edge.hxx>
32
33 #include <ShapeBuild_Edge.hxx>
34 #include <ShapeBuild_ReShape.hxx>
35
36 #include <ShapeExtend_WireData.hxx>
37 #include <ShapeExtend_CompositeSurface.hxx>
38
39 #include <ShapeFix_Face.hxx>
40 #include <ShapeFix_ComposeShell.hxx>
41 #include <ShapeFix_SequenceOfWireSegment.hxx>
42 #include <ShapeFix_WireSegment.hxx>
43 #include <ShapeFix_Wire.hxx>
44 #include <ShapeFix_Edge.hxx>
45
46 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
47 #include <IntPatch_ImpImpIntersection.hxx>
48 #else
49 #include <IntPatch_TheIIIntOfIntersection.hxx>
50 #endif
51
52 #include <BRep_Tool.hxx>
53 #include <BRep_Builder.hxx>
54 #include <BRepTools.hxx>
55 #include <BRepTopAdaptor_TopolTool.hxx>
56
57 #include <TopExp.hxx>
58 #include <TopExp_Explorer.hxx>
59
60 #include <TopTools_SequenceOfShape.hxx>
61 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
62 #include <TopTools_ListOfShape.hxx>
63 #include <TopTools_ListIteratorOfListOfShape.hxx>
64 #include <TopTools_MapOfShape.hxx>
65 #include <TopTools_MapIteratorOfMapOfShape.hxx>
66
67 #include <TopoDS.hxx>
68 #include <TopoDS_Edge.hxx>
69 #include <TopoDS_Wire.hxx>
70 #include <TopoDS_Face.hxx>
71 #include <TopoDS_Solid.hxx>
72 #include <TopoDS_Vertex.hxx>
73 #include <TopoDS_Shell.hxx>
74 #include <TopoDS_Iterator.hxx>
75 #include <TopoDS_Shape.hxx>
76
77 #include <TColGeom_HArray2OfSurface.hxx>
78
79 #include <GeomAdaptor_HSurface.hxx>
80 #include <GeomLib_IsPlanarSurface.hxx>
81
82 #include <Geom_Surface.hxx>
83 #include <Geom_Plane.hxx>
84 #include <Geom_OffsetSurface.hxx>
85 #include <Geom_SphericalSurface.hxx>
86 #include <Geom_CylindricalSurface.hxx>
87 #include <Geom_SurfaceOfRevolution.hxx>
88 #include <Geom_SurfaceOfLinearExtrusion.hxx>
89 #include <Geom_RectangularTrimmedSurface.hxx>
90
91 #include <Geom_Curve.hxx>
92 #include <Geom_Line.hxx>
93 #include <Geom_Circle.hxx>
94
95 #include <Geom2d_Line.hxx>
96
97 #include <gp_XY.hxx>
98 #include <gp_Pnt2d.hxx>
99
100 #include <Standard_Failure.hxx>
101 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
102
103 //=======================================================================
104 //function : BlockFix_UnionFaces
105 //purpose  :
106 //=======================================================================
107 BlockFix_UnionFaces::BlockFix_UnionFaces()
108   : myTolerance(Precision::Confusion()),
109     myOptimumNbFaces(6)
110 {
111 }
112
113 //=======================================================================
114 //function : GetTolerance
115 //purpose  :
116 //=======================================================================
117 Standard_Real& BlockFix_UnionFaces::GetTolerance()
118 {
119   return myTolerance;
120 }
121
122 //=======================================================================
123 //function : GetOptimumNbFaces
124 //purpose  :
125 //=======================================================================
126 Standard_Integer& BlockFix_UnionFaces::GetOptimumNbFaces()
127 {
128   return myOptimumNbFaces;
129 }
130
131 //=======================================================================
132 //function : AddOrdinaryEdges
133 //purpose  : auxilary
134 //           adds edges from the shape to the sequence
135 //           seams and equal edges are dropped
136 //           Returns true if one of original edges dropped
137 //=======================================================================
138 static Standard_Boolean AddOrdinaryEdges(TopTools_SequenceOfShape& edges,
139                                          const TopoDS_Shape aShape,
140                                          Standard_Integer& anIndex)
141 {
142   //map of edges
143   TopTools_MapOfShape aNewEdges;
144   //add edges without seams
145   for(TopExp_Explorer exp(aShape,TopAbs_EDGE); exp.More(); exp.Next()) {
146     TopoDS_Shape edge = exp.Current();
147     if(aNewEdges.Contains(edge))
148       aNewEdges.Remove(edge);
149     else
150       aNewEdges.Add(edge);
151   }
152
153   Standard_Boolean isDropped = Standard_False;
154   //merge edges and drop seams
155   for(Standard_Integer i = 1; i <= edges.Length(); i++) {
156     TopoDS_Shape current = edges(i);
157     if(aNewEdges.Contains(current)) {
158
159       aNewEdges.Remove(current);
160       edges.Remove(i);
161       i--;
162
163       if(!isDropped) {
164         isDropped = Standard_True;
165         anIndex = i;
166       }
167     }
168   }
169
170   //add edges to the sequemce
171   for(TopTools_MapIteratorOfMapOfShape anIter(aNewEdges); anIter.More(); anIter.Next())
172     edges.Append(anIter.Key());
173
174   return isDropped;
175 }
176
177 //=======================================================================
178 //function : ClearRts
179 //purpose  : auxilary
180 //=======================================================================
181 static Handle(Geom_Surface) ClearRts(const Handle(Geom_Surface)& aSurface)
182 {
183   if(aSurface->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
184     Handle(Geom_RectangularTrimmedSurface) rts =
185       Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
186     return rts->BasisSurface();
187   }
188   return aSurface;
189 }
190
191 //=======================================================================
192 //function : Perform
193 //purpose  :
194 //=======================================================================
195 TopoDS_Shape BlockFix_UnionFaces::Perform(const TopoDS_Shape& Shape)
196 {
197   Handle(ShapeBuild_ReShape) myContext = new ShapeBuild_ReShape;
198   TopoDS_Shape aResShape = myContext->Apply(Shape);
199
200   // processing each solid
201   TopExp_Explorer exps;
202   for (exps.Init(Shape, TopAbs_SOLID); exps.More(); exps.Next()) {
203     TopoDS_Solid aSolid = TopoDS::Solid(exps.Current());
204
205     // creating map of edge faces
206     TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
207     TopExp::MapShapesAndAncestors(aSolid, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
208
209     // map of processed shapes
210     TopTools_MapOfShape aProcessed;
211
212     Handle(ShapeBuild_ReShape) aContext = new ShapeBuild_ReShape;
213
214     Standard_Integer NbModif = 0;
215     Standard_Boolean hasFailed = Standard_False;
216     Standard_Real tol = Min(Max(Precision::Confusion(), myTolerance/10.), 0.1);
217
218     // count faces
219     int nbf = 0;
220     TopExp_Explorer exp;
221     TopTools_MapOfShape mapF;
222     for (exp.Init(aSolid, TopAbs_FACE); exp.More(); exp.Next()) {
223       if (mapF.Add(exp.Current()))
224         nbf++;
225     }
226
227     bool doUnion = ((myOptimumNbFaces == 0) ||
228                     ((myOptimumNbFaces > 0) && (nbf > myOptimumNbFaces)));
229
230     // processing each face
231     mapF.Clear();
232     for (exp.Init(aSolid, TopAbs_FACE); exp.More() && doUnion; exp.Next()) {
233       TopoDS_Face aFace = TopoDS::Face(exp.Current().Oriented(TopAbs_FORWARD));
234
235       if (aProcessed.Contains(aFace))
236         continue;
237
238       Standard_Integer dummy;
239       TopTools_SequenceOfShape edges;
240       AddOrdinaryEdges(edges,aFace,dummy);
241
242       TopTools_SequenceOfShape faces;
243       faces.Append(aFace);
244
245       //surface and location to construct result
246       TopLoc_Location aBaseLocation;
247       Handle(Geom_Surface) aBaseSurface = BRep_Tool::Surface(aFace,aBaseLocation);
248       aBaseSurface = ClearRts(aBaseSurface);
249
250       // find adjacent faces to union
251       Standard_Integer i;
252       for (i = 1; i <= edges.Length(); i++) {
253         TopoDS_Edge edge = TopoDS::Edge(edges(i));
254         if (BRep_Tool::Degenerated(edge))
255           continue;
256
257         const TopTools_ListOfShape& aList = aMapEdgeFaces.FindFromKey(edge);
258         TopTools_ListIteratorOfListOfShape anIter(aList);
259         for (; anIter.More(); anIter.Next()) {
260           TopoDS_Face anCheckedFace = TopoDS::Face(anIter.Value().Oriented(TopAbs_FORWARD));
261           if (anCheckedFace.IsSame(aFace))
262             continue;
263
264           if (aProcessed.Contains(anCheckedFace))
265             continue;
266
267           if (IsSameDomain(aFace,anCheckedFace)) {
268
269             if (aList.Extent() != 2) {
270               // non mainfold case is not processed
271               continue;
272             }
273
274             // replacing pcurves
275             TopoDS_Face aMockUpFace;
276             BRep_Builder B;
277             B.MakeFace(aMockUpFace,aBaseSurface,aBaseLocation,0.);
278             MovePCurves(aMockUpFace,anCheckedFace);
279
280             if (AddOrdinaryEdges(edges,aMockUpFace,dummy)) {
281               // sequence edges is modified
282               i = dummy;
283             }
284
285             faces.Append(anCheckedFace);
286             aProcessed.Add(anCheckedFace);
287             break;
288           }
289         }
290       }
291
292       // all faces collected in the sequence. Perform union of faces
293       if (faces.Length() > 1) {
294         NbModif++;
295         TopoDS_Face aResult;
296         BRep_Builder B;
297         B.MakeFace(aResult,aBaseSurface,aBaseLocation,0);
298         Standard_Integer nbWires = 0;
299
300         // connecting wires
301         while (edges.Length()>0) {
302
303           Standard_Boolean isEdge3d = Standard_False;
304           nbWires++;
305           TopTools_MapOfShape aVertices;
306           TopoDS_Wire aWire;
307           B.MakeWire(aWire);
308
309           TopoDS_Edge anEdge = TopoDS::Edge(edges(1));
310           edges.Remove(1);
311
312           isEdge3d |= !BRep_Tool::Degenerated(anEdge);
313           B.Add(aWire,anEdge);
314           TopoDS_Vertex V1,V2;
315           TopExp::Vertices(anEdge,V1,V2);
316           aVertices.Add(V1);
317           aVertices.Add(V2);
318
319           Standard_Boolean isNewFound = Standard_False;
320           do {
321             isNewFound = Standard_False;
322             for(Standard_Integer j = 1; j <= edges.Length(); j++) {
323               anEdge = TopoDS::Edge(edges(j));
324               TopExp::Vertices(anEdge,V1,V2);
325               if(aVertices.Contains(V1) || aVertices.Contains(V2)) {
326                 isEdge3d |= !BRep_Tool::Degenerated(anEdge);
327                 aVertices.Add(V1);
328                 aVertices.Add(V2);
329                 B.Add(aWire,anEdge);
330                 edges.Remove(j);
331                 j--;
332                 isNewFound = Standard_True;
333               }
334             }
335           } while (isNewFound);
336
337           // sorting any type of edges
338           aWire = TopoDS::Wire(aContext->Apply(aWire));
339
340           TopoDS_Face tmpF = TopoDS::Face(aContext->Apply(faces(1).Oriented(TopAbs_FORWARD)));
341           Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire(aWire,tmpF,Precision::Confusion());
342           sfw->FixReorder();
343           Standard_Boolean isDegRemoved = Standard_False;
344           if(!sfw->StatusReorder ( ShapeExtend_FAIL )) {
345             // clear degenerated edges if at least one with 3d curve exist
346             if(isEdge3d) {
347               Handle(ShapeExtend_WireData) sewd = sfw->WireData();
348               for(Standard_Integer j = 1; j<=sewd->NbEdges();j++) {
349                 TopoDS_Edge E = sewd->Edge(j);
350                 if(BRep_Tool::Degenerated(E)) {
351                   sewd->Remove(j);
352                   isDegRemoved = Standard_True;
353                   j--;
354                 }
355               }
356             }
357             sfw->FixShifted();
358             if(isDegRemoved)
359               sfw->FixDegenerated();
360           }
361           TopoDS_Wire aWireFixed = sfw->Wire();
362           aContext->Replace(aWire,aWireFixed);
363           // add resulting wire
364           if(isEdge3d) {
365             B.Add(aResult,aWireFixed);
366           }
367           else  {
368             // sorting edges
369             Handle(ShapeExtend_WireData) sbwd = sfw->WireData();
370             Standard_Integer nbEdges = sbwd->NbEdges();
371             // sort degenerated edges and create one edge instead of several ones
372             ShapeAnalysis_WireOrder sawo(Standard_False, 0);
373             ShapeAnalysis_Edge sae;
374             Standard_Integer aLastEdge = nbEdges;
375             for(Standard_Integer j = 1; j <= nbEdges; j++) {
376               Standard_Real f,l;
377               //smh protection on NULL pcurve
378               Handle(Geom2d_Curve) c2d;
379               if(!sae.PCurve(sbwd->Edge(j),tmpF,c2d,f,l)) {
380                 aLastEdge--;
381                 continue;
382               }
383               sawo.Add(c2d->Value(f).XY(),c2d->Value(l).XY());
384             }
385             sawo.Perform();
386
387             // constructind one degenerative edge
388             gp_XY aStart, anEnd, tmp;
389             Standard_Integer nbFirst = sawo.Ordered(1);
390             TopoDS_Edge anOrigE = TopoDS::Edge(sbwd->Edge(nbFirst).Oriented(TopAbs_FORWARD));
391             ShapeBuild_Edge sbe;
392             TopoDS_Vertex aDummyV;
393             TopoDS_Edge E = sbe.CopyReplaceVertices(anOrigE,aDummyV,aDummyV);
394             sawo.XY(nbFirst,aStart,tmp);
395             sawo.XY(sawo.Ordered(aLastEdge),tmp,anEnd);
396
397             gp_XY aVec = anEnd-aStart;
398             Handle(Geom2d_Line) aLine = new Geom2d_Line(aStart,gp_Dir2d(anEnd-aStart));
399
400             B.UpdateEdge(E,aLine,tmpF,0.);
401             B.Range(E,tmpF,0.,aVec.Modulus());
402             Handle(Geom_Curve) C3d;
403             B.UpdateEdge(E,C3d,0.);
404             B.Degenerated(E,Standard_True);
405             TopoDS_Wire aW;
406             B.MakeWire(aW);
407             B.Add(aW,E);
408             B.Add(aResult,aW);
409           }
410         }
411
412         // perform substitution of face
413         aContext->Replace(aContext->Apply(aFace),aResult);
414
415         ShapeFix_Face sff (aResult);
416         //Intializing by tolerances
417         sff.SetPrecision(myTolerance);
418         sff.SetMinTolerance(tol);
419         sff.SetMaxTolerance(Max(1.,myTolerance*1000.));
420         //Setting modes
421         sff.FixOrientationMode() = 0;
422         //sff.FixWireMode() = 0;
423         sff.SetContext(aContext);
424         // Applying the fixes
425         sff.Perform();
426         if(sff.Status(ShapeExtend_FAIL))
427         hasFailed = Standard_True;
428
429         // breaking down to several faces
430         TopoDS_Shape theResult = aContext->Apply(aResult);
431         for (TopExp_Explorer aFaceExp (theResult,TopAbs_FACE); aFaceExp.More(); aFaceExp.Next()) {
432           TopoDS_Face aCurrent = TopoDS::Face(aFaceExp.Current().Oriented(TopAbs_FORWARD));
433           Handle(TColGeom_HArray2OfSurface) grid = new TColGeom_HArray2OfSurface ( 1, 1, 1, 1 );
434           grid->SetValue ( 1, 1, aBaseSurface );
435           Handle(ShapeExtend_CompositeSurface) G = new ShapeExtend_CompositeSurface ( grid );
436           ShapeFix_ComposeShell CompShell;
437           CompShell.Init ( G, aBaseLocation, aCurrent, ::Precision::Confusion() );//myPrecision
438           CompShell.SetContext( aContext );
439
440           TopTools_SequenceOfShape parts;
441           ShapeFix_SequenceOfWireSegment wires;
442           for(TopExp_Explorer W_Exp(aCurrent,TopAbs_WIRE);W_Exp.More();W_Exp.Next()) {
443             Handle(ShapeExtend_WireData) sbwd =
444               new ShapeExtend_WireData ( TopoDS::Wire(W_Exp.Current() ));
445             ShapeFix_WireSegment seg ( sbwd, TopAbs_REVERSED );
446             wires.Append(seg);
447           }
448
449           CompShell.DispatchWires ( parts,wires );
450           for (Standard_Integer j=1; j <= parts.Length(); j++ ) {
451             ShapeFix_Face aFixOrient(TopoDS::Face(parts(j)));
452             aFixOrient.SetContext(aContext);
453             aFixOrient.FixOrientation();
454           }
455
456           TopoDS_Shape CompRes;
457           if ( faces.Length() !=1 ) {
458             TopoDS_Shell S;
459             B.MakeShell ( S );
460             for ( i=1; i <= parts.Length(); i++ )
461               B.Add ( S, parts(i) );
462             CompRes = S;
463           }
464           else CompRes = parts(1);
465
466           aContext->Replace(aCurrent,CompRes);
467         }
468
469         // remove the remaining faces
470         for(i = 2; i <= faces.Length(); i++)
471           aContext->Remove(faces(i));
472       }
473     } // end processing each face
474
475     //TopoDS_Shape aResult = Shape;
476     if (NbModif > 0 && !hasFailed) {
477       TopoDS_Shape aResult = aContext->Apply(aSolid);
478
479       ShapeFix_Edge sfe;
480       for (exp.Init(aResult,TopAbs_EDGE); exp.More(); exp.Next()) {
481         TopoDS_Edge E = TopoDS::Edge(exp.Current());
482         sfe.FixVertexTolerance (E);
483         // ptv add fix same parameter
484         sfe.FixSameParameter(E, myTolerance);
485       }
486
487       myContext->Replace(aSolid, aResult);
488     }
489     //else
490     {
491       for (exp.Init(aSolid, TopAbs_FACE); exp.More(); exp.Next()) {
492         TopoDS_Face aFace = TopoDS::Face(exp.Current().Oriented(TopAbs_FORWARD));
493         Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire;
494         sfw->SetContext(myContext);
495         sfw->SetPrecision(myTolerance);
496         sfw->SetMinTolerance(myTolerance);
497         sfw->SetMaxTolerance(Max(1.,myTolerance*1000.));
498         sfw->SetFace(aFace);
499         for (TopoDS_Iterator iter (aFace,Standard_False); iter.More(); iter.Next()) {
500           TopoDS_Wire wire = TopoDS::Wire(iter.Value());
501           sfw->Load(wire);
502           sfw->FixReorder();
503           sfw->FixShifted();
504         }
505       }
506     }
507   } // end processing each solid
508
509   aResShape = myContext->Apply(Shape);
510   return aResShape;
511 }
512
513 //=======================================================================
514 //function : IsSameDomain
515 //purpose  :
516 //=======================================================================
517 bool getCylinder (Handle(Geom_Surface)& theInSurface, gp_Cylinder& theOutCylinder)
518 {
519   bool isCylinder = false;
520
521   if (theInSurface->IsKind(STANDARD_TYPE(Geom_CylindricalSurface))) {
522     Handle(Geom_CylindricalSurface) aGC = Handle(Geom_CylindricalSurface)::DownCast(theInSurface);
523
524     theOutCylinder = aGC->Cylinder();
525     isCylinder = true;
526   }
527   else if (theInSurface->IsKind(STANDARD_TYPE(Geom_SurfaceOfRevolution))) {
528     Handle(Geom_SurfaceOfRevolution) aRS =
529       Handle(Geom_SurfaceOfRevolution)::DownCast(theInSurface);
530     Handle(Geom_Curve) aBasis = aRS->BasisCurve();
531     if (aBasis->IsKind(STANDARD_TYPE(Geom_Line))) {
532       Handle(Geom_Line) aBasisLine = Handle(Geom_Line)::DownCast(aBasis);
533       gp_Dir aDir = aRS->Direction();
534       gp_Dir aBasisDir = aBasisLine->Position().Direction();
535       if (aBasisDir.IsParallel(aDir, Precision::Confusion())) {
536         // basis line is parallel to the revolution axis: it is a cylinder
537         gp_Pnt aLoc = aRS->Location();
538         Standard_Real aR = aBasisLine->Lin().Distance(aLoc);
539         gp_Ax3 aCylAx (aLoc, aDir);
540
541         theOutCylinder = gp_Cylinder(aCylAx, aR);
542         isCylinder = true;
543       }
544     }
545   }
546   else if (theInSurface->IsKind(STANDARD_TYPE(Geom_SurfaceOfLinearExtrusion))) {
547     Handle(Geom_SurfaceOfLinearExtrusion) aLES =
548       Handle(Geom_SurfaceOfLinearExtrusion)::DownCast(theInSurface);
549     Handle(Geom_Curve) aBasis = aLES->BasisCurve();
550     if (aBasis->IsKind(STANDARD_TYPE(Geom_Circle))) {
551       Handle(Geom_Circle) aBasisCircle = Handle(Geom_Circle)::DownCast(aBasis);
552       gp_Dir aDir = aLES->Direction();
553       gp_Dir aBasisDir = aBasisCircle->Position().Direction();
554       if (aBasisDir.IsParallel(aDir, Precision::Confusion())) {
555         // basis circle is normal to the extrusion axis: it is a cylinder
556         gp_Pnt aLoc = aBasisCircle->Location();
557         Standard_Real aR = aBasisCircle->Radius();
558         gp_Ax3 aCylAx (aLoc, aDir);
559
560         theOutCylinder = gp_Cylinder(aCylAx, aR);
561         isCylinder = true;
562       }
563     }
564   }
565   else {
566   }
567
568   return isCylinder;
569 }
570
571 Standard_Boolean BlockFix_UnionFaces::IsSameDomain(const TopoDS_Face& aFace,
572                                                    const TopoDS_Face& aCheckedFace) const
573 {
574   //checking the same handles
575   TopLoc_Location L1, L2;
576   Handle(Geom_Surface) S1, S2;
577
578   S1 = BRep_Tool::Surface(aFace,L1);
579   S2 = BRep_Tool::Surface(aCheckedFace,L2);
580
581   if (S1 == S2 && L1 == L2)
582     return true;
583
584   // planar and cylindrical cases (IMP 20052)
585   Standard_Real aPrec = Precision::Confusion();
586
587   S1 = BRep_Tool::Surface(aFace);
588   S2 = BRep_Tool::Surface(aCheckedFace);
589
590   S1 = ClearRts(S1);
591   S2 = ClearRts(S2);
592
593   //Handle(Geom_OffsetSurface) aGOFS1, aGOFS2;
594   //aGOFS1 = Handle(Geom_OffsetSurface)::DownCast(S1);
595   //aGOFS2 = Handle(Geom_OffsetSurface)::DownCast(S2);
596   //if (!aGOFS1.IsNull()) S1 = aGOFS1->BasisSurface();
597   //if (!aGOFS2.IsNull()) S2 = aGOFS2->BasisSurface();
598
599   // case of two elementary surfaces: use OCCT tool
600   // elementary surfaces: ConicalSurface, CylindricalSurface,
601   //                      Plane, SphericalSurface and ToroidalSurface
602   if (S1->IsKind(STANDARD_TYPE(Geom_ElementarySurface)) &&
603       S2->IsKind(STANDARD_TYPE(Geom_ElementarySurface)))
604   {
605     Handle(GeomAdaptor_HSurface) aGA1 = new GeomAdaptor_HSurface(S1);
606     Handle(GeomAdaptor_HSurface) aGA2 = new GeomAdaptor_HSurface(S2);
607
608     Handle(BRepTopAdaptor_TopolTool) aTT1 = new BRepTopAdaptor_TopolTool();
609     Handle(BRepTopAdaptor_TopolTool) aTT2 = new BRepTopAdaptor_TopolTool();
610
611     try {
612 #if OCC_VERSION_LARGE > 0x06010000
613       OCC_CATCH_SIGNALS;
614 #endif
615
616 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
617       IntPatch_ImpImpIntersection anIIInt (aGA1, aTT1, aGA2, aTT2, aPrec, aPrec);
618 #else
619       IntPatch_TheIIIntOfIntersection anIIInt (aGA1, aTT1, aGA2, aTT2, aPrec, aPrec);
620 #endif
621       if (!anIIInt.IsDone() || anIIInt.IsEmpty())
622         return false;
623
624       return anIIInt.TangentFaces();
625     }
626     catch (Standard_Failure) {
627       return false;
628     }
629   }
630
631   // case of two planar surfaces:
632   // all kinds of surfaces checked, including b-spline and bezier
633   GeomLib_IsPlanarSurface aPlanarityChecker1 (S1, aPrec);
634   if (aPlanarityChecker1.IsPlanar()) {
635     GeomLib_IsPlanarSurface aPlanarityChecker2 (S2, aPrec);
636     if (aPlanarityChecker2.IsPlanar()) {
637       gp_Pln aPln1 = aPlanarityChecker1.Plan();
638       gp_Pln aPln2 = aPlanarityChecker2.Plan();
639
640       if (aPln1.Position().Direction().IsParallel(aPln2.Position().Direction(), aPrec) &&
641           aPln1.Distance(aPln2) < aPrec) {
642         return true;
643       }
644     }
645   }
646
647   // case of two cylindrical surfaces, at least one of which is a swept surface
648   // swept surfaces: SurfaceOfLinearExtrusion, SurfaceOfRevolution
649   if ((S1->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) ||
650        S1->IsKind(STANDARD_TYPE(Geom_SweptSurface))) &&
651       (S2->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) ||
652        S2->IsKind(STANDARD_TYPE(Geom_SweptSurface))))
653   {
654     gp_Cylinder aCyl1, aCyl2;
655     if (getCylinder(S1, aCyl1) && getCylinder(S2, aCyl2)) {
656       if (fabs(aCyl1.Radius() - aCyl2.Radius()) < aPrec) {
657         gp_Dir aDir1 = aCyl1.Position().Direction();
658         gp_Dir aDir2 = aCyl2.Position().Direction();
659         if (aDir1.IsParallel(aDir2, aPrec)) {
660           gp_Pnt aLoc1 = aCyl1.Location();
661           gp_Pnt aLoc2 = aCyl2.Location();
662           gp_Vec aVec12 (aLoc1, aLoc2);
663           if (aVec12.SquareMagnitude() < aPrec*aPrec ||
664               aVec12.IsParallel(aDir1, aPrec)) {
665             return true;
666           }
667         }
668       }
669     }
670   }
671
672   return false;
673 }
674
675 //=======================================================================
676 //function : MovePCurves
677 //purpose  :
678 //=======================================================================
679 void BlockFix_UnionFaces::MovePCurves(TopoDS_Face& aTarget,
680                                       const TopoDS_Face& aSource) const
681 {
682   BRep_Builder B;
683   for(TopExp_Explorer wexp(aSource,TopAbs_WIRE);wexp.More();wexp.Next()) {
684     Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire(TopoDS::Wire(wexp.Current()),
685                                                   aTarget, Precision::Confusion());
686     sfw->FixReorder();
687     Standard_Boolean isReoredFailed = sfw->StatusReorder ( ShapeExtend_FAIL );
688     sfw->FixEdgeCurves();
689     if(isReoredFailed)
690       continue;
691
692     sfw->FixShifted();
693     sfw->FixDegenerated();
694
695     // remove degenerated edges from not degenerated points
696     ShapeAnalysis_Edge sae;
697     Handle(ShapeExtend_WireData) sewd = sfw->WireData();
698     for(Standard_Integer i = 1; i<=sewd->NbEdges();i++) {
699       TopoDS_Edge E = sewd->Edge(i);
700       if(BRep_Tool::Degenerated(E)&&!sae.HasPCurve(E,aTarget)) {
701         sewd->Remove(i);
702         i--;
703       }
704     }
705
706     TopoDS_Wire ResWire = sfw->Wire();
707     B.Add(aTarget,ResWire);
708   }
709 }