1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #include <Standard_Stream.hxx>
24 #include <GEOMImpl_HealingDriver.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOMImpl_IHealing.hxx>
27 #include <GEOM_Function.hxx>
29 #include <GEOMImpl_GlueDriver.hxx>
31 #include <ShHealOper_ShapeProcess.hxx>
32 #include <ShHealOper_RemoveFace.hxx>
33 #include <ShHealOper_CloseContour.hxx>
34 #include <ShHealOper_RemoveInternalWires.hxx>
35 #include <ShHealOper_FillHoles.hxx>
36 #include <ShHealOper_Sewing.hxx>
37 #include <ShHealOper_EdgeDivide.hxx>
38 #include <ShHealOper_ChangeOrientation.hxx>
40 #include <BRep_Builder.hxx>
44 #include <TopoDS_Iterator.hxx>
45 #include <TopTools_IndexedMapOfShape.hxx>
47 #include <TColStd_IndexedDataMapOfTransientTransient.hxx>
48 #include <TNaming_CopyShape.hxx>
49 #include <ShapeFix_ShapeTolerance.hxx>
50 #include <ShapeFix_Shape.hxx>
51 #include <BRepCheck_Analyzer.hxx>
53 #include <Precision.hxx>
55 #include <StdFail_NotDone.hxx>
57 //=======================================================================
58 //function : raiseNotDoneExeption
59 //purpose : global function: forms error message and raises exeption
60 //=======================================================================
61 void raiseNotDoneExeption( const int theErrorStatus )
63 switch ( theErrorStatus )
65 case ShHealOper_NotError: StdFail_NotDone::Raise( "ShHealOper_NotError_msg" );
66 case ShHealOper_InvalidParameters: StdFail_NotDone::Raise( "ShHealOper_InvalidParameters_msg" );
67 case ShHealOper_ErrorExecution:
68 default: StdFail_NotDone::Raise( "ShHealOper_ErrorExecution_msg" );
72 //=======================================================================
75 //=======================================================================
76 const Standard_GUID& GEOMImpl_HealingDriver::GetID()
78 static Standard_GUID aHealingDriver("FF1BBB61-5D14-4df2-980B-3A668264EA16");
79 return aHealingDriver;
82 //=======================================================================
83 //function : GEOMImpl_HealingDriver
85 //=======================================================================
86 GEOMImpl_HealingDriver::GEOMImpl_HealingDriver()
90 //=======================================================================
93 //=======================================================================
94 Standard_Integer GEOMImpl_HealingDriver::Execute(TFunction_Logbook& log) const
96 if (Label().IsNull()) return 0;
97 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
99 if (aFunction.IsNull()) return 0;
101 GEOMImpl_IHealing HI (aFunction);
102 Standard_Integer aType = aFunction->GetType();
103 Handle(GEOM_Function) anOriginalFunction = HI.GetOriginal();
104 if (anOriginalFunction.IsNull()) return 0;
105 TopoDS_Shape aShape, anOriginalShape = anOriginalFunction->GetValue();
106 if (anOriginalShape.IsNull()) return 0;
111 ShapeProcess(&HI, anOriginalShape, aShape);
114 SuppressFaces(&HI, anOriginalShape, aShape);
117 CloseContour(&HI, anOriginalShape, aShape);
119 case REMOVE_INT_WIRES:
120 RemoveIntWires(&HI, anOriginalShape, aShape);
123 RemoveHoles(&HI, anOriginalShape, aShape);
126 Sew(&HI, anOriginalShape, aShape);
129 AddPointOnEdge(&HI, anOriginalShape, aShape);
131 case CHANGE_ORIENTATION:
132 ChangeOrientation(&HI, anOriginalShape, aShape);
134 case LIMIT_TOLERANCE:
135 LimitTolerance(&HI, anOriginalShape, aShape);
142 raiseNotDoneExeption( ShHealOper_ErrorExecution );
144 aFunction->SetValue(aShape);
146 log.SetTouched(Label());
150 //=======================================================================
151 //function : ShapeProcess
153 //=======================================================================
154 Standard_Boolean GEOMImpl_HealingDriver::ShapeProcess (GEOMImpl_IHealing* theHI,
155 const TopoDS_Shape& theOriginalShape,
156 TopoDS_Shape& theOutShape) const
158 Handle(TColStd_HArray1OfExtendedString) anOperators = theHI->GetOperators();
159 Handle(TColStd_HArray1OfExtendedString) aParams = theHI->GetParameters();
160 Handle(TColStd_HArray1OfExtendedString) aValues = theHI->GetValues();
162 if (anOperators.IsNull() || anOperators->Length() <= 0)
163 return Standard_False;
165 Standard_Integer nbParams = 0, nbValues = 0;
166 if (!aParams.IsNull()) {
167 nbParams = aParams->Length();
169 if (!aValues.IsNull()) {
170 nbValues = aValues->Length();
172 if (nbParams != nbValues)
173 return Standard_False;
175 ShHealOper_ShapeProcess aHealer;
176 TColStd_SequenceOfAsciiString anOperatorsAS, aParamsAS, aValuesAS;
178 for (i = 1; i <= anOperators->Length(); i++)
179 anOperatorsAS.Append(TCollection_AsciiString(anOperators->Value(i)));
181 aHealer.SetOperators(anOperatorsAS);
183 for (i = 1; i <= nbParams; i++) {
184 aHealer.SetParameter(TCollection_AsciiString(aParams->Value(i)),
185 TCollection_AsciiString(aValues->Value(i)));
188 aHealer.Perform(theOriginalShape, theOutShape);
190 if (!aHealer.isDone())
191 raiseNotDoneExeption( ShHealOper_NotError );
193 return Standard_True;
196 //=======================================================================
197 //function : SupressFaces
199 //=======================================================================
200 void SuppressFacesRec (const TopTools_SequenceOfShape& theShapesFaces,
201 const TopoDS_Shape& theOriginalShape,
202 TopoDS_Shape& theOutShape)
204 if ((theOriginalShape.ShapeType() != TopAbs_COMPOUND &&
205 theOriginalShape.ShapeType() != TopAbs_COMPSOLID))
207 ShHealOper_RemoveFace aHealer (theOriginalShape);
208 Standard_Boolean aResult = aHealer.Perform(theShapesFaces);
211 theOutShape = aHealer.GetResultShape();
213 raiseNotDoneExeption(aHealer.GetErrorStatus());
221 TopTools_MapOfShape mapShape;
222 TopoDS_Iterator It (theOriginalShape, Standard_True, Standard_True);
224 for (; It.More(); It.Next()) {
225 TopoDS_Shape aShape_i = It.Value();
226 if (mapShape.Add(aShape_i)) {
227 // check, if current shape contains at least one of faces to be removed
228 bool isFound = false;
229 TopTools_IndexedMapOfShape aShapes_i;
230 TopExp::MapShapes(aShape_i, aShapes_i);
231 for (int i = 1; i <= theShapesFaces.Length() && !isFound; i++) {
232 const TopoDS_Shape& aFace_i = theShapesFaces.Value(i);
233 if (aShapes_i.Contains(aFace_i)) isFound = true;
236 TopoDS_Shape anOutSh_i;
237 SuppressFacesRec(theShapesFaces, aShape_i, anOutSh_i);
238 if ( !anOutSh_i.IsNull() )
239 BB.Add(CC, anOutSh_i);
243 BB.Add(CC, aShape_i);
251 Standard_Boolean GEOMImpl_HealingDriver::SuppressFaces (GEOMImpl_IHealing* theHI,
252 const TopoDS_Shape& theOriginalShape,
253 TopoDS_Shape& theOutShape) const
255 Handle(TColStd_HArray1OfInteger) aFaces = theHI->GetFaces();
257 Standard_Boolean aResult = Standard_False;
259 if (aFaces.IsNull()) {
260 ShHealOper_RemoveFace aHealer (theOriginalShape);
261 aResult = aHealer.Perform();
264 theOutShape = aHealer.GetResultShape();
266 raiseNotDoneExeption(aHealer.GetErrorStatus());
269 TopTools_SequenceOfShape aShapesFaces;
270 TopTools_IndexedMapOfShape aShapes;
271 TopExp::MapShapes(theOriginalShape, aShapes);
272 for (int i = 1; i <= aFaces->Length(); i++) {
273 int indexOfFace = aFaces->Value(i);
274 TopoDS_Shape aFace = aShapes.FindKey(indexOfFace);
275 aShapesFaces.Append(aFace);
277 SuppressFacesRec(aShapesFaces, theOriginalShape, theOutShape);
278 if ((theOriginalShape.ShapeType() == TopAbs_COMPOUND ||
279 theOriginalShape.ShapeType() == TopAbs_COMPSOLID)) {
280 TopoDS_Shape aSh = theOutShape;
281 theOutShape = GEOMImpl_GlueDriver::GlueFaces(aSh, Precision::Confusion(), Standard_True);
285 return Standard_True;
288 //=======================================================================
289 //function : CloseContour
291 //=======================================================================
292 Standard_Boolean GEOMImpl_HealingDriver::CloseContour (GEOMImpl_IHealing* theHI,
293 const TopoDS_Shape& theOriginalShape,
294 TopoDS_Shape& theOutShape) const
296 Standard_Boolean isByVertex = theHI->GetIsCommonVertex();
297 Handle(TColStd_HArray1OfInteger) aWires = theHI->GetWires();
299 ShHealOper_CloseContour aHealer (theOriginalShape);
301 Standard_Boolean aResult = Standard_False;
302 if ( aWires.IsNull() ) {
303 if ( theOriginalShape.ShapeType() == TopAbs_WIRE )
304 aResult = aHealer.Perform(TopoDS::Wire(theOriginalShape), isByVertex, !isByVertex);
307 TopTools_SequenceOfShape aShapesWires;
308 TopTools_IndexedMapOfShape aShapes;
309 TopExp::MapShapes(theOriginalShape, aShapes);
310 for (int i = 1; i <= aWires->Length(); i++) {
311 int indexOfWire = aWires->Value(i);
312 TopoDS_Shape aWire = aShapes.FindKey(indexOfWire);
313 aShapesWires.Append(aWire);
316 aResult = aHealer.Perform( aShapesWires, isByVertex, !isByVertex );
320 theOutShape = aHealer.GetResultShape();
322 raiseNotDoneExeption( aHealer.GetErrorStatus() );
327 //=======================================================================
328 //function : RemoveIntWires
330 //=======================================================================
331 Standard_Boolean GEOMImpl_HealingDriver::RemoveIntWires (GEOMImpl_IHealing* theHI,
332 const TopoDS_Shape& theOriginalShape,
333 TopoDS_Shape& theOutShape) const
335 Handle(TColStd_HArray1OfInteger) aWires = theHI->GetWires();
337 ShHealOper_RemoveInternalWires aHealer(theOriginalShape);
339 Standard_Boolean aResult = Standard_False;
340 if (aWires.IsNull()) { // remove all faces
341 aResult = aHealer.Remove();
343 TopTools_SequenceOfShape aShapesWires;
344 TopTools_IndexedMapOfShape aShapes;
345 TopExp::MapShapes(theOriginalShape, aShapes);
346 for (int i = 1; i <= aWires->Length(); i++) {
347 int indexOfWire = aWires->Value(i);
348 TopoDS_Shape aWire = aShapes.FindKey(indexOfWire);
349 aShapesWires.Append(aWire);
352 aResult = aHealer.Remove(aShapesWires);
356 theOutShape = aHealer.GetResultShape();
358 raiseNotDoneExeption( aHealer.GetErrorStatus() );
363 //=======================================================================
364 //function : RemoveHoles
366 //=======================================================================
367 Standard_Boolean GEOMImpl_HealingDriver::RemoveHoles (GEOMImpl_IHealing* theHI,
368 const TopoDS_Shape& theOriginalShape,
369 TopoDS_Shape& theOutShape) const
371 Handle(TColStd_HArray1OfInteger) aWires = theHI->GetWires();
373 ShHealOper_FillHoles aHealer (theOriginalShape);
375 Standard_Boolean aResult = Standard_False;
376 if (aWires.IsNull()) { // remove all faces
377 aResult = aHealer.Fill();
379 TopTools_SequenceOfShape aShapesWires;
380 TopTools_IndexedMapOfShape aShapes;
381 TopExp::MapShapes(theOriginalShape, aShapes);
382 for (int i = 1; i <= aWires->Length(); i++) {
383 int indexOfWire = aWires->Value(i);
384 TopoDS_Shape aWire = aShapes.FindKey(indexOfWire);
385 aShapesWires.Append(aWire);
388 aResult = aHealer.Fill(aShapesWires);
392 theOutShape = aHealer.GetResultShape();
394 raiseNotDoneExeption( aHealer.GetErrorStatus() );
399 //=======================================================================
402 //=======================================================================
403 Standard_Boolean GEOMImpl_HealingDriver::Sew (GEOMImpl_IHealing* theHI,
404 const TopoDS_Shape& theOriginalShape,
405 TopoDS_Shape& theOutShape) const
407 Standard_Real aTol = theHI->GetTolerance();
409 ShHealOper_Sewing aHealer (theOriginalShape, aTol);
411 Standard_Boolean aResult = aHealer.Perform();
414 theOutShape = aHealer.GetResultShape();
416 raiseNotDoneExeption( aHealer.GetErrorStatus() );
421 //=======================================================================
422 //function : AddPointOnEdge
424 //=======================================================================
425 Standard_Boolean GEOMImpl_HealingDriver::AddPointOnEdge (GEOMImpl_IHealing* theHI,
426 const TopoDS_Shape& theOriginalShape,
427 TopoDS_Shape& theOutShape) const
429 Standard_Boolean isByParameter = theHI->GetIsByParameter();
430 Standard_Integer anIndex = theHI->GetIndex();
431 Standard_Real aValue = theHI->GetDevideEdgeValue();
433 ShHealOper_EdgeDivide aHealer (theOriginalShape);
435 Standard_Boolean aResult = Standard_False;
436 if (anIndex == -1) { // apply algorythm for the whole shape which is EDGE
437 if (theOriginalShape.ShapeType() == TopAbs_EDGE)
438 aResult = aHealer.Perform(TopoDS::Edge(theOriginalShape), aValue, isByParameter);
440 TopTools_IndexedMapOfShape aShapes;
441 TopExp::MapShapes(theOriginalShape, aShapes);
442 TopoDS_Shape aEdgeShape = aShapes.FindKey(anIndex);
443 if (aEdgeShape.ShapeType() == TopAbs_EDGE)
444 aResult = aHealer.Perform(TopoDS::Edge(aEdgeShape), aValue, isByParameter);
448 theOutShape = aHealer.GetResultShape();
450 raiseNotDoneExeption( aHealer.GetErrorStatus() );
456 //=======================================================================
457 //function : ChangeOrientation
459 //=======================================================================
460 Standard_Boolean GEOMImpl_HealingDriver::ChangeOrientation (GEOMImpl_IHealing* theHI,
461 const TopoDS_Shape& theOriginalShape,
462 TopoDS_Shape& theOutShape) const
464 ShHealOper_ChangeOrientation aHealer (theOriginalShape);
466 Standard_Boolean aResult = aHealer.Perform();
469 theOutShape = aHealer.GetResultShape();
471 raiseNotDoneExeption( aHealer.GetErrorStatus() );
476 //=======================================================================
477 //function : LimitTolerance
479 //=======================================================================
480 void GEOMImpl_HealingDriver::LimitTolerance (GEOMImpl_IHealing* theHI,
481 const TopoDS_Shape& theOriginalShape,
482 TopoDS_Shape& theOutShape) const
484 Standard_Real aTol = theHI->GetTolerance();
485 if (aTol < Precision::Confusion())
486 aTol = Precision::Confusion();
488 // 1. Make a copy to prevent the original shape changes.
489 TopoDS_Shape aShapeCopy;
490 TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
491 TNaming_CopyShape::CopyTool(theOriginalShape, aMapTShapes, aShapeCopy);
493 // 2. Limit tolerance.
494 ShapeFix_ShapeTolerance aSFT;
495 aSFT.LimitTolerance(aShapeCopy, aTol, aTol, TopAbs_SHAPE);
497 // 3. Fix obtained shape.
498 Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape (aShapeCopy);
500 theOutShape = aSfs->Shape();
502 BRepCheck_Analyzer ana (theOutShape, Standard_True);
504 StdFail_NotDone::Raise("Non valid shape result");
507 //=======================================================================
508 //function : GEOMImpl_HealingDriver_Type_
510 //=======================================================================
511 Standard_EXPORT Handle_Standard_Type& GEOMImpl_HealingDriver_Type_()
514 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
515 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
516 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
517 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
518 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
519 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
522 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
523 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_HealingDriver",
524 sizeof(GEOMImpl_HealingDriver),
526 (Standard_Address)_Ancestors,
527 (Standard_Address)NULL);
532 //=======================================================================
533 //function : DownCast
535 //=======================================================================
537 const Handle(GEOMImpl_HealingDriver) Handle(GEOMImpl_HealingDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
539 Handle(GEOMImpl_HealingDriver) _anOtherObject;
541 if (!AnObject.IsNull()) {
542 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_HealingDriver))) {
543 _anOtherObject = Handle(GEOMImpl_HealingDriver)((Handle(GEOMImpl_HealingDriver)&)AnObject);
547 return _anOtherObject;