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