Salome HOME
Merge from V6_main 01/04/2013
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_Builder.cxx
1 // Copyright (C) 2007-2013  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.hxx>
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 #include <GEOMAlgo_Tools.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 //=======================================================================
163 //function : AddShape1
164 //purpose  :
165 //=======================================================================
166   void GEOMAlgo_Builder::AddShape1(const TopoDS_Shape& theShape)
167 {
168   Standard_Integer iType;
169   TopAbs_ShapeEnum aType;
170   TopoDS_Iterator aIt;
171   TopTools_MapOfShape aM;
172   //
173   aType=theShape.ShapeType();
174   if (aType==TopAbs_COMPOUND) {
175     aIt.Initialize(theShape);
176     for (; aIt.More(); aIt.Next()) {
177       const TopoDS_Shape& aS=aIt.Value();
178       AddShape1(aS);
179     }
180   }
181   //
182   iType=(Standard_Integer)aType;
183   if (aM.Add(theShape)) {
184     myShapes1[iType].Append(theShape);
185   }
186 }
187 //=======================================================================
188 //function : BuildResult
189 //purpose  :
190 //=======================================================================
191   void GEOMAlgo_Builder::BuildResult(const TopAbs_ShapeEnum theType)
192 {
193   myErrorStatus=0;
194   //
195   TopAbs_ShapeEnum aType;
196   BRep_Builder aBB;
197   TopTools_MapOfShape aM;
198   TopTools_ListIteratorOfListOfShape aIt, aItIm;
199   //
200   aIt.Initialize(myShapes);
201   for (; aIt.More(); aIt.Next()) {
202     const TopoDS_Shape& aS=aIt.Value();
203     aType=aS.ShapeType();
204     if (aType==theType) {
205       if (myImages.HasImage(aS)){
206         const TopTools_ListOfShape& aLSIm=myImages.Image(aS);
207         aItIm.Initialize(aLSIm);
208         for (; aItIm.More(); aItIm.Next()) {
209           const TopoDS_Shape& aSIm=aItIm.Value();
210           if (aM.Add(aSIm)) {
211             aBB.Add(myShape, aSIm);
212           }
213         }
214       }
215       else {
216         if (aM.Add(aS)) {
217           aBB.Add(myShape, aS);
218         }
219       }
220     }
221   }
222 }
223 //=======================================================================
224 //function : Perform
225 //purpose  :
226 //=======================================================================
227   void GEOMAlgo_Builder::Perform()
228 {
229   myErrorStatus=0;
230   //
231   BRep_Builder aBB;
232   TopoDS_Compound aCS;
233   TopTools_ListIteratorOfListOfShape aIt;
234   //
235   aBB.MakeCompound(aCS);
236   //
237   aIt.Initialize(myShapes);
238   for (; aIt.More(); aIt.Next()) {
239     const TopoDS_Shape& aS=aIt.Value();
240     aBB.Add(aCS, aS);
241   }
242   //
243   NMTTools_PaveFiller* pPF=new NMTTools_PaveFiller;
244   //
245   pPF->SetCompositeShape(aCS);
246   pPF->Perform();
247   //
248   myEntryPoint=1;
249   PerformInternal(*pPF);
250 }
251 //=======================================================================
252 //function : PerformWithFiller
253 //purpose  :
254 //=======================================================================
255   void GEOMAlgo_Builder::PerformWithFiller(const NMTTools_PaveFiller& theDSF)
256 {
257   myEntryPoint=0;
258   //
259   PerformInternal(theDSF);
260 }
261 //=======================================================================
262 //function : PerformInternal
263 //purpose  :
264 //=======================================================================
265   void GEOMAlgo_Builder::PerformInternal(const NMTTools_PaveFiller& pPF)
266 {
267   myErrorStatus=0;
268   //
269   Standard_Boolean bIsDone;
270   //
271   // 0. myPaveFiller
272   myPaveFiller=(NMTTools_PaveFiller *)&pPF;
273   //
274   bIsDone=myPaveFiller->IsDone();
275   if (!bIsDone) {
276     myErrorStatus=2; // PaveFiller is failed
277     return;
278   }
279   //
280   // 1. Clear Internal fields
281   ClearInternals();
282   //
283   // 2. Prepare
284   Prepare();
285   if (myErrorStatus) {
286     return;
287   }
288   //
289   // 3. Fill Images
290   //
291   // 3.1 Vertices
292   FillImagesVertices();
293   if (myErrorStatus) {
294     return;
295   }
296   //
297   BuildResult(TopAbs_VERTEX);
298   if (myErrorStatus) {
299     return;
300   }
301   // 3.2 Edges
302   FillImagesEdges();
303   if (myErrorStatus) {
304     return;
305   }
306
307   BuildResult(TopAbs_EDGE);
308   if (myErrorStatus) {
309     return;
310   }
311   // 3.3 Wires
312   FillImagesContainers(TopAbs_WIRE);
313   if (myErrorStatus) {
314     return;
315   }
316
317   BuildResult(TopAbs_WIRE);
318   if (myErrorStatus) {
319     return;
320   }
321
322   // 3.4 Faces
323   FillImagesFaces();
324   if (myErrorStatus) {
325     return;
326   }
327
328   BuildResult(TopAbs_FACE);
329   if (myErrorStatus) {
330     return;
331   }
332   // 3.5 Shells
333
334   FillImagesContainers(TopAbs_SHELL);
335   if (myErrorStatus) {
336     return;
337   }
338
339   BuildResult(TopAbs_SHELL);
340   if (myErrorStatus) {
341     return;
342   }
343   // 3.6 Solids
344   FillImagesSolids();
345   if (myErrorStatus) {
346     return;
347   }
348
349   BuildResult(TopAbs_SOLID);
350   if (myErrorStatus) {
351     return;
352   }
353   // 3.7 CompSolids
354   FillImagesContainers(TopAbs_COMPSOLID);
355   if (myErrorStatus) {
356     return;
357   }
358
359   BuildResult(TopAbs_COMPSOLID);
360   if (myErrorStatus) {
361     return;
362   }
363   // 3.8 Compounds
364   FillImagesContainers(TopAbs_COMPOUND);
365   if (myErrorStatus) {
366     return;
367   }
368
369   BuildResult(TopAbs_COMPOUND);
370   if (myErrorStatus) {
371     return;
372   }
373   //
374   // 4.History
375   PrepareHistory();
376   //
377   // 5 Post-treatment
378   PostTreat();
379 }
380 //
381 // static
382 //   void CorrectWires(const TopoDS_Shape& aS);
383 //
384 //=======================================================================
385 //function : PostTreat
386 //purpose  :
387 //=======================================================================
388   void GEOMAlgo_Builder::PostTreat()
389 {
390   Standard_Integer aNbS;
391   TopoDS_Iterator aIt;
392   TopTools_ListOfShape aLS;
393   //
394   aIt.Initialize(myShape);
395   for (; aIt.More(); aIt.Next()) {
396     const TopoDS_Shape& aS=aIt.Value();
397     aLS.Append(aS);
398   }
399   aNbS=aLS.Extent();
400   if (aNbS==1) {
401     myShape=aLS.First();
402   }
403
404   BRepLib::SameParameter(myShape, 1.e-7, Standard_True);
405   //
406   GEOMAlgo_Tools::CorrectWires(myShape);
407 }
408 //
409 // myErrorStatus
410 //
411 // 0  - Ok
412 // 1  - The object is just initialized
413 // 2  - PaveFiller is failed
414 // 10 - No shapes to process
415 // 30 - SolidBuilder failed
416