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