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