Salome HOME
NPAL19658: Deletion problem. Invalid actions were called by shortcuts.
[modules/geom.git] / src / ShHealOper / ShHealOper_FillHoles.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 //
21 // File:      ShHealOper_FillHoles.cxx
22 // Created:   26.04.04 17:35:30
23 // Author:    Galina KULIKOVA
24
25
26 #include <ShHealOper_FillHoles.hxx>
27 #include <TopExp.hxx>
28 #include <TopAbs_ShapeEnum.hxx>
29 #include <ShapeAnalysis_FreeBounds.hxx>
30 #include <TopoDS_Compound.hxx>
31 #include <TopoDS_Iterator.hxx>
32 #include <Geom_Curve.hxx>
33 #include <BRep_Tool.hxx>
34 #include <TopExp_Explorer.hxx>
35 #include <TopTools_HSequenceOfShape.hxx>
36 #include <TopoDS.hxx>
37 #include <Geom_BSplineSurface.hxx>
38 #include <GeomPlate_BuildPlateSurface.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <BRepAdaptor_Curve.hxx>
41 #include <BRepAdaptor_HCurve.hxx>
42 #include <GeomPlate_CurveConstraint.hxx>
43 #include <GeomPlate_Surface.hxx>
44 #include <TColgp_SequenceOfXY.hxx>
45 #include <TColgp_SequenceOfXYZ.hxx>
46 #include <BRep_Builder.hxx>
47 #include <ShapeFix_Face.hxx>
48 #include <BRep_Tool.hxx>
49 #include <TopLoc_Location.hxx>
50 #include <TopTools_IndexedMapOfShape.hxx>
51 #include <TopoDS_Shell.hxx>
52 #include <ShapeFix_Shell.hxx>
53 #include <GeomPlate_PlateG0Criterion.hxx>
54 #include <GeomPlate_MakeApprox.hxx>
55 #include <Precision.hxx>
56 #include <TopTools_ListOfShape.hxx>
57 #include <BRepFill_CurveConstraint.hxx>
58 #include <BRepBuilderAPI_MakeFace.hxx>
59
60
61 //=======================================================================
62 //function : ShHealOper_FillHoles()
63 //purpose  : Constructor
64 //=======================================================================
65 ShHealOper_FillHoles::ShHealOper_FillHoles ()
66 {
67   InitParameters();
68 }
69
70 //=======================================================================
71 //function : ShHealOper_FillHoles
72 //purpose  : 
73 //=======================================================================
74
75 ShHealOper_FillHoles::ShHealOper_FillHoles (const TopoDS_Shape& theShape)
76 {
77   
78   Init(theShape);
79   InitParameters();
80 }
81 //=======================================================================
82 //function : Init
83 //purpose  : 
84 //=======================================================================
85
86 void ShHealOper_FillHoles::Init(const TopoDS_Shape& theShape)
87 {
88   ShHealOper_Tool::Init(theShape);
89   TopExp::MapShapesAndAncestors( myInitShape, TopAbs_EDGE,TopAbs_SHELL   , myEdgeShells);
90   TopExp::MapShapesAndAncestors ( myInitShape, TopAbs_EDGE,TopAbs_COMPOUND, myEdgeComps );
91   
92   TopExp::MapShapesAndAncestors ( myInitShape, TopAbs_EDGE,TopAbs_FACE, myEdgeFaces );
93 }
94 //=======================================================================
95 //function : InitParameters
96 //purpose  : 
97 //=======================================================================
98
99 void ShHealOper_FillHoles::InitParameters(Standard_Integer theDegree,
100                                           Standard_Integer theNbPtsOnCur,
101                                           Standard_Integer theNbIter,
102                                           Standard_Real theTol3d,
103                                           Standard_Real theTol2d,
104                                           Standard_Real theTolAng,
105                                           Standard_Real theTolCrv,
106                                           Standard_Integer theMaxDeg,
107                                           Standard_Integer theMaxSeg)
108 {
109   myNbPtsOnCur = theNbPtsOnCur;
110   myNbIter = theNbIter;
111   myDegree =theDegree;
112   myTol2d = theTol2d;
113   myTol3d = theTol3d;
114   myTolAng = theTolAng;
115   myTolCrv = theTolCrv;
116   myMaxDeg = theMaxDeg;
117   myMaxSeg =theMaxSeg;
118 }
119 //=======================================================================
120 //function : Fill
121 //purpose  : 
122 //=======================================================================
123
124 Standard_Boolean ShHealOper_FillHoles::Fill()
125 {
126   ShapeAnalysis_FreeBounds sab(myInitShape);
127   TopoDS_Compound aCompClosed = sab.GetClosedWires();
128   TopoDS_Compound aCompOpen = sab.GetOpenWires();
129   TopTools_SequenceOfShape aFillWires;
130   if(!aCompClosed.IsNull()) {
131     TopoDS_Iterator aIt(aCompClosed);
132
133     for( ; aIt.More(); aIt.Next()) 
134       aFillWires.Append(aIt.Value());
135   }
136   if(!aCompOpen.IsNull()) {
137     TopoDS_Iterator aIt(aCompOpen);
138     for(  ; aIt.More(); aIt.Next()) 
139       aFillWires.Append(aIt.Value());
140   }
141   
142   TopExp_Explorer aExp(myInitShape,TopAbs_EDGE,TopAbs_FACE);
143   
144   for( ; aExp.More(); aExp.Next())
145     aFillWires.Append(aExp.Current());
146
147   return Fill(aFillWires);
148 }
149
150 //=======================================================================
151 //function : Fill
152 //purpose  : 
153 //=======================================================================
154
155 Standard_Boolean ShHealOper_FillHoles::Fill(const TopTools_SequenceOfShape& theFillShapes)
156 {
157   myDone = Standard_False;
158   myErrorStatus = ShHealOper_NotError;
159   if(myInitShape.IsNull()) {
160     myErrorStatus = ShHealOper_InvalidParameters;
161     return myDone;
162   }
163   if(!theFillShapes.Length()) {
164     return myDone;
165   }
166
167   Handle(TopTools_HSequenceOfShape) aSeqWires = new TopTools_HSequenceOfShape;
168   if(!prepareWires(theFillShapes,aSeqWires)) {
169     myErrorStatus = ShHealOper_InvalidParameters;
170     return myDone;
171   }
172
173   myResultShape = myInitShape;
174   Standard_Integer i =1;
175   for( ; i <= aSeqWires->Length(); i++) {
176     TopoDS_Wire aWire = TopoDS::Wire(aSeqWires->Value(i));
177     Handle(TColGeom2d_HArray1OfCurve) aCurves2d;
178     Handle(TColStd_HArray1OfInteger) aOrders;
179     Handle(TColStd_HArray1OfInteger) aSenses;
180     Handle(Geom_Surface) aSurf = buildSurface(aWire,aCurves2d,aOrders,aSenses);
181     if(aSurf.IsNull())
182       myErrorStatus = ShHealOper_ErrorExecution;
183     else 
184       myDone = (addFace(aSurf,aWire,aCurves2d,aOrders,aSenses) || myDone);
185   }
186   if(myDone)
187     myResultShape = myContext->Apply(myResultShape);
188   return myDone;
189 }
190 //=======================================================================
191 //function : isCircle
192 //purpose  : 
193 //=======================================================================
194 static Standard_Boolean isCircle(const TopoDS_Edge theEdge)
195 {
196   Standard_Real aFirst, aLast;
197   Handle(Geom_Curve) aC3D = BRep_Tool::Curve(theEdge,aFirst, aLast );
198   if(aC3D.IsNull()) return Standard_False;
199   Standard_Boolean isCirc = (aC3D->Value(aFirst).Distance(aC3D->Value(aLast)) < 
200      aC3D->Value(aFirst).Distance(aC3D->Value((aFirst +aLast)/2)));
201   return isCirc;
202 }
203 //=======================================================================
204 //function : prepareWires
205 //purpose  : 
206 //=======================================================================
207
208 Standard_Boolean ShHealOper_FillHoles::prepareWires(const TopTools_SequenceOfShape& theFillShapes,
209                                                     Handle(TopTools_HSequenceOfShape)& theSeqWires)
210 {
211   Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
212   BRep_Builder aB;
213   Standard_Integer i =1;
214   for( ; i <= theFillShapes.Length(); i++) {
215     TopExp_Explorer aExp;
216     for (aExp.Init (theFillShapes.Value(i),TopAbs_WIRE); aExp.More(); aExp.Next()) {
217       TopoDS_Iterator aIt(aExp.Current());
218       Standard_Boolean isAdd = Standard_True;
219       for( ; aIt.More() && isAdd; aIt.Next()) {
220         if(myEdgeFaces.Contains(aIt.Value()))
221           isAdd =  (myEdgeFaces.FindFromKey(aIt.Value()).Extent() <2);
222       }
223       if(isAdd)
224         theSeqWires->Append(aExp.Current());
225     }
226     for (aExp.Init (theFillShapes.Value(i),TopAbs_EDGE, TopAbs_WIRE); aExp.More(); aExp.Next()) {
227       if (!BRep_Tool::Degenerated (TopoDS::Edge (aExp.Current())))
228         if(myEdgeFaces.Contains(aExp.Current()) && myEdgeFaces.FindFromKey(aExp.Current()).Extent() >1)
229           continue;
230         aSeqEdges->Append(aExp.Current());
231     }
232   }
233   
234   if(aSeqEdges->Length())
235   {
236     Standard_Real aTol = 0.;
237     Standard_Boolean aShared = Standard_True;
238     Handle(TopTools_HSequenceOfShape) aTmpWires = new TopTools_HSequenceOfShape;
239     ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdges, aTol, aShared, aTmpWires);
240     Handle(TopTools_HSequenceOfShape) anWiresClosed = new TopTools_HSequenceOfShape,
241     anWiresOpen   = new TopTools_HSequenceOfShape;
242     ShapeAnalysis_FreeBounds::SplitWires(aTmpWires, aTol, aShared, anWiresClosed, anWiresOpen);
243     
244     for (i = 1; i <= anWiresClosed->Length(); i++)
245       theSeqWires->Append (anWiresClosed->Value (i));
246     for (i = 1; i <= anWiresOpen->Length(); i++)
247       theSeqWires->Append (anWiresOpen->Value (i));
248   }
249   
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     
280       GeomPlate_BuildPlateSurface aBuilder(myDegree, myNbPtsOnCur, myNbIter,
281                                                  myTol2d, myTol3d, myTolAng, myTolCrv);
282       TopoDS_Iterator aIter;
283       for(aIter.Initialize (theWire); aIter.More(); aIter.Next()) {
284
285         TopoDS_Edge ae = TopoDS::Edge(aIter.Value());
286         BRepAdaptor_Curve adC(ae);
287         Handle(BRepAdaptor_HCurve) aHAD= new BRepAdaptor_HCurve(adC);
288         Handle(BRepFill_CurveConstraint) aConst =
289             new BRepFill_CurveConstraint (aHAD, (Standard_Integer) GeomAbs_C0, myNbPtsOnCur, myTol3d);
290         //Handle(GeomPlate_CurveConstraint) aConst = 
291          // new GeomPlate_CurveConstraint(aHAD, (Standard_Integer) GeomAbs_C0, myNbPtsOnCur, myTol3d);
292         aBuilder.Add (aConst);
293       }
294       aBuilder.Perform();
295       if(!aBuilder.IsDone())
296         return aSurf;
297       Handle(GeomPlate_Surface) aPlSurf = aBuilder.Surface();
298
299       //for filling holes without initial specified surface
300       //the initial surface should be build by GeomPlate itself
301       //following code was taken from BRepFill_Filling::Build
302
303       Standard_Real aDist = aBuilder.G0Error();
304       TColgp_SequenceOfXY S2d;
305       TColgp_SequenceOfXYZ S3d;
306       S2d.Clear();
307       S3d.Clear();
308       aBuilder.Disc2dContour(4,S2d);
309       aBuilder.Disc3dContour(4,0,S3d);
310       Standard_Real amaxTol = Max( myTol3d, 10* aDist); 
311       GeomPlate_PlateG0Criterion Criterion( S2d, S3d, amaxTol );
312       GeomPlate_MakeApprox Approx( aPlSurf, Criterion, myTol3d, myMaxSeg, myMaxDeg );
313       aSurf = Approx.Surface();
314       if(aSurf.IsNull())
315         return aSurf;
316       
317       theCurves2d = aBuilder.Curves2d();
318       theOrders    = aBuilder.Order();
319       theSenses    = aBuilder.Sense();
320       
321     }
322
323   catch (Standard_Failure) {
324     aSurf.Nullify();
325     return aSurf;
326   }
327   return aSurf;
328 }
329
330 //=======================================================================
331 //function : addFace
332 //purpose  : 
333 //=======================================================================
334
335 Standard_Boolean ShHealOper_FillHoles::addFace(const Handle(Geom_Surface)& theSurf,
336                                                const TopoDS_Wire& theWire,
337                                                const Handle(TColGeom2d_HArray1OfCurve)& theCurves2d,
338                                                const Handle(TColStd_HArray1OfInteger)& theOrders,
339                                                const Handle(TColStd_HArray1OfInteger)& theSenses)
340 {
341   BRepBuilderAPI_MakeFace aMakeFace (theSurf);
342   TopoDS_Face aFace = aMakeFace.Face();
343   aFace.EmptyCopy();
344   
345
346   TopoDS_Wire aWire;
347   BRep_Builder aB;
348   aB.MakeWire(aWire);
349
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.);
360     
361     aB.Range (anEdge, aFace, aF, aL);
362     
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)) {
368       anEdge.Reverse();
369     }
370     aB.SameParameter(anEdge,Standard_False);
371     aB.Add (aWire, anEdge);
372     Standard_Boolean isAdd = Standard_False;
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 }