Salome HOME
untabify
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_RemoverWebs.cxx
1 // Copyright (C) 2007-2012  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 // File:        GEOMAlgo_RemoverWebs.cxx
23 // Created:     Thu Mar 28 07:40:32 2013
24 // Author:      Peter KURNEV
25 //              <pkv@irinox>
26 //
27 #include <GEOMAlgo_RemoverWebs.hxx>
28
29 #include <TopoDS_Iterator.hxx>
30 #include <TopoDS_Shape.hxx>
31 #include <TopoDS_Solid.hxx>
32 #include <BRep_Builder.hxx>
33
34 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
35 #include <TopTools_IndexedMapOfShape.hxx>
36 #include <TopTools_ListIteratorOfListOfShape.hxx>
37 #include <TopTools_ListOfShape.hxx>
38
39 #include <TopExp.hxx>
40
41 #include <BRepClass3d_SolidClassifier.hxx>
42
43 #include <IntTools_Context.hxx>
44
45 #include <GEOMAlgo_ShapeAlgo.hxx>
46 #include <GEOMAlgo_BuilderSolid.hxx>
47 #include <GEOMAlgo_Tools3D.hxx>
48
49
50 //=======================================================================
51 //function : 
52 //purpose  :
53 //=======================================================================
54 GEOMAlgo_RemoverWebs::GEOMAlgo_RemoverWebs()
55 :
56   GEOMAlgo_ShapeAlgo()
57 {
58 }
59 //=======================================================================
60 //function : ~
61 //purpose  :
62 //=======================================================================
63 GEOMAlgo_RemoverWebs::~GEOMAlgo_RemoverWebs()
64 {
65 }
66 //=======================================================================
67 //function : CheckData
68 //purpose  :
69 //=======================================================================
70 void GEOMAlgo_RemoverWebs::CheckData()
71 {
72   TopoDS_Iterator aIt;
73   //
74   myErrorStatus=0;
75   //
76   if (myShape.IsNull()) {
77     myErrorStatus=10;
78     return;
79   }
80   //
81   aIt.Initialize(myShape);
82   for (; aIt.More(); aIt.Next()) {
83     const TopoDS_Shape& aS=aIt.Value();
84     if (aS.ShapeType()!=TopAbs_SOLID) {
85       myErrorStatus=11;
86       return;
87     }
88   }
89 }
90 //=======================================================================
91 //function : Perform
92 //purpose  :
93 //=======================================================================
94 void GEOMAlgo_RemoverWebs::Perform()
95 {
96   myErrorStatus=0;
97   myWarningStatus=0;
98   //
99   // 1.
100   CheckData();
101   if(myErrorStatus) {
102     return;
103   }
104   //
105   // 2. Init myContext
106   if (!myContext.IsNull()) {
107     myContext.Nullify();
108   }
109   myContext=new IntTools_Context;
110   //
111   BuildSolid();
112   //
113 }
114
115 //=======================================================================
116 //function : BuildSolid
117 //purpose  :
118 //=======================================================================
119 void GEOMAlgo_RemoverWebs::BuildSolid()
120 {
121   Standard_Integer i, aNbF, aNbSx, iErr, aNbSI, aNbF2;  
122   TopAbs_Orientation aOr;
123   TopoDS_Iterator aIt1, aIt2;
124   BRep_Builder aBB;
125   TopTools_IndexedMapOfShape aMSI;
126   TopTools_IndexedDataMapOfShapeListOfShape aMFS;
127   TopTools_ListOfShape aSFS;
128   TopTools_ListIteratorOfListOfShape aItLS;
129   GEOMAlgo_BuilderSolid aSB;
130   //
131   aNbF2=0;
132   //
133   // 1. aSFS: Faces 
134   TopExp::MapShapesAndAncestors(myShape, TopAbs_FACE, TopAbs_SOLID, aMFS);
135   //
136   aNbF=aMFS.Extent();
137   for (i=1; i<=aNbF; ++i) {
138     const TopoDS_Shape& aFx=aMFS.FindKey(i);
139     aOr=aFx.Orientation();
140     if (aOr==TopAbs_INTERNAL) {
141       TopoDS_Shape aFi;
142       //
143       aFi=aFx;
144       aFi.Orientation(TopAbs_FORWARD);
145       aSFS.Append(aFi);
146       aFi.Orientation(TopAbs_REVERSED);
147       aSFS.Append(aFi);
148     }
149     else {
150       const TopTools_ListOfShape& aLSx=aMFS(i);
151       aNbSx=aLSx.Extent();
152       if (aNbSx==1) {
153         aSFS.Append(aFx);
154       }
155       else if (aNbSx==2) {
156         ++aNbF2;
157       }
158     }
159   }
160   //
161   if (!aNbF2) { // nothing to do here
162     myResult=myShape;
163     return;
164   }
165   //
166   // 2 Internal shapes: edges, vertices
167   aIt1.Initialize(myShape);
168   for (; aIt1.More(); aIt1.Next()) {
169     const TopoDS_Shape& aSD=aIt1.Value(); 
170     //
171     aIt2.Initialize(aSD);
172     for (; aIt2.More(); aIt2.Next()) {
173       const TopoDS_Shape& aSi=aIt2.Value(); 
174       if (aSi.ShapeType()!=TopAbs_SHELL) {
175         aOr=aSi.Orientation();
176         if (aOr==TopAbs_INTERNAL) {
177           aMSI.Add(aSi);
178         }
179       }
180     }
181   }
182   aNbSI=aMSI.Extent();
183   //
184   // 3 Solids without internals
185   GEOMAlgo_Tools3D::MakeContainer(TopAbs_COMPOUND, myResult);  
186   //
187   aSB.SetContext(myContext);
188   aSB.SetShapes(aSFS);
189   aSB.Perform();
190   iErr=aSB.ErrorStatus();
191   if (iErr) {
192     myErrorStatus=20; // SolidBuilder failed
193     return;
194   }
195   //
196   const TopTools_ListOfShape& aLSR=aSB.Areas();
197   // 
198   // 4 Add the internals
199   if (aNbSI) {
200     AddInternalShapes(aLSR, aMSI);
201   }
202   //
203   aItLS.Initialize(aLSR);
204   for (; aItLS.More(); aItLS.Next()) {
205     const TopoDS_Shape& aSR=aItLS.Value();
206     aBB.Add(myResult, aSR);
207   }
208 }
209
210 //=======================================================================
211 //function : AddInternalShapes
212 //purpose  : 
213 //=======================================================================
214 void GEOMAlgo_RemoverWebs::AddInternalShapes(const TopTools_ListOfShape& aLSR,
215                                              const TopTools_IndexedMapOfShape& aMSI)
216 {
217   Standard_Integer i, aNbSI;
218   TopAbs_State aState;  
219   TopoDS_Solid aSd;
220   BRep_Builder aBB;
221   TopTools_ListIteratorOfListOfShape aItLS;
222   Handle(IntTools_Context) aCtx;
223   //
224   aCtx=new IntTools_Context;
225   //
226   aNbSI=aMSI.Extent();
227   for (i=1; i<=aNbSI; ++i) {
228     const TopoDS_Shape& aSI=aMSI(i);
229     //
230     aItLS.Initialize(aLSR);
231     for (; aItLS.More(); aItLS.Next()) {
232       aSd=*((TopoDS_Solid*)&aItLS.Value());
233       //
234       aState=GEOMAlgo_Tools3D::ComputeStateByOnePoint(aSI, aSd, 1.e-11, aCtx);
235       if (aState==TopAbs_IN) {
236         aBB.Add(aSd, aSI);
237         //
238         BRepClass3d_SolidClassifier& aSC=aCtx->SolidClassifier(aSd);
239         aSC.Load(aSd);
240       }
241     }
242   }
243 }
244 //
245 // myErrorStatus
246 // 0  - OK
247 // 10 - myShape is Null
248 // 11 - myShape contains non-solids
249 // 20 - BuilderSolid failed