Salome HOME
Update copyrights 2014.
[modules/geom.git] / src / ShHealOper / ShHealOper_FillHoles.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 // File:      ShHealOper_FillHoles.cxx
24 // Created:   26.04.04 17:35:30
25 // Author:    Galina KULIKOVA
26
27 #include <Basics_OCCTVersion.hxx>
28
29 #include <ShapeFix_Shell.hxx>
30 #include <ShapeFix_Face.hxx>
31 #include <ShHealOper_FillHoles.hxx>
32 #include <ShapeAnalysis_FreeBounds.hxx>
33
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>
40
41 #include <TopAbs_ShapeEnum.hxx>
42 #include <TopExp.hxx>
43 #include <TopExp_Explorer.hxx>
44 #include <TopoDS.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>
53
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>
61
62 #include <TColgp_SequenceOfXY.hxx>
63 #include <TColgp_SequenceOfXYZ.hxx>
64
65 #include <Precision.hxx>
66
67 //=======================================================================
68 //function : ShHealOper_FillHoles()
69 //purpose  : Constructor
70 //=======================================================================
71 ShHealOper_FillHoles::ShHealOper_FillHoles ()
72 {
73   InitParameters();
74 }
75
76 //=======================================================================
77 //function : ShHealOper_FillHoles
78 //purpose  :
79 //=======================================================================
80 ShHealOper_FillHoles::ShHealOper_FillHoles (const TopoDS_Shape& theShape)
81 {
82   Init(theShape);
83   InitParameters();
84 }
85
86 //=======================================================================
87 //function : Init
88 //purpose  :
89 //=======================================================================
90 void ShHealOper_FillHoles::Init(const TopoDS_Shape& theShape)
91 {
92   ShHealOper_Tool::Init(theShape);
93   TopExp::MapShapesAndAncestors( myInitShape, TopAbs_EDGE,TopAbs_SHELL   , myEdgeShells);
94   TopExp::MapShapesAndAncestors ( myInitShape, TopAbs_EDGE,TopAbs_COMPOUND, myEdgeComps );
95
96   TopExp::MapShapesAndAncestors ( myInitShape, TopAbs_EDGE,TopAbs_FACE, myEdgeFaces );
97 }
98
99 //=======================================================================
100 //function : InitParameters
101 //purpose  :
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)
112 {
113   myNbPtsOnCur = theNbPtsOnCur;
114   myNbIter = theNbIter;
115   myDegree =theDegree;
116   myTol2d = theTol2d;
117   myTol3d = theTol3d;
118   myTolAng = theTolAng;
119   myTolCrv = theTolCrv;
120   myMaxDeg = theMaxDeg;
121   myMaxSeg =theMaxSeg;
122 }
123 //=======================================================================
124 //function : Fill
125 //purpose  :
126 //=======================================================================
127 Standard_Boolean ShHealOper_FillHoles::Fill()
128 {
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);
135
136     for( ; aIt.More(); aIt.Next())
137       aFillWires.Append(aIt.Value());
138   }
139   if(!aCompOpen.IsNull()) {
140     TopoDS_Iterator aIt(aCompOpen);
141     for(  ; aIt.More(); aIt.Next())
142       aFillWires.Append(aIt.Value());
143   }
144
145   TopExp_Explorer aExp(myInitShape,TopAbs_EDGE,TopAbs_FACE);
146
147   for( ; aExp.More(); aExp.Next())
148     aFillWires.Append(aExp.Current());
149
150   return Fill(aFillWires);
151 }
152
153 //=======================================================================
154 //function : Fill
155 //purpose  :
156 //=======================================================================
157 Standard_Boolean ShHealOper_FillHoles::Fill(const TopTools_SequenceOfShape& theFillShapes)
158 {
159   myDone = Standard_False;
160   myErrorStatus = ShHealOper_NotError;
161   if(myInitShape.IsNull()) {
162     myErrorStatus = ShHealOper_InvalidParameters;
163     return myDone;
164   }
165   if(!theFillShapes.Length()) {
166     return myDone;
167   }
168
169   Handle(TopTools_HSequenceOfShape) aSeqWires = new TopTools_HSequenceOfShape;
170   if(!prepareWires(theFillShapes,aSeqWires)) {
171     myErrorStatus = ShHealOper_InvalidParameters;
172     return myDone;
173   }
174
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);
183     if(aSurf.IsNull())
184       myErrorStatus = ShHealOper_ErrorExecution;
185     else
186       myDone = (addFace(aSurf,aWire,aCurves2d,aOrders,aSenses) || myDone);
187   }
188   if(myDone)
189     myResultShape = myContext->Apply(myResultShape);
190   return myDone;
191 }
192
193 //=======================================================================
194 //function : isCircle
195 //purpose  :
196 //=======================================================================
197 static Standard_Boolean isCircle(const TopoDS_Edge theEdge)
198 {
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)));
204   return isCirc;
205 }
206 //=======================================================================
207 //function : prepareWires
208 //purpose  :
209 //=======================================================================
210
211 Standard_Boolean ShHealOper_FillHoles::prepareWires(const TopTools_SequenceOfShape& theFillShapes,
212                                                     Handle(TopTools_HSequenceOfShape)& theSeqWires)
213 {
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);
224       }
225       if(isAdd)
226         theSeqWires->Append(aExp.Current());
227     }
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)
231           continue;
232         aSeqEdges->Append(aExp.Current());
233     }
234   }
235
236   if(aSeqEdges->Length())
237   {
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);
245
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));
250   }
251
252   for( i =1; i <= theSeqWires->Length(); i++) {
253     TopoDS_Wire aWire = TopoDS::Wire(theSeqWires->Value(i));
254
255     TopoDS_Iterator aIt(aWire);
256     Standard_Integer ne =0;
257     TopoDS_Edge ae;
258     for( ; aIt.More(); aIt.Next(), ne++)
259       ae = TopoDS::Edge(aIt.Value());
260     if((ne == 1) && ( !isCircle(ae))) {
261       theSeqWires->Remove(i--);
262       continue;
263     }
264   }
265   return (theSeqWires->Length());
266 }
267 //=======================================================================
268 //function : buildSurface
269 //purpose  :
270 //=======================================================================
271
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)
276 {
277   Handle(Geom_BSplineSurface) aSurf;
278   try {
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()) {
283
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);
292       }
293       aBuilder.Perform();
294       if(!aBuilder.IsDone())
295         return aSurf;
296       Handle(GeomPlate_Surface) aPlSurf = aBuilder.Surface();
297
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
301
302       Standard_Real aDist = aBuilder.G0Error();
303       TColgp_SequenceOfXY S2d;
304       TColgp_SequenceOfXYZ S3d;
305       S2d.Clear();
306       S3d.Clear();
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();
313       if(aSurf.IsNull())
314         return aSurf;
315
316       theCurves2d = aBuilder.Curves2d();
317       theOrders    = aBuilder.Order();
318       theSenses    = aBuilder.Sense();
319     }
320
321   catch (Standard_Failure) {
322     aSurf.Nullify();
323     return aSurf;
324   }
325   return aSurf;
326 }
327
328 //=======================================================================
329 //function : addFace
330 //purpose  :
331 //=======================================================================
332
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)
338 {
339 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
340   BRepBuilderAPI_MakeFace aMakeFace (theSurf, Precision::Confusion());
341 #else
342   BRepBuilderAPI_MakeFace aMakeFace (theSurf);
343 #endif
344   TopoDS_Face aFace = aMakeFace.Face();
345   aFace.EmptyCopy();
346
347   TopoDS_Wire aWire;
348   BRep_Builder aB;
349   aB.MakeWire(aWire);
350
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.);
361
362     aB.Range (anEdge, aFace, aF, aL);
363
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)) {
369       anEdge.Reverse();
370     }
371     aB.SameParameter(anEdge,Standard_False);
372     aB.Add (aWire, anEdge);
373     TopoDS_Shape aParent;
374     if(!myEdgeFaces.Contains(anEdge))
375       continue;
376
377     if(myEdgeFaces.FindFromKey(anEdge).Extent() >1)
378       continue;
379
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);
385      }
386     }
387     else if(myEdgeComps.Contains(anEdge)) {
388       if(myEdgeComps.FindFromKey(anEdge).Extent()) {
389         aParent = myEdgeComps.FindFromKey(anEdge).First();
390         aMapParent.Add(aParent);
391       }
392     }
393
394   }
395   aB.Add(aFace,aWire);
396   Handle(ShapeFix_Face) aSff = new ShapeFix_Face(aFace);
397   aSff->SetContext(myContext);
398   aSff->SetPrecision(myTol3d);
399   aSff->Perform();
400   if(aSff->Status(ShapeExtend_FAIL)) {
401     myErrorStatus = ShHealOper_ErrorExecution;
402     return Standard_False;
403   }
404   //theFace = aSff->Face();
405   TopoDS_Shape aResShape = aSff->Result();
406   getResShape(aResShape,aMapParent,hasShell);
407   return Standard_True;
408 }
409
410 //=======================================================================
411 //function : getResShape
412 //purpose  :
413 //=======================================================================
414
415 void ShHealOper_FillHoles::getResShape(const TopoDS_Shape& theAddShape,
416                                        const TopTools_IndexedMapOfShape& aMapParent,
417                                        const Standard_Boolean theHasShell)
418 {
419   BRep_Builder aB;
420
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;
428     return ;
429   }
430   Standard_Boolean anhasShell = theHasShell;
431   TopoDS_Shell aTmpShell;
432   aB.MakeShell(aTmpShell);
433   TopTools_SequenceOfShape aseqShells;
434   if(anhasShell) {
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);
444       }
445     }
446     anhasShell = aseqShells.Length();
447   }
448   if(anhasShell) {
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));
456   }
457   else {
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);
466   }
467 }