Salome HOME
Bugs 20300 and 20361: Error occured during check of geometric coincidence.
[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 <BRep_Tool.hxx>
45 #include <BRep_Builder.hxx>
46 #include <BRepTools.hxx>
47
48 #include <TopExp.hxx>
49 #include <TopExp_Explorer.hxx>
50
51 #include <TopTools_SequenceOfShape.hxx>
52 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
53 #include <TopTools_ListOfShape.hxx>
54 #include <TopTools_ListIteratorOfListOfShape.hxx>
55 #include <TopTools_MapOfShape.hxx>
56 #include <TopTools_MapIteratorOfMapOfShape.hxx>
57
58 #include <TopoDS.hxx>
59 #include <TopoDS_Edge.hxx>
60 #include <TopoDS_Wire.hxx>
61 #include <TopoDS_Face.hxx>
62 #include <TopoDS_Solid.hxx>
63 #include <TopoDS_Vertex.hxx>
64 #include <TopoDS_Shell.hxx>
65 #include <TopoDS_Iterator.hxx>
66
67 #include <TColGeom_HArray2OfSurface.hxx>
68
69 #include <Geom_Plane.hxx>
70 #include <Geom_OffsetSurface.hxx>
71 #include <Geom_CylindricalSurface.hxx>
72 #include <Geom_SphericalSurface.hxx>
73 #include <Geom_Surface.hxx>
74 #include <Geom_Curve.hxx>
75 #include <Geom_RectangularTrimmedSurface.hxx>
76
77 #include <Geom2d_Line.hxx>
78
79 #include <gp_XY.hxx>
80 #include <gp_Pnt2d.hxx>
81
82 //=======================================================================
83 //function : BlockFix_UnionFaces
84 //purpose  :
85 //=======================================================================
86
87 BlockFix_UnionFaces::BlockFix_UnionFaces()
88   : myTolerance(Precision::Confusion()),
89     myOptimumNbFaces(6)
90 {
91 }
92
93
94 //=======================================================================
95 //function : GetTolerance
96 //purpose  :
97 //=======================================================================
98
99 Standard_Real& BlockFix_UnionFaces::GetTolerance()
100 {
101   return myTolerance;
102 }
103
104
105 //=======================================================================
106 //function : GetOptimumNbFaces
107 //purpose  :
108 //=======================================================================
109
110 Standard_Integer& BlockFix_UnionFaces::GetOptimumNbFaces()
111 {
112   return myOptimumNbFaces;
113 }
114
115
116 //=======================================================================
117 //function : AddOrdinaryEdges
118 //purpose  : auxilary
119 //=======================================================================
120 // adds edges from the shape to the sequence
121 // seams and equal edges are dropped
122 // Returns true if one of original edges dropped
123 static Standard_Boolean AddOrdinaryEdges(TopTools_SequenceOfShape& edges,
124                                          const TopoDS_Shape aShape,
125                                          Standard_Integer& anIndex)
126 {
127   //map of edges
128   TopTools_MapOfShape aNewEdges;
129   //add edges without seams
130   for(TopExp_Explorer exp(aShape,TopAbs_EDGE); exp.More(); exp.Next()) {
131     TopoDS_Shape edge = exp.Current();
132     if(aNewEdges.Contains(edge))
133       aNewEdges.Remove(edge);
134     else
135       aNewEdges.Add(edge);
136   }
137
138   Standard_Boolean isDropped = Standard_False;
139   //merge edges and drop seams
140   for(Standard_Integer i = 1; i <= edges.Length(); i++) {
141     TopoDS_Shape current = edges(i);
142     if(aNewEdges.Contains(current)) {
143
144       aNewEdges.Remove(current);
145       edges.Remove(i);
146       i--;
147
148       if(!isDropped) {
149         isDropped = Standard_True;
150         anIndex = i;
151       }
152     }
153   }
154
155   //add edges to the sequemce
156   for(TopTools_MapIteratorOfMapOfShape anIter(aNewEdges); anIter.More(); anIter.Next())
157     edges.Append(anIter.Key());
158
159   return isDropped;
160 }
161
162
163 //=======================================================================
164 //function : ClearRts
165 //purpose  : auxilary
166 //=======================================================================
167 static Handle(Geom_Surface) ClearRts(const Handle(Geom_Surface)& aSurface)
168 {
169   if(aSurface->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
170     Handle(Geom_RectangularTrimmedSurface) rts =
171       Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
172     return rts->BasisSurface();
173   }
174   return aSurface;
175 }
176
177
178 //=======================================================================
179 //function : Perform
180 //purpose  :
181 //=======================================================================
182
183 TopoDS_Shape BlockFix_UnionFaces::Perform(const TopoDS_Shape& Shape)
184 {
185   Handle(ShapeBuild_ReShape) myContext = new ShapeBuild_ReShape;
186   TopoDS_Shape aResShape = myContext->Apply(Shape);
187
188   // processing each solid
189   TopExp_Explorer exps;
190   for (exps.Init(Shape, TopAbs_SOLID); exps.More(); exps.Next()) {
191     TopoDS_Solid aSolid = TopoDS::Solid(exps.Current());
192
193     // creating map of edge faces
194     TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
195     TopExp::MapShapesAndAncestors(aSolid, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
196
197     // map of processed shapes
198     TopTools_MapOfShape aProcessed;
199
200     Handle(ShapeBuild_ReShape) aContext = new ShapeBuild_ReShape;
201
202     Standard_Integer NbModif = 0;
203     Standard_Boolean hasFailed = Standard_False;
204     Standard_Real tol = Min(Max(Precision::Confusion(), myTolerance/10.), 0.1);
205
206     // count faces
207     int nbf = 0;
208     TopExp_Explorer exp;
209     TopTools_MapOfShape mapF;
210     for (exp.Init(aSolid, TopAbs_FACE); exp.More(); exp.Next()) {
211       if (mapF.Add(exp.Current()))
212         nbf++;
213     }
214
215     bool doUnion = ((myOptimumNbFaces == 0) ||
216                     ((myOptimumNbFaces > 0) && (nbf > myOptimumNbFaces)));
217
218     // processing each face
219     mapF.Clear();
220     for (exp.Init(aSolid, TopAbs_FACE); exp.More() && doUnion; exp.Next()) {
221       TopoDS_Face aFace = TopoDS::Face(exp.Current().Oriented(TopAbs_FORWARD));
222
223       if (aProcessed.Contains(aFace))
224         continue;
225
226       Standard_Integer dummy;
227       TopTools_SequenceOfShape edges;
228       AddOrdinaryEdges(edges,aFace,dummy);
229
230       TopTools_SequenceOfShape faces;
231       faces.Append(aFace);
232
233       //surface and location to construct result
234       TopLoc_Location aBaseLocation;
235       Handle(Geom_Surface) aBaseSurface = BRep_Tool::Surface(aFace,aBaseLocation);
236       aBaseSurface = ClearRts(aBaseSurface);
237
238       // find adjacent faces to union
239       Standard_Integer i;
240       for (i = 1; i <= edges.Length(); i++) {
241         TopoDS_Edge edge = TopoDS::Edge(edges(i));
242         if (BRep_Tool::Degenerated(edge))
243           continue;
244
245         const TopTools_ListOfShape& aList = aMapEdgeFaces.FindFromKey(edge);
246         TopTools_ListIteratorOfListOfShape anIter(aList);
247         for (; anIter.More(); anIter.Next()) {
248           TopoDS_Face anCheckedFace = TopoDS::Face(anIter.Value().Oriented(TopAbs_FORWARD));
249           if (anCheckedFace.IsSame(aFace))
250             continue;
251
252           if (aProcessed.Contains(anCheckedFace))
253             continue;
254
255           if (IsSameDomain(aFace,anCheckedFace)) {
256
257             if (aList.Extent() != 2) {
258               // non mainfold case is not processed
259               continue;
260             }
261
262             // replacing pcurves
263             TopoDS_Face aMockUpFace;
264             BRep_Builder B;
265             B.MakeFace(aMockUpFace,aBaseSurface,aBaseLocation,0.);
266             MovePCurves(aMockUpFace,anCheckedFace);
267
268             if (AddOrdinaryEdges(edges,aMockUpFace,dummy)) {
269               // sequence edges is modified
270               i = dummy;
271             }
272
273             faces.Append(anCheckedFace);
274             aProcessed.Add(anCheckedFace);
275             break;
276           }
277         }
278       }
279
280       // all faces collected in the sequence. Perform union of faces
281       if (faces.Length() > 1) {
282         NbModif++;
283         TopoDS_Face aResult;
284         BRep_Builder B;
285         B.MakeFace(aResult,aBaseSurface,aBaseLocation,0);
286         Standard_Integer nbWires = 0;
287
288         // connecting wires
289         while (edges.Length()>0) {
290
291           Standard_Boolean isEdge3d = Standard_False;
292           nbWires++;
293           TopTools_MapOfShape aVertices;
294           TopoDS_Wire aWire;
295           B.MakeWire(aWire);
296
297           TopoDS_Edge anEdge = TopoDS::Edge(edges(1));
298           edges.Remove(1);
299
300           isEdge3d |= !BRep_Tool::Degenerated(anEdge);
301           B.Add(aWire,anEdge);
302           TopoDS_Vertex V1,V2;
303           TopExp::Vertices(anEdge,V1,V2);
304           aVertices.Add(V1);
305           aVertices.Add(V2);
306
307           Standard_Boolean isNewFound = Standard_False;
308           do {
309             isNewFound = Standard_False;
310             for(Standard_Integer j = 1; j <= edges.Length(); j++) {
311               anEdge = TopoDS::Edge(edges(j));
312               TopExp::Vertices(anEdge,V1,V2);
313               if(aVertices.Contains(V1) || aVertices.Contains(V2)) {
314                 isEdge3d |= !BRep_Tool::Degenerated(anEdge);
315                 aVertices.Add(V1);
316                 aVertices.Add(V2);
317                 B.Add(aWire,anEdge);
318                 edges.Remove(j);
319                 j--;
320                 isNewFound = Standard_True;
321               }
322             }
323           } while (isNewFound);
324
325           // sorting any type of edges
326           aWire = TopoDS::Wire(aContext->Apply(aWire));
327
328           TopoDS_Face tmpF = TopoDS::Face(aContext->Apply(faces(1).Oriented(TopAbs_FORWARD)));
329           Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire(aWire,tmpF,Precision::Confusion());
330           sfw->FixReorder();
331           Standard_Boolean isDegRemoved = Standard_False;
332           if(!sfw->StatusReorder ( ShapeExtend_FAIL )) {
333             // clear degenerated edges if at least one with 3d curve exist
334             if(isEdge3d) {
335               Handle(ShapeExtend_WireData) sewd = sfw->WireData();
336               for(Standard_Integer j = 1; j<=sewd->NbEdges();j++) {
337                 TopoDS_Edge E = sewd->Edge(j);
338                 if(BRep_Tool::Degenerated(E)) {
339                   sewd->Remove(j);
340                   isDegRemoved = Standard_True;
341                   j--;
342                 }
343               }
344             }
345             sfw->FixShifted();
346             if(isDegRemoved)
347               sfw->FixDegenerated();
348           }
349           TopoDS_Wire aWireFixed = sfw->Wire();
350           aContext->Replace(aWire,aWireFixed);
351           // add resulting wire
352           if(isEdge3d) {
353             B.Add(aResult,aWireFixed);
354           }
355           else  {
356             // sorting edges
357             Handle(ShapeExtend_WireData) sbwd = sfw->WireData();
358             Standard_Integer nbEdges = sbwd->NbEdges();
359             // sort degenerated edges and create one edge instead of several ones
360             ShapeAnalysis_WireOrder sawo(Standard_False, 0);
361             ShapeAnalysis_Edge sae;
362             Standard_Integer aLastEdge = nbEdges;
363             for(Standard_Integer j = 1; j <= nbEdges; j++) {
364               Standard_Real f,l;
365               //smh protection on NULL pcurve
366               Handle(Geom2d_Curve) c2d;
367               if(!sae.PCurve(sbwd->Edge(j),tmpF,c2d,f,l)) {
368                 aLastEdge--;
369                 continue;
370               }
371               sawo.Add(c2d->Value(f).XY(),c2d->Value(l).XY());
372             }
373             sawo.Perform();
374
375             // constructind one degenerative edge
376             gp_XY aStart, anEnd, tmp;
377             Standard_Integer nbFirst = sawo.Ordered(1);
378             TopoDS_Edge anOrigE = TopoDS::Edge(sbwd->Edge(nbFirst).Oriented(TopAbs_FORWARD));
379             ShapeBuild_Edge sbe;
380             TopoDS_Vertex aDummyV;
381             TopoDS_Edge E = sbe.CopyReplaceVertices(anOrigE,aDummyV,aDummyV);
382             sawo.XY(nbFirst,aStart,tmp);
383             sawo.XY(sawo.Ordered(aLastEdge),tmp,anEnd);
384
385             gp_XY aVec = anEnd-aStart;
386             Handle(Geom2d_Line) aLine = new Geom2d_Line(aStart,gp_Dir2d(anEnd-aStart));
387
388             B.UpdateEdge(E,aLine,tmpF,0.);
389             B.Range(E,tmpF,0.,aVec.Modulus());
390             Handle(Geom_Curve) C3d;
391             B.UpdateEdge(E,C3d,0.);
392             B.Degenerated(E,Standard_True);
393             TopoDS_Wire aW;
394             B.MakeWire(aW);
395             B.Add(aW,E);
396             B.Add(aResult,aW);
397           }
398         }
399
400         // perform substitution of face
401         aContext->Replace(aContext->Apply(aFace),aResult);
402
403         ShapeFix_Face sff (aResult);
404         //Intializing by tolerances
405         sff.SetPrecision(myTolerance);
406         sff.SetMinTolerance(tol);
407         sff.SetMaxTolerance(Max(1.,myTolerance*1000.));
408         //Setting modes
409         sff.FixOrientationMode() = 0;
410         //sff.FixWireMode() = 0;
411         sff.SetContext(aContext);
412         // Applying the fixes
413         sff.Perform();
414         if(sff.Status(ShapeExtend_FAIL))
415         hasFailed = Standard_True;
416
417         // breaking down to several faces
418         TopoDS_Shape theResult = aContext->Apply(aResult);
419         for (TopExp_Explorer aFaceExp (theResult,TopAbs_FACE); aFaceExp.More(); aFaceExp.Next()) {
420           TopoDS_Face aCurrent = TopoDS::Face(aFaceExp.Current().Oriented(TopAbs_FORWARD));
421           Handle(TColGeom_HArray2OfSurface) grid = new TColGeom_HArray2OfSurface ( 1, 1, 1, 1 );
422           grid->SetValue ( 1, 1, aBaseSurface );
423           Handle(ShapeExtend_CompositeSurface) G = new ShapeExtend_CompositeSurface ( grid );
424           ShapeFix_ComposeShell CompShell;
425           CompShell.Init ( G, aBaseLocation, aCurrent, ::Precision::Confusion() );//myPrecision
426           CompShell.SetContext( aContext );
427
428           TopTools_SequenceOfShape parts;
429           ShapeFix_SequenceOfWireSegment wires;
430           for(TopExp_Explorer W_Exp(aCurrent,TopAbs_WIRE);W_Exp.More();W_Exp.Next()) {
431             Handle(ShapeExtend_WireData) sbwd =
432               new ShapeExtend_WireData ( TopoDS::Wire(W_Exp.Current() ));
433             ShapeFix_WireSegment seg ( sbwd, TopAbs_REVERSED );
434             wires.Append(seg);
435           }
436
437           CompShell.DispatchWires ( parts,wires );
438           for (Standard_Integer j=1; j <= parts.Length(); j++ ) {
439             ShapeFix_Face aFixOrient(TopoDS::Face(parts(j)));
440             aFixOrient.SetContext(aContext);
441             aFixOrient.FixOrientation();
442           }
443
444           TopoDS_Shape CompRes;
445           if ( faces.Length() !=1 ) {
446             TopoDS_Shell S;
447             B.MakeShell ( S );
448             for ( i=1; i <= parts.Length(); i++ )
449               B.Add ( S, parts(i) );
450             CompRes = S;
451           }
452           else CompRes = parts(1);
453
454           aContext->Replace(aCurrent,CompRes);
455         }
456
457         // remove the remaining faces
458         for(i = 2; i <= faces.Length(); i++)
459           aContext->Remove(faces(i));
460       }
461     } // end processing each face
462
463     //TopoDS_Shape aResult = Shape;
464     if (NbModif > 0 && !hasFailed) {
465       TopoDS_Shape aResult = aContext->Apply(aSolid);
466
467       ShapeFix_Edge sfe;
468       for (exp.Init(aResult,TopAbs_EDGE); exp.More(); exp.Next()) {
469         TopoDS_Edge E = TopoDS::Edge(exp.Current());
470         sfe.FixVertexTolerance (E);
471         // ptv add fix same parameter
472         sfe.FixSameParameter(E, myTolerance);
473       }
474
475       myContext->Replace(aSolid, aResult);
476     }
477     //else
478     {
479       for (exp.Init(aSolid, TopAbs_FACE); exp.More(); exp.Next()) {
480         TopoDS_Face aFace = TopoDS::Face(exp.Current().Oriented(TopAbs_FORWARD));
481         Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire;
482         sfw->SetContext(myContext);
483         sfw->SetPrecision(myTolerance);
484         sfw->SetMinTolerance(myTolerance);
485         sfw->SetMaxTolerance(Max(1.,myTolerance*1000.));
486         sfw->SetFace(aFace);
487         for (TopoDS_Iterator iter (aFace,Standard_False); iter.More(); iter.Next()) {
488           TopoDS_Wire wire = TopoDS::Wire(iter.Value());
489           sfw->Load(wire);
490           sfw->FixReorder();
491           sfw->FixShifted();
492         }
493       }
494     }
495   } // end processing each solid
496
497   aResShape = myContext->Apply(Shape);
498   return aResShape;
499 }
500
501
502 //=======================================================================
503 //function : IsSameDomain
504 //purpose  :
505 //=======================================================================
506
507 Standard_Boolean BlockFix_UnionFaces::IsSameDomain(const TopoDS_Face& aFace,
508                                                    const TopoDS_Face& aCheckedFace) const
509 {
510   //checking the same handless
511   TopLoc_Location L1, L2;
512   Handle(Geom_Surface) S1, S2;
513
514   S1 = BRep_Tool::Surface(aFace,L1);
515   S2 = BRep_Tool::Surface(aCheckedFace,L2);
516
517   if (S1 == S2 && L1 == L2)
518     return true;
519
520   // begin: planar case (improvement 20052)
521   S1 = BRep_Tool::Surface(aFace);
522   S2 = BRep_Tool::Surface(aCheckedFace);
523
524   Handle(Geom_Plane) aGP1, aGP2;
525   Handle(Geom_RectangularTrimmedSurface) aGRTS1, aGRTS2;
526   Handle(Geom_OffsetSurface) aGOFS1, aGOFS2;
527
528   aGRTS1 = Handle(Geom_RectangularTrimmedSurface)::DownCast(S1);
529   aGRTS2 = Handle(Geom_RectangularTrimmedSurface)::DownCast(S2);
530
531   aGOFS1 = Handle(Geom_OffsetSurface)::DownCast(S1);
532   aGOFS2 = Handle(Geom_OffsetSurface)::DownCast(S2);
533
534   if (!aGOFS1.IsNull()) {
535     aGP1 = Handle(Geom_Plane)::DownCast(aGOFS1->BasisSurface());
536   }
537   else if (!aGRTS1.IsNull()) {
538     aGP1 = Handle(Geom_Plane)::DownCast(aGRTS1->BasisSurface());
539   }
540   else {
541     aGP1 = Handle(Geom_Plane)::DownCast(S1);
542   }
543
544   if (!aGOFS2.IsNull()) {
545     aGP2 = Handle(Geom_Plane)::DownCast(aGOFS2->BasisSurface());
546   }
547   else if (!aGRTS2.IsNull()) {
548     aGP2 = Handle(Geom_Plane)::DownCast(aGRTS2->BasisSurface());
549   }
550   else {
551     aGP2 = Handle(Geom_Plane)::DownCast(S2);
552   }
553
554   if (!aGP1.IsNull() && !aGP2.IsNull()) {
555     // both surfaces are planar, check equality
556     Standard_Real A1, B1, C1, D1;
557     Standard_Real A2, B2, C2, D2;
558     aGP1->Coefficients(A1, B1, C1, D1);
559     aGP2->Coefficients(A2, B2, C2, D2);
560
561     if (fabs(A1) > Precision::Confusion()) {
562       A1 = 1.0;
563       B1 /= A1;
564       C1 /= A1;
565       D1 /= A1;
566     }
567     else if (fabs(B1) > Precision::Confusion()) {
568       B1 = 1.0;
569       C1 /= B1;
570       D1 /= B1;
571     }
572     else {
573       C1 = 1.0;
574       D1 /= C1;
575     }
576
577     if (fabs(A2) > Precision::Confusion()) {
578       A2 = 1.0;
579       B2 /= A2;
580       C2 /= A2;
581       D2 /= A2;
582     }
583     else if (fabs(B2) > Precision::Confusion()) {
584       B2 = 1.0;
585       C2 /= B2;
586       D2 /= B2;
587     }
588     else {
589       C2 = 1.0;
590       D2 /= C2;
591     }
592
593     if (fabs(A1 - A2) < Precision::Confusion() &&
594         fabs(B1 - B2) < Precision::Confusion() &&
595         fabs(C1 - C2) < Precision::Confusion() &&
596         fabs(D1 - D2) < Precision::Confusion())
597       return true;
598   }
599   // end: planar case (improvement 20052)
600
601   return false;
602 }
603
604
605 //=======================================================================
606 //function : MovePCurves
607 //purpose  :
608 //=======================================================================
609
610 void BlockFix_UnionFaces::MovePCurves(TopoDS_Face& aTarget,
611                                       const TopoDS_Face& aSource) const
612 {
613   BRep_Builder B;
614   for(TopExp_Explorer wexp(aSource,TopAbs_WIRE);wexp.More();wexp.Next()) {
615     Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire(TopoDS::Wire(wexp.Current()),
616                                                   aTarget, Precision::Confusion());
617     sfw->FixReorder();
618     Standard_Boolean isReoredFailed = sfw->StatusReorder ( ShapeExtend_FAIL );
619     sfw->FixEdgeCurves();
620     if(isReoredFailed)
621       continue;
622
623     sfw->FixShifted();
624     sfw->FixDegenerated();
625
626     // remove degenerated edges from not degenerated points
627     ShapeAnalysis_Edge sae;
628     Handle(ShapeExtend_WireData) sewd = sfw->WireData();
629     for(Standard_Integer i = 1; i<=sewd->NbEdges();i++) {
630       TopoDS_Edge E = sewd->Edge(i);
631       if(BRep_Tool::Degenerated(E)&&!sae.HasPCurve(E,aTarget)) {
632         sewd->Remove(i);
633         i--;
634       }
635     }
636
637     TopoDS_Wire ResWire = sfw->Wire();
638     B.Add(aTarget,ResWire);
639   }
640 }