1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 // pdn 10.12.98: tr9_r0501-ug
15 // pdn 28.12.98: PRO10366 shifting pcurve between two singularities
16 //:k7 abv 5.01.99: USA60022.igs ent 243: FixMissingSeam() improved
17 //:l2 abv 10.01.99: USA60022 7289: corrections for reversed face
18 //gka 11.01.99 file PRO7755.stp #2018: work-around error in BRepLib_MakeFace
19 //:p4 abv, pdn 23.02.99: PRO9234 #15720: call BRepTools::Update() for faces
20 // rln 03.03.99 S4135: transmission of parameter precision to SA_Surface::NbSingularities
21 //:q5 abv 19.03.99 code improvement
22 //%14 pdn 15.03.99 adding function for fixing null area wires
23 //%15 pdn 20.03.99 code improvement
24 // abv 09.04.99 S4136: improve tolerance management; remove unused flag Closed
25 //#4 szv S4163: optimization
26 // smh 31.01.01 BUC60810 : Case of small wire on face in solid
27 // sln 25.09.2001 checking order of 3d and 2d representation curves
28 // abv 19.10.2001 FixAddNaturalBound improved and extracted as separate fix
29 // skl,pdn 14.05.2002 OCC55 (correction precision for small faces)
31 #include <ShapeFix_Face.ixx>
33 #include <Standard_Failure.hxx>
34 #include <Standard_ErrorHandler.hxx>
36 #include <Precision.hxx>
38 #include <Geom2d_Curve.hxx>
39 #include <Geom2d_Line.hxx>
40 #include <Geom2dAdaptor_Curve.hxx>
41 #include <Geom_Curve.hxx>
42 #include <Geom_BSplineSurface.hxx>
43 #include <GeomAdaptor_HSurface.hxx>
44 #include <GProp_GProps.hxx>
47 #include <TopoDS_Edge.hxx>
48 #include <TopoDS_Vertex.hxx>
49 #include <TopoDS_Shell.hxx>
50 #include <TopoDS_Compound.hxx>
51 #include <TopoDS_Iterator.hxx>
52 #include <TopExp_Explorer.hxx>
53 #include <TopTools_SequenceOfShape.hxx>
55 #include <BRepGProp.hxx>
56 #include <BRep_Tool.hxx>
57 #include <BRep_Builder.hxx>
58 #include <BRepTopAdaptor_FClass2d.hxx>
59 #include <BRepTools.hxx>
60 #include <BRepBuilderAPI_MakeFace.hxx>
61 #include <BRepBuilderAPI_MakeVertex.hxx>
62 #include <BRepBuilderAPI_MakeWire.hxx>
64 #include <Message_Msg.hxx>
65 #include <ShapeBuild_ReShape.hxx>
66 #include <ShapeExtend_WireData.hxx>
67 #include <ShapeAnalysis.hxx>
68 #include <ShapeFix_Wire.hxx>
69 #include <ShapeFix_Edge.hxx>
70 #include <ShapeAnalysis_Edge.hxx>
71 #include <Bnd_Box2d.hxx>
72 #include <Geom_Circle.hxx>
73 #include <Geom_SphericalSurface.hxx>
74 #include <Geom_RectangularTrimmedSurface.hxx>
75 #include <Geom_ConicalSurface.hxx>
76 #include <ShapeAnalysis_Wire.hxx>
77 #include <ShapeAnalysis_Surface.hxx>
79 #include <ShapeExtend_CompositeSurface.hxx>
80 #include <ShapeFix_ComposeShell.hxx>
81 #include <TColGeom_HArray2OfSurface.hxx>
82 #include <ShapeBuild_Edge.hxx>
83 #include <TColgp_SequenceOfPnt2d.hxx>
84 #include <Bnd_Box.hxx>
85 #include <TopTools_IndexedMapOfShape.hxx>
86 #include <TopTools_DataMapOfShapeListOfShape.hxx>
87 #include <TopTools_ListIteratorOfListOfShape.hxx>
88 #include <TopTools_MapOfShape.hxx>
92 #include <ShapeFix.hxx>
93 #include <ShapeFix_DataMapOfShapeBox2d.hxx>
94 #include <BndLib_Add2dCurve.hxx>
95 #include <Geom2dAdaptor_Curve.hxx>
96 #include <IntRes2d_Domain.hxx>
97 #include <Geom2dInt_GInter.hxx>
98 #include <IntRes2d_IntersectionPoint.hxx>
99 #include <IntRes2d_Transition.hxx>
100 #include <TopTools_SequenceOfShape.hxx>
101 #include <IntRes2d_IntersectionSegment.hxx>
102 #include <TopTools_DataMapOfShapeInteger.hxx>
104 #include <ShapeFix_IntersectionTool.hxx>
105 #include <ShapeFix_SplitTool.hxx>
106 #include <TColStd_MapOfInteger.hxx>
107 #include <TopTools_DataMapOfShapeShape.hxx>
108 #include <NCollection_Array1.hxx>
114 static Standard_Boolean IsSurfaceUVInfinite(const Handle(Geom_Surface)& theSurf)
116 Standard_Real UMin,UMax,VMin,VMax;
117 theSurf->Bounds(UMin,UMax,VMin,VMax);
119 return (Precision::IsInfinite(UMin) ||
120 Precision::IsInfinite(UMax) ||
121 Precision::IsInfinite(VMin) ||
122 Precision::IsInfinite(VMax) );
125 static Standard_Boolean IsSurfaceUVPeriodic(const Handle(Geom_Surface)& theSurf)
127 return theSurf->IsUPeriodic() && theSurf->IsVPeriodic();
130 //=======================================================================
131 //function : ShapeFix_Face
133 //=======================================================================
135 ShapeFix_Face::ShapeFix_Face()
137 myFwd = Standard_True;
139 myFixWire = new ShapeFix_Wire;
143 //=======================================================================
144 //function : ShapeFix_Face
146 //=======================================================================
148 ShapeFix_Face::ShapeFix_Face(const TopoDS_Face &face)
150 myFwd = Standard_True;
152 myFixWire = new ShapeFix_Wire;
157 //=======================================================================
158 //function : ClearModes
160 //=======================================================================
162 void ShapeFix_Face::ClearModes()
165 myFixOrientationMode = -1;
166 myFixAddNaturalBoundMode = -1;
167 myFixMissingSeamMode = -1;
168 myFixSmallAreaWireMode = -1;
169 myFixIntersectingWiresMode = -1;
170 myFixLoopWiresMode = -1;
171 myFixSplitFaceMode = -1;
172 myAutoCorrectPrecisionMode = 1;
173 myFixPeriodicDegenerated = -1;
176 //=======================================================================
177 //function : SetMsgRegistrator
179 //=======================================================================
181 void ShapeFix_Face::SetMsgRegistrator(const Handle(ShapeExtend_BasicMsgRegistrator)& msgreg)
183 ShapeFix_Root::SetMsgRegistrator ( msgreg );
184 myFixWire->SetMsgRegistrator ( msgreg );
187 //=======================================================================
188 //function : SetPrecision
190 //=======================================================================
192 void ShapeFix_Face::SetPrecision (const Standard_Real preci)
194 ShapeFix_Root::SetPrecision ( preci );
195 myFixWire->SetPrecision ( preci );
198 //=======================================================================
199 //function : SetMinTolerance
201 //=======================================================================
203 void ShapeFix_Face::SetMinTolerance (const Standard_Real mintol)
205 ShapeFix_Root::SetMinTolerance ( mintol );
206 myFixWire->SetMinTolerance ( mintol );
209 //=======================================================================
210 //function : SetMaxTolerance
212 //=======================================================================
214 void ShapeFix_Face::SetMaxTolerance (const Standard_Real maxtol)
216 ShapeFix_Root::SetMaxTolerance ( maxtol );
217 myFixWire->SetMaxTolerance ( maxtol );
220 //=======================================================================
223 //=======================================================================
225 void ShapeFix_Face::Init (const Handle(Geom_Surface)& surf,
226 const Standard_Real preci, const Standard_Boolean fwd)
229 Handle(ShapeAnalysis_Surface) sas = new ShapeAnalysis_Surface ( surf );
230 Init ( sas, preci, fwd );
233 //=======================================================================
236 //=======================================================================
238 void ShapeFix_Face::Init (const Handle(ShapeAnalysis_Surface)& surf,
239 const Standard_Real preci, const Standard_Boolean fwd)
243 SetPrecision ( preci );
245 B.MakeFace ( myFace, mySurf->Surface(), ::Precision::Confusion() );
248 if ( !fwd ) myFace.Orientation(TopAbs_REVERSED);
251 //=======================================================================
254 //=======================================================================
256 void ShapeFix_Face::Init (const TopoDS_Face& face)
259 mySurf = new ShapeAnalysis_Surface ( BRep_Tool::Surface (face) );
260 myFwd = ( face.Orientation() != TopAbs_REVERSED );
263 // myFace = TopoDS::Face(face.EmptyCopied());
264 // for (TopoDS_Iterator ws (face,Standard_False); ws.More(); ws.Next())
265 // Add (TopoDS::Wire (ws.Value()) );
268 //=======================================================================
271 //=======================================================================
273 void ShapeFix_Face::Add (const TopoDS_Wire& wire)
275 if ( wire.IsNull() ) return;
277 //szv#4:S4163:12Mar99 SGI warns
278 TopoDS_Shape fc = myFace.Oriented(TopAbs_FORWARD); //:l2 abv 10 Jan 99: Oriented()
283 //=======================================================================
284 //function : SplitWire
285 //purpose : auxilary - try to split wire (it is needed if some segments
286 // were removed in ShapeFix_Wire::FixSelfIntersection()
287 //=======================================================================
288 static Standard_Boolean SplitWire(const TopoDS_Face &face, const TopoDS_Wire& wire,
289 TopTools_SequenceOfShape& aResWires)
291 TColStd_MapOfInteger UsedEdges;
292 Handle(ShapeExtend_WireData) sewd = new ShapeExtend_WireData(wire);
293 Standard_Integer i,j,k;
294 ShapeAnalysis_Edge sae;
295 for(i=1; i<=sewd->NbEdges(); i++) {
296 if(UsedEdges.Contains(i)) continue;
297 TopoDS_Edge E1 = sewd->Edge(i);
299 TopoDS_Vertex V0,V1,V2;
300 V0 = sae.FirstVertex(E1);
301 V1 = sae.LastVertex(E1);
302 Handle(ShapeExtend_WireData) sewd1 = new ShapeExtend_WireData;
304 Standard_Boolean IsConnectedEdge = Standard_True;
305 for(j=2; j<=sewd->NbEdges() && IsConnectedEdge; j++) {
307 for(k=2; k<=sewd->NbEdges(); k++) {
308 if(UsedEdges.Contains(k)) continue;
310 TopoDS_Vertex V21 = sae.FirstVertex(E2);
311 TopoDS_Vertex V22 = sae.LastVertex(E2);
312 if( sae.FirstVertex(E2).IsSame(V1) ) {
315 V1 = sae.LastVertex(E2);
319 if(k>sewd->NbEdges()) {
320 IsConnectedEdge = Standard_False;
324 //check that V0 and V1 are same in 2d too
325 Standard_Real a1,b1,a2,b2;
326 Handle (Geom2d_Curve) curve1 = BRep_Tool::CurveOnSurface(E1,face,a1,b1);
327 Handle (Geom2d_Curve) curve2 = BRep_Tool::CurveOnSurface(E2,face,a2,b2);
329 if (E1.Orientation() == TopAbs_REVERSED)
331 if (E2.Orientation() == TopAbs_REVERSED)
335 GeomAdaptor_Surface anAdaptor(BRep_Tool::Surface(face));
336 Standard_Real tol = Max(BRep_Tool::Tolerance(V0),BRep_Tool::Tolerance(V1));
337 Standard_Real maxResolution = 2 * Max ( anAdaptor.UResolution(tol), anAdaptor.VResolution(tol) );
338 if (v0.SquareDistance(v1) < maxResolution) {
339 // new wire is closed, put it into sequence
340 aResWires.Append(sewd1->Wire());
345 if(!IsConnectedEdge) {
346 // create new notclosed wire
347 aResWires.Append(sewd1->Wire());
349 if(UsedEdges.Extent()==sewd->NbEdges()) break;
352 if(aResWires.Length()>1) {
354 cout<<"Wire was splitted on "<<aResWires.Length()<<" wires"<< endl;
358 return Standard_True;
362 //=======================================================================
365 //=======================================================================
367 Standard_Boolean ShapeFix_Face::Perform()
369 myStatus = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
370 myFixWire->SetContext ( Context() );
371 Handle(ShapeFix_Wire) theAdvFixWire = Handle(ShapeFix_Wire)::DownCast(myFixWire);
372 if (theAdvFixWire.IsNull()) return Standard_False;
375 TopoDS_Shape aInitFace = myFace;
376 // perform first part of fixes on wires
377 Standard_Boolean isfixReorder = Standard_False;
378 Standard_Boolean isReplaced = Standard_False;
380 //gka fix in order to avoid lost messages (following OCC21771)
381 TopTools_DataMapOfShapeShape aMapReorderedWires;
383 Standard_Real aSavPreci = Precision();
384 if ( NeedFix ( myFixWireMode ) ) {
385 theAdvFixWire->SetFace ( myFace );
387 Standard_Integer usFixLackingMode = theAdvFixWire->FixLackingMode();
388 //Standard_Integer usFixNotchedEdgesMode = theAdvFixWire->FixNotchedEdgesMode(); // CR0024983
389 Standard_Integer usFixSelfIntersectionMode = theAdvFixWire->FixSelfIntersectionMode();
390 theAdvFixWire->FixLackingMode() = Standard_False;
391 //theAdvFixWire->FixNotchedEdgesMode() = Standard_False; // CR0024983
392 theAdvFixWire->FixSelfIntersectionMode() = Standard_False;
394 Standard_Boolean fixed = Standard_False;
395 TopoDS_Shape S = myFace;
396 if ( ! Context().IsNull() )
397 S = Context()->Apply ( myFace );
398 TopoDS_Shape emptyCopied = S.EmptyCopied();
399 TopoDS_Face tmpFace = TopoDS::Face(emptyCopied);
400 tmpFace.Orientation ( TopAbs_FORWARD );
403 // skl 14.05.2002 OCC55 + corrected 03.03.2004
404 Standard_Real dPreci = aSavPreci*aSavPreci;
406 Standard_Real newpreci=dPreci;
407 for(TopExp_Explorer exp(S,TopAbs_EDGE); exp.More(); exp.Next()) {
408 TopoDS_Edge edge = TopoDS::Edge ( exp.Current() );
409 Standard_Real first,last;
410 Handle(Geom_Curve) c3d = BRep_Tool::Curve(edge, first, last);
413 bb.Add(c3d->Value(first));
414 bb.Add(c3d->Value(last));
415 bb.Add(c3d->Value((last+first)/2.));
416 Standard_Real x1,x2,y1,y2,z1,z2,size;
417 bb.Get(x1,y1,z1,x2,y2,z2);
418 size = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1);
419 if(size<newpreci) newpreci=size;
422 newpreci=sqrt(newpreci)/2.*1.00001;
423 if( aSavPreci > newpreci && newpreci > Precision::Confusion()) {
424 SetPrecision(newpreci);
425 theAdvFixWire->SetPrecision(newpreci);
427 // end skl 14.05.2002
430 // skl 29.03.2010 (OCC21623)
431 if( myAutoCorrectPrecisionMode ) {
432 Standard_Real size = ShapeFix::LeastEdgeSize(S);
433 Standard_Real newpreci = Min(aSavPreci,size/2.);
434 newpreci = newpreci*1.00001;
435 if( aSavPreci > newpreci && newpreci > Precision::Confusion()) {
436 SetPrecision(newpreci);
437 theAdvFixWire->SetPrecision(newpreci);
441 isfixReorder = Standard_False;
442 for ( TopoDS_Iterator iter(S,Standard_False); iter.More(); iter.Next()) {
443 if(iter.Value().ShapeType() != TopAbs_WIRE) {
444 B.Add ( tmpFace, iter.Value() );
447 TopoDS_Wire wire = TopoDS::Wire ( iter.Value() );
448 theAdvFixWire->Load ( wire );
449 if(theAdvFixWire->NbEdges() == 0) {
450 if(theAdvFixWire->WireData()->NbNonManifoldEdges())
451 B.Add ( tmpFace, wire );
453 fixed = Standard_True;
454 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE5 );
458 if ( theAdvFixWire->Perform() ) {
459 //fixed = Standard_True;
460 isfixReorder = (theAdvFixWire->StatusReorder(ShapeExtend_DONE) || isfixReorder);
461 fixed = (theAdvFixWire->StatusSmall(ShapeExtend_DONE) ||
462 theAdvFixWire->StatusConnected(ShapeExtend_DONE) ||
463 theAdvFixWire->StatusEdgeCurves(ShapeExtend_DONE) ||
464 theAdvFixWire->StatusNotches(ShapeExtend_DONE) || // CR0024983
465 theAdvFixWire->StatusDegenerated(ShapeExtend_DONE) ||
466 theAdvFixWire->StatusClosed(ShapeExtend_DONE));
467 TopoDS_Wire w = theAdvFixWire->Wire();
469 if ( ! Context().IsNull() ) Context()->Replace ( wire, w );
470 if(theAdvFixWire->NbEdges() == 0) {
471 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE5 );
475 else if(!wire.IsSame(w))
476 aMapReorderedWires.Bind(wire,w);
480 B.Add ( tmpFace, wire );
481 // if ( theAdvFixWire->Status ( ShapeExtend_FAIL ) )
482 // myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL1 );
485 theAdvFixWire->FixLackingMode() = usFixLackingMode;
486 //theAdvFixWire->FixNotchedEdgesMode() = usFixNotchedEdgesMode; // CR0024983
487 theAdvFixWire->FixSelfIntersectionMode() = usFixSelfIntersectionMode;
488 if ( ! myFwd ) tmpFace.Orientation ( TopAbs_REVERSED );
491 //if ( ! myFwd ) tmpFace.Orientation ( TopAbs_REVERSED );
492 if ( ! Context().IsNull() ) Context()->Replace ( S, tmpFace );
494 isReplaced = Standard_True;
496 if(fixed || isfixReorder) {
498 if (!theAdvFixWire->StatusReorder(ShapeExtend_DONE5)) {
499 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
505 TopoDS_Shape savShape = myFace; //gka BUG 6555
507 // Specific case for conic surfaces
508 if ( NeedFix(myFixPeriodicDegenerated) )
509 this->FixPeriodicDegenerated();
512 if ( NeedFix ( myFixMissingSeamMode ) ) {
513 if ( FixMissingSeam() ) {
514 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE3 );
518 // cycle by all possible faces coming from FixMissingSeam
519 // each face is processed as if it was single
520 TopExp_Explorer exp(myResult,TopAbs_FACE);
521 for ( ; exp.More(); exp.Next() ) {
522 myFace = TopoDS::Face ( exp.Current() );
523 Standard_Boolean NeedCheckSplitWire = Standard_False;
525 // perform second part of fixes on wires
526 if ( NeedFix ( myFixWireMode ) ) {
527 theAdvFixWire->SetFace ( myFace );
529 Standard_Integer usFixSmallMode = theAdvFixWire->FixSmallMode();
530 Standard_Integer usFixConnectedMode = theAdvFixWire->FixConnectedMode();
531 Standard_Integer usFixEdgeCurvesMode =theAdvFixWire->FixEdgeCurvesMode();
532 Standard_Integer usFixDegeneratedMode = theAdvFixWire->FixDegeneratedMode();
533 theAdvFixWire->FixSmallMode() = Standard_False;
534 theAdvFixWire->FixConnectedMode() = Standard_False;
535 theAdvFixWire->FixEdgeCurvesMode() = Standard_False;
536 theAdvFixWire->FixDegeneratedMode() = Standard_False;
538 Standard_Boolean fixed = Standard_False;
539 TopoDS_Shape S = myFace;
540 if ( ! Context().IsNull() )
541 S = Context()->Apply ( myFace );
542 TopoDS_Shape emptyCopied = S.EmptyCopied();
543 TopoDS_Face tmpFace = TopoDS::Face(emptyCopied);
544 tmpFace.Orientation ( TopAbs_FORWARD );
545 for ( TopoDS_Iterator iter(S,Standard_False); iter.More(); iter.Next()) {
546 if(iter.Value().ShapeType() != TopAbs_WIRE) {
547 B.Add ( tmpFace,iter.Value());
551 TopoDS_Wire wire = TopoDS::Wire ( iter.Value() );
552 theAdvFixWire->Load ( wire );
553 if(theAdvFixWire->NbEdges() == 0) {
554 if(theAdvFixWire->WireData()->NbNonManifoldEdges())
555 B.Add ( tmpFace, wire );
557 fixed = Standard_True;
558 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE5 );
562 if ( theAdvFixWire->Perform() ) {
563 isfixReorder = theAdvFixWire->StatusReorder(ShapeExtend_DONE);
564 fixed = (theAdvFixWire->StatusLacking(ShapeExtend_DONE) ||
565 theAdvFixWire->StatusSelfIntersection(ShapeExtend_DONE) ||
566 theAdvFixWire->StatusNotches(ShapeExtend_DONE)); //Standard_True;
567 TopoDS_Wire w = theAdvFixWire->Wire();
569 if ( ! Context().IsNull() ) Context()->Replace ( wire, w );
572 else if(!wire.IsSame(w))
573 aMapReorderedWires.Bind(wire,w);
577 if(theAdvFixWire->StatusRemovedSegment())
578 NeedCheckSplitWire = Standard_True;
580 //fix for loop of wire
581 TopTools_SequenceOfShape aLoopWires;
582 if(NeedFix ( myFixLoopWiresMode) && FixLoopWire(aLoopWires)) {
583 if (aLoopWires.Length() > 1)
584 SendWarning ( wire, Message_Msg ( "FixAdvFace.FixLoopWire.MSG0" ) );// Wire was splitted on several wires
585 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE7 );
586 fixed = Standard_True;
587 Standard_Integer k=1;
588 for( ; k <= aLoopWires.Length(); k++)
589 B.Add (tmpFace,aLoopWires.Value(k));
592 B.Add ( tmpFace, wire );
595 theAdvFixWire->FixSmallMode() = usFixSmallMode;
596 theAdvFixWire->FixConnectedMode() = usFixConnectedMode;
597 theAdvFixWire->FixEdgeCurvesMode() = usFixEdgeCurvesMode;
598 theAdvFixWire->FixDegeneratedMode() = usFixDegeneratedMode;
601 if ( ! myFwd ) tmpFace.Orientation ( TopAbs_REVERSED );
602 if(!isReplaced && !aInitFace.IsSame(myResult) && ! Context().IsNull()) //gka 06.09.04 BUG 6555
603 Context()->Replace(aInitFace,savShape);
604 if ( ! Context().IsNull() ) Context()->Replace ( S, tmpFace );
606 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
610 if(NeedCheckSplitWire) {
611 // try to split wire - it is needed if some segments were removed
612 // in ShapeFix_Wire::FixSelfIntersection()
613 TopoDS_Shape S = myFace;
614 if ( ! Context().IsNull() )
615 S = Context()->Apply ( myFace );
616 TopoDS_Shape emptyCopied = S.EmptyCopied();
617 TopoDS_Face tmpFace = TopoDS::Face(emptyCopied);
618 tmpFace.Orientation ( TopAbs_FORWARD );
619 TopTools_SequenceOfShape aWires;
620 Standard_Integer nbw=0;
621 for ( TopoDS_Iterator iter(S,Standard_False); iter.More(); iter.Next()) {
622 if(iter.Value().ShapeType() != TopAbs_WIRE) {
623 B.Add (tmpFace,iter.Value());
626 if(iter.Value().Orientation() != TopAbs_FORWARD &&
627 iter.Value().Orientation() != TopAbs_REVERSED) {
628 B.Add (tmpFace,TopoDS::Wire(iter.Value()));
632 TopoDS_Wire wire = TopoDS::Wire ( iter.Value() );
633 SplitWire(tmpFace,wire,aWires);
635 if(nbw<aWires.Length()) {
636 for(Standard_Integer iw=1; iw<=aWires.Length(); iw++)
637 B.Add (tmpFace,aWires.Value(iw));
638 if ( ! Context().IsNull() ) Context()->Replace ( S, tmpFace );
639 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE8 );
644 // fix intersecting wires
645 if(FixWiresTwoCoincEdges())
646 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE7 );
647 if ( NeedFix ( myFixIntersectingWiresMode ) ) {
648 if ( FixIntersectingWires() ) {
649 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE6 );
654 TopTools_DataMapOfShapeListOfShape MapWires;
656 if ( NeedFix ( myFixOrientationMode ) ) {
657 if ( FixOrientation(MapWires) )
658 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE2 );
661 BRepTools::Update(myFace);
663 // fix natural bounds
664 Standard_Boolean NeedSplit = Standard_True;
665 if ( NeedFix ( myFixAddNaturalBoundMode ) ) {
666 if ( FixAddNaturalBound() ) {
667 NeedSplit = Standard_False;
668 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE5 );
673 if ( NeedFix ( myFixSplitFaceMode ) && NeedSplit && MapWires.Extent()>1 ) {
674 if ( FixSplitFace(MapWires) )
675 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE8 );
680 //return the original preci
681 SetPrecision(aSavPreci);
682 theAdvFixWire->SetPrecision(aSavPreci);
684 // cycle by all possible faces coming from FixAddNaturalBound
685 // each face is processed as if it was single
686 for ( exp.Init(myResult,TopAbs_FACE); exp.More(); exp.Next() ) {
687 myFace = TopoDS::Face ( exp.Current() );
689 // fix small-area wires
690 if ( NeedFix ( myFixSmallAreaWireMode, Standard_False ) )
692 const Standard_Boolean isRemoveFace = NeedFix( myRemoveSmallAreaFaceMode, Standard_False );
693 if ( FixSmallAreaWire( isRemoveFace ) )
694 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE4 );
698 if ( ! Context().IsNull() ) {
699 if(Status ( ShapeExtend_DONE ) && !isReplaced && !aInitFace.IsSame(savShape))
701 //gka fix in order to avoid lost messages (following OCC21771)
702 if(aMapReorderedWires.Extent())
704 TopoDS_Iterator aItW(aInitFace,Standard_False);
705 for( ; aItW.More() ; aItW.Next())
707 TopoDS_Shape aCurW = aItW.Value();
708 while(aMapReorderedWires.IsBound(aCurW))
710 TopoDS_Shape aFixW = aMapReorderedWires.Find(aCurW);
711 Context()->Replace(aCurW, aFixW);
717 Context()->Replace(aInitFace, savShape);
719 myResult = Context()->Apply ( aInitFace ); //gka 06.09.04
721 else if(!Status ( ShapeExtend_DONE ))
722 myResult = aInitFace;
724 return Status ( ShapeExtend_DONE );
727 //=======================================================================
728 //function : Auxiliary functions
730 //=======================================================================
732 // Shift all pcurves of edges in the given wire on the given face
734 static void Shift2dWire(const TopoDS_Wire w, const TopoDS_Face f,
736 const Handle(ShapeAnalysis_Surface)& mySurf,
737 Standard_Boolean recompute3d = Standard_False)
740 tr2d.SetTranslation(vec.XY());
741 ShapeAnalysis_Edge sae;
744 for (TopoDS_Iterator ei (w,Standard_False); ei.More(); ei.Next()){
745 TopoDS_Edge edge = TopoDS::Edge(ei.Value());
746 Handle (Geom2d_Curve) C2d;
747 Standard_Real cf, cl;
748 if ( ! sae.PCurve(edge, f, C2d, cf, cl, Standard_True) ) continue;
749 C2d->Transform(tr2d);
751 // recompute 3d curve and vertex
752 sbe.RemoveCurve3d ( edge );
753 sbe.BuildCurve3d ( edge );
754 B.UpdateVertex ( sae.FirstVertex(edge), mySurf->Value(C2d->Value(cf)), 0. );
759 // Cut interval from the sequence of intervals
760 static Standard_Boolean CutInterval (TColgp_SequenceOfPnt2d &intervals,
761 const gp_Pnt2d &toAddI,
762 const Standard_Real period)
764 if ( intervals.Length() <=0 ) return Standard_False;
765 for ( Standard_Integer j=0; j <2; j++ ) { // try twice, align to bottom and to top
766 for ( Standard_Integer i=1; i <= intervals.Length(); i++ ) {
767 gp_Pnt2d interval = intervals(i);
768 // ACIS907, OCC921 a054a.sat (face 124)
769 Standard_Real shift = ShapeAnalysis::AdjustByPeriod ( ( j ? toAddI.X() : toAddI.Y() ),
770 0.5*( interval.X() + interval.Y() ),period);
771 gp_Pnt2d toAdd ( toAddI.X() + shift, toAddI.Y() + shift );
772 if ( toAdd.Y() <= interval.X() || toAdd.X() >= interval.Y() ) continue;
773 if ( toAdd.X() > interval.X() ) {
774 if ( toAdd.Y() < interval.Y() ) {
775 intervals.InsertBefore ( i, interval );
776 intervals.ChangeValue(i+1).SetX ( toAdd.Y() ); // i++...
778 intervals.ChangeValue(i).SetY ( toAdd.X() );
780 else if ( toAdd.Y() < interval.Y() ) {
781 intervals.ChangeValue(i).SetX ( toAdd.Y() );
783 else intervals.Remove ( i-- );
786 return Standard_True;
789 // Find middle of the biggest interval
790 static Standard_Real FindBestInterval (TColgp_SequenceOfPnt2d &intervals)
792 Standard_Real shift = 0., max = -1.;
793 for ( Standard_Integer i=1; i <= intervals.Length(); i++ ) {
794 gp_Pnt2d interval = intervals(i);
795 if ( interval.Y() - interval.X() <= max ) continue;
796 max = interval.Y() - interval.X();
797 shift = interval.X() + 0.5 * max;
802 //=======================================================================
803 //function : FixAddNaturalBound
805 //=======================================================================
806 // Detect missing natural bounary on spherical surfaces and add it if
808 //pdn 981202: add natural bounds if missing (on sphere only)
809 //:abv 28.08.01: rewritten and extended for toruses
811 Standard_Boolean ShapeFix_Face::FixAddNaturalBound()
813 if ( ! Context().IsNull() ) {
814 TopoDS_Shape S = Context()->Apply ( myFace );
815 myFace = TopoDS::Face ( S );
818 // collect wires in sequence
819 TopTools_SequenceOfShape ws;
820 TopTools_SequenceOfShape vs;
821 TopoDS_Iterator wi (myFace,Standard_False);
822 for ( ; wi.More(); wi.Next()) {
823 if(wi.Value().ShapeType() == TopAbs_WIRE &&
824 (wi.Value().Orientation() == TopAbs_FORWARD || wi.Value().Orientation() == TopAbs_REVERSED))
825 ws.Append (wi.Value());
827 vs.Append(wi.Value());
830 // deal with the case of an empty face: just create a new face by a standard tool
831 if (ws.IsEmpty() && !IsSurfaceUVInfinite (mySurf->Surface()))
833 BRepBuilderAPI_MakeFace aFaceBuilder (mySurf->Surface(), Precision::Confusion());
835 TopoDS_Face aNewFace = aFaceBuilder.Face();
836 aNewFace.Orientation (myFace.Orientation());
838 if ( ! Context().IsNull() )
839 Context()->Replace (myFace, aNewFace);
841 // taking into account orientation
844 //gka 11.01.99 file PRO7755.stp entity #2018 surface #1895: error BRepLib_MakeFace func IsDegenerated
845 Handle(ShapeFix_Edge) sfe = myFixWire->FixEdgeTool();
846 for (TopExp_Explorer Eed (myFace, TopAbs_EDGE); Eed.More(); Eed.Next()) {
847 TopoDS_Edge edg = TopoDS::Edge (Eed.Current());
848 sfe->FixVertexTolerance(edg);
851 // B.UpdateFace (myFace,myPrecision);
852 SendWarning ( myFace, Message_Msg ( "FixAdvFace.FixOrientation.MSG0" ) );// Face created with natural bounds
853 BRepTools::Update(myFace);
855 return Standard_True;
858 // check if surface is double-closed and fix is needed
859 if ( !IsSurfaceUVPeriodic (mySurf->Surface()) || ShapeAnalysis::IsOuterBound (myFace) )
860 return Standard_False;
862 // Collect informations on free intervals in U and V
863 TColgp_SequenceOfPnt2d intU, intV, centers;
864 Standard_Real SUF, SUL, SVF, SVL;
865 mySurf->Bounds(SUF, SUL, SVF, SVL);
866 intU.Append ( gp_Pnt2d(SUF, SUL) );
867 intV.Append ( gp_Pnt2d(SVF, SVL) );
868 Standard_Integer nb = ws.Length();
870 for ( i=1; i <= nb; i ++) {
871 Standard_Real Umin, Vmin, Umax, Vmax;
873 TopoDS_Wire aw = TopoDS::Wire (ws.Value(i));
874 // PTV 01.11.2002 ACIS907, OCC921 begin
875 // BRepTools::AddUVBounds(myFace,aw,B);
876 // B.Get(Umin, Vmin, Umax, Vmax);
877 TopoDS_Face aWireFace = TopoDS::Face( myFace.EmptyCopied() );
879 aB.Add( aWireFace, aw );
880 ShapeAnalysis::GetFaceUVBounds(aWireFace, Umin, Umax, Vmin, Vmax);
881 // PTV 01.11.2002 ACIS907, OCC921 end
882 if ( mySurf->IsUClosed() ) CutInterval ( intU, gp_Pnt2d(Umin,Umax), SUL-SUF );
883 if ( mySurf->IsVClosed() ) CutInterval ( intV, gp_Pnt2d(Vmin,Vmax), SVL-SVF );
884 centers.Append ( gp_Pnt2d ( 0.5*(Umin+Umax), 0.5*(Vmin+Vmax) ) );
887 // find best interval and thus compute shift
888 gp_Pnt2d shift(0.,0.);
889 if ( mySurf->IsUClosed() ) shift.SetX ( FindBestInterval ( intU ) );
890 if ( mySurf->IsVClosed() ) shift.SetY ( FindBestInterval ( intV ) );
892 // Adjust all other wires to be inside outer one
893 gp_Pnt2d center ( shift.X() + 0.5*(SUL-SUF), shift.Y() + 0.5*(SVL-SVF) );
894 for ( i=1; i <= nb; i++ ) {
895 TopoDS_Wire wire = TopoDS::Wire (ws.Value(i));
897 if ( mySurf->IsUClosed() )
898 sh.SetX ( ShapeAnalysis::AdjustByPeriod ( centers(i).X(), center.X(), SUL-SUF ) );
899 if ( mySurf->IsVClosed() )
900 sh.SetY ( ShapeAnalysis::AdjustByPeriod ( centers(i).Y(), center.Y(), SVL-SVF ) );
901 Shift2dWire ( wire, myFace, sh.XY(), mySurf );
904 // Create naturally bounded surface and add that wire to sequence
906 // Create fictive grid and call ComposeShell
907 Handle(Geom_RectangularTrimmedSurface) RTS =
908 new Geom_RectangularTrimmedSurface ( mySurf->Surface(), SUF+shift.X(), SUL+shift.X(),
909 SVF+shift.Y(), SVL+shift.Y() );
910 Handle(TColGeom_HArray2OfSurface) grid = new TColGeom_HArray2OfSurface ( 1, 1, 1, 1 );
911 grid->SetValue ( 1, 1, RTS );
912 Handle(ShapeExtend_CompositeSurface) G = new ShapeExtend_CompositeSurface ( grid );
915 ShapeFix_ComposeShell CompShell;
916 CompShell.Init ( G, L, myFace, ::Precision::Confusion() );
917 CompShell.ClosedMode() = Standard_True;
918 CompShell.NaturalBoundMode() = Standard_True;
919 CompShell.SetContext( Context() );
920 CompShell.SetMaxTolerance(MaxTolerance());
922 TopoDS_Shape res = CompShell.Result();
924 Context()->Replace ( myFace, res );
925 for (TopExp_Explorer exp ( res, TopAbs_FACE ); exp.More(); exp.Next() ) {
926 myFace = TopoDS::Face ( exp.Current() );
927 BRepTools::Update(myFace); //:p4
929 myResult = Context()->Apply ( myResult );
933 Handle(Geom_Surface) surf = BRep_Tool::Surface ( myFace, L );
934 BRepBuilderAPI_MakeFace mf (surf, Precision::Confusion());
935 TopoDS_Face ftmp = mf.Face();
937 for (wi.Initialize (ftmp,Standard_False); wi.More(); wi.Next()) {
938 if(wi.Value().ShapeType() != TopAbs_WIRE)
940 TopoDS_Wire wire = TopoDS::Wire ( wi.Value() );
942 if ( shift.XY().Modulus() < ::Precision::PConfusion() ) continue;
943 Shift2dWire ( wire, myFace, shift.XY(), mySurf, Standard_True );
946 // Fix possible case on sphere when gap contains degenerated edge
947 // and thus has a common part with natural boundary
948 // Such hole should be merged with boundary
949 if ( mySurf->Adaptor3d()->GetType() == GeomAbs_Sphere &&
950 ws.Length() == nb+1 ) {
951 Handle(ShapeExtend_WireData) bnd =
952 new ShapeExtend_WireData ( TopoDS::Wire ( ws.Last() ) );
953 // code to become separate method FixTouchingWires()
954 for ( i=1; i <= nb; i++ ) {
955 Handle(ShapeExtend_WireData) sbwd =
956 new ShapeExtend_WireData ( TopoDS::Wire ( ws.Value(i) ) );
957 for (Standard_Integer j=1; j <= sbwd->NbEdges(); j++ ) {
958 if ( ! BRep_Tool::Degenerated ( sbwd->Edge(j) ) ) continue;
959 // find corresponding place in boundary
960 ShapeAnalysis_Edge sae;
961 TopoDS_Vertex V = sae.FirstVertex ( sbwd->Edge(j) );
963 for ( k=1; k <= bnd->NbEdges(); k++ ) {
964 if ( ! BRep_Tool::Degenerated ( bnd->Edge(k) ) ) continue;
965 if ( BRepTools::Compare ( V, sae.FirstVertex ( bnd->Edge(k) ) ) ) break;
967 if ( k > bnd->NbEdges() ) continue;
968 // and insert hole to that place
970 B.Degenerated ( sbwd->Edge(j), Standard_False );
971 B.Degenerated ( bnd->Edge(k), Standard_False );
973 bnd->Add ( sbwd, k+1 );
976 myFixWire->SetFace ( myFace );
977 myFixWire->Load ( bnd );
978 myFixWire->FixConnected();
979 myFixWire->FixDegenerated();
980 ws.SetValue ( ws.Length(), bnd->Wire() );
986 // Create resulting face
988 TopoDS_Shape S = myFace.EmptyCopied();
989 S.Orientation ( TopAbs_FORWARD );
990 for ( i = 1; i <= ws.Length(); i++ ) B.Add ( S, ws.Value(i) );
991 for ( i = 1; i <= vs.Length(); i++ ) B.Add ( S, vs.Value(i) );
992 if ( ! myFwd ) S.Orientation (TopAbs_REVERSED);
993 if ( ! Context().IsNull() ) Context()->Replace ( myFace, S );
994 myFace = TopoDS::Face ( S );
995 BRepTools::Update(myFace);
999 cout<<"Natural bound on sphere or torus with holes added"<<endl; // mise au point !
1001 SendWarning ( myFace, Message_Msg ( "FixAdvFace.FixOrientation.MSG0" ) );// Face created with natural bounds
1002 return Standard_True;
1006 //=======================================================================
1007 //function : FixOrientation
1009 //=======================================================================
1011 Standard_Boolean ShapeFix_Face::FixOrientation()
1013 TopTools_DataMapOfShapeListOfShape MapWires;
1015 return FixOrientation(MapWires);
1019 //=======================================================================
1020 //function : FixOrientation
1022 //=======================================================================
1024 Standard_Boolean ShapeFix_Face::FixOrientation(TopTools_DataMapOfShapeListOfShape &MapWires)
1026 Standard_Boolean done = Standard_False;
1028 if ( ! Context().IsNull() ) {
1029 TopoDS_Shape S = Context()->Apply ( myFace );
1030 myFace = TopoDS::Face ( S );
1032 TopTools_SequenceOfShape ws;
1033 TopTools_SequenceOfShape allSubShapes;
1034 // smh: BUC60810 : protection against very small wires (one-edge, null-length)
1035 TopTools_SequenceOfShape VerySmallWires;
1036 for ( TopoDS_Iterator wi (myFace,Standard_False); wi.More(); wi.Next()) {
1037 if(wi.Value().ShapeType() == TopAbs_VERTEX ||
1038 (wi.Value().Orientation() != TopAbs_FORWARD &&
1039 wi.Value().Orientation() != TopAbs_REVERSED)) {
1040 allSubShapes.Append (wi.Value());
1041 //ws.Append (wi.Value());
1045 TopoDS_Iterator ei (wi.Value(),Standard_False);
1047 Standard_Real length = RealLast();
1049 anEdge = TopoDS::Edge(ei.Value());
1051 if ( ! ei.More() ) {
1053 Standard_Real First, Last;
1054 Handle(Geom_Curve) c3d;
1055 ShapeAnalysis_Edge sae;
1056 if ( sae.Curve3d(anEdge,c3d,First,Last) ) {
1057 gp_Pnt pntIni = c3d->Value(First);
1059 prev = pntIni.XYZ();
1060 Standard_Integer NbControl = 10;
1061 for ( Standard_Integer j = 1; j < NbControl; j++) {
1062 Standard_Real prm = ((NbControl-1-j)*First + j*Last)/(NbControl-1);
1063 gp_Pnt pntCurr = c3d->Value(prm);
1064 gp_XYZ curr = pntCurr.XYZ();
1065 gp_XYZ delta = curr - prev;
1066 length += delta.Modulus();
1073 if (length > ::Precision::Confusion()) {
1074 ws.Append (wi.Value());
1075 allSubShapes.Append (wi.Value());
1077 else VerySmallWires.Append (wi.Value());
1079 if ( VerySmallWires.Length() >0 ) done = Standard_True;
1081 Standard_Integer nb = ws.Length();
1082 Standard_Integer nbAll = allSubShapes.Length();
1085 // if no wires, just do nothing
1086 if ( nb <= 0) return Standard_False;
1087 Standard_Integer nbInternal=0;
1088 Standard_Boolean isAddNaturalBounds = (NeedFix (myFixAddNaturalBoundMode) &&
1089 IsSurfaceUVPeriodic (mySurf->Surface()));
1090 TColStd_SequenceOfInteger aSeqReversed;
1091 // if wire is only one, check its orientation
1093 // skl 12.04.2002 for cases with nbwires>1 (VerySmallWires>1)
1094 // make face with only one wire (ws.Value(1))
1095 TopoDS_Shape dummy = myFace.EmptyCopied();
1096 TopoDS_Face af = TopoDS::Face ( dummy );
1097 af.Orientation ( TopAbs_FORWARD );
1098 B.Add (af,ws.Value(1));
1099 if ((myFixAddNaturalBoundMode != Standard_True || //: abv 29.08.01: Spatial_firex_lofting.sat
1100 !IsSurfaceUVPeriodic (mySurf->Surface()) ) &&
1101 !ShapeAnalysis::IsOuterBound (af) )
1103 Handle(ShapeExtend_WireData) sbdw =
1104 new ShapeExtend_WireData (TopoDS::Wire(ws.Value(1)));
1105 sbdw->Reverse ( myFace );
1106 ws.SetValue ( 1, sbdw->Wire() );
1107 SendWarning ( sbdw->Wire(), Message_Msg ( "FixAdvFace.FixOrientation.MSG5" ) );// Wire on face was reversed
1108 done = Standard_True;
1110 cout<<"Wire reversed"<<endl; // mise au point !
1114 // in case of several wires, perform complex analysis
1116 // Plusieurs wires : orientations relatives
1117 // Chaque wire doit "contenir" tous les autres
1118 // Evidemment, en cas de peau de leopard, il peut y avoir probleme
1120 // On prend chaque wire (NB: pcurves presentes !)
1121 // En principe on devrait rejeter les wires non fermes (cf couture manque ?)
1122 // On le classe par rapport aux autres, qui doivent tous etre, soit IN soit
1123 // OUT. Sinon il y a imbrication -> SDB. Si IN, OK, si OUT on inverse
1124 // (nb : ici pas myClos donc pas de pb de couture)
1125 // Si au moins une inversion, il faut refaire la face (cf myRebil)
1127 //:94 abv 30 Jan 98: calculate parametric precision
1129 // GeomAdaptor_Surface& Ads = mySurf->Adaptor3d()->ChangeSurface();
1130 // Standard_Real toluv = Min ( Ads.UResolution(Precision()), Ads.VResolution(Precision()) );
1131 Standard_Boolean uclosed = mySurf->IsUClosed();
1132 Standard_Boolean vclosed = mySurf->IsVClosed();
1133 Standard_Real SUF, SUL, SVF, SVL;
1134 mySurf->Bounds(SUF, SUL, SVF, SVL);
1135 Standard_Real uRange = SUL - SUF;
1136 Standard_Real vRange = SVL - SVF;
1138 TopTools_DataMapOfShapeListOfShape MW;
1139 TopTools_DataMapOfShapeInteger SI;
1140 TopTools_MapOfShape MapIntWires;
1143 MapIntWires.Clear();
1144 Standard_Integer NbOuts=0;
1147 NCollection_Array1<Bnd_Box2d> aWireBoxes(1, nb);
1148 Standard_Real uMiddle = 0, vMiddle = 0;
1149 Standard_Boolean isFirst = Standard_True;
1150 //create Bounding boxes for each wire
1151 for ( i = 1; i <= nb; i ++) {
1152 TopoDS_Shape aShape = ws.Value(i);
1153 TopoDS_Wire aWire = TopoDS::Wire (aShape);
1155 Standard_Real cf,cl;
1156 TopoDS_Iterator ew (aWire);
1157 for(;ew.More(); ew.Next()) {
1158 TopoDS_Edge ed = TopoDS::Edge (ew.Value());
1159 Handle(Geom2d_Curve) cw = BRep_Tool::CurveOnSurface (ed,myFace,cf,cl);
1160 Geom2dAdaptor_Curve gac;
1161 Standard_Real aFirst = cw->FirstParameter();
1162 Standard_Real aLast = cw->LastParameter();
1163 if(cw->IsKind(STANDARD_TYPE(Geom2d_BSplineCurve)) && (cf < aFirst || cl > aLast)) {
1164 //avoiding problems with segment in Bnd_Box
1169 BndLib_Add2dCurve::Add(gac,::Precision::Confusion(),aBox);
1172 Standard_Real aXMin, aXMax, aYMin, aYMax;
1173 aBox.Get(aXMin, aYMin, aXMax, aYMax);
1175 isFirst = Standard_False;
1176 uMiddle = (aXMin + aXMax) * 0.5;
1177 vMiddle = (aYMin + aYMax) * 0.5;
1180 Standard_Real xShift = 0, yShift = 0;
1181 if ( mySurf->IsUClosed() )
1182 xShift = ShapeAnalysis::AdjustByPeriod ( 0.5*(aXMin + aXMax), uMiddle, uRange );
1183 if ( mySurf->IsVClosed() )
1184 yShift = ShapeAnalysis::AdjustByPeriod ( 0.5*(aYMin + aYMax), vMiddle, vRange ) ;
1185 aBox.Update(aXMin + xShift, aYMin + yShift, aXMax + xShift, aYMax + yShift);
1187 aWireBoxes.ChangeValue(i) = aBox;
1190 for ( i = 1; i <= nb; i ++) {
1191 TopoDS_Shape asw = ws.Value(i);
1192 TopoDS_Wire aw = TopoDS::Wire (asw);
1193 Bnd_Box2d aBox1 = aWireBoxes.Value(i);
1194 TopoDS_Shape dummy = myFace.EmptyCopied();
1195 TopoDS_Face af = TopoDS::Face ( dummy );
1196 // B.MakeFace (af,mySurf->Surface(),::Precision::Confusion());
1197 af.Orientation ( TopAbs_FORWARD );
1199 // PTV OCC945 06.11.2002 files ie_exhaust-A.stp (entities 3782, 3787)
1200 // tolerance is too big. It is seems that to identify placement of 2d point
1201 // it is enough Precision::PConfusion(), cause wea re know that 2d point in TopAbs_ON
1202 // BRepTopAdaptor_FClass2d clas (af,toluv);
1203 Standard_Boolean CheckShift = Standard_True;
1204 BRepTopAdaptor_FClass2d clas (af,::Precision::PConfusion());
1205 TopAbs_State sta = TopAbs_OUT;
1206 TopAbs_State staout = clas.PerformInfinitePoint();
1207 TopTools_ListOfShape IntWires;
1208 Standard_Integer aWireIt = 0;
1209 for ( Standard_Integer j = 1; j <= nbAll; j ++) {
1211 //if(i==j) continue;
1212 TopoDS_Shape aSh2 = allSubShapes.Value(j);
1215 TopAbs_State stb = TopAbs_UNKNOWN;
1216 if(aSh2.ShapeType() == TopAbs_VERTEX) {
1218 gp_Pnt aP = BRep_Tool::Pnt(TopoDS::Vertex(aSh2));
1219 gp_Pnt2d p2d = mySurf->ValueOfUV(aP,Precision::Confusion());
1220 stb = clas.Perform (p2d,Standard_False);
1221 if(stb == staout && (uclosed || vclosed)) {
1224 p2d1.SetCoord(p2d.X()+uRange, p2d.Y());
1225 stb = clas.Perform (p2d1,Standard_False);
1228 if(stb == staout && vclosed) {
1229 p2d1.SetCoord(p2d.X(), p2d.Y()+ vRange);
1230 stb = clas.Perform (p2d1,Standard_False);
1234 else if (aSh2.ShapeType() == TopAbs_WIRE) {
1235 CheckShift = Standard_True;
1236 TopoDS_Wire bw = TopoDS::Wire (aSh2);
1237 //Standard_Integer numin =0;
1238 Bnd_Box2d aBox2 = aWireBoxes.Value(aWireIt);
1239 if (aBox2.IsOut(aBox1))
1242 TopoDS_Iterator ew (bw);
1243 for(;ew.More(); ew.Next()) {
1244 TopoDS_Edge ed = TopoDS::Edge (ew.Value());
1245 Standard_Real cf,cl;
1246 Handle(Geom2d_Curve) cw = BRep_Tool::CurveOnSurface (ed,myFace,cf,cl);
1247 if (cw.IsNull()) continue;
1248 gp_Pnt2d unp = cw->Value ((cf+cl)/2.);
1249 TopAbs_State ste = clas.Perform (unp,Standard_False);
1250 if( ste==TopAbs_OUT || ste==TopAbs_IN ) {
1251 if(stb==TopAbs_UNKNOWN) {
1256 sta = TopAbs_UNKNOWN;
1264 Standard_Boolean found = Standard_False;
1266 if( stb == staout && CheckShift ) {
1267 CheckShift = Standard_False;
1269 unp1.SetCoord(unp.X()+uRange, unp.Y());
1270 found = (staout != clas.Perform (unp1,Standard_False));
1272 unp1.SetX(unp.X()-uRange);
1273 found = (staout != clas.Perform (unp1,Standard_False));
1276 if(vclosed&&!found) {
1277 unp1.SetCoord(unp.X(), unp.Y()+vRange);
1278 found = (staout != clas.Perform (unp1,Standard_False));
1280 unp1.SetY(unp.Y()-vRange);
1281 found = (staout != clas.Perform (unp1,Standard_False));
1284 // Additional check of diagonal steps for toroidal surfaces
1285 if (!found && uclosed && vclosed)
1287 for (Standard_Real dX = -1.0; dX <= 1.0 && !found; dX += 2.0)
1288 for (Standard_Real dY = -1.0; dY <= 1.0 && !found; dY += 2.0)
1290 unp1.SetCoord(unp.X() + uRange * dX, unp.Y() + vRange * dY);
1291 found = (staout != clas.Perform(unp1, Standard_False));
1296 if(stb==TopAbs_IN) stb = TopAbs_OUT;
1297 else stb = TopAbs_IN;
1298 Shift2dWire(bw,myFace,unp1.XY()-unp.XY(), mySurf);
1306 IntWires.Append(aSh2);
1307 MapIntWires.Add(aSh2);
1311 if (sta == TopAbs_UNKNOWN) { // ERREUR
1312 SendWarning ( aw, Message_Msg ( "FixAdvFace.FixOrientation.MSG11" ) );// Cannot orient wire
1315 MW.Bind(aw,IntWires);
1316 if(sta==TopAbs_OUT) {
1318 if(staout==TopAbs_IN) {
1319 // wire is OUT but InfinitePoint is IN => need to reverse
1320 ShapeExtend_WireData sewd (aw);
1321 sewd.Reverse(myFace);
1322 ws.SetValue (i,sewd.Wire());
1323 SendWarning ( sewd.Wire(), Message_Msg ( "FixAdvFace.FixOrientation.MSG5" ) );// Wire on face was reversed
1324 aSeqReversed.Append(i);
1325 done = Standard_True;
1326 SI.Bind(ws.Value(i),1);
1327 MapWires.Bind(ws.Value(i),IntWires);
1331 MapWires.Bind(aw,IntWires);
1335 if(staout==TopAbs_OUT) SI.Bind(aw,2);
1342 for(i=1; i<=nb; i++) {
1343 TopoDS_Wire aw = TopoDS::Wire (ws.Value(i));
1344 Standard_Integer tmpi = SI.Find(aw);
1346 if(!MapIntWires.Contains(aw)) {
1348 const TopTools_ListOfShape& IW = MW.Find(aw);
1350 // wire is OUT but InfinitePoint is IN => need to reverse
1351 ShapeExtend_WireData sewd (aw);
1352 sewd.Reverse(myFace);
1353 ws.SetValue (i,sewd.Wire());
1354 SendWarning ( sewd.Wire(), Message_Msg ( "FixAdvFace.FixOrientation.MSG5" ) );// Wire on face was reversed
1355 aSeqReversed.Append(i);
1356 done = Standard_True;
1357 MapWires.Bind(ws.Value(i),IW);
1359 else MapWires.Bind(aw,IW);
1363 // wire is IN but InfinitePoint is OUT => need to reverse
1364 ShapeExtend_WireData sewd (aw);
1365 sewd.Reverse(myFace);
1366 ws.SetValue (i,sewd.Wire());
1367 SendWarning ( sewd.Wire(), Message_Msg ( "FixAdvFace.FixOrientation.MSG5" ) );// Wire on face was reversed
1368 aSeqReversed.Append(i);
1369 done = Standard_True;
1377 //done = (done && (nb ==1 || (isAddNaturalBounds || (!isAddNaturalBounds && nbInternal <nb))));
1378 if(isAddNaturalBounds && nb == aSeqReversed.Length())
1379 done = Standard_False;
1381 done = (done && (nb ==1 || (isAddNaturalBounds || (!isAddNaturalBounds && nbInternal <nb))));
1382 // Faut-il reconstruire ? si myRebil est mis
1384 TopoDS_Shape S = myFace.EmptyCopied();
1385 S.Orientation ( TopAbs_FORWARD );
1386 Standard_Integer i = 1;
1387 for ( ; i <= nb; i++ )
1388 B.Add ( S, ws.Value(i) );
1391 for( i =1; i <= nbAll;i++) {
1392 TopoDS_Shape aS2 = allSubShapes.Value(i);
1393 if(aS2.ShapeType() != TopAbs_WIRE ||
1394 (aS2.Orientation() != TopAbs_FORWARD && aS2.Orientation() != TopAbs_REVERSED))
1399 if ( ! myFwd ) S.Orientation (TopAbs_REVERSED);
1400 if ( ! Context().IsNull() ) Context()->Replace ( myFace, S );
1401 myFace = TopoDS::Face ( S );
1402 BRepTools::Update(myFace);
1403 Standard_Integer k =1;
1404 for( ; k <= aSeqReversed.Length(); k++ )
1407 cout<<"Wire no "<<aSeqReversed.Value(k)<<" of "<<nb<<" reversed"<<endl; // mise au point !
1416 //=======================================================================
1417 //function : CheckWire
1418 //purpose : auxilary for FixMissingSeam
1419 //=======================================================================
1420 //:i7 abv 18 Sep 98: ProSTEP TR9 r0501-ug.stp: algorithm of fixing missing seam changed
1421 // test whether the wire is opened on period of periodical surface
1422 static Standard_Boolean CheckWire (const TopoDS_Wire &wire,
1423 const TopoDS_Face &face,
1424 const Standard_Real dU,
1425 const Standard_Real dV,
1426 Standard_Integer &isuopen,
1427 Standard_Integer &isvopen,
1428 Standard_Boolean &isDeg)
1433 ShapeAnalysis_Edge sae;
1435 isuopen = isvopen = 0;
1436 isDeg = Standard_True;
1437 for ( TopoDS_Iterator ed(wire); ed.More(); ed.Next() ) {
1438 TopoDS_Edge edge = TopoDS::Edge ( ed.Value() );
1439 if ( ! BRep_Tool::Degenerated ( edge ) ) isDeg = Standard_False;
1440 Handle(Geom2d_Curve) c2d;
1442 if ( ! sae.PCurve ( edge, face, c2d, f, l, Standard_True ) )
1443 return Standard_False;
1444 vec += c2d->Value(l).XY() - c2d->Value(f).XY();
1447 Standard_Real aDelta = Abs(vec.X())-dU;
1448 if(Abs(aDelta) < 0.1*dU)
1464 aDelta = Abs(vec.Y())-dV;
1465 if(Abs(aDelta) < 0.1*dV)
1481 return isuopen || isvopen;
1484 //=======================================================================
1485 //function : FixMissingSeam
1487 //=======================================================================
1488 Standard_Boolean ShapeFix_Face::FixMissingSeam()
1490 Standard_Boolean uclosed = mySurf->IsUClosed();
1491 Standard_Boolean vclosed = mySurf->IsVClosed();
1493 if ( ! uclosed && ! vclosed ) return Standard_False;
1495 if ( ! Context().IsNull() ) {
1496 TopoDS_Shape S = Context()->Apply ( myFace );
1497 myFace = TopoDS::Face ( S );
1500 //%pdn: surface should be made periodic before (see ShapeCustom_Surface)!
1501 if (mySurf->Surface()->IsKind(STANDARD_TYPE (Geom_BSplineSurface))) {
1502 Handle (Geom_BSplineSurface) BSpl = Handle (Geom_BSplineSurface)::DownCast (mySurf->Surface());
1503 if (!BSpl->IsUPeriodic() && !BSpl->IsVPeriodic())
1504 return Standard_False;
1507 Standard_Real URange, VRange, SUF, SUL, SVF, SVL;
1508 mySurf->Bounds ( SUF, SUL, SVF, SVL );
1509 Standard_Real fU1,fU2,fV1,fV2;
1510 BRepTools::UVBounds(myFace,fU1,fU2,fV1,fV2);
1512 //pdn OCC55 fix to faces without the wires to avoid identical first and last parameters
1513 if ( ::Precision::IsInfinite ( SUF ) || ::Precision::IsInfinite ( SUL ) ) {
1514 if ( ::Precision::IsInfinite ( SUF ) ) SUF = fU1;
1515 if ( ::Precision::IsInfinite ( SUL ) ) SUL = fU2;
1516 if(Abs(SUL-SUF) < ::Precision::PConfusion()) {
1517 if ( ::Precision::IsInfinite ( SUF ) ) SUF-=1000.;
1521 if ( ::Precision::IsInfinite ( SVF ) || ::Precision::IsInfinite ( SVL ) ) {
1522 if ( ::Precision::IsInfinite ( SVF ) ) SVF = fV1;
1523 if ( ::Precision::IsInfinite ( SVL ) ) SVL = fV2;
1524 if(Abs(SVL-SVF) < ::Precision::PConfusion()) {
1525 if ( ::Precision::IsInfinite ( SVF ) ) SVF-=1000.;
1530 URange = Min(Abs (SUL - SUF), Precision::Infinite());
1531 VRange = Min(Abs(SVL - SVF), Precision::Infinite());
1532 // Standard_Real UTol = 0.2 * URange, VTol = 0.2 * VRange;
1533 Standard_Integer ismodeu = 0, ismodev = 0; //szv#4:S4163:12Mar99 was Boolean
1534 Standard_Integer isdeg1=0, isdeg2=0;
1536 TopTools_SequenceOfShape ws;
1537 TopTools_SequenceOfShape aSeqNonManif;
1538 for ( TopoDS_Iterator wi(myFace,Standard_False); wi.More(); wi.Next() ) {
1539 if(wi.Value().ShapeType() != TopAbs_WIRE ||
1540 (wi.Value().Orientation() != TopAbs_FORWARD && wi.Value().Orientation() != TopAbs_REVERSED)) {
1541 aSeqNonManif.Append(wi.Value());
1544 ws.Append ( wi.Value() );
1549 for ( i=1; i <= ws.Length(); i++ ) {
1550 TopoDS_Wire wire = TopoDS::Wire ( ws.Value(i) );
1551 Standard_Integer isuopen, isvopen;
1552 Standard_Boolean isdeg;
1553 if ( ! CheckWire ( wire, myFace, URange, VRange, isuopen, isvopen, isdeg ) )
1555 if ( w1.IsNull() ) { w1 = wire; ismodeu = isuopen; ismodev = isvopen; isdeg1 = isdeg ? i : 0; }
1556 else if ( w2.IsNull() ) {
1557 if ( ismodeu == -isuopen && ismodev == -isvopen ) { w2 = wire; isdeg2 = isdeg ? i : 0; }
1558 else if ( ismodeu == isuopen && ismodev == isvopen ) {
1561 //:abv 29.08.01: If wires are contraversal, reverse one of them
1562 // If first one is single degenerated edge, reverse it; else second
1571 if ( ! isdeg2 ) cout << "Warning: ShapeFix_Face::FixMissingSeam(): wire reversed" << endl;
1576 else cout << "Warning: ShapeFix_Face::FixMissingSeam(): incompatible open wires" << endl;
1579 // else return Standard_False; // abort
1582 cout << "Warning: ShapeFix_Face::FixMissingSeam(): more than two open wires detected!" << endl;
1584 //:abv 30.08.09: if more than one open wires and more than two of them are
1585 // completely degenerated, remove any of them
1586 if ( isdeg || isdeg1 || isdeg2 ) {
1587 ws.Remove ( isdeg ? i : isdeg2 ? isdeg2 : isdeg1 );
1592 cout << "Warning: ShapeFix_Face::FixMissingSeam(): open degenerated wire removed" << endl;
1600 if ( w1.IsNull() ) return Standard_False;
1601 else if ( w2.IsNull()) {
1602 // For spheres and BSpline cone-like surfaces(bug 24055):
1603 // If only one of wires limiting face on surface is open in 2d,
1604 // this may means that degenerated edge should be added, and
1605 // then usual procedure applied
1608 Standard_Real aRange;
1610 if ( ismodeu && mySurf->Surface()->IsKind(STANDARD_TYPE(Geom_SphericalSurface)) ) {
1611 p.SetCoord ( ( ismodeu < 0 ? 0. : 2.*M_PI ), ismodeu * 0.5 * M_PI );
1612 Standard_Real aXCoord = -ismodeu;
1613 d.SetCoord ( aXCoord, 0.);
1616 else if ( ismodev && mySurf->Surface()->IsKind(STANDARD_TYPE(Geom_BSplineSurface))) {
1617 Standard_Real uCoord;
1618 if (mySurf->Value(SUF, SVF).Distance(mySurf->Value(SUF, (SVF + SVL) / 2)) < ::Precision::Confusion())
1620 else if (mySurf->Value(SUL, SVF).Distance(mySurf->Value(SUL, (SVF + SVL) / 2)) < ::Precision::Confusion())
1622 else return Standard_False;
1624 p.SetCoord ( uCoord, ( ismodev < 0 ? 0. : VRange ) );
1625 d.SetCoord ( 0., -ismodev);
1628 else if ( ismodeu && mySurf->Surface()->IsKind(STANDARD_TYPE(Geom_BSplineSurface))) {
1629 Standard_Real vCoord;
1630 if (mySurf->Value(SUF, SVF).Distance(mySurf->Value((SUF + SUL) / 2, SVF)) < ::Precision::Confusion())
1632 else if (mySurf->Value(SUL, SVL).Distance(mySurf->Value((SUF + SUL) / 2, SVL)) < ::Precision::Confusion())
1634 else return Standard_False;
1636 p.SetCoord ( ( ismodeu < 0 ? 0. : URange ), vCoord );
1637 Standard_Real aXCoord = -ismodeu;
1638 d.SetCoord ( aXCoord, 0.);
1641 else return Standard_False;
1643 Handle(Geom2d_Line) line = new Geom2d_Line ( p, d );
1645 B.MakeEdge ( edge );
1646 B.Degenerated ( edge, Standard_True );
1647 B.UpdateEdge ( edge, line, myFace, ::Precision::Confusion() );
1648 B.Range ( edge, myFace, 0., aRange );
1650 B.MakeVertex ( V, mySurf->Value ( p.X(), p.Y() ), ::Precision::Confusion() );
1651 V.Orientation(TopAbs_FORWARD);
1653 V.Orientation(TopAbs_REVERSED);
1660 // sort original wires
1661 Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire;
1662 sfw->SetFace ( myFace );
1663 sfw->SetPrecision ( Precision() );
1665 Handle(ShapeExtend_WireData) wd1 = new ShapeExtend_WireData ( w1 );
1666 Handle(ShapeExtend_WireData) wd2 = new ShapeExtend_WireData ( w2 );
1669 // Standard_Boolean degenerated = ( secondDeg != firstDeg );
1670 // if ( ! degenerated ) {
1677 //:abv 29.08.01: reconstruct face taking into account reversing
1678 TopoDS_Shape dummy = myFace.EmptyCopied();
1679 TopoDS_Face tmpF = TopoDS::Face ( dummy );
1680 tmpF.Orientation ( TopAbs_FORWARD );
1681 for ( i=1; i <= ws.Length(); i++ ) {
1682 TopoDS_Wire wire = TopoDS::Wire ( ws.Value(i) );
1683 if ( wire.IsSame ( w1 ) ) wire = w1;
1684 else if ( wire.IsSame ( w2 ) ) wire = w2;
1685 B.Add ( tmpF, wire );
1687 tmpF.Orientation ( myFace.Orientation() );
1689 Standard_Real uf=SUF, vf=SVF;
1691 // A special kind of FixShifted is necessary for torus-like
1692 // surfaces to adjust wires by period ALONG the missing SEAM direction
1693 // tr9_r0501-ug.stp #187640
1694 if ( uclosed && vclosed ) {
1695 Standard_Integer coord = ( ismodeu ? 1 : 0 );
1696 Standard_Integer isneg = ( ismodeu ? ismodeu : -ismodev );
1697 Standard_Real period = ( ismodeu ? URange : VRange );
1699 Standard_Real m1[2][2], m2[2][2];
1700 S = tmpF.EmptyCopied();
1702 ShapeAnalysis::GetFaceUVBounds (TopoDS::Face(S), m1[0][0], m1[0][1], m1[1][0], m1[1][1]);
1703 S = tmpF.EmptyCopied();
1705 ShapeAnalysis::GetFaceUVBounds (TopoDS::Face(S), m2[0][0], m2[0][1], m2[1][0], m2[1][1]);
1706 Standard_Real shiftw2 =
1707 ShapeAnalysis::AdjustByPeriod ( 0.5 * ( m2[coord][0] + m2[coord][1] ),
1708 0.5 * ( m1[coord][0] + m1[coord][1] +
1709 isneg * ( period + ::Precision::PConfusion() ) ),
1711 m1[coord][0] = Min ( m1[coord][0], m2[coord][0] + shiftw2 );
1712 m1[coord][1] = Max ( m1[coord][1], m2[coord][1] + shiftw2 );
1713 for ( TopoDS_Iterator it(tmpF,Standard_False); it.More(); it.Next() ) {
1714 if(it.Value().ShapeType() != TopAbs_WIRE)
1716 TopoDS_Wire w = TopoDS::Wire ( it.Value() );
1717 if ( w == w1 ) continue;
1718 Standard_Real shift;
1719 if ( w == w2 ) shift = shiftw2;
1721 S = tmpF.EmptyCopied();
1723 ShapeAnalysis::GetFaceUVBounds (TopoDS::Face(S), m2[0][0], m2[0][1], m2[1][0], m2[1][1]);
1724 shift = ShapeAnalysis::AdjustByPeriod ( 0.5 * ( m2[coord][0] + m2[coord][1] ),
1725 0.5 * ( m1[coord][0] + m1[coord][1] ), period );
1727 if ( shift != 0. ) {
1729 V.SetCoord ( coord+1, shift );
1730 ShapeAnalysis_Edge sae;
1731 for ( TopoDS_Iterator iw(w); iw.More(); iw.Next() ) {
1732 TopoDS_Edge E = TopoDS::Edge ( iw.Value() );
1733 Handle(Geom2d_Curve) C;
1735 if ( ! sae.PCurve ( E, tmpF, C, a, b ) ) continue;
1740 // abv 05 Feb 02: OCC34
1741 // by the way, select proper split place by V to avoid extra intersections
1742 if ( m1[coord][1] - m1[coord][0] <= period ) {
1743 Standard_Real other = 0.5 * ( m1[coord][0] + m1[coord][1] - period );
1744 if ( ismodeu ) vf = other;
1749 // find the best place by u and v to insert a seam
1750 // (so as to minimize splitting edges as possible)
1751 ShapeAnalysis_Edge sae;
1752 Standard_Integer foundU=0, foundV=0;
1753 Standard_Integer nb1 = wd1->NbEdges();
1754 Standard_Integer nb2 = wd2->NbEdges();
1755 for ( Standard_Integer i1 = 1; i1 <= nb1 + nb2; i1++ ) {
1756 TopoDS_Edge edge1 = ( i1 <= nb1 ? wd1->Edge ( i1 ) : wd2->Edge ( i1-nb1 ) );
1757 Handle(Geom2d_Curve) c2d;
1759 if ( ! sae.PCurve ( edge1, tmpF, c2d, f, l, Standard_True ) ) return Standard_False;
1760 gp_Pnt2d pos1 = c2d->Value(l).XY();
1761 // the best place is end of edge which is nearest to 0
1762 Standard_Boolean skipU = ! uclosed;
1763 if ( uclosed && ismodeu ) {
1764 pos1.SetX ( pos1.X() + ShapeAnalysis::AdjustByPeriod ( pos1.X(), SUF, URange ) );
1765 if ( foundU ==2 && Abs ( pos1.X() ) > Abs(uf) ) skipU = Standard_True;
1766 else if ( ! foundU || ( foundU ==1 && Abs ( pos1.X() ) < Abs(uf) ) ) {
1771 Standard_Boolean skipV = ! vclosed;
1772 if ( vclosed && ! ismodeu ) {
1773 pos1.SetY ( pos1.Y() + ShapeAnalysis::AdjustByPeriod ( pos1.Y(), SVF, VRange ) );
1774 if ( foundV ==2 && Abs ( pos1.Y() ) > Abs(vf) ) skipV = Standard_True;
1775 else if ( ! foundV || ( foundV ==1 && Abs ( pos1.Y() ) < Abs(vf) ) ) {
1780 if ( skipU && skipV ) {
1781 if ( i1 <= nb1 ) continue;
1784 // or yet better - if it is end of some edges on both wires
1785 for ( Standard_Integer i2 = 1; i1 <= nb1 && i2 <= nb2; i2++ ) {
1786 TopoDS_Edge edge2 = wd2->Edge ( i2 );
1787 if ( ! sae.PCurve ( edge2, tmpF, c2d, f, l, Standard_True ) ) return Standard_False;
1788 gp_Pnt2d pos2 = c2d->Value(f).XY();
1789 if ( uclosed && ismodeu ) {
1790 pos2.SetX ( pos2.X() + ShapeAnalysis::AdjustByPeriod ( pos2.X(), pos1.X(), URange ) );
1791 if ( Abs ( pos2.X() - pos1.X() ) < ::Precision::PConfusion() &&
1792 ( foundU != 2 || Abs ( pos1.X() ) < Abs ( uf ) ) ) {
1797 if ( vclosed && ! ismodeu ) {
1798 pos2.SetY ( pos2.Y() + ShapeAnalysis::AdjustByPeriod ( pos2.Y(), pos1.Y(), VRange ) );
1799 if ( Abs ( pos2.Y() - pos1.Y() ) < ::Precision::PConfusion() &&
1800 ( foundV != 2 || Abs ( pos1.Y() ) < Abs ( vf ) ) ) {
1808 //pdn fixing RTS on offsets
1809 if ( uf < SUF || uf > SUL )
1810 uf+=ShapeAnalysis::AdjustToPeriod(uf,SUF,SUF+URange);
1811 if ( vf < SVF || vf > SVL )
1812 vf+=ShapeAnalysis::AdjustToPeriod(vf,SVF,SVF+VRange);
1814 // Create fictive grid and call ComposeShell to insert a seam
1815 Handle(Geom_RectangularTrimmedSurface) RTS =
1816 new Geom_RectangularTrimmedSurface ( mySurf->Surface(), uf, uf+URange, vf, vf+VRange );
1817 Handle(TColGeom_HArray2OfSurface) grid = new TColGeom_HArray2OfSurface ( 1, 1, 1, 1 );
1818 grid->SetValue ( 1, 1, RTS ); //mySurf->Surface() );
1819 Handle(ShapeExtend_CompositeSurface) G = new ShapeExtend_CompositeSurface ( grid );
1822 //addition non-manifold topology
1823 Standard_Integer j=1;
1824 for( ; j <= aSeqNonManif.Length(); j++)
1825 B.Add(tmpF,aSeqNonManif.Value(j));
1827 ShapeFix_ComposeShell CompShell;
1828 // TopoDS_Face tmpF = myFace;
1829 // tmpF.Orientation(TopAbs_FORWARD);
1830 CompShell.Init ( G, L, tmpF, ::Precision::Confusion() );//myPrecision
1831 if ( Context().IsNull() ) SetContext ( new ShapeBuild_ReShape );
1832 CompShell.ClosedMode() = Standard_True;
1833 CompShell.SetContext( Context() );
1834 CompShell.SetMaxTolerance(MaxTolerance());
1835 CompShell.Perform();
1837 // abv 03.07.00: CAX-IF TRJ4: trj4_k1_goe-tu-214.stp: #785: reset mySurf
1838 mySurf = new ShapeAnalysis_Surface ( RTS );
1840 myResult = CompShell.Result();
1841 // if ( myFace.Orientation() == TopAbs_REVERSED ) res.Reverse();
1842 Context()->Replace ( myFace, myResult );
1843 for (TopExp_Explorer exp ( myResult, TopAbs_FACE ); exp.More(); exp.Next() ) {
1844 myFace = TopoDS::Face ( exp.Current() );
1845 BRepTools::Update(myFace); //:p4
1848 SendWarning ( Message_Msg ( "FixAdvFace.FixMissingSeam.MSG0" ) );// Missing seam-edge added
1849 return Standard_True;
1852 //=======================================================================
1853 //function : FixSmallAreaWire
1855 //=======================================================================
1856 //%14 pdn 24.02.99 PRO10109, USA60293 fix wire on face with small area.
1857 Standard_Boolean ShapeFix_Face::FixSmallAreaWire(const Standard_Boolean theIsRemoveSmallFace)
1859 if ( !Context().IsNull() )
1861 TopoDS_Shape aShape = Context()->Apply(myFace);
1862 myFace = TopoDS::Face(aShape);
1865 BRep_Builder aBuilder;
1866 Standard_Integer nbRemoved = 0, nbWires = 0;
1868 TopoDS_Shape anEmptyCopy = myFace.EmptyCopied();
1869 TopoDS_Face aFace = TopoDS::Face(anEmptyCopy);
1871 const TopoDS_Wire anOuterWire = BRepTools::OuterWire(myFace);
1872 const Standard_Real aTolerance3d = ShapeFix_Root::Precision();
1873 for (TopoDS_Iterator aWIt(myFace, Standard_False); aWIt.More(); aWIt.Next())
1875 const TopoDS_Shape& aShape = aWIt.Value();
1876 if ( aShape.ShapeType() != TopAbs_WIRE &&
1877 aShape.Orientation() != TopAbs_FORWARD &&
1878 aShape.Orientation() != TopAbs_REVERSED )
1883 const TopoDS_Wire& aWire = TopoDS::Wire(aShape);
1884 const Standard_Boolean isOuterWire = anOuterWire.IsEqual(aWire);
1885 Handle(ShapeAnalysis_Wire) anAnalyzer = new ShapeAnalysis_Wire(aWire, myFace, aTolerance3d);
1886 if ( anAnalyzer->CheckSmallArea(aWire, isOuterWire) )
1888 // Null area wire detected, wire skipped
1889 SendWarning(aWire, Message_Msg("FixAdvFace.FixSmallAreaWire.MSG0"));
1894 aBuilder.Add(aFace, aWire);
1899 if ( nbRemoved <= 0 )
1900 return Standard_False;
1905 cout << "Warning: ShapeFix_Face: All wires on a face have small area; left untouched" << endl;
1907 if ( theIsRemoveSmallFace && !Context().IsNull() )
1908 Context()->Remove(myFace);
1910 return Standard_False;
1913 cout << "Warning: ShapeFix_Face: " << nbRemoved << " small area wire(s) removed" << endl;
1915 if ( !Context().IsNull() )
1916 Context()->Replace(myFace, aFace);
1919 return Standard_True;
1921 //=======================================================================
1922 //function : FixLoopWire
1924 //=======================================================================
1925 static void FindNext(const TopoDS_Shape& aVert,
1926 const TopoDS_Shape& ainitEdge,
1927 TopTools_IndexedMapOfShape& aMapVertices,
1928 TopTools_DataMapOfShapeListOfShape& aMapVertexEdges,
1929 const TopTools_MapOfShape& aMapSmallEdges,
1930 const TopTools_MapOfShape& aMapSeemEdges,
1931 TopTools_MapOfShape& aMapEdges,
1932 Handle(ShapeExtend_WireData)& aWireData)
1934 TopoDS_Iterator aItV(ainitEdge);
1935 TopoDS_Shape anextVert = aVert;
1936 Standard_Boolean isFind = Standard_False;
1937 for( ; aItV.More() && !isFind; aItV.Next())
1939 if(!aItV.Value().IsSame(aVert) ) {
1940 isFind = Standard_True;
1941 anextVert = aItV.Value();
1946 if(!isFind && !aMapSmallEdges.Contains(ainitEdge))
1948 if(isFind && aMapVertices.Contains(anextVert))
1951 const TopTools_ListOfShape& aledges = aMapVertexEdges.Find(anextVert);
1952 TopTools_ListIteratorOfListOfShape liter(aledges);
1953 isFind = Standard_False;
1954 TopoDS_Shape anextEdge;
1955 for( ; liter.More() && !isFind; liter.Next())
1957 if(!aMapEdges.Contains(liter.Value()) && !liter.Value().IsSame(ainitEdge)) {
1958 anextEdge = liter.Value();
1959 aWireData->Add(anextEdge);
1960 if(aMapSeemEdges.Contains(anextEdge))
1961 aWireData->Add(anextEdge.Reversed());
1962 isFind = Standard_True;
1963 aMapEdges.Add(anextEdge);
1964 FindNext(anextVert,anextEdge,aMapVertices,aMapVertexEdges,aMapSmallEdges,aMapSeemEdges,aMapEdges,aWireData);
1970 static Standard_Boolean isClosed2D(const TopoDS_Face& aFace,const TopoDS_Wire& aWire)
1972 Standard_Boolean isClosed = Standard_True;
1973 Handle(ShapeAnalysis_Wire) asaw = new ShapeAnalysis_Wire(aWire,aFace,Precision::Confusion());
1974 for (Standard_Integer i = 1; i <= asaw->NbEdges() && isClosed; i++) {
1975 TopoDS_Edge edge1 = asaw->WireData()->Edge(i);
1976 //checking that wire is closed in 2D space with tolerance of vertex.
1977 ShapeAnalysis_Edge sae;
1978 TopoDS_Vertex v1 = sae.FirstVertex(edge1);
1979 asaw->SetPrecision(BRep_Tool::Tolerance(v1));
1980 asaw->CheckGap2d(i);
1981 isClosed = (asaw->LastCheckStatus(ShapeExtend_OK));
1987 //=======================================================================
1988 //function : FixLoopWire
1990 //=======================================================================
1992 Standard_Boolean ShapeFix_Face::FixLoopWire(TopTools_SequenceOfShape& aResWires)
1994 TopTools_IndexedMapOfShape aMapVertices;
1995 TopTools_DataMapOfShapeListOfShape aMapVertexEdges;
1996 TopTools_MapOfShape aMapSmallEdges;
1997 TopTools_MapOfShape aMapSeemEdges;
1998 if(!FixWireTool()->Analyzer()->CheckLoop(aMapVertices, aMapVertexEdges,aMapSmallEdges,aMapSeemEdges))
1999 return Standard_False;
2002 TopTools_MapOfShape aMapEdges;
2003 TopTools_SequenceOfShape aSeqWires;
2005 //collecting wires from common vertex belonging more than 2 edges
2006 Standard_Integer i =1;
2007 for( ; i <= aMapVertices.Extent(); i++) {
2008 TopoDS_Shape aVert = aMapVertices.FindKey(i);
2009 const TopTools_ListOfShape& aledges = aMapVertexEdges.Find(aVert);
2010 TopTools_ListIteratorOfListOfShape liter(aledges);
2011 for( ; liter.More(); liter.Next())
2013 TopoDS_Edge Edge = TopoDS::Edge(liter.Value());
2014 if(aMapEdges.Contains(Edge))
2017 Handle(ShapeExtend_WireData) aWireData = new ShapeExtend_WireData;
2018 aWireData->Add(Edge);
2019 if(aMapSeemEdges.Contains(Edge))
2020 aWireData->Add(Edge.Reversed());
2021 aMapEdges.Add(Edge);
2022 FindNext(aVert,Edge,aMapVertices,aMapVertexEdges,aMapSmallEdges,aMapSeemEdges,aMapEdges,aWireData);
2023 if(aWireData->NbEdges() ==1 && aMapSmallEdges.Contains(aWireData->Edge(1)))
2025 TopoDS_Vertex aV1,aV2;
2026 TopoDS_Wire aWire = aWireData->Wire();
2027 TopExp::Vertices(aWire,aV1,aV2);
2029 if(aV1.IsSame(aV2)) {
2030 Handle(ShapeExtend_WireData) asewd = new ShapeExtend_WireData(aWire);
2031 Handle(ShapeFix_Wire) asfw = new ShapeFix_Wire;
2034 TopoDS_Wire awire2 = asfw->Wire();
2035 aResWires.Append(awire2);
2038 else aSeqWires.Append(aWireData->Wire());
2043 if(aSeqWires.Length() ==1) {
2044 aResWires.Append(aSeqWires.Value(1));
2047 //collecting whole wire from two not closed wires having two common vertices.
2048 for( i =1; i <= aSeqWires.Length(); i++) {
2049 TopoDS_Vertex aV1,aV2;
2050 TopoDS_Wire aWire = TopoDS::Wire(aSeqWires.Value(i));
2051 TopExp::Vertices(aWire,aV1,aV2);
2052 Standard_Integer j = i+1;
2053 for( ; j <= aSeqWires.Length(); j++)
2055 TopoDS_Vertex aV21,aV22;
2056 TopoDS_Wire aWire2 = TopoDS::Wire(aSeqWires.Value(j));
2057 TopExp::Vertices(aWire2,aV21,aV22);
2058 if((aV1.IsSame(aV21) || aV1.IsSame(aV22)) && (aV2.IsSame(aV21) || aV2.IsSame(aV22)))
2060 Handle(ShapeExtend_WireData) asewd = new ShapeExtend_WireData(aWire);
2062 Handle(ShapeFix_Wire) asfw = new ShapeFix_Wire;
2065 aResWires.Append(asfw->Wire());
2066 aSeqWires.Remove(j--);
2067 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE7 );
2072 if(j <= aSeqWires.Length())
2073 aSeqWires.Remove(i--);
2076 if(aSeqWires.Length()<3) {
2077 for( i =1; i <= aSeqWires.Length(); i++)
2078 aResWires.Append(aSeqWires.Value(i));
2082 //collecting wires having one common vertex
2083 for( i =1; i <= aSeqWires.Length(); i++) {
2084 TopoDS_Vertex aV1,aV2;
2085 TopoDS_Wire aWire = TopoDS::Wire(aSeqWires.Value(i));
2086 TopExp::Vertices(aWire,aV1,aV2);
2087 Standard_Integer j =i+1;
2088 for( ; j <= aSeqWires.Length(); j++)
2090 TopoDS_Vertex aV21,aV22;
2091 TopoDS_Wire aWire2 = TopoDS::Wire(aSeqWires.Value(j));
2092 TopExp::Vertices(aWire2,aV21,aV22);
2093 if((aV1.IsSame(aV21) || aV1.IsSame(aV22)) || (aV2.IsSame(aV21) || aV2.IsSame(aV22)))
2095 Handle(ShapeExtend_WireData) asewd = new ShapeExtend_WireData(aWire);
2097 Handle(ShapeFix_Wire) asfw = new ShapeFix_Wire;
2100 aWire = asfw->Wire();
2101 TopExp::Vertices(aWire,aV1,aV2);
2102 aSeqWires.Remove(j--);
2103 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE7 );
2106 aResWires.Append(aWire);
2111 Standard_Boolean isClosed = Standard_True;
2113 //checking that obtained wires is closed in 2D space
2114 if (mySurf->Adaptor3d()->GetType() != GeomAbs_Plane) {
2116 TopoDS_Shape emptyCopied = myFace.EmptyCopied();
2117 TopoDS_Face tmpFace = TopoDS::Face(emptyCopied);
2118 tmpFace.Orientation ( TopAbs_FORWARD );
2120 for(i =1; i <= aResWires.Length() && isClosed; i++) {
2121 TopoDS_Wire awire = TopoDS::Wire(aResWires.Value(i));
2122 isClosed = isClosed2D(tmpFace,awire);
2126 Standard_Boolean isDone =(aResWires.Length() && isClosed);
2127 if(isDone && aResWires.Length() >1)
2130 cout<<"Wire was splitted on "<<aResWires.Length()<<" wires"<< endl;
2137 //=======================================================================
2138 //function : SplitEdge
2140 //=======================================================================
2142 Standard_Boolean ShapeFix_Face::SplitEdge(const Handle(ShapeExtend_WireData)& sewd,
2143 const Standard_Integer num,
2144 const Standard_Real param,
2145 const TopoDS_Vertex& vert,
2146 const Standard_Real preci,
2147 ShapeFix_DataMapOfShapeBox2d& boxes)
2149 TopoDS_Edge edge = sewd->Edge(num);
2150 TopoDS_Edge newE1, newE2;
2151 ShapeFix_SplitTool aTool;
2152 if(aTool.SplitEdge(edge,param,vert,myFace,newE1,newE2,preci,0.01*preci)) {
2154 Handle(ShapeExtend_WireData) wd = new ShapeExtend_WireData;
2157 if(!Context().IsNull()) Context()->Replace( edge, wd->Wire() );
2158 for (TopExp_Explorer exp ( wd->Wire(), TopAbs_EDGE ); exp.More(); exp.Next() ) {
2159 TopoDS_Edge E = TopoDS::Edge ( exp.Current() );
2160 BRepTools::Update(E);
2163 // for ( Standard_Integer i=1; i <= sewd->NbEdges(); i++ ) {
2164 // TopoDS_Edge E = sewd->Edge(i);
2165 // TopoDS_Shape S = Context()->Apply ( E );
2166 // if ( S == E ) continue;
2167 // for ( TopExp_Explorer exp(S,TopAbs_EDGE); exp.More(); exp.Next() )
2168 // sewd->Add ( exp.Current(), i++ );
2169 // sewd->Remove ( i-- );
2172 // change sewd and boxes
2173 sewd->Set(newE1,num);
2174 if(num==sewd->NbEdges())
2177 sewd->Add(newE2,num+1);
2181 const Handle(Geom_Surface)& S = BRep_Tool::Surface(myFace,L);
2182 Handle(Geom2d_Curve) c2d;
2183 Standard_Real cf,cl;
2184 ShapeAnalysis_Edge sae;
2185 if(sae.PCurve(newE1,S,L,c2d,cf,cl,Standard_False)) {
2187 Geom2dAdaptor_Curve gac;
2188 Standard_Real aFirst = c2d->FirstParameter();
2189 Standard_Real aLast = c2d->LastParameter();
2190 if(c2d->IsKind(STANDARD_TYPE(Geom2d_BSplineCurve))
2191 && (cf < aFirst || cl > aLast)) {
2192 //pdn avoiding problems with segment in Bnd_Box
2196 gac.Load(c2d,cf,cl);
2197 BndLib_Add2dCurve::Add(gac,::Precision::Confusion(),box);
2198 boxes.Bind(newE1,box);
2200 if(sae.PCurve(newE2,S,L,c2d,cf,cl,Standard_False)) {
2202 Geom2dAdaptor_Curve gac;
2203 Standard_Real aFirst = c2d->FirstParameter();
2204 Standard_Real aLast = c2d->LastParameter();
2205 if(c2d->IsKind(STANDARD_TYPE(Geom2d_BSplineCurve))
2206 && (cf < aFirst || cl > aLast)) {
2207 //pdn avoiding problems with segment in Bnd_Box
2211 gac.Load(c2d,cf,cl);
2212 BndLib_Add2dCurve::Add(gac,::Precision::Confusion(),box);
2213 boxes.Bind(newE2,box);
2215 return Standard_True;
2217 return Standard_False;
2221 //=======================================================================
2222 //function : SplitEdge
2224 //=======================================================================
2226 Standard_Boolean ShapeFix_Face::SplitEdge(const Handle(ShapeExtend_WireData)& sewd,
2227 const Standard_Integer num,
2228 const Standard_Real param1,
2229 const Standard_Real param2,
2230 const TopoDS_Vertex& vert,
2231 const Standard_Real preci,
2232 ShapeFix_DataMapOfShapeBox2d& boxes)
2234 TopoDS_Edge edge = sewd->Edge(num);
2235 TopoDS_Edge newE1, newE2;
2236 ShapeFix_SplitTool aTool;
2237 if(aTool.SplitEdge(edge,param1,param2,vert,myFace,newE1,newE2,preci,0.01*preci)) {
2239 Handle(ShapeExtend_WireData) wd = new ShapeExtend_WireData;
2242 if(!Context().IsNull()) Context()->Replace( edge, wd->Wire() );
2243 for (TopExp_Explorer exp ( wd->Wire(), TopAbs_EDGE ); exp.More(); exp.Next() ) {
2244 TopoDS_Edge E = TopoDS::Edge ( exp.Current() );
2245 BRepTools::Update(E);
2248 // change sewd and boxes
2249 sewd->Set(newE1,num);
2250 if(num==sewd->NbEdges())
2253 sewd->Add(newE2,num+1);
2257 const Handle(Geom_Surface)& S = BRep_Tool::Surface(myFace,L);
2258 Handle(Geom2d_Curve) c2d;
2259 Standard_Real cf,cl;
2260 ShapeAnalysis_Edge sae;
2261 if(sae.PCurve(newE1,S,L,c2d,cf,cl,Standard_False)) {
2263 Geom2dAdaptor_Curve gac;
2264 Standard_Real aFirst = c2d->FirstParameter();
2265 Standard_Real aLast = c2d->LastParameter();
2266 if(c2d->IsKind(STANDARD_TYPE(Geom2d_BSplineCurve))
2267 && (cf < aFirst || cl > aLast)) {
2268 //pdn avoiding problems with segment in Bnd_Box
2272 gac.Load(c2d,cf,cl);
2273 BndLib_Add2dCurve::Add(gac,::Precision::Confusion(),box);
2274 boxes.Bind(newE1,box);
2276 if(sae.PCurve(newE2,S,L,c2d,cf,cl,Standard_False)) {
2278 Geom2dAdaptor_Curve gac;
2279 Standard_Real aFirst = c2d->FirstParameter();
2280 Standard_Real aLast = c2d->LastParameter();
2281 if(c2d->IsKind(STANDARD_TYPE(Geom2d_BSplineCurve))
2282 && (cf < aFirst || cl > aLast)) {
2283 //pdn avoiding problems with segment in Bnd_Box
2287 gac.Load(c2d,cf,cl);
2288 BndLib_Add2dCurve::Add(gac,::Precision::Confusion(),box);
2289 boxes.Bind(newE2,box);
2291 return Standard_True;
2293 return Standard_False;
2297 //=======================================================================
2298 //function : FixIntersectingWires
2300 //=======================================================================
2302 Standard_Boolean ShapeFix_Face::FixIntersectingWires()
2304 ShapeFix_IntersectionTool ITool(Context(),Precision(),MaxTolerance());
2305 return ITool.FixIntersectingWires(myFace);
2309 //=======================================================================
2310 //function : FixWiresTwoCoincEdges
2312 //=======================================================================
2314 Standard_Boolean ShapeFix_Face::FixWiresTwoCoincEdges()
2316 if ( ! Context().IsNull() ) {
2317 TopoDS_Shape S = Context()->Apply ( myFace );
2318 myFace = TopoDS::Face ( S );
2321 TopAbs_Orientation ori = myFace.Orientation();
2322 TopoDS_Shape emptyCopied = myFace.EmptyCopied();
2323 TopoDS_Face face = TopoDS::Face (emptyCopied);
2324 face.Orientation(TopAbs_FORWARD);
2325 Standard_Integer nbWires = 0;
2328 for (TopoDS_Iterator it (myFace, Standard_False); it.More(); it.Next()) {
2329 if(it.Value().ShapeType() != TopAbs_WIRE ||
2330 (it.Value().Orientation() != TopAbs_FORWARD && it.Value().Orientation() != TopAbs_REVERSED)) {
2335 if(nbWires<2) return Standard_False;
2336 Standard_Boolean isFixed = Standard_False;
2337 for (TopoDS_Iterator wi (myFace, Standard_False); wi.More(); wi.Next()) {
2338 if(wi.Value().ShapeType() != TopAbs_WIRE ||
2339 (wi.Value().Orientation() != TopAbs_FORWARD && wi.Value().Orientation() != TopAbs_REVERSED)) {
2340 B.Add(face,wi.Value());
2343 TopoDS_Wire wire = TopoDS::Wire ( wi.Value() );
2344 Handle(ShapeExtend_WireData) sewd = new ShapeExtend_WireData(wire);
2345 if(sewd->NbEdges()==2) {
2346 TopoDS_Edge E1 = sewd->Edge(1);
2347 TopoDS_Edge E2 = sewd->Edge(2);
2348 E1.Orientation(TopAbs_FORWARD);
2349 E2.Orientation(TopAbs_FORWARD);
2353 else isFixed = Standard_True;
2360 face.Orientation(ori);
2361 if ( ! Context().IsNull() ) Context()->Replace ( myFace, face );
2369 //=======================================================================
2370 //function : FixSplitFace
2372 //=======================================================================
2374 Standard_Boolean ShapeFix_Face::FixSplitFace(const TopTools_DataMapOfShapeListOfShape &MapWires)
2377 TopTools_SequenceOfShape faces;
2378 TopoDS_Shape S = myFace;
2379 if ( ! Context().IsNull() )
2380 S = Context()->Apply ( myFace );
2381 Standard_Integer NbWires=0, NbWiresNew=0, NbEdges;
2382 for(TopoDS_Iterator iter(S,Standard_False); iter.More(); iter.Next()) {
2383 const TopoDS_Shape& aShape = iter.Value();
2384 if(aShape.ShapeType() != TopAbs_WIRE ||
2385 (aShape.Orientation() != TopAbs_FORWARD && aShape.Orientation() != TopAbs_REVERSED))
2387 TopoDS_Wire wire = TopoDS::Wire ( aShape );
2389 if(MapWires.IsBound(wire)) {
2390 // if wire not closed --> stop split and return false
2391 Handle(ShapeExtend_WireData) sewd = new ShapeExtend_WireData(wire);
2392 NbEdges = sewd->NbEdges();
2397 TopoDS_Edge E1 = sewd->Edge(1);
2398 TopoDS_Edge E2 = sewd->Edge(NbEdges);
2399 TopoDS_Vertex V1,V2;
2400 ShapeAnalysis_Edge sae;
2401 V1=sae.FirstVertex(E1);
2402 V2=sae.LastVertex(E2);
2403 if(!V1.IsSame(V2)) {
2405 cout<<"wire not closed --> stop split"<<endl;
2407 return Standard_False;
2410 TopoDS_Shape emptyCopied = S.EmptyCopied();
2411 TopoDS_Face tmpFace = TopoDS::Face(emptyCopied);
2412 tmpFace.Orientation ( TopAbs_FORWARD );
2413 B.Add(tmpFace,wire);
2415 const TopTools_ListOfShape& IntWires = MapWires.Find(wire);
2416 TopTools_ListIteratorOfListOfShape liter(IntWires);
2417 for( ; liter.More(); liter.Next()) {
2418 TopoDS_Shape aShape = tmpFace.EmptyCopied();
2419 TopoDS_Face aFace = TopoDS::Face ( aShape );
2420 aFace.Orientation ( TopAbs_FORWARD );
2421 B.Add (aFace,liter.Value());
2422 BRepTopAdaptor_FClass2d clas (aFace,::Precision::PConfusion());
2423 TopAbs_State staout = clas.PerformInfinitePoint();
2424 if (staout == TopAbs_IN)
2425 B.Add(tmpFace,liter.Value());
2427 B.Add(tmpFace,liter.Value().Reversed());
2430 if(!myFwd) tmpFace.Orientation(TopAbs_REVERSED);
2431 faces.Append(tmpFace);
2435 if(NbWires!=NbWiresNew) return Standard_False;
2437 if(faces.Length()>1) {
2438 TopoDS_Compound Comp;
2439 B.MakeCompound(Comp);
2440 for(Standard_Integer i=1; i<=faces.Length(); i++ )
2441 B.Add(Comp,faces(i));
2443 Context()->Replace ( myFace, myResult );
2444 for (TopExp_Explorer exp ( myResult, TopAbs_FACE ); exp.More(); exp.Next() ) {
2445 myFace = TopoDS::Face ( exp.Current() );
2446 BRepTools::Update(myFace);
2448 return Standard_True;
2451 return Standard_False;
2454 //=======================================================================
2455 //function : IsPeriodicConicalLoop
2456 //purpose : Checks whether the passed wire makes up a periodic loop on
2457 // passed conical surface
2458 //=======================================================================
2460 static Standard_Boolean IsPeriodicConicalLoop(const Handle(Geom_ConicalSurface)& theSurf,
2461 const TopoDS_Wire& theWire,
2462 const Standard_Real theTolerance,
2463 Standard_Real& theMinU,
2464 Standard_Real& theMaxU,
2465 Standard_Real& theMinV,
2466 Standard_Real& theMaxV,
2467 Standard_Boolean& isUDecrease)
2469 if ( theSurf.IsNull() )
2470 return Standard_False;
2472 ShapeAnalysis_Edge aSAE;
2473 TopLoc_Location aLoc;
2475 Standard_Real aCumulDeltaU = 0.0, aCumulDeltaUAbs = 0.0;
2476 Standard_Real aMinU = RealLast();
2477 Standard_Real aMinV = aMinU;
2478 Standard_Real aMaxU = -aMinU;
2479 Standard_Real aMaxV = aMaxU;
2481 // Iterate over the edges to check whether the wire is periodic on conical surface
2482 TopoDS_Iterator aWireIter(theWire, Standard_False);
2483 for ( ; aWireIter.More(); aWireIter.Next() )
2485 const TopoDS_Edge& aCurrentEdge = TopoDS::Edge(aWireIter.Value());
2486 Handle(Geom2d_Curve) aC2d;
2487 Standard_Real aPFirst, aPLast;
2489 aSAE.PCurve(aCurrentEdge, theSurf, aLoc, aC2d, aPFirst, aPLast, Standard_True);
2491 if ( aC2d.IsNull() )
2492 return Standard_False;
2494 gp_Pnt2d aUVFirst = aC2d->Value(aPFirst),
2495 aUVLast = aC2d->Value(aPLast);
2497 Standard_Real aUFirst = aUVFirst.X(), aULast = aUVLast.X();
2498 Standard_Real aVFirst = aUVFirst.Y(), aVLast = aUVLast.Y();
2500 Standard_Real aCurMaxU = Max(aUFirst, aULast),
2501 aCurMinU = Min(aUFirst, aULast);
2502 Standard_Real aCurMaxV = Max(aVFirst, aVLast),
2503 aCurMinV = Min(aVFirst, aVLast);
2505 if ( aCurMinU < aMinU )
2507 if ( aCurMaxU > aMaxU )
2509 if ( aCurMinV < aMinV )
2511 if ( aCurMaxV > aMaxV )
2514 Standard_Real aDeltaU = aULast - aUFirst;
2516 aCumulDeltaU += aDeltaU;
2517 aCumulDeltaUAbs += Abs(aDeltaU);
2524 isUDecrease = (aCumulDeltaU < 0 ? Standard_True : Standard_False);
2526 Standard_Boolean is2PIDelta = Abs(aCumulDeltaUAbs - 2*M_PI) <= theTolerance;
2527 Standard_Boolean isAroundApex = Abs(theMaxU - theMinU) > 2*M_PI - theTolerance;
2529 return is2PIDelta && isAroundApex;
2532 //=======================================================================
2533 //function : FixPeriodicDegenerated
2535 //=======================================================================
2537 Standard_Boolean ShapeFix_Face::FixPeriodicDegenerated()
2539 /* =====================
2540 * Prepare fix routine
2541 * ===================== */
2543 if ( !Context().IsNull() )
2545 TopoDS_Shape aSh = Context()->Apply(myFace);
2546 myFace = TopoDS::Face(aSh);
2549 /* ================================================
2550 * Check if fix can be applied on the passed face
2551 * ================================================ */
2553 // Collect all wires owned by the face
2554 TopTools_SequenceOfShape aWireSeq;
2555 for ( TopoDS_Iterator aWireIt(myFace, Standard_False); aWireIt.More(); aWireIt.Next() )
2557 const TopoDS_Shape& aSubSh = aWireIt.Value();
2558 if ( aSubSh.ShapeType() != TopAbs_WIRE || ( aSubSh.Orientation() != TopAbs_FORWARD &&
2559 aSubSh.Orientation() != TopAbs_REVERSED ) )
2562 aWireSeq.Append( aWireIt.Value() );
2565 // Get number of wires and surface
2566 Standard_Integer aNbWires = aWireSeq.Length();
2567 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(myFace);
2569 // Only single wires on conical surfaces are checked
2570 if ( aNbWires != 1 || aSurface.IsNull() ||
2571 aSurface->DynamicType() != STANDARD_TYPE(Geom_ConicalSurface) )
2572 return Standard_False;
2574 // Get the single wire
2575 TopoDS_Wire aSoleWire = TopoDS::Wire( aWireSeq.Value(1) );
2577 // Check whether this wire is belting the conical surface by period
2578 Handle(Geom_ConicalSurface) aConeSurf = Handle(Geom_ConicalSurface)::DownCast(aSurface);
2579 Standard_Real aMinLoopU = 0.0, aMaxLoopU = 0.0, aMinLoopV = 0.0, aMaxLoopV = 0.0;
2580 Standard_Boolean isUDecrease = Standard_False;
2582 Standard_Boolean isConicLoop = IsPeriodicConicalLoop(aConeSurf, aSoleWire, Precision(),
2583 aMinLoopU, aMaxLoopU,
2584 aMinLoopV, aMaxLoopV,
2588 return Standard_False;
2592 * =============== */
2594 // Get base circle of the conical surface (the circle it was built from)
2595 Handle(Geom_Curve) aConeBaseCrv = aConeSurf->VIso(0.0);
2596 Handle(Geom_Circle) aConeBaseCirc = Handle(Geom_Circle)::DownCast(aConeBaseCrv);
2598 // Retrieve conical props
2599 Standard_Real aConeBaseR = aConeBaseCirc->Radius();
2600 Standard_Real aSemiAngle = aConeSurf->SemiAngle();
2602 if ( fabs(aSemiAngle) <= Precision::Confusion() )
2603 return Standard_False; // Bad surface
2605 // Find the V parameter of the apex
2606 Standard_Real aConeBaseH = aConeBaseR / Sin(aSemiAngle);
2607 Standard_Real anApexV = -aConeBaseH;
2610 TopoDS_Vertex anApex = BRepBuilderAPI_MakeVertex( aConeSurf->Apex() );
2612 // ====================================
2613 // Build degenerated edge in the apex
2614 // ====================================
2616 TopoDS_Edge anApexEdge;
2617 BRep_Builder aBuilder;
2618 aBuilder.MakeEdge(anApexEdge);
2620 // Check if positional relationship between the initial wire and apex
2621 // line in 2D is going to be consistent
2622 if ( fabs(anApexV - aMinLoopV) <= Precision() ||
2623 fabs(anApexV - aMaxLoopV) <= Precision() ||
2624 ( anApexV < aMaxLoopV && anApexV > aMinLoopV ) )
2625 return Standard_False;
2627 Handle(Geom2d_Line) anApexCurve2d;
2629 // Apex curve below the wire
2630 if ( anApexV < aMinLoopV )
2632 anApexCurve2d = new Geom2d_Line( gp_Pnt2d(aMinLoopU, anApexV), gp_Dir2d(1, 0) );
2634 aSoleWire.Reverse();
2637 // Apex curve above the wire
2638 if ( anApexV > aMaxLoopV )
2640 anApexCurve2d = new Geom2d_Line( gp_Pnt2d(aMaxLoopU, anApexV), gp_Dir2d(-1, 0) );
2642 aSoleWire.Reverse();
2645 // Create degenerated edge & wire for apex
2646 aBuilder.UpdateEdge( anApexEdge, anApexCurve2d, myFace, Precision() );
2647 aBuilder.Add( anApexEdge, anApex );
2648 aBuilder.Add( anApexEdge, anApex.Reversed() );
2649 aBuilder.Degenerated(anApexEdge, Standard_True);
2650 aBuilder.Range( anApexEdge, 0, fabs(aMaxLoopU - aMinLoopU) );
2651 TopoDS_Wire anApexWire = BRepBuilderAPI_MakeWire(anApexEdge);
2653 // ===============================================================
2654 // Finalize the fix building new face and setting up the results
2655 // ===============================================================
2657 // Collect the resulting set of wires
2658 TopTools_SequenceOfShape aNewWireSeq;
2659 aNewWireSeq.Append(aSoleWire);
2660 aNewWireSeq.Append(anApexWire);
2662 // Assemble new face
2663 TopoDS_Face aNewFace = TopoDS::Face( myFace.EmptyCopied() );
2664 aNewFace.Orientation(TopAbs_FORWARD);
2665 BRep_Builder aFaceBuilder;
2666 for ( Standard_Integer i = 1; i <= aNewWireSeq.Length(); i++ )
2668 TopoDS_Wire aNewWire = TopoDS::Wire( aNewWireSeq.Value(i) );
2669 aFaceBuilder.Add(aNewFace, aNewWire);
2671 aNewFace.Orientation( myFace.Orientation() );
2673 // Adjust the resulting state of the healing tool
2674 myResult = aNewFace;
2675 Context()->Replace(myFace, myResult);
2677 return Standard_True;