0024825: Fit Boolean Operation Algorithm to treat multiple arguments.
[occt.git] / src / BOPAlgo / BOPAlgo_Builder_4.cxx
1 // Created by: Peter KURNEV
2 // Copyright (c) 2010-2014 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 // This file is part of Open CASCADE Technology software library.
8 //
9 // This library is free software; you can redistribute it and/or modify it under
10 // the terms of the GNU Lesser General Public License version 2.1 as published
11 // by the Free Software Foundation, with special exception defined in the file
12 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
13 // distribution for complete text of the license and disclaimer of any warranty.
14 //
15 // Alternatively, this file may be used under the terms of Open CASCADE
16 // commercial license or contractual agreement.
17
18 #include <BOPAlgo_Builder.ixx>
19
20 #include <TopoDS_Iterator.hxx>
21
22 #include <BOPCol_ListOfShape.hxx>
23 #include <BOPCol_MapOfShape.hxx>
24
25 #include <BOPDS_DS.hxx>
26
27 #include <BOPTools_AlgoTools.hxx>
28 #include <BOPTools.hxx>
29
30
31 //=======================================================================
32 //function : Generated
33 //purpose  : 
34 //=======================================================================
35 const TopTools_ListOfShape& BOPAlgo_Builder::Generated
36   (const TopoDS_Shape&)
37 {
38   myHistShapes.Clear();
39   return myHistShapes;
40 }
41 //=======================================================================
42 //function : Modified
43 //purpose  : 
44 //=======================================================================
45 const TopTools_ListOfShape& BOPAlgo_Builder::Modified
46   (const TopoDS_Shape& theS)
47 {
48   Standard_Boolean bHasImage, bToReverse;
49   TopAbs_ShapeEnum aType;
50   BOPCol_ListIteratorOfListOfShape aIt;
51   //
52   myHistShapes.Clear();
53   //
54   if (theS.IsNull()) {
55     return myHistShapes;
56   }
57   //
58   bHasImage=myImages.IsBound(theS);
59   if (!bHasImage) {
60     return myHistShapes;
61   }
62   //
63   aType=theS.ShapeType();
64   //
65   if (!(aType==TopAbs_EDGE   || aType==TopAbs_FACE || 
66       aType==TopAbs_VERTEX || aType==TopAbs_SOLID)) {
67     return myHistShapes;
68   } 
69   //
70   //PrepareHistory();
71   //
72   const BOPCol_ListOfShape& aLSp=myImages.Find(theS);
73   aIt.Initialize(aLSp);
74   for (; aIt.More(); aIt.Next()) {
75     TopoDS_Shape aSp=aIt.Value();
76     if (myShapesSD.IsBound(aSp)) {
77       aSp = myShapesSD.Find(aSp);
78     }
79     //
80     if (myMapShape.Contains(aSp)) {
81       //
82       if (aType==TopAbs_VERTEX || aType==TopAbs_SOLID) {
83         aSp.Orientation(theS.Orientation());
84       }
85       else {
86         bToReverse=
87           BOPTools_AlgoTools::IsSplitToReverse(aSp, theS, myContext);
88         if (bToReverse) {
89           aSp.Reverse();
90         }
91       }
92       //
93       myHistShapes.Append(aSp);
94     }
95   }
96   //
97   return myHistShapes;
98 }
99 //=======================================================================
100 //function : IsDeleted
101 //purpose  : 
102 //=======================================================================
103 Standard_Boolean BOPAlgo_Builder::IsDeleted
104   (const TopoDS_Shape& theS)
105 {
106   Standard_Boolean bRet, bHasImage, bContains;
107   TopAbs_ShapeEnum aType;
108   BOPCol_ListIteratorOfListOfShape aIt;
109   //
110   bRet=Standard_False;
111   //
112   if (theS.IsNull()) {
113     return !bRet; //true
114   }
115   //
116   aType=theS.ShapeType();
117   if (!(aType==TopAbs_EDGE || aType==TopAbs_FACE || 
118       aType==TopAbs_VERTEX || aType==TopAbs_SOLID)) {
119     return !bRet;
120   }
121   //
122   bHasImage=myImages.IsBound(theS);
123   if (!bHasImage) {
124     return !bRet; //true
125   }
126   //
127   //PrepareHistory();
128   //
129   bContains=myMapShape.Contains(theS);
130   if (bContains) {
131     return bRet; //false
132   }
133   //
134   const BOPCol_ListOfShape& aLSp=myImages.Find(theS);
135   aIt.Initialize(aLSp);
136   for (; aIt.More(); aIt.Next()) {
137     TopoDS_Shape aSp=aIt.Value();
138     //
139     if (!myShapesSD.IsBound(aSp)) {
140       if (myMapShape.Contains(aSp)) {
141         return bRet; //false
142       }
143     }
144     else {
145       TopoDS_Shape aSpR=myShapesSD.Find(aSp);
146       if (myMapShape.Contains(aSpR)) {
147         return bRet; //false
148       }
149     }
150   }
151   return !bRet; // true
152 }
153 //=======================================================================
154 //function : PrepareHistory
155 //purpose  : 
156 //=======================================================================
157 void BOPAlgo_Builder::PrepareHistory()
158 {
159   if (!myFlagHistory) {
160     return;
161   }
162   //
163   if(myShape.IsNull()) {
164     return;
165   }
166   //
167   Standard_Boolean bHasImage;
168   TopAbs_ShapeEnum aType;
169   BOPCol_MapOfShape aMS;
170   BOPCol_ListIteratorOfListOfShape aIt;
171   BOPCol_MapIteratorOfMapOfShape aItM;
172   //
173   // 1. Clearing 
174   BOPAlgo_BuilderShape::PrepareHistory();
175   //
176   // 2. myMapShape - all shapes of result with theirs sub-shapes 
177   BOPTools::MapShapes(myShape, myMapShape);
178   //
179   // 3. MS - all argument shapes with theirs sub-shapes
180   const BOPCol_ListOfShape& aArguments=myDS->Arguments();
181   aIt.Initialize(aArguments);
182   for (; aIt.More(); aIt.Next()) {
183     const TopoDS_Shape& aSx=aIt.Value();
184     BOPTools::MapShapes(aSx, aMS);
185   }
186   //
187   // 4. Treatment
188   aItM.Initialize(aMS);
189   for (; aItM.More(); aItM.Next()) {
190     const TopoDS_Shape& aSx=aItM.Key();
191     aType=aSx.ShapeType();
192     //
193     // 4.1 .myImagesResult
194     bHasImage=myImages.IsBound(aSx); 
195     //
196     BOPCol_ListOfShape aLSx;
197     if (!bHasImage) {
198       if (myMapShape.Contains(aSx)) {
199         aLSx.Append(aSx);
200         myImagesResult.Add(aSx, aLSx);
201       }
202     }
203     else {
204       const BOPCol_ListOfShape& aLSp=myImages.Find(aSx);
205       aIt.Initialize(aLSp);
206       for (; aIt.More(); aIt.Next()) {
207         const TopoDS_Shape& aSp=aIt.Value();
208         if (myMapShape.Contains(aSp)) {
209           aLSx.Append(aSp);
210         }
211       }
212       myImagesResult.Add(aSx, aLSx);
213     }
214     // <- A
215     //
216     // 4.2 As it was 
217     if (!myHasDeleted) {
218       myHasDeleted=IsDeleted(aSx);
219     }
220     //
221     if (!myHasModified && bHasImage) {
222       if (aType==TopAbs_EDGE   || aType==TopAbs_FACE || 
223           aType==TopAbs_VERTEX || aType==TopAbs_SOLID) {
224         myHasModified = Standard_True;
225       } 
226     }
227     //
228     if (!myHasGenerated) {
229       if (aType==TopAbs_FACE) {
230         const TopTools_ListOfShape& aLG = Generated(aSx);
231         myHasGenerated = aLG.Extent() > 0;
232       }
233     }
234   }
235   myFlagHistory=Standard_True;
236 }