Test for 0022778: Bug in BRepMesh
[occt.git] / src / BRepBuilderAPI / BRepBuilderAPI_Collect.cxx
1 // Created on: 1996-04-09
2 // Created by: Yves FRICAUD
3 // Copyright (c) 1996-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21
22
23 #include <BRepBuilderAPI_Collect.ixx>
24
25 #include <TopoDS.hxx>
26 #include <TopExp_Explorer.hxx>
27
28 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
29 #include <TopTools_ListOfShape.hxx>
30 #include <TopTools_ListIteratorOfListOfShape.hxx>
31 #include <TopTools_DataMapOfShapeShape.hxx>
32 #include <TopTools_MapOfShape.hxx>
33
34 #ifdef DEB
35 #include <stdio.h>
36 Standard_Boolean Affich;
37 #endif
38
39 #ifdef DRAW
40 #include <DBRep.hxx>
41 #endif
42 //=======================================================================
43 //function : BuilBack
44 //purpose  : 
45 //=======================================================================
46
47 static void BuildBack (const TopTools_DataMapOfShapeListOfShape& M1,
48                              TopTools_DataMapOfShapeShape&       BM1)
49 {
50   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape it(M1);
51   for (; it.More(); it.Next()) {
52     const TopoDS_Shape& KS = it.Key();
53     TopTools_ListIteratorOfListOfShape itl(it.Value());
54     for ( ; itl.More(); itl.Next()) {
55       const TopoDS_Shape& VS = itl.Value();
56       BM1.Bind(VS,KS);
57     }
58   }
59 }
60
61 //=======================================================================
62 //function : Replace
63 //purpose  : 
64 //=======================================================================
65
66 static void  Replace (      TopTools_ListOfShape& L,
67                       const TopoDS_Shape          Old,
68                       const TopTools_ListOfShape& New)
69 {
70   //-----------------------------------
71   // Suppression de Old dans la liste.
72   //-----------------------------------
73   TopTools_ListIteratorOfListOfShape it(L);
74   while (it.More()) {
75     if (it.Value().IsSame(Old)) {
76       L.Remove(it);
77       break;
78     }
79     if (it.More()) it.Next();
80   }
81   //---------------------------
82   // Ajout de New a L.
83   //---------------------------
84   TopTools_ListOfShape copNew;
85   copNew = New;
86   L.Append(copNew);
87 }
88
89
90 //=======================================================================
91 //function : StoreImage
92 //purpose  : 
93 //=======================================================================
94
95 static void StoreImage (      TopTools_DataMapOfShapeListOfShape& MG,
96                         const TopoDS_Shape&                       S,
97                         const TopTools_DataMapOfShapeShape&       MGBack,
98                         const TopTools_ListOfShape&               LI)
99 {    
100   if (!LI.IsEmpty()) {
101     if (MGBack.IsBound(S)) {
102
103       Replace (MG.ChangeFind(MGBack(S)),S,LI);
104     }
105     else {
106       if (!MG.IsBound(S)) {
107         TopTools_ListOfShape empty;
108         MG.Bind(S,empty);
109       }
110       // Dans tous les cas on copie la liste pour eviter les pb de
111       // const& dans BRepBuilderAPI.
112       TopTools_ListIteratorOfListOfShape it;
113       for (it.Initialize(LI); it.More(); it.Next()) {
114         const TopoDS_Shape& SS = it.Value();
115         MG(S).Append(SS);
116       }
117     }
118   }
119 }
120
121 //=======================================================================
122 //function : UpdateGen
123 //purpose  : 
124 //=======================================================================
125
126 static void Update (   TopTools_DataMapOfShapeListOfShape& Mod,
127                        TopTools_DataMapOfShapeListOfShape& Gen,
128                        const TopTools_DataMapOfShapeShape& ModBack,
129                        const TopTools_DataMapOfShapeShape& GenBack,
130                        const TopoDS_Shape&                 SI,
131                        BRepBuilderAPI_MakeShape&                  MKS,
132                        const TopAbs_ShapeEnum              ShapeType)
133 {  
134   
135   TopTools_MapOfShape DejaVu;
136   TopExp_Explorer     exp;
137   
138   for (exp.Init(SI,ShapeType); exp.More(); exp.Next()) {
139     const TopoDS_Shape& S = exp.Current();
140     if (!DejaVu.Add(S))  continue;
141
142     //---------------------------------------
143     // Recuperation de l image de S par MKS.
144     //---------------------------------------
145     const TopTools_ListOfShape& LIM = MKS.Modified(S);
146     if (!LIM.IsEmpty()) {
147       if (GenBack.IsBound(S)) {
148         // Modif de generation => generation du shape initial
149         StoreImage (Gen,S,GenBack,LIM);
150       }
151       else {
152         StoreImage (Mod,S,ModBack,LIM);
153       }
154     }
155     const TopTools_ListOfShape& LIG = MKS.Generated(S);
156     if (!LIG.IsEmpty()) {
157       if (ModBack.IsBound(S)) {
158         // Generation de modif  => generation du shape initial
159         TopoDS_Shape IS = ModBack(S);
160         StoreImage (Gen,IS,GenBack,LIG);
161       }
162       else {
163         StoreImage (Gen,S,GenBack,LIG);
164       }
165     }
166   }
167 }
168 #ifdef DEB
169 //=======================================================================
170 //function : DEBControl
171 //purpose  : 
172 //=======================================================================
173
174 static void DEBControl (const TopTools_DataMapOfShapeListOfShape& MG)
175 {
176   char name[100];
177   Standard_Integer IK = 0;
178   
179   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape it(MG);
180   for (; it.More(); it.Next()) {
181     const TopoDS_Shape& OS = it.Key();
182     sprintf(name, "SK_%d",++IK);
183 #ifdef DRAW
184     DBRep::Set(name,OS);
185 #endif
186     TopTools_ListIteratorOfListOfShape itl(MG(OS));
187     Standard_Integer IV = 1;
188     for (; itl.More(); itl.Next()) {
189       const TopoDS_Shape& NS = itl.Value();
190       sprintf(name, "SV_%d_%d",IK,IV++);
191 #ifdef DRAW
192       DBRep::Set(name,NS);
193 #endif
194     }
195   }
196 }
197 #endif
198 //=======================================================================
199 //function : BRepBuilderAPI_Collect
200 //purpose  : 
201 //=======================================================================
202
203 BRepBuilderAPI_Collect::BRepBuilderAPI_Collect()
204 {}
205
206 //=======================================================================
207 //function : Add
208 //purpose  : 
209 //=======================================================================
210
211 void BRepBuilderAPI_Collect::Add (const TopoDS_Shape& SI, 
212                            BRepBuilderAPI_MakeShape&  MKS)
213
214 {
215   TopTools_DataMapOfShapeShape GenBack;
216   TopTools_DataMapOfShapeShape ModBack;
217   BuildBack (myGen, GenBack);   // Vraiment pas optimum a Revoir
218   BuildBack (myMod, ModBack);
219   
220   Update (myMod,myGen,ModBack,GenBack,SI,MKS,TopAbs_FACE);
221   Update (myMod,myGen,ModBack,GenBack,SI,MKS,TopAbs_EDGE);
222   Update (myMod,myGen,ModBack,GenBack,SI,MKS,TopAbs_VERTEX);
223
224 #ifdef DEB
225   if (Affich) {
226     DEBControl (myGen);
227     DEBControl (myMod);
228   }
229 #endif
230 }
231 //=======================================================================
232 //function : Add
233 //purpose  : 
234 //=======================================================================
235
236 void BRepBuilderAPI_Collect::AddGenerated (const TopoDS_Shape& S,
237                                     const TopoDS_Shape& NS) 
238 {  
239   TopTools_DataMapOfShapeShape GenBack;
240   TopTools_DataMapOfShapeShape ModBack;
241   BuildBack (myGen, GenBack);
242   BuildBack (myMod, ModBack);
243
244   TopTools_ListOfShape LIG;
245   LIG.Append(NS);
246   if (ModBack.IsBound(S)) {
247     // Generation de modif  => generation du shape initial
248     TopoDS_Shape IS = ModBack(S);
249     StoreImage (myGen,IS,GenBack,LIG);
250   }
251   else {
252     StoreImage (myGen,S,GenBack,LIG);
253   }
254 }
255
256 //=======================================================================
257 //function : Add
258 //purpose  : 
259 //=======================================================================
260
261 void BRepBuilderAPI_Collect::AddModif  (const TopoDS_Shape& S, 
262                                  const TopoDS_Shape& NS)
263
264 {  
265   TopTools_DataMapOfShapeShape GenBack;
266   TopTools_DataMapOfShapeShape ModBack;
267   BuildBack (myGen, GenBack);
268   BuildBack (myMod, ModBack);
269   
270   TopTools_ListOfShape LIG;
271   LIG.Append(NS);
272   if (GenBack.IsBound(S)) {
273     // Modif de generation => generation du shape initial
274     StoreImage (myGen,S,GenBack,LIG);
275   }
276   else {
277     StoreImage (myMod,S,ModBack,LIG);
278   }
279 }
280
281
282 //=======================================================================
283 //function : Filter
284 //purpose  : 
285 //=======================================================================
286
287 static void FilterByShape(TopTools_DataMapOfShapeListOfShape& MG,
288                           const TopoDS_Shape&                 SF)
289 {
290   TopTools_MapOfShape MSF;
291   TopExp_Explorer     exp;
292   Standard_Boolean    YaEdge   = Standard_False;
293   Standard_Boolean    YaVertex = Standard_False;
294   for (exp.Init(SF,TopAbs_FACE)  ; exp.More(); exp.Next()) MSF.Add(exp.Current());
295
296   //-------------------------------------------------------------
297   // Suppression de toutes les images qui ne sont pas dans MSF.
298   //-------------------------------------------------------------
299   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape it(MG);
300   for (; it.More(); it.Next()) {
301     const TopoDS_Shape&   OS  = it.Key();
302     TopTools_ListOfShape& LNS = MG.ChangeFind(OS); 
303     TopTools_ListIteratorOfListOfShape itl(LNS);
304     while (itl.More()) {
305       const TopoDS_Shape& NS = itl.Value();
306       //-------------------------------------------------------------------
307       // Images contiennet des edges => ajout des edges resultat dans MSF.
308       //-------------------------------------------------------------------
309       if (!YaEdge && NS.ShapeType() == TopAbs_EDGE) {  
310         for (exp.Init(SF,TopAbs_EDGE)  ; exp.More(); exp.Next()) { 
311           MSF.Add(exp.Current());
312         }
313         YaEdge = Standard_True;
314       }
315       //-------------------------------------------------------------------
316       // Images contiennet des vertex => ajout des vertex resultat dans MSF.
317       //-------------------------------------------------------------------
318       if (!YaVertex && NS.ShapeType() == TopAbs_VERTEX) {  
319         for (exp.Init(SF,TopAbs_VERTEX)  ; exp.More(); exp.Next()) { 
320           MSF.Add(exp.Current());
321         }
322         YaVertex = Standard_True;
323       }
324       //---------------------------------------
325       // Si pas dans MSF suprresion de l image.
326       //---------------------------------------
327       if (!MSF.Contains(NS)) {
328         LNS.Remove(itl);
329       }
330       else if (itl.More()) itl.Next();
331     }
332   }
333 #ifdef DEB
334   if (Affich) {
335     DEBControl (MG);
336   }
337 #endif
338
339 }
340
341 //=======================================================================
342 //function : Modification
343 //purpose  : 
344 //=======================================================================
345
346 const TopTools_DataMapOfShapeListOfShape&   BRepBuilderAPI_Collect::Modification() const
347 {
348   return myMod;
349 }
350
351 //=======================================================================
352 //function : Generation
353 //purpose  : 
354 //=======================================================================
355
356 const TopTools_DataMapOfShapeListOfShape&   BRepBuilderAPI_Collect::Generated() const
357 {
358   return myGen;
359 }
360
361 //=======================================================================
362 //function : Filter
363 //purpose  : 
364 //=======================================================================
365
366 void BRepBuilderAPI_Collect::Filter(const TopoDS_Shape& SF)
367 {
368   FilterByShape (myGen,SF);
369   FilterByShape (myMod,SF);
370 }
371
372
373
374
375
376