Salome HOME
untabify
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_Builder.cxx
1 //  Copyright (C) 2007-2008  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_Builder.cxx
23 // Created:     
24 // Author:      Peter KURNEV 
25 //
26 #include <GEOMAlgo_Builder.ixx>
27
28 #include <TopAbs_ShapeEnum.hxx>
29
30 #include <TopoDS_Shape.hxx>
31 #include <TopoDS_Compound.hxx>
32 #include <TopoDS_Iterator.hxx>
33
34 #include <BRep_Builder.hxx>
35
36 #include <TopTools_MapOfShape.hxx>
37 #include <TopTools_ListOfShape.hxx>
38 #include <TopTools_ListIteratorOfListOfShape.hxx>
39
40 #include <BOP_CorrectTolerances.hxx>
41
42 #include <BRepLib.hxx>
43 #include <NMTTools_PaveFiller.hxx>
44
45 //=======================================================================
46 //function : 
47 //purpose  : 
48 //=======================================================================
49   GEOMAlgo_Builder::GEOMAlgo_Builder()
50 :
51   GEOMAlgo_BuilderShape()
52 {
53   myNbTypes=9;
54   myPaveFiller=NULL;
55   myEntryPoint=0; // Entry point through PerformWithFiller ()
56 }
57 //=======================================================================
58 //function : ~
59 //purpose  : 
60 //=======================================================================
61   GEOMAlgo_Builder::~GEOMAlgo_Builder()
62 {
63   if (myEntryPoint==1) {
64     if (myPaveFiller) {
65       delete myPaveFiller;
66       myPaveFiller=NULL;
67     }
68   }
69 }
70 //=======================================================================
71 //function : AddCompound
72 //purpose  : 
73 //=======================================================================
74   void GEOMAlgo_Builder::AddCompound(const TopoDS_Shape& theShape)
75 {
76   TopoDS_Iterator aIt;
77   //
78   aIt.Initialize(theShape);
79   for (; aIt.More(); aIt.Next()) {
80     const TopoDS_Shape& aS=aIt.Value();
81     AddShape(aS);
82   }
83 }
84 //=======================================================================
85 //function : AddShape
86 //purpose  : 
87 //=======================================================================
88   void GEOMAlgo_Builder::AddShape(const TopoDS_Shape& theShape)
89 {
90   if (myMapFence.Add(theShape)) {
91     myShapes.Append(theShape);
92   }
93 }
94 //=======================================================================
95 //function : Shapes
96 //purpose  : 
97 //=======================================================================
98   const TopTools_ListOfShape& GEOMAlgo_Builder::Shapes()const
99 {
100   return myShapes;
101 }
102 //=======================================================================
103 //function : Clear
104 //purpose  : 
105 //=======================================================================
106   void GEOMAlgo_Builder::Clear()
107 {
108   myShapes.Clear();
109   ClearInternals();
110 }
111 //=======================================================================
112 //function : ClearInternals
113 //purpose  : 
114 //=======================================================================
115   void GEOMAlgo_Builder::ClearInternals()
116 {
117   Standard_Integer i;
118   //
119   for (i=0; i<myNbTypes; ++i){
120     myShapes1[i].Clear();
121   }
122   myImages.Clear();
123   //
124   myInParts.Clear();
125   mySplitFaces.Clear();
126   mySameDomainShapes.Clear();
127   myDraftSolids.Clear();
128   myMapFence.Clear();
129 }
130 //=======================================================================
131 //function : Prepare
132 //purpose  : 
133 //=======================================================================
134   void GEOMAlgo_Builder::Prepare()
135 {
136   myErrorStatus=0;
137   //
138   Standard_Integer aNb;
139   BRep_Builder aBB;
140   TopoDS_Compound aC;
141   TopTools_ListIteratorOfListOfShape aIt;
142   //
143   aNb=myShapes.Extent();
144   if (aNb<2) {
145     myErrorStatus=10; // No shapes to process
146     return;
147   }
148   //
149   // 1. myShape is empty compound
150   aBB.MakeCompound(aC);
151   myShape=aC;
152   //
153   // 2. myShapes1
154   aIt.Initialize(myShapes);
155   for (; aIt.More(); aIt.Next()) {
156     const TopoDS_Shape& aS=aIt.Value();
157     AddShape1(aS);
158   }
159 }
160 //=======================================================================
161 //function : PostTreat
162 //purpose  : 
163 //=======================================================================
164   void GEOMAlgo_Builder::PostTreat()
165 {
166   Standard_Integer aNbS;
167   TopoDS_Iterator aIt;
168   TopTools_ListOfShape aLS;
169   //
170   aIt.Initialize(myShape);
171   for (; aIt.More(); aIt.Next()) {
172     const TopoDS_Shape& aS=aIt.Value();
173     aLS.Append(aS);
174   }
175   aNbS=aLS.Extent();
176   if (aNbS==1) {
177     myShape=aLS.First();
178   }
179   
180   BRepLib::SameParameter(myShape, 1.e-7, Standard_True);
181 }
182 //=======================================================================
183 //function : AddShape1
184 //purpose  : 
185 //=======================================================================
186   void GEOMAlgo_Builder::AddShape1(const TopoDS_Shape& theShape)
187 {
188   Standard_Integer iType;
189   TopAbs_ShapeEnum aType;
190   TopoDS_Iterator aIt;
191   TopTools_MapOfShape aM;
192   //
193   aType=theShape.ShapeType();
194   if (aType==TopAbs_COMPOUND) {
195     aIt.Initialize(theShape);
196     for (; aIt.More(); aIt.Next()) {
197       const TopoDS_Shape& aS=aIt.Value();
198       AddShape1(aS);
199     }
200   }
201   //
202   iType=(Standard_Integer)aType;
203   if (aM.Add(theShape)) {
204     myShapes1[iType].Append(theShape);
205   }
206 }
207 //=======================================================================
208 //function : BuildResult
209 //purpose  : 
210 //=======================================================================
211   void GEOMAlgo_Builder::BuildResult(const TopAbs_ShapeEnum theType)
212 {
213   myErrorStatus=0;
214   //
215   TopAbs_ShapeEnum aType;
216   BRep_Builder aBB;
217   TopTools_MapOfShape aM;
218   TopTools_ListIteratorOfListOfShape aIt, aItIm;
219   //
220   aIt.Initialize(myShapes);
221   for (; aIt.More(); aIt.Next()) {
222     const TopoDS_Shape& aS=aIt.Value();
223     aType=aS.ShapeType();
224     if (aType==theType) {
225       if (myImages.HasImage(aS)){
226         const TopTools_ListOfShape& aLSIm=myImages.Image(aS);
227         aItIm.Initialize(aLSIm);
228         for (; aItIm.More(); aItIm.Next()) {
229           const TopoDS_Shape& aSIm=aItIm.Value();
230           if (aM.Add(aSIm)) {
231             aBB.Add(myShape, aSIm);
232           }
233         }
234       }
235       else {
236         if (aM.Add(aS)) {
237           aBB.Add(myShape, aS);
238         }
239       }
240     }
241   }
242 }
243 //=======================================================================
244 //function : Perform
245 //purpose  : 
246 //=======================================================================
247   void GEOMAlgo_Builder::Perform()
248 {
249   myErrorStatus=0;
250   //
251   BRep_Builder aBB;
252   TopoDS_Compound aCS;
253   TopTools_ListIteratorOfListOfShape aIt;
254   //
255   aBB.MakeCompound(aCS);
256   //
257   aIt.Initialize(myShapes);
258   for (; aIt.More(); aIt.Next()) {
259     const TopoDS_Shape& aS=aIt.Value();
260     aBB.Add(aCS, aS);
261   }
262   //
263   NMTTools_PaveFiller* pPF=new NMTTools_PaveFiller;
264   //
265   pPF->SetCompositeShape(aCS);
266   pPF->Perform();
267   //
268   myEntryPoint=1;
269   PerformInternal(*pPF);
270 }
271 //=======================================================================
272 //function : PerformWithFiller
273 //purpose  : 
274 //=======================================================================
275   void GEOMAlgo_Builder::PerformWithFiller(const NMTTools_PaveFiller& theDSF)
276 {
277   myEntryPoint=0;
278   //
279   PerformInternal(theDSF);
280 }
281 //=======================================================================
282 //function : PerformInternal
283 //purpose  : 
284 //=======================================================================
285   void GEOMAlgo_Builder::PerformInternal(const NMTTools_PaveFiller& pPF)
286 {
287   myErrorStatus=0;
288   //
289   Standard_Boolean bIsDone;
290   //
291   // 0. myPaveFiller
292   myPaveFiller=(NMTTools_PaveFiller *)&pPF;
293   //
294   bIsDone=myPaveFiller->IsDone();
295   if (!bIsDone) {
296     myErrorStatus=2; // PaveFiller is failed
297     return;
298   }
299   //
300   // 1. Clear Internal fields
301   ClearInternals();
302   //
303   // 2. Prepare
304   Prepare();
305   if (myErrorStatus) {
306     return;
307   }
308   //
309   // 3. Fill Images
310   //
311   // 3.1 Vertices
312   FillImagesVertices();
313   if (myErrorStatus) {
314     return;
315   }
316   //
317   BuildResult(TopAbs_VERTEX);
318   if (myErrorStatus) {
319     return;
320   }
321   // 3.2 Edges
322   FillImagesEdges();
323   if (myErrorStatus) {
324     return;
325   }
326   
327   BuildResult(TopAbs_EDGE);
328   if (myErrorStatus) {
329     return;
330   }
331   // 3.3 Wires
332   FillImagesContainers(TopAbs_WIRE);
333   if (myErrorStatus) {
334     return;
335   }
336   
337   BuildResult(TopAbs_WIRE);
338   if (myErrorStatus) {
339     return;
340   }
341   
342   // 3.4 Faces
343   FillImagesFaces();
344   if (myErrorStatus) {
345     return;
346   }
347   
348   BuildResult(TopAbs_FACE);
349   if (myErrorStatus) {
350     return;
351   }
352   // 3.5 Shells
353   
354   FillImagesContainers(TopAbs_SHELL);
355   if (myErrorStatus) {
356     return;
357   }
358   
359   BuildResult(TopAbs_SHELL);
360   if (myErrorStatus) {
361     return;
362   }
363   // 3.6 Solids
364   FillImagesSolids();
365   if (myErrorStatus) {
366     return;
367   }
368   
369   BuildResult(TopAbs_SOLID);
370   if (myErrorStatus) {
371     return;
372   }
373   // 3.7 CompSolids
374   FillImagesContainers(TopAbs_COMPSOLID);
375   if (myErrorStatus) {
376     return;
377   }
378   
379   BuildResult(TopAbs_COMPSOLID);
380   if (myErrorStatus) {
381     return;
382   }
383   // 3.8 Compounds
384   FillImagesContainers(TopAbs_COMPOUND);
385   if (myErrorStatus) {
386     return;
387   }
388   
389   BuildResult(TopAbs_COMPOUND);
390   if (myErrorStatus) {
391     return;
392   }
393   //
394   // 4.History
395   PrepareHistory();
396   //
397   // 5 Post-treatment 
398   PostTreat();
399 }
400 //
401 // myErrorStatus
402 // 
403 // 0  - Ok
404 // 1  - The object is just initialized
405 // 2  - PaveFiller is failed
406 // 10 - No shapes to process
407 // 30 - SolidBuilder failed