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