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