b311480e |
1 | // Created on: 2000-12-25 |
2 | // Created by: Igor FEOKTISTOV |
3 | // Copyright (c) 2000-2012 OPEN CASCADE SAS |
4 | // |
5 | // The content of this file is subject to the Open CASCADE Technology Public |
6 | // License Version 6.5 (the "License"). You may not use the content of this file |
7 | // except in compliance with the License. Please obtain a copy of the License |
8 | // at http://www.opencascade.org and read it completely before using this file. |
9 | // |
10 | // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its |
11 | // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France. |
12 | // |
13 | // The Original Code and all software distributed under the License is |
14 | // distributed on an "AS IS" basis, without warranty of any kind, and the |
15 | // Initial Developer hereby disclaims all such warranties, including without |
16 | // limitation, any warranties of merchantability, fitness for a particular |
17 | // purpose or non-infringement. Please see the License for the specific terms |
18 | // and conditions governing the rights and limitations under the License. |
7fd59977 |
19 | |
20 | #include <QANewModTopOpe_Intersection.ixx> |
21 | #include <TopoDS.hxx> |
22 | #include <TopTools_ListIteratorOfListOfShape.hxx> |
23 | #include <BRep_Builder.hxx> |
24 | #include <BRep_Tool.hxx> |
25 | #include <TopExp_Explorer.hxx> |
26 | #include <BRepExtrema_SupportType.hxx> |
27 | #include <BRepExtrema_DistShapeShape.hxx> |
28 | #include <BRepBuilderAPI_MakeVertex.hxx> |
29 | #include <TopoDS_Vertex.hxx> |
30 | #include <gp_Pnt.hxx> |
31 | #include <BRepAlgo_EdgeConnector.hxx> |
32 | #include <TopTools_ListOfShape.hxx> |
33 | #include <TopoDS_Iterator.hxx> |
34 | #include <BRepTools.hxx> |
7fd59977 |
35 | #include <TopTools_MapOfShape.hxx> |
36 | #include <TopoDS_Edge.hxx> |
37 | #include <QANewModTopOpe_Tools.hxx> |
38 | |
4e57c75e |
39 | #include <BOPAlgo_PaveFiller.hxx> |
40 | |
7fd59977 |
41 | |
42 | static Standard_Boolean NoFaces(const TopoDS_Shape& S1, const TopoDS_Shape& S2) |
43 | { |
44 | TopExp_Explorer Ex; |
45 | Ex.Init(S1,TopAbs_FACE); |
46 | Standard_Boolean f1 = Ex.More(); |
47 | Ex.Init(S2,TopAbs_FACE); |
48 | Standard_Boolean f2 = Ex.More(); |
49 | |
50 | return !f1&&!f2; |
51 | } |
52 | |
53 | QANewModTopOpe_Intersection::QANewModTopOpe_Intersection( const TopoDS_Shape& theObject1, |
54 | const TopoDS_Shape& theObject2 ) |
4e57c75e |
55 | : BRepAlgoAPI_BooleanOperation( theObject1, theObject2, BOPAlgo_SECTION) |
7fd59977 |
56 | { |
57 | myMapGener.Clear(); |
58 | |
59 | if(NoFaces(myS1, myS2)) { |
60 | |
61 | BRep_Builder BB; |
62 | BB.MakeCompound(TopoDS::Compound(myShape)); |
63 | |
64 | BRepExtrema_DistShapeShape DSS(myS1, myS2); |
65 | |
66 | |
67 | if(DSS.IsDone() && DSS.NbSolution() > 0) { |
68 | Standard_Integer nbs = DSS.NbSolution(); |
69 | Standard_Real mindist = DSS.Value(); |
70 | Standard_Real maxtol = 0., tol; |
71 | TopExp_Explorer ExV; |
72 | for(ExV.Init(myS1,TopAbs_VERTEX); ExV.More(); ExV.Next()) { |
73 | TopoDS_Vertex Vertex=TopoDS::Vertex(ExV.Current()); |
74 | tol=BRep_Tool::Tolerance(Vertex); |
75 | if(tol > maxtol) maxtol = tol; |
76 | } |
77 | |
78 | for(ExV.Init(myS2,TopAbs_VERTEX); ExV.More(); ExV.Next()) { |
79 | TopoDS_Vertex Vertex=TopoDS::Vertex(ExV.Current()); |
80 | tol=BRep_Tool::Tolerance(Vertex); |
81 | if(tol > maxtol) maxtol = tol; |
82 | } |
83 | |
84 | if(mindist <= maxtol) { |
85 | tol = maxtol*maxtol; |
86 | Standard_Integer i; |
87 | |
88 | for(i = 1; i <= nbs; i++) { |
89 | gp_Pnt p1 = DSS.PointOnShape1(i); |
90 | gp_Pnt p2 = DSS.PointOnShape2(i); |
91 | if(p1.SquareDistance(p2) > tol) continue; |
92 | BRepExtrema_SupportType aSupTyp = DSS. SupportTypeShape1(i); |
93 | if(aSupTyp == BRepExtrema_IsVertex) { |
94 | BB.Add(myShape, DSS.SupportOnShape1(i)); |
95 | } |
96 | else { |
97 | aSupTyp = DSS. SupportTypeShape2(i); |
98 | if(aSupTyp == BRepExtrema_IsVertex) { |
99 | BB.Add(myShape, DSS.SupportOnShape2(i)); |
100 | } |
101 | else { |
102 | gp_Pnt p(.5*(p1.XYZ()+p2.XYZ())); |
103 | BRepBuilderAPI_MakeVertex mkV(p); |
104 | BB.Add(myShape, mkV.Vertex()); |
105 | |
106 | if (!myMapGener.IsBound(DSS. SupportOnShape1(i))) { |
107 | // for Mandrake-10 - mkv,02.06.06 - myMapGener.Bind(DSS.SupportOnShape1(i), TopTools_ListOfShape()); |
108 | TopTools_ListOfShape aListOfShape1; |
109 | myMapGener.Bind(DSS.SupportOnShape1(i), aListOfShape1); |
110 | } |
111 | myMapGener(DSS.SupportOnShape1(i)).Append(mkV.Vertex()); |
112 | |
113 | if (!myMapGener.IsBound(DSS.SupportOnShape2(i))) { |
114 | // for Mandrake-10 - mkv,02.06.06 - myMapGener.Bind(DSS.SupportOnShape2(i), TopTools_ListOfShape()); |
115 | TopTools_ListOfShape aListOfShape2; |
116 | myMapGener.Bind(DSS.SupportOnShape2(i), aListOfShape2); |
117 | } |
118 | myMapGener(DSS.SupportOnShape2(i)).Append(mkV.Vertex()); |
119 | |
120 | } |
121 | |
122 | } |
123 | } |
124 | } |
125 | } |
126 | |
127 | Done(); |
128 | return; |
129 | } |
130 | |
131 | Standard_Boolean bIsNewFiller = PrepareFiller(); |
132 | // |
133 | if (myErrorStatus!=1) { |
134 | // there were errors during the preparation |
135 | return; |
136 | } |
137 | // |
138 | if (bIsNewFiller) { |
139 | //Prepare the DS |
4e57c75e |
140 | myDSFiller->Perform(); |
141 | |
7fd59977 |
142 | } |
143 | |
144 | Build(); |
145 | |
146 | Standard_Boolean bcw = BuilderCanWork(); |
147 | |
148 | if ( ! bcw ) return; |
149 | |
150 | BRep_Builder BB; |
151 | BB.MakeCompound(TopoDS::Compound(myShape)); |
152 | TopTools_ListIteratorOfListOfShape itloe(SectionEdges()); |
153 | |
154 | if(itloe.More()) { |
155 | |
156 | for(; itloe.More(); itloe.Next()) BB.Add(myShape,itloe.Value()); |
157 | |
158 | // try to make wire from set of edges |
159 | TopTools_ListOfShape LOEdge; |
160 | TopTools_ListOfShape LOSEdge; |
161 | Standard_Integer nbe = 0; |
162 | TopoDS_Shape aux; |
163 | TopExp_Explorer Ex; |
164 | BB.MakeCompound(TopoDS::Compound(aux)); |
165 | for(Ex.Init(myShape,TopAbs_EDGE); Ex.More(); Ex.Next()) { |
166 | LOEdge.Append(Ex.Current()); |
167 | LOSEdge.Append(Ex.Current()); |
168 | nbe++; |
169 | } |
170 | BRepAlgo_EdgeConnector EC; |
171 | TopoDS_Shape se = LOEdge.First(); |
172 | EC.Add(LOEdge); |
173 | EC.AddStart(LOSEdge); |
174 | const TopTools_ListOfShape& LOWire = EC.MakeBlock(); |
175 | TopTools_ListIteratorOfListOfShape its; |
176 | for(its.Initialize(LOWire);its.More();its.Next()) { |
177 | BB.Add(aux,its.Value()); |
178 | } |
179 | for(Ex.Init(aux,TopAbs_EDGE); Ex.More(); Ex.Next()) { |
180 | nbe--; |
181 | } |
182 | if(nbe == 0) myShape = aux; |
183 | |
184 | } |
185 | else { |
186 | // check if there are new vertices - IFV |
187 | |
188 | Standard_Integer nbp = QANewModTopOpe_Tools::NbPoints(myDSFiller); |
189 | |
190 | if(nbp > 0) { |
191 | |
192 | TopoDS_Shape aux1, aux2; |
193 | BB.MakeCompound(TopoDS::Compound(aux1)); |
194 | BB.MakeCompound(TopoDS::Compound(aux2)); |
195 | |
196 | BB.Add(aux1, myS1); |
197 | BB.Add(aux1, myS2); |
198 | |
199 | Standard_Integer i; |
200 | Standard_Real maxtol = 0., tol; |
201 | |
202 | for(i = 1; i <= nbp; i++) { |
203 | TopoDS_Vertex aVertex= |
204 | TopoDS::Vertex(QANewModTopOpe_Tools::NewVertex(myDSFiller, i)); |
205 | |
206 | tol=BRep_Tool::Tolerance(aVertex); |
207 | if(tol > maxtol) maxtol = tol; |
208 | BB.Add(aux2, aVertex); |
209 | } |
210 | |
211 | BRepExtrema_DistShapeShape DSS(aux1, aux2); |
212 | if(DSS.IsDone() && DSS.NbSolution() > 0) { |
213 | Standard_Integer nbs = DSS.NbSolution(); |
214 | Standard_Real mindist = DSS.Value(); |
215 | if(mindist <= maxtol) { |
216 | tol = maxtol*maxtol; |
217 | |
218 | TopTools_MapOfShape aMap; |
219 | |
220 | for(i = 1; i <= nbs; i++) { |
221 | gp_Pnt p1 = DSS.PointOnShape1(i); |
222 | gp_Pnt p2 = DSS.PointOnShape2(i); |
223 | if(p1.SquareDistance(p2) > tol) continue; |
224 | BRepExtrema_SupportType aSupTyp = DSS.SupportTypeShape1(i); |
225 | TopoDS_Vertex aVertex; |
226 | if(aSupTyp == BRepExtrema_IsVertex) { |
227 | aVertex = TopoDS::Vertex(DSS.SupportOnShape1(i)); |
228 | TopoDS_Iterator anIter(myShape); |
229 | Standard_Boolean anIsNew = Standard_True; |
230 | for(; anIter.More(); anIter.Next()) { |
231 | if(BRepTools::Compare(aVertex, TopoDS::Vertex(anIter.Value()))) { |
232 | anIsNew = Standard_False; |
233 | break; |
234 | } |
235 | } |
236 | if(anIsNew) { |
237 | BB.Add(myShape, aVertex); |
238 | } |
239 | } |
240 | else { |
241 | aVertex = TopoDS::Vertex(DSS.SupportOnShape2(i)); |
242 | if(aMap.Add(aVertex)) BB.Add(myShape, aVertex); |
243 | if (!myMapGener.IsBound(DSS.SupportOnShape1(i))) { |
244 | // for Mandrake-10 - mkv,02.06.06 - myMapGener.Bind(DSS.SupportOnShape1(i), TopTools_ListOfShape()); |
245 | TopTools_ListOfShape aListOfShape3; |
246 | myMapGener.Bind(DSS.SupportOnShape1(i), aListOfShape3); |
247 | } |
248 | myMapGener(DSS.SupportOnShape1(i)).Append(aVertex); |
249 | |
250 | } |
251 | } // end for |
252 | } |
253 | } |
254 | } |
255 | } |
256 | |
257 | |
258 | Done(); |
259 | |
260 | } |
261 | |
262 | //======================================================================= |
263 | //function : Generated |
264 | //purpose : gives list of shape generated from shape S |
265 | //======================================================================= |
266 | const TopTools_ListOfShape& QANewModTopOpe_Intersection::Generated(const TopoDS_Shape& theS) |
267 | { |
268 | myGenerated.Clear(); |
269 | |
270 | if(theS.ShapeType() == TopAbs_FACE || theS.ShapeType() == TopAbs_EDGE) { |
271 | if (BRepAlgoAPI_BooleanOperation::HasGenerated()) { |
272 | const TopTools_ListOfShape &aLOfShape = |
273 | BRepAlgoAPI_BooleanOperation::Generated(theS); |
274 | TopTools_ListIteratorOfListOfShape anIter(aLOfShape); |
275 | |
276 | for (; anIter.More(); anIter.Next()) { |
277 | const TopoDS_Shape &aGenShape = anIter.Value(); |
278 | |
279 | if (aGenShape.ShapeType() == TopAbs_VERTEX) |
280 | continue; |
281 | |
282 | myGenerated.Append(aGenShape); |
283 | } |
284 | } |
285 | |
286 | if(myMapGener.IsBound(theS)) { |
287 | TopTools_ListIteratorOfListOfShape anIter(myMapGener(theS)); |
288 | |
289 | for(; anIter.More(); anIter.Next()) |
290 | myGenerated.Append(anIter.Value()); |
291 | } |
292 | } |
293 | |
294 | return myGenerated; |
295 | |
296 | } |
297 | |
298 | //======================================================================= |
299 | //function : HasGenerated |
300 | //purpose : |
301 | //======================================================================= |
302 | Standard_Boolean QANewModTopOpe_Intersection::HasGenerated() const |
303 | { |
304 | Standard_Boolean aHasGenerated = Standard_False; |
305 | |
306 | if (BRepAlgoAPI_BooleanOperation::HasGenerated()) { |
307 | aHasGenerated = Standard_True; |
308 | } |
309 | else { |
310 | aHasGenerated = !myMapGener.IsEmpty(); |
311 | } |
312 | return aHasGenerated; |
313 | } |
314 | |
315 | |
316 | //======================================================================= |
317 | //function : IsDeleted |
318 | //purpose : |
319 | //======================================================================= |
320 | Standard_Boolean QANewModTopOpe_Intersection::IsDeleted(const TopoDS_Shape& aS) |
321 | { |
322 | if(BRepAlgoAPI_BooleanOperation::IsDeleted(aS)) |
323 | return Standard_True; |
324 | |
325 | if(myMapGener.IsBound(aS)) |
326 | return Standard_False; |
327 | return Standard_True; |
328 | } |
329 | |
330 | //======================================================================= |
331 | //function : HasDeleted |
332 | //purpose : |
333 | //======================================================================= |
334 | Standard_Boolean QANewModTopOpe_Intersection::HasDeleted() const |
335 | { |
7fd59977 |
336 | TopExp_Explorer anExp; |
337 | |
338 | for(Standard_Integer argit = 0; argit < 2; argit++) { |
339 | Standard_Boolean bTillVertex = Standard_True; |
340 | |
341 | if(argit == 0) |
342 | anExp.Init(myS1, TopAbs_FACE); |
343 | else |
344 | anExp.Init(myS2, TopAbs_FACE); |
345 | |
346 | if(anExp.More()) |
347 | bTillVertex = Standard_False; |
348 | Standard_Integer nb = (bTillVertex) ? 3 : 2; |
349 | |
350 | for(Standard_Integer i = 0; i < nb; i++) { |
351 | TopAbs_ShapeEnum aType = (i == 0) ? TopAbs_FACE : ((i == 1) ? TopAbs_EDGE : TopAbs_VERTEX); |
352 | |
353 | if(argit == 0) |
354 | anExp.Init(myS1, aType); |
355 | else |
356 | anExp.Init(myS2, aType); |
357 | |
358 | for(; anExp.More(); anExp.Next()) { |
359 | if(!myMapGener.IsBound(anExp.Current())) { |
360 | return Standard_True; |
361 | } |
362 | } |
363 | } |
364 | } |
365 | return Standard_False; |
366 | } |