1 // Copyright (C) 2007-2015 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, or (at your option) any later version.
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);
190 myResultShape = myContext->Apply(myResultShape);
191 myStatistics.AddModif( "Face created to fill hole" , aSeqWires->Length() );
196 //=======================================================================
197 //function : isCircle
199 //=======================================================================
200 static Standard_Boolean isCircle(const TopoDS_Edge theEdge)
202 Standard_Real aFirst, aLast;
203 Handle(Geom_Curve) aC3D = BRep_Tool::Curve(theEdge,aFirst, aLast );
204 if(aC3D.IsNull()) return Standard_False;
205 Standard_Boolean isCirc = (aC3D->Value(aFirst).Distance(aC3D->Value(aLast)) <
206 aC3D->Value(aFirst).Distance(aC3D->Value((aFirst +aLast)/2)));
209 //=======================================================================
210 //function : prepareWires
212 //=======================================================================
214 Standard_Boolean ShHealOper_FillHoles::prepareWires(const TopTools_SequenceOfShape& theFillShapes,
215 Handle(TopTools_HSequenceOfShape)& theSeqWires)
217 Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
218 Standard_Integer i =1;
219 for( ; i <= theFillShapes.Length(); i++) {
220 TopExp_Explorer aExp;
221 for (aExp.Init (theFillShapes.Value(i),TopAbs_WIRE); aExp.More(); aExp.Next()) {
222 TopoDS_Iterator aIt(aExp.Current());
223 Standard_Boolean isAdd = Standard_True;
224 for( ; aIt.More() && isAdd; aIt.Next()) {
225 if(myEdgeFaces.Contains(aIt.Value()))
226 isAdd = (myEdgeFaces.FindFromKey(aIt.Value()).Extent() <2);
229 theSeqWires->Append(aExp.Current());
231 for (aExp.Init (theFillShapes.Value(i),TopAbs_EDGE, TopAbs_WIRE); aExp.More(); aExp.Next()) {
232 if (!BRep_Tool::Degenerated (TopoDS::Edge (aExp.Current())))
233 if(myEdgeFaces.Contains(aExp.Current()) && myEdgeFaces.FindFromKey(aExp.Current()).Extent() >1)
235 aSeqEdges->Append(aExp.Current());
239 if(aSeqEdges->Length())
241 Standard_Real aTol = 0.;
242 Standard_Boolean aShared = Standard_True;
243 Handle(TopTools_HSequenceOfShape) aTmpWires = new TopTools_HSequenceOfShape;
244 ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdges, aTol, aShared, aTmpWires);
245 Handle(TopTools_HSequenceOfShape) anWiresClosed = new TopTools_HSequenceOfShape,
246 anWiresOpen = new TopTools_HSequenceOfShape;
247 ShapeAnalysis_FreeBounds::SplitWires(aTmpWires, aTol, aShared, anWiresClosed, anWiresOpen);
249 for (i = 1; i <= anWiresClosed->Length(); i++)
250 theSeqWires->Append (anWiresClosed->Value (i));
251 for (i = 1; i <= anWiresOpen->Length(); i++)
252 theSeqWires->Append (anWiresOpen->Value (i));
255 for( i =1; i <= theSeqWires->Length(); i++) {
256 TopoDS_Wire aWire = TopoDS::Wire(theSeqWires->Value(i));
258 TopoDS_Iterator aIt(aWire);
259 Standard_Integer ne =0;
261 for( ; aIt.More(); aIt.Next(), ne++)
262 ae = TopoDS::Edge(aIt.Value());
263 if((ne == 1) && ( !isCircle(ae))) {
264 theSeqWires->Remove(i--);
268 return (theSeqWires->Length());
270 //=======================================================================
271 //function : buildSurface
273 //=======================================================================
275 Handle(Geom_Surface) ShHealOper_FillHoles::buildSurface(const TopoDS_Wire& theWire,
276 Handle(TColGeom2d_HArray1OfCurve)& theCurves2d,
277 Handle(TColStd_HArray1OfInteger)& theOrders,
278 Handle(TColStd_HArray1OfInteger)& theSenses)
280 Handle(Geom_BSplineSurface) aSurf;
282 GeomPlate_BuildPlateSurface aBuilder(myDegree, myNbPtsOnCur, myNbIter,
283 myTol2d, myTol3d, myTolAng, myTolCrv);
284 TopoDS_Iterator aIter;
285 for(aIter.Initialize (theWire); aIter.More(); aIter.Next()) {
287 TopoDS_Edge ae = TopoDS::Edge(aIter.Value());
288 BRepAdaptor_Curve adC(ae);
289 Handle(BRepAdaptor_HCurve) aHAD= new BRepAdaptor_HCurve(adC);
290 Handle(BRepFill_CurveConstraint) aConst =
291 new BRepFill_CurveConstraint (aHAD, (Standard_Integer) GeomAbs_C0, myNbPtsOnCur, myTol3d);
292 //Handle(GeomPlate_CurveConstraint) aConst =
293 // new GeomPlate_CurveConstraint(aHAD, (Standard_Integer) GeomAbs_C0, myNbPtsOnCur, myTol3d);
294 aBuilder.Add (aConst);
297 if(!aBuilder.IsDone())
299 Handle(GeomPlate_Surface) aPlSurf = aBuilder.Surface();
301 //for filling holes without initial specified surface
302 //the initial surface should be build by GeomPlate itself
303 //following code was taken from BRepFill_Filling::Build
305 Standard_Real aDist = aBuilder.G0Error();
306 TColgp_SequenceOfXY S2d;
307 TColgp_SequenceOfXYZ S3d;
310 aBuilder.Disc2dContour(4,S2d);
311 aBuilder.Disc3dContour(4,0,S3d);
312 Standard_Real amaxTol = Max( myTol3d, 10* aDist);
313 GeomPlate_PlateG0Criterion Criterion( S2d, S3d, amaxTol );
314 GeomPlate_MakeApprox Approx( aPlSurf, Criterion, myTol3d, myMaxSeg, myMaxDeg );
315 aSurf = Approx.Surface();
319 theCurves2d = aBuilder.Curves2d();
320 theOrders = aBuilder.Order();
321 theSenses = aBuilder.Sense();
324 catch (Standard_Failure) {
331 //=======================================================================
334 //=======================================================================
336 Standard_Boolean ShHealOper_FillHoles::addFace(const Handle(Geom_Surface)& theSurf,
337 const TopoDS_Wire& theWire,
338 const Handle(TColGeom2d_HArray1OfCurve)& theCurves2d,
339 const Handle(TColStd_HArray1OfInteger)& theOrders,
340 const Handle(TColStd_HArray1OfInteger)& theSenses)
342 BRepBuilderAPI_MakeFace aMakeFace (theSurf, Precision::Confusion());
343 TopoDS_Face aFace = aMakeFace.Face();
350 TopTools_IndexedMapOfShape aMapParent;
351 Standard_Integer aInd = 1;
352 Standard_Boolean hasShell = Standard_False;
353 TopoDS_Iterator aIter(theWire);
354 for ( ; aIter.More(); aIter.Next(), aInd++) {
355 TopoDS_Edge anEdge = TopoDS::Edge (aIter.Value());
356 Standard_Real aF, aL;
357 BRep_Tool::Range (anEdge, aF, aL);
358 TopLoc_Location aLoc;
359 aB.UpdateEdge (anEdge, theCurves2d->Value (aInd),aFace, 0.);
361 aB.Range (anEdge, aFace, aF, aL);
363 // Set orientation of the edge: orientation should be changed
364 // if its orientation does not make sence with curve orientation
365 // recommended by GeomPlate
366 if ((anEdge.Orientation() == TopAbs_FORWARD) ==
367 (theSenses->Value (theOrders->Value (aInd)) == 1)) {
370 aB.SameParameter(anEdge,Standard_False);
371 aB.Add (aWire, anEdge);
372 TopoDS_Shape aParent;
373 if(!myEdgeFaces.Contains(anEdge))
376 if(myEdgeFaces.FindFromKey(anEdge).Extent() >1)
379 if(myEdgeShells.Contains(anEdge)) {
380 if(myEdgeShells.FindFromKey(anEdge).Extent()) {
381 aParent = myEdgeShells.FindFromKey(anEdge).First();
382 hasShell = Standard_True;
383 aMapParent.Add(aParent);
386 else if(myEdgeComps.Contains(anEdge)) {
387 if(myEdgeComps.FindFromKey(anEdge).Extent()) {
388 aParent = myEdgeComps.FindFromKey(anEdge).First();
389 aMapParent.Add(aParent);
395 Handle(ShapeFix_Face) aSff = new ShapeFix_Face(aFace);
396 aSff->SetContext(myContext);
397 aSff->SetPrecision(myTol3d);
399 if(aSff->Status(ShapeExtend_FAIL)) {
400 myErrorStatus = ShHealOper_ErrorExecution;
401 return Standard_False;
403 //theFace = aSff->Face();
404 TopoDS_Shape aResShape = aSff->Result();
405 getResShape(aResShape,aMapParent,hasShell);
406 return Standard_True;
409 //=======================================================================
410 //function : getResShape
412 //=======================================================================
414 void ShHealOper_FillHoles::getResShape(const TopoDS_Shape& theAddShape,
415 const TopTools_IndexedMapOfShape& aMapParent,
416 const Standard_Boolean theHasShell)
420 if(!aMapParent.Extent()) {
421 TopoDS_Compound aComp;
422 aB.MakeCompound(aComp);
423 TopoDS_Shape aresShape = myContext->Apply(myResultShape);
424 aB.Add(aComp,aresShape);
425 aB.Add(aComp,theAddShape);
426 myResultShape = aComp;
429 Standard_Boolean anhasShell = theHasShell;
430 TopoDS_Shell aTmpShell;
431 aB.MakeShell(aTmpShell);
432 TopTools_SequenceOfShape aseqShells;
434 aB.Add(aTmpShell,theAddShape);
435 Standard_Integer i =1;
436 for( ; i <= aMapParent.Extent(); i++) {
437 TopoDS_Shape aParShape = myContext->Apply(aMapParent.FindKey(i));
438 if(aParShape.ShapeType() == TopAbs_SHELL) {
439 TopExp_Explorer aexp(aParShape,TopAbs_FACE);
440 for( ; aexp.More(); aexp.Next())
441 aB.Add(aTmpShell,aexp.Current());
442 aseqShells.Append(aParShape);
445 anhasShell = aseqShells.Length();
448 Handle(ShapeFix_Shell) asfs = new ShapeFix_Shell;
449 asfs->FixFaceOrientation(aTmpShell);
450 TopoDS_Shape anshape = asfs->Shape();
451 myContext->Replace(aseqShells.Value(1),anshape);
452 Standard_Integer i =2;
453 for( ; i<= aseqShells.Length(); i++)
454 myContext->Remove(aseqShells.Value(i));
457 TopoDS_Compound aComp;
458 aB.MakeCompound(aComp);
459 TopoDS_Shape oldshape = myContext->Apply(aMapParent.FindKey(1));
460 TopoDS_Iterator aIt(oldshape);
461 for( ; aIt.More(); aIt.Next())
462 aB.Add(aComp,aIt.Value());
463 aB.Add(aComp,theAddShape);
464 myContext->Replace( oldshape,aComp);