]> SALOME platform Git repositories - modules/geom.git/blob - src/ShHealOper/ShHealOper_FillHoles.cxx
Salome HOME
rnc : Set icons to constructor radio buttons (instead of text) in the sketcher dialog...
[modules/geom.git] / src / ShHealOper / ShHealOper_FillHoles.cxx
1 // Copyright (C) 2007-2011  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.
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 <ShHealOper_FillHoles.hxx>
28 #include <TopExp.hxx>
29 #include <TopAbs_ShapeEnum.hxx>
30 #include <ShapeAnalysis_FreeBounds.hxx>
31 #include <TopoDS_Compound.hxx>
32 #include <TopoDS_Iterator.hxx>
33 #include <Geom_Curve.hxx>
34 #include <BRep_Tool.hxx>
35 #include <TopExp_Explorer.hxx>
36 #include <TopTools_HSequenceOfShape.hxx>
37 #include <TopoDS.hxx>
38 #include <Geom_BSplineSurface.hxx>
39 #include <GeomPlate_BuildPlateSurface.hxx>
40 #include <TopoDS_Edge.hxx>
41 #include <BRepAdaptor_Curve.hxx>
42 #include <BRepAdaptor_HCurve.hxx>
43 #include <GeomPlate_CurveConstraint.hxx>
44 #include <GeomPlate_Surface.hxx>
45 #include <TColgp_SequenceOfXY.hxx>
46 #include <TColgp_SequenceOfXYZ.hxx>
47 #include <BRep_Builder.hxx>
48 #include <ShapeFix_Face.hxx>
49 #include <BRep_Tool.hxx>
50 #include <TopLoc_Location.hxx>
51 #include <TopTools_IndexedMapOfShape.hxx>
52 #include <TopoDS_Shell.hxx>
53 #include <ShapeFix_Shell.hxx>
54 #include <GeomPlate_PlateG0Criterion.hxx>
55 #include <GeomPlate_MakeApprox.hxx>
56 #include <Precision.hxx>
57 #include <TopTools_ListOfShape.hxx>
58 #include <BRepFill_CurveConstraint.hxx>
59 #include <BRepBuilderAPI_MakeFace.hxx>
60
61
62 //=======================================================================
63 //function : ShHealOper_FillHoles()
64 //purpose  : Constructor
65 //=======================================================================
66 ShHealOper_FillHoles::ShHealOper_FillHoles ()
67 {
68   InitParameters();
69 }
70
71 //=======================================================================
72 //function : ShHealOper_FillHoles
73 //purpose  : 
74 //=======================================================================
75
76 ShHealOper_FillHoles::ShHealOper_FillHoles (const TopoDS_Shape& theShape)
77 {
78   
79   Init(theShape);
80   InitParameters();
81 }
82 //=======================================================================
83 //function : Init
84 //purpose  : 
85 //=======================================================================
86
87 void ShHealOper_FillHoles::Init(const TopoDS_Shape& theShape)
88 {
89   ShHealOper_Tool::Init(theShape);
90   TopExp::MapShapesAndAncestors( myInitShape, TopAbs_EDGE,TopAbs_SHELL   , myEdgeShells);
91   TopExp::MapShapesAndAncestors ( myInitShape, TopAbs_EDGE,TopAbs_COMPOUND, myEdgeComps );
92   
93   TopExp::MapShapesAndAncestors ( myInitShape, TopAbs_EDGE,TopAbs_FACE, myEdgeFaces );
94 }
95 //=======================================================================
96 //function : InitParameters
97 //purpose  : 
98 //=======================================================================
99
100 void ShHealOper_FillHoles::InitParameters(Standard_Integer theDegree,
101                                           Standard_Integer theNbPtsOnCur,
102                                           Standard_Integer theNbIter,
103                                           Standard_Real theTol3d,
104                                           Standard_Real theTol2d,
105                                           Standard_Real theTolAng,
106                                           Standard_Real theTolCrv,
107                                           Standard_Integer theMaxDeg,
108                                           Standard_Integer theMaxSeg)
109 {
110   myNbPtsOnCur = theNbPtsOnCur;
111   myNbIter = theNbIter;
112   myDegree =theDegree;
113   myTol2d = theTol2d;
114   myTol3d = theTol3d;
115   myTolAng = theTolAng;
116   myTolCrv = theTolCrv;
117   myMaxDeg = theMaxDeg;
118   myMaxSeg =theMaxSeg;
119 }
120 //=======================================================================
121 //function : Fill
122 //purpose  : 
123 //=======================================================================
124
125 Standard_Boolean ShHealOper_FillHoles::Fill()
126 {
127   ShapeAnalysis_FreeBounds sab(myInitShape);
128   TopoDS_Compound aCompClosed = sab.GetClosedWires();
129   TopoDS_Compound aCompOpen = sab.GetOpenWires();
130   TopTools_SequenceOfShape aFillWires;
131   if(!aCompClosed.IsNull()) {
132     TopoDS_Iterator aIt(aCompClosed);
133
134     for( ; aIt.More(); aIt.Next()) 
135       aFillWires.Append(aIt.Value());
136   }
137   if(!aCompOpen.IsNull()) {
138     TopoDS_Iterator aIt(aCompOpen);
139     for(  ; aIt.More(); aIt.Next()) 
140       aFillWires.Append(aIt.Value());
141   }
142   
143   TopExp_Explorer aExp(myInitShape,TopAbs_EDGE,TopAbs_FACE);
144   
145   for( ; aExp.More(); aExp.Next())
146     aFillWires.Append(aExp.Current());
147
148   return Fill(aFillWires);
149 }
150
151 //=======================================================================
152 //function : Fill
153 //purpose  : 
154 //=======================================================================
155
156 Standard_Boolean ShHealOper_FillHoles::Fill(const TopTools_SequenceOfShape& theFillShapes)
157 {
158   myDone = Standard_False;
159   myErrorStatus = ShHealOper_NotError;
160   if(myInitShape.IsNull()) {
161     myErrorStatus = ShHealOper_InvalidParameters;
162     return myDone;
163   }
164   if(!theFillShapes.Length()) {
165     return myDone;
166   }
167
168   Handle(TopTools_HSequenceOfShape) aSeqWires = new TopTools_HSequenceOfShape;
169   if(!prepareWires(theFillShapes,aSeqWires)) {
170     myErrorStatus = ShHealOper_InvalidParameters;
171     return myDone;
172   }
173
174   myResultShape = myInitShape;
175   Standard_Integer i =1;
176   for( ; i <= aSeqWires->Length(); i++) {
177     TopoDS_Wire aWire = TopoDS::Wire(aSeqWires->Value(i));
178     Handle(TColGeom2d_HArray1OfCurve) aCurves2d;
179     Handle(TColStd_HArray1OfInteger) aOrders;
180     Handle(TColStd_HArray1OfInteger) aSenses;
181     Handle(Geom_Surface) aSurf = buildSurface(aWire,aCurves2d,aOrders,aSenses);
182     if(aSurf.IsNull())
183       myErrorStatus = ShHealOper_ErrorExecution;
184     else 
185       myDone = (addFace(aSurf,aWire,aCurves2d,aOrders,aSenses) || myDone);
186   }
187   if(myDone)
188     myResultShape = myContext->Apply(myResultShape);
189   return myDone;
190 }
191 //=======================================================================
192 //function : isCircle
193 //purpose  : 
194 //=======================================================================
195 static Standard_Boolean isCircle(const TopoDS_Edge theEdge)
196 {
197   Standard_Real aFirst, aLast;
198   Handle(Geom_Curve) aC3D = BRep_Tool::Curve(theEdge,aFirst, aLast );
199   if(aC3D.IsNull()) return Standard_False;
200   Standard_Boolean isCirc = (aC3D->Value(aFirst).Distance(aC3D->Value(aLast)) < 
201      aC3D->Value(aFirst).Distance(aC3D->Value((aFirst +aLast)/2)));
202   return isCirc;
203 }
204 //=======================================================================
205 //function : prepareWires
206 //purpose  : 
207 //=======================================================================
208
209 Standard_Boolean ShHealOper_FillHoles::prepareWires(const TopTools_SequenceOfShape& theFillShapes,
210                                                     Handle(TopTools_HSequenceOfShape)& theSeqWires)
211 {
212   Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
213   BRep_Builder aB;
214   Standard_Integer i =1;
215   for( ; i <= theFillShapes.Length(); i++) {
216     TopExp_Explorer aExp;
217     for (aExp.Init (theFillShapes.Value(i),TopAbs_WIRE); aExp.More(); aExp.Next()) {
218       TopoDS_Iterator aIt(aExp.Current());
219       Standard_Boolean isAdd = Standard_True;
220       for( ; aIt.More() && isAdd; aIt.Next()) {
221         if(myEdgeFaces.Contains(aIt.Value()))
222           isAdd =  (myEdgeFaces.FindFromKey(aIt.Value()).Extent() <2);
223       }
224       if(isAdd)
225         theSeqWires->Append(aExp.Current());
226     }
227     for (aExp.Init (theFillShapes.Value(i),TopAbs_EDGE, TopAbs_WIRE); aExp.More(); aExp.Next()) {
228       if (!BRep_Tool::Degenerated (TopoDS::Edge (aExp.Current())))
229         if(myEdgeFaces.Contains(aExp.Current()) && myEdgeFaces.FindFromKey(aExp.Current()).Extent() >1)
230           continue;
231         aSeqEdges->Append(aExp.Current());
232     }
233   }
234   
235   if(aSeqEdges->Length())
236   {
237     Standard_Real aTol = 0.;
238     Standard_Boolean aShared = Standard_True;
239     Handle(TopTools_HSequenceOfShape) aTmpWires = new TopTools_HSequenceOfShape;
240     ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdges, aTol, aShared, aTmpWires);
241     Handle(TopTools_HSequenceOfShape) anWiresClosed = new TopTools_HSequenceOfShape,
242     anWiresOpen   = new TopTools_HSequenceOfShape;
243     ShapeAnalysis_FreeBounds::SplitWires(aTmpWires, aTol, aShared, anWiresClosed, anWiresOpen);
244     
245     for (i = 1; i <= anWiresClosed->Length(); i++)
246       theSeqWires->Append (anWiresClosed->Value (i));
247     for (i = 1; i <= anWiresOpen->Length(); i++)
248       theSeqWires->Append (anWiresOpen->Value (i));
249   }
250   
251  
252
253   for( i =1; i <= theSeqWires->Length(); i++) {
254     TopoDS_Wire aWire = TopoDS::Wire(theSeqWires->Value(i));
255     
256     TopoDS_Iterator aIt(aWire);
257     Standard_Integer ne =0;
258     TopoDS_Edge ae;
259     for( ; aIt.More(); aIt.Next(), ne++)
260       ae = TopoDS::Edge(aIt.Value()); 
261     if((ne == 1) && ( !isCircle(ae))) {
262       theSeqWires->Remove(i--);
263       continue;
264     }
265   }
266   return (theSeqWires->Length());
267 }
268 //=======================================================================
269 //function : buildSurface
270 //purpose  : 
271 //=======================================================================
272
273 Handle(Geom_Surface) ShHealOper_FillHoles::buildSurface(const TopoDS_Wire& theWire,
274                                                         Handle(TColGeom2d_HArray1OfCurve)& theCurves2d,
275                                                         Handle(TColStd_HArray1OfInteger)& theOrders,
276                                                         Handle(TColStd_HArray1OfInteger)& theSenses)
277 {
278   Handle(Geom_BSplineSurface) aSurf;
279   try {
280     
281       GeomPlate_BuildPlateSurface aBuilder(myDegree, myNbPtsOnCur, myNbIter,
282                                                  myTol2d, myTol3d, myTolAng, myTolCrv);
283       TopoDS_Iterator aIter;
284       for(aIter.Initialize (theWire); aIter.More(); aIter.Next()) {
285
286         TopoDS_Edge ae = TopoDS::Edge(aIter.Value());
287         BRepAdaptor_Curve adC(ae);
288         Handle(BRepAdaptor_HCurve) aHAD= new BRepAdaptor_HCurve(adC);
289         Handle(BRepFill_CurveConstraint) aConst =
290             new BRepFill_CurveConstraint (aHAD, (Standard_Integer) GeomAbs_C0, myNbPtsOnCur, myTol3d);
291         //Handle(GeomPlate_CurveConstraint) aConst = 
292          // new GeomPlate_CurveConstraint(aHAD, (Standard_Integer) GeomAbs_C0, myNbPtsOnCur, myTol3d);
293         aBuilder.Add (aConst);
294       }
295       aBuilder.Perform();
296       if(!aBuilder.IsDone())
297         return aSurf;
298       Handle(GeomPlate_Surface) aPlSurf = aBuilder.Surface();
299
300       //for filling holes without initial specified surface
301       //the initial surface should be build by GeomPlate itself
302       //following code was taken from BRepFill_Filling::Build
303
304       Standard_Real aDist = aBuilder.G0Error();
305       TColgp_SequenceOfXY S2d;
306       TColgp_SequenceOfXYZ S3d;
307       S2d.Clear();
308       S3d.Clear();
309       aBuilder.Disc2dContour(4,S2d);
310       aBuilder.Disc3dContour(4,0,S3d);
311       Standard_Real amaxTol = Max( myTol3d, 10* aDist); 
312       GeomPlate_PlateG0Criterion Criterion( S2d, S3d, amaxTol );
313       GeomPlate_MakeApprox Approx( aPlSurf, Criterion, myTol3d, myMaxSeg, myMaxDeg );
314       aSurf = Approx.Surface();
315       if(aSurf.IsNull())
316         return aSurf;
317       
318       theCurves2d = aBuilder.Curves2d();
319       theOrders    = aBuilder.Order();
320       theSenses    = aBuilder.Sense();
321       
322     }
323
324   catch (Standard_Failure) {
325     aSurf.Nullify();
326     return aSurf;
327   }
328   return aSurf;
329 }
330
331 //=======================================================================
332 //function : addFace
333 //purpose  : 
334 //=======================================================================
335
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)
341 {
342   BRepBuilderAPI_MakeFace aMakeFace (theSurf);
343   TopoDS_Face aFace = aMakeFace.Face();
344   aFace.EmptyCopy();
345   
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     
436     aB.Add(aTmpShell,theAddShape);
437     Standard_Integer i =1;
438     for( ; i <= aMapParent.Extent(); i++) {
439       TopoDS_Shape aParShape = myContext->Apply(aMapParent.FindKey(i));
440       if(aParShape.ShapeType() == TopAbs_SHELL) {
441         TopExp_Explorer aexp(aParShape,TopAbs_FACE);
442         for( ; aexp.More(); aexp.Next())
443           aB.Add(aTmpShell,aexp.Current());
444         aseqShells.Append(aParShape);
445       }
446     }
447     anhasShell = aseqShells.Length();
448   }
449   if(anhasShell) {
450     Handle(ShapeFix_Shell) asfs = new ShapeFix_Shell;
451     asfs->FixFaceOrientation(aTmpShell);
452     TopoDS_Shape anshape = asfs->Shape();
453     myContext->Replace(aseqShells.Value(1),anshape);
454     Standard_Integer i =2;
455     for( ; i<= aseqShells.Length(); i++) 
456       myContext->Remove(aseqShells.Value(i));
457     
458   }
459   else {
460     TopoDS_Compound aComp;
461     aB.MakeCompound(aComp);
462     TopoDS_Shape oldshape = myContext->Apply(aMapParent.FindKey(1));
463     TopoDS_Iterator aIt(oldshape);
464     for( ; aIt.More(); aIt.Next())
465       aB.Add(aComp,aIt.Value());
466     aB.Add(aComp,theAddShape);
467     myContext->Replace( oldshape,aComp);
468                         
469   }
470 }