1 // Copyright (C) 2007-2012 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
23 // File: ShHealOper_FillHoles.cxx
24 // Created: 26.04.04 17:35:30
25 // Author: Galina KULIKOVA
27 #include <Basics_OCCTVersion.hxx>
29 #include <ShapeFix_Shell.hxx>
30 #include <ShapeFix_Face.hxx>
31 #include <ShHealOper_FillHoles.hxx>
32 #include <ShapeAnalysis_FreeBounds.hxx>
34 #include <BRep_Tool.hxx>
35 #include <BRepAdaptor_Curve.hxx>
36 #include <BRepAdaptor_HCurve.hxx>
37 #include <BRep_Builder.hxx>
38 #include <BRepFill_CurveConstraint.hxx>
39 #include <BRepBuilderAPI_MakeFace.hxx>
41 #include <TopAbs_ShapeEnum.hxx>
43 #include <TopExp_Explorer.hxx>
45 #include <TopoDS_Edge.hxx>
46 #include <TopoDS_Shell.hxx>
47 #include <TopoDS_Compound.hxx>
48 #include <TopoDS_Iterator.hxx>
49 #include <TopLoc_Location.hxx>
50 #include <TopTools_ListOfShape.hxx>
51 #include <TopTools_HSequenceOfShape.hxx>
52 #include <TopTools_IndexedMapOfShape.hxx>
54 #include <Geom_Curve.hxx>
55 #include <Geom_BSplineSurface.hxx>
56 #include <GeomPlate_Surface.hxx>
57 #include <GeomPlate_MakeApprox.hxx>
58 #include <GeomPlate_CurveConstraint.hxx>
59 #include <GeomPlate_PlateG0Criterion.hxx>
60 #include <GeomPlate_BuildPlateSurface.hxx>
62 #include <TColgp_SequenceOfXY.hxx>
63 #include <TColgp_SequenceOfXYZ.hxx>
65 #include <Precision.hxx>
67 //=======================================================================
68 //function : ShHealOper_FillHoles()
69 //purpose : Constructor
70 //=======================================================================
71 ShHealOper_FillHoles::ShHealOper_FillHoles ()
76 //=======================================================================
77 //function : ShHealOper_FillHoles
79 //=======================================================================
80 ShHealOper_FillHoles::ShHealOper_FillHoles (const TopoDS_Shape& theShape)
86 //=======================================================================
89 //=======================================================================
90 void ShHealOper_FillHoles::Init(const TopoDS_Shape& theShape)
92 ShHealOper_Tool::Init(theShape);
93 TopExp::MapShapesAndAncestors( myInitShape, TopAbs_EDGE,TopAbs_SHELL , myEdgeShells);
94 TopExp::MapShapesAndAncestors ( myInitShape, TopAbs_EDGE,TopAbs_COMPOUND, myEdgeComps );
96 TopExp::MapShapesAndAncestors ( myInitShape, TopAbs_EDGE,TopAbs_FACE, myEdgeFaces );
99 //=======================================================================
100 //function : InitParameters
102 //=======================================================================
103 void ShHealOper_FillHoles::InitParameters(Standard_Integer theDegree,
104 Standard_Integer theNbPtsOnCur,
105 Standard_Integer theNbIter,
106 Standard_Real theTol3d,
107 Standard_Real theTol2d,
108 Standard_Real theTolAng,
109 Standard_Real theTolCrv,
110 Standard_Integer theMaxDeg,
111 Standard_Integer theMaxSeg)
113 myNbPtsOnCur = theNbPtsOnCur;
114 myNbIter = theNbIter;
118 myTolAng = theTolAng;
119 myTolCrv = theTolCrv;
120 myMaxDeg = theMaxDeg;
123 //=======================================================================
126 //=======================================================================
127 Standard_Boolean ShHealOper_FillHoles::Fill()
129 ShapeAnalysis_FreeBounds sab(myInitShape);
130 TopoDS_Compound aCompClosed = sab.GetClosedWires();
131 TopoDS_Compound aCompOpen = sab.GetOpenWires();
132 TopTools_SequenceOfShape aFillWires;
133 if(!aCompClosed.IsNull()) {
134 TopoDS_Iterator aIt(aCompClosed);
136 for( ; aIt.More(); aIt.Next())
137 aFillWires.Append(aIt.Value());
139 if(!aCompOpen.IsNull()) {
140 TopoDS_Iterator aIt(aCompOpen);
141 for( ; aIt.More(); aIt.Next())
142 aFillWires.Append(aIt.Value());
145 TopExp_Explorer aExp(myInitShape,TopAbs_EDGE,TopAbs_FACE);
147 for( ; aExp.More(); aExp.Next())
148 aFillWires.Append(aExp.Current());
150 return Fill(aFillWires);
153 //=======================================================================
156 //=======================================================================
157 Standard_Boolean ShHealOper_FillHoles::Fill(const TopTools_SequenceOfShape& theFillShapes)
159 myDone = Standard_False;
160 myErrorStatus = ShHealOper_NotError;
161 if(myInitShape.IsNull()) {
162 myErrorStatus = ShHealOper_InvalidParameters;
165 if(!theFillShapes.Length()) {
169 Handle(TopTools_HSequenceOfShape) aSeqWires = new TopTools_HSequenceOfShape;
170 if(!prepareWires(theFillShapes,aSeqWires)) {
171 myErrorStatus = ShHealOper_InvalidParameters;
175 myResultShape = myInitShape;
176 Standard_Integer i =1;
177 for( ; i <= aSeqWires->Length(); i++) {
178 TopoDS_Wire aWire = TopoDS::Wire(aSeqWires->Value(i));
179 Handle(TColGeom2d_HArray1OfCurve) aCurves2d;
180 Handle(TColStd_HArray1OfInteger) aOrders;
181 Handle(TColStd_HArray1OfInteger) aSenses;
182 Handle(Geom_Surface) aSurf = buildSurface(aWire,aCurves2d,aOrders,aSenses);
184 myErrorStatus = ShHealOper_ErrorExecution;
186 myDone = (addFace(aSurf,aWire,aCurves2d,aOrders,aSenses) || myDone);
189 myResultShape = myContext->Apply(myResultShape);
193 //=======================================================================
194 //function : isCircle
196 //=======================================================================
197 static Standard_Boolean isCircle(const TopoDS_Edge theEdge)
199 Standard_Real aFirst, aLast;
200 Handle(Geom_Curve) aC3D = BRep_Tool::Curve(theEdge,aFirst, aLast );
201 if(aC3D.IsNull()) return Standard_False;
202 Standard_Boolean isCirc = (aC3D->Value(aFirst).Distance(aC3D->Value(aLast)) <
203 aC3D->Value(aFirst).Distance(aC3D->Value((aFirst +aLast)/2)));
206 //=======================================================================
207 //function : prepareWires
209 //=======================================================================
211 Standard_Boolean ShHealOper_FillHoles::prepareWires(const TopTools_SequenceOfShape& theFillShapes,
212 Handle(TopTools_HSequenceOfShape)& theSeqWires)
214 Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
215 Standard_Integer i =1;
216 for( ; i <= theFillShapes.Length(); i++) {
217 TopExp_Explorer aExp;
218 for (aExp.Init (theFillShapes.Value(i),TopAbs_WIRE); aExp.More(); aExp.Next()) {
219 TopoDS_Iterator aIt(aExp.Current());
220 Standard_Boolean isAdd = Standard_True;
221 for( ; aIt.More() && isAdd; aIt.Next()) {
222 if(myEdgeFaces.Contains(aIt.Value()))
223 isAdd = (myEdgeFaces.FindFromKey(aIt.Value()).Extent() <2);
226 theSeqWires->Append(aExp.Current());
228 for (aExp.Init (theFillShapes.Value(i),TopAbs_EDGE, TopAbs_WIRE); aExp.More(); aExp.Next()) {
229 if (!BRep_Tool::Degenerated (TopoDS::Edge (aExp.Current())))
230 if(myEdgeFaces.Contains(aExp.Current()) && myEdgeFaces.FindFromKey(aExp.Current()).Extent() >1)
232 aSeqEdges->Append(aExp.Current());
236 if(aSeqEdges->Length())
238 Standard_Real aTol = 0.;
239 Standard_Boolean aShared = Standard_True;
240 Handle(TopTools_HSequenceOfShape) aTmpWires = new TopTools_HSequenceOfShape;
241 ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdges, aTol, aShared, aTmpWires);
242 Handle(TopTools_HSequenceOfShape) anWiresClosed = new TopTools_HSequenceOfShape,
243 anWiresOpen = new TopTools_HSequenceOfShape;
244 ShapeAnalysis_FreeBounds::SplitWires(aTmpWires, aTol, aShared, anWiresClosed, anWiresOpen);
246 for (i = 1; i <= anWiresClosed->Length(); i++)
247 theSeqWires->Append (anWiresClosed->Value (i));
248 for (i = 1; i <= anWiresOpen->Length(); i++)
249 theSeqWires->Append (anWiresOpen->Value (i));
252 for( i =1; i <= theSeqWires->Length(); i++) {
253 TopoDS_Wire aWire = TopoDS::Wire(theSeqWires->Value(i));
255 TopoDS_Iterator aIt(aWire);
256 Standard_Integer ne =0;
258 for( ; aIt.More(); aIt.Next(), ne++)
259 ae = TopoDS::Edge(aIt.Value());
260 if((ne == 1) && ( !isCircle(ae))) {
261 theSeqWires->Remove(i--);
265 return (theSeqWires->Length());
267 //=======================================================================
268 //function : buildSurface
270 //=======================================================================
272 Handle(Geom_Surface) ShHealOper_FillHoles::buildSurface(const TopoDS_Wire& theWire,
273 Handle(TColGeom2d_HArray1OfCurve)& theCurves2d,
274 Handle(TColStd_HArray1OfInteger)& theOrders,
275 Handle(TColStd_HArray1OfInteger)& theSenses)
277 Handle(Geom_BSplineSurface) aSurf;
279 GeomPlate_BuildPlateSurface aBuilder(myDegree, myNbPtsOnCur, myNbIter,
280 myTol2d, myTol3d, myTolAng, myTolCrv);
281 TopoDS_Iterator aIter;
282 for(aIter.Initialize (theWire); aIter.More(); aIter.Next()) {
284 TopoDS_Edge ae = TopoDS::Edge(aIter.Value());
285 BRepAdaptor_Curve adC(ae);
286 Handle(BRepAdaptor_HCurve) aHAD= new BRepAdaptor_HCurve(adC);
287 Handle(BRepFill_CurveConstraint) aConst =
288 new BRepFill_CurveConstraint (aHAD, (Standard_Integer) GeomAbs_C0, myNbPtsOnCur, myTol3d);
289 //Handle(GeomPlate_CurveConstraint) aConst =
290 // new GeomPlate_CurveConstraint(aHAD, (Standard_Integer) GeomAbs_C0, myNbPtsOnCur, myTol3d);
291 aBuilder.Add (aConst);
294 if(!aBuilder.IsDone())
296 Handle(GeomPlate_Surface) aPlSurf = aBuilder.Surface();
298 //for filling holes without initial specified surface
299 //the initial surface should be build by GeomPlate itself
300 //following code was taken from BRepFill_Filling::Build
302 Standard_Real aDist = aBuilder.G0Error();
303 TColgp_SequenceOfXY S2d;
304 TColgp_SequenceOfXYZ S3d;
307 aBuilder.Disc2dContour(4,S2d);
308 aBuilder.Disc3dContour(4,0,S3d);
309 Standard_Real amaxTol = Max( myTol3d, 10* aDist);
310 GeomPlate_PlateG0Criterion Criterion( S2d, S3d, amaxTol );
311 GeomPlate_MakeApprox Approx( aPlSurf, Criterion, myTol3d, myMaxSeg, myMaxDeg );
312 aSurf = Approx.Surface();
316 theCurves2d = aBuilder.Curves2d();
317 theOrders = aBuilder.Order();
318 theSenses = aBuilder.Sense();
321 catch (Standard_Failure) {
328 //=======================================================================
331 //=======================================================================
333 Standard_Boolean ShHealOper_FillHoles::addFace(const Handle(Geom_Surface)& theSurf,
334 const TopoDS_Wire& theWire,
335 const Handle(TColGeom2d_HArray1OfCurve)& theCurves2d,
336 const Handle(TColStd_HArray1OfInteger)& theOrders,
337 const Handle(TColStd_HArray1OfInteger)& theSenses)
339 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
340 BRepBuilderAPI_MakeFace aMakeFace (theSurf, Precision::Confusion());
342 BRepBuilderAPI_MakeFace aMakeFace (theSurf);
344 TopoDS_Face aFace = aMakeFace.Face();
351 TopTools_IndexedMapOfShape aMapParent;
352 Standard_Integer aInd = 1;
353 Standard_Boolean hasShell = Standard_False;
354 TopoDS_Iterator aIter(theWire);
355 for ( ; aIter.More(); aIter.Next(), aInd++) {
356 TopoDS_Edge anEdge = TopoDS::Edge (aIter.Value());
357 Standard_Real aF, aL;
358 BRep_Tool::Range (anEdge, aF, aL);
359 TopLoc_Location aLoc;
360 aB.UpdateEdge (anEdge, theCurves2d->Value (aInd),aFace, 0.);
362 aB.Range (anEdge, aFace, aF, aL);
364 // Set orientation of the edge: orientation should be changed
365 // if its orientation does not make sence with curve orientation
366 // recommended by GeomPlate
367 if ((anEdge.Orientation() == TopAbs_FORWARD) ==
368 (theSenses->Value (theOrders->Value (aInd)) == 1)) {
371 aB.SameParameter(anEdge,Standard_False);
372 aB.Add (aWire, anEdge);
373 TopoDS_Shape aParent;
374 if(!myEdgeFaces.Contains(anEdge))
377 if(myEdgeFaces.FindFromKey(anEdge).Extent() >1)
380 if(myEdgeShells.Contains(anEdge)) {
381 if(myEdgeShells.FindFromKey(anEdge).Extent()) {
382 aParent = myEdgeShells.FindFromKey(anEdge).First();
383 hasShell = Standard_True;
384 aMapParent.Add(aParent);
387 else if(myEdgeComps.Contains(anEdge)) {
388 if(myEdgeComps.FindFromKey(anEdge).Extent()) {
389 aParent = myEdgeComps.FindFromKey(anEdge).First();
390 aMapParent.Add(aParent);
396 Handle(ShapeFix_Face) aSff = new ShapeFix_Face(aFace);
397 aSff->SetContext(myContext);
398 aSff->SetPrecision(myTol3d);
400 if(aSff->Status(ShapeExtend_FAIL)) {
401 myErrorStatus = ShHealOper_ErrorExecution;
402 return Standard_False;
404 //theFace = aSff->Face();
405 TopoDS_Shape aResShape = aSff->Result();
406 getResShape(aResShape,aMapParent,hasShell);
407 return Standard_True;
410 //=======================================================================
411 //function : getResShape
413 //=======================================================================
415 void ShHealOper_FillHoles::getResShape(const TopoDS_Shape& theAddShape,
416 const TopTools_IndexedMapOfShape& aMapParent,
417 const Standard_Boolean theHasShell)
421 if(!aMapParent.Extent()) {
422 TopoDS_Compound aComp;
423 aB.MakeCompound(aComp);
424 TopoDS_Shape aresShape = myContext->Apply(myResultShape);
425 aB.Add(aComp,aresShape);
426 aB.Add(aComp,theAddShape);
427 myResultShape = aComp;
430 Standard_Boolean anhasShell = theHasShell;
431 TopoDS_Shell aTmpShell;
432 aB.MakeShell(aTmpShell);
433 TopTools_SequenceOfShape aseqShells;
435 aB.Add(aTmpShell,theAddShape);
436 Standard_Integer i =1;
437 for( ; i <= aMapParent.Extent(); i++) {
438 TopoDS_Shape aParShape = myContext->Apply(aMapParent.FindKey(i));
439 if(aParShape.ShapeType() == TopAbs_SHELL) {
440 TopExp_Explorer aexp(aParShape,TopAbs_FACE);
441 for( ; aexp.More(); aexp.Next())
442 aB.Add(aTmpShell,aexp.Current());
443 aseqShells.Append(aParShape);
446 anhasShell = aseqShells.Length();
449 Handle(ShapeFix_Shell) asfs = new ShapeFix_Shell;
450 asfs->FixFaceOrientation(aTmpShell);
451 TopoDS_Shape anshape = asfs->Shape();
452 myContext->Replace(aseqShells.Value(1),anshape);
453 Standard_Integer i =2;
454 for( ; i<= aseqShells.Length(); i++)
455 myContext->Remove(aseqShells.Value(i));
458 TopoDS_Compound aComp;
459 aB.MakeCompound(aComp);
460 TopoDS_Shape oldshape = myContext->Apply(aMapParent.FindKey(1));
461 TopoDS_Iterator aIt(oldshape);
462 for( ; aIt.More(); aIt.Next())
463 aB.Add(aComp,aIt.Value());
464 aB.Add(aComp,theAddShape);
465 myContext->Replace( oldshape,aComp);