Salome HOME
0678df2c7c1b676b10349456fe74a35cb88cc7c9
[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 <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   TopoDS_Compound faceCompound;
436   BRep_Builder builder;
437   builder.MakeCompound( faceCompound );
438
439   TopExp_Explorer faceExp( theOriginalShape, TopAbs_FACE );
440   for ( ; faceExp.More(); faceExp.Next() )
441     builder.Add( faceCompound, faceExp.Current() );
442   
443   Handle(TColStd_HSequenceOfTransient) otherObjs = theHI->GetShapes();
444   for ( int ind = 1; ind <= otherObjs->Length(); ind++)
445   {
446     Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(otherObjs->Value(ind));
447     TopoDS_Shape aShape = aRefShape->GetValue();
448     if (aShape.IsNull())
449       Standard_NullObject::Raise("Null object given");
450     for ( faceExp.Init( aShape, TopAbs_FACE ); faceExp.More(); faceExp.Next() )
451       builder.Add( faceCompound, faceExp.Current() );
452   }
453
454   ShHealOper_Sewing aHealer (faceCompound, aTol);
455
456   // Set non-manifold mode.
457   aHealer.SetNonManifoldMode(isAllowNonManifold);
458
459   Standard_Boolean aResult = aHealer.Perform();
460
461   if (aResult)
462     theOutShape = aHealer.GetResultShape();
463   else
464     raiseNotDoneExeption( aHealer.GetErrorStatus() );
465
466   return aResult;
467 }
468
469 //=======================================================================
470 //function : RemoveInternalFaces
471 //purpose  :
472 //=======================================================================
473 Standard_Boolean GEOMImpl_HealingDriver::RemoveInternalFaces (const TopoDS_Shape& theOriginalShape,
474                                                               TopoDS_Shape& theOutShape) const
475 {
476   GEOMAlgo_RemoverWebs aTool;
477   aTool.SetShape(theOriginalShape);
478   aTool.Perform();
479
480   if (aTool.ErrorStatus() == 0) { // OK
481     theOutShape = aTool.Result();
482
483     // as GEOMAlgo_RemoverWebs always produces compound, lets simplify it
484     // for the case, if it contains only one sub-shape
485     TopTools_ListOfShape listShapeRes;
486     GEOMUtils::AddSimpleShapes(theOutShape, listShapeRes);
487     if (listShapeRes.Extent() == 1) {
488       theOutShape = listShapeRes.First();
489     }
490   }
491   else if (aTool.ErrorStatus() == 11) // invalid argument (contains non-solids), do nothing
492     theOutShape = theOriginalShape;
493   else // error
494     StdFail_NotDone::Raise("GEOMAlgo_RemoverWebs failed!");
495
496   return Standard_True;
497 }
498
499 //=======================================================================
500 //function :  AddPointOnEdge
501 //purpose  :
502 //=======================================================================
503 Standard_Boolean GEOMImpl_HealingDriver::AddPointOnEdge (GEOMImpl_IHealing* theHI,
504                                                          const TopoDS_Shape& theOriginalShape,
505                                                          TopoDS_Shape& theOutShape) const
506 {
507   Standard_Boolean isByParameter = theHI->GetIsByParameter();
508   Standard_Integer anIndex = theHI->GetIndex();
509   Standard_Real aValue = theHI->GetDevideEdgeValue();
510
511   ShHealOper_EdgeDivide aHealer (theOriginalShape);
512
513   Standard_Boolean aResult = Standard_False;
514   if (anIndex == -1) { // apply algorythm for the whole shape which is EDGE
515     if (theOriginalShape.ShapeType() == TopAbs_EDGE)
516       aResult = aHealer.Perform(TopoDS::Edge(theOriginalShape), aValue, isByParameter);
517   } else {
518     TopTools_IndexedMapOfShape aShapes;
519     TopExp::MapShapes(theOriginalShape, aShapes);
520     TopoDS_Shape aEdgeShape = aShapes.FindKey(anIndex);
521     if (aEdgeShape.ShapeType() == TopAbs_EDGE)
522       aResult = aHealer.Perform(TopoDS::Edge(aEdgeShape), aValue, isByParameter);
523   }
524
525   if (aResult)
526     theOutShape = aHealer.GetResultShape();
527   else
528     raiseNotDoneExeption( aHealer.GetErrorStatus() );
529
530   return aResult;
531 }
532
533
534 //=======================================================================
535 //function :  ChangeOrientation
536 //purpose  :
537 //=======================================================================
538 Standard_Boolean GEOMImpl_HealingDriver::ChangeOrientation (GEOMImpl_IHealing* theHI,
539                                                             const TopoDS_Shape& theOriginalShape,
540                                                             TopoDS_Shape& theOutShape) const
541 {
542   ShHealOper_ChangeOrientation aHealer (theOriginalShape);
543
544   Standard_Boolean aResult = aHealer.Perform();
545
546   if (aResult)
547     theOutShape = aHealer.GetResultShape();
548   else
549     raiseNotDoneExeption( aHealer.GetErrorStatus() );
550
551   return aResult;
552 }
553
554 //=======================================================================
555 //function : LimitTolerance
556 //purpose  :
557 //=======================================================================
558 void GEOMImpl_HealingDriver::LimitTolerance (GEOMImpl_IHealing* theHI,
559                                              const TopoDS_Shape& theOriginalShape,
560                                              TopoDS_Shape& theOutShape) const
561 {
562   Standard_Real aTol = theHI->GetTolerance();
563   if (aTol < Precision::Confusion())
564     aTol = Precision::Confusion();
565
566   // 1. Make a copy to prevent the original shape changes.
567   TopoDS_Shape aShapeCopy;
568   TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
569   TNaming_CopyShape::CopyTool(theOriginalShape, aMapTShapes, aShapeCopy);
570
571   // 2. Limit tolerance.
572   ShapeFix_ShapeTolerance aSFT;
573   aSFT.LimitTolerance(aShapeCopy, aTol, aTol, TopAbs_SHAPE);
574
575   // 3. Fix obtained shape.
576   Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape (aShapeCopy);
577   aSfs->Perform();
578   theOutShape = aSfs->Shape();
579
580   BRepCheck_Analyzer ana (theOutShape, Standard_True);
581   if (!ana.IsValid())
582     StdFail_NotDone::Raise("Non valid shape result");
583 }
584
585 //=======================================================================
586 //function : FuseCollinearEdges
587 //purpose  :
588 //=======================================================================
589 void GEOMImpl_HealingDriver::FuseCollinearEdges (const TopoDS_Shape& theOriginalShape,
590                                                  const Handle(TColStd_HSequenceOfTransient)& aVerts,
591                                                  TopoDS_Shape& theOutShape)
592 {
593   if (theOriginalShape.ShapeType() != TopAbs_WIRE)
594     Standard_TypeMismatch::Raise("Not a wire is given");
595
596   // Tolerances
597   Standard_Real AngTol = Precision::Angular();
598   Standard_Real LinTol = Precision::Confusion();
599   Standard_Real tol;
600   for (TopExp_Explorer ExV (theOriginalShape, TopAbs_VERTEX); ExV.More(); ExV.Next()) {
601     TopoDS_Vertex Vertex = TopoDS::Vertex(ExV.Current());
602     tol = BRep_Tool::Tolerance(Vertex);
603     if (tol > LinTol)
604       LinTol = tol;
605   }
606
607   // 1. Make a copy to prevent the original shape changes.
608   TopoDS_Shape aWire;
609   TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
610   TNaming_CopyShape::CopyTool(theOriginalShape, aMapTShapes, aWire);
611   TopoDS_Wire theWire = TopoDS::Wire(aWire);
612
613   // 2. Sub-shapes of the wire
614   TopTools_MapOfShape aMapToRemove;
615
616   TopTools_IndexedMapOfShape anOldIndices;
617   TopExp::MapShapes(theOriginalShape, anOldIndices);
618
619   TopTools_IndexedMapOfShape aNewIndices;
620   TopExp::MapShapes(theWire, aNewIndices);
621
622   // 3. Collect vertices of the wire, same or equal to the given vertices
623   bool removeAll = false;
624   if (aVerts.IsNull() || aVerts->Length() < 1)
625     removeAll = true;
626
627   if (!removeAll) {
628     for ( int ind = 1; ind <= aVerts->Length(); ind++) {
629       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aVerts->Value(ind));
630       TopoDS_Shape aShape_i = aRefShape->GetValue();
631       if (aShape_i.IsNull())
632         Standard_NullObject::Raise("Null vertex given");
633       if (aShape_i.ShapeType() != TopAbs_VERTEX)
634         Standard_TypeMismatch::Raise("Shape to suppress is not a vertex");
635
636       // find vertices shared with the initial wire
637       if (anOldIndices.Contains(aShape_i)) {
638         aMapToRemove.Add(aNewIndices.FindKey(anOldIndices.FindIndex(aShape_i)));
639       } else {
640         // try to find by coords in the new wire
641         TopoDS_Vertex aVert = TopoDS::Vertex(aShape_i);
642         gp_Pnt aP = BRep_Tool::Pnt(aVert);
643
644         bool isFound = false;
645         TopTools_MapOfShape mapShape;
646         TopExp_Explorer exp (theWire, TopAbs_VERTEX);
647         for (; exp.More() && !isFound; exp.Next()) {
648           if (mapShape.Add(exp.Current())) {
649             TopoDS_Vertex aVi = TopoDS::Vertex(exp.Current());
650             gp_Pnt aPi = BRep_Tool::Pnt(aVi);
651             if (aPi.Distance(aP) < LinTol) {
652               aMapToRemove.Add(aVi);
653               isFound = true;
654             }
655           }
656         }
657       }
658     }
659   }
660
661   /*
662   BRepLib::BuildCurves3d(theWire);
663   Handle(ShapeFix_Shape) Fixer = new ShapeFix_Shape(theWire);
664   Fixer->SetPrecision(LinTol);
665   Fixer->SetMaxTolerance(LinTol);
666   Fixer->Perform();
667   theWire = TopoDS::Wire(Fixer->Shape());
668   */
669
670   TopoDS_Edge prevEdge;
671   TopTools_ListOfShape finalList, currChain;
672
673   BRepTools_WireExplorer wexp (theWire);
674   if (wexp.More()) {
675     prevEdge = wexp.Current();
676     currChain.Append(prevEdge);
677     wexp.Next();
678   }
679   else {
680     Standard_NullObject::Raise("Empty wire given");
681   }
682
683   for (; wexp.More(); wexp.Next()) {
684     TopoDS_Edge anEdge = wexp.Current();
685     TopoDS_Vertex CurVertex = wexp.CurrentVertex();
686
687     bool continueChain = false;
688     if (aMapToRemove.Contains(CurVertex) || removeAll) {
689       // if C1 -> continue chain
690       if (AreEdgesC1(prevEdge, anEdge)) {
691         continueChain = true;
692       }
693     }
694
695     if (!continueChain) {
696       if (currChain.Extent() == 1) {
697         // add one edge to the final list
698         finalList.Append(currChain.First());
699       }
700       else {
701         // make wire from the list of edges
702         BRep_Builder B;
703         TopoDS_Wire aCurrWire;
704         B.MakeWire(aCurrWire);
705         TopTools_ListIteratorOfListOfShape itEdges (currChain);
706         for (; itEdges.More(); itEdges.Next()) {
707           TopoDS_Shape aValue = itEdges.Value();
708           B.Add(aCurrWire, TopoDS::Edge(aValue));
709         }
710
711         // make edge from the wire
712         TopoDS_Edge anEdge = GEOMImpl_ShapeDriver::MakeEdgeFromWire(aCurrWire, LinTol, AngTol);
713
714         // add this new edge to the final list
715         finalList.Append(anEdge);
716       }
717       currChain.Clear();
718     }
719
720     // add one edge to the chain
721     currChain.Append(anEdge);
722     prevEdge = anEdge;
723   }
724
725   if (currChain.Extent() == 1) {
726     // add one edge to the final list
727     finalList.Append(currChain.First());
728   }
729   else {
730     // make wire from the list of edges
731     BRep_Builder B;
732     TopoDS_Wire aCurrWire;
733     B.MakeWire(aCurrWire);
734     TopTools_ListIteratorOfListOfShape itEdges (currChain);
735     for (; itEdges.More(); itEdges.Next()) {
736       TopoDS_Shape aValue = itEdges.Value();
737       B.Add(aCurrWire, TopoDS::Edge(aValue));
738     }
739
740     // make edge from the wire
741     TopoDS_Edge anEdge = GEOMImpl_ShapeDriver::MakeEdgeFromWire(aCurrWire, LinTol, AngTol);
742
743     // add this new edge to the final list
744     finalList.Append(anEdge);
745   }
746
747   BRep_Builder B;
748   TopoDS_Wire aFinalWire;
749   B.MakeWire(aFinalWire);
750   TopTools_ListIteratorOfListOfShape itEdges (finalList);
751   for (; itEdges.More(); itEdges.Next()) {
752     TopoDS_Shape aValue = itEdges.Value();
753     B.Add(aFinalWire, TopoDS::Edge(aValue));
754   }
755   theOutShape = aFinalWire;
756
757   BRepCheck_Analyzer ana (theOutShape, Standard_True);
758   if (!ana.IsValid())
759     StdFail_NotDone::Raise("Non valid shape result");
760 }
761
762 //=======================================================================
763 //function : AreEdgesC1
764 //purpose  :
765 //=======================================================================
766 Standard_Boolean GEOMImpl_HealingDriver::AreEdgesC1 (const TopoDS_Edge& E1, const TopoDS_Edge& E2)
767 {
768   BRepAdaptor_Curve aCurve1 (E1);
769   BRepAdaptor_Curve aCurve2 (E2);
770
771   if (aCurve1.Continuity() == GeomAbs_C0 || aCurve2.Continuity() == GeomAbs_C0)
772     return Standard_False;
773
774   Standard_Real tol, tolMax = Precision::Confusion();
775   for (TopExp_Explorer ExV1 (E1, TopAbs_VERTEX); ExV1.More(); ExV1.Next()) {
776     TopoDS_Vertex Vertex = TopoDS::Vertex(ExV1.Current());
777     tol = BRep_Tool::Tolerance(Vertex);
778       if (tol > tolMax)
779         tolMax = tol;
780   }
781   for (TopExp_Explorer ExV2 (E2, TopAbs_VERTEX); ExV2.More(); ExV2.Next()) {
782     TopoDS_Vertex Vertex = TopoDS::Vertex(ExV2.Current());
783     tol = BRep_Tool::Tolerance(Vertex);
784       if (tol > tolMax)
785         tolMax = tol;
786   }
787
788   Standard_Real f1, l1, f2, l2;
789   f1 = aCurve1.FirstParameter();
790   l1 = aCurve1.LastParameter();
791   f2 = aCurve2.FirstParameter();
792   l2 = aCurve2.LastParameter();
793
794   if (f1 > l1) {
795     Standard_Real tmp = f1;
796     f1 = l1;
797     l1 = tmp;
798   }
799
800   if (f2 > l2) {
801     Standard_Real tmp = f2;
802     f2 = l2;
803     l2 = tmp;
804   }
805
806   gp_Pnt pf1, pl1, pf2, pl2;
807   gp_Vec vf1, vl1, vf2, vl2;
808   aCurve1.D1(f1, pf1, vf1);
809   aCurve1.D1(l1, pl1, vl1);
810   aCurve2.D1(f2, pf2, vf2);
811   aCurve2.D1(l2, pl2, vl2);
812
813   // pf1--->---pl1.pf2--->---pl2
814   if (pl1.SquareDistance(pf2) < tolMax*tolMax) {
815     if (vl1.Angle(vf2) < Precision::Angular())
816       return Standard_True;
817   }
818   // pl1---<---pf1.pf2--->---pl2
819   else if (pf1.SquareDistance(pf2) < tolMax*tolMax) {
820     if (vf1.Angle(-vf2) < Precision::Angular())
821       return Standard_True;
822   }
823   // pf1--->---pl1.pl2---<---pf2
824   else if (pl1.SquareDistance(pl2) < tolMax*tolMax) {
825     if (vl1.Angle(-vl2) < Precision::Angular())
826       return Standard_True;
827   }
828   // pl1---<---pf1.pl2---<---pf2
829   else {
830     if (vf1.Angle(vl2) < Precision::Angular())
831       return Standard_True;
832   }
833
834   return Standard_False;
835 }
836
837 //================================================================================
838 /*!
839  * \brief Returns a name of creation operation and names and values of creation parameters
840  */
841 //================================================================================
842
843 bool GEOMImpl_HealingDriver::
844 GetCreationInformation(std::string&             theOperationName,
845                        std::vector<GEOM_Param>& theParams)
846 {
847   if (Label().IsNull()) return 0;
848   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
849
850   GEOMImpl_IHealing aCI( function );
851   Standard_Integer aType = function->GetType();
852
853   switch ( aType ) {
854   case SHAPE_PROCESS:
855   {
856     theOperationName = "SHAPE_PROCESS";
857     AddParam( theParams, "Object", aCI.GetOriginal() );
858     Handle(TColStd_HArray1OfExtendedString) anOperators = aCI.GetOperators();
859     Handle(TColStd_HArray1OfExtendedString) aParams     = aCI.GetParameters();
860     Handle(TColStd_HArray1OfExtendedString) aValues     = aCI.GetValues();
861     for ( int i = anOperators->Lower(), nb = anOperators->Upper(); i <= nb; ++i )
862     {
863       const TCollection_ExtendedString& op = anOperators->Value(i);
864       AddParam( theParams, "Operation", op );
865       for ( int iP = aParams->Lower(), nbP = aParams->Upper(); iP <= nbP; ++iP )
866       {
867         const TCollection_ExtendedString& par = aParams->Value(i);
868         TCollection_AsciiString parAscii( par );
869         if ( par.Search( op ) == 1 && parAscii.Value( op.Length() + 1 ) == '.' )
870         {
871           GEOM_Param& p = AddParam( theParams, parAscii.ToCString() );
872           if ( iP <= aValues->Upper() )
873             p << aValues->Value( iP );
874         }
875       }
876     }
877     break;
878   }
879   case SUPPRESS_FACES:
880     theOperationName = "SUPPRESS_FACES";
881     AddParam( theParams, "Selected Shape", aCI.GetOriginal() );
882     AddParam( theParams, "Faces to remove", aCI.GetFaces() );
883     break;
884   case CLOSE_CONTOUR:
885     theOperationName = "CLOSE_CONTOUR";
886     AddParam( theParams, "Selected Shape", aCI.GetOriginal() );
887     AddParam( theParams, "Contour to close", aCI.GetWires() );
888     AddParam( theParams, "Close by common vertex", aCI.GetIsCommonVertex() );
889     break;
890   case REMOVE_INT_WIRES:
891     theOperationName = "SUPPRESS_INT_WIRES";
892     AddParam( theParams, "Selected face", aCI.GetOriginal() );
893     AddParam( theParams, "Wires to remove", aCI.GetWires(), "all" );
894     break;
895   case FILL_HOLES:
896     theOperationName = "SUPPERSS_HOLES";
897     AddParam( theParams, "Selected shape", aCI.GetOriginal() );
898     AddParam( theParams, "Wires to remove", aCI.GetWires(), "all" );
899     break;
900   case SEWING:
901   case SEWING_NON_MANIFOLD:
902     theOperationName = "SEWING";
903     AddParam( theParams, "Selected shapes", aCI.GetOriginalAndShapes() );
904     AddParam( theParams, "Allow Non Manifold", ( aType == SEWING_NON_MANIFOLD ));
905     AddParam( theParams, "Tolerance", aCI.GetTolerance() );
906     break;
907   case DIVIDE_EDGE:
908     theOperationName = "POINT_ON_EDGE";
909     if ( aCI.GetIndex() > 0 )
910       AddParam( theParams, "Edge", "#" ) << aCI.GetIndex() << " of " << aCI.GetOriginal();
911     else
912       AddParam( theParams, "Edge", aCI.GetOriginal() );
913     AddParam( theParams, "By parameter", aCI.GetIsByParameter() );
914     AddParam( theParams, "Value", aCI.GetDevideEdgeValue() );
915     break;
916   case CHANGE_ORIENTATION:
917     theOperationName = "CHANGE_ORIENTATION";
918     AddParam( theParams, "Selected shape", aCI.GetOriginal() );
919     break;
920   case LIMIT_TOLERANCE:
921     theOperationName = "LIMIT_TOLERANCE";
922     AddParam( theParams, "Selected shape", aCI.GetOriginal() );
923     AddParam( theParams, "Tolerance", aCI.GetTolerance() );
924     break;
925   case FUSE_COLLINEAR_EDGES:
926     theOperationName = "FUSE_EDGES";
927     AddParam( theParams, "Wire", aCI.GetOriginal() );
928     AddParam( theParams, "Vertexes", aCI.GetShapes() );
929     break;
930   case REMOVE_INTERNAL_FACES:
931     theOperationName = "REMOVE_WEBS";
932     AddParam( theParams, "Selected shape", aCI.GetOriginal() );
933     break;
934   default:
935     return false;
936   }
937   
938   return true;
939 }
940 IMPLEMENT_STANDARD_HANDLE (GEOMImpl_HealingDriver,GEOM_BaseDriver);
941
942 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_HealingDriver,GEOM_BaseDriver);