7d1486516101fc4809455d07b28b19b01370c965
[occt.git] / src / ShapeFix / ShapeFix_Solid.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #include <ShapeFix_Solid.ixx>
15
16 #include <Standard_ErrorHandler.hxx>
17 #include <Standard_Failure.hxx>
18
19 #include <BRep_Builder.hxx>
20 #include <BRepClass3d_SolidClassifier.hxx>
21 #include <Precision.hxx>
22 #include <TopoDS_Shape.hxx>
23 #include <ShapeBuild_ReShape.hxx> 
24 #include <TopoDS_Iterator.hxx>
25 #include <TopoDS.hxx>
26 #include <ShapeExtend.hxx>
27 #include <ShapeAnalysis_Edge.hxx>
28 #include <ShapeAnalysis_Curve.hxx>
29 #include <TopoDS_Wire.hxx>
30 #include <ShapeExtend_WireData.hxx>
31 #include <TopTools_MapIteratorOfMapOfShape.hxx>
32 #include <TopoDS_Iterator.hxx>
33 #include <TopTools_MapOfShape.hxx>
34 #include <TopTools_DataMapOfShapeListOfShape.hxx>
35 #include <TopTools_SequenceOfShape.hxx>
36 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
37 #include <TopExp.hxx>
38 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
39 #include <gp_Pnt.hxx>
40 #include <Bnd_Box2d.hxx>
41 #include <ShapeAnalysis.hxx>
42 #include <TopoDS_Edge.hxx>
43 #include <BRep_Tool.hxx>
44 #include <Geom_Surface.hxx>
45 #include <TopTools_ListOfShape.hxx>
46 #include <Precision.hxx>
47 #include <TopAbs.hxx>
48 #include <TopTools_ListIteratorOfListOfShape.hxx>
49 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
50 #include <TopTools_ListIteratorOfListOfShape.hxx>
51 #include <TopoDS_Solid.hxx>
52 #include <BRep_Builder.hxx>
53 #include <TopoDS_CompSolid.hxx>
54 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
55 #include <TopAbs.hxx>
56 #include <BRep_Tool.hxx>
57 #include <TopTools_IndexedMapOfShape.hxx>
58 #include <TopTools_IndexedDataMapOfShapeShape.hxx>
59 #include <Message_Msg.hxx>
60 #include <Message_ProgressSentry.hxx>
61 #include <TopoDS_Vertex.hxx>
62 #include <TopTools_IndexedMapOfShape.hxx>
63
64 #include <TopTools_DataMapOfShapeInteger.hxx>
65 #include <Geom_Curve.hxx>
66 #include <ShapeAnalysis_FreeBounds.hxx>
67
68 //======================================================
69 //function : ShapeFix_Solid
70 //purpose  : 
71 //=======================================================================
72
73 ShapeFix_Solid::ShapeFix_Solid()
74 {
75   myStatus = ShapeExtend::EncodeStatus (ShapeExtend_OK);
76   myFixShellMode = -1;
77   myFixShell = new ShapeFix_Shell;
78   myCreateOpenSolidMode = Standard_False;
79 }
80
81 //=======================================================================
82 //function : ShapeFix_Solid
83 //purpose  : 
84 //=======================================================================
85
86 ShapeFix_Solid::ShapeFix_Solid(const TopoDS_Solid& solid)
87 {
88   myStatus = ShapeExtend::EncodeStatus (ShapeExtend_OK);
89   myFixShellMode = -1;
90   myFixShell = new ShapeFix_Shell;
91   myCreateOpenSolidMode = Standard_False;
92   Init(solid);
93 }
94
95 //=======================================================================
96 //function : Init
97 //purpose  : 
98 //=======================================================================
99
100  void ShapeFix_Solid::Init(const TopoDS_Solid& solid) 
101 {
102   mySolid = solid;
103   //mySolid = TopoDS::Solid(shape.EmptyCopied());
104   //BRep_Builder B;
105  // for( TopoDS_Iterator iter(solid); iter.More(); iter.Next()) 
106  //  B.Add(mySolid,TopoDS::Shell(iter.Value()));
107   myShape = solid;
108 }
109 #ifdef OCCT_DEBUG_GET_MIDDLE_POINT
110 //=======================================================================
111 //function : GetMiddlePoint
112 //purpose  : 
113 //=======================================================================
114 static void GetMiddlePoint(const TopoDS_Shape& aShape, gp_Pnt& pmid)
115 {
116   TopExp_Explorer aExp(aShape,TopAbs_EDGE);
117   gp_XYZ center(0.0,0.0,0.0);
118   Standard_Integer numpoints =0;
119   for( ; aExp.More(); aExp.Next()) {
120     TopoDS_Edge e1 = TopoDS::Edge(aExp.Current());
121     Standard_Real f,l;
122     Handle(Geom_Curve) c3d = BRep_Tool::Curve(e1,f,l);
123     if(!c3d.IsNull()) {
124       for(Standard_Integer i =1 ; i <=5; i++) {
125         Standard_Real param = f+(l-f)/4*(i-1);
126         gp_Pnt pt;
127         numpoints++;
128         c3d->D0(param,pt);
129         center+=pt.XYZ();
130       
131       }
132     }
133   }
134   center /= numpoints;
135   pmid.SetXYZ(center);
136 }
137 #endif
138 //=======================================================================
139 //function : CollectSolids
140 //purpose  : 
141 //=======================================================================
142 static void CollectSolids(const TopTools_SequenceOfShape& aSeqShells , 
143                           TopTools_DataMapOfShapeListOfShape& aMapShellHoles,
144                           TopTools_DataMapOfShapeInteger& theMapStatus)
145 {
146   TopTools_MapOfShape aMapHoles;
147   for ( Standard_Integer i1 = 1; i1 <= aSeqShells.Length(); i1++ ) {
148     TopoDS_Shell aShell1 = TopoDS::Shell(aSeqShells.Value(i1));
149     TopTools_ListOfShape lshells;
150     aMapShellHoles.Bind(aShell1,lshells);
151   }
152   //Finds roots shells and hole shells.
153   for ( Standard_Integer i = 1; i <= aSeqShells.Length(); i++ ) {
154     TopoDS_Shell aShell1 =  TopoDS::Shell(aSeqShells.Value(i));
155     TopExp_Explorer aExpEdges(aShell1,TopAbs_EDGE);
156     if(!BRep_Tool::IsClosed(aShell1) || !aExpEdges.More()) continue;
157     TopoDS_Solid solid;
158     BRep_Builder B;
159     B.MakeSolid (solid);
160     B.Add (solid,aShell1);
161     try {
162       OCC_CATCH_SIGNALS
163      TopAbs_State infinstatus = TopAbs_UNKNOWN;
164      BRepClass3d_SolidClassifier bsc3d (solid);
165      Standard_Integer st = 0;
166      if(!theMapStatus.IsBound(aShell1)) {
167          
168          bsc3d.PerformInfinitePoint(Precision::Confusion());
169          infinstatus = bsc3d.State();
170          
171          if(infinstatus != TopAbs_UNKNOWN &&  infinstatus !=TopAbs_ON)
172            st = (infinstatus == TopAbs_IN ? 1 :2);
173          theMapStatus.Bind(aShell1,st);
174          
175        
176        }
177       else {
178         st = theMapStatus.Find(aShell1);
179         if(st)
180           infinstatus =  (theMapStatus.Find(aShell1) == 1 ? TopAbs_IN : TopAbs_OUT);
181       }
182       if(!st) continue;
183       for ( Standard_Integer j = 1; j <= aSeqShells.Length(); j++ ) {
184         if(i==j) continue;
185         TopoDS_Shape aShell2 = aSeqShells.Value(j);
186         if(!BRep_Tool::IsClosed(aShell2)) continue;
187         if(aMapHoles.Contains(aShell2)) continue;
188         if(aMapShellHoles.IsBound(aShell2)) {
189           Standard_Boolean isAnalysis = Standard_False;
190           const TopTools_ListOfShape& ls = aMapShellHoles.Find(aShell2);
191           for(TopTools_ListIteratorOfListOfShape li(ls); li.More() && !isAnalysis; li.Next())
192             isAnalysis = li.Value().IsSame(aShell1);
193           if(isAnalysis) continue;
194         }
195         TopAbs_State pointstatus = TopAbs_UNKNOWN;
196         Standard_Integer numon =0;
197         TopTools_IndexedMapOfShape amapVert;
198         for(TopExp_Explorer aExpVert(aShell2,TopAbs_VERTEX); aExpVert.More() && amapVert.Extent() < 10; aExpVert.Next())
199           amapVert.Add(aExpVert.Current());
200         for(Standard_Integer k = 1; k <= amapVert.Extent() && 
201             (pointstatus ==TopAbs_UNKNOWN || (pointstatus ==TopAbs_ON && numon < 3)); k++) {
202           TopoDS_Vertex aV = TopoDS::Vertex(amapVert.FindKey(k));
203           gp_Pnt aPf =  BRep_Tool::Pnt(aV);
204           bsc3d.Perform(aPf,Precision::Confusion());
205           pointstatus =bsc3d.State();
206           if(pointstatus ==TopAbs_ON) numon++;
207         }
208         
209         if(numon == 3 && pointstatus ==TopAbs_ON) {
210 #ifdef OCCT_DEBUG_GET_MIDDLE_POINT
211           gp_Pnt pmid;
212           GetMiddlePoint(aShell2,pmid);
213           bsc3d.Perform(pmid,Precision::Confusion());
214 #endif
215           pointstatus = /*(bsc3d.State() == TopAbs_IN ? TopAbs_IN :*/TopAbs_OUT;
216         }
217         if(pointstatus != infinstatus) {
218           aMapShellHoles.ChangeFind(aShell1).Append(aShell2);
219           if( aMapHoles.Contains(aShell2))
220             aMapHoles.Remove(aShell2);
221           else aMapHoles.Add(aShell2);
222         }
223       }
224     }
225     catch(Standard_Failure) {
226 #ifdef OCCT_DEBUG
227       cout << "Warning: ShapeFix_Solid::SolidFromShell: Exception: ";
228       Standard_Failure::Caught()->Print(cout); cout << endl;
229 #endif
230       continue;
231     }
232   }
233   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItShellHoles( aMapShellHoles);
234   for(; aItShellHoles.More();aItShellHoles.Next()) {
235     if(aMapHoles.Contains(aItShellHoles.Key())) continue;
236     const TopTools_ListOfShape& lHoles =aItShellHoles.Value();
237     if(lHoles.IsEmpty()) continue;
238     for(TopTools_ListIteratorOfListOfShape lItHoles(lHoles);lItHoles.More(); lItHoles.Next()) {
239       if(aMapHoles.Contains(lItHoles.Value())) {
240         const TopTools_ListOfShape& lUnHoles = aMapShellHoles.Find(lItHoles.Value());
241         for(TopTools_ListIteratorOfListOfShape lItUnHoles(lUnHoles);lItUnHoles.More(); lItUnHoles.Next()) 
242           aMapHoles.Remove(lItUnHoles.Value());
243       }
244     }
245   }
246   for(TopTools_MapIteratorOfMapOfShape aIterHoles(aMapHoles);aIterHoles.More(); aIterHoles.Next())
247     aMapShellHoles.UnBind(aIterHoles.Key());
248     
249 }
250 //=======================================================================
251 //function : CreateSolids
252 //purpose  : 
253 //=======================================================================
254
255 static Standard_Boolean CreateSolids(const TopoDS_Shape aShape,TopTools_IndexedMapOfShape& aMapSolids)
256 {
257   TopTools_SequenceOfShape aSeqShells;
258   Standard_Boolean isDone = Standard_False;
259
260   for(TopExp_Explorer aExpShell(aShape,TopAbs_SHELL); aExpShell.More(); aExpShell.Next()) {
261     aSeqShells.Append(aExpShell.Current());
262   }
263   TopTools_DataMapOfShapeListOfShape aMapShellHoles;
264   TopTools_DataMapOfShapeInteger aMapStatus;
265   CollectSolids(aSeqShells,aMapShellHoles,aMapStatus);
266   TopTools_IndexedDataMapOfShapeShape ShellSolid;
267   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItShellHoles( aMapShellHoles);
268   //Defines correct orientation of shells
269   for(; aItShellHoles.More();aItShellHoles.Next()) {
270     TopoDS_Shell aShell = TopoDS::Shell(aItShellHoles.Key());
271     TopExp_Explorer aExpEdges(aShell,TopAbs_EDGE);
272     if(!BRep_Tool::IsClosed(aShell) || !aExpEdges.More()) {
273       ShellSolid.Add(aShell,aShell);
274       isDone = Standard_True;
275       continue;
276     }
277     BRep_Builder B;
278     TopAbs_State infinstatus = TopAbs_UNKNOWN;
279     TopoDS_Solid aSolid;
280     B.MakeSolid (aSolid);
281     B.Add (aSolid,aShell);
282     if(aMapStatus.IsBound(aShell)) { 
283       Standard_Integer st = aMapStatus.Find(aShell);
284       if(st)
285         infinstatus =  (aMapStatus.Find(aShell) == 1 ? TopAbs_IN : TopAbs_OUT);
286     }
287
288     else {
289       
290     try {
291       OCC_CATCH_SIGNALS
292       BRepClass3d_SolidClassifier bsc3d (aSolid);
293       bsc3d.PerformInfinitePoint(Precision::Confusion());
294       infinstatus = bsc3d.State();
295       }
296     catch(Standard_Failure) {
297 #ifdef OCCT_DEBUG
298       cout << "Warning: ShapeFix_Solid::SolidFromShell: Exception: ";
299       Standard_Failure::Caught()->Print(cout); cout << endl;
300 #endif
301       ShellSolid.Add(aShell,aSolid);
302       continue;
303     } 
304   }
305   if (infinstatus == TopAbs_IN) {
306     isDone = Standard_True;
307     aShell.Reverse();
308     TopoDS_Solid aTmpSolid;
309     B.MakeSolid (aTmpSolid);
310     B.Add (aTmpSolid,aShell);
311     aSolid = aTmpSolid;
312   }
313     
314     const TopTools_ListOfShape& lHoles = aItShellHoles.Value();
315     for(TopTools_ListIteratorOfListOfShape lItHoles(lHoles); lItHoles.More();lItHoles.Next()) {
316       TopoDS_Shell aShellHole = TopoDS::Shell(lItHoles.Value());
317       if(aMapStatus.IsBound(aShellHole)) {
318         infinstatus = (aMapStatus.Find(aShellHole) == 1 ? TopAbs_IN : TopAbs_OUT);
319       }
320       else {
321         TopoDS_Solid solid;
322         B.MakeSolid (solid);
323         B.Add (solid,aShellHole);
324         BRepClass3d_SolidClassifier bsc3dHol (solid);
325         bsc3dHol.PerformInfinitePoint(Precision::Confusion());
326         infinstatus = bsc3dHol.State();
327       }
328       if (infinstatus == TopAbs_OUT) {
329         aShellHole.Reverse();
330         isDone = Standard_True;
331       }
332       B.Add(aSolid,aShellHole);
333     }
334     ShellSolid.Add(aShell,aSolid);
335   }
336   //Creation of compsolid from shells containing shared faces. 
337   TopTools_IndexedDataMapOfShapeListOfShape aMapFaceShells;
338   TopExp::MapShapesAndAncestors(aShape,TopAbs_FACE,TopAbs_SHELL,aMapFaceShells); 
339   for(Standard_Integer i =1; i <= aMapFaceShells.Extent(); i++) {
340     const TopTools_ListOfShape& lshells = aMapFaceShells.FindFromIndex(i);
341     if(lshells.Extent() <2) continue;
342     TopoDS_CompSolid aCompSolid;
343     BRep_Builder aB;
344     aB.MakeCompSolid(aCompSolid);
345     isDone = (aShape.ShapeType() != TopAbs_COMPSOLID || isDone);
346     for(TopTools_ListIteratorOfListOfShape lItSh(lshells);lItSh.More(); lItSh.Next()) {
347       if(ShellSolid.Contains(lItSh.Value())) {
348         for(TopExp_Explorer aExpSol(ShellSolid.FindFromKey(lItSh.Value()),TopAbs_SOLID);aExpSol.More(); aExpSol.Next())
349           aB.Add(aCompSolid,aExpSol.Current());
350         ShellSolid.ChangeFromKey(lItSh.Value()) = aCompSolid;
351       }
352     }
353   }
354   for(Standard_Integer kk =1 ; kk <= ShellSolid.Extent();kk++)
355     if(!aMapSolids.Contains(ShellSolid.FindFromIndex(kk)))
356        aMapSolids.Add(ShellSolid.FindFromIndex(kk));
357   isDone = (aMapSolids.Extent() >1 || isDone);
358   return isDone;
359 }
360 //=======================================================================
361 //function : Perform
362 //purpose  : 
363 //=======================================================================
364
365 Standard_Boolean ShapeFix_Solid::Perform(const Handle(Message_ProgressIndicator)& theProgress) 
366 {
367
368   Standard_Boolean status = Standard_False;
369   if ( Context().IsNull() )
370     SetContext ( new ShapeBuild_ReShape );
371   myFixShell->SetContext(Context());
372
373   Standard_Integer NbShells = 0;
374   TopoDS_Shape S = Context()->Apply ( myShape );
375
376   // Calculate number of underlying shells
377   Standard_Integer aNbShells = 0;
378   for ( TopExp_Explorer aExpSh(S, TopAbs_SHELL); aExpSh.More(); aExpSh.Next() )
379     aNbShells++;
380
381   // Start progress scope (no need to check if progress exists -- it is safe)
382   Message_ProgressSentry aPSentry(theProgress, "Fixing solid stage", 0, 2, 1);
383
384   if ( NeedFix(myFixShellMode) )
385   {
386     // Start progress scope (no need to check if progress exists -- it is safe)
387     Message_ProgressSentry aPSentry(theProgress, "Fixing shell", 0, aNbShells, 1);
388
389     // Fix shell by shell using ShapeFix_Shell tool
390     for ( TopExp_Explorer aExpSh(S, TopAbs_SHELL); aExpSh.More() && aPSentry.More(); aExpSh.Next(), aPSentry.Next() )
391     { 
392       TopoDS_Shape sh = aExpSh.Current();
393
394       myFixShell->Init( TopoDS::Shell(sh) );
395       if ( myFixShell->Perform(theProgress) )
396       {
397         status = Standard_True;
398         myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
399       }
400       NbShells+= myFixShell->NbShells();
401     }
402
403     // Halt algorithm in case of user's abort
404     if ( !aPSentry.More() )
405       return Standard_False;
406   }
407   else 
408   {
409     NbShells = aNbShells;
410   }
411
412   // Switch to the second stage
413   aPSentry.Next();
414     
415   if(NbShells ==1) {
416     TopoDS_Shape tmpShape = Context()->Apply(myShape);
417     TopExp_Explorer aExp(tmpShape,TopAbs_SHELL);
418     Standard_Boolean isClosed = Standard_False;
419     if(aExp.More()) {
420       TopoDS_Shell  aShtmp = TopoDS::Shell(aExp.Current());
421       ShapeAnalysis_FreeBounds sfb(aShtmp);
422       TopoDS_Compound aC1 = sfb.GetClosedWires();
423       TopoDS_Compound aC2 = sfb.GetOpenWires();
424       Standard_Integer numedge =0;
425       TopExp_Explorer aExp1(aC1,TopAbs_EDGE); 
426       for( ; aExp1.More(); aExp1.Next())
427         numedge++;
428       for(aExp1.Init(aC2,TopAbs_EDGE) ; aExp1.More(); aExp1.Next())
429         numedge++;
430       isClosed = (!numedge);
431       aShtmp.Closed(isClosed);
432     }
433       
434     if(isClosed || myCreateOpenSolidMode) {
435       TopoDS_Iterator itersh(tmpShape);
436       TopoDS_Shell aShell;
437       if(itersh.More() && itersh.Value().ShapeType() == TopAbs_SHELL)
438         aShell = TopoDS::Shell(itersh.Value());
439       if(!aShell.IsNull()) {
440         TopoDS_Solid aSol = SolidFromShell(aShell);
441         if(ShapeExtend::DecodeStatus(myStatus,ShapeExtend_DONE2)) {
442           SendWarning (Message_Msg ("FixAdvSolid.FixOrientation.MSG20"));// Orientaion of shell was corrected.
443           Context()->Replace(tmpShape,aSol);
444           tmpShape = aSol;
445         }
446       }
447       mySolid  = TopoDS::Solid(tmpShape);
448     }
449     else {
450       TopoDS_Iterator aIt(tmpShape,Standard_False);
451       Context()->Replace(tmpShape,aIt.Value());
452       SendFail (Message_Msg ("FixAdvSolid.FixShell.MSG10")); // Solid can not be created from open shell. 
453     }
454   }
455   else {
456     TopoDS_Shape aResShape = Context()->Apply(myShape);
457     TopTools_SequenceOfShape aSeqShells;
458     TopTools_IndexedMapOfShape aMapSolids;
459     if(CreateSolids(aResShape,aMapSolids)) {
460       SendWarning (Message_Msg ("FixAdvSolid.FixOrientation.MSG20"));// Orientaion of shell was corrected.. 
461       if(aMapSolids.Extent() ==1) {
462         TopoDS_Shape aResSol = aMapSolids.FindKey(1);
463         if(aResShape.ShapeType() == TopAbs_SHELL && myCreateOpenSolidMode) {
464           TopoDS_Solid solid;
465           BRep_Builder B;
466           B.MakeSolid (solid);
467           B.Add (solid,aResSol);
468           mySolid = solid;
469         }
470         else {
471           mySolid =aResSol;
472           if(aResSol.ShapeType() == TopAbs_SHELL)
473             SendFail (Message_Msg ("FixAdvSolid.FixShell.MSG10")); // Solid can not be created from open shell. 
474         }
475         Context()->Replace(aResShape,mySolid);
476       }
477       else if(aMapSolids.Extent() >1) {
478         SendWarning (Message_Msg ("FixAdvSolid.FixOrientation.MSG30"));// Bad connected solid a few solids were created.
479         BRep_Builder aB;
480         TopoDS_Compound aComp;
481         aB.MakeCompound(aComp);
482         Message_ProgressSentry aPSentry(theProgress, "Creating solid",
483                                         0, aMapSolids.Extent(), 1);
484         for(Standard_Integer i =1; (i <= aMapSolids.Extent()) && (aPSentry.More()); 
485             i++, aPSentry.Next()) 
486         {
487           TopoDS_Shape aResSh =aMapSolids.FindKey(i);
488           if(aResShape.ShapeType() == TopAbs_SHELL && myCreateOpenSolidMode) {
489             aResSh.Closed(Standard_False);
490             TopoDS_Solid solid;
491             BRep_Builder B;
492             B.MakeSolid (solid);
493             B.Add (solid,aResSh);
494             aResSh = solid;
495           }
496           else if (aResShape.ShapeType() == TopAbs_SHELL)
497             SendFail(Message_Msg ("FixAdvSolid.FixShell.MSG10")); // Solid can not be created from open shell. 
498           aB.Add(aComp,aResSh);
499           
500         }
501         if ( !aPSentry.More() )
502           return Standard_False; // aborted execution
503         Context()->Replace(aResShape,aComp);
504       }
505     }
506   }
507   myShape = Context()->Apply(myShape);
508   return status;
509 }
510 //=======================================================================
511 //function : Shape
512 //purpose  : 
513 //=======================================================================
514
515 TopoDS_Shape ShapeFix_Solid::Shape() 
516 {
517  return myShape; 
518 }
519 //=======================================================================
520 //function : SolidFromShell
521 //purpose  : 
522 //=======================================================================
523
524 TopoDS_Solid ShapeFix_Solid::SolidFromShell (const TopoDS_Shell& shell) 
525 {
526   TopoDS_Shell sh = shell;
527   if (!sh.Free ()) sh.Free(Standard_True);
528
529   TopoDS_Solid solid;
530   BRep_Builder B;
531   B.MakeSolid (solid);
532   B.Add (solid,sh);
533 //   Pas encore fini : il faut une bonne orientation
534   try {
535     OCC_CATCH_SIGNALS
536     BRepClass3d_SolidClassifier bsc3d (solid);
537     Standard_Real t = Precision::Confusion();    // tolerance moyenne
538     bsc3d.PerformInfinitePoint(t);
539     
540     if (bsc3d.State() == TopAbs_IN) {
541       //         Ensuite, inverser C-A-D REPRENDRE LES SHELLS
542       //         (l inversion du solide n est pas bien prise en compte)
543       sh = shell;
544       if (!sh.Free ()) sh.Free(Standard_True);
545       TopoDS_Solid soli2;
546       B.MakeSolid (soli2);    // on recommence
547       sh.Reverse();
548       B.Add (soli2,sh);
549       solid = soli2;
550       myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE2 );
551     }
552   }
553   catch(Standard_Failure) {
554 #ifdef OCCT_DEBUG
555     cout << "Warning: ShapeFix_Solid::SolidFromShell: Exception: ";
556     Standard_Failure::Caught()->Print(cout); cout << endl;
557 #endif
558     return solid;
559   }
560   return solid;
561 }
562
563 //=======================================================================
564 //function : Status
565 //purpose  : 
566 //=======================================================================
567
568  Standard_Boolean ShapeFix_Solid::Status(const ShapeExtend_Status /*status*/) const
569 {
570  return myStatus;
571 }
572
573 //=======================================================================
574 //function : Solid
575 //purpose  : 
576 //=======================================================================
577
578  TopoDS_Shape ShapeFix_Solid::Solid() const
579 {
580  return mySolid;
581 }
582
583 //=======================================================================
584 //function : SetMsgRegistrator
585 //purpose  : 
586 //=======================================================================
587
588 void ShapeFix_Solid::SetMsgRegistrator(const Handle(ShapeExtend_BasicMsgRegistrator)& msgreg)
589 {
590   ShapeFix_Root::SetMsgRegistrator ( msgreg );
591   myFixShell->SetMsgRegistrator ( msgreg );
592 }
593
594 //=======================================================================
595 //function : SetPrecision
596 //purpose  : 
597 //=======================================================================
598
599 void ShapeFix_Solid::SetPrecision (const Standard_Real preci) 
600 {
601   ShapeFix_Root::SetPrecision ( preci );
602   myFixShell->SetPrecision ( preci );
603 }
604
605 //=======================================================================
606 //function : SetMinTolerance
607 //purpose  : 
608 //=======================================================================
609
610 void ShapeFix_Solid::SetMinTolerance (const Standard_Real mintol) 
611 {
612   ShapeFix_Root::SetMinTolerance ( mintol );
613   myFixShell->SetMinTolerance ( mintol );
614 }
615
616 //=======================================================================
617 //function : SetMaxTolerance
618 //purpose  : 
619 //=======================================================================
620
621 void ShapeFix_Solid::SetMaxTolerance (const Standard_Real maxtol) 
622 {
623   ShapeFix_Root::SetMaxTolerance ( maxtol );
624   myFixShell->SetMaxTolerance ( maxtol );
625 }