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