]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMAlgo/GEOMAlgo_RemoverWebs.cxx
Salome HOME
0022356: [CEA 968] Regression on the test script 01_geom.py
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_RemoverWebs.cxx
1 // Copyright (C) 2007-2013  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
26 #include <GEOMAlgo_RemoverWebs.hxx>
27 #include <GEOMAlgo_ShapeAlgo.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 <TopExp.hxx>
35
36 #include <BRepClass3d_SolidClassifier.hxx>
37
38 #include <BOPInt_Context.hxx>
39
40 #include <BOPAlgo_BuilderSolid.hxx>
41
42 #include <BOPTools.hxx>
43 #include <BOPTools_AlgoTools.hxx>
44 #include <BOPCol_MapOfShape.hxx>
45
46 //=======================================================================
47 //function : 
48 //purpose  :
49 //=======================================================================
50 GEOMAlgo_RemoverWebs::GEOMAlgo_RemoverWebs()
51 :
52   GEOMAlgo_ShapeAlgo()
53 {
54 }
55 //=======================================================================
56 //function : ~
57 //purpose  :
58 //=======================================================================
59 GEOMAlgo_RemoverWebs::~GEOMAlgo_RemoverWebs()
60 {
61 }
62 //=======================================================================
63 //function : CheckData
64 //purpose  :
65 //=======================================================================
66 void GEOMAlgo_RemoverWebs::CheckData()
67 {
68   TopoDS_Iterator aIt;
69   //
70   myErrorStatus=0;
71   //
72   if (myShape.IsNull()) {
73     myErrorStatus=10;
74     return;
75   }
76   //
77   aIt.Initialize(myShape);
78   for (; aIt.More(); aIt.Next()) {
79     const TopoDS_Shape& aS=aIt.Value();
80     if (aS.ShapeType()!=TopAbs_SOLID) {
81       myErrorStatus=11;
82       return;
83     }
84   }
85 }
86 //=======================================================================
87 //function : Perform
88 //purpose  :
89 //=======================================================================
90 void GEOMAlgo_RemoverWebs::Perform()
91 {
92   myErrorStatus=0;
93   myWarningStatus=0;
94   //
95   // 1.
96   CheckData();
97   if(myErrorStatus) {
98     return;
99   }
100   //
101   // 2. Init myContext
102   if (!myContext.IsNull()) {
103     myContext.Nullify();
104   }
105   myContext=new BOPInt_Context;
106   //
107   BuildSolid();
108   //
109 }
110
111 //=======================================================================
112 //function : BuildSolid
113 //purpose  :
114 //=======================================================================
115 void GEOMAlgo_RemoverWebs::BuildSolid()
116 {
117   Standard_Integer i, aNbF, aNbSx, iErr, aNbSI, aNbF2, aNbS, aNbR;  
118   TopAbs_Orientation aOr;
119   TopoDS_Iterator aIt1, aIt2;
120   TopoDS_Shape aShape;
121   BRep_Builder aBB;
122   BOPCol_MapOfShape aMFence;
123   BOPCol_IndexedMapOfShape aMSI;
124   BOPCol_IndexedDataMapOfShapeListOfShape aMFS;
125   BOPCol_ListOfShape aSFS;
126   BOPCol_ListIteratorOfListOfShape aItLS;
127   BOPAlgo_BuilderSolid aSB;
128   //
129   //modified by NIZNHY-PKV Thu Jul 11 06:54:51 2013f
130   //
131   // 0. 
132   // The compound myShape may contain equal solids 
133   // (itz.brep for e.g.). The block is to refine 
134   // such data if it is necessary. The shape to treat 
135   // will be aShape (not myShape).
136   //
137   aShape=myShape;
138   //
139   aIt1.Initialize(myShape);
140   for (aNbS=0; aIt1.More(); aIt1.Next(), ++aNbS) {
141     const TopoDS_Shape& aS=aIt1.Value();
142     aMFence.Add(aS);
143   }
144   //
145   aNbR=aMFence.Extent();
146   if (aNbS!=aNbR) {
147     BOPCol_MapIteratorOfMapOfShape aItMS;
148     //
149     BOPTools_AlgoTools::MakeContainer(TopAbs_COMPOUND, aShape);  
150     //
151     aItMS.Initialize(aMFence);
152     for (; aItMS.More(); aItMS.Next()) {
153       const TopoDS_Shape& aS=aItMS.Key();
154       aBB.Add(aShape, aS);
155     }
156   }
157   //modified by NIZNHY-PKV Thu Jul 11 06:54:54 2013t
158   //
159   aNbF2=0;
160   //
161   // 1. aSFS: Faces 
162   BOPTools::MapShapesAndAncestors(aShape, TopAbs_FACE, TopAbs_SOLID, aMFS);
163   //
164   aNbF=aMFS.Extent();
165   for (i=1; i<=aNbF; ++i) {
166     const TopoDS_Shape& aFx=aMFS.FindKey(i);
167     aOr=aFx.Orientation();
168     if (aOr==TopAbs_INTERNAL) {
169       TopoDS_Shape aFi;
170       //
171       aFi=aFx;
172       aFi.Orientation(TopAbs_FORWARD);
173       aSFS.Append(aFi);
174       aFi.Orientation(TopAbs_REVERSED);
175       aSFS.Append(aFi);
176     }
177     else {
178       const BOPCol_ListOfShape& aLSx=aMFS(i);
179       aNbSx=aLSx.Extent();
180       if (aNbSx==1) {
181         aSFS.Append(aFx);
182       }
183       else if (aNbSx==2) {
184         ++aNbF2;
185       }
186     }
187   }
188   //
189   if (!aNbF2) { // nothing to do here
190     myResult=aShape;
191     return;
192   }
193   //
194   // 2 Internal shapes: edges, vertices
195   aIt1.Initialize(aShape);
196   for (; aIt1.More(); aIt1.Next()) {
197     const TopoDS_Shape& aSD=aIt1.Value(); 
198     //
199     aIt2.Initialize(aSD);
200     for (; aIt2.More(); aIt2.Next()) {
201       const TopoDS_Shape& aSi=aIt2.Value(); 
202       if (aSi.ShapeType()!=TopAbs_SHELL) {
203         aOr=aSi.Orientation();
204         if (aOr==TopAbs_INTERNAL) {
205           aMSI.Add(aSi);
206         }
207       }
208     }
209   }
210   aNbSI=aMSI.Extent();
211   //
212   // 3 Solids without internals
213   BOPTools_AlgoTools::MakeContainer(TopAbs_COMPOUND, myResult);  
214   //
215   aSB.SetContext(myContext);
216   aSB.SetShapes(aSFS);
217   aSB.Perform();
218   iErr=aSB.ErrorStatus();
219   if (iErr) {
220     myErrorStatus=20; // SolidBuilder failed
221     return;
222   }
223   //
224   const BOPCol_ListOfShape& aLSR=aSB.Areas();
225   // 
226   // 4 Add the internals
227   if (aNbSI) {
228     AddInternalShapes(aLSR, aMSI);
229   }
230   //
231   aItLS.Initialize(aLSR);
232   for (; aItLS.More(); aItLS.Next()) {
233     const TopoDS_Shape& aSR=aItLS.Value();
234     aBB.Add(myResult, aSR);
235   }
236 }
237
238 //=======================================================================
239 //function : AddInternalShapes
240 //purpose  : 
241 //=======================================================================
242 void GEOMAlgo_RemoverWebs::AddInternalShapes(const BOPCol_ListOfShape& aLSR,
243                                              const BOPCol_IndexedMapOfShape& aMSI)
244 {
245   Standard_Integer i, aNbSI;
246   TopAbs_State aState;  
247   TopoDS_Solid aSd;
248   BRep_Builder aBB;
249   BOPCol_ListIteratorOfListOfShape aItLS;
250   Handle(BOPInt_Context) aCtx;
251   //
252   aCtx=new BOPInt_Context;
253   //
254   aNbSI=aMSI.Extent();
255   for (i=1; i<=aNbSI; ++i) {
256     const TopoDS_Shape& aSI=aMSI(i);
257     //
258     aItLS.Initialize(aLSR);
259     for (; aItLS.More(); aItLS.Next()) {
260       aSd=*((TopoDS_Solid*)&aItLS.Value());
261       //
262       aState=BOPTools_AlgoTools::ComputeStateByOnePoint(aSI, aSd, 1.e-11, aCtx);
263       if (aState==TopAbs_IN) {
264         aBB.Add(aSd, aSI);
265         //
266         BRepClass3d_SolidClassifier& aSC=aCtx->SolidClassifier(aSd);
267         aSC.Load(aSd);
268       }
269     }
270   }
271 }
272 //
273 // myErrorStatus
274 // 0  - OK
275 // 10 - myShape is Null
276 // 11 - myShape contains non-solids
277 // 20 - BuilderSolid failed