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