f22482b300332e1d62c737253f7caad1b4e0eb4d
[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
10 // under the terms of the GNU Lesser General Public 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 <BOPTools_AlgoTools.hxx>
26 #include <BOPTools.hxx>
27
28 //=======================================================================
29 //function : Generated
30 //purpose  : 
31 //=======================================================================
32   const TopTools_ListOfShape& BOPAlgo_Builder::Generated(const TopoDS_Shape& theS)
33 {
34   Standard_Boolean bHasImage, bToReverse;
35   TopAbs_ShapeEnum aType;
36   BOPCol_ListIteratorOfListOfShape aIt;
37   //
38   myHistShapes.Clear();
39   //
40   if (theS.IsNull()) {
41     return myHistShapes;
42   }
43   //
44   bHasImage=myImages.IsBound(theS);
45   if (!bHasImage) {
46     return myHistShapes;
47   }
48   //
49   aType=theS.ShapeType();
50   //
51   if (!(aType==TopAbs_EDGE   || aType==TopAbs_FACE || 
52       aType==TopAbs_VERTEX || aType==TopAbs_SOLID)) {
53     return myHistShapes;
54   } 
55   //
56   //PrepareHistory();
57   //
58   const BOPCol_ListOfShape& aLSp=myImages.Find(theS);
59   aIt.Initialize(aLSp);
60   for (; aIt.More(); aIt.Next()) {
61     const TopoDS_Shape& aSp=aIt.Value();
62     if (myShapesSD.IsBound(aSp)) {
63       if (myMapShape.Contains(aSp)) {
64         TopoDS_Shape aSpR=myShapesSD.Find(aSp);
65         //
66         if (aType==TopAbs_VERTEX || aType==TopAbs_SOLID) {
67           aSpR.Orientation(theS.Orientation());
68         }
69         else {
70           bToReverse=BOPTools_AlgoTools::IsSplitToReverse(aSpR, theS, myContext);
71           if (bToReverse) {
72             aSpR.Reverse();
73           }
74         }
75         //
76           myHistShapes.Append(aSpR);
77       }
78     }
79   }
80   //
81   return myHistShapes;
82 }
83 //=======================================================================
84 //function : Modified
85 //purpose  : 
86 //=======================================================================
87   const TopTools_ListOfShape& BOPAlgo_Builder::Modified(const TopoDS_Shape& theS)
88 {
89   Standard_Boolean bHasImage, bToReverse;
90   TopAbs_ShapeEnum aType;
91   BOPCol_ListIteratorOfListOfShape aIt;
92   //
93   myHistShapes.Clear();
94   //
95   if (theS.IsNull()) {
96     return myHistShapes;
97   }
98   //
99   bHasImage=myImages.IsBound(theS);
100   if (!bHasImage) {
101     return myHistShapes;
102   }
103   //
104   aType=theS.ShapeType();
105   //
106   if (!(aType==TopAbs_EDGE   || aType==TopAbs_FACE || 
107       aType==TopAbs_VERTEX || aType==TopAbs_SOLID)) {
108     return myHistShapes;
109   } 
110   //
111   //PrepareHistory();
112   //
113   const BOPCol_ListOfShape& aLSp=myImages.Find(theS);
114   aIt.Initialize(aLSp);
115   for (; aIt.More(); aIt.Next()) {
116     TopoDS_Shape aSp=aIt.Value();
117     if (!myShapesSD.IsBound(aSp)) {
118       if (myMapShape.Contains(aSp)) {
119         //
120         if (aType==TopAbs_VERTEX || aType==TopAbs_SOLID) {
121           aSp.Orientation(theS.Orientation());
122         }
123         else {
124           bToReverse=BOPTools_AlgoTools::IsSplitToReverse(aSp, theS, myContext);
125           if (bToReverse) {
126             aSp.Reverse();
127           }
128         }
129         //
130         myHistShapes.Append(aSp);
131       }
132     }
133   }
134   //
135   return myHistShapes;
136 }
137 //=======================================================================
138 //function : IsDeleted
139 //purpose  : 
140 //=======================================================================
141   Standard_Boolean BOPAlgo_Builder::IsDeleted(const TopoDS_Shape& theS)
142 {
143   Standard_Boolean bRet, bHasImage, bContains;
144   TopAbs_ShapeEnum aType;
145   BOPCol_ListIteratorOfListOfShape aIt;
146   //
147   bRet=Standard_False;
148   //
149   if (theS.IsNull()) {
150     return !bRet; //true
151   }
152   //
153   aType=theS.ShapeType();
154   if (!(aType==TopAbs_EDGE || aType==TopAbs_FACE || 
155       aType==TopAbs_VERTEX || aType==TopAbs_SOLID)) {
156     return !bRet;
157   }
158   //
159   bHasImage=myImages.IsBound(theS);
160   if (!bHasImage) {
161     return !bRet; //true
162   }
163   //
164   //PrepareHistory();
165   //
166   bContains=myMapShape.Contains(theS);
167   if (bContains) {
168     return bRet; //false
169   }
170   //
171   const BOPCol_ListOfShape& aLSp=myImages.Find(theS);
172   aIt.Initialize(aLSp);
173   for (; aIt.More(); aIt.Next()) {
174     TopoDS_Shape aSp=aIt.Value();
175     //
176     if (!myShapesSD.IsBound(aSp)) {
177       if (myMapShape.Contains(aSp)) {
178         return bRet; //false
179       }
180     }
181     else {
182       TopoDS_Shape aSpR=myShapesSD.Find(aSp);
183       if (myMapShape.Contains(aSpR)) {
184         return bRet; //false
185       }
186     }
187   }
188   return !bRet; // true
189 }
190 //=======================================================================
191 //function : PrepareHistory
192 //purpose  : 
193 //=======================================================================
194   void BOPAlgo_Builder::PrepareHistory()
195 {
196   if (!myFlagHistory) {
197     return;
198   }
199   //
200   if(myShape.IsNull()) {
201     return;
202   }
203   //
204   Standard_Boolean bHasImage, bContainsSD;
205   TopAbs_ShapeEnum aType;
206   BOPCol_MapOfShape aMS;
207   BOPCol_ListIteratorOfListOfShape aIt;
208   BOPCol_MapIteratorOfMapOfShape aItM;
209   //
210   // 1. Clearing 
211   BOPAlgo_BuilderShape::PrepareHistory();
212   //
213   // 2. myMapShape - all shapes of result with theirs sub-shapes 
214   BOPTools::MapShapes(myShape, myMapShape);
215   //
216   // 3. MS - all argument shapes with theirs sub-shapes
217   aIt.Initialize(myArguments);
218   for (; aIt.More(); aIt.Next()) {
219     const TopoDS_Shape& aSx=aIt.Value();
220     BOPTools::MapShapes(aSx, aMS);
221   }
222   //
223   // 4. Treatment
224   aItM.Initialize(aMS);
225   for (; aItM.More(); aItM.Next()) {
226     const TopoDS_Shape& aSx=aItM.Key();
227     aType=aSx.ShapeType();
228     //
229     // 4.1 .myImagesResult
230     bHasImage=myImages.IsBound(aSx); 
231     //
232     BOPCol_ListOfShape aLSx;
233     if (!bHasImage) {
234       if (myMapShape.Contains(aSx)) {
235         aLSx.Append(aSx);
236         myImagesResult.Add(aSx, aLSx);
237       }
238     }
239     else {
240       const BOPCol_ListOfShape& aLSp=myImages.Find(aSx);
241       aIt.Initialize(aLSp);
242       for (; aIt.More(); aIt.Next()) {
243         const TopoDS_Shape& aSp=aIt.Value();
244         if (myMapShape.Contains(aSp)) {
245           aLSx.Append(aSp);
246         }
247       }
248       myImagesResult.Add(aSx, aLSx);
249     }
250     // <- A
251     //
252     // 4.2 As it was 
253     if (!myHasDeleted) {
254       myHasDeleted=IsDeleted(aSx);
255     }
256     //
257     if (!myHasGenerated || !myHasModified) {
258       if (aType==TopAbs_EDGE   || aType==TopAbs_FACE || 
259           aType==TopAbs_VERTEX || aType==TopAbs_SOLID) {
260         if (bHasImage) {
261           const BOPCol_ListOfShape& aLSp=myImages.Find(aSx);
262           aIt.Initialize(aLSp);
263           for (; aIt.More(); aIt.Next()) {
264             const TopoDS_Shape& aSp=aIt.Value();
265             //
266             if (myMapShape.Contains(aSp)) {
267               bContainsSD=myShapesSD.IsBound(aSp);
268               //
269               if (!myHasGenerated) {
270                 if (bContainsSD) {
271                   myHasGenerated=Standard_True;
272                 }
273               }
274               if (!myHasModified) {
275                 if (!bContainsSD) {
276                   myHasModified=Standard_True;
277                 }
278               }
279             } // if (myMapShape.Contains(aSp))
280           }
281         }
282       } 
283     }
284   }
285   myFlagHistory=Standard_True;
286 }
287
288 // <- A
289     /*
290     BOPCol_ListOfShape aLSx;
291     if (!bHasImage) {
292       if (myMapShape.Contains(aSx)) {
293         aLSx.Append(aSx);
294         myImagesResult.Add(aSx, aLSx);
295       }
296     }
297     else {
298       const BOPCol_ListOfShape& aLSp=myImages.Find(aSx);
299       aIt.Initialize(aLSp);
300       for (; aIt.More(); aIt.Next()) {
301         const TopoDS_Shape& aSp=aIt.Value();
302         if (myMapShape.Contains(aSp)) {
303           aLSx.Append(aSp);
304         }
305       }
306       myImagesResult.Add(aSx, aLSx);
307     }
308     */