Salome HOME
0022178: [CEA 798] Sewing: Make option NonManifoldMode available in GEOM
[modules/geom.git] / src / GEOMImpl / GEOMImpl_HealingDriver.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <Standard_Stream.hxx>
24
25 #include <GEOMImpl_HealingDriver.hxx>
26 #include <GEOMImpl_Types.hxx>
27 #include <GEOMImpl_IHealing.hxx>
28 #include <GEOM_Function.hxx>
29
30 #include <GEOMImpl_GlueDriver.hxx>
31 #include <GEOMImpl_ShapeDriver.hxx>
32
33 #include <ShHealOper_ShapeProcess.hxx>
34 #include <ShHealOper_RemoveFace.hxx>
35 #include <ShHealOper_CloseContour.hxx>
36 #include <ShHealOper_RemoveInternalWires.hxx>
37 #include <ShHealOper_FillHoles.hxx>
38 #include <ShHealOper_Sewing.hxx>
39 #include <ShHealOper_EdgeDivide.hxx>
40 #include <ShHealOper_ChangeOrientation.hxx>
41
42 #include <TNaming_CopyShape.hxx>
43
44 #include <ShapeFix_ShapeTolerance.hxx>
45 #include <ShapeFix_Shape.hxx>
46
47 #include <BRep_Builder.hxx>
48 #include <BRepAdaptor_Curve.hxx>
49 #include <BRepCheck_Analyzer.hxx>
50 #include <BRepTools_WireExplorer.hxx>
51
52 #include <TopExp.hxx>
53 #include <TopExp_Explorer.hxx>
54 #include <TopoDS.hxx>
55 #include <TopoDS_Iterator.hxx>
56 #include <TopTools_IndexedMapOfShape.hxx>
57 #include <TopTools_ListOfShape.hxx>
58 #include <TopTools_ListIteratorOfListOfShape.hxx>
59
60 #include <TColStd_IndexedDataMapOfTransientTransient.hxx>
61
62 #include <Precision.hxx>
63
64 #include <StdFail_NotDone.hxx>
65 #include <Standard_NullObject.hxx>
66
67 //=======================================================================
68 //function :  raiseNotDoneExeption
69 //purpose  :  global function: forms error message and raises exeption
70 //=======================================================================
71 void raiseNotDoneExeption( const int theErrorStatus )
72 {
73   switch ( theErrorStatus )
74   {
75   case ShHealOper_NotError:           StdFail_NotDone::Raise( "ShHealOper_NotError_msg" );
76   case ShHealOper_InvalidParameters:  StdFail_NotDone::Raise( "ShHealOper_InvalidParameters_msg" );
77   case ShHealOper_ErrorExecution:
78   default:                            StdFail_NotDone::Raise( "ShHealOper_ErrorExecution_msg" );
79   }
80 }
81
82 //=======================================================================
83 //function : GetID
84 //purpose  :
85 //=======================================================================
86 const Standard_GUID& GEOMImpl_HealingDriver::GetID()
87 {
88   static Standard_GUID aHealingDriver("FF1BBB61-5D14-4df2-980B-3A668264EA16");
89   return aHealingDriver;
90 }
91
92 //=======================================================================
93 //function : GEOMImpl_HealingDriver
94 //purpose  :
95 //=======================================================================
96 GEOMImpl_HealingDriver::GEOMImpl_HealingDriver()
97 {
98 }
99
100 //=======================================================================
101 //function : Execute
102 //purpose  :
103 //=======================================================================
104 Standard_Integer GEOMImpl_HealingDriver::Execute(TFunction_Logbook& log) const
105 {
106   if (Label().IsNull()) return 0;
107   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
108
109   if (aFunction.IsNull()) return 0;
110
111   GEOMImpl_IHealing HI (aFunction);
112   Standard_Integer aType = aFunction->GetType();
113   Handle(GEOM_Function) anOriginalFunction = HI.GetOriginal();
114   if (anOriginalFunction.IsNull()) return 0;
115   TopoDS_Shape aShape, anOriginalShape = anOriginalFunction->GetValue();
116   if (anOriginalShape.IsNull()) return 0;
117
118   switch (aType)
119   {
120   case SHAPE_PROCESS:
121     ShapeProcess(&HI, anOriginalShape, aShape);
122     break;
123   case SUPPRESS_FACES:
124     SuppressFaces(&HI, anOriginalShape, aShape);
125     break;
126   case CLOSE_CONTOUR:
127     CloseContour(&HI, anOriginalShape, aShape);
128     break;
129   case REMOVE_INT_WIRES:
130     RemoveIntWires(&HI, anOriginalShape, aShape);
131     break;
132   case FILL_HOLES:
133     RemoveHoles(&HI, anOriginalShape, aShape);
134     break;
135   case SEWING:
136     Sew(&HI, anOriginalShape, aShape, false);
137     break;
138   case SEWING_NON_MANIFOLD:
139     Sew(&HI, anOriginalShape, aShape, true);
140     break;
141   case DIVIDE_EDGE:
142     AddPointOnEdge(&HI, anOriginalShape, aShape);
143     break;
144   case CHANGE_ORIENTATION:
145     ChangeOrientation(&HI, anOriginalShape, aShape);
146     break;
147   case LIMIT_TOLERANCE:
148     LimitTolerance(&HI, anOriginalShape, aShape);
149     break;
150   case FUSE_COLLINEAR_EDGES:
151     {
152       Handle(TColStd_HSequenceOfTransient) aVerts = HI.GetShapes();
153       FuseCollinearEdges(anOriginalShape, aVerts, aShape);
154     }
155     break;
156   default:
157     return 0;
158   }
159
160   if (aShape.IsNull())
161     raiseNotDoneExeption( ShHealOper_ErrorExecution );
162
163   aFunction->SetValue(aShape);
164
165   log.SetTouched(Label());
166   return 1;
167 }
168
169 //=======================================================================
170 //function :  ShapeProcess
171 //purpose  :
172 //=======================================================================
173 Standard_Boolean GEOMImpl_HealingDriver::ShapeProcess (GEOMImpl_IHealing* theHI,
174                                                        const TopoDS_Shape& theOriginalShape,
175                                                        TopoDS_Shape& theOutShape) const
176 {
177   Handle(TColStd_HArray1OfExtendedString) anOperators = theHI->GetOperators();
178   Handle(TColStd_HArray1OfExtendedString) aParams = theHI->GetParameters();
179   Handle(TColStd_HArray1OfExtendedString) aValues = theHI->GetValues();
180
181   if (anOperators.IsNull() || anOperators->Length() <= 0)
182     return Standard_False;
183
184   Standard_Integer nbParams = 0, nbValues = 0;
185   if (!aParams.IsNull()) {
186     nbParams = aParams->Length();
187   }
188   if (!aValues.IsNull()) {
189     nbValues = aValues->Length();
190   }
191   if (nbParams != nbValues)
192     return Standard_False;
193
194   ShHealOper_ShapeProcess aHealer;
195   TColStd_SequenceOfAsciiString anOperatorsAS, aParamsAS, aValuesAS;
196   int i;
197   for (i = 1; i <= anOperators->Length(); i++)
198     anOperatorsAS.Append(TCollection_AsciiString(anOperators->Value(i)));
199
200   aHealer.SetOperators(anOperatorsAS);
201
202   for (i = 1; i <= nbParams; i++) {
203     aHealer.SetParameter(TCollection_AsciiString(aParams->Value(i)),
204                          TCollection_AsciiString(aValues->Value(i)));
205   }
206
207   aHealer.Perform(theOriginalShape, theOutShape);
208
209   if (!aHealer.isDone())
210     raiseNotDoneExeption( ShHealOper_NotError );
211
212   return Standard_True;
213 }
214
215 //=======================================================================
216 //function :  SupressFaces
217 //purpose  :
218 //=======================================================================
219 void SuppressFacesRec (const TopTools_SequenceOfShape& theShapesFaces,
220                        const TopoDS_Shape&             theOriginalShape,
221                        TopoDS_Shape&                   theOutShape)
222 {
223   if ((theOriginalShape.ShapeType() != TopAbs_COMPOUND &&
224        theOriginalShape.ShapeType() != TopAbs_COMPSOLID))
225   {
226     ShHealOper_RemoveFace aHealer (theOriginalShape);
227     Standard_Boolean aResult = aHealer.Perform(theShapesFaces);
228
229     if (aResult)
230       theOutShape = aHealer.GetResultShape();
231     else
232       raiseNotDoneExeption(aHealer.GetErrorStatus());
233   }
234   else
235   {
236     BRep_Builder BB;
237     TopoDS_Compound CC;
238     BB.MakeCompound(CC);
239
240     TopTools_MapOfShape mapShape;
241     TopoDS_Iterator It (theOriginalShape, Standard_True, Standard_True);
242
243     for (; It.More(); It.Next()) {
244       TopoDS_Shape aShape_i = It.Value();
245       if (mapShape.Add(aShape_i)) {
246         // check, if current shape contains at least one of faces to be removed
247         bool isFound = false;
248         TopTools_IndexedMapOfShape aShapes_i;
249         TopExp::MapShapes(aShape_i, aShapes_i);
250         for (int i = 1; i <= theShapesFaces.Length() && !isFound; i++) {
251           const TopoDS_Shape& aFace_i = theShapesFaces.Value(i);
252           if (aShapes_i.Contains(aFace_i)) isFound = true;
253         }
254         if (isFound) {
255           TopoDS_Shape anOutSh_i;
256           SuppressFacesRec(theShapesFaces, aShape_i, anOutSh_i);
257           if ( !anOutSh_i.IsNull() )
258             BB.Add(CC, anOutSh_i);
259         }
260         else {
261           // nothing to do
262           BB.Add(CC, aShape_i);
263         }
264       }
265     }
266     theOutShape = CC;
267   }
268 }
269
270 Standard_Boolean GEOMImpl_HealingDriver::SuppressFaces (GEOMImpl_IHealing* theHI,
271                                                         const TopoDS_Shape& theOriginalShape,
272                                                         TopoDS_Shape& theOutShape) const
273 {
274   Handle(TColStd_HArray1OfInteger) aFaces = theHI->GetFaces();
275
276   Standard_Boolean aResult = Standard_False;
277
278   if (aFaces.IsNull()) {
279     ShHealOper_RemoveFace aHealer (theOriginalShape);
280     aResult = aHealer.Perform();
281
282     if (aResult)
283       theOutShape = aHealer.GetResultShape();
284     else
285       raiseNotDoneExeption(aHealer.GetErrorStatus());
286   }
287   else {
288     TopTools_SequenceOfShape aShapesFaces;
289     TopTools_IndexedMapOfShape aShapes;
290     TopExp::MapShapes(theOriginalShape, aShapes);
291     for (int i = 1; i <= aFaces->Length(); i++) {
292       int indexOfFace = aFaces->Value(i);
293       TopoDS_Shape aFace = aShapes.FindKey(indexOfFace);
294       aShapesFaces.Append(aFace);
295     }
296     SuppressFacesRec(aShapesFaces, theOriginalShape, theOutShape);
297     if ((theOriginalShape.ShapeType() == TopAbs_COMPOUND ||
298          theOriginalShape.ShapeType() == TopAbs_COMPSOLID)) {
299       TopoDS_Shape aSh = theOutShape;
300       theOutShape = GEOMImpl_GlueDriver::GlueFaces(aSh, Precision::Confusion(), Standard_True);
301     }
302   }
303
304   return Standard_True;
305 }
306
307 //=======================================================================
308 //function :  CloseContour
309 //purpose  :
310 //=======================================================================
311 Standard_Boolean GEOMImpl_HealingDriver::CloseContour (GEOMImpl_IHealing* theHI,
312                                                        const TopoDS_Shape& theOriginalShape,
313                                                        TopoDS_Shape& theOutShape) const
314 {
315   Standard_Boolean isByVertex = theHI->GetIsCommonVertex();
316   Handle(TColStd_HArray1OfInteger) aWires = theHI->GetWires();
317
318   ShHealOper_CloseContour aHealer (theOriginalShape);
319
320   Standard_Boolean aResult = Standard_False;
321   if ( aWires.IsNull() ) {
322     if ( theOriginalShape.ShapeType() == TopAbs_WIRE )
323       aResult = aHealer.Perform(TopoDS::Wire(theOriginalShape), isByVertex, !isByVertex);
324   }
325   else {
326     TopTools_SequenceOfShape aShapesWires;
327     TopTools_IndexedMapOfShape aShapes;
328     TopExp::MapShapes(theOriginalShape, aShapes);
329     for (int i = 1; i <= aWires->Length(); i++) {
330       int indexOfWire = aWires->Value(i);
331       TopoDS_Shape aWire = aShapes.FindKey(indexOfWire);
332       aShapesWires.Append(aWire);
333     }
334
335     aResult = aHealer.Perform( aShapesWires, isByVertex, !isByVertex );
336   }
337
338   if (aResult)
339     theOutShape = aHealer.GetResultShape();
340   else
341     raiseNotDoneExeption( aHealer.GetErrorStatus() );
342
343   return aResult;
344 }
345
346 //=======================================================================
347 //function :  RemoveIntWires
348 //purpose  :
349 //=======================================================================
350 Standard_Boolean GEOMImpl_HealingDriver::RemoveIntWires (GEOMImpl_IHealing* theHI,
351                                                          const TopoDS_Shape& theOriginalShape,
352                                                          TopoDS_Shape& theOutShape) const
353 {
354   Handle(TColStd_HArray1OfInteger) aWires = theHI->GetWires();
355
356   ShHealOper_RemoveInternalWires aHealer(theOriginalShape);
357
358   Standard_Boolean aResult = Standard_False;
359   if (aWires.IsNull()) { // remove all faces
360     aResult = aHealer.Remove();
361   } else {
362     TopTools_SequenceOfShape aShapesWires;
363     TopTools_IndexedMapOfShape aShapes;
364     TopExp::MapShapes(theOriginalShape, aShapes);
365     for (int i = 1; i <= aWires->Length(); i++) {
366       int indexOfWire = aWires->Value(i);
367       TopoDS_Shape aWire = aShapes.FindKey(indexOfWire);
368       aShapesWires.Append(aWire);
369     }
370
371     aResult = aHealer.Remove(aShapesWires);
372   }
373
374   if (aResult)
375     theOutShape = aHealer.GetResultShape();
376   else
377     raiseNotDoneExeption( aHealer.GetErrorStatus() );
378
379   return aResult;
380 }
381
382 //=======================================================================
383 //function :  RemoveHoles
384 //purpose  :
385 //=======================================================================
386 Standard_Boolean GEOMImpl_HealingDriver::RemoveHoles (GEOMImpl_IHealing* theHI,
387                                                       const TopoDS_Shape& theOriginalShape,
388                                                       TopoDS_Shape& theOutShape) const
389 {
390   Handle(TColStd_HArray1OfInteger) aWires = theHI->GetWires();
391
392   ShHealOper_FillHoles aHealer (theOriginalShape);
393
394   Standard_Boolean aResult = Standard_False;
395   if (aWires.IsNull()) { // remove all faces
396     aResult = aHealer.Fill();
397   } else {
398     TopTools_SequenceOfShape aShapesWires;
399     TopTools_IndexedMapOfShape aShapes;
400     TopExp::MapShapes(theOriginalShape, aShapes);
401     for (int i = 1; i <= aWires->Length(); i++) {
402       int indexOfWire = aWires->Value(i);
403       TopoDS_Shape aWire = aShapes.FindKey(indexOfWire);
404       aShapesWires.Append(aWire);
405     }
406
407     aResult = aHealer.Fill(aShapesWires);
408   }
409
410   if (aResult)
411     theOutShape = aHealer.GetResultShape();
412   else
413     raiseNotDoneExeption( aHealer.GetErrorStatus() );
414
415   return aResult;
416 }
417
418 //=======================================================================
419 //function :  Sew
420 //purpose  :
421 //=======================================================================
422 Standard_Boolean GEOMImpl_HealingDriver::Sew (GEOMImpl_IHealing* theHI,
423                                               const TopoDS_Shape& theOriginalShape,
424                                               TopoDS_Shape& theOutShape,
425                                               Standard_Boolean isAllowNonManifold) const
426 {
427   Standard_Real aTol = theHI->GetTolerance();
428
429   ShHealOper_Sewing aHealer (theOriginalShape, aTol);
430
431   // Set non-manifold mode.
432   aHealer.SetNonManifoldMode(isAllowNonManifold);
433
434   Standard_Boolean aResult = aHealer.Perform();
435
436   if (aResult)
437     theOutShape = aHealer.GetResultShape();
438   else
439     raiseNotDoneExeption( aHealer.GetErrorStatus() );
440
441   return aResult;
442 }
443
444 //=======================================================================
445 //function :  AddPointOnEdge
446 //purpose  :
447 //=======================================================================
448 Standard_Boolean GEOMImpl_HealingDriver::AddPointOnEdge (GEOMImpl_IHealing* theHI,
449                                                          const TopoDS_Shape& theOriginalShape,
450                                                          TopoDS_Shape& theOutShape) const
451 {
452   Standard_Boolean isByParameter = theHI->GetIsByParameter();
453   Standard_Integer anIndex = theHI->GetIndex();
454   Standard_Real aValue = theHI->GetDevideEdgeValue();
455
456   ShHealOper_EdgeDivide aHealer (theOriginalShape);
457
458   Standard_Boolean aResult = Standard_False;
459   if (anIndex == -1) { // apply algorythm for the whole shape which is EDGE
460     if (theOriginalShape.ShapeType() == TopAbs_EDGE)
461       aResult = aHealer.Perform(TopoDS::Edge(theOriginalShape), aValue, isByParameter);
462   } else {
463     TopTools_IndexedMapOfShape aShapes;
464     TopExp::MapShapes(theOriginalShape, aShapes);
465     TopoDS_Shape aEdgeShape = aShapes.FindKey(anIndex);
466     if (aEdgeShape.ShapeType() == TopAbs_EDGE)
467       aResult = aHealer.Perform(TopoDS::Edge(aEdgeShape), aValue, isByParameter);
468   }
469
470   if (aResult)
471     theOutShape = aHealer.GetResultShape();
472   else
473     raiseNotDoneExeption( aHealer.GetErrorStatus() );
474
475   return aResult;
476 }
477
478
479 //=======================================================================
480 //function :  ChangeOrientation
481 //purpose  :
482 //=======================================================================
483 Standard_Boolean GEOMImpl_HealingDriver::ChangeOrientation (GEOMImpl_IHealing* theHI,
484                                                             const TopoDS_Shape& theOriginalShape,
485                                                             TopoDS_Shape& theOutShape) const
486 {
487   ShHealOper_ChangeOrientation aHealer (theOriginalShape);
488
489   Standard_Boolean aResult = aHealer.Perform();
490
491   if (aResult)
492     theOutShape = aHealer.GetResultShape();
493   else
494     raiseNotDoneExeption( aHealer.GetErrorStatus() );
495
496   return aResult;
497 }
498
499 //=======================================================================
500 //function : LimitTolerance
501 //purpose  :
502 //=======================================================================
503 void GEOMImpl_HealingDriver::LimitTolerance (GEOMImpl_IHealing* theHI,
504                                              const TopoDS_Shape& theOriginalShape,
505                                              TopoDS_Shape& theOutShape) const
506 {
507   Standard_Real aTol = theHI->GetTolerance();
508   if (aTol < Precision::Confusion())
509     aTol = Precision::Confusion();
510
511   // 1. Make a copy to prevent the original shape changes.
512   TopoDS_Shape aShapeCopy;
513   TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
514   TNaming_CopyShape::CopyTool(theOriginalShape, aMapTShapes, aShapeCopy);
515
516   // 2. Limit tolerance.
517   ShapeFix_ShapeTolerance aSFT;
518   aSFT.LimitTolerance(aShapeCopy, aTol, aTol, TopAbs_SHAPE);
519
520   // 3. Fix obtained shape.
521   Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape (aShapeCopy);
522   aSfs->Perform();
523   theOutShape = aSfs->Shape();
524
525   BRepCheck_Analyzer ana (theOutShape, Standard_True);
526   if (!ana.IsValid())
527     StdFail_NotDone::Raise("Non valid shape result");
528 }
529
530 //=======================================================================
531 //function : FuseCollinearEdges
532 //purpose  :
533 //=======================================================================
534 void GEOMImpl_HealingDriver::FuseCollinearEdges (const TopoDS_Shape& theOriginalShape,
535                                                  const Handle(TColStd_HSequenceOfTransient)& aVerts,
536                                                  TopoDS_Shape& theOutShape)
537 {
538   if (theOriginalShape.ShapeType() != TopAbs_WIRE)
539     Standard_TypeMismatch::Raise("Not a wire is given");
540
541   // Tolerances
542   Standard_Real AngTol = Precision::Angular();
543   Standard_Real LinTol = Precision::Confusion();
544   Standard_Real tol;
545   for (TopExp_Explorer ExV (theOriginalShape, TopAbs_VERTEX); ExV.More(); ExV.Next()) {
546     TopoDS_Vertex Vertex = TopoDS::Vertex(ExV.Current());
547     tol = BRep_Tool::Tolerance(Vertex);
548     if (tol > LinTol)
549       LinTol = tol;
550   }
551
552   // 1. Make a copy to prevent the original shape changes.
553   TopoDS_Shape aWire;
554   TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
555   TNaming_CopyShape::CopyTool(theOriginalShape, aMapTShapes, aWire);
556   TopoDS_Wire theWire = TopoDS::Wire(aWire);
557
558   // 2. Sub-shapes of the wire
559   TopTools_MapOfShape aMapToRemove;
560
561   TopTools_IndexedMapOfShape anOldIndices;
562   TopExp::MapShapes(theOriginalShape, anOldIndices);
563
564   TopTools_IndexedMapOfShape aNewIndices;
565   TopExp::MapShapes(theWire, aNewIndices);
566
567   // 3. Collect vertices of the wire, same or equal to the given vertices
568   bool removeAll = false;
569   if (aVerts.IsNull() || aVerts->Length() < 1)
570     removeAll = true;
571
572   if (!removeAll) {
573     for (unsigned int ind = 1; ind <= aVerts->Length(); ind++) {
574       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aVerts->Value(ind));
575       TopoDS_Shape aShape_i = aRefShape->GetValue();
576       if (aShape_i.IsNull())
577         Standard_NullObject::Raise("Null vertex given");
578       if (aShape_i.ShapeType() != TopAbs_VERTEX)
579         Standard_TypeMismatch::Raise("Shape to suppress is not a vertex");
580
581       // find vertices shared with the initial wire
582       if (anOldIndices.Contains(aShape_i)) {
583         aMapToRemove.Add(aNewIndices.FindKey(anOldIndices.FindIndex(aShape_i)));
584       } else {
585         // try to find by coords in the new wire
586         TopoDS_Vertex aVert = TopoDS::Vertex(aShape_i);
587         gp_Pnt aP = BRep_Tool::Pnt(aVert);
588
589         bool isFound = false;
590         TopTools_MapOfShape mapShape;
591         TopExp_Explorer exp (theWire, TopAbs_VERTEX);
592         for (; exp.More() && !isFound; exp.Next()) {
593           if (mapShape.Add(exp.Current())) {
594             TopoDS_Vertex aVi = TopoDS::Vertex(exp.Current());
595             gp_Pnt aPi = BRep_Tool::Pnt(aVi);
596             if (aPi.Distance(aP) < LinTol) {
597               aMapToRemove.Add(aVi);
598               isFound = true;
599             }
600           }
601         }
602       }
603     }
604   }
605
606   /*
607   BRepLib::BuildCurves3d(theWire);
608   Handle(ShapeFix_Shape) Fixer = new ShapeFix_Shape(theWire);
609   Fixer->SetPrecision(LinTol);
610   Fixer->SetMaxTolerance(LinTol);
611   Fixer->Perform();
612   theWire = TopoDS::Wire(Fixer->Shape());
613   */
614
615   TopoDS_Edge prevEdge;
616   TopTools_ListOfShape finalList, currChain;
617
618   BRepTools_WireExplorer wexp (theWire);
619   if (wexp.More()) {
620     prevEdge = wexp.Current();
621     currChain.Append(prevEdge);
622     wexp.Next();
623   }
624   else {
625     Standard_NullObject::Raise("Empty wire given");
626   }
627
628   for (; wexp.More(); wexp.Next()) {
629     TopoDS_Edge anEdge = wexp.Current();
630     TopoDS_Vertex CurVertex = wexp.CurrentVertex();
631
632     bool continueChain = false;
633     if (aMapToRemove.Contains(CurVertex) || removeAll) {
634       // if C1 -> continue chain
635       if (AreEdgesC1(prevEdge, anEdge)) {
636         continueChain = true;
637       }
638     }
639
640     if (!continueChain) {
641       if (currChain.Extent() == 1) {
642         // add one edge to the final list
643         finalList.Append(currChain.First());
644       }
645       else {
646         // make wire from the list of edges
647         BRep_Builder B;
648         TopoDS_Wire aCurrWire;
649         B.MakeWire(aCurrWire);
650         TopTools_ListIteratorOfListOfShape itEdges (currChain);
651         for (; itEdges.More(); itEdges.Next()) {
652           TopoDS_Shape aValue = itEdges.Value();
653           B.Add(aCurrWire, TopoDS::Edge(aValue));
654         }
655
656         // make edge from the wire
657         TopoDS_Edge anEdge = GEOMImpl_ShapeDriver::MakeEdgeFromWire(aCurrWire, LinTol, AngTol);
658
659         // add this new edge to the final list
660         finalList.Append(anEdge);
661       }
662       currChain.Clear();
663     }
664
665     // add one edge to the chain
666     currChain.Append(anEdge);
667     prevEdge = anEdge;
668   }
669
670   if (currChain.Extent() == 1) {
671     // add one edge to the final list
672     finalList.Append(currChain.First());
673   }
674   else {
675     // make wire from the list of edges
676     BRep_Builder B;
677     TopoDS_Wire aCurrWire;
678     B.MakeWire(aCurrWire);
679     TopTools_ListIteratorOfListOfShape itEdges (currChain);
680     for (; itEdges.More(); itEdges.Next()) {
681       TopoDS_Shape aValue = itEdges.Value();
682       B.Add(aCurrWire, TopoDS::Edge(aValue));
683     }
684
685     // make edge from the wire
686     TopoDS_Edge anEdge = GEOMImpl_ShapeDriver::MakeEdgeFromWire(aCurrWire, LinTol, AngTol);
687
688     // add this new edge to the final list
689     finalList.Append(anEdge);
690   }
691
692   BRep_Builder B;
693   TopoDS_Wire aFinalWire;
694   B.MakeWire(aFinalWire);
695   TopTools_ListIteratorOfListOfShape itEdges (finalList);
696   for (; itEdges.More(); itEdges.Next()) {
697     TopoDS_Shape aValue = itEdges.Value();
698     B.Add(aFinalWire, TopoDS::Edge(aValue));
699   }
700   theOutShape = aFinalWire;
701
702   BRepCheck_Analyzer ana (theOutShape, Standard_True);
703   if (!ana.IsValid())
704     StdFail_NotDone::Raise("Non valid shape result");
705 }
706
707 //=======================================================================
708 //function : AreEdgesC1
709 //purpose  :
710 //=======================================================================
711 Standard_Boolean GEOMImpl_HealingDriver::AreEdgesC1 (const TopoDS_Edge& E1, const TopoDS_Edge& E2)
712 {
713   BRepAdaptor_Curve aCurve1 (E1);
714   BRepAdaptor_Curve aCurve2 (E2);
715
716   if (aCurve1.Continuity() == GeomAbs_C0 || aCurve2.Continuity() == GeomAbs_C0)
717     return Standard_False;
718
719   Standard_Real tol, tolMax = Precision::Confusion();
720   for (TopExp_Explorer ExV1 (E1, TopAbs_VERTEX); ExV1.More(); ExV1.Next()) {
721     TopoDS_Vertex Vertex = TopoDS::Vertex(ExV1.Current());
722     tol = BRep_Tool::Tolerance(Vertex);
723       if (tol > tolMax)
724         tolMax = tol;
725   }
726   for (TopExp_Explorer ExV2 (E2, TopAbs_VERTEX); ExV2.More(); ExV2.Next()) {
727     TopoDS_Vertex Vertex = TopoDS::Vertex(ExV2.Current());
728     tol = BRep_Tool::Tolerance(Vertex);
729       if (tol > tolMax)
730         tolMax = tol;
731   }
732
733   Standard_Real f1, l1, f2, l2;
734   f1 = aCurve1.FirstParameter();
735   l1 = aCurve1.LastParameter();
736   f2 = aCurve2.FirstParameter();
737   l2 = aCurve2.LastParameter();
738
739   if (f1 > l1) {
740     Standard_Real tmp = f1;
741     f1 = l1;
742     l1 = tmp;
743   }
744
745   if (f2 > l2) {
746     Standard_Real tmp = f2;
747     f2 = l2;
748     l2 = tmp;
749   }
750
751   gp_Pnt pf1, pl1, pf2, pl2;
752   gp_Vec vf1, vl1, vf2, vl2;
753   aCurve1.D1(f1, pf1, vf1);
754   aCurve1.D1(l1, pl1, vl1);
755   aCurve2.D1(f2, pf2, vf2);
756   aCurve2.D1(l2, pl2, vl2);
757
758   // pf1--->---pl1.pf2--->---pl2
759   if (pl1.SquareDistance(pf2) < tolMax*tolMax) {
760     if (vl1.Angle(vf2) < Precision::Angular())
761       return Standard_True;
762   }
763   // pl1---<---pf1.pf2--->---pl2
764   else if (pf1.SquareDistance(pf2) < tolMax*tolMax) {
765     if (vf1.Angle(-vf2) < Precision::Angular())
766       return Standard_True;
767   }
768   // pf1--->---pl1.pl2---<---pf2
769   else if (pl1.SquareDistance(pl2) < tolMax*tolMax) {
770     if (vl1.Angle(-vl2) < Precision::Angular())
771       return Standard_True;
772   }
773   // pl1---<---pf1.pl2---<---pf2
774   else {
775     if (vf1.Angle(vl2) < Precision::Angular())
776       return Standard_True;
777   }
778
779   return Standard_False;
780 }
781
782 //=======================================================================
783 //function :  GEOMImpl_HealingDriver_Type_
784 //purpose  :
785 //=======================================================================
786 Standard_EXPORT Handle_Standard_Type& GEOMImpl_HealingDriver_Type_()
787 {
788
789   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
790   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
791   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
792   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
793   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
794   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
795
796
797   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
798   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_HealingDriver",
799                                                          sizeof(GEOMImpl_HealingDriver),
800                                                          1,
801                                                          (Standard_Address)_Ancestors,
802                                                          (Standard_Address)NULL);
803
804   return _aType;
805 }
806
807 //=======================================================================
808 //function : DownCast
809 //purpose  :
810 //=======================================================================
811
812 const Handle(GEOMImpl_HealingDriver) Handle(GEOMImpl_HealingDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
813 {
814   Handle(GEOMImpl_HealingDriver) _anOtherObject;
815
816   if (!AnObject.IsNull()) {
817      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_HealingDriver))) {
818        _anOtherObject = Handle(GEOMImpl_HealingDriver)((Handle(GEOMImpl_HealingDriver)&)AnObject);
819      }
820   }
821
822   return _anOtherObject;
823 }