0032768: Coding - get rid of unused headers [BopAlgo to BRepBuilderAPI]
[occt.git] / src / BRepAlgo / BRepAlgo_AsDes.cxx
CommitLineData
b311480e 1// Created on: 1995-10-26
2// Created by: Yves FRICAUD
3// Copyright (c) 1995-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 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
973c2be1 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.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
42cf5bc1 17
18#include <BRepAlgo_AsDes.hxx>
19#include <Standard_ConstructionError.hxx>
20#include <Standard_Type.hxx>
21#include <TopoDS_Shape.hxx>
b0fbc579 22#include <TopTools_MapOfOrientedShape.hxx>
7fd59977 23
25e59720 24IMPLEMENT_STANDARD_RTTIEXT(BRepAlgo_AsDes,Standard_Transient)
92efcf78 25
7fd59977 26//=======================================================================
27//function : BRepAlgo_AsDes
28//purpose :
29//=======================================================================
7fd59977 30BRepAlgo_AsDes::BRepAlgo_AsDes()
31{
32}
33
34
35//=======================================================================
36//function : Add
37//purpose :
38//=======================================================================
39
40void BRepAlgo_AsDes::Add(const TopoDS_Shape& S, const TopoDS_Shape& SS)
41{
42 if (!down.IsBound(S)) {
43 TopTools_ListOfShape L;
44 down.Bind(S,L);
45 }
46 down(S).Append(SS);
47
48 if (!up.IsBound(SS)) {
49 TopTools_ListOfShape L;
50 up.Bind(SS,L);
51 }
52 up(SS).Append(S);
53}
54
55
56//=======================================================================
57//function : Add
58//purpose :
59//=======================================================================
60
61void BRepAlgo_AsDes::Add(const TopoDS_Shape& S, const TopTools_ListOfShape& SS)
62{
63 TopTools_ListIteratorOfListOfShape it(SS);
64 for ( ; it.More(); it.Next()) {
65 Add(S,it.Value());
66 }
67}
68
69//=======================================================================
70//function : Clear
71//purpose :
72//=======================================================================
73
74void BRepAlgo_AsDes::Clear()
75{
76 up .Clear();
77 down.Clear();
78}
79
80
81//=======================================================================
82//function : HasAscendant
83//purpose :
84//=======================================================================
85
86Standard_Boolean BRepAlgo_AsDes::HasAscendant(const TopoDS_Shape& S)
87const
88{
89 return up.IsBound(S);
90}
91
92//=======================================================================
93//function : HasDescendant
94//purpose :
95//=======================================================================
96
97Standard_Boolean BRepAlgo_AsDes::HasDescendant(const TopoDS_Shape& S)
98const
99{
100 return down.IsBound(S);
101}
102
103//=======================================================================
104//function : Ascendant
105//purpose :
106//=======================================================================
107
108const TopTools_ListOfShape& BRepAlgo_AsDes::Ascendant(const TopoDS_Shape& S) const
109{
110 if (up.IsBound(S))
111 return up(S);
112 static TopTools_ListOfShape empty;
113 return empty;
114}
115
116
117//=======================================================================
118//function : Descendant
119//purpose :
120//=======================================================================
121
122const TopTools_ListOfShape& BRepAlgo_AsDes::Descendant(const TopoDS_Shape& S) const
123{
124 if (down.IsBound(S))
125 return down(S);
126 static TopTools_ListOfShape empty;
127 return empty;
128}
129
130//=======================================================================
131//function : ChangeDescendant
132//purpose :
133//=======================================================================
134
135TopTools_ListOfShape& BRepAlgo_AsDes::ChangeDescendant(const TopoDS_Shape& S)
136{
137 if (down.IsBound(S))
138 return down.ChangeFind(S);
139 static TopTools_ListOfShape empty;
140 return empty;
141}
142
143//=======================================================================
144//function : ReplaceInList
145//purpose :
146//=======================================================================
147
148static void ReplaceInList(const TopoDS_Shape& OldS,
149 const TopoDS_Shape& NewS,
150 TopTools_ListOfShape& L)
151{
b0fbc579 152 TopTools_MapOfOrientedShape aMS;
7fd59977 153 TopTools_ListIteratorOfListOfShape it(L);
b0fbc579 154 for (; it.More(); it.Next()) {
155 aMS.Add(it.Value());
156 }
157 it.Initialize(L);
7fd59977 158 while(it.More()) {
159 if (it.Value().IsSame(OldS)) {
160 TopAbs_Orientation O = it.Value().Orientation();
b0fbc579 161 if (aMS.Add(NewS.Oriented(O))) {
162 L.InsertBefore(NewS.Oriented(O),it);
163 }
7fd59977 164 L.Remove(it);
165 }
166 else it.Next();
167 }
168}
169//=======================================================================
170//function : RemoveInList
171//purpose :
172//=======================================================================
173
174static void RemoveInList(const TopoDS_Shape& S,
175 TopTools_ListOfShape& L)
176{
177 TopTools_ListIteratorOfListOfShape it(L);
178 while(it.More()) {
179 if (it.Value().IsSame(S)) {
180 L.Remove(it);
181 break;
182 }
183 it.Next();
184 }
185}
186
187//=======================================================================
188//function : HasCommonDescendant
189//purpose :
190//=======================================================================
191
192Standard_Boolean BRepAlgo_AsDes::HasCommonDescendant(const TopoDS_Shape& S1,
193 const TopoDS_Shape& S2,
194 TopTools_ListOfShape& LC)
195const
196{
197 LC.Clear();
198 if (HasDescendant (S1) && HasDescendant (S2)) {
199 TopTools_ListIteratorOfListOfShape it1(Descendant(S1));
200 for (; it1.More(); it1.Next()) {
201 const TopoDS_Shape& DS1 = it1.Value();
202 TopTools_ListIteratorOfListOfShape it2(Ascendant(DS1));
203 for (; it2.More(); it2.Next()) {
204 const TopoDS_Shape& ADS1 = it2.Value();
205 if (ADS1.IsSame(S2)) {
206 LC.Append(DS1);
207 }
208 }
209 }
210 }
211 return (!LC.IsEmpty());
212}
213
214//=======================================================================
215//function : BackReplace
216//purpose :
217//=======================================================================
218
219void BRepAlgo_AsDes::BackReplace(const TopoDS_Shape& OldS,
220 const TopoDS_Shape& NewS,
221 const TopTools_ListOfShape& L,
222 const Standard_Boolean InUp)
223{
224 TopTools_ListIteratorOfListOfShape it(L);
225 for ( ; it.More(); it.Next()) {
226 const TopoDS_Shape& S = it.Value();
227 if (InUp) {
228 if (up.IsBound(S)) {
229 ReplaceInList(OldS,NewS,up.ChangeFind(S));
230 }
231 }
232 else {
233 if (down.IsBound(S)) {
234 ReplaceInList(OldS,NewS,down.ChangeFind(S));
235 }
236 }
237 }
238}
239
240//=======================================================================
241//function : Replace
242//purpose :
243//=======================================================================
7fd59977 244void BRepAlgo_AsDes::Replace(const TopoDS_Shape& OldS,
b0fbc579 245 const TopoDS_Shape& NewS)
7fd59977 246{
b0fbc579 247 for (Standard_Integer i = 0; i < 2; ++i) {
248 TopTools_DataMapOfShapeListOfShape& aMap = !i ? up : down;
249 TopTools_ListOfShape* pLSOld = aMap.ChangeSeek(OldS);
250 if (!pLSOld) {
251 continue;
7fd59977 252 }
b0fbc579 253 //
254 Standard_Boolean InUp = !i ? Standard_False : Standard_True;
255 BackReplace(OldS, NewS, *pLSOld, InUp);
256 //
257 TopTools_ListOfShape* pLSNew = aMap.ChangeSeek(NewS);
258 if (!pLSNew) {
259 // filter the list
260 TopTools_MapOfOrientedShape aMS;
261 TopTools_ListIteratorOfListOfShape aIt(*pLSOld);
262 for (; aIt.More(); ) {
263 if (!aMS.Add(aIt.Value())) {
264 pLSOld->Remove(aIt);
265 }
266 else {
267 aIt.Next();
268 }
269 }
270 aMap.Bind(NewS, *pLSOld);
7fd59977 271 }
272 else {
b0fbc579 273 // avoid duplicates
274 TopTools_MapOfOrientedShape aMS;
275 TopTools_ListIteratorOfListOfShape aIt(*pLSNew);
276 for (; aIt.More(); aIt.Next()) {
277 aMS.Add(aIt.Value());
278 }
279 //
280 aIt.Initialize(*pLSOld);
281 for (; aIt.More(); aIt.Next()) {
282 const TopoDS_Shape& aS = aIt.Value();
283 if (aMS.Add(aS)) {
284 pLSNew->Append(aS);
285 }
286 }
7fd59977 287 }
b0fbc579 288 //
289 aMap.UnBind(OldS);
7fd59977 290 }
291}
292
293//=======================================================================
294//function : Remove
295//purpose :
296//=======================================================================
297
298void BRepAlgo_AsDes::Remove(const TopoDS_Shape& SS)
299{
300 if (down.IsBound(SS)) {
9775fa61 301 throw Standard_ConstructionError(" BRepAlgo_AsDes::Remove");
7fd59977 302 }
303 if (!up.IsBound(SS)) {
9775fa61 304 throw Standard_ConstructionError(" BRepAlgo_AsDes::Remove");
7fd59977 305 }
306 TopTools_ListIteratorOfListOfShape it(up(SS));
307 for (; it.More(); it.Next()) {
308 RemoveInList(SS,down.ChangeFind((it.Value())));
309 }
310 up.UnBind(SS);
311}