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