]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMImpl/GEOMImpl_HealingDriver.cxx
Salome HOME
Merge from V6_main 01/04/2013
[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);
137     break;
138   case DIVIDE_EDGE:
139     AddPointOnEdge(&HI, anOriginalShape, aShape);
140     break;
141   case CHANGE_ORIENTATION:
142     ChangeOrientation(&HI, anOriginalShape, aShape);
143     break;
144   case LIMIT_TOLERANCE:
145     LimitTolerance(&HI, anOriginalShape, aShape);
146     break;
147   case FUSE_COLLINEAR_EDGES:
148     {
149       Handle(TColStd_HSequenceOfTransient) aVerts = HI.GetShapes();
150       FuseCollinearEdges(anOriginalShape, aVerts, aShape);
151     }
152     break;
153   default:
154     return 0;
155   }
156
157   if (aShape.IsNull())
158     raiseNotDoneExeption( ShHealOper_ErrorExecution );
159
160   aFunction->SetValue(aShape);
161
162   log.SetTouched(Label());
163   return 1;
164 }
165
166 //=======================================================================
167 //function :  ShapeProcess
168 //purpose  :
169 //=======================================================================
170 Standard_Boolean GEOMImpl_HealingDriver::ShapeProcess (GEOMImpl_IHealing* theHI,
171                                                        const TopoDS_Shape& theOriginalShape,
172                                                        TopoDS_Shape& theOutShape) const
173 {
174   Handle(TColStd_HArray1OfExtendedString) anOperators = theHI->GetOperators();
175   Handle(TColStd_HArray1OfExtendedString) aParams = theHI->GetParameters();
176   Handle(TColStd_HArray1OfExtendedString) aValues = theHI->GetValues();
177
178   if (anOperators.IsNull() || anOperators->Length() <= 0)
179     return Standard_False;
180
181   Standard_Integer nbParams = 0, nbValues = 0;
182   if (!aParams.IsNull()) {
183     nbParams = aParams->Length();
184   }
185   if (!aValues.IsNull()) {
186     nbValues = aValues->Length();
187   }
188   if (nbParams != nbValues)
189     return Standard_False;
190
191   ShHealOper_ShapeProcess aHealer;
192   TColStd_SequenceOfAsciiString anOperatorsAS, aParamsAS, aValuesAS;
193   int i;
194   for (i = 1; i <= anOperators->Length(); i++)
195     anOperatorsAS.Append(TCollection_AsciiString(anOperators->Value(i)));
196
197   aHealer.SetOperators(anOperatorsAS);
198
199   for (i = 1; i <= nbParams; i++) {
200     aHealer.SetParameter(TCollection_AsciiString(aParams->Value(i)),
201                          TCollection_AsciiString(aValues->Value(i)));
202   }
203
204   aHealer.Perform(theOriginalShape, theOutShape);
205
206   if (!aHealer.isDone())
207     raiseNotDoneExeption( ShHealOper_NotError );
208
209   return Standard_True;
210 }
211
212 //=======================================================================
213 //function :  SupressFaces
214 //purpose  :
215 //=======================================================================
216 void SuppressFacesRec (const TopTools_SequenceOfShape& theShapesFaces,
217                        const TopoDS_Shape&             theOriginalShape,
218                        TopoDS_Shape&                   theOutShape)
219 {
220   if ((theOriginalShape.ShapeType() != TopAbs_COMPOUND &&
221        theOriginalShape.ShapeType() != TopAbs_COMPSOLID))
222   {
223     ShHealOper_RemoveFace aHealer (theOriginalShape);
224     Standard_Boolean aResult = aHealer.Perform(theShapesFaces);
225
226     if (aResult)
227       theOutShape = aHealer.GetResultShape();
228     else
229       raiseNotDoneExeption(aHealer.GetErrorStatus());
230   }
231   else
232   {
233     BRep_Builder BB;
234     TopoDS_Compound CC;
235     BB.MakeCompound(CC);
236
237     TopTools_MapOfShape mapShape;
238     TopoDS_Iterator It (theOriginalShape, Standard_True, Standard_True);
239
240     for (; It.More(); It.Next()) {
241       TopoDS_Shape aShape_i = It.Value();
242       if (mapShape.Add(aShape_i)) {
243         // check, if current shape contains at least one of faces to be removed
244         bool isFound = false;
245         TopTools_IndexedMapOfShape aShapes_i;
246         TopExp::MapShapes(aShape_i, aShapes_i);
247         for (int i = 1; i <= theShapesFaces.Length() && !isFound; i++) {
248           const TopoDS_Shape& aFace_i = theShapesFaces.Value(i);
249           if (aShapes_i.Contains(aFace_i)) isFound = true;
250         }
251         if (isFound) {
252           TopoDS_Shape anOutSh_i;
253           SuppressFacesRec(theShapesFaces, aShape_i, anOutSh_i);
254           if ( !anOutSh_i.IsNull() )
255             BB.Add(CC, anOutSh_i);
256         }
257         else {
258           // nothing to do
259           BB.Add(CC, aShape_i);
260         }
261       }
262     }
263     theOutShape = CC;
264   }
265 }
266
267 Standard_Boolean GEOMImpl_HealingDriver::SuppressFaces (GEOMImpl_IHealing* theHI,
268                                                         const TopoDS_Shape& theOriginalShape,
269                                                         TopoDS_Shape& theOutShape) const
270 {
271   Handle(TColStd_HArray1OfInteger) aFaces = theHI->GetFaces();
272
273   Standard_Boolean aResult = Standard_False;
274
275   if (aFaces.IsNull()) {
276     ShHealOper_RemoveFace aHealer (theOriginalShape);
277     aResult = aHealer.Perform();
278
279     if (aResult)
280       theOutShape = aHealer.GetResultShape();
281     else
282       raiseNotDoneExeption(aHealer.GetErrorStatus());
283   }
284   else {
285     TopTools_SequenceOfShape aShapesFaces;
286     TopTools_IndexedMapOfShape aShapes;
287     TopExp::MapShapes(theOriginalShape, aShapes);
288     for (int i = 1; i <= aFaces->Length(); i++) {
289       int indexOfFace = aFaces->Value(i);
290       TopoDS_Shape aFace = aShapes.FindKey(indexOfFace);
291       aShapesFaces.Append(aFace);
292     }
293     SuppressFacesRec(aShapesFaces, theOriginalShape, theOutShape);
294     if ((theOriginalShape.ShapeType() == TopAbs_COMPOUND ||
295          theOriginalShape.ShapeType() == TopAbs_COMPSOLID)) {
296       TopoDS_Shape aSh = theOutShape;
297       theOutShape = GEOMImpl_GlueDriver::GlueFaces(aSh, Precision::Confusion(), Standard_True);
298     }
299   }
300
301   return Standard_True;
302 }
303
304 //=======================================================================
305 //function :  CloseContour
306 //purpose  :
307 //=======================================================================
308 Standard_Boolean GEOMImpl_HealingDriver::CloseContour (GEOMImpl_IHealing* theHI,
309                                                        const TopoDS_Shape& theOriginalShape,
310                                                        TopoDS_Shape& theOutShape) const
311 {
312   Standard_Boolean isByVertex = theHI->GetIsCommonVertex();
313   Handle(TColStd_HArray1OfInteger) aWires = theHI->GetWires();
314
315   ShHealOper_CloseContour aHealer (theOriginalShape);
316
317   Standard_Boolean aResult = Standard_False;
318   if ( aWires.IsNull() ) {
319     if ( theOriginalShape.ShapeType() == TopAbs_WIRE )
320       aResult = aHealer.Perform(TopoDS::Wire(theOriginalShape), isByVertex, !isByVertex);
321   }
322   else {
323     TopTools_SequenceOfShape aShapesWires;
324     TopTools_IndexedMapOfShape aShapes;
325     TopExp::MapShapes(theOriginalShape, aShapes);
326     for (int i = 1; i <= aWires->Length(); i++) {
327       int indexOfWire = aWires->Value(i);
328       TopoDS_Shape aWire = aShapes.FindKey(indexOfWire);
329       aShapesWires.Append(aWire);
330     }
331
332     aResult = aHealer.Perform( aShapesWires, isByVertex, !isByVertex );
333   }
334
335   if (aResult)
336     theOutShape = aHealer.GetResultShape();
337   else
338     raiseNotDoneExeption( aHealer.GetErrorStatus() );
339
340   return aResult;
341 }
342
343 //=======================================================================
344 //function :  RemoveIntWires
345 //purpose  :
346 //=======================================================================
347 Standard_Boolean GEOMImpl_HealingDriver::RemoveIntWires (GEOMImpl_IHealing* theHI,
348                                                          const TopoDS_Shape& theOriginalShape,
349                                                          TopoDS_Shape& theOutShape) const
350 {
351   Handle(TColStd_HArray1OfInteger) aWires = theHI->GetWires();
352
353   ShHealOper_RemoveInternalWires aHealer(theOriginalShape);
354
355   Standard_Boolean aResult = Standard_False;
356   if (aWires.IsNull()) { // remove all faces
357     aResult = aHealer.Remove();
358   } else {
359     TopTools_SequenceOfShape aShapesWires;
360     TopTools_IndexedMapOfShape aShapes;
361     TopExp::MapShapes(theOriginalShape, aShapes);
362     for (int i = 1; i <= aWires->Length(); i++) {
363       int indexOfWire = aWires->Value(i);
364       TopoDS_Shape aWire = aShapes.FindKey(indexOfWire);
365       aShapesWires.Append(aWire);
366     }
367
368     aResult = aHealer.Remove(aShapesWires);
369   }
370
371   if (aResult)
372     theOutShape = aHealer.GetResultShape();
373   else
374     raiseNotDoneExeption( aHealer.GetErrorStatus() );
375
376   return aResult;
377 }
378
379 //=======================================================================
380 //function :  RemoveHoles
381 //purpose  :
382 //=======================================================================
383 Standard_Boolean GEOMImpl_HealingDriver::RemoveHoles (GEOMImpl_IHealing* theHI,
384                                                       const TopoDS_Shape& theOriginalShape,
385                                                       TopoDS_Shape& theOutShape) const
386 {
387   Handle(TColStd_HArray1OfInteger) aWires = theHI->GetWires();
388
389   ShHealOper_FillHoles aHealer (theOriginalShape);
390
391   Standard_Boolean aResult = Standard_False;
392   if (aWires.IsNull()) { // remove all faces
393     aResult = aHealer.Fill();
394   } else {
395     TopTools_SequenceOfShape aShapesWires;
396     TopTools_IndexedMapOfShape aShapes;
397     TopExp::MapShapes(theOriginalShape, aShapes);
398     for (int i = 1; i <= aWires->Length(); i++) {
399       int indexOfWire = aWires->Value(i);
400       TopoDS_Shape aWire = aShapes.FindKey(indexOfWire);
401       aShapesWires.Append(aWire);
402     }
403
404     aResult = aHealer.Fill(aShapesWires);
405   }
406
407   if (aResult)
408     theOutShape = aHealer.GetResultShape();
409   else
410     raiseNotDoneExeption( aHealer.GetErrorStatus() );
411
412   return aResult;
413 }
414
415 //=======================================================================
416 //function :  Sew
417 //purpose  :
418 //=======================================================================
419 Standard_Boolean GEOMImpl_HealingDriver::Sew (GEOMImpl_IHealing* theHI,
420                                               const TopoDS_Shape& theOriginalShape,
421                                               TopoDS_Shape& theOutShape) const
422 {
423   Standard_Real aTol = theHI->GetTolerance();
424
425   ShHealOper_Sewing aHealer (theOriginalShape, aTol);
426
427   Standard_Boolean aResult = aHealer.Perform();
428
429   if (aResult)
430     theOutShape = aHealer.GetResultShape();
431   else
432     raiseNotDoneExeption( aHealer.GetErrorStatus() );
433
434   return aResult;
435 }
436
437 //=======================================================================
438 //function :  AddPointOnEdge
439 //purpose  :
440 //=======================================================================
441 Standard_Boolean GEOMImpl_HealingDriver::AddPointOnEdge (GEOMImpl_IHealing* theHI,
442                                                          const TopoDS_Shape& theOriginalShape,
443                                                          TopoDS_Shape& theOutShape) const
444 {
445   Standard_Boolean isByParameter = theHI->GetIsByParameter();
446   Standard_Integer anIndex = theHI->GetIndex();
447   Standard_Real aValue = theHI->GetDevideEdgeValue();
448
449   ShHealOper_EdgeDivide aHealer (theOriginalShape);
450
451   Standard_Boolean aResult = Standard_False;
452   if (anIndex == -1) { // apply algorythm for the whole shape which is EDGE
453     if (theOriginalShape.ShapeType() == TopAbs_EDGE)
454       aResult = aHealer.Perform(TopoDS::Edge(theOriginalShape), aValue, isByParameter);
455   } else {
456     TopTools_IndexedMapOfShape aShapes;
457     TopExp::MapShapes(theOriginalShape, aShapes);
458     TopoDS_Shape aEdgeShape = aShapes.FindKey(anIndex);
459     if (aEdgeShape.ShapeType() == TopAbs_EDGE)
460       aResult = aHealer.Perform(TopoDS::Edge(aEdgeShape), aValue, isByParameter);
461   }
462
463   if (aResult)
464     theOutShape = aHealer.GetResultShape();
465   else
466     raiseNotDoneExeption( aHealer.GetErrorStatus() );
467
468   return aResult;
469 }
470
471
472 //=======================================================================
473 //function :  ChangeOrientation
474 //purpose  :
475 //=======================================================================
476 Standard_Boolean GEOMImpl_HealingDriver::ChangeOrientation (GEOMImpl_IHealing* theHI,
477                                                             const TopoDS_Shape& theOriginalShape,
478                                                             TopoDS_Shape& theOutShape) const
479 {
480   ShHealOper_ChangeOrientation aHealer (theOriginalShape);
481
482   Standard_Boolean aResult = aHealer.Perform();
483
484   if (aResult)
485     theOutShape = aHealer.GetResultShape();
486   else
487     raiseNotDoneExeption( aHealer.GetErrorStatus() );
488
489   return aResult;
490 }
491
492 //=======================================================================
493 //function : LimitTolerance
494 //purpose  :
495 //=======================================================================
496 void GEOMImpl_HealingDriver::LimitTolerance (GEOMImpl_IHealing* theHI,
497                                              const TopoDS_Shape& theOriginalShape,
498                                              TopoDS_Shape& theOutShape) const
499 {
500   Standard_Real aTol = theHI->GetTolerance();
501   if (aTol < Precision::Confusion())
502     aTol = Precision::Confusion();
503
504   // 1. Make a copy to prevent the original shape changes.
505   TopoDS_Shape aShapeCopy;
506   TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
507   TNaming_CopyShape::CopyTool(theOriginalShape, aMapTShapes, aShapeCopy);
508
509   // 2. Limit tolerance.
510   ShapeFix_ShapeTolerance aSFT;
511   aSFT.LimitTolerance(aShapeCopy, aTol, aTol, TopAbs_SHAPE);
512
513   // 3. Fix obtained shape.
514   Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape (aShapeCopy);
515   aSfs->Perform();
516   theOutShape = aSfs->Shape();
517
518   BRepCheck_Analyzer ana (theOutShape, Standard_True);
519   if (!ana.IsValid())
520     StdFail_NotDone::Raise("Non valid shape result");
521 }
522
523 //=======================================================================
524 //function : FuseCollinearEdges
525 //purpose  :
526 //=======================================================================
527 void GEOMImpl_HealingDriver::FuseCollinearEdges (const TopoDS_Shape& theOriginalShape,
528                                                  const Handle(TColStd_HSequenceOfTransient)& aVerts,
529                                                  TopoDS_Shape& theOutShape)
530 {
531   if (theOriginalShape.ShapeType() != TopAbs_WIRE)
532     Standard_TypeMismatch::Raise("Not a wire is given");
533
534   // Tolerances
535   Standard_Real AngTol = Precision::Angular();
536   Standard_Real LinTol = Precision::Confusion();
537   Standard_Real tol;
538   for (TopExp_Explorer ExV (theOriginalShape, TopAbs_VERTEX); ExV.More(); ExV.Next()) {
539     TopoDS_Vertex Vertex = TopoDS::Vertex(ExV.Current());
540     tol = BRep_Tool::Tolerance(Vertex);
541     if (tol > LinTol)
542       LinTol = tol;
543   }
544
545   // 1. Make a copy to prevent the original shape changes.
546   TopoDS_Shape aWire;
547   TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
548   TNaming_CopyShape::CopyTool(theOriginalShape, aMapTShapes, aWire);
549   TopoDS_Wire theWire = TopoDS::Wire(aWire);
550
551   // 2. Sub-shapes of the wire
552   TopTools_MapOfShape aMapToRemove;
553
554   TopTools_IndexedMapOfShape anOldIndices;
555   TopExp::MapShapes(theOriginalShape, anOldIndices);
556
557   TopTools_IndexedMapOfShape aNewIndices;
558   TopExp::MapShapes(theWire, aNewIndices);
559
560   // 3. Collect vertices of the wire, same or equal to the given vertices
561   bool removeAll = false;
562   if (aVerts.IsNull() || aVerts->Length() < 1)
563     removeAll = true;
564
565   if (!removeAll) {
566     for (unsigned int ind = 1; ind <= aVerts->Length(); ind++) {
567       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aVerts->Value(ind));
568       TopoDS_Shape aShape_i = aRefShape->GetValue();
569       if (aShape_i.IsNull())
570         Standard_NullObject::Raise("Null vertex given");
571       if (aShape_i.ShapeType() != TopAbs_VERTEX)
572         Standard_TypeMismatch::Raise("Shape to suppress is not a vertex");
573
574       // find vertices shared with the initial wire
575       if (anOldIndices.Contains(aShape_i)) {
576         aMapToRemove.Add(aNewIndices.FindKey(anOldIndices.FindIndex(aShape_i)));
577       } else {
578         // try to find by coords in the new wire
579         TopoDS_Vertex aVert = TopoDS::Vertex(aShape_i);
580         gp_Pnt aP = BRep_Tool::Pnt(aVert);
581
582         bool isFound = false;
583         TopTools_MapOfShape mapShape;
584         TopExp_Explorer exp (theWire, TopAbs_VERTEX);
585         for (; exp.More() && !isFound; exp.Next()) {
586           if (mapShape.Add(exp.Current())) {
587             TopoDS_Vertex aVi = TopoDS::Vertex(exp.Current());
588             gp_Pnt aPi = BRep_Tool::Pnt(aVi);
589             if (aPi.Distance(aP) < LinTol) {
590               aMapToRemove.Add(aVi);
591               isFound = true;
592             }
593           }
594         }
595       }
596     }
597   }
598
599   /*
600   BRepLib::BuildCurves3d(theWire);
601   Handle(ShapeFix_Shape) Fixer = new ShapeFix_Shape(theWire);
602   Fixer->SetPrecision(LinTol);
603   Fixer->SetMaxTolerance(LinTol);
604   Fixer->Perform();
605   theWire = TopoDS::Wire(Fixer->Shape());
606   */
607
608   TopoDS_Edge prevEdge;
609   TopTools_ListOfShape finalList, currChain;
610
611   BRepTools_WireExplorer wexp (theWire);
612   if (wexp.More()) {
613     prevEdge = wexp.Current();
614     currChain.Append(prevEdge);
615     wexp.Next();
616   }
617   else {
618     Standard_NullObject::Raise("Empty wire given");
619   }
620
621   for (; wexp.More(); wexp.Next()) {
622     TopoDS_Edge anEdge = wexp.Current();
623     TopoDS_Vertex CurVertex = wexp.CurrentVertex();
624
625     bool continueChain = false;
626     if (aMapToRemove.Contains(CurVertex) || removeAll) {
627       // if C1 -> continue chain
628       if (AreEdgesC1(prevEdge, anEdge)) {
629         continueChain = true;
630       }
631     }
632
633     if (!continueChain) {
634       if (currChain.Extent() == 1) {
635         // add one edge to the final list
636         finalList.Append(currChain.First());
637       }
638       else {
639         // make wire from the list of edges
640         BRep_Builder B;
641         TopoDS_Wire aCurrWire;
642         B.MakeWire(aCurrWire);
643         TopTools_ListIteratorOfListOfShape itEdges (currChain);
644         for (; itEdges.More(); itEdges.Next()) {
645           TopoDS_Shape aValue = itEdges.Value();
646           B.Add(aCurrWire, TopoDS::Edge(aValue));
647         }
648
649         // make edge from the wire
650         TopoDS_Edge anEdge = GEOMImpl_ShapeDriver::MakeEdgeFromWire(aCurrWire, LinTol, AngTol);
651
652         // add this new edge to the final list
653         finalList.Append(anEdge);
654       }
655       currChain.Clear();
656     }
657
658     // add one edge to the chain
659     currChain.Append(anEdge);
660     prevEdge = anEdge;
661   }
662
663   if (currChain.Extent() == 1) {
664     // add one edge to the final list
665     finalList.Append(currChain.First());
666   }
667   else {
668     // make wire from the list of edges
669     BRep_Builder B;
670     TopoDS_Wire aCurrWire;
671     B.MakeWire(aCurrWire);
672     TopTools_ListIteratorOfListOfShape itEdges (currChain);
673     for (; itEdges.More(); itEdges.Next()) {
674       TopoDS_Shape aValue = itEdges.Value();
675       B.Add(aCurrWire, TopoDS::Edge(aValue));
676     }
677
678     // make edge from the wire
679     TopoDS_Edge anEdge = GEOMImpl_ShapeDriver::MakeEdgeFromWire(aCurrWire, LinTol, AngTol);
680
681     // add this new edge to the final list
682     finalList.Append(anEdge);
683   }
684
685   BRep_Builder B;
686   TopoDS_Wire aFinalWire;
687   B.MakeWire(aFinalWire);
688   TopTools_ListIteratorOfListOfShape itEdges (finalList);
689   for (; itEdges.More(); itEdges.Next()) {
690     TopoDS_Shape aValue = itEdges.Value();
691     B.Add(aFinalWire, TopoDS::Edge(aValue));
692   }
693   theOutShape = aFinalWire;
694
695   BRepCheck_Analyzer ana (theOutShape, Standard_True);
696   if (!ana.IsValid())
697     StdFail_NotDone::Raise("Non valid shape result");
698 }
699
700 //=======================================================================
701 //function : AreEdgesC1
702 //purpose  :
703 //=======================================================================
704 Standard_Boolean GEOMImpl_HealingDriver::AreEdgesC1 (const TopoDS_Edge& E1, const TopoDS_Edge& E2)
705 {
706   BRepAdaptor_Curve aCurve1 (E1);
707   BRepAdaptor_Curve aCurve2 (E2);
708
709   if (aCurve1.Continuity() == GeomAbs_C0 || aCurve2.Continuity() == GeomAbs_C0)
710     return Standard_False;
711
712   Standard_Real tol, tolMax = Precision::Confusion();
713   for (TopExp_Explorer ExV1 (E1, TopAbs_VERTEX); ExV1.More(); ExV1.Next()) {
714     TopoDS_Vertex Vertex = TopoDS::Vertex(ExV1.Current());
715     tol = BRep_Tool::Tolerance(Vertex);
716       if (tol > tolMax)
717         tolMax = tol;
718   }
719   for (TopExp_Explorer ExV2 (E2, TopAbs_VERTEX); ExV2.More(); ExV2.Next()) {
720     TopoDS_Vertex Vertex = TopoDS::Vertex(ExV2.Current());
721     tol = BRep_Tool::Tolerance(Vertex);
722       if (tol > tolMax)
723         tolMax = tol;
724   }
725
726   Standard_Real f1, l1, f2, l2;
727   f1 = aCurve1.FirstParameter();
728   l1 = aCurve1.LastParameter();
729   f2 = aCurve2.FirstParameter();
730   l2 = aCurve2.LastParameter();
731
732   if (f1 > l1) {
733     Standard_Real tmp = f1;
734     f1 = l1;
735     l1 = tmp;
736   }
737
738   if (f2 > l2) {
739     Standard_Real tmp = f2;
740     f2 = l2;
741     l2 = tmp;
742   }
743
744   gp_Pnt pf1, pl1, pf2, pl2;
745   gp_Vec vf1, vl1, vf2, vl2;
746   aCurve1.D1(f1, pf1, vf1);
747   aCurve1.D1(l1, pl1, vl1);
748   aCurve2.D1(f2, pf2, vf2);
749   aCurve2.D1(l2, pl2, vl2);
750
751   // pf1--->---pl1.pf2--->---pl2
752   if (pl1.SquareDistance(pf2) < tolMax*tolMax) {
753     if (vl1.Angle(vf2) < Precision::Angular())
754       return Standard_True;
755   }
756   // pl1---<---pf1.pf2--->---pl2
757   else if (pf1.SquareDistance(pf2) < tolMax*tolMax) {
758     if (vf1.Angle(-vf2) < Precision::Angular())
759       return Standard_True;
760   }
761   // pf1--->---pl1.pl2---<---pf2
762   else if (pl1.SquareDistance(pl2) < tolMax*tolMax) {
763     if (vl1.Angle(-vl2) < Precision::Angular())
764       return Standard_True;
765   }
766   // pl1---<---pf1.pl2---<---pf2
767   else {
768     if (vf1.Angle(vl2) < Precision::Angular())
769       return Standard_True;
770   }
771
772   return Standard_False;
773 }
774
775 //=======================================================================
776 //function :  GEOMImpl_HealingDriver_Type_
777 //purpose  :
778 //=======================================================================
779 Standard_EXPORT Handle_Standard_Type& GEOMImpl_HealingDriver_Type_()
780 {
781
782   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
783   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
784   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
785   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
786   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
787   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
788
789
790   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
791   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_HealingDriver",
792                                                          sizeof(GEOMImpl_HealingDriver),
793                                                          1,
794                                                          (Standard_Address)_Ancestors,
795                                                          (Standard_Address)NULL);
796
797   return _aType;
798 }
799
800 //=======================================================================
801 //function : DownCast
802 //purpose  :
803 //=======================================================================
804
805 const Handle(GEOMImpl_HealingDriver) Handle(GEOMImpl_HealingDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
806 {
807   Handle(GEOMImpl_HealingDriver) _anOtherObject;
808
809   if (!AnObject.IsNull()) {
810      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_HealingDriver))) {
811        _anOtherObject = Handle(GEOMImpl_HealingDriver)((Handle(GEOMImpl_HealingDriver)&)AnObject);
812      }
813   }
814
815   return _anOtherObject;
816 }