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