]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMAlgo/GEOMAlgo_RemoverWebs.cxx
Salome HOME
Mantis issue 0021934: [CEA 694] Delete the internal faces of an object.
[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
26 #include <GEOMAlgo_RemoverWebs.hxx>
27
28 #include <TopoDS_Iterator.hxx>
29 #include <TopoDS_Shape.hxx>
30 #include <TopoDS_Solid.hxx>
31 #include <BRep_Builder.hxx>
32
33 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
34 #include <TopTools_IndexedMapOfShape.hxx>
35 #include <TopTools_ListIteratorOfListOfShape.hxx>
36 #include <TopTools_ListOfShape.hxx>
37 #include <TopTools_MapOfShape.hxx>
38 #include <TopTools_MapIteratorOfMapOfShape.hxx>
39
40 #include <TopExp.hxx>
41
42 #include <BRepClass3d_SolidClassifier.hxx>
43
44 #include <IntTools_Context.hxx>
45
46 #include <GEOMAlgo_ShapeAlgo.hxx>
47 #include <GEOMAlgo_BuilderSolid.hxx>
48 #include <GEOMAlgo_Tools3D.hxx>
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, aNbS, aNbR;
122   TopAbs_Orientation aOr;
123   TopoDS_Iterator aIt1, aIt2;
124   TopoDS_Shape aShape;
125   BRep_Builder aBB;
126   TopTools_MapOfShape aMFence;
127   TopTools_IndexedMapOfShape aMSI;
128   TopTools_IndexedDataMapOfShapeListOfShape aMFS;
129   TopTools_ListOfShape aSFS;
130   TopTools_ListIteratorOfListOfShape aItLS;
131   GEOMAlgo_BuilderSolid aSB;
132   //
133   //modified by NIZNHY-PKV Fri Jul 12 07:22:58 2013f
134   //
135   // 0.
136   // The compound myShape may contain equal solids
137   // (itz.brep for e.g.). The block is to refine
138   // such data if it is necessary. The shape to treat
139   // will be aShape (not myShape).
140   //
141   aShape=myShape;
142   //
143   aIt1.Initialize(myShape);
144   for (aNbS=0; aIt1.More(); aIt1.Next(), ++aNbS) {
145     const TopoDS_Shape& aS=aIt1.Value();
146     aMFence.Add(aS);
147   }
148   //
149   aNbR=aMFence.Extent();
150   if (aNbS!=aNbR) {
151     TopTools_MapIteratorOfMapOfShape aItMS;
152     //
153     GEOMAlgo_Tools3D::MakeContainer(TopAbs_COMPOUND, aShape);
154     //
155     aItMS.Initialize(aMFence);
156     for (; aItMS.More(); aItMS.Next()) {
157       const TopoDS_Shape& aS=aItMS.Key();
158       aBB.Add(aShape, aS);
159     }
160   }
161   //modified by NIZNHY-PKV Fri Jul 12 07:23:02 2013t
162   //
163   aNbF2=0;
164   //
165   // 1. aSFS: Faces
166   TopExp::MapShapesAndAncestors(aShape, TopAbs_FACE, TopAbs_SOLID, aMFS);
167   //
168   aNbF=aMFS.Extent();
169   for (i=1; i<=aNbF; ++i) {
170     const TopoDS_Shape& aFx=aMFS.FindKey(i);
171     aOr=aFx.Orientation();
172     if (aOr==TopAbs_INTERNAL) {
173       TopoDS_Shape aFi;
174       //
175       aFi=aFx;
176       aFi.Orientation(TopAbs_FORWARD);
177       aSFS.Append(aFi);
178       aFi.Orientation(TopAbs_REVERSED);
179       aSFS.Append(aFi);
180     }
181     else {
182       const TopTools_ListOfShape& aLSx=aMFS(i);
183       aNbSx=aLSx.Extent();
184       if (aNbSx==1) {
185         aSFS.Append(aFx);
186       }
187       else if (aNbSx==2) {
188         ++aNbF2;
189       }
190     }
191   }
192   //
193   if (!aNbF2) { // nothing to do here
194     myResult=aShape;
195     return;
196   }
197   //
198   // 2 Internal shapes: edges, vertices
199   aIt1.Initialize(aShape);
200   for (; aIt1.More(); aIt1.Next()) {
201     const TopoDS_Shape& aSD=aIt1.Value();
202     //
203     aIt2.Initialize(aSD);
204     for (; aIt2.More(); aIt2.Next()) {
205       const TopoDS_Shape& aSi=aIt2.Value();
206       if (aSi.ShapeType()!=TopAbs_SHELL) {
207         aOr=aSi.Orientation();
208         if (aOr==TopAbs_INTERNAL) {
209           aMSI.Add(aSi);
210         }
211       }
212     }
213   }
214   aNbSI=aMSI.Extent();
215   //
216   // 3 Solids without internals
217   GEOMAlgo_Tools3D::MakeContainer(TopAbs_COMPOUND, myResult);
218   //
219   aSB.SetContext(myContext);
220   aSB.SetShapes(aSFS);
221   aSB.Perform();
222   iErr=aSB.ErrorStatus();
223   if (iErr) {
224     myErrorStatus=20; // SolidBuilder failed
225     return;
226   }
227   //
228   const TopTools_ListOfShape& aLSR=aSB.Areas();
229   //
230   // 4 Add the internals
231   if (aNbSI) {
232     AddInternalShapes(aLSR, aMSI);
233   }
234   //
235   aItLS.Initialize(aLSR);
236   for (; aItLS.More(); aItLS.Next()) {
237     const TopoDS_Shape& aSR=aItLS.Value();
238     aBB.Add(myResult, aSR);
239   }
240 }
241
242 //=======================================================================
243 //function : AddInternalShapes
244 //purpose  :
245 //=======================================================================
246 void GEOMAlgo_RemoverWebs::AddInternalShapes(const TopTools_ListOfShape& aLSR,
247                                              const TopTools_IndexedMapOfShape& aMSI)
248 {
249   Standard_Integer i, aNbSI;
250   TopAbs_State aState;
251   TopoDS_Solid aSd;
252   BRep_Builder aBB;
253   TopTools_ListIteratorOfListOfShape aItLS;
254   Handle(IntTools_Context) aCtx;
255   //
256   aCtx=new IntTools_Context;
257   //
258   aNbSI=aMSI.Extent();
259   for (i=1; i<=aNbSI; ++i) {
260     const TopoDS_Shape& aSI=aMSI(i);
261     //
262     aItLS.Initialize(aLSR);
263     for (; aItLS.More(); aItLS.Next()) {
264       aSd=*((TopoDS_Solid*)&aItLS.Value());
265       //
266       aState=GEOMAlgo_Tools3D::ComputeStateByOnePoint(aSI, aSd, 1.e-11, aCtx);
267       if (aState==TopAbs_IN) {
268         aBB.Add(aSd, aSI);
269         //
270         BRepClass3d_SolidClassifier& aSC=aCtx->SolidClassifier(aSd);
271         aSC.Load(aSd);
272       }
273     }
274   }
275 }
276 //
277 // myErrorStatus
278 // 0  - OK
279 // 10 - myShape is Null
280 // 11 - myShape contains non-solids
281 // 20 - BuilderSolid failed