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