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