7fd59977 |
1 | // File: QANewModTopOpe_Intersection.cxx |
2 | // Created: Mon Dec 25 11:14:00 2000 |
3 | // Author: Igor FEOKTISTOV <ifv@nnov.matra-dtv.fr> |
4 | // Copyright: SAMTECH S.A. 2000 |
5 | |
7fd59977 |
6 | // Lastly modified by : |
7 | // +---------------------------------------------------------------------------+ |
8 | // ! ifv ! Creation !25-12-2000! 3.0-00-2! |
9 | // ! skv ! Adaptation to OCC version 5.0 ! 6-05-2003! 3.0-00-2! |
10 | // ! mkk ! History correction !19-05-2003! 3.0-00-2! |
11 | // +---------------------------------------------------------------------------+ |
12 | |
13 | |
14 | #include <QANewModTopOpe_Intersection.ixx> |
15 | #include <TopoDS.hxx> |
16 | #include <TopTools_ListIteratorOfListOfShape.hxx> |
17 | #include <BRep_Builder.hxx> |
18 | #include <BRep_Tool.hxx> |
19 | #include <TopExp_Explorer.hxx> |
20 | #include <BRepExtrema_SupportType.hxx> |
21 | #include <BRepExtrema_DistShapeShape.hxx> |
22 | #include <BRepBuilderAPI_MakeVertex.hxx> |
23 | #include <TopoDS_Vertex.hxx> |
24 | #include <gp_Pnt.hxx> |
25 | #include <BRepAlgo_EdgeConnector.hxx> |
26 | #include <TopTools_ListOfShape.hxx> |
27 | #include <TopoDS_Iterator.hxx> |
28 | #include <BRepTools.hxx> |
29 | #include <BOPTools_SSIntersectionAttribute.hxx> |
30 | #include <BOPTools_DSFiller.hxx> |
31 | #include <TopTools_MapOfShape.hxx> |
32 | #include <TopoDS_Edge.hxx> |
33 | #include <QANewModTopOpe_Tools.hxx> |
34 | |
35 | |
36 | static Standard_Boolean NoFaces(const TopoDS_Shape& S1, const TopoDS_Shape& S2) |
37 | { |
38 | TopExp_Explorer Ex; |
39 | Ex.Init(S1,TopAbs_FACE); |
40 | Standard_Boolean f1 = Ex.More(); |
41 | Ex.Init(S2,TopAbs_FACE); |
42 | Standard_Boolean f2 = Ex.More(); |
43 | |
44 | return !f1&&!f2; |
45 | } |
46 | |
47 | QANewModTopOpe_Intersection::QANewModTopOpe_Intersection( const TopoDS_Shape& theObject1, |
48 | const TopoDS_Shape& theObject2 ) |
49 | : BRepAlgoAPI_BooleanOperation( theObject1, theObject2, BOP_SECTION) |
50 | { |
51 | myMapGener.Clear(); |
52 | |
53 | if(NoFaces(myS1, myS2)) { |
54 | |
55 | BRep_Builder BB; |
56 | BB.MakeCompound(TopoDS::Compound(myShape)); |
57 | |
58 | BRepExtrema_DistShapeShape DSS(myS1, myS2); |
59 | |
60 | |
61 | if(DSS.IsDone() && DSS.NbSolution() > 0) { |
62 | Standard_Integer nbs = DSS.NbSolution(); |
63 | Standard_Real mindist = DSS.Value(); |
64 | Standard_Real maxtol = 0., tol; |
65 | TopExp_Explorer ExV; |
66 | for(ExV.Init(myS1,TopAbs_VERTEX); ExV.More(); ExV.Next()) { |
67 | TopoDS_Vertex Vertex=TopoDS::Vertex(ExV.Current()); |
68 | tol=BRep_Tool::Tolerance(Vertex); |
69 | if(tol > maxtol) maxtol = tol; |
70 | } |
71 | |
72 | for(ExV.Init(myS2,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 | if(mindist <= maxtol) { |
79 | tol = maxtol*maxtol; |
80 | Standard_Integer i; |
81 | |
82 | for(i = 1; i <= nbs; i++) { |
83 | gp_Pnt p1 = DSS.PointOnShape1(i); |
84 | gp_Pnt p2 = DSS.PointOnShape2(i); |
85 | if(p1.SquareDistance(p2) > tol) continue; |
86 | BRepExtrema_SupportType aSupTyp = DSS. SupportTypeShape1(i); |
87 | if(aSupTyp == BRepExtrema_IsVertex) { |
88 | BB.Add(myShape, DSS.SupportOnShape1(i)); |
89 | } |
90 | else { |
91 | aSupTyp = DSS. SupportTypeShape2(i); |
92 | if(aSupTyp == BRepExtrema_IsVertex) { |
93 | BB.Add(myShape, DSS.SupportOnShape2(i)); |
94 | } |
95 | else { |
96 | gp_Pnt p(.5*(p1.XYZ()+p2.XYZ())); |
97 | BRepBuilderAPI_MakeVertex mkV(p); |
98 | BB.Add(myShape, mkV.Vertex()); |
99 | |
100 | if (!myMapGener.IsBound(DSS. SupportOnShape1(i))) { |
101 | // for Mandrake-10 - mkv,02.06.06 - myMapGener.Bind(DSS.SupportOnShape1(i), TopTools_ListOfShape()); |
102 | TopTools_ListOfShape aListOfShape1; |
103 | myMapGener.Bind(DSS.SupportOnShape1(i), aListOfShape1); |
104 | } |
105 | myMapGener(DSS.SupportOnShape1(i)).Append(mkV.Vertex()); |
106 | |
107 | if (!myMapGener.IsBound(DSS.SupportOnShape2(i))) { |
108 | // for Mandrake-10 - mkv,02.06.06 - myMapGener.Bind(DSS.SupportOnShape2(i), TopTools_ListOfShape()); |
109 | TopTools_ListOfShape aListOfShape2; |
110 | myMapGener.Bind(DSS.SupportOnShape2(i), aListOfShape2); |
111 | } |
112 | myMapGener(DSS.SupportOnShape2(i)).Append(mkV.Vertex()); |
113 | |
114 | } |
115 | |
116 | } |
117 | } |
118 | } |
119 | } |
120 | |
121 | Done(); |
122 | return; |
123 | } |
124 | |
125 | Standard_Boolean bIsNewFiller = PrepareFiller(); |
126 | // |
127 | if (myErrorStatus!=1) { |
128 | // there were errors during the preparation |
129 | return; |
130 | } |
131 | // |
132 | if (bIsNewFiller) { |
133 | //Prepare the DS |
134 | BOPTools_SSIntersectionAttribute aSectionAttribute(Standard_True, |
135 | Standard_False, Standard_False); |
136 | myDSFiller->Perform(aSectionAttribute); |
137 | } |
138 | |
139 | Build(); |
140 | |
141 | Standard_Boolean bcw = BuilderCanWork(); |
142 | |
143 | if ( ! bcw ) return; |
144 | |
145 | BRep_Builder BB; |
146 | BB.MakeCompound(TopoDS::Compound(myShape)); |
147 | TopTools_ListIteratorOfListOfShape itloe(SectionEdges()); |
148 | |
149 | if(itloe.More()) { |
150 | |
151 | for(; itloe.More(); itloe.Next()) BB.Add(myShape,itloe.Value()); |
152 | |
153 | // try to make wire from set of edges |
154 | TopTools_ListOfShape LOEdge; |
155 | TopTools_ListOfShape LOSEdge; |
156 | Standard_Integer nbe = 0; |
157 | TopoDS_Shape aux; |
158 | TopExp_Explorer Ex; |
159 | BB.MakeCompound(TopoDS::Compound(aux)); |
160 | for(Ex.Init(myShape,TopAbs_EDGE); Ex.More(); Ex.Next()) { |
161 | LOEdge.Append(Ex.Current()); |
162 | LOSEdge.Append(Ex.Current()); |
163 | nbe++; |
164 | } |
165 | BRepAlgo_EdgeConnector EC; |
166 | TopoDS_Shape se = LOEdge.First(); |
167 | EC.Add(LOEdge); |
168 | EC.AddStart(LOSEdge); |
169 | const TopTools_ListOfShape& LOWire = EC.MakeBlock(); |
170 | TopTools_ListIteratorOfListOfShape its; |
171 | for(its.Initialize(LOWire);its.More();its.Next()) { |
172 | BB.Add(aux,its.Value()); |
173 | } |
174 | for(Ex.Init(aux,TopAbs_EDGE); Ex.More(); Ex.Next()) { |
175 | nbe--; |
176 | } |
177 | if(nbe == 0) myShape = aux; |
178 | |
179 | } |
180 | else { |
181 | // check if there are new vertices - IFV |
182 | |
183 | Standard_Integer nbp = QANewModTopOpe_Tools::NbPoints(myDSFiller); |
184 | |
185 | if(nbp > 0) { |
186 | |
187 | TopoDS_Shape aux1, aux2; |
188 | BB.MakeCompound(TopoDS::Compound(aux1)); |
189 | BB.MakeCompound(TopoDS::Compound(aux2)); |
190 | |
191 | BB.Add(aux1, myS1); |
192 | BB.Add(aux1, myS2); |
193 | |
194 | Standard_Integer i; |
195 | Standard_Real maxtol = 0., tol; |
196 | |
197 | for(i = 1; i <= nbp; i++) { |
198 | TopoDS_Vertex aVertex= |
199 | TopoDS::Vertex(QANewModTopOpe_Tools::NewVertex(myDSFiller, i)); |
200 | |
201 | tol=BRep_Tool::Tolerance(aVertex); |
202 | if(tol > maxtol) maxtol = tol; |
203 | BB.Add(aux2, aVertex); |
204 | } |
205 | |
206 | BRepExtrema_DistShapeShape DSS(aux1, aux2); |
207 | if(DSS.IsDone() && DSS.NbSolution() > 0) { |
208 | Standard_Integer nbs = DSS.NbSolution(); |
209 | Standard_Real mindist = DSS.Value(); |
210 | if(mindist <= maxtol) { |
211 | tol = maxtol*maxtol; |
212 | |
213 | TopTools_MapOfShape aMap; |
214 | |
215 | for(i = 1; i <= nbs; i++) { |
216 | gp_Pnt p1 = DSS.PointOnShape1(i); |
217 | gp_Pnt p2 = DSS.PointOnShape2(i); |
218 | if(p1.SquareDistance(p2) > tol) continue; |
219 | BRepExtrema_SupportType aSupTyp = DSS.SupportTypeShape1(i); |
220 | TopoDS_Vertex aVertex; |
221 | if(aSupTyp == BRepExtrema_IsVertex) { |
222 | aVertex = TopoDS::Vertex(DSS.SupportOnShape1(i)); |
223 | TopoDS_Iterator anIter(myShape); |
224 | Standard_Boolean anIsNew = Standard_True; |
225 | for(; anIter.More(); anIter.Next()) { |
226 | if(BRepTools::Compare(aVertex, TopoDS::Vertex(anIter.Value()))) { |
227 | anIsNew = Standard_False; |
228 | break; |
229 | } |
230 | } |
231 | if(anIsNew) { |
232 | BB.Add(myShape, aVertex); |
233 | } |
234 | } |
235 | else { |
236 | aVertex = TopoDS::Vertex(DSS.SupportOnShape2(i)); |
237 | if(aMap.Add(aVertex)) BB.Add(myShape, aVertex); |
238 | if (!myMapGener.IsBound(DSS.SupportOnShape1(i))) { |
239 | // for Mandrake-10 - mkv,02.06.06 - myMapGener.Bind(DSS.SupportOnShape1(i), TopTools_ListOfShape()); |
240 | TopTools_ListOfShape aListOfShape3; |
241 | myMapGener.Bind(DSS.SupportOnShape1(i), aListOfShape3); |
242 | } |
243 | myMapGener(DSS.SupportOnShape1(i)).Append(aVertex); |
244 | |
245 | } |
246 | } // end for |
247 | } |
248 | } |
249 | } |
250 | } |
251 | |
252 | |
253 | Done(); |
254 | |
255 | } |
256 | |
257 | //======================================================================= |
258 | //function : Generated |
259 | //purpose : gives list of shape generated from shape S |
260 | //======================================================================= |
261 | const TopTools_ListOfShape& QANewModTopOpe_Intersection::Generated(const TopoDS_Shape& theS) |
262 | { |
263 | myGenerated.Clear(); |
264 | |
265 | if(theS.ShapeType() == TopAbs_FACE || theS.ShapeType() == TopAbs_EDGE) { |
266 | if (BRepAlgoAPI_BooleanOperation::HasGenerated()) { |
267 | const TopTools_ListOfShape &aLOfShape = |
268 | BRepAlgoAPI_BooleanOperation::Generated(theS); |
269 | TopTools_ListIteratorOfListOfShape anIter(aLOfShape); |
270 | |
271 | for (; anIter.More(); anIter.Next()) { |
272 | const TopoDS_Shape &aGenShape = anIter.Value(); |
273 | |
274 | if (aGenShape.ShapeType() == TopAbs_VERTEX) |
275 | continue; |
276 | |
277 | myGenerated.Append(aGenShape); |
278 | } |
279 | } |
280 | |
281 | if(myMapGener.IsBound(theS)) { |
282 | TopTools_ListIteratorOfListOfShape anIter(myMapGener(theS)); |
283 | |
284 | for(; anIter.More(); anIter.Next()) |
285 | myGenerated.Append(anIter.Value()); |
286 | } |
287 | } |
288 | |
289 | return myGenerated; |
290 | |
291 | } |
292 | |
293 | //======================================================================= |
294 | //function : HasGenerated |
295 | //purpose : |
296 | //======================================================================= |
297 | Standard_Boolean QANewModTopOpe_Intersection::HasGenerated() const |
298 | { |
299 | Standard_Boolean aHasGenerated = Standard_False; |
300 | |
301 | if (BRepAlgoAPI_BooleanOperation::HasGenerated()) { |
302 | aHasGenerated = Standard_True; |
303 | } |
304 | else { |
305 | aHasGenerated = !myMapGener.IsEmpty(); |
306 | } |
307 | return aHasGenerated; |
308 | } |
309 | |
310 | |
311 | //======================================================================= |
312 | //function : IsDeleted |
313 | //purpose : |
314 | //======================================================================= |
315 | Standard_Boolean QANewModTopOpe_Intersection::IsDeleted(const TopoDS_Shape& aS) |
316 | { |
317 | if(BRepAlgoAPI_BooleanOperation::IsDeleted(aS)) |
318 | return Standard_True; |
319 | |
320 | if(myMapGener.IsBound(aS)) |
321 | return Standard_False; |
322 | return Standard_True; |
323 | } |
324 | |
325 | //======================================================================= |
326 | //function : HasDeleted |
327 | //purpose : |
328 | //======================================================================= |
329 | Standard_Boolean QANewModTopOpe_Intersection::HasDeleted() const |
330 | { |
331 | Standard_Real aresult = Standard_False; |
332 | TopExp_Explorer anExp; |
333 | |
334 | for(Standard_Integer argit = 0; argit < 2; argit++) { |
335 | Standard_Boolean bTillVertex = Standard_True; |
336 | |
337 | if(argit == 0) |
338 | anExp.Init(myS1, TopAbs_FACE); |
339 | else |
340 | anExp.Init(myS2, TopAbs_FACE); |
341 | |
342 | if(anExp.More()) |
343 | bTillVertex = Standard_False; |
344 | Standard_Integer nb = (bTillVertex) ? 3 : 2; |
345 | |
346 | for(Standard_Integer i = 0; i < nb; i++) { |
347 | TopAbs_ShapeEnum aType = (i == 0) ? TopAbs_FACE : ((i == 1) ? TopAbs_EDGE : TopAbs_VERTEX); |
348 | |
349 | if(argit == 0) |
350 | anExp.Init(myS1, aType); |
351 | else |
352 | anExp.Init(myS2, aType); |
353 | |
354 | for(; anExp.More(); anExp.Next()) { |
355 | if(!myMapGener.IsBound(anExp.Current())) { |
356 | return Standard_True; |
357 | } |
358 | } |
359 | } |
360 | } |
361 | return Standard_False; |
362 | } |
363 | |
364 | |
365 | |
366 | // @@SDM: begin |
367 | |
368 | // Copyright SAMTECH ..........................................Version 3.0-00 |
369 | // Lastly modified by : mkk Date : 19-05-2003 |
370 | |
371 | // File history synopsis (creation,modification,correction) |
372 | // +---------------------------------------------------------------------------+ |
373 | // ! Developer ! Comments ! Date ! Version ! |
374 | // +-----------!-----------------------------------------!----------!----------+ |
375 | // ! ifv ! Creation !25-12-2000! 3.0-00-2! |
376 | // ! skv ! Adaptation to OCC version 5.0 ! 6-05-2003! 3.0-00-2! |
377 | // ! mkk ! History correction !19-05-2003! 3.0-00-2! |
378 | // ! vladimir ! adaptation to CAS 5.0 ! 07/01/03! 4.0-2! |
379 | // +---------------------------------------------------------------------------+ |
380 | // |
381 | // @@SDM: end |