0021762: Integration of new Boolean Operation algorithm to OCCT.
[occt.git] / src / BOPAlgo / BOPAlgo_Builder.cxx
1 // Created by: Peter KURNEV
2 // Copyright (c) 2010-2012 OPEN CASCADE SAS
3 // Copyright (c) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
4 // Copyright (c) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, CEDRAT,
5 //                         EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 //
7 // The content of this file is subject to the Open CASCADE Technology Public
8 // License Version 6.5 (the "License"). You may not use the content of this file
9 // except in compliance with the License. Please obtain a copy of the License
10 // at http://www.opencascade.org and read it completely before using this file.
11 //
12 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
13 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
14 //
15 // The Original Code and all software distributed under the License is
16 // distributed on an "AS IS" basis, without warranty of any kind, and the
17 // Initial Developer hereby disclaims all such warranties, including without
18 // limitation, any warranties of merchantability, fitness for a particular
19 // purpose or non-infringement. Please see the License for the specific terms
20 // and conditions governing the rights and limitations under the License.
21
22
23 #include <BOPAlgo_Builder.ixx>
24
25 #include <NCollection_IncAllocator.hxx>
26
27 #include <TopoDS_Compound.hxx>
28 #include <BRep_Builder.hxx>
29
30
31 #include <BOPTools_AlgoTools.hxx>
32
33 //=======================================================================
34 //function : 
35 //purpose  : 
36 //=======================================================================
37   BOPAlgo_Builder::BOPAlgo_Builder()
38 :
39   BOPAlgo_BuilderShape(),
40   myArguments(myAllocator),
41   myMapFence(100, myAllocator),
42   myPaveFiller(NULL),
43   myDS(NULL),
44   //myContext(NULL),
45   myEntryPoint(0),
46   myImages(100, myAllocator),
47   myShapesSD(100, myAllocator),
48   mySplits(100, myAllocator),
49   myOrigins(100, myAllocator)
50 {
51 }
52 //=======================================================================
53 //function : 
54 //purpose  : 
55 //=======================================================================
56   BOPAlgo_Builder::BOPAlgo_Builder(const Handle(NCollection_BaseAllocator)& theAllocator)
57 :
58   BOPAlgo_BuilderShape(theAllocator),
59   myArguments(myAllocator),
60   myMapFence(100, myAllocator),
61   myPaveFiller(NULL),
62   myDS(NULL),
63   //myContext(NULL),
64   myEntryPoint(0),
65   myImages(100, myAllocator), 
66   myShapesSD(100, myAllocator),
67   mySplits(100, myAllocator),
68   myOrigins(100, myAllocator)
69 {
70 }
71 //=======================================================================
72 //function : ~
73 //purpose  : 
74 //=======================================================================
75   BOPAlgo_Builder::~BOPAlgo_Builder()
76 {
77   if (myEntryPoint==1) {
78     if (myPaveFiller) {
79       delete myPaveFiller;
80       myPaveFiller=NULL;
81     }
82   }
83 }
84 //=======================================================================
85 //function : Clear
86 //purpose  : 
87 //=======================================================================
88   void BOPAlgo_Builder::Clear()
89 {
90   myArguments.Clear();
91   myMapFence.Clear();
92   myImages.Clear();
93   myShapesSD.Clear();
94   mySplits.Clear();
95   myOrigins.Clear();
96 }
97 //=======================================================================
98 //function : AddArgument
99 //purpose  : 
100 //=======================================================================
101   void BOPAlgo_Builder::AddArgument(const TopoDS_Shape& theShape)
102 {
103   if (myMapFence.Add(theShape)) {
104     myArguments.Append(theShape);
105   }
106 }
107 //=======================================================================
108 //function : Arguments
109 //purpose  : 
110 //=======================================================================
111   const BOPCol_ListOfShape& BOPAlgo_Builder::Arguments()const
112 {
113   return myArguments;
114 }
115 //=======================================================================
116 //function : Images
117 //purpose  : 
118 //=======================================================================
119   const BOPCol_DataMapOfShapeListOfShape& BOPAlgo_Builder::Images()const
120 {
121   return myImages;
122 }
123 //=======================================================================
124 //function : Origins
125 //purpose  : 
126 //=======================================================================
127   const BOPCol_DataMapOfShapeShape& BOPAlgo_Builder::Origins()const
128 {
129   return myOrigins;
130 }
131
132 //=======================================================================
133 //function : ShapesSd
134 //purpose  : 
135 //=======================================================================
136   const BOPCol_DataMapOfShapeShape& BOPAlgo_Builder::ShapesSD()const
137 {
138   return myShapesSD;
139 }
140 //=======================================================================
141 //function : Splits
142 //purpose  : 
143 //=======================================================================
144   const BOPCol_DataMapOfShapeListOfShape& BOPAlgo_Builder::Splits()const
145 {
146   return mySplits;
147 }
148 //=======================================================================
149 //function : PPaveFiller
150 //purpose  : 
151 //=======================================================================
152   BOPAlgo_PPaveFiller BOPAlgo_Builder::PPaveFiller()
153 {
154   return myPaveFiller;
155 }
156 //=======================================================================
157 //function : PDS
158 //purpose  : 
159 //=======================================================================
160   BOPDS_PDS BOPAlgo_Builder::PDS()
161 {
162   return myDS;
163 }
164 //=======================================================================
165 // function: CheckData
166 // purpose: 
167 //=======================================================================
168   void BOPAlgo_Builder::CheckData()
169 {
170   Standard_Integer aNb;
171   //
172   myErrorStatus=0;
173   //
174   aNb=myArguments.Extent();
175   if (aNb<2) {
176     myErrorStatus=100; // too few arguments to process
177     return;
178   }
179   //
180   //  myPaveFiller
181   if (!myPaveFiller) {
182     myErrorStatus=101; 
183     return;
184   }
185   //
186   myErrorStatus=myPaveFiller->ErrorStatus();
187   if (myErrorStatus) {
188     myErrorStatus=102; // PaveFiller is failed
189     return;
190   }
191 }
192 //=======================================================================
193 //function : Prepare
194 //purpose  : 
195 //=======================================================================
196   void BOPAlgo_Builder::Prepare()
197 {
198   myErrorStatus=0;
199   //
200   BRep_Builder aBB;
201   TopoDS_Compound aC;
202   //
203   // 1. myShape is empty compound
204   aBB.MakeCompound(aC);
205   myShape=aC;
206   myFlagHistory=Standard_True;
207 }
208 //=======================================================================
209 //function : Perform
210 //purpose  : 
211 //=======================================================================
212   void BOPAlgo_Builder::Perform()
213 {
214   myErrorStatus=0;
215   //
216   if (myEntryPoint==1) {
217     if (myPaveFiller) {
218       delete myPaveFiller;
219       myPaveFiller=NULL;
220     }
221   }
222   //
223   Handle(NCollection_BaseAllocator) aAllocator=new NCollection_IncAllocator;
224   //
225   BOPAlgo_PaveFiller* pPF=new BOPAlgo_PaveFiller(aAllocator);
226   //
227   pPF->SetArguments(myArguments);
228   //
229   pPF->Perform();
230   //
231   myEntryPoint=1;
232   PerformInternal(*pPF);
233 }
234 //=======================================================================
235 //function : PerformWithFiller
236 //purpose  : 
237 //=======================================================================
238   void BOPAlgo_Builder::PerformWithFiller(const BOPAlgo_PaveFiller& theFiller)
239 {
240   myEntryPoint=0;
241   PerformInternal(theFiller);
242 }
243 //=======================================================================
244 //function : PerformInternal
245 //purpose  : 
246 //=======================================================================
247   void BOPAlgo_Builder::PerformInternal(const BOPAlgo_PaveFiller& theFiller)
248 {
249   myErrorStatus=0;
250   //
251   myPaveFiller=(BOPAlgo_PaveFiller*)&theFiller;
252   myDS=myPaveFiller->PDS();
253   myContext=myPaveFiller->Context();
254   //
255   // 1. CheckData
256   CheckData();
257   if (myErrorStatus) {
258     return;
259   }
260   //
261   // 2. Prepare
262   Prepare();
263   if (myErrorStatus) {
264     return;
265   }
266   //
267   // 3. Fill Images
268   // 3.1 Vertice
269   FillImagesVertices();
270   if (myErrorStatus) {
271     return;
272   }
273   //
274   BuildResult(TopAbs_VERTEX);
275   if (myErrorStatus) {
276     return;
277   }
278   // 3.2 Edges
279   FillImagesEdges();
280   if (myErrorStatus) {
281     return;
282   }
283   //
284   BuildResult(TopAbs_EDGE);
285   if (myErrorStatus) {
286     return;
287   }
288   //
289   // 3.3 Wires
290   FillImagesContainers(TopAbs_WIRE);
291   if (myErrorStatus) {
292     return;
293   }
294   //
295   BuildResult(TopAbs_WIRE);
296   if (myErrorStatus) {
297     return;
298   }
299   
300   // 3.4 Faces
301   FillImagesFaces();
302   if (myErrorStatus) {
303     return;
304   }
305   //
306   BuildResult(TopAbs_FACE);
307   if (myErrorStatus) {
308     return;
309   }
310   // 3.5 Shells
311   FillImagesContainers(TopAbs_SHELL);
312   if (myErrorStatus) {
313     return;
314   }
315   
316   BuildResult(TopAbs_SHELL);
317   if (myErrorStatus) {
318     return;
319   }
320   // 3.6 Solids
321   FillImagesSolids();
322   if (myErrorStatus) {
323     return;
324   }
325   
326   BuildResult(TopAbs_SOLID);
327   if (myErrorStatus) {
328     return;
329   }
330   // 3.7 CompSolids
331   FillImagesContainers(TopAbs_COMPSOLID);
332   if (myErrorStatus) {
333     return;
334   }
335   
336   BuildResult(TopAbs_COMPSOLID);
337   if (myErrorStatus) {
338     return;
339   }
340   
341   // 3.8 Compounds
342   FillImagesCompounds();
343   if (myErrorStatus) {
344     return;
345   }
346   
347   BuildResult(TopAbs_COMPOUND);
348   if (myErrorStatus) {
349     return;
350   }
351   //
352   // 4.History
353   PrepareHistory();
354   //
355   //
356   // 5 Post-treatment 
357   PostTreat();
358   
359 }
360 //
361 // myErrorStatus
362 // 
363 // 0  - Ok
364 // 
365 //=======================================================================
366 //function : PostTreat
367 //purpose  : 
368 //=======================================================================
369   void BOPAlgo_Builder::PostTreat()
370 {
371   //BRepLib::SameParameter(myShape, 1.e-7, Standard_True);
372   BOPTools_AlgoTools::CorrectTolerances(myShape, 0.01);
373   BOPTools_AlgoTools::CorrectShapeTolerances(myShape);
374 }