1 // Created on: 2000-01-26
2 // Created by: Peter KURNEV
3 // Copyright (c) 2000-2012 OPEN CASCADE SAS
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.
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.
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.
22 #include <TopOpeBRepBuild_CorrectFace2d.ixx>
25 #include <TopoDS_Shape.hxx>
26 #include <TopoDS_Wire.hxx>
27 #include <TopoDS_Vertex.hxx>
28 #include <TopoDS_Wire.hxx>
29 #include <TopoDS_Edge.hxx>
33 #include <TopLoc_Location.hxx>
34 #include <TopExp_Explorer.hxx>
35 #include <TopOpeBRepBuild_Tools2d.hxx>
37 #include <TopTools_ListOfShape.hxx>
38 #include <TopTools_ListIteratorOfListOfShape.hxx>
39 #include <TopTools_IndexedMapOfShape.hxx>
40 #include <TopTools_SequenceOfShape.hxx>
41 #include <TopTools_IndexedDataMapOfShapeShape.hxx>
43 #include <Geom2d_TrimmedCurve.hxx>
44 #include <Geom_Surface.hxx>
45 #include <Geom2d_Curve.hxx>
47 #include <BRepTopAdaptor_FClass2d.hxx>
48 #include <BRepAdaptor_Curve2d.hxx>
49 #include <BRep_Tool.hxx>
50 #include <BRep_Builder.hxx>
52 #include <Bnd_Box2d.hxx>
53 #include <BndLib_Add2dCurve.hxx>
56 #include <Precision.hxx>
57 #include <BRepAdaptor_Surface.hxx>
60 //=======================================================================
61 // function :TopOpeBRepBuild_CorrectFace2d::TopOpeBRepBuild_CorrectFace2d
63 //=======================================================================
64 TopOpeBRepBuild_CorrectFace2d::TopOpeBRepBuild_CorrectFace2d()
66 myIsDone=Standard_False;
70 //=======================================================================
71 // function :TopOpeBRepBuild_CorrectFace2d::TopOpeBRepBuild_CorrectFace2d
73 //=======================================================================
74 TopOpeBRepBuild_CorrectFace2d::TopOpeBRepBuild_CorrectFace2d(const TopoDS_Face& aFace,
75 const TopTools_IndexedMapOfOrientedShape& anAvoidMap,
76 TopTools_IndexedDataMapOfShapeShape& aMap)
79 myAvoidMap=anAvoidMap;
80 myIsDone=Standard_False;
82 myMap=(Standard_Address) &aMap;
85 //=======================================================================
86 // function : TopOpeBRepBuild_CorrectFace2d::SetMapOfTrans2dInfo
88 //=======================================================================
89 void TopOpeBRepBuild_CorrectFace2d::SetMapOfTrans2dInfo (TopTools_IndexedDataMapOfShapeShape& aMap)
91 myMap=(Standard_Address) &aMap;
94 //=======================================================================
95 // function : TopOpeBRepBuild_CorrectFace2d::MapOfTrans2dInfo
97 //=======================================================================
98 TopTools_IndexedDataMapOfShapeShape& TopOpeBRepBuild_CorrectFace2d::MapOfTrans2dInfo ()
100 return *(TopTools_IndexedDataMapOfShapeShape*) myMap;
102 //=======================================================================
103 // function :TopOpeBRepBuild_CorrectFace2d::Face
105 //=======================================================================
106 const TopoDS_Face& TopOpeBRepBuild_CorrectFace2d::Face() const
111 //=======================================================================
112 // function :TopOpeBRepBuild_CorrectFace2d::IsDone
114 //=======================================================================
115 Standard_Boolean TopOpeBRepBuild_CorrectFace2d::IsDone() const
119 //=======================================================================
120 // function :TopOpeBRepBuild_CorrectFace2d::ErrorStatus
122 //=======================================================================
123 Standard_Integer TopOpeBRepBuild_CorrectFace2d::ErrorStatus() const
125 return myErrorStatus;
127 //=======================================================================
128 // function :TopOpeBRepBuild_CorrectFace2d::CorrectedFace
130 //=======================================================================
131 const TopoDS_Face& TopOpeBRepBuild_CorrectFace2d::CorrectedFace() const
133 return myCorrectedFace;
135 //=======================================================================
136 // function :TopOpeBRepBuild_CorrectFace2d::BuildCopyData
138 //=======================================================================
139 void TopOpeBRepBuild_CorrectFace2d::BuildCopyData(const TopoDS_Face& aFace,
140 const TopTools_IndexedMapOfOrientedShape& anAvoidMap,
141 TopoDS_Face& aCopyFace,
142 TopTools_IndexedMapOfOrientedShape& aCopyAvoidMap,
143 const Standard_Boolean aNeedToUsePMap)
145 TopTools_IndexedDataMapOfShapeShape EdMap;
149 TopTools_IndexedDataMapOfShapeShape* pMap=
150 (TopTools_IndexedDataMapOfShapeShape*) myMap;
152 // 1. Copy myFace =>> myCopyFace
153 TopoDS_Shape aLocalShape = aFace.EmptyCopied();
154 aCopyFace=TopoDS::Face(aLocalShape);
155 // aCopyFace=TopoDS::Face(aFace.EmptyCopied());
157 TopExp_Explorer anExpWires(aFace, TopAbs_WIRE);
158 for (; anExpWires.More(); anExpWires.Next()) {
159 const TopoDS_Wire& W=TopoDS::Wire(anExpWires.Current());
161 aLocalShape = W.EmptyCopied();
162 TopoDS_Wire aWire = TopoDS::Wire(aLocalShape);
163 // TopoDS_Wire aWire = TopoDS::Wire(W.EmptyCopied());
165 TopExp_Explorer anExpEdges(W, TopAbs_EDGE);
166 for (; anExpEdges.More(); anExpEdges.Next()) {
167 const TopoDS_Edge& E = TopoDS::Edge(anExpEdges.Current());
169 if (EdMap.Contains(E)) {
170 TopoDS_Shape anEdge = EdMap.ChangeFromKey(E);
171 anEdge.Orientation(E.Orientation());
172 BB.Add (aWire, anEdge);
176 if (aNeedToUsePMap) {
177 if (pMap->Contains(E)) {
178 TopoDS_Shape& anEdge=pMap->ChangeFromKey(E);
179 anEdge.Orientation(E.Orientation());
180 EdMap.Add(E, anEdge);
181 BB.Add (aWire, anEdge);
187 aLocalShape = E.EmptyCopied();
188 TopoDS_Shape anEdge = TopoDS::Edge(aLocalShape);
189 // TopoDS_Shape anEdge = TopoDS::Edge(E.EmptyCopied());
191 EdMap.Add(E, anEdge);
193 TopExp_Explorer anExpVertices(E, TopAbs_VERTEX);
194 for (; anExpVertices.More(); anExpVertices.Next()) {
195 const TopoDS_Shape& aV=anExpVertices.Current();
199 anEdge.Orientation(E.Orientation());
200 BB.Add (aWire, anEdge);
203 aWire.Orientation(W.Orientation());
206 BB.Add (aCopyFace, aWire);
210 // 2. Copy myAvoidMap =>> myCopyAvoidMap
211 Standard_Integer i, aNb;
212 aNb=anAvoidMap.Extent();
214 for (i=1; i<=aNb; i++) {
215 const TopoDS_Shape& aSh=anAvoidMap(i);
217 if (EdMap.Contains (aSh)) {
218 TopoDS_Shape& aCopyShape=EdMap.ChangeFromKey(aSh);
219 aCopyShape.Orientation(aSh.Orientation());
220 aCopyAvoidMap.Add(aCopyShape);
226 if (aNeedToUsePMap) {
228 myEdMapInversed.Clear();
229 for (i=1; i<=aNb; i++) {
230 const TopoDS_Shape& aSh =EdMap.FindKey(i);
231 const TopoDS_Shape& aShCopy=EdMap.FindFromIndex(i);
232 myEdMapInversed.Add (aShCopy, aSh);
236 //=======================================================================
237 // function :TopOpeBRepBuild_CorrectFace2d::CheckFace
239 //=======================================================================
240 void TopOpeBRepBuild_CorrectFace2d::CheckFace()
243 // I. Is the surface periodic
244 TopLoc_Location aLocF;
245 Handle(Geom_Surface) Surf = BRep_Tool::Surface(myCopyFace, aLocF);
246 if (!(Surf->IsUPeriodic() || Surf->IsVPeriodic())) {
247 myIsDone=Standard_True;
252 //modified by NIZHNY-MZV Mon Apr 24 11:29:56 2000
253 //don't treat torus surfaces
254 if(Surf->IsUPeriodic() && Surf->IsVPeriodic()) {
255 myIsDone=Standard_True;
261 // II. Has each wire at least one "licit" edge
262 TopTools_IndexedMapOfOrientedShape aVoidWires;
263 Standard_Integer i, aNbVoidWires, anEdgeExists=0, priz;
264 TopExp_Explorer aFExp,aWExp;
265 aFExp.Init (myCopyFace, TopAbs_WIRE);
266 for (; aFExp.More(); aFExp.Next()) {
267 const TopoDS_Shape& aWire=aFExp.Current();
268 if (myCopyAvoidMap.Contains(aWire)) {
274 aWExp.Init(aWire, TopAbs_EDGE);
275 for (; aWExp.More(); aWExp.Next()) {
276 TopoDS_Shape anEdge=aWExp.Current();
277 if (myCopyAvoidMap.Contains(anEdge)) {
284 // This is the wire without any "Right" information
285 aVoidWires.Add(aWire);
290 // We assume the first edge from the wire is non-movable edge
292 aNbVoidWires=aVoidWires.Extent();
293 for (i=1; i<=aNbVoidWires; i++) {
294 TopTools_IndexedMapOfShape aEM;
295 TopExp::MapShapes(aVoidWires(i), TopAbs_EDGE, aEM);
297 myCopyAvoidMap.Add (aEM(1));
300 // III. Check all wires to know whether they are closed or not
301 aFExp.Init (myCopyFace, TopAbs_WIRE);
302 for (; aFExp.More(); aFExp.Next()) {
303 myCurrentWire = TopoDS::Wire(aFExp.Current());
304 priz=MakeRightWire ();
306 // This myFace contais a wire (myCurrentWire) that is not closed.
307 myIsDone=Standard_True;
314 // VI. Check connectability wires in 2d
315 TopoDS_Face aCopyFace;
316 TopTools_IndexedMapOfOrientedShape aCopyAvoidMap;
319 BuildCopyData(myCopyFace, myCopyAvoidMap, aCopyFace, aCopyAvoidMap, Standard_False);
321 aFExp.Init (aCopyFace, TopAbs_WIRE);
322 for (; aFExp.More(); aFExp.Next()) {
323 myCurrentWire = TopoDS::Wire(aFExp.Current());
324 if (!aCopyAvoidMap.Contains(myCurrentWire)) {
325 priz=ConnectWire (aCopyFace, aCopyAvoidMap, Standard_True);
327 myIsDone=Standard_True;
334 // Face seems to be OK
336 //=======================================================================
337 // function : TopOpeBRepBuild_CorrectFace2d::ConnectWire
339 //=======================================================================
340 Standard_Integer TopOpeBRepBuild_CorrectFace2d::ConnectWire (TopoDS_Face& aCopyFace,
341 const TopTools_IndexedMapOfOrientedShape& aCopyAvoidMap,
342 const Standard_Boolean aTryFlag)
346 // 1.Make right order
347 Standard_Integer priz;
348 priz=MakeRightWire ();
349 if (priz) return priz;
351 // 2. Define the First Edge on the Wire from aCopyAvoidMap
352 Standard_Integer i, aNbEdges=0, aNbAvoidEdgesOnWire;
353 Standard_Boolean IsAllAvoidEdgesDegenerated=Standard_False;
354 Standard_Real aDMax=0, aR;
356 TopExp_Explorer aWExp;
358 TopoDS_Shape aFirstEdge;
359 TopTools_IndexedMapOfOrientedShape anAvoidMap;
361 aWExp.Init(myCurrentWire, TopAbs_EDGE);
362 for (; aWExp.More(); aWExp.Next(), aNbEdges++) {
363 TopoDS_Shape anEdge=aWExp.Current();
364 if (aCopyAvoidMap.Contains(anEdge))
365 anAvoidMap.Add(anEdge);
368 if (aNbEdges==1) // nothing to do with this wire.
371 aNbAvoidEdgesOnWire=anAvoidMap.Extent();
372 if (aNbAvoidEdgesOnWire==aNbEdges) // nothing to do. all edges on wire are good.
375 // if at least one non-degenerated edge exists
376 // among anAvoidMap set it as aFirstEdge
378 for (i=1; i<=aNbAvoidEdgesOnWire; i++) {
379 const TopoDS_Edge& anEdge=TopoDS::Edge(anAvoidMap(i));
380 if (!BRep_Tool::Degenerated(anEdge)) {
389 // All of anAvoidMap edges are degenerated
390 // So take the edge with max dist. between
391 //First and Last 2d points as the First edge
392 IsAllAvoidEdgesDegenerated=Standard_True;
393 //if(aNbAvoidEdgesOnWire != 1)
394 // return 1; //in case of several degenerated edges we cannot connect wire by right way
395 for (i=1; i<=aNbAvoidEdgesOnWire; i++) {
397 const TopoDS_Edge& aDegenEdge=TopoDS::Edge(anAvoidMap(i));
398 GetP2dFL(aCopyFace, aDegenEdge, PF, PL);
400 if ((aR - aDMax) > 1e-7) {
402 aFirstEdge=anAvoidMap(i);
410 // 3. Build HeadList (from myOrderedWireList), where aFirstEdge will be the first
411 TopTools_ListOfShape HeadList;
412 MakeHeadList(aFirstEdge, HeadList);
414 // 4. Check HeadList to provide toward point-arrow direction
415 //modified by NIZNHY-PKV Mon Apr 24 14:43:20 2000 f
416 //CheckList (HeadList);
417 //modified by NIZNHY-PKV Tue Apr 25 12:08:29 2000CheckList (aCopyFace, HeadList);
418 //modified by NIZNHY-PKV Mon Apr 24 14:43:24 2000t
421 TopTools_IndexedMapOfShape anEdMap;
422 gp_Pnt2d PF, PL, PA, PB, PA1, PB1;
423 Handle(Geom2d_Curve) aTrCF, aTrCR, aTrC;
424 Standard_Real aD, aDTolerance=Precision::Confusion();
426 TopTools_SequenceOfShape aSeqEdges;
428 TopTools_ListIteratorOfListOfShape anIt;
429 anIt.Initialize(HeadList);
430 for (; anIt.More(); anIt.Next())
431 aSeqEdges.Append(anIt.Value());
432 aNbEdges=aSeqEdges.Length();
436 TopoDS_Edge aFstEdge=TopoDS::Edge(aSeqEdges(1));
437 GetP2dFL(aCopyFace, aFstEdge, PA, PB);
441 for (i=2; i<=aNbEdges; i++) {
442 TopoDS_Edge anEdge= TopoDS::Edge(aSeqEdges(i));
443 GetP2dFL(aCopyFace, anEdge, PF, PL);
446 if (aD<aDTolerance) {
448 continue; // nothing to do with the edge cose it already connected
451 // tralslation's vector
452 gp_Vec2d aTrV(PF, PB);
454 //Begin modified by NIZHNY-MZV Mon Mar 27 16:04:04 2000
455 //translation vector must be equal to 2PI*n or it is wrong wire
456 Standard_Real U = aTrV.X();
457 Standard_Real V = aTrV.Y();
459 BRepAdaptor_Surface BAS(aCopyFace);
460 Standard_Boolean UP = BAS.IsUPeriodic();
461 Standard_Boolean VP = BAS.IsVPeriodic();
463 Standard_Boolean nonPU = (fabs(U) < 1e-7) ? Standard_True : Standard_False;
464 Standard_Boolean nonPV = (fabs(V) < 1e-7) ? Standard_True : Standard_False;
467 Standard_Real dU = fmod(fabs(U), 2*M_PI);
468 nonPU = (dU > 1e-7 && (2*M_PI - dU > 1e-7)) ? Standard_True : Standard_False;
472 Standard_Real dV = fmod(fabs(V), 2*M_PI);
473 nonPV = (dV > 1e-7 && (2*M_PI - dV > 1e-7)) ? Standard_True : Standard_False;
476 // printf("(fmod(fabs(U), 2*M_PI) =%lf\n", (fmod(fabs(U), 2*M_PI)));
477 // printf(" (fmod(fabs(V), 2*M_PI) > 1e-7)=%lf\n", (fmod(fabs(V), 2*M_PI)));
479 if(nonPU && nonPV && !BRep_Tool::Degenerated(anEdge))
481 //End modified by NIZHNY-MZV Mon Mar 27 16:04:11 2000
483 if (BRep_Tool::IsClosed(anEdge, aCopyFace)) {
484 // a. Closed edge <--->
485 if (anEdMap.Contains(anEdge)) continue;
488 TopAbs_Orientation anOri = anEdge.Orientation();
490 TopoDS_Edge anEF, anER;
491 if (anOri==TopAbs_FORWARD) {
493 TopoDS_Shape aLocalShape = anEdge.Reversed();
494 anER=TopoDS::Edge(aLocalShape);
495 // anER=TopoDS::Edge(anEdge.Reversed());
500 TopoDS_Shape aLocalShape = anEdge.Reversed();
501 anEF=TopoDS::Edge(aLocalShape);
502 // anEF=TopoDS::Edge(anEdge.Reversed());
505 TranslateCurve2d (anEF, aCopyFace, aTrV, aTrCF);
506 TranslateCurve2d (anER, aCopyFace, aTrV, aTrCR);
508 if (aTryFlag) // Use Builder in a trying case
509 BB.UpdateEdge(anEdge, aTrCF, aTrCR, aCopyFace, myFaceTolerance);
511 else // Use "False-Builder" otherwise
512 UpdateEdge(anEdge, aTrCF, aTrCR, aCopyFace, myFaceTolerance);
518 TranslateCurve2d (anEdge, aCopyFace, aTrV, aTrC);
521 BB.UpdateEdge(anEdge, aTrC, aCopyFace, myFaceTolerance);
523 UpdateEdge(anEdge, aTrC, aCopyFace, myFaceTolerance);
526 GetP2dFL(aCopyFace, anEdge, PF, PL);
531 ////////////////////////////////////////////
532 // In case of a trying we check the first
533 // and last 2d point of the contour
541 ////////////////////////////////////////////
543 } //end of for (i=2; i<=aNbEdges; i++)
549 //=======================================================================
550 // function :TopOpeBRepBuild_CorrectFace2d::Perform
552 //=======================================================================
553 void TopOpeBRepBuild_CorrectFace2d::Perform()
557 myCopyAvoidMap.Clear();
558 BuildCopyData(myFace, myAvoidMap, myCopyFace, myCopyAvoidMap, Standard_True);
561 myCorrectedFace=myCopyFace;
562 myCorrectedFace.Orientation(myCopyFace.Orientation());
563 myFaceTolerance=BRep_Tool::Tolerance(myCopyFace);
565 // 1. Check the input face first
571 // 2. Make all wires connected
572 Standard_Integer priz;
573 TopExp_Explorer aFExp;
575 aFExp.Init (myCopyFace, TopAbs_WIRE);
576 for (; aFExp.More(); aFExp.Next()) {
577 myCurrentWire = TopoDS::Wire(aFExp.Current());
578 if (!myCopyAvoidMap.Contains(myCurrentWire)) {
579 priz=ConnectWire (myCopyFace, myCopyAvoidMap, Standard_False);
581 // This myFace contais a wire (myCurrentWire) that is not closed.
582 myIsDone=Standard_False;
589 // 3. Define Outer Wire
590 TopoDS_Wire anOuterWire;
591 priz=OuterWire (anOuterWire);
593 myIsDone=Standard_False;
594 myErrorStatus=5; // can't find outer wire
598 // 4. Moving the anOuterWire and other wires in 2d space
599 MoveWires2d(anOuterWire);
601 myIsDone=Standard_True;
605 //=======================================================================
606 // function : TopOpeBRepBuild_CorrectFace2d::MakeRightWire
608 //=======================================================================
609 Standard_Integer TopOpeBRepBuild_CorrectFace2d::MakeRightWire ()
611 Standard_Integer aNbEdgesReally=0;
612 myOrderedWireList.Clear();
614 //1. Real amount of the edges on aWire
615 TopExp_Explorer aWExp;
616 aWExp.Init(myCurrentWire, TopAbs_EDGE);
617 for (; aWExp.More(); aWExp.Next()) aNbEdgesReally++;
619 //2. We'll use TopOpeBRepBuild_Tools::Path
620 TopTools_ListOfShape aL;
621 TopOpeBRepBuild_Tools2d::Path (myCurrentWire, aL);
622 if (aL.Extent()!=aNbEdgesReally) {
627 //Begin modified by NIZNHY-PKV Tue Apr 25 12:04:45 2000
628 //from path we obtained list in reverse order, so to have right wire
629 //we need to reverse it
630 TopTools_ListOfShape aFL;
631 TopTools_ListIteratorOfListOfShape lit(aL);
632 for(; lit.More(); lit.Next())
633 aFL.Prepend(lit.Value());
635 myOrderedWireList=aFL;
636 //End modified by NIZNHY-PKV Tue Apr 25 12:06:45 2000
640 //=======================================================================
641 // function : TopOpeBRepBuild_CorrectFace2d::GetP2dFL
643 //=======================================================================
644 void TopOpeBRepBuild_CorrectFace2d::GetP2dFL (const TopoDS_Face& aF, const TopoDS_Edge& anEdge,
645 gp_Pnt2d& P2dF, gp_Pnt2d& P2dL)
647 Standard_Real aFirst, aLast;
648 const Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface (anEdge, aF, aFirst, aLast);
649 C2d->D0 (aFirst, P2dF);
650 C2d->D0 (aLast , P2dL);
651 if (anEdge.Orientation()==TopAbs_REVERSED) {
653 P2dTmp=P2dF; P2dF=P2dL; P2dL=P2dTmp;
658 //=======================================================================
659 // function :TopOpeBRepBuild_CorrectFace2d::MakeHeadList
661 //=======================================================================
662 void TopOpeBRepBuild_CorrectFace2d::MakeHeadList(const TopoDS_Shape& aFirstEdge,
663 TopTools_ListOfShape& HeadList) const
665 TopoDS_Shape aFE = aFirstEdge;
666 TopTools_ListOfShape aTailList;
667 TopTools_ListIteratorOfListOfShape anIt;
668 Standard_Integer aFlag=0;
670 anIt.Initialize(myOrderedWireList);
671 for (; anIt.More(); anIt.Next()) {
672 const TopoDS_Shape& anEdge=anIt.Value();
673 //modified by NIZHNY-MZV Mon Mar 27 11:40:00 2000
674 if(aFE.IsNull() && !BRep_Tool::Degenerated(TopoDS::Edge(anEdge)))
676 if (anEdge==aFE) aFlag=1; //turn the switch ON
677 if (aFlag) HeadList.Append(anEdge);
680 anIt.Initialize(myOrderedWireList);
681 for (; anIt.More(); anIt.Next()) {
682 const TopoDS_Shape& anEdge=anIt.Value();
683 if (anEdge==aFE) break;
684 aTailList.Append(anEdge);
686 HeadList.Append(aTailList);
688 //=======================================================================
689 // function :TopOpeBRepBuild_CorrectFace2d::CheckList
691 //=======================================================================
692 void TopOpeBRepBuild_CorrectFace2d::CheckList (const TopoDS_Face& aFace,
693 TopTools_ListOfShape& HeadList)
695 TopAbs_Orientation r1, r2;
696 Standard_Real aDTolerance=Precision::Confusion();
697 TopTools_SequenceOfShape aSeq;
698 TopTools_ListIteratorOfListOfShape anIt(HeadList);
699 for (; anIt.More(); anIt.Next()) {
700 aSeq.Append(anIt.Value());
703 r1=aSeq(1).Orientation();
704 r2=aSeq(2).Orientation();
705 Standard_Integer i, aNb=aSeq.Length();
706 //modified by NIZNHY-PKV Mon Apr 24 14:43:57 2000f
707 Standard_Boolean aFirstCheck=Standard_False;
708 if (r1==TopAbs_REVERSED ) {
709 // when the orientation of the first edge is Revesed,
710 // and when everything in 2d is Ok :
711 // if at least one Forward edge exists in the List
712 // we consider that no necessity to reverse
713 // the list (aFirstCheck=Standard_True) .
716 gp_Pnt2d PF, PL, PA, PB;
719 TopoDS_Edge aFstEdge=TopoDS::Edge(aSeq(1));
720 GetP2dFL(aFace, aFstEdge, PA, PB);
721 for (i=2; i<=aNb; i++) {
722 TopoDS_Edge anEdge= TopoDS::Edge(aSeq(i));
723 GetP2dFL(aFace, anEdge, PF, PL);
726 if (aD<aDTolerance) {
728 if (anEdge.Orientation()==TopAbs_FORWARD) {
729 aFirstCheck=Standard_True;
735 //modified by NIZNHY-PKV Mon Apr 24 14:43:59 2000t
737 r1=aSeq(1).Orientation();
738 r2=aSeq(2).Orientation();
740 TopoDS_Vertex aV1R, aV2F;
742 TopoDS_Edge aFirstEdge=TopoDS::Edge(aSeq(1));
743 TopExp_Explorer anExp;
744 anExp.Init (aFirstEdge, TopAbs_VERTEX);
745 for (; anExp.More(); anExp.Next()) {
746 const TopoDS_Shape& aV1=anExp.Current();
747 if (aV1.Orientation()==TopAbs_REVERSED){
748 aV1R=TopoDS::Vertex(aV1);
753 TopoDS_Edge aSecondEdge=TopoDS::Edge(aSeq(2));
754 anExp.Init (aSecondEdge, TopAbs_VERTEX);
755 for (; anExp.More(); anExp.Next()) {
756 const TopoDS_Shape& aV1=anExp.Current();
757 if (aV1.Orientation()==TopAbs_FORWARD){
758 aV2F=TopoDS::Vertex(aV1);
764 P1=BRep_Tool::Pnt(aV1R);
765 P2=BRep_Tool::Pnt(aV2F);
768 //modified by NIZNHY-PKV Mon Apr 24 13:24:06 2000 f
769 Standard_Real dist=P1.Distance(P2);
771 (!(dist < aDTolerance) && r1==TopAbs_FORWARD)||
772 //(r1==TopAbs_REVERSED)) {
773 (!aFirstCheck && r1==TopAbs_REVERSED)
775 //modified by NIZNHY-PKV Mon Apr 24 14:28:06 2000 t
776 // We have to reverse the order in list
777 aSeq.Append(aFirstEdge);
780 for (i=aNb; i>1; i--) {
781 HeadList.Append(aSeq(i));
786 //=======================================================================
787 // function :TopOpeBRepBuild_CorrectFace2d::TranslateCurve2d
789 //=======================================================================
790 void TopOpeBRepBuild_CorrectFace2d::TranslateCurve2d(const TopoDS_Edge& anE,
791 const TopoDS_Face& aFace,
792 const gp_Vec2d& aTrV,
793 Handle(Geom2d_Curve)& aC2DOut)
795 Standard_Real aFirst, aLast;
796 Handle(Geom2d_Curve) C2d=BRep_Tool::CurveOnSurface (anE, aFace, aFirst, aLast);
797 Handle(Geom2d_Curve) aTrC;
798 aTrC = Handle(Geom2d_Curve)::DownCast(C2d->Copy());
799 Handle(Geom2d_TrimmedCurve) newC2D = new Geom2d_TrimmedCurve(aTrC, aFirst, aLast);
800 newC2D -> Translate(aTrV);
803 //=======================================================================
804 // function :TopOpeBRepBuild_CorrectFace2d::OuterWire
806 //=======================================================================
807 Standard_Integer TopOpeBRepBuild_CorrectFace2d::OuterWire(TopoDS_Wire& anOuterWire) const
810 Handle(Geom_Surface) Surf = BRep_Tool::Surface(TopoDS::Face(myCorrectedFace), Loc);
812 TopExp_Explorer ex(myCorrectedFace, TopAbs_WIRE);
813 for (; ex.More(); ex.Next()) {
814 const TopoDS_Wire& aWire=TopoDS::Wire(ex.Current());
818 BB.MakeFace(newFace, Surf, Loc, myFaceTolerance);
819 BB.Add(newFace, aWire);
821 BRepTopAdaptor_FClass2d aClass2d(newFace, myFaceTolerance);
822 TopAbs_State aState=aClass2d.PerformInfinitePoint();
823 if (aState==TopAbs_OUT) {
828 return 1; // such wire is not found
831 //=======================================================================
832 // function :TopOpeBRepBuild_CorrectFace2d::BndBoxWire
834 //=======================================================================
835 void TopOpeBRepBuild_CorrectFace2d::BndBoxWire(const TopoDS_Wire& aWire,
836 Bnd_Box2d& B2d) const
839 TopExp_Explorer aWEx (aWire, TopAbs_EDGE);
840 for (; aWEx.More(); aWEx.Next()) {
841 const TopoDS_Edge& anEdge = TopoDS::Edge(aWEx.Current());
842 Standard_Real aTolE = BRep_Tool::Tolerance(anEdge);
843 BRepAdaptor_Curve2d aBAC2d (anEdge,myCorrectedFace);
844 BndLib_Add2dCurve::Add(aBAC2d, aTolE, aB2d);
849 //=======================================================================
850 // function : TopOpeBRepBuild_CorrectFace2d::MoveWire2d
852 //=======================================================================
853 void TopOpeBRepBuild_CorrectFace2d::MoveWire2d (TopoDS_Wire& aWire,
854 const gp_Vec2d& aTrV)
856 if (aTrV.Magnitude()<Precision::Confusion())
859 Standard_Integer i, aNbEdges;
860 TopTools_SequenceOfShape aSeqEdges;
861 TopTools_IndexedMapOfShape anEdMap;
862 Handle(Geom2d_Curve) aTrCF, aTrCR, aTrC;
864 TopExp_Explorer aWExp;
866 aWExp.Init(aWire, TopAbs_EDGE);
867 for (; aWExp.More(); aWExp.Next()) {
868 aSeqEdges.Append(aWExp.Current());
870 aNbEdges=aSeqEdges.Length();
874 for (i=1; i<=aNbEdges; i++) {
875 TopoDS_Edge anEdge= TopoDS::Edge(aSeqEdges(i));
876 if (BRep_Tool::IsClosed(anEdge, myCorrectedFace)) {
877 // a. Closed edge <--->
878 if (anEdMap.Contains(anEdge)) continue;
881 TopAbs_Orientation anOri = anEdge.Orientation();
883 TopoDS_Edge anEF, anER;
885 if (anOri==TopAbs_FORWARD) {
887 TopoDS_Shape aLocalShape = anEdge.Reversed();
888 anER=TopoDS::Edge(aLocalShape);
889 // anER=TopoDS::Edge(anEdge.Reversed());
894 TopoDS_Shape aLocalShape = anEdge.Reversed();
895 anEF=TopoDS::Edge(aLocalShape);
896 // anEF=TopoDS::Edge(anEdge.Reversed());
899 TranslateCurve2d (anEF, myCorrectedFace, aTrV, aTrCF);
900 TranslateCurve2d (anER, myCorrectedFace, aTrV, aTrCR);
901 UpdateEdge(anEdge, aTrCF, aTrCR, myCorrectedFace, myFaceTolerance);
906 TranslateCurve2d (anEdge, myCorrectedFace, aTrV, aTrC);
907 UpdateEdge(anEdge, aTrC, myCorrectedFace, myFaceTolerance);
909 } //end of for (i=1; i<=aNbEdges; i++)
911 //=======================================================================
912 // function : TopOpeBRepBuild_CorrectFace2d::MoveWires2d
914 //=======================================================================
915 void TopOpeBRepBuild_CorrectFace2d::MoveWires2d (TopoDS_Wire& anOuterWire)
917 Bnd_Box2d B2d, B2dOuterWire;
918 Standard_Real OuterU1,OuterV1,OuterU2,OuterV2, x, a, TwoPI;
919 Standard_Integer b, k;
924 BndBoxWire(anOuterWire, B2dOuterWire);
925 B2dOuterWire.Get(OuterU1,OuterV1,OuterU2,OuterV2);
927 a=.5*(OuterU1+OuterU2);
928 b=Standard_Integer(-a/TwoPI);
932 // 1. Move the Outer Wire to [0, 2*PI]
933 aTrV.SetCoord (x, 0.);
934 MoveWire2d (anOuterWire, aTrV);
936 BndBoxWire(anOuterWire, B2dOuterWire);
937 B2dOuterWire.Get(OuterU1,OuterV1,OuterU2,OuterV2);
939 // 2. Move all other wires into bounding box of the Outer Wire
940 TopExp_Explorer aFExp;
941 aFExp.Init (myCorrectedFace, TopAbs_WIRE);
942 for (; aFExp.More(); aFExp.Next()) {
943 myCurrentWire = TopoDS::Wire(aFExp.Current());
944 if (myCurrentWire!=anOuterWire) {
945 BndBoxWire(myCurrentWire, B2d);
946 Standard_Real u1,v1,u2,v2;
947 B2d.Get(u1,v1,u2,v2);
949 if (B2d.IsOut(B2dOuterWire)) {
950 //printf(" Need to Move\n" );
952 b=Standard_Integer(-a/TwoPI);
955 aTrV.SetCoord (x, 0.);
956 MoveWire2d (myCurrentWire, aTrV);
961 //=======================================================================
962 // function : TopOpeBRepBuild_CorrectFace2d::UpdateEdge
964 //=======================================================================
965 void TopOpeBRepBuild_CorrectFace2d::UpdateEdge (const TopoDS_Edge& ECopy,
966 const Handle(Geom2d_Curve)& C,
967 const TopoDS_Face& F,
968 const Standard_Real Tol)
972 TopTools_IndexedDataMapOfShapeShape* pMap=
973 (TopTools_IndexedDataMapOfShapeShape*) myMap;
975 // E is the Original Edge from Original Face.
976 if (myEdMapInversed.Contains(ECopy)) {
977 const TopoDS_Shape& E=myEdMapInversed.FindFromKey(ECopy);
979 if (!pMap->Contains(E)) {
980 TopExp_Explorer anExp;
982 TopoDS_Shape anEdge=E.EmptyCopied();
984 anExp.Init(E, TopAbs_VERTEX);
985 for (; anExp.More(); anExp.Next()) {
986 const TopoDS_Shape& aV=anExp.Current();
990 BB.UpdateEdge(TopoDS::Edge(anEdge), C, F, Tol);
991 pMap->Add(E, anEdge);
992 //printf("pMap->Extent()=%d\n", pMap->Extent());
996 TopoDS_Shape& anEdge=pMap->ChangeFromKey(E);
997 BB.UpdateEdge(TopoDS::Edge(anEdge), C, F, Tol);
1001 BB.UpdateEdge (ECopy, C, F, Tol);
1005 //=======================================================================
1006 // function : TopOpeBRepBuild_CorrectFace2d::UpdateEdge
1008 //=======================================================================
1009 void TopOpeBRepBuild_CorrectFace2d::UpdateEdge (const TopoDS_Edge& ECopy,
1010 const Handle(Geom2d_Curve)& C1,
1011 const Handle(Geom2d_Curve)& C2,
1012 const TopoDS_Face& F,
1013 const Standard_Real Tol)
1017 TopTools_IndexedDataMapOfShapeShape* pMap=
1018 (TopTools_IndexedDataMapOfShapeShape*) myMap;
1020 // E is the Original Edge from Original Face.
1021 if (myEdMapInversed.Contains(ECopy)) {
1022 const TopoDS_Shape& E=myEdMapInversed.FindFromKey(ECopy);
1024 if (!pMap->Contains(E)) {
1025 TopoDS_Shape anEdge=E.EmptyCopied();
1026 TopExp_Explorer anExp;
1027 anExp.Init(E, TopAbs_VERTEX);
1028 for (; anExp.More(); anExp.Next()) {
1029 const TopoDS_Shape& aV=anExp.Current();
1032 BB.UpdateEdge(TopoDS::Edge(anEdge), C1, C2, F, Tol);
1033 pMap->Add(E, anEdge);
1034 //printf("pMap->Extent()=%d\n", pMap->Extent());
1038 TopoDS_Shape& anEdge=pMap->ChangeFromKey(E);
1039 BB.UpdateEdge(TopoDS::Edge(anEdge), C1, C2, F, Tol);
1043 BB.UpdateEdge (ECopy, C1, C2, F, Tol);
1047 // Description for ErrorStatus
1048 // ErrorStatus=0 : All is done;
1049 // ErrorStatus=1 : Nothing is done;
1050 // ErrorStatus=2 : The wire doesn't contain any licit edges and the wire is not closed;
1051 // ErrorStatus=3 : The face contains a wire that is not connectable
1052 // ErrorStatus=4 : The base surface is not periodic;
1053 // ErrorStatus=5 : an outer wir can not be found;
1054 // ErrorStatus=6 : Wrong connectability of a wire in 2d