b311480e |
1 | // Created on: 1995-06-27 |
2 | // Created by: Jacques GOUSSARD |
3 | // Copyright (c) 1995-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 | |
7fd59977 |
21 | |
22 | |
23 | #include <LocOpe_SplitShape.ixx> |
24 | |
25 | #include <TopTools_ListOfShape.hxx> |
26 | #include <TopTools_ListIteratorOfListOfShape.hxx> |
27 | #include <TopTools_MapOfShape.hxx> |
253881cf |
28 | #include <TopTools_MapOfOrientedShape.hxx> |
7fd59977 |
29 | #include <TopTools_DataMapOfShapeListOfShape.hxx> |
30 | #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx> |
ed60a55e |
31 | #include <TopTools_DataMapOfShapeInteger.hxx> |
32 | #include <TopTools_DataMapOfShapeShape.hxx> |
33 | #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx> |
7fd59977 |
34 | #include <TopTools_MapIteratorOfMapOfShape.hxx> |
253881cf |
35 | #include <TopTools_MapIteratorOfMapOfOrientedShape.hxx> |
7fd59977 |
36 | #include <TopoDS_Iterator.hxx> |
37 | #include <TopExp_Explorer.hxx> |
38 | #include <BRep_Builder.hxx> |
39 | #include <TopoDS_Vertex.hxx> |
40 | #include <BRepLib_MakeFace.hxx> |
ed60a55e |
41 | #include <BRepLib_MakeWire.hxx> |
7fd59977 |
42 | #include <BRep_Tool.hxx> |
43 | #include <BRepTools.hxx> |
ed60a55e |
44 | #include <BRepTools_WireExplorer.hxx> |
7fd59977 |
45 | #include <BRep_Builder.hxx> |
46 | |
47 | #include <BRepClass_FaceExplorer.hxx> |
48 | #include <BRepTopAdaptor_FClass2d.hxx> |
49 | #include <BRepAdaptor_Surface.hxx> |
50 | |
51 | #include <Geom2d_Curve.hxx> |
52 | #include <gp_Pnt2d.hxx> |
53 | #include <gp_Vec2d.hxx> |
54 | #include <gp_Dir2d.hxx> |
55 | #include <TopoDS.hxx> |
56 | #include <TopExp.hxx> |
57 | #include <Precision.hxx> |
58 | #include <BRepTools.hxx> |
59 | #include <LocOpe.hxx> |
60 | #include <Standard_ErrorHandler.hxx> |
61 | |
62 | static Standard_Boolean IsInside(const TopoDS_Face&, |
63 | const TopoDS_Wire&, |
64 | const TopoDS_Wire&); |
65 | |
66 | static Standard_Boolean IsInside(const TopoDS_Face&, |
67 | const TopoDS_Wire&); |
68 | |
69 | static void ChoixUV(const TopoDS_Edge&, |
70 | const TopoDS_Face&, |
71 | const TopTools_MapOfShape&, |
72 | TopTools_MapIteratorOfMapOfShape&, |
73 | gp_Pnt2d&, |
74 | gp_Vec2d&, |
75 | const Standard_Real tol); |
76 | |
ed60a55e |
77 | static TopoDS_Shape ChooseDirection(const TopoDS_Shape&, |
78 | const TopoDS_Vertex&, |
79 | const TopoDS_Face&, |
80 | const TopTools_ListOfShape&); |
81 | |
7fd59977 |
82 | inline Standard_Boolean SameUV(const gp_Pnt2d& P1, const gp_Pnt2d& P2, |
83 | const BRepAdaptor_Surface& theBAS)//const Standard_Real tol) |
84 | { |
85 | // Standard_Real tol = Precision::Confusion(); |
86 | // return P1.SquareDistance(P2) < 10*tol; |
87 | //gka |
88 | Standard_Boolean isSame = Standard_True; |
89 | if(theBAS.IsUPeriodic()) |
90 | isSame = (fabs(P1.X() - P2.X()) < theBAS.UPeriod() *0.5); |
91 | if(theBAS.IsVPeriodic()) |
92 | isSame = (isSame && (fabs(P1.Y() - P2.Y()) < theBAS.VPeriod() *0.5)); |
93 | return isSame; |
94 | //return P1.SquareDistance(P2) < tol * tol; //IFV |
95 | } |
96 | |
97 | |
98 | |
99 | //======================================================================= |
100 | //function : Init |
101 | //purpose : |
102 | //======================================================================= |
103 | |
104 | void LocOpe_SplitShape::Init(const TopoDS_Shape& S) |
105 | { |
106 | myDone = Standard_False; |
107 | myShape = S; |
108 | myDblE.Clear(); |
109 | myMap.Clear(); |
110 | Put(myShape); |
111 | } |
112 | |
113 | |
114 | //======================================================================= |
115 | //function : CanSplit |
116 | //purpose : |
117 | //======================================================================= |
118 | |
119 | Standard_Boolean LocOpe_SplitShape::CanSplit(const TopoDS_Edge& E) const |
120 | { |
121 | if (myDone) { |
122 | return Standard_False; |
123 | } |
124 | if (myMap.IsEmpty()) { |
125 | return Standard_False; |
126 | } |
127 | |
128 | if (!myMap.IsBound(E)) { |
129 | return Standard_False; |
130 | } |
131 | |
132 | // On verifie que l`edge n`appartient pas a un wire deja reconstruit |
133 | TopExp_Explorer exp; |
134 | TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itm(myMap); |
135 | for (; itm.More(); itm.Next()) { |
136 | if (itm.Key().ShapeType() == TopAbs_WIRE && !itm.Value().IsEmpty()) { |
137 | for (exp.Init(itm.Key(),TopAbs_EDGE); exp.More(); exp.Next()) { |
138 | if (exp.Current().IsSame(E)) { |
139 | return Standard_False; |
140 | } |
141 | } |
142 | } |
143 | } |
144 | return Standard_True; |
145 | } |
146 | |
147 | |
148 | //======================================================================= |
149 | //function : Add |
150 | //purpose : |
151 | //======================================================================= |
152 | |
153 | void LocOpe_SplitShape::Add(const TopoDS_Vertex& V, |
154 | const Standard_Real P, |
155 | const TopoDS_Edge& E) |
156 | { |
157 | if (!CanSplit(E)) { |
158 | Standard_ConstructionError::Raise(); |
159 | } |
160 | |
161 | BRep_Builder B; |
162 | TopTools_ListOfShape& le = myMap(E); |
163 | if (le.IsEmpty()) { |
164 | le.Append(E); |
165 | } |
166 | TopTools_ListIteratorOfListOfShape itl(le); |
167 | Standard_Real f,l; |
168 | |
169 | for (; itl.More(); itl.Next()) { |
170 | const TopoDS_Edge& edg = TopoDS::Edge(itl.Value()); |
171 | BRep_Tool::Range(edg,f,l); |
172 | if (P>f && P <l) { |
173 | break; |
174 | } |
175 | } |
176 | if (!itl.More()) { |
177 | Standard_ConstructionError::Raise(); |
178 | } |
179 | TopoDS_Edge edg = TopoDS::Edge(itl.Value()); |
180 | le.Remove(itl); |
181 | if (V.Orientation() == TopAbs_FORWARD || |
182 | V.Orientation() == TopAbs_REVERSED) { |
183 | |
184 | TopoDS_Shape aLocalShape = edg.EmptyCopied(); |
185 | TopoDS_Edge E1 = TopoDS::Edge(aLocalShape); |
186 | aLocalShape = edg.EmptyCopied(); |
187 | TopoDS_Edge E2 = TopoDS::Edge(aLocalShape); |
188 | // TopoDS_Edge E1 = TopoDS::Edge(edg.EmptyCopied()); |
189 | // TopoDS_Edge E2 = TopoDS::Edge(edg.EmptyCopied()); |
190 | E1.Orientation(TopAbs_FORWARD); |
191 | E2.Orientation(TopAbs_FORWARD); |
192 | TopoDS_Vertex newVtx = V; |
193 | newVtx.Orientation(TopAbs_REVERSED); |
194 | B.Add(E1,newVtx); |
195 | B.UpdateVertex(newVtx,P,E1,BRep_Tool::Tolerance(V)); |
196 | newVtx.Orientation(TopAbs_FORWARD); |
197 | B.Add(E2,newVtx); |
198 | B.UpdateVertex(newVtx,P,E2,BRep_Tool::Tolerance(V)); |
199 | edg.Orientation(TopAbs_FORWARD); |
200 | TopExp_Explorer exp; |
201 | for (exp.Init(edg,TopAbs_VERTEX); exp.More(); exp.Next()) { |
202 | // for (TopExp_Explorer exp(edg,TopAbs_VERTEX); exp.More(); exp.Next()) { |
203 | const TopoDS_Vertex& vtx = TopoDS::Vertex(exp.Current()); |
204 | f = BRep_Tool::Parameter(vtx,edg); |
205 | if (f < P) { |
206 | B.Add(E1,vtx); |
207 | B.UpdateVertex(vtx,f,E1,BRep_Tool::Tolerance(vtx)); |
208 | } |
209 | else { |
210 | B.Add(E2,vtx); |
211 | B.UpdateVertex(vtx,f,E2,BRep_Tool::Tolerance(vtx)); |
212 | } |
213 | } |
214 | le.Append(E1); |
215 | le.Append(E2); |
216 | } |
217 | else { |
218 | TopoDS_Shape aLocalShape = edg.EmptyCopied(); |
219 | TopoDS_Edge E1 = TopoDS::Edge(aLocalShape); |
220 | // TopoDS_Edge E1 = TopoDS::Edge(edg.EmptyCopied()); |
221 | TopExp_Explorer exp; |
222 | for (exp.Init(edg,TopAbs_VERTEX); exp.More(); exp.Next()) { |
223 | // for (TopExp_Explorer exp(edg,TopAbs_VERTEX); exp.More(); exp.Next()) { |
224 | const TopoDS_Vertex& vtx = TopoDS::Vertex(exp.Current()); |
225 | f = BRep_Tool::Parameter(vtx,edg); |
226 | B.Add(E1,vtx); |
227 | B.UpdateVertex(vtx,f,E1,BRep_Tool::Tolerance(vtx)); |
228 | } |
229 | B.Add(E1,V); |
230 | B.UpdateVertex(V,P,E1,BRep_Tool::Tolerance(V)); |
231 | le.Append(E1); |
232 | } |
233 | } |
234 | |
ed60a55e |
235 | //======================================================================= |
236 | //function : Add |
237 | //purpose : adds the list of wires on the face <F> |
238 | //======================================================================= |
239 | |
240 | void LocOpe_SplitShape::Add(const TopTools_ListOfShape& Lwires, |
241 | const TopoDS_Face& F) |
242 | { |
243 | |
244 | if (myDone) { |
245 | Standard_ConstructionError::Raise(); |
246 | } |
247 | |
248 | TopTools_ListOfShape& lf = myMap(F); |
249 | if (lf.IsEmpty()) { |
250 | Rebuild(F); |
251 | } |
252 | |
253 | // On cherche la face descendante de F qui continent le wire |
254 | lf = myMap(F); |
255 | TopTools_ListIteratorOfListOfShape itl(lf); |
256 | TopoDS_Vertex Vfirst,Vlast; |
257 | |
258 | BRepTools::Update(F); |
259 | |
260 | for (; itl.More(); itl.Next()) |
261 | { |
262 | const TopoDS_Face& fac = TopoDS::Face(itl.Value()); |
263 | Standard_Boolean AllWiresInside = Standard_True; |
264 | TopTools_ListIteratorOfListOfShape itwires(Lwires); |
265 | for (; itwires.More(); itwires.Next()) |
266 | { |
267 | const TopoDS_Wire& aWire = TopoDS::Wire(itwires.Value()); |
268 | if (!IsInside(fac, aWire)) |
269 | { |
270 | AllWiresInside = Standard_False; |
271 | break; |
272 | } |
273 | } |
274 | if (AllWiresInside) |
275 | break; |
276 | } |
277 | if (!itl.More()) { |
278 | Standard_ConstructionError::Raise(); |
279 | } |
280 | |
281 | TopoDS_Face FaceRef = TopoDS::Face(itl.Value()); |
282 | FaceRef.Orientation(TopAbs_FORWARD); |
283 | lf.Remove(itl); |
284 | |
285 | TopTools_ListOfShape NewWires; |
286 | |
287 | TopTools_DataMapOfShapeInteger SectionsTimes; |
288 | for (itl.Initialize(Lwires); itl.More(); itl.Next()) |
289 | SectionsTimes.Bind(itl.Value(), 2); |
290 | |
291 | TopTools_ListOfShape BreakVertices; |
292 | TopTools_ListOfShape BreakOnWires; |
293 | |
294 | TopTools_DataMapOfShapeShape VerWireMap; |
295 | Standard_Integer i; |
296 | TopExp_Explorer ExploF, ExploW; |
297 | for (itl.Initialize(Lwires); itl.More(); itl.Next()) |
298 | { |
299 | const TopoDS_Wire& aSection = TopoDS::Wire(itl.Value()); |
300 | TopoDS_Vertex Ver [2]; |
301 | TopExp::Vertices(aSection, Ver[0], Ver[1]); |
302 | for (i = 0; i < 2; i++) |
303 | { |
304 | if (VerWireMap.IsBound(Ver[i])) |
305 | continue; |
306 | for (ExploF.Init(FaceRef, TopAbs_WIRE); ExploF.More(); ExploF.Next()) |
307 | { |
308 | const TopoDS_Shape& aWire = ExploF.Current(); |
309 | TopoDS_Shape aVer; |
310 | for (ExploW.Init(aWire, TopAbs_VERTEX); ExploW.More(); ExploW.Next()) |
311 | { |
312 | aVer = ExploW.Current(); |
313 | if (aVer.IsSame(Ver[i])) |
314 | break; |
315 | } |
316 | if (aVer.IsSame(Ver[i])) |
317 | { |
318 | VerWireMap.Bind(aVer, aWire); |
319 | break; |
320 | } |
321 | } |
322 | } |
323 | } |
324 | |
325 | TopTools_DataMapOfShapeListOfShape VerSecMap; |
326 | for (itl.Initialize(Lwires); itl.More(); itl.Next()) |
327 | { |
328 | const TopoDS_Wire& aWire = TopoDS::Wire(itl.Value()); |
329 | TopoDS_Vertex V1, V2; |
330 | TopExp::Vertices(aWire, V1, V2); |
331 | TopTools_ListOfShape LW1, LW2; |
332 | if (!VerSecMap.IsBound(V1)) |
333 | VerSecMap.Bind(V1, LW1); |
334 | VerSecMap(V1).Append(aWire); |
335 | if (!VerSecMap.IsBound(V2)) |
336 | VerSecMap.Bind(V2, LW2); |
337 | VerSecMap(V2).Append(aWire); |
338 | } |
339 | |
340 | //TopTools_IndexedDataMapOfShapeShape InnerTouchingWiresOnVertex; |
341 | |
342 | TopoDS_Wire outerW = BRepTools::OuterWire(FaceRef); |
343 | TopoDS_Wire CurWire = outerW; |
344 | BRepLib_MakeWire *MW; |
345 | MW = new BRepLib_MakeWire(); |
346 | BRepTools_WireExplorer wexp(CurWire, FaceRef); |
347 | for (;;) |
348 | { |
349 | TopoDS_Vertex theStartVertex = wexp.CurrentVertex(), CurVertex; |
350 | TopoDS_Edge CurEdge = wexp.Current(); |
351 | TopoDS_Edge LastEdge = CurEdge; |
352 | MW->Add(CurEdge); |
353 | TopoDS_Wire aSectionWire; |
354 | TopoDS_Vertex aBreakVertex; |
355 | wexp.Next(); |
356 | if (!wexp.More()) |
357 | wexp.Init(CurWire, FaceRef); |
358 | for (;;) |
359 | { |
360 | if (MW->Wire().Closed()) |
361 | break; |
362 | CurVertex = wexp.CurrentVertex(); |
363 | if (VerSecMap.IsBound(CurVertex)) |
364 | { |
365 | TopoDS_Shape aLocalWire = ChooseDirection(LastEdge, CurVertex, FaceRef, VerSecMap(CurVertex)); |
366 | aSectionWire = TopoDS::Wire(aLocalWire); |
367 | break; |
368 | } |
369 | CurEdge = wexp.Current(); |
370 | MW->Add(CurEdge); |
371 | LastEdge = CurEdge; |
372 | wexp.Next(); |
373 | if (!wexp.More()) |
374 | wexp.Init(CurWire, FaceRef); |
375 | } |
376 | if (MW->Wire().Closed()) |
377 | { |
378 | NewWires.Append(MW->Wire()); |
379 | theStartVertex = TopoDS::Vertex(BreakVertices.First()); |
380 | BreakVertices.RemoveFirst(); |
381 | CurWire = TopoDS::Wire(BreakOnWires.First()); |
382 | BreakOnWires.RemoveFirst(); |
383 | wexp.Init(CurWire, FaceRef); |
384 | while (!wexp.CurrentVertex().IsSame(theStartVertex)) |
385 | wexp.Next(); |
386 | MW = new BRepLib_MakeWire(); |
387 | continue; |
388 | } |
389 | aBreakVertex = CurVertex; |
390 | BreakVertices.Append(aBreakVertex); |
391 | BreakOnWires.Append(CurWire); |
392 | for (;;) |
393 | { |
394 | MW->Add(aSectionWire); |
395 | (SectionsTimes(aSectionWire))--; |
396 | if (SectionsTimes(aSectionWire) == 0) |
397 | SectionsTimes.UnBind(aSectionWire); |
398 | if (MW->Wire().Closed()) |
399 | { |
400 | NewWires.Append(MW->Wire()); |
401 | if (SectionsTimes.IsEmpty()) |
402 | break; |
403 | theStartVertex = TopoDS::Vertex(BreakVertices.First()); |
404 | BreakVertices.RemoveFirst(); |
405 | CurWire = TopoDS::Wire(BreakOnWires.First()); |
406 | BreakOnWires.RemoveFirst(); |
407 | wexp.Init(CurWire, FaceRef); |
408 | while (!wexp.CurrentVertex().IsSame(theStartVertex)) |
409 | wexp.Next(); |
410 | MW = new BRepLib_MakeWire(); |
411 | break; |
412 | } |
413 | else |
414 | { |
415 | TopoDS_Vertex V1, V2, aStartVertex; |
416 | TopExp::Vertices(aSectionWire, V1, V2); |
417 | aStartVertex = (V1.IsSame(aBreakVertex))? V2 : V1; |
418 | CurWire = TopoDS::Wire(VerWireMap(aStartVertex)); |
419 | |
420 | wexp.Init(CurWire, FaceRef); |
421 | while (!wexp.CurrentVertex().IsSame(aStartVertex)) |
422 | wexp.Next(); |
423 | |
424 | const TopTools_ListOfShape& Lsections = VerSecMap(aStartVertex); |
425 | if (Lsections.Extent() == 1) |
426 | break; |
427 | |
428 | //else: choose the way |
429 | TopoDS_Wire NextSectionWire = |
430 | TopoDS::Wire((aSectionWire.IsSame(Lsections.First()))? Lsections.Last() : Lsections.First()); |
431 | |
432 | Standard_Integer Times = 0; |
433 | TopTools_DataMapIteratorOfDataMapOfShapeShape itVW(VerWireMap); |
434 | for (; itVW.More(); itVW.Next()) |
435 | if (itVW.Value().IsSame(CurWire)) |
436 | Times++; |
437 | if (Times == 1) //it is inner touching wire |
438 | { |
439 | //InnerTouchingWiresOnVertex.Bind(aWire, aStartVertex); |
440 | } |
441 | else |
442 | { |
443 | //we have to choose the direction |
444 | TopoDS_Edge aStartEdge = wexp.Current(); |
445 | TopTools_ListOfShape Ldirs; |
446 | Ldirs.Append(aStartEdge); |
447 | Ldirs.Append(NextSectionWire); |
448 | TopoDS_Shape theDirection = ChooseDirection(aSectionWire, aStartVertex, FaceRef, Ldirs); |
449 | if (theDirection.IsSame(aStartEdge)) |
450 | break; |
451 | } |
452 | aSectionWire = NextSectionWire; |
453 | aBreakVertex = aStartVertex; |
454 | } //end of else (MW is not closed) |
455 | } //end of for (;;) (loop on section wires) |
456 | if (SectionsTimes.IsEmpty()) |
457 | break; |
458 | } //end of global for (;;) |
459 | |
460 | TopTools_ListOfShape NewFaces; |
461 | BRep_Builder BB; |
462 | for (itl.Initialize(NewWires); itl.More(); itl.Next()) |
463 | { |
464 | TopoDS_Shape aLocalFace = FaceRef.EmptyCopied(); |
465 | TopoDS_Face aNewFace = TopoDS::Face(aLocalFace); |
466 | aNewFace.Orientation(TopAbs_FORWARD); |
467 | BB.Add(aNewFace, itl.Value()); |
468 | NewFaces.Append(aNewFace); |
469 | } |
470 | |
471 | //Inserting holes |
472 | TopTools_ListOfShape Holes; |
473 | for (ExploF.Init(FaceRef, TopAbs_WIRE); ExploF.More(); ExploF.Next()) |
474 | { |
475 | const TopoDS_Shape& aWire = ExploF.Current(); |
476 | ExploW.Init(aWire, TopAbs_EDGE); |
477 | TopoDS_Shape anEdge = ExploW.Current(); |
478 | Standard_Boolean found = Standard_False; |
479 | for (itl.Initialize(NewWires); itl.More(); itl.Next()) |
480 | { |
481 | const TopoDS_Shape& aNewWire = itl.Value(); |
482 | for (ExploW.Init(aNewWire, TopAbs_EDGE); ExploW.More(); ExploW.Next()) |
483 | { |
484 | if (anEdge.IsSame(ExploW.Current())) |
485 | { |
486 | found = Standard_True; |
487 | break; |
488 | } |
489 | } |
490 | if (found) |
491 | break; |
492 | } |
493 | if (!found) |
494 | Holes.Append(aWire); |
495 | } |
496 | TopTools_ListIteratorOfListOfShape itlNewF; |
497 | for (itl.Initialize(Holes); itl.More(); itl.Next()) |
498 | { |
499 | const TopoDS_Wire& aHole = TopoDS::Wire(itl.Value()); |
500 | for (itlNewF.Initialize(NewFaces); itlNewF.More(); itlNewF.Next()) |
501 | { |
502 | TopoDS_Face& aNewFace = TopoDS::Face(itlNewF.Value()); |
503 | if (IsInside(aNewFace, aHole)) |
504 | { |
505 | BB.Add(aNewFace, aHole); |
506 | break; |
507 | } |
508 | } |
509 | } |
510 | |
511 | //Update "myMap" |
512 | lf.Append(NewFaces); |
513 | |
514 | //Update of descendants of wires |
515 | for (ExploF.Init(F, TopAbs_WIRE); ExploF.More(); ExploF.Next()) |
516 | { |
517 | TopTools_ListOfShape& ls = myMap(ExploF.Current()); |
518 | ls.Clear(); |
519 | } |
520 | /////////////////// |
521 | |
522 | // JAG 10.11.95 Codage des regularites |
523 | for (itl.Initialize(Lwires); itl.More(); itl.Next()) |
524 | for (ExploW.Init(itl.Value(), TopAbs_EDGE); ExploW.More(); ExploW.Next()) |
525 | { |
526 | const TopoDS_Edge& edg = TopoDS::Edge(ExploW.Current()); |
527 | if (!BRep_Tool::HasContinuity(edg,F,F)) { |
528 | BB.Continuity(edg,F,F,GeomAbs_CN); |
529 | } |
530 | } |
531 | } |
7fd59977 |
532 | |
533 | |
534 | //======================================================================= |
535 | //function : Add |
536 | //purpose : |
537 | //======================================================================= |
538 | |
539 | void LocOpe_SplitShape::Add(const TopoDS_Wire& W, |
540 | const TopoDS_Face& F) |
541 | { |
542 | |
543 | if (myDone) { |
544 | Standard_ConstructionError::Raise(); |
545 | } |
546 | |
547 | |
548 | TopExp_Explorer exp; |
549 | TopTools_ListOfShape& lf = myMap(F); |
550 | if (lf.IsEmpty()) { |
551 | Rebuild(F); |
552 | } |
553 | try { |
554 | OCC_CATCH_SIGNALS |
555 | if (!LocOpe::Closed(W,F)) { |
556 | AddOpenWire(W,F); |
557 | } |
558 | else { |
559 | AddClosedWire(W,F); |
560 | } |
561 | } catch (Standard_Failure ) { |
562 | #ifdef DEB |
563 | cout << "Warning: SpliShape internal problem detected, some faces may be lost. Check input edges/wires" <<endl; |
564 | #endif |
565 | return; |
566 | } |
567 | // JAG 10.11.95 Codage des regularites |
568 | BRep_Builder B; |
569 | for (exp.Init(W,TopAbs_EDGE); exp.More(); exp.Next()) { |
570 | const TopoDS_Edge& edg = TopoDS::Edge(exp.Current()); |
571 | if (!BRep_Tool::HasContinuity(edg,F,F)) { |
572 | B.Continuity(edg,F,F,GeomAbs_CN); |
573 | } |
574 | } |
575 | } |
576 | |
577 | |
578 | |
579 | //======================================================================= |
580 | //function : AddClosedWire |
581 | //purpose : |
582 | //======================================================================= |
583 | |
584 | void LocOpe_SplitShape::AddClosedWire(const TopoDS_Wire& W, |
585 | const TopoDS_Face& F) |
586 | { |
587 | TopExp_Explorer exp; |
588 | |
589 | // On cherche la face descendante de F qui continent le wire |
590 | TopTools_ListOfShape& lf = myMap(F); |
591 | TopTools_ListIteratorOfListOfShape itl(lf); |
592 | TopoDS_Wire outerW; |
593 | for (; itl.More(); itl.Next()) { |
594 | const TopoDS_Face& fac = TopoDS::Face(itl.Value()); |
595 | /* |
596 | outerW = BRepTools::OuterWire(fac); |
597 | if (IsInside(F,W,outerW)) { |
598 | break; |
599 | } |
600 | */ |
601 | if (IsInside(fac,W)) { |
602 | break; |
603 | } |
604 | |
605 | } |
606 | if (!itl.More()) { |
607 | Standard_ConstructionError::Raise(); |
608 | } |
609 | |
610 | BRep_Builder B; |
611 | |
612 | TopAbs_Orientation orWire = W.Orientation(); |
613 | TopoDS_Shape aLocalFace = F.EmptyCopied(); |
614 | TopoDS_Face newFace = TopoDS::Face(aLocalFace); |
615 | // TopoDS_Face newFace = TopoDS::Face(F.EmptyCopied()); |
616 | newFace.Orientation(TopAbs_FORWARD); |
617 | B.Add(newFace,W); |
618 | // GProp_GProps GP; |
619 | // BRepGProp::SurfaceProperties (newFace,GP); |
620 | // if (GP.Mass() < 0) { |
621 | BRepTopAdaptor_FClass2d classif(newFace,Precision::PConfusion()); |
622 | if (classif.PerformInfinitePoint() == TopAbs_IN) { |
623 | //le wire donne defini un trou |
624 | aLocalFace = F.EmptyCopied(); |
625 | newFace = TopoDS::Face(aLocalFace); |
626 | // newFace = TopoDS::Face(F.EmptyCopied()); |
627 | newFace.Orientation(TopAbs_FORWARD); |
628 | orWire = TopAbs::Reverse(orWire); |
629 | B.Add(newFace,W.Oriented(orWire)); |
630 | } |
631 | |
632 | TopoDS_Face FaceRef = TopoDS::Face(itl.Value()); |
633 | FaceRef.Orientation(TopAbs_FORWARD); |
634 | lf.Remove(itl); |
635 | |
636 | aLocalFace = FaceRef.EmptyCopied(); |
637 | TopoDS_Face newRef = TopoDS::Face(aLocalFace); |
638 | // TopoDS_Face newRef = TopoDS::Face(FaceRef.EmptyCopied()); |
639 | newRef.Orientation(TopAbs_FORWARD); |
640 | |
641 | // On suppose que les edges du wire ont des courbes 2d. |
642 | // Comme on ne change pas de surface de base, pas besoin d`UpdateEdge. |
643 | |
644 | for (exp.Init(FaceRef.Oriented(TopAbs_FORWARD),TopAbs_WIRE); |
645 | exp.More(); exp.Next()) { |
646 | const TopoDS_Wire& wir = TopoDS::Wire(exp.Current()); |
647 | if (IsInside(F,wir,W)) { |
648 | B.Add(newFace,wir); |
649 | } |
650 | else { |
651 | B.Add(newRef,wir); |
652 | } |
653 | } |
654 | B.Add(newRef,W.Oriented(TopAbs::Reverse(orWire))); |
655 | lf.Append(newRef); |
656 | lf.Append(newFace); |
657 | |
658 | } |
659 | |
660 | |
661 | //======================================================================= |
662 | //function : AddOpenWire |
663 | //purpose : |
664 | //======================================================================= |
665 | |
666 | void LocOpe_SplitShape::AddOpenWire(const TopoDS_Wire& W, |
667 | const TopoDS_Face& F) |
668 | { |
669 | // On cherche la face descendante de F qui continent le wire |
670 | TopTools_ListOfShape& lf = myMap(F); |
671 | TopTools_ListIteratorOfListOfShape itl(lf); |
672 | TopoDS_Vertex Vfirst,Vlast; |
673 | |
674 | BRepTools::Update(F); |
675 | |
7fd59977 |
676 | Standard_Real tolf, toll, tol1; |
677 | |
678 | TopoDS_Shape aLocalShape = W.Oriented(TopAbs_FORWARD); |
679 | TopExp::Vertices(TopoDS::Wire(aLocalShape),Vfirst,Vlast); |
7fd59977 |
680 | |
681 | tolf = BRep_Tool::Tolerance(Vfirst); |
682 | toll = BRep_Tool::Tolerance(Vlast); |
683 | tol1 = Max(tolf, toll); |
684 | |
685 | |
253881cf |
686 | TopExp_Explorer exp,exp2; |
7fd59977 |
687 | |
688 | TopoDS_Wire wfirst,wlast; |
689 | for (; itl.More(); itl.Next()) { |
253881cf |
690 | TopoDS_Face fac = TopoDS::Face(itl.Value()); |
7fd59977 |
691 | if (!IsInside(fac,W)) { |
692 | continue; |
693 | } |
253881cf |
694 | |
695 | fac.Orientation(TopAbs_FORWARD); |
7fd59977 |
696 | Standard_Boolean ffound = Standard_False; |
697 | Standard_Boolean lfound = Standard_False; |
698 | for (exp.Init(fac,TopAbs_WIRE); exp.More(); exp.Next()) { |
699 | const TopoDS_Wire& wir = TopoDS::Wire(exp.Current()); |
700 | for (exp2.Init(wir,TopAbs_VERTEX); exp2.More(); exp2.Next()) { |
701 | if (!ffound && exp2.Current().IsSame(Vfirst)) { |
702 | ffound = Standard_True; |
703 | wfirst = wir; |
704 | } |
705 | else if (!lfound && exp2.Current().IsSame(Vlast)) { |
706 | lfound = Standard_True; |
707 | wlast = wir; |
708 | } |
709 | if (ffound && lfound) { |
710 | break; |
711 | } |
712 | } |
713 | if (exp2.More()) { |
714 | break; |
715 | } |
716 | } |
717 | if (exp.More()) { |
718 | break; |
719 | } |
720 | } |
721 | if (!itl.More()) { |
722 | Standard_ConstructionError::Raise(); |
723 | } |
724 | |
725 | TopoDS_Face FaceRef = TopoDS::Face(itl.Value()); |
726 | FaceRef.Orientation(TopAbs_FORWARD); |
727 | lf.Remove(itl); |
728 | BRep_Builder B; |
729 | |
730 | BRepAdaptor_Surface BAS(FaceRef, Standard_False); |
731 | |
732 | Standard_Boolean IsPeriodic = BAS.IsUPeriodic() || BAS.IsVPeriodic(); |
733 | |
734 | tol1 = Max(BAS.UResolution(tol1), BAS.VResolution(tol1)); |
735 | |
736 | if (wfirst.IsSame(wlast)) { |
737 | // on cree 2 faces en remplacement de itl.Value() |
738 | // Essai JAG |
253881cf |
739 | TopTools_ListOfShape WiresFirst; |
7fd59977 |
740 | for (exp.Init(wfirst,TopAbs_EDGE); exp.More(); exp.Next()) { |
741 | if (BRep_Tool::IsClosed(TopoDS::Edge(exp.Current()),FaceRef)) { |
742 | myDblE.Add(exp.Current()); |
743 | } |
253881cf |
744 | WiresFirst.Append(exp.Current()); |
7fd59977 |
745 | } |
746 | |
747 | TopAbs_Orientation orient; |
748 | TopoDS_Wire newW1,newW2; |
749 | B.MakeWire(newW1); |
750 | newW1.Orientation(TopAbs_FORWARD); |
751 | B.MakeWire(newW2); |
752 | newW2.Orientation(TopAbs_FORWARD); |
753 | |
253881cf |
754 | Standard_Integer nbE = 0; |
7fd59977 |
755 | for (exp.Init(W.Oriented(TopAbs_FORWARD),TopAbs_EDGE); |
756 | exp.More(); exp.Next()) { |
757 | nbE++; |
758 | const TopoDS_Edge& edg = TopoDS::Edge(exp.Current()); |
253881cf |
759 | orient = edg.Orientation(); |
760 | WiresFirst.Append(edg); |
761 | WiresFirst.Append(edg.Oriented(TopAbs::Reverse(orient))); |
762 | myDblE.Add(edg); |
763 | } |
764 | |
765 | TopTools_MapOfShape PossE; |
766 | TopTools_MapOfOrientedShape MapE; |
7fd59977 |
767 | TopTools_MapIteratorOfMapOfShape itm; |
768 | TopoDS_Vertex vdeb,vfin; |
769 | Standard_Integer nbPoss; |
770 | |
771 | // On recherche l`edge contenant Vlast |
772 | TopoDS_Edge LastEdge; |
773 | gp_Pnt2d pfirst,plast; |
774 | gp_Vec2d dlast; |
775 | Handle(Geom2d_Curve) C2d; |
776 | Standard_Real f,l; |
777 | |
778 | for (exp.Init(W.Oriented(TopAbs_FORWARD),TopAbs_EDGE); |
779 | exp.More(); exp.Next()) { |
780 | const TopoDS_Edge& edg = TopoDS::Edge(exp.Current()); |
781 | for (exp2.Init(edg,TopAbs_VERTEX); exp2.More(); exp2.Next()) { |
782 | if (exp2.Current().IsSame(Vfirst)) { |
783 | break; |
784 | } |
785 | } |
786 | if (exp2.More()) { |
787 | LastEdge = edg; |
788 | LastEdge.Orientation(edg.Orientation()); |
789 | break; |
790 | } |
791 | } |
792 | |
793 | TopoDS_Shape aLocalFace = FaceRef.Oriented(wfirst.Orientation()); |
794 | C2d = BRep_Tool::CurveOnSurface(LastEdge, TopoDS::Face(aLocalFace), f, l); |
253881cf |
795 | |
7fd59977 |
796 | if (LastEdge.Orientation() == TopAbs_FORWARD) { |
797 | pfirst = C2d->Value(f); |
798 | } |
799 | else { |
800 | pfirst = C2d->Value(l); |
801 | } |
802 | |
803 | for (exp.Init(W.Oriented(TopAbs_FORWARD),TopAbs_EDGE); |
804 | exp.More(); exp.Next()) { |
805 | const TopoDS_Edge& edg = TopoDS::Edge(exp.Current()); |
806 | if( nbE>1 && edg.IsSame(LastEdge) ) |
807 | continue; |
808 | for (exp2.Init(edg,TopAbs_VERTEX); exp2.More(); exp2.Next()) { |
809 | if (exp2.Current().IsSame(Vlast)) { |
810 | break; |
811 | } |
812 | } |
813 | if (exp2.More()) { |
814 | LastEdge = edg; |
815 | LastEdge.Orientation(edg.Orientation()); |
816 | break; |
817 | } |
818 | } |
819 | aLocalFace = FaceRef.Oriented(wfirst.Orientation()); |
820 | C2d = BRep_Tool::CurveOnSurface(LastEdge, TopoDS::Face(aLocalFace), f, l); |
7c104885 |
821 | Standard_Real dpar = (l - f)*0.01; |
253881cf |
822 | |
7fd59977 |
823 | if (LastEdge.Orientation() == TopAbs_FORWARD) { |
824 | C2d->D1(l,plast,dlast); |
7c104885 |
825 | if (dlast.Magnitude() < gp::Resolution()) |
826 | { |
827 | gp_Pnt2d PrevPnt = C2d->Value(l - dpar); |
828 | dlast.SetXY(plast.XY() - PrevPnt.XY()); |
829 | } |
7fd59977 |
830 | } |
831 | else { |
7fd59977 |
832 | C2d->D1(f,plast,dlast); |
7c104885 |
833 | if (dlast.Magnitude() < gp::Resolution()) |
834 | { |
835 | gp_Pnt2d NextPnt = C2d->Value(f + dpar); |
836 | dlast.SetXY(NextPnt.XY() - plast.XY()); |
837 | } |
7fd59977 |
838 | dlast.Reverse(); |
839 | } |
840 | |
841 | Standard_Boolean cond; |
842 | |
843 | if(IsPeriodic) { |
844 | |
845 | cond = !(Vfirst.IsSame(Vlast) && SameUV(pfirst,plast,BAS)); |
846 | } |
847 | else { |
848 | cond = !(Vfirst.IsSame(Vlast)); |
849 | } |
253881cf |
850 | |
851 | while (cond) { |
7fd59977 |
852 | PossE.Clear(); |
853 | |
253881cf |
854 | // On enchaine par la fin |
855 | TopTools_ListIteratorOfListOfShape lexp(WiresFirst); |
856 | for (; lexp.More(); lexp.Next()) { |
857 | const TopoDS_Edge& edg = TopoDS::Edge(lexp.Value()); |
7fd59977 |
858 | |
859 | orient = edg.Orientation(); |
860 | TopExp::Vertices(edg,vdeb,vfin); |
861 | if (orient == TopAbs_FORWARD && Vlast.IsSame(vdeb)) { |
862 | PossE.Add(edg.Oriented(orient)); |
863 | } |
864 | else if (orient == TopAbs_REVERSED && Vlast.IsSame(vfin)) { |
865 | PossE.Add(edg.Oriented(orient)); |
866 | } |
867 | } |
868 | nbPoss = PossE.Extent(); |
253881cf |
869 | if (nbPoss == 0) |
870 | { |
871 | break; |
872 | } |
873 | |
7fd59977 |
874 | if (nbPoss == 1) { |
875 | itm.Initialize(PossE); |
876 | TopoDS_Shape aLocalFace = FaceRef.Oriented(wfirst.Orientation()); |
877 | C2d = BRep_Tool::CurveOnSurface(TopoDS::Edge(itm.Key()), |
878 | TopoDS::Face(aLocalFace), f, l); |
7c104885 |
879 | Standard_Real dpar = (l - f)*0.01; |
253881cf |
880 | |
7fd59977 |
881 | if (itm.Key().Orientation() == TopAbs_FORWARD) { |
7fd59977 |
882 | C2d->D1(l,plast,dlast); |
7c104885 |
883 | if (dlast.Magnitude() < gp::Resolution()) |
884 | { |
885 | gp_Pnt2d PrevPnt = C2d->Value(l - dpar); |
886 | dlast.SetXY(plast.XY() - PrevPnt.XY()); |
887 | } |
7fd59977 |
888 | } |
889 | else { |
7fd59977 |
890 | C2d->D1(f,plast,dlast); |
7c104885 |
891 | if (dlast.Magnitude() < gp::Resolution()) |
892 | { |
893 | gp_Pnt2d NextPnt = C2d->Value(f + dpar); |
894 | dlast.SetXY(NextPnt.XY() - plast.XY()); |
895 | } |
7fd59977 |
896 | dlast.Reverse(); |
897 | } |
898 | } |
899 | else if (nbPoss > 1) { |
900 | // Faire choix en U,V... |
901 | TopoDS_Shape aLocalFace = FaceRef.Oriented(wfirst.Orientation()); |
902 | |
7fd59977 |
903 | ChoixUV(LastEdge, TopoDS::Face(aLocalFace), PossE, |
904 | itm, plast, dlast, toll); |
7fd59977 |
905 | } |
906 | |
907 | if (nbPoss >= 1) { |
253881cf |
908 | if (MapE.Contains(itm.Key())) |
909 | break; |
7fd59977 |
910 | B.Add(newW1,itm.Key()); |
253881cf |
911 | MapE.Add(itm.Key()); |
7fd59977 |
912 | LastEdge = TopoDS::Edge(itm.Key()); |
253881cf |
913 | |
7fd59977 |
914 | if (LastEdge.Orientation() == TopAbs_FORWARD) { |
915 | Vlast = TopExp::LastVertex(LastEdge); |
916 | } |
917 | else { |
918 | Vlast = TopExp::FirstVertex(LastEdge); |
919 | } |
920 | |
921 | toll = BRep_Tool::Tolerance(Vlast); |
922 | tol1 = Max(tolf, toll); |
923 | |
924 | } |
925 | //MODIFICATION PIERRE SMEYERS : si pas de possibilite, on sort avec erreur |
926 | else{ |
927 | cout<<"erreur Spliter : pas de chainage du wire"<<endl; |
253881cf |
928 | Standard_ConstructionError::Raise(); |
7fd59977 |
929 | } |
930 | //fin MODIF. |
931 | |
932 | tol1 = Max(BAS.UResolution(tol1), BAS.VResolution(tol1)); |
933 | |
7fd59977 |
934 | } |
935 | |
253881cf |
936 | TopTools_ListIteratorOfListOfShape lexp(WiresFirst); |
937 | for (; lexp.More(); lexp.Next()) { |
938 | const TopoDS_Edge& edg = TopoDS::Edge(lexp.Value()); |
7fd59977 |
939 | if (!MapE.Contains(edg)) { |
940 | B.Add(newW2,edg); |
941 | MapE.Add(edg); |
942 | } |
253881cf |
943 | } |
944 | |
7fd59977 |
945 | TopoDS_Face newF1,newF2; |
946 | aLocalFace = FaceRef.EmptyCopied(); |
947 | newF1 = TopoDS::Face(aLocalFace); |
948 | newF1.Orientation(TopAbs_FORWARD); |
949 | aLocalFace = FaceRef.EmptyCopied(); |
950 | newF2 = TopoDS::Face(aLocalFace); |
7fd59977 |
951 | newF2.Orientation(TopAbs_FORWARD); |
952 | |
953 | // modifs JAG 97.05.28 |
954 | #ifdef DEB |
955 | TopAbs_Orientation orfila; |
956 | #else |
957 | TopAbs_Orientation orfila=TopAbs_FORWARD; |
958 | #endif |
959 | for (exp.Init(FaceRef.Oriented(TopAbs_FORWARD),TopAbs_WIRE); |
960 | exp.More(); exp.Next()) { |
961 | const TopoDS_Wire& wir = TopoDS::Wire(exp.Current()); |
962 | if (wir.IsSame(wfirst)) { |
963 | orfila = exp.Current().Orientation(); |
964 | break; |
965 | } |
966 | } |
967 | |
253881cf |
968 | newW1.Oriented(orfila); |
7c104885 |
969 | newW2.Oriented(orfila); |
970 | |
253881cf |
971 | B.Add(newF1,newW1); |
7c104885 |
972 | BRepTools::Write(newF1, "k:/queries/WrongBOP/NewF1.brep"); |
253881cf |
973 | B.Add(newF2,newW2); |
7c104885 |
974 | BRepTools::Write(newF2, "k:/queries/WrongBOP/NewF2.brep"); |
7fd59977 |
975 | |
976 | for (exp.ReInit(); exp.More(); exp.Next()) { |
977 | const TopoDS_Wire& wir = TopoDS::Wire(exp.Current()); |
978 | if (!wir.IsSame(wfirst)) { |
7fd59977 |
979 | if (IsInside(newF1, wir)) { |
980 | B.Add(newF1,wir); |
981 | } |
982 | else if (IsInside(newF2, wir)) { |
983 | B.Add(newF2,wir); |
984 | } |
985 | else { |
986 | // Ce wire est ni dans newF2 ni dans newF1 |
987 | // Peut etre faut il construire une troisieme face |
988 | cout << "WARNING: LocOpe_SPlitShape : Ce wire est ni dans newF2 ni dans newF1" << endl; |
989 | } |
990 | } |
991 | } |
992 | lf.Append(newF1); |
993 | lf.Append(newF2); |
994 | |
995 | // Mise a jour des descendants des wires |
996 | for (exp.Init(F,TopAbs_WIRE); exp.More(); exp.Next()) { |
997 | TopTools_ListOfShape& ls = myMap(exp.Current()); |
998 | itl.Initialize(ls); |
999 | for (; itl.More(); itl.Next()) { |
1000 | if (itl.Value().IsSame(wfirst)) { |
1001 | break; |
1002 | } |
1003 | } |
1004 | if (itl.More()) { // on a trouve le wire |
1005 | ls.Remove(itl); |
1006 | ls.Append(newW1); |
1007 | ls.Append(newW2); |
1008 | } |
1009 | } |
1010 | } |
1011 | else { |
1012 | // on ne cree qu`une seule face |
1013 | TopoDS_Wire outerW = BRepTools::OuterWire(FaceRef); |
1014 | TopoDS_Wire newWire; |
1015 | TopoDS_Face newFace; |
1016 | B.MakeWire(newWire); |
1017 | newWire.Orientation(TopAbs_FORWARD); |
1018 | TopAbs_Orientation orient,orRelat; |
1019 | |
1020 | if (wfirst.Orientation() == wlast.Orientation()) { |
1021 | orRelat = TopAbs_FORWARD; |
1022 | } |
1023 | else { |
1024 | orRelat = TopAbs_REVERSED; |
1025 | } |
1026 | |
1027 | if (wlast.IsSame(outerW)) { |
1028 | wlast = wfirst; |
1029 | wfirst = outerW; |
1030 | } |
1031 | |
1032 | // Edges de wfirst |
1033 | for (exp.Init(wfirst.Oriented(TopAbs_FORWARD),TopAbs_EDGE); |
1034 | exp.More(); exp.Next()) { |
1035 | B.Add(newWire,TopoDS::Edge(exp.Current())); |
1036 | } |
1037 | |
1038 | // Edges de wlast |
1039 | for (exp.Init(wlast.Oriented(TopAbs_FORWARD),TopAbs_EDGE); |
1040 | exp.More(); exp.Next()) { |
1041 | const TopoDS_Edge& edg = TopoDS::Edge(exp.Current()); |
1042 | orient = TopAbs::Compose(edg.Orientation(),orRelat); |
1043 | B.Add(newWire,edg.Oriented(orient)); |
1044 | } |
1045 | |
1046 | |
1047 | // Edges du wire ajoute, et dans les 2 sens |
1048 | for (exp.Init(W.Oriented(TopAbs_FORWARD),TopAbs_EDGE); |
1049 | exp.More(); exp.Next()) { |
1050 | const TopoDS_Edge& edg = TopoDS::Edge(exp.Current()); |
1051 | orient = edg.Orientation(); |
1052 | B.Add(newWire,edg.Oriented(orient)); |
1053 | B.Add(newWire,edg.Oriented(TopAbs::Reverse(orient))); |
1054 | myDblE.Add(edg.Oriented(orient)); |
1055 | } |
1056 | |
1057 | // on refait une face |
1058 | |
1059 | TopoDS_Shape aLocalFace = FaceRef.EmptyCopied(); |
1060 | newFace = TopoDS::Face(aLocalFace); |
1061 | // newFace = TopoDS::Face(FaceRef.EmptyCopied()); |
1062 | FaceRef.Orientation(TopAbs_FORWARD); |
1063 | for (exp.Init(FaceRef.Oriented(TopAbs_FORWARD),TopAbs_WIRE); |
1064 | exp.More(); exp.Next()) { |
1065 | const TopoDS_Wire& wir = TopoDS::Wire(exp.Current()); |
1066 | if (wir.IsSame(wfirst)) { |
1067 | B.Add(newFace,newWire.Oriented(wir.Orientation())); |
1068 | } |
1069 | else if (!wir.IsSame(wlast)) { |
1070 | B.Add(newFace,wir); |
1071 | } |
1072 | } |
1073 | lf.Append(newFace); |
1074 | |
1075 | // Mise a jour des descendants des wires |
1076 | for (exp.Init(F,TopAbs_WIRE); exp.More(); exp.Next()) { |
1077 | TopTools_ListOfShape& ls = myMap(exp.Current()); |
1078 | itl.Initialize(ls); |
1079 | Standard_Boolean touch = Standard_False; |
1080 | while (itl.More()) { |
1081 | if (itl.Value().IsSame(wfirst) || itl.Value().IsSame(wlast)) { |
1082 | ls.Remove(itl); |
1083 | touch = Standard_True; |
1084 | } |
1085 | else { |
1086 | itl.Next(); |
1087 | } |
1088 | } |
1089 | if (touch) { |
1090 | ls.Append(newWire); |
1091 | } |
1092 | |
1093 | } |
1094 | } |
1095 | } |
1096 | |
1097 | |
1098 | //======================================================================= |
1099 | //function : LeftOf |
1100 | //purpose : |
1101 | //======================================================================= |
1102 | |
1103 | const TopTools_ListOfShape& LocOpe_SplitShape::LeftOf(const TopoDS_Wire& W, |
1104 | const TopoDS_Face& F) |
1105 | { |
1106 | if (myShape.IsNull()) { |
1107 | Standard_NoSuchObject::Raise(); |
1108 | } |
1109 | |
1110 | TopExp_Explorer exp,expw,expf; |
1111 | exp.Init(myShape,TopAbs_FACE); |
1112 | for (; exp.More(); exp.Next()) { |
1113 | if (exp.Current().IsSame(F)) { |
1114 | break; |
1115 | } |
1116 | } |
1117 | if (!exp.More()) { |
1118 | Standard_NoSuchObject::Raise(); |
1119 | } |
1120 | myLeft.Clear(); |
1121 | |
1122 | const TopoDS_Face& theFace = TopoDS::Face(exp.Current()); |
1123 | TopAbs_Orientation orFace = theFace.Orientation(); |
1124 | TopTools_ListIteratorOfListOfShape itl,itl2; |
1125 | |
1126 | for (expw.Init(W,TopAbs_EDGE); expw.More(); expw.Next()) { |
1127 | const TopoDS_Edge& edg = TopoDS::Edge(expw.Current()); |
1128 | for (itl.Initialize(myMap(theFace)); itl.More(); itl.Next()) { |
1129 | TopoDS_Face fac = TopoDS::Face(itl.Value()); |
1130 | fac.Orientation(orFace); |
1131 | for (expf.Init(fac,TopAbs_EDGE); expf.More(); expf.Next()) { |
1132 | const TopoDS_Edge& edgbis = TopoDS::Edge(expf.Current()); |
1133 | if (edgbis.IsSame(edg) && |
1134 | edgbis.Orientation() == edg.Orientation()) { |
1135 | for (itl2.Initialize(myLeft); itl2.More(); itl2.Next()) { |
1136 | if (itl2.Value().IsSame(fac)) { |
1137 | break; |
1138 | } |
1139 | } |
1140 | if (!itl2.More()) { // la face n`est pas deja presente |
1141 | myLeft.Append(fac); |
1142 | } |
1143 | break; |
1144 | } |
1145 | } |
1146 | if (expf.More()) { // face found |
1147 | break; |
1148 | } |
1149 | } |
1150 | } |
1151 | return myLeft; |
1152 | } |
1153 | |
1154 | |
1155 | //======================================================================= |
1156 | //function : DescendantShapes |
1157 | //purpose : |
1158 | //======================================================================= |
1159 | |
1160 | const TopTools_ListOfShape& LocOpe_SplitShape::DescendantShapes |
1161 | (const TopoDS_Shape& S) |
1162 | { |
1163 | if (!myDone) { |
1164 | Rebuild(myShape); |
1165 | myDone = Standard_True; |
1166 | } |
1167 | #ifdef DEB |
1168 | if (!myDblE.IsEmpty()) { |
1169 | cout << "Le shape comporte des faces invalides" << endl; |
1170 | } |
1171 | #endif |
1172 | return myMap(S); |
1173 | } |
1174 | |
1175 | |
1176 | |
1177 | //======================================================================= |
1178 | //function : Put |
1179 | //purpose : |
1180 | //======================================================================= |
1181 | |
1182 | void LocOpe_SplitShape::Put(const TopoDS_Shape& S) |
1183 | { |
1184 | if (!myMap.IsBound(S)) { |
1185 | TopTools_ListOfShape thelist; |
1186 | myMap.Bind(S, thelist); |
1187 | if (S.ShapeType() != TopAbs_VERTEX) { |
1188 | for(TopoDS_Iterator theIterator(S);theIterator.More(); |
1189 | theIterator.Next()) { |
1190 | Put(theIterator.Value()); |
1191 | } |
1192 | } |
1193 | else { |
1194 | myMap(S).Append(S); |
1195 | } |
1196 | } |
1197 | } |
1198 | |
1199 | |
1200 | //======================================================================= |
1201 | //function : Rebuild |
1202 | //purpose : |
1203 | //======================================================================= |
1204 | |
1205 | Standard_Boolean LocOpe_SplitShape::Rebuild(const TopoDS_Shape& S) |
1206 | |
1207 | { |
1208 | |
1209 | TopTools_ListIteratorOfListOfShape itr(myMap(S)); |
1210 | if (itr.More()) { |
1211 | if (itr.Value().IsSame(S)) { |
1212 | return Standard_False; |
1213 | } |
1214 | return Standard_True; |
1215 | } |
1216 | Standard_Boolean rebuild = Standard_False; |
1217 | TopoDS_Iterator it; |
1218 | for(it.Initialize(S); it.More(); it.Next()) { |
1219 | rebuild = Rebuild(it.Value()) || rebuild; |
1220 | } |
1221 | |
1222 | if (rebuild) { |
1223 | BRep_Builder B; |
1224 | TopoDS_Shape result = S.EmptyCopied(); |
1225 | TopAbs_Orientation orient; |
1226 | for(it.Initialize(S); it.More(); it.Next()) { |
1227 | orient = it.Value().Orientation(); |
1228 | for (itr.Initialize(myMap(it.Value())); itr.More(); itr.Next()) { |
1229 | B.Add(result,itr.Value().Oriented(orient)); |
1230 | } |
1231 | } |
1232 | myMap(S).Append(result); |
1233 | } |
1234 | else { |
1235 | myMap(S).Append(S); |
1236 | } |
1237 | return rebuild; |
1238 | } |
1239 | |
1240 | |
1241 | |
1242 | //======================================================================= |
1243 | //function : IsInside |
1244 | //purpose : |
1245 | //======================================================================= |
1246 | |
1247 | static Standard_Boolean IsInside(const TopoDS_Face& F, |
1248 | const TopoDS_Wire& W1, |
1249 | const TopoDS_Wire& W2) |
1250 | { |
1251 | // Attention, c`est tres boeuf !!!! |
1252 | BRep_Builder B; |
1253 | TopoDS_Shape aLocalShape = F.EmptyCopied(); |
1254 | TopoDS_Face newFace = TopoDS::Face(aLocalShape); |
1255 | // TopoDS_Face newFace = TopoDS::Face(F.EmptyCopied()); |
6e6cd5d9 |
1256 | |
1257 | //TopAbs_Orientation orWire = W2.Orientation(); |
1258 | |
7fd59977 |
1259 | newFace.Orientation(TopAbs_FORWARD); |
1260 | B.Add(newFace,W2); |
1261 | // GProp_GProps GP; |
1262 | // BRepGProp::SurfaceProperties(newFace,GP); |
1263 | // if (GP.Mass() < 0) { |
1264 | BRepTopAdaptor_FClass2d classif(newFace,Precision::PConfusion()); |
1265 | Standard_Boolean Reversed = Standard_False; |
1266 | if (classif.PerformInfinitePoint() == TopAbs_IN) { |
1267 | //le wire donne defini un trou |
1268 | // newFace = TopoDS::Face(F.EmptyCopied()); |
1269 | // newFace.Orientation(TopAbs_FORWARD); |
1270 | // orWire = TopAbs::Reverse(orWire); |
1271 | // B.Add(newFace,W2.Oriented(orWire)); |
1272 | Reversed = Standard_True; |
1273 | } |
1274 | |
1275 | // Standard_Real U,V; |
1276 | TopExp_Explorer exp(W1,TopAbs_EDGE); |
1277 | const TopoDS_Edge& edg = TopoDS::Edge(exp.Current()); |
1278 | TopExp_Explorer exp2(edg,TopAbs_VERTEX); |
1279 | const TopoDS_Vertex& vtx = TopoDS::Vertex(exp2.Current()); |
1280 | Standard_Real prm = BRep_Tool::Parameter(vtx,edg); |
1281 | Standard_Real f,l; |
1282 | Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(edg,F,f,l); |
1283 | if(C2d.IsNull()) { |
1284 | #ifdef DEB |
1285 | cout << "Edge is not on surface" <<endl; |
1286 | #endif |
1287 | return Standard_False; |
1288 | } |
1289 | gp_Pnt2d pt2d(C2d->Value(prm)); |
1290 | // BRepClass_FaceClassifier classif(newFace,pt2d,Precision::PConfusion()); |
1291 | // return (classif.State() == TopAbs_IN); |
1292 | if (!Reversed) { |
1293 | return (classif.Perform(pt2d) == TopAbs_IN); |
1294 | } |
1295 | else { |
1296 | return (classif.Perform(pt2d) == TopAbs_OUT); |
1297 | } |
1298 | } |
1299 | |
1300 | |
1301 | //======================================================================= |
1302 | //function : IsInside |
1303 | //purpose : |
1304 | //======================================================================= |
1305 | |
1306 | static Standard_Boolean IsInside(const TopoDS_Face& F, |
1307 | const TopoDS_Wire& W) |
1308 | { |
1309 | // Attention, c`est tres boeuf !!!! |
1310 | TopExp_Explorer exp(W,TopAbs_EDGE); |
1311 | for( ; exp.More(); exp.Next()) { |
1312 | const TopoDS_Edge& edg = TopoDS::Edge(exp.Current()); |
1313 | // TopExp_Explorer exp2(edg,TopAbs_VERTEX); |
1314 | // const TopoDS_Vertex& vtx = TopoDS::Vertex(exp2.Current()); |
1315 | // Standard_Real prm = BRep_Tool::Parameter(vtx,edg); |
1316 | Standard_Real f,l,prm; |
1317 | Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(edg,F,f,l); |
1318 | if (!Precision::IsNegativeInfinite(f) && |
1319 | !Precision::IsPositiveInfinite(l)) { |
1320 | prm = (f+l)/2.; |
1321 | } |
1322 | else { |
1323 | if (Precision::IsNegativeInfinite(f) && |
1324 | Precision::IsPositiveInfinite(l)){ |
1325 | prm = 0.; |
1326 | } |
1327 | else if (Precision::IsNegativeInfinite(f)) { |
1328 | prm = l-1.; |
1329 | } |
1330 | else { |
1331 | prm = f+1.; |
1332 | } |
1333 | } |
1334 | |
1335 | gp_Pnt2d pt2d(C2d->Value(prm)); |
1336 | // BRepClass_FaceClassifier classif(F,pt2d,Precision::PConfusion()); |
1337 | // return (classif.State() != TopAbs_OUT); |
1338 | BRepTopAdaptor_FClass2d classif(F,Precision::PConfusion()); |
ed60a55e |
1339 | TopAbs_State stat = classif.Perform(pt2d); |
7fd59977 |
1340 | // return (classif.Perform(pt2d) != TopAbs_OUT); |
1341 | if(stat == TopAbs_OUT) return Standard_False; |
1342 | |
1343 | if(stat == TopAbs_ON) { |
1344 | Standard_Integer nbPnt =10; |
1345 | Standard_Integer nbOut =0,nbIn =0,nbOn=0; |
1346 | Standard_Integer j =1; |
1347 | for( ; j<= nbPnt ; j++) |
1348 | { |
1349 | //check neighbouring point |
1350 | //prm = .66 * prm + .34 * l; |
1351 | prm = f + (l-f)/nbPnt*(j-1); |
1352 | pt2d = C2d->Value(prm); |
1353 | stat = classif.Perform(pt2d); |
1354 | if(stat == TopAbs_OUT ) |
1355 | nbOut++; |
1356 | else if(stat == TopAbs_IN) |
1357 | nbIn++; |
1358 | else |
1359 | nbOn++; |
1360 | } |
1361 | if(nbOut > nbIn + nbOn) |
1362 | return Standard_False; |
1363 | } |
1364 | } |
1365 | return Standard_True; |
1366 | } |
1367 | |
1368 | |
1369 | //======================================================================= |
1370 | //function : ChoixUV |
1371 | //purpose : |
1372 | //======================================================================= |
1373 | |
1374 | static void ChoixUV(const TopoDS_Edge& Last, |
1375 | const TopoDS_Face& F, |
1376 | const TopTools_MapOfShape& Poss, |
1377 | TopTools_MapIteratorOfMapOfShape& It, |
1378 | gp_Pnt2d& plst, |
1379 | gp_Vec2d& dlst, |
1380 | const Standard_Real toll) |
1381 | { |
1382 | |
1383 | Standard_Real f,l; |
7fd59977 |
1384 | gp_Pnt2d p2d; |
1385 | gp_Vec2d v2d; |
c3297e82 |
1386 | gp_Pnt aPCur, aPlst; |
7fd59977 |
1387 | |
1388 | BRepAdaptor_Surface surf(F,Standard_False); // no restriction |
c3297e82 |
1389 | surf.D0 (plst.X(), plst.Y(), aPlst); |
7fd59977 |
1390 | |
1391 | Standard_Real tol; |
1392 | |
1393 | TopoDS_Vertex vtx; |
1394 | |
1395 | gp_Dir2d ref2d(dlst); |
1396 | |
1397 | Handle(Geom2d_Curve) C2d; |
7c104885 |
1398 | Standard_Real dpar; |
7fd59977 |
1399 | |
1400 | Standard_Integer index = 0, imin=0; |
c6541a0c |
1401 | Standard_Real angmax = -M_PI, dist, ang; |
7fd59977 |
1402 | |
1403 | |
1404 | for (It.Initialize(Poss); It.More(); It.Next()) { |
1405 | index++; |
1406 | C2d = BRep_Tool::CurveOnSurface(TopoDS::Edge(It.Key()),F,f,l); |
7c104885 |
1407 | dpar = (l - f)*0.01; |
7fd59977 |
1408 | if (It.Key().Orientation() == TopAbs_FORWARD) { |
1409 | // p2d = C2d->Value(f); |
1410 | C2d->D1(f,p2d,v2d); |
7c104885 |
1411 | if (v2d.Magnitude() < gp::Resolution()) |
1412 | { |
1413 | gp_Pnt2d NextPnt = C2d->Value(f + dpar); |
1414 | v2d.SetXY(NextPnt.XY() - p2d.XY()); |
1415 | } |
7fd59977 |
1416 | vtx = TopExp::FirstVertex(TopoDS::Edge(It.Key())); |
1417 | } |
1418 | else { |
1419 | // p2d = C2d->Value(l); |
1420 | C2d->D1(l,p2d,v2d); |
7c104885 |
1421 | if (v2d.Magnitude() < gp::Resolution()) |
1422 | { |
1423 | gp_Pnt2d PrevPnt = C2d->Value(l - dpar); |
1424 | v2d.SetXY(p2d.XY() - PrevPnt.XY()); |
1425 | } |
7fd59977 |
1426 | v2d.Reverse(); |
1427 | vtx = TopExp::LastVertex(TopoDS::Edge(It.Key())); |
1428 | } |
1429 | |
c3297e82 |
1430 | surf.D0 (p2d.X(), p2d.Y(), aPCur); |
7fd59977 |
1431 | |
1432 | tol = BRep_Tool::Tolerance(vtx); |
7fd59977 |
1433 | tol = Max(toll, tol); tol *= tol; |
1434 | |
c3297e82 |
1435 | dist = aPCur.SquareDistance(aPlst); |
7fd59977 |
1436 | |
1437 | if (!Last.IsSame(It.Key())) { |
1438 | ang = ref2d.Angle(gp_Dir2d(v2d)); |
1439 | } |
1440 | else { |
c6541a0c |
1441 | ang = -M_PI; |
7fd59977 |
1442 | } |
1443 | |
1444 | //if ((dist < dmin - tol) || |
1445 | //(dist <= dmin+tol && ang > angmax)) { |
1446 | if ((dist < tol) && (ang > angmax)) { |
1447 | imin = index; |
1448 | // dmin = dist; |
1449 | angmax = ang; |
1450 | } |
1451 | } |
1452 | |
1453 | for (index = 1, It.Initialize(Poss); It.More(); It.Next()) { |
1454 | if (index == imin) { |
1455 | C2d = BRep_Tool::CurveOnSurface(TopoDS::Edge(It.Key()),F,f,l); |
7c104885 |
1456 | dpar = (l - f)*0.01; |
7fd59977 |
1457 | if (It.Key().Orientation() == TopAbs_FORWARD) { |
1458 | // plst = C2d->Value(l); |
1459 | C2d->D1(l,plst,dlst); |
7c104885 |
1460 | if (dlst.Magnitude() < gp::Resolution()) |
1461 | { |
1462 | gp_Pnt2d PrevPnt = C2d->Value(l - dpar); |
1463 | dlst.SetXY(plst.XY() - PrevPnt.XY()); |
1464 | } |
7fd59977 |
1465 | } |
1466 | else { |
1467 | // plst = C2d->Value(f); |
1468 | C2d->D1(f,plst,dlst); |
7c104885 |
1469 | if (dlst.Magnitude() < gp::Resolution()) |
1470 | { |
1471 | gp_Pnt2d NextPnt = C2d->Value(f + dpar); |
1472 | dlst.SetXY(NextPnt.XY() - plst.XY()); |
1473 | } |
7fd59977 |
1474 | dlst.Reverse(); |
1475 | } |
1476 | break; |
1477 | } |
1478 | index++; |
1479 | } |
1480 | |
1481 | } |
ed60a55e |
1482 | |
1483 | //======================================================================= |
1484 | //function : ChooseDirection |
1485 | //purpose : |
1486 | //======================================================================= |
1487 | |
1488 | static TopoDS_Shape ChooseDirection(const TopoDS_Shape& RefDir, |
1489 | const TopoDS_Vertex& RefVertex, |
1490 | const TopoDS_Face& theFace, |
1491 | const TopTools_ListOfShape& Ldirs) |
1492 | { |
1493 | TopExp_Explorer Explo(RefDir, TopAbs_EDGE); |
1494 | TopoDS_Edge RefEdge; |
1495 | TopoDS_Vertex V1, V2; |
1496 | TopAbs_Orientation anOr; |
1497 | for (; Explo.More(); Explo.Next()) |
1498 | { |
1499 | RefEdge = TopoDS::Edge(Explo.Current()); |
1500 | TopExp::Vertices(RefEdge, V1, V2); |
1501 | if (V1.IsSame(RefVertex)) |
1502 | { |
1503 | anOr = TopAbs_REVERSED; |
1504 | break; |
1505 | } |
1506 | else if (V2.IsSame(RefVertex)) |
1507 | { |
1508 | anOr = TopAbs_FORWARD; |
1509 | break; |
1510 | } |
1511 | } |
1512 | |
1513 | Standard_Real RefFirst, RefLast; |
1514 | Handle(Geom2d_Curve) RefCurve = BRep_Tool::CurveOnSurface(RefEdge, theFace, RefFirst, RefLast); |
1515 | gp_Pnt2d RefPnt; |
1516 | gp_Vec2d RefVec; |
1517 | |
1518 | //Standard_Real RefPar = (RefEdge.Orientation() == TopAbs_FORWARD)? RefLast : RefFirst; |
1519 | Standard_Real RefPar = (anOr == TopAbs_FORWARD)? RefLast : RefFirst; |
1520 | RefCurve->D1(RefPar, RefPnt, RefVec); |
1521 | if (anOr == TopAbs_FORWARD) |
1522 | RefVec.Reverse(); |
1523 | |
1524 | Handle(Geom2d_Curve) aCurve; |
1525 | Standard_Real aFirst, aLast, aPar; |
1526 | gp_Vec2d aVec; |
1527 | Standard_Real MinAngle = RealLast(), anAngle; |
1528 | TopoDS_Shape TargetDir; |
1529 | TopTools_ListIteratorOfListOfShape itl(Ldirs); |
1530 | for (; itl.More(); itl.Next()) |
1531 | { |
1532 | const TopoDS_Shape& aShape = itl.Value(); |
1533 | TopoDS_Edge anEdge; |
1534 | for (Explo.Init(aShape, TopAbs_EDGE); Explo.More(); Explo.Next()) |
1535 | { |
1536 | anEdge = TopoDS::Edge(Explo.Current()); |
1537 | TopExp::Vertices(anEdge, V1, V2); |
1538 | if (V1.IsSame(RefVertex)) |
1539 | { |
1540 | anOr = TopAbs_FORWARD; |
1541 | break; |
1542 | } |
1543 | else if (V2.IsSame(RefVertex)) |
1544 | { |
1545 | anOr = TopAbs_REVERSED; |
1546 | break; |
1547 | } |
1548 | } |
1549 | aCurve = BRep_Tool::CurveOnSurface(anEdge, theFace, aFirst, aLast); |
1550 | aPar = (anOr == TopAbs_FORWARD)? aFirst : aLast; |
1551 | aCurve->D1(aPar, RefPnt, aVec); |
1552 | if (anOr == TopAbs_REVERSED) |
1553 | aVec.Reverse(); |
1554 | anAngle = aVec.Angle(RefVec); |
1555 | if (anAngle < 0.) |
1556 | anAngle += 2.*M_PI; |
1557 | |
1558 | if (anAngle < MinAngle) |
1559 | { |
1560 | MinAngle = anAngle; |
1561 | TargetDir = aShape; |
1562 | } |
1563 | } |
1564 | |
1565 | return TargetDir; |
1566 | } |