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