Salome HOME
NPAL15298: geompy.KindOfShape(aShape) first version implemented.
[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 <NMTTools_DSFiller.hxx>
42
43 #include <BRepLib.hxx>
44
45 //=======================================================================
46 //function : 
47 //purpose  : 
48 //=======================================================================
49   GEOMAlgo_Builder::GEOMAlgo_Builder()
50 :
51   GEOMAlgo_BuilderShape()
52 {
53   myNbTypes=9;
54   myDSFiller=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 (myDSFiller) {
65       delete myDSFiller;
66       myDSFiller=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   if (myDSFiller) {
264     delete myDSFiller;
265     myDSFiller=NULL;
266   }
267   NMTTools_DSFiller* pDSF=new NMTTools_DSFiller;
268   //
269   pDSF->SetCompositeShape(aCS);
270   pDSF->Perform();
271   //
272   myEntryPoint=1;
273   PerformInternal(*pDSF);
274 }
275 //=======================================================================
276 //function : PerformWithFiller
277 //purpose  : 
278 //=======================================================================
279   void GEOMAlgo_Builder::PerformWithFiller(const NMTTools_DSFiller& theDSF)
280 {
281   myEntryPoint=0;
282   //
283   PerformInternal(theDSF);
284 }
285 //=======================================================================
286 //function : PerformInternal
287 //purpose  : 
288 //=======================================================================
289   void GEOMAlgo_Builder::PerformInternal(const NMTTools_DSFiller& theDSF)
290 {
291   myErrorStatus=0;
292   //
293   Standard_Boolean bIsDone;
294   //
295   // 0. myDSFiller
296   myDSFiller=(NMTTools_DSFiller *)&theDSF;
297   //
298   bIsDone=myDSFiller->IsDone();
299   if (!bIsDone) {
300     myErrorStatus=2; // DSFiller is failed
301     return;
302   }
303   //
304   // 1. Clear Internal fields
305   ClearInternals();
306   //
307   // 2. Prepare
308   Prepare();
309   if (myErrorStatus) {
310     return;
311   }
312   //
313   // 3. Fill Images
314   //
315   // 3.1 Vertices
316   FillImagesVertices();
317   if (myErrorStatus) {
318     return;
319   }
320   //
321   BuildResult(TopAbs_VERTEX);
322   if (myErrorStatus) {
323     return;
324   }
325   // 3.2 Edges
326   FillImagesEdges();
327   if (myErrorStatus) {
328     return;
329   }
330   
331   BuildResult(TopAbs_EDGE);
332   if (myErrorStatus) {
333     return;
334   }
335   // 3.3 Wires
336   FillImagesContainers(TopAbs_WIRE);
337   if (myErrorStatus) {
338     return;
339   }
340   
341   BuildResult(TopAbs_WIRE);
342   if (myErrorStatus) {
343     return;
344   }
345   
346   // 3.4 Faces
347   FillImagesFaces();
348   if (myErrorStatus) {
349     return;
350   }
351   
352   BuildResult(TopAbs_FACE);
353   if (myErrorStatus) {
354     return;
355   }
356   // 3.5 Shells
357   
358   FillImagesContainers(TopAbs_SHELL);
359   if (myErrorStatus) {
360     return;
361   }
362   
363   BuildResult(TopAbs_SHELL);
364   if (myErrorStatus) {
365     return;
366   }
367   // 3.6 Solids
368   FillImagesSolids();
369   if (myErrorStatus) {
370     return;
371   }
372   
373   BuildResult(TopAbs_SOLID);
374   if (myErrorStatus) {
375     return;
376   }
377   // 3.7 CompSolids
378   FillImagesContainers(TopAbs_COMPSOLID);
379   if (myErrorStatus) {
380     return;
381   }
382   
383   BuildResult(TopAbs_COMPSOLID);
384   if (myErrorStatus) {
385     return;
386   }
387   // 3.8 Compounds
388   FillImagesContainers(TopAbs_COMPOUND);
389   if (myErrorStatus) {
390     return;
391   }
392   
393   BuildResult(TopAbs_COMPOUND);
394   if (myErrorStatus) {
395     return;
396   }
397   //
398   // 4.History
399   PrepareHistory();
400   //
401   // 5 Post-treatment 
402   PostTreat();
403 }
404 //
405 // myErrorStatus
406 // 
407 // 0  - Ok
408 // 1  - The object is just initialized
409 // 2  - DSFiller is failed
410 // 10 - No shapes to process
411 // 30 - SolidBuilder failed