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 //:j6 abv 7 Dec 98: ProSTEP TR10 r0601_id.stp #57676 & #58586:
15 // in FixIntersectingEdges, do not cut edges because of influence on adjacent faces
16 // pdn 17.12.98: shifting whole wire in FixShifted
17 //:k3 abv 24 Dec 98: BUC50070 #26682 and #30087: removing self-int loops on pcurves
18 // pdn 30.12.98: PRO10366 #210: shifting pcurve between two singularities
19 // pdn 05.01.98: renaming method FixLittle to FixSmall
20 //:l0 abv 10.01.99: remove unused code
21 //:n2 abv 22.01.99: ma-test5.igs: IGES read (pref3d): remove degen edge with no pcurve
22 //:o4 abv 17.02.99: r0301_db.stp #53082: using parameter isClosed in CheckOrder
23 //:p4 abv 23.02.99: PRO9234 #15720: update UV points of edges after shifting pcurves
24 //#67 rln 01.03.99 S4135: ims010.igs treatment of Geom_SphericalSurface together with V-closed surfaces
25 //:p7 abv 10.03.99 PRO18206: using method IsDegenerated() to detect singularity in FixLacking
26 //:r0 abv 19.03.99 PRO7226.stp #489490: remove degenerated edges if several
27 //#78 rln 12.03.99 S4135: checking spatial closure with Precision
28 //#79 rln 15.03.99 S4135: bmarkmdl.igs: check for gap before shifting on singularities
29 // pdn 17.03.99 S4135: implemented fixing not adjacent intersection
30 //#86 rln 22.03.99 S4135: repeat of fix self-intersection if it was fixed just before
31 //%15 pdn 06.04.99 CTS18546-2: fix of self-intersection is repeated while fixed
32 //#3 smh 01.04.99 S4163: Overflow
33 //#4 szv S4163: optimization
34 //:r7 abv 12.04.99 S4136: FixLk: extend cases of adding degenerated edge
35 //:r8 abv 12.04.99 PRO10107.stp #2241: try increasing tolerance of edge for fix IE
36 //:s2 abv 21.04.99 S4136, PRO7978.igs: FixLacking extended to be able to bend pcurves
37 //szv#9:S4244:19Aug99 Methods FixGaps3d and FixGaps2d were introduced
38 //#15 smh 03.04.2000 PRO19800. Checking degenerated point on a surface of revolution.
39 // sln 25.09.2001 checking order of 3d and 2d representation curves
40 // van 19.10.2001 fix of non-adjacent self-intersection corrected to agree with BRepCheck
41 // skl 07.03.2002 fix for bug OCC180
42 // skl 15.05.2002 for OCC208 (if few edges have reference to
43 // one pcurve we make replace pcurve)
44 // PTV 26.06.2002 Remove regressions after fix OCC450
46 #include <ShapeFix_Wire.ixx>
48 #include <Standard_ErrorHandler.hxx>
49 #include <Standard_Failure.hxx>
50 #include <TColStd_Array1OfReal.hxx>
51 #include <TColStd_Array1OfInteger.hxx>
52 #include <Precision.hxx>
54 #include <Geom_Curve.hxx>
55 #include <Geom_TrimmedCurve.hxx>
56 #include <Geom_BSplineCurve.hxx>
57 #include <Geom_SphericalSurface.hxx> //S4135
58 #include <Geom_SurfaceOfRevolution.hxx>
59 #include <GeomAdaptor_Curve.hxx>
60 #include <GeomAdaptor_HSurface.hxx>
61 #include <GeomAdaptor_Surface.hxx>
62 #include <GeomConvert_CompCurveToBSplineCurve.hxx>
63 #include <GeomAPI.hxx>
65 #include <Geom2d_Curve.hxx>
66 #include <Geom2d_TrimmedCurve.hxx>
67 #include <Geom2d_Line.hxx>
68 #include <Geom2d_BSplineCurve.hxx>
69 #include <Geom2dAdaptor_Curve.hxx>
70 #include <Geom2dConvert.hxx>
72 #include <Bnd_Box2d.hxx>
73 #include <Bnd_Array1OfBox2d.hxx>
74 #include <BndLib_Add2dCurve.hxx>
75 #include <IntRes2d_SequenceOfIntersectionPoint.hxx>
76 #include <IntRes2d_IntersectionPoint.hxx>
77 #include <TColgp_Array1OfPnt.hxx>
78 #include <TColgp_SequenceOfPnt.hxx>
82 #include <TopoDS_Vertex.hxx>
83 #include <TopoDS_Edge.hxx>
84 #include <TopTools_Array1OfShape.hxx>
85 #include <TopTools_HSequenceOfShape.hxx>
86 #include <TopExp_Explorer.hxx>
88 #include <BRep_Tool.hxx>
89 #include <BRep_Builder.hxx>
90 #include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
91 #include <BRep_TEdge.hxx>
92 #include <BRep_GCurve.hxx>
93 #include <BRepBuilderAPI_MakeEdge.hxx>
94 #include <BRepBuilderAPI_MakeVertex.hxx>
95 #include <BRepTools.hxx>
97 #include <Message_Msg.hxx>
98 #include <ShapeExtend.hxx>
99 #include <ShapeBuild_Edge.hxx>
100 #include <ShapeBuild_Vertex.hxx>
101 #include <ShapeBuild_ReShape.hxx>
102 #include <ShapeAnalysis_Curve.hxx>
103 #include <ShapeAnalysis_Edge.hxx>
104 #include <ShapeAnalysis_Surface.hxx>
105 #include <ShapeAnalysis.hxx>
106 #include <ShapeConstruct_ProjectCurveOnSurface.hxx>
107 #include <ShapeAnalysis_TransferParametersProj.hxx>
108 #include <Geom_Plane.hxx>
109 #include <Geom_OffsetCurve.hxx>
111 #include <TColStd_HSequenceOfReal.hxx>
112 #include <Handle_Geom2dAdaptor_HCurve.hxx>
113 #include <Adaptor3d_CurveOnSurface.hxx>
114 #include <Geom2dAdaptor_HCurve.hxx>
115 #include <GeomAPI_ProjectPointOnCurve.hxx>
117 #include <ShapeAnalysis_TransferParameters.hxx>
118 #include <ShapeFix.hxx>
119 #include <ShapeFix_IntersectionTool.hxx>
120 #include <ShapeFix_SplitTool.hxx>
121 #include <ShapeFix_Edge.hxx>
122 #include <ShapeAnalysis_Wire.hxx>
125 //#######################################################################
126 // Constructors, initializations, modes, querying
127 //#######################################################################
129 //=======================================================================
130 //function : ShapeFix_Wire
132 //=======================================================================
134 ShapeFix_Wire::ShapeFix_Wire() : myMaxTailAngleSine(0), myMaxTailWidth(-1)
136 myFixEdge = new ShapeFix_Edge;
137 myAnalyzer = new ShapeAnalysis_Wire;
140 myStatusRemovedSegment = Standard_False;
143 //=======================================================================
144 //function : ShapeFix_Wire
146 //=======================================================================
148 ShapeFix_Wire::ShapeFix_Wire (
149 const TopoDS_Wire& wire,
150 const TopoDS_Face &face,
151 const Standard_Real prec) : myMaxTailAngleSine(0), myMaxTailWidth(-1)
153 myFixEdge = new ShapeFix_Edge;
154 myAnalyzer = new ShapeAnalysis_Wire;
156 SetMaxTolerance ( prec );
157 myStatusRemovedSegment = Standard_False;
158 Init ( wire, face, prec );
161 //=======================================================================
162 //function : SetPrecision
164 //=======================================================================
166 void ShapeFix_Wire::SetPrecision (const Standard_Real prec)
168 ShapeFix_Root::SetPrecision ( prec );
169 myAnalyzer->SetPrecision ( prec );
172 //=======================================================================
173 //function : SetMaxTailAngle
175 //=======================================================================
176 void ShapeFix_Wire::SetMaxTailAngle(const Standard_Real theMaxTailAngle)
178 myMaxTailAngleSine = Sin(theMaxTailAngle);
179 myMaxTailAngleSine = (myMaxTailAngleSine >= 0) ? myMaxTailAngleSine : 0;
182 //=======================================================================
183 //function : SetMaxTailWidth
185 //=======================================================================
186 void ShapeFix_Wire::SetMaxTailWidth(const Standard_Real theMaxTailWidth)
188 myMaxTailWidth = theMaxTailWidth;
191 //=======================================================================
192 //function : ClearModes
194 //=======================================================================
196 void ShapeFix_Wire::ClearModes()
198 myTopoMode = Standard_False;
199 myGeomMode = Standard_True;
200 myClosedMode = Standard_True;
201 myPreference2d = Standard_True;
202 myFixGapsByRanges = Standard_False;
204 myRemoveLoopMode = -1;
206 myFixReversed2dMode = -1;
207 myFixRemovePCurveMode = -1;
208 myFixRemoveCurve3dMode = -1;
209 myFixAddPCurveMode = -1;
210 myFixAddCurve3dMode = -1;
212 myFixShiftedMode = -1;
213 myFixSameParameterMode = -1;
214 myFixVertexToleranceMode = -1;
216 myFixNotchedEdgesMode = -1;
217 myFixSelfIntersectingEdgeMode = -1;
218 myFixIntersectingEdgesMode = -1;
219 myFixNonAdjacentIntersectingEdgesMode = -1;
222 myFixReorderMode = -1;
224 myFixConnectedMode = -1;
225 myFixEdgeCurvesMode = -1;
226 myFixDegeneratedMode = -1;
227 myFixSelfIntersectionMode = -1;
228 myFixLackingMode = -1;
229 myFixGaps3dMode = -1;
230 myFixGaps2dMode = -1;
233 //=======================================================================
234 //function : ClearStatuses
236 //=======================================================================
238 void ShapeFix_Wire::ClearStatuses()
240 Standard_Integer emptyStatus = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
242 myLastFixStatus = emptyStatus;
244 myStatusReorder = emptyStatus;
245 myStatusSmall = emptyStatus;
246 myStatusConnected = emptyStatus;
247 myStatusEdgeCurves = emptyStatus;
248 myStatusDegenerated = emptyStatus;
249 myStatusSelfIntersection = emptyStatus;
250 myStatusLacking = emptyStatus;
251 myStatusGaps3d = emptyStatus; //szv#9:S4244:19Aug99 new method introduced
252 myStatusGaps2d = emptyStatus; //szv#9:S4244:19Aug99 new method introduced
253 myStatusClosed = emptyStatus;
254 myStatusNotches = emptyStatus;
255 myStatusFixTails = emptyStatus;
258 //=======================================================================
261 //=======================================================================
263 void ShapeFix_Wire::Init (const TopoDS_Wire& wire,
264 const TopoDS_Face &face, const Standard_Real prec)
268 SetPrecision ( prec );
271 //=======================================================================
274 //=======================================================================
276 void ShapeFix_Wire::Init (const Handle(ShapeAnalysis_Wire)& saw)
281 // SetPrecision ( saw.Precision() );
284 //=======================================================================
287 //=======================================================================
289 void ShapeFix_Wire::Load (const TopoDS_Wire& wire)
293 TopoDS_Wire W = wire;
294 if ( ! Context().IsNull() ) {
295 TopoDS_Shape S = Context()->Apply ( wire );
296 W = TopoDS::Wire ( S );
299 myAnalyzer->Load ( W );
303 //=======================================================================
306 //=======================================================================
308 void ShapeFix_Wire::Load (const Handle(ShapeExtend_WireData)& sbwd)
311 myAnalyzer->Load ( sbwd );
312 if ( ! Context().IsNull() ) UpdateWire();
316 //=======================================================================
319 //=======================================================================
321 Standard_Integer ShapeFix_Wire::NbEdges() const
323 Handle(ShapeExtend_WireData) sbwd = myAnalyzer->WireData();
324 return sbwd.IsNull() ? 0 : sbwd->NbEdges();
327 //#######################################################################
328 // Fixing methods of API level
329 //#######################################################################
331 //=======================================================================
333 //purpose : This method performs all the available fixes.
334 // If some fix is turned on or off explicitly by the flag,
335 // it is called or not depending on that flag
336 // Else (i.e. if flag is default) fix is called depending on the
337 // situation: default is True, but some fixes are not called or
338 // are limited if order of edges in the wire is not OK
339 //=======================================================================
341 Standard_Boolean ShapeFix_Wire::Perform()
344 if ( ! IsLoaded() ) return Standard_False;
347 Standard_Integer Fixed = Standard_False;
349 // FixReorder is first, because as a rule wire is required to be ordered
350 // We shall analyze the order of edges in the wire and set appropriate
351 // status even if FixReorder should not be called (if it is forbidden)
353 ShapeAnalysis_WireOrder sawo;
354 Standard_Boolean ReorderOK = ( myAnalyzer->CheckOrder ( sawo, myClosedMode ) ==0 );
355 if ( NeedFix ( myFixReorderMode, ! ReorderOK ) ) {
356 if(FixReorder()) Fixed = Standard_True;
357 ReorderOK = ! StatusReorder ( ShapeExtend_FAIL );
360 // FixSmall is allowed to change topology only if mode is set and FixReorder
362 if ( NeedFix ( myFixSmallMode, myTopoMode ) ) {
363 if ( FixSmall ( ! myTopoMode || ! ReorderOK, MinTolerance() ) ) {
364 Fixed = Standard_True;
365 // retry reorder if necessary (after FixSmall it can work better)
366 if ( NeedFix ( myFixReorderMode, ! ReorderOK ) ) {
368 ReorderOK = ! StatusReorder ( ShapeExtend_FAIL );
373 if ( NeedFix ( myFixConnectedMode, ReorderOK ) ) {
374 if ( FixConnected() ) Fixed = Standard_True;
377 if ( NeedFix ( myFixEdgeCurvesMode ) ) {
378 Standard_Integer savFixShiftedMode = myFixShiftedMode;
379 // turn out FixShifted if reorder not done
380 if ( myFixShiftedMode == -1 && ! ReorderOK ) myFixShiftedMode = 0;
381 if ( FixEdgeCurves() ) Fixed = Standard_True;
382 myFixShiftedMode = savFixShiftedMode;
385 if ( NeedFix ( myFixDegeneratedMode ) ) {
386 if ( FixDegenerated() ) Fixed = Standard_True; // ?? if ! ReorderOK ??
389 //pdn - temporary to test
390 if (myFixTailMode <= 0 && NeedFix(myFixNotchedEdgesMode, ReorderOK))
392 Fixed |= FixNotchedEdges();
393 if(Fixed) FixShifted(); //skl 07.03.2002 for OCC180
396 if (myFixTailMode != 0)
405 if ( NeedFix ( myFixSelfIntersectionMode, myClosedMode ) ) {
406 Standard_Integer savFixIntersectingEdgesMode = myFixIntersectingEdgesMode;
407 // switch off FixIntEdges if reorder not done
408 if ( myFixIntersectingEdgesMode == -1 && ! ReorderOK )
409 myFixIntersectingEdgesMode = 0;
410 if ( FixSelfIntersection() ) Fixed = Standard_True;
412 myFixIntersectingEdgesMode = savFixIntersectingEdgesMode;
415 if ( NeedFix ( myFixLackingMode, ReorderOK ) ) {
416 if ( FixLacking() ) Fixed = Standard_True;
419 // TEMPORARILY without special mode !!!
420 Handle(ShapeExtend_WireData) sbwd = WireData();
421 for (Standard_Integer iedge = 1; iedge <= sbwd->NbEdges(); iedge++)
422 if ( myFixEdge->FixVertexTolerance (sbwd->Edge (iedge)) )
423 Fixed = Standard_True;
428 //=======================================================================
429 //function : FixReorder
431 //=======================================================================
433 Standard_Boolean ShapeFix_Wire::FixReorder()
435 myStatusReorder = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
436 if ( ! IsLoaded() ) return Standard_False;
439 ShapeAnalysis_WireOrder sawo;
440 myAnalyzer->CheckOrder ( sawo, myClosedMode, Standard_True );
442 //:abv revolCuts.sat -23: in case of bi-periodic surface check case
443 // of reversed wire specifically. This is necessary because degenerated
444 // cases are possible when direct evaluation will give bad result.
445 Standard_Boolean isReorder = Standard_False;
446 if ( sawo.Status() != 0 &&
447 ! myAnalyzer->Surface().IsNull() &&
448 myAnalyzer->Surface()->Surface()->IsUPeriodic() &&
449 myAnalyzer->Surface()->Surface()->IsVPeriodic() ) {
450 Handle(ShapeExtend_WireData) sbwd2 = new ShapeExtend_WireData;
451 for ( Standard_Integer i=WireData()->NbEdges(); i >=1; i-- )
452 sbwd2->Add ( WireData()->Edge(i) );
453 ShapeAnalysis_WireOrder sawo2;
454 ShapeAnalysis_Wire analyzer2 ( sbwd2, myAnalyzer->Face(), Precision() );
455 analyzer2.CheckOrder ( sawo2, myClosedMode, Standard_True );
456 if ( ( sawo2.Status() >=0 && sawo2.Status() < sawo.Status() ) ||
457 ( sawo.Status() <0 && sawo2.Status() > sawo.Status() ) ) {
458 WireData()->Init ( sbwd2 );
460 isReorder = Standard_True;
466 if ( LastFixStatus ( ShapeExtend_FAIL ) )
467 myStatusReorder |= ShapeExtend::EncodeStatus ( LastFixStatus ( ShapeExtend_FAIL1 ) ?
468 ShapeExtend_FAIL1 : ShapeExtend_FAIL2 );
469 if ( ! LastFixStatus ( ShapeExtend_DONE )&& !isReorder ) return Standard_False;
471 myStatusReorder |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
472 if ( sawo.Status() ==2 || sawo.Status() ==-2 )
473 myStatusReorder |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE2 );
474 if ( sawo.Status() <0 )
475 myStatusReorder |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE3 );
476 if ( sawo.Status() == 3)
477 myStatusReorder |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE5 );//only shifted
478 return Standard_True;
481 //=======================================================================
482 //function : FixSmall
484 //=======================================================================
486 Standard_Integer ShapeFix_Wire::FixSmall (const Standard_Boolean lockvtx,
487 const Standard_Real precsmall)
489 myStatusSmall = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
490 if ( ! IsLoaded() ) return Standard_False;
492 for ( Standard_Integer i = NbEdges(); i >0; i-- ) {
493 FixSmall ( i, lockvtx, precsmall );
494 myStatusSmall |= myLastFixStatus;
497 return StatusSmall ( ShapeExtend_DONE );
501 //=======================================================================
502 //function : FixConnected
504 //=======================================================================
506 Standard_Boolean ShapeFix_Wire::FixConnected (const Standard_Real prec)
508 myStatusConnected = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
509 if ( ! IsLoaded() ) return Standard_False;
511 Standard_Integer stop = ( myClosedMode ? 0 : 1 );
512 for ( Standard_Integer i = NbEdges(); i > stop; i-- ) {
513 FixConnected ( i, prec );
514 myStatusConnected |= myLastFixStatus;
517 return StatusConnected ( ShapeExtend_DONE );
520 //=======================================================================
521 //function : FixEdgeCurves
523 //=======================================================================
525 Standard_Boolean ShapeFix_Wire::FixEdgeCurves()
527 myStatusEdgeCurves = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
528 if ( ! IsLoaded() ) return Standard_False;
529 Standard_Boolean isReady = IsReady();
531 Handle(ShapeExtend_WireData) sbwd = WireData();
532 Standard_Integer i, nb = sbwd->NbEdges();
533 TopoDS_Face face = Face();
534 Handle(ShapeFix_Edge) theAdvFixEdge = Handle(ShapeFix_Edge)::DownCast(myFixEdge);
535 if (theAdvFixEdge.IsNull()) myFixReversed2dMode = Standard_False;
537 // fix revesred 2d / 3d curves
538 if ( isReady && NeedFix ( myFixReversed2dMode ) ) {
539 for ( i=1; i <= nb; i++ ) {
540 theAdvFixEdge->FixReversed2d ( sbwd->Edge(i), face );
541 if ( theAdvFixEdge->Status ( ShapeExtend_DONE ) )
542 myStatusEdgeCurves |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
543 if ( theAdvFixEdge->Status ( ShapeExtend_FAIL ) )
544 myStatusEdgeCurves |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL1 );
548 // add / remove pcurve
549 if ( isReady && NeedFix ( myFixRemovePCurveMode, Standard_False ) ) {
550 for ( i=1; i <= nb; i++ ) {
551 myFixEdge->FixRemovePCurve ( sbwd->Edge(i), face );
552 if ( myFixEdge->Status ( ShapeExtend_DONE ) )
553 myStatusEdgeCurves |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE2 );
554 if ( myFixEdge->Status ( ShapeExtend_FAIL ) )
555 myStatusEdgeCurves |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL2 );
559 if ( isReady && NeedFix ( myFixAddPCurveMode ) ) {
560 Standard_Integer overdegen = 0; //:c0
561 for ( i=1; i <= nb; i++ ) {
562 myFixEdge->FixAddPCurve ( sbwd->Edge(i), face, sbwd->IsSeam(i),
563 myAnalyzer->Surface(), Precision() );
564 if ( myFixEdge->Status ( ShapeExtend_DONE ) )
565 myStatusEdgeCurves |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE3 );
566 if ( myFixEdge->Status ( ShapeExtend_FAIL ) )
567 myStatusEdgeCurves |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL3 );
569 //if ( !sbwd->IsSeam(i) && myFixEdge->Status ( ShapeExtend_DONE2 )
570 // && BRep_Tool::SameParameter(sbwd->Edge(i)) ) {
571 if ( !sbwd->IsSeam(i) && myFixEdge->Status ( ShapeExtend_DONE2 ) ) {
572 // abv 24 Feb 00: trj3_s1-ac-214.stp #1631 etc.: try to split the edge in singularity
573 if ( ! Context().IsNull() ) {
575 TopoDS_Edge E = sbwd->Edge ( i );
576 ShapeAnalysis_Curve SAC;
578 Handle(Geom_Curve) C = BRep_Tool::Curve ( E, a, b );
579 Handle(ShapeAnalysis_Surface) S = myAnalyzer->Surface();
580 Standard_Integer nbs = S->NbSingularities(MinTolerance());
581 GeomAdaptor_Curve GAC ( C, a, b );
582 TColStd_SequenceOfReal seq;
583 for (Standard_Integer j=1; j <= nbs; j++) {
587 Standard_Real par1, par2, split;
588 Standard_Boolean tmpUIsoDeg;
589 S->Singularity (j, Preci, P3d, pd1, pd2, par1, par2, tmpUIsoDeg);
590 if ( SAC.Project ( GAC, P3d, MinTolerance(), pr, split, Standard_True ) < Max(Preci,MinTolerance()) ) {
591 if ( split - a > ::Precision::PConfusion() &&
592 b - split > ::Precision::PConfusion() ) {
594 for ( k=1; k <= seq.Length(); k++ ) {
595 if ( split < seq(k)-::Precision::PConfusion() ) {
596 seq.InsertBefore ( k, split );
599 else if ( split < seq(k)+::Precision::PConfusion() ) break;
601 if ( k > seq.Length() ) seq.Append ( split );
605 if ( seq.Length() >0 ) { // supposed that edge is SP
607 cout << "Edge going over singularity detected; splitted" << endl;
609 Standard_Boolean isFwd = ( E.Orientation() == TopAbs_FORWARD );
610 E.Orientation ( TopAbs_FORWARD );
612 //if( BRep_Tool::SameParameter(sbwd->Edge(i)) )
613 // sbe.RemovePCurve ( E, face );
615 //10.04.2003 skl for using trimmed lines as pcurves
616 ShapeAnalysis_Edge sae;
617 if( BRep_Tool::SameParameter(sbwd->Edge(i)) )
618 sbe.RemovePCurve ( E, face );
620 if(sae.HasPCurve(E,face)) {
621 Handle(Geom2d_Curve) C2d;
622 Standard_Real fp2d,lp2d;
623 if(sae.PCurve(E,face,C2d,fp2d,lp2d)) {
624 if( !C2d->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve)) )
625 sbe.RemovePCurve(E,face);
630 // myFixEdge->FixSameParameter ( E ); // to ensure SameRange & SP
632 TopoDS_Vertex V1, V2, V;
633 //ShapeAnalysis_Edge sae;
634 V1 = sae.FirstVertex ( E );
635 V2 = sae.LastVertex ( E );
637 Handle(ShapeExtend_WireData) sw = new ShapeExtend_WireData;
638 for ( Standard_Integer k=0; k <= seq.Length(); k++ )
640 Standard_Real split = ( k < seq.Length() ? seq(k+1) : b );
641 if ( k < seq.Length() )
643 B.MakeVertex ( V, C->Value(split), BRep_Tool::Tolerance(E) );
644 //try increase tolerance before splitting
645 Standard_Real aDist = BRep_Tool::Pnt(V1).Distance(BRep_Tool::Pnt(V));
646 if (aDist < BRep_Tool::Tolerance(V1) * 1.01) {
647 B.UpdateVertex(V1, Max(aDist, BRep_Tool::Tolerance(V1)));
654 aDist = BRep_Tool::Pnt(V2).Distance(BRep_Tool::Pnt(V));
655 if (aDist < BRep_Tool::Tolerance(V2) * 1.01) {
656 B.UpdateVertex(V, Max(aDist, BRep_Tool::Tolerance(V2)));
668 TopoDS_Edge edge = sbe.CopyReplaceVertices ( E, V1, V );
669 if( BRep_Tool::SameParameter(sbwd->Edge(i)) ) {
670 //TopoDS_Edge edge = sbe.CopyReplaceVertices ( E, V1, V );
671 B.Range ( edge, a, split );
675 //TopoDS_Edge edge = sbe.CopyReplaceVertices(sbwd->Edge(i),V1,V);
676 Handle(ShapeAnalysis_TransferParameters) sftp =
677 new ShapeAnalysis_TransferParameters(E,face);
678 sftp->TransferRange(edge, a, split, Standard_False);
687 E.Orientation ( TopAbs_REVERSED );
689 Context()->Replace ( E, sw->Wire() );
691 nb = sbwd->NbEdges();
701 //:c0 abv 20 Feb 98: treat case of curve going over degenerated pole and seam
702 if ( overdegen && myAnalyzer->Surface()->IsUClosed(Precision()) ) {
704 Standard_Real URange, SUF, SUL, SVF, SVL;
705 myAnalyzer->Surface()->Bounds(SUF, SUL, SVF, SVL);
706 URange = (Abs(SUL - SUF));
708 ShapeAnalysis_Edge sae;
710 for ( k = 1; k <= nb; k++) {
711 Standard_Real cf, cl;
712 Handle(Geom2d_Curve) c2d;
713 if ( ! sae.PCurve ( sbwd->Edge(k), face, c2d, cf, cl, Standard_True ) ) break;
714 vec += c2d->Value(cl).XY() - c2d->Value(cf).XY();
716 if ( k > nb && Abs ( Abs ( vec.X() ) - URange ) < 0.1 * URange ) {
717 sbe.RemovePCurve (sbwd->Edge ( overdegen ), face);
718 myFixEdge->Projector()->AdjustOverDegenMode() = Standard_False;
719 myFixEdge->FixAddPCurve ( sbwd->Edge(overdegen), face, sbwd->IsSeam(overdegen), myAnalyzer->Surface(), Precision());
722 cout << "Edge going over singularity detected; pcurve adjusted" << endl;
727 // add / remove pcurve
728 if ( isReady && NeedFix ( myFixRemoveCurve3dMode, Standard_False ) ) {
729 for ( i=1; i <= nb; i++ ) {
730 myFixEdge->FixRemoveCurve3d ( sbwd->Edge(i) );
731 if ( myFixEdge->Status ( ShapeExtend_DONE ) )
732 myStatusEdgeCurves |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE4 );
733 if ( myFixEdge->Status ( ShapeExtend_FAIL ) )
734 myStatusEdgeCurves |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL4 );
737 if ( NeedFix ( myFixAddCurve3dMode ) ) {
738 for ( i=1; i <= nb; i++ ) {
739 myFixEdge->FixAddCurve3d ( sbwd->Edge(i) );
740 if ( myFixEdge->Status ( ShapeExtend_DONE ) )
741 myStatusEdgeCurves |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE5 );
742 if ( myFixEdge->Status ( ShapeExtend_FAIL ) ) {
743 //:abv 29.08.01: Spatial_firex_lofting.sat: if 3d curve cannot
744 // be built because edge has no pcurves either, remove that edge
745 Handle(Geom2d_Curve) C;
746 Handle(Geom_Surface) S;
748 Standard_Real first = 0., last = 0.;
749 BRep_Tool::CurveOnSurface ( sbwd->Edge(i), C, S, L, first, last );
750 if ( C.IsNull() || Abs (last - first) < Precision::PConfusion())
752 SendWarning ( sbwd->Edge ( i ), Message_Msg ( "FixWire.FixCurve3d.Removed" ) );// Incomplete edge (with no pcurves or 3d curve) removed
753 sbwd->Remove ( i-- );
755 myStatusEdgeCurves |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE5 );
756 FixConnected (i + 1, Precision());
758 myStatusEdgeCurves |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL5 );
764 if ( isReady && NeedFix ( myFixSeamMode, Standard_False ) ) {
765 for ( i=1; i <= nb; i++ ) {
767 if ( LastFixStatus ( ShapeExtend_DONE ) )
768 myStatusEdgeCurves |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE6 );
769 if ( LastFixStatus ( ShapeExtend_FAIL ) )
770 myStatusEdgeCurves |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL6 );
775 if ( isReady && NeedFix ( myFixShiftedMode ) ) {
777 if ( LastFixStatus ( ShapeExtend_DONE ) )
778 myStatusEdgeCurves |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE7 );
779 if ( LastFixStatus ( ShapeExtend_FAIL ) )
780 myStatusEdgeCurves |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL7 );
783 // fix same parameter
784 if ( isReady && NeedFix ( myFixSameParameterMode ) ) {
785 for ( i=1; i <= nb; i++ ) {
786 // skl 28.10.2004 for OCC6366 - check SameRange
787 ShapeAnalysis_Edge sae;
788 Standard_Real First, Last;
789 Handle(Geom_Curve) tmpc3d = BRep_Tool::Curve(sbwd->Edge(i), First, Last);
790 if(sae.HasPCurve(sbwd->Edge(i),face)) {
791 Handle(Geom2d_Curve) C2d;
792 Standard_Real fp2d,lp2d;
793 if(sae.PCurve(sbwd->Edge(i),face,C2d,fp2d,lp2d)) {
794 if( fabs(First-fp2d)>Precision::PConfusion() ||
795 fabs(Last-lp2d)>Precision::PConfusion() ) {
797 B.SameRange(sbwd->Edge(i),Standard_False);
801 myFixEdge->FixSameParameter ( sbwd->Edge(i) );
802 if ( myFixEdge->Status ( ShapeExtend_DONE ) )
803 myStatusEdgeCurves |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE8 );
804 if ( myFixEdge->Status ( ShapeExtend_FAIL ) )
805 myStatusEdgeCurves |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL8 );
809 //:abv 10.06.02: porting C40 -> dev (CC670-12608.stp): moved from Perform()
810 // Update with face is needed for plane surfaces (w/o stored pcurves)
811 if ( NeedFix ( myFixVertexToleranceMode ) ) {
812 for ( i=1; i <= nb; i++) {
813 myFixEdge->FixVertexTolerance (sbwd->Edge (i), face);
814 if ( myFixEdge->Status ( ShapeExtend_DONE ) )
815 myStatusEdgeCurves |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE8 );
816 if ( myFixEdge->Status ( ShapeExtend_FAIL ) )
817 myStatusEdgeCurves |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL8 );
821 return StatusEdgeCurves ( ShapeExtend_DONE );
824 //=======================================================================
825 //function : FixDegenerated
827 //=======================================================================
829 Standard_Boolean ShapeFix_Wire::FixDegenerated()
831 myStatusDegenerated = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
832 if ( ! IsReady() ) return Standard_False;
834 // if ( ! myAnalyzer->Surface()->HasSingularities ( Precision() ) ) return;
836 Standard_Integer lastcoded = -1, prevcoded = 0;
837 Standard_Integer stop = ( myClosedMode ? 0 : 1 );
838 for ( Standard_Integer i = NbEdges(); i > stop; i-- ) {
839 FixDegenerated ( i );
840 myStatusDegenerated |= myLastFixStatus;
841 //:r0 abv 19 Mar 99: PRO7226.stp #489490: remove duplicated degenerated edges
842 Standard_Integer coded = ( LastFixStatus ( ShapeExtend_DONE2 ) ? 1 : 0 );
843 if ( lastcoded ==-1 ) lastcoded = coded;
844 if ( coded && ( prevcoded || ( i ==1 && lastcoded ) ) && NbEdges() >1 ) {
845 Handle(ShapeExtend_WireData) sbwd = WireData();
848 if ( ! prevcoded ) i = NbEdges();
849 B.Degenerated ( sbwd->Edge ( i++ ), Standard_False );
852 else prevcoded = coded;
855 return StatusDegenerated ( ShapeExtend_DONE );
859 //=======================================================================
860 //function : FixSelfIntersection
861 //purpose : Applies FixSelfIntersectingEdge and FixIntersectingEdges
862 // and removes wrong edges
863 //=======================================================================
865 Standard_Boolean ShapeFix_Wire::FixSelfIntersection()
867 myStatusSelfIntersection = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
868 if ( ! IsReady() ) return Standard_False;
870 Handle(ShapeExtend_WireData) sbwd = WireData();
871 Standard_Integer nb = sbwd->NbEdges();
873 if ( NeedFix ( myFixSelfIntersectingEdgeMode ) ) {
874 if (myRemoveLoopMode<1)
875 for ( Standard_Integer num=1; num <=nb; num++ )
877 FixSelfIntersectingEdge ( num );
878 myStatusSelfIntersection |= myLastFixStatus;
880 else if (myRemoveLoopMode==1)
882 for ( Standard_Integer num=1; num <=nb; num++ )
884 FixSelfIntersectingEdge ( num );
885 myStatusSelfIntersection |= myLastFixStatus;
886 if(nb<sbwd->NbEdges()) num--;
887 nb = sbwd->NbEdges();
889 FixClosed(Precision());
893 if ( NeedFix ( myFixIntersectingEdgesMode ) ) {
894 Standard_Integer num = ( myClosedMode ? 1 : 2 );
895 for ( ; nb >1 && num <= nb; num++ ) {
896 FixIntersectingEdges ( num );
897 if ( LastFixStatus ( ShapeExtend_FAIL1 ) )
898 myStatusSelfIntersection |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL1 );
899 if ( LastFixStatus ( ShapeExtend_FAIL2 ) )
900 myStatusSelfIntersection |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL2 );
901 if ( ! LastFixStatus ( ShapeExtend_DONE ) ) continue;
903 if ( LastFixStatus ( ShapeExtend_DONE1 ) )
904 myStatusSelfIntersection |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
905 if ( LastFixStatus ( ShapeExtend_DONE2 ) )
906 myStatusSelfIntersection |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE2 );
907 if(LastFixStatus (ShapeExtend_DONE6))
908 myStatusSelfIntersection |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE6 );
910 if ( /*! myTopoMode ||*/ nb < 3 ) {
911 //#86 rln 22.03.99 sim2.igs, entity 4292: After fixing of self-intersecting
912 //BRepCheck finds one more self-intersection not found by ShapeAnalysis
913 //%15 pdn 06.04.99 repeat until fixed CTS18546-2 entity 777
915 // if the tolerance was modified we should recheck the result, if it was enough
916 if ( LastFixStatus ( ShapeExtend_DONE7 ) ) num--;
920 if ( LastFixStatus ( ShapeExtend_DONE4 ) ) sbwd->Remove ( num );
921 if ( LastFixStatus ( ShapeExtend_DONE3 ) ) sbwd->Remove ( num >1 ? num-1 : nb+num-1 );
922 if ( LastFixStatus ( ShapeExtend_DONE4 ) ||
923 LastFixStatus ( ShapeExtend_DONE3 ) ) {
924 myStatusSelfIntersection |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE3 );
925 num = ( myClosedMode ? 1 : 2 );
926 nb = sbwd->NbEdges();
928 cout << "Warning: ShapeFix_Wire::FixSelfIntersection: Edge removed" << endl;
933 //%15 pdn 06.04.99 repeat until fixed CTS18546-2 entity 777
934 //FixIntersectingEdges ( num );
935 /*if ( LastFixStatus ( ShapeExtend_DONE7 ) )*/
936 // Always revisit the fixed edge
942 //pdn 17.03.99 S4135 to avoid regression fixing not adjacent intersection
943 if ( NeedFix ( myFixNonAdjacentIntersectingEdgesMode ) ) {
945 ShapeFix_IntersectionTool ITool(Context(),Precision());
946 Standard_Integer NbSplit=0, NbCut=0,NbRemoved=0;
947 if(ITool.FixSelfIntersectWire(sbwd,myAnalyzer->Face(),NbSplit,NbCut,NbRemoved)) {
948 myStatusSelfIntersection |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE5 );//gka 06.09.04
950 if( NbSplit>0 || NbRemoved>0 ) {
951 if(NbRemoved>0) myStatusRemovedSegment = Standard_True;
952 //Load(sbwd); commented by skl 29.12.2004 for OCC7624, instead this
953 // string inserted following three strings:
954 myAnalyzer->Load ( sbwd );
955 if ( ! Context().IsNull() ) UpdateWire();
959 if (StatusSelfIntersection (ShapeExtend_DONE5))
960 cout<<"Warning: ShapeFix_Wire::FixIntersection: Non-adjacent intersection fixed (split-"
961 <<NbSplit<<", cut-"<<NbCut<<", removed-"<<NbRemoved<<")"<<endl;
965 Bnd_Array1OfBox2d boxes(1,nb);
967 const Handle(Geom_Surface)& S = BRep_Tool::Surface(Face(), L);
968 Handle(Geom2d_Curve) c2d;
970 ShapeAnalysis_Edge sae;
971 for(Standard_Integer i = 1; i <= nb; i++){
972 TopoDS_Edge E = sbwd->Edge (i);
973 if(sae.PCurve (E,S,L,c2d,cf,cl,Standard_False)) {
975 Geom2dAdaptor_Curve gac;
976 Standard_Real aFirst = c2d->FirstParameter();
977 Standard_Real aLast = c2d->LastParameter();
978 if(c2d->IsKind(STANDARD_TYPE(Geom2d_BSplineCurve))
979 && (cf < aFirst || cl > aLast)) {
980 //pdn avoiding problems with segment in Bnd_Box
985 BndLib_Add2dCurve::Add(gac,::Precision::Confusion(),box);
990 Standard_Boolean isFail = Standard_False, isDone = Standard_False;
991 for(Standard_Integer num1 = 1; num1 < nb-1; num1++) {
992 Standard_Integer fin = (num1 == 1 ? nb-1 : nb);
993 for(Standard_Integer num2 = num1+2; num2 <= fin; num2++)
994 if(!boxes(num1).IsOut(boxes(num2))){
995 FixIntersectingEdges (num1, num2);
996 isFail |= LastFixStatus ( ShapeExtend_FAIL1 );
997 isDone |= LastFixStatus ( ShapeExtend_DONE1 );
1002 myStatusSelfIntersection |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL3 );
1004 myStatusSelfIntersection |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE5 );
1006 if (StatusSelfIntersection (ShapeExtend_DONE5))
1007 cout << "Warning: ShapeFix_Wire::FixSelfIntersection: Non ajacent intersection fixed" << endl;
1012 return StatusSelfIntersection ( ShapeExtend_DONE );
1016 //=======================================================================
1017 //function : FixLacking
1019 //=======================================================================
1021 Standard_Boolean ShapeFix_Wire::FixLacking ( const Standard_Boolean force )
1023 myStatusLacking = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
1024 if ( ! IsReady() ) return Standard_False;
1026 Standard_Integer start = ( myClosedMode ? 1 : 2 );
1027 for ( Standard_Integer i = start; i <= NbEdges(); i++ ) {
1028 FixLacking ( i, force );
1029 myStatusLacking |= myLastFixStatus;
1032 return StatusLacking ( ShapeExtend_DONE );
1036 //=======================================================================
1037 //function : FixClosed
1039 //=======================================================================
1041 Standard_Boolean ShapeFix_Wire::FixClosed (const Standard_Real prec)
1043 myStatusClosed = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
1044 if ( ! IsLoaded() || NbEdges() <1 ) return Standard_False;
1046 FixConnected ( 1, prec );
1047 if ( LastFixStatus ( ShapeExtend_DONE ) )
1048 myStatusClosed |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
1049 if ( LastFixStatus ( ShapeExtend_FAIL ) )
1050 myStatusClosed |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL1 );
1052 FixDegenerated ( 1 );
1053 if ( LastFixStatus ( ShapeExtend_DONE ) )
1054 myStatusClosed |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE2 );
1055 if ( LastFixStatus ( ShapeExtend_FAIL ) )
1056 myStatusClosed |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL2 );
1059 if ( LastFixStatus ( ShapeExtend_DONE ) )
1060 myStatusClosed |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE3 );
1061 if ( LastFixStatus ( ShapeExtend_FAIL ) )
1062 myStatusClosed |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL3 );
1064 return StatusClosed ( ShapeExtend_DONE );
1068 //#######################################################################
1069 // Fixing methods of advanced level
1070 //#######################################################################
1072 //=======================================================================
1073 //function : FixReorder
1075 //=======================================================================
1077 Standard_Boolean ShapeFix_Wire::FixReorder (const ShapeAnalysis_WireOrder& wi)
1079 myLastFixStatus = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
1080 if ( ! IsLoaded() ) return Standard_False;
1082 Standard_Integer status = wi.Status();
1083 if ( status ==0 ) return Standard_False;
1084 if ( status <=-10 ) {
1085 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL1 );
1086 return Standard_False;
1089 Handle(ShapeExtend_WireData) sbwd = WireData();
1090 Standard_Integer i, nb = sbwd->NbEdges();
1091 if ( nb != wi.NbEdges() ) {
1092 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL2 );
1093 return Standard_False;
1095 // D abord on protege
1096 for (i = 1; i <= nb; i ++) {
1097 if ( wi.Ordered(i) == 0 ) {
1098 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL3 );
1099 return Standard_False;
1103 Handle(TopTools_HSequenceOfShape) newedges = new TopTools_HSequenceOfShape();
1104 for ( i=1; i <= nb; i++ )
1105 newedges->Append ( sbwd->Edge ( wi.Ordered(i) ) );
1106 for ( i=1; i <= nb; i++ )
1107 sbwd->Set ( TopoDS::Edge ( newedges->Value(i) ), i );
1109 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
1110 return Standard_True;
1114 //=======================================================================
1115 //function : FixSmall
1117 //=======================================================================
1119 Standard_Boolean ShapeFix_Wire::FixSmall (const Standard_Integer num,
1120 const Standard_Boolean lockvtx,
1121 const Standard_Real precsmall)
1123 myLastFixStatus = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
1124 if ( ! IsLoaded() || NbEdges() <=1 ) return Standard_False;
1127 Handle(ShapeAnalysis_Wire) theAdvAnalyzer = Handle(ShapeAnalysis_Wire)::DownCast(myAnalyzer);
1128 if (theAdvAnalyzer.IsNull()) return Standard_False;
1129 Standard_Integer n = ( num >0 ? num : NbEdges() );
1130 theAdvAnalyzer->CheckSmall ( n, precsmall );
1131 if ( theAdvAnalyzer->LastCheckStatus ( ShapeExtend_FAIL ) ) {
1132 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL1 );
1133 //:n2 return Standard_False;
1136 if ( ! theAdvAnalyzer->LastCheckStatus ( ShapeExtend_DONE ) ) return Standard_False;
1138 // OUI cette edge est NULLE
1140 if ( theAdvAnalyzer->LastCheckStatus ( ShapeExtend_DONE2 ) ) {
1141 // edge is small, but vertices are not the same..
1142 if ( lockvtx || ! myTopoMode ) {
1143 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL2 );
1144 return Standard_False;
1146 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE2 );
1148 else myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
1150 // action: remove edge
1151 if ( ! Context().IsNull() )
1152 Context()->Remove(WireData()->Edge(n));
1153 SendWarning ( WireData()->Edge ( n ), Message_Msg ( "FixAdvWire.FixSmall.MSG0" ) ); //Small edge(s) removed
1154 WireData()->Remove ( n );
1156 // call FixConnected in the case if vertices of the small edge were not the same
1157 if ( LastFixStatus ( ShapeExtend_DONE2 ) ) {
1158 Standard_Integer savLastFixStatus = myLastFixStatus;
1159 //#43 rln 20.11.98 S4054 CTS18544 entity 21734 removing last edge
1160 FixConnected ( n <= NbEdges() ? n : 1, precsmall );
1161 if ( LastFixStatus ( ShapeExtend_FAIL ) )
1162 savLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL3 );
1163 myLastFixStatus = savLastFixStatus;
1166 return Standard_True;
1170 //=======================================================================
1171 //function : FixConnected
1173 //=======================================================================
1175 Standard_Boolean ShapeFix_Wire::FixConnected (const Standard_Integer num,
1176 const Standard_Real prec)
1178 myLastFixStatus = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
1179 if ( ! IsLoaded() || NbEdges() <=0 ) return Standard_False;
1183 myAnalyzer->CheckConnected ( num, prec < 0 ? MaxTolerance() : prec );
1184 if ( myAnalyzer->LastCheckStatus ( ShapeExtend_FAIL ) ) {
1185 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL1 );
1187 if ( ! myAnalyzer->LastCheckStatus ( ShapeExtend_DONE ) ) return Standard_False;
1189 // action: replacing vertex
1191 Handle(ShapeExtend_WireData) sbwd = WireData();
1192 Standard_Integer n2 = ( num >0 ? num : sbwd->NbEdges() );
1193 Standard_Integer n1 = ( n2 >1 ? n2-1 : sbwd->NbEdges() );
1194 TopoDS_Edge E1 = sbwd->Edge(n1);
1195 TopoDS_Edge E2 = sbwd->Edge(n2);
1197 ShapeAnalysis_Edge sae;
1198 TopoDS_Vertex V1 = sae.LastVertex (E1);
1199 TopoDS_Vertex V2 = sae.FirstVertex (E2);
1202 if ( myAnalyzer->LastCheckStatus ( ShapeExtend_DONE1 ) ) { // absolutely confused
1203 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
1204 //#40 rln 18.11.98 S4054 BUC60035 entity 2393 (2-nd sub-curve is edge with the same vertex)
1205 if ( V2.IsSame ( sae.LastVertex (E2) ) ) {
1207 if ( ! Context().IsNull() )
1208 Context()->Replace ( V1, V.Oriented(V1.Orientation()) );
1212 if ( ! Context().IsNull() )
1213 Context()->Replace ( V2, V.Oriented(V2.Orientation()) );
1216 else { // on moyenne ...
1217 if ( myAnalyzer->LastCheckStatus ( ShapeExtend_DONE2 ) )
1218 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE2 );
1219 else myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE3 );
1220 ShapeBuild_Vertex sbv;
1221 V = sbv.CombineVertex ( V1, V2, 1.0001 );
1222 if ( ! Context().IsNull() ) {
1223 Context()->Replace ( V1, V.Oriented(V1.Orientation()) );
1224 Context()->Replace ( V2, V.Oriented(V2.Orientation()) );
1228 // replace vertices to a new one
1229 ShapeBuild_Edge sbe;
1230 if ( sbwd->NbEdges() <2 ) {
1231 if(E2.Free() && myTopoMode) {
1233 B.Remove(E2,sae.FirstVertex(E2));
1234 B.Remove(E2,sae.LastVertex(E2));
1235 B.Add(E2,V.Oriented(TopAbs_FORWARD));
1236 B.Add(E2,V.Oriented(TopAbs_REVERSED));
1239 TopoDS_Edge tmpE = sbe.CopyReplaceVertices ( E2, V, V );
1240 sbwd->Set ( tmpE, n2 );
1241 if ( ! Context().IsNull() ) Context()->Replace(E2,tmpE);
1245 if(E2.Free() &&E1.Free() && myTopoMode) {
1247 B.Remove(E2,sae.FirstVertex(E2));
1248 B.Add(E2,V.Oriented(TopAbs_FORWARD));
1249 if ( ! myAnalyzer->LastCheckStatus ( ShapeExtend_DONE1 ) ||
1250 sae.FirstVertex (E2).IsSame (sae.LastVertex (E2)) ) {
1251 B.Remove(E1,sae.LastVertex(E1));
1252 B.Add(E1,V.Oriented(TopAbs_REVERSED));
1256 TopoDS_Edge tmpE2 = sbe.CopyReplaceVertices ( E2, V, TopoDS_Vertex() );
1257 sbwd->Set ( tmpE2, n2 );
1258 if ( ! Context().IsNull() ) Context()->Replace(E2,tmpE2);
1259 if ( ! myAnalyzer->LastCheckStatus ( ShapeExtend_DONE1 ) ||
1260 sae.FirstVertex (E2).IsSame (sae.LastVertex (E2)) ) {
1261 TopoDS_Edge tmpE1 = sbe.CopyReplaceVertices ( E1, TopoDS_Vertex(), V );
1262 sbwd->Set (tmpE1, n1 );
1263 if ( ! Context().IsNull() ) Context()->Replace(E1,tmpE1);
1267 if ( ! Context().IsNull() ) UpdateWire();
1269 return Standard_True;
1273 //=======================================================================
1274 //function : FixSeam
1276 //=======================================================================
1278 Standard_Boolean ShapeFix_Wire::FixSeam (const Standard_Integer num)
1280 myLastFixStatus = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
1281 if ( ! IsReady() ) return Standard_False;
1283 Handle(Geom2d_Curve) C1, C2;
1284 Standard_Real cf, cl;
1285 if ( ! myAnalyzer->CheckSeam (num, C1, C2, cf, cl) ) return Standard_False;
1288 TopoDS_Edge E = WireData()->Edge ( num >0 ? num : NbEdges() );
1289 B.UpdateEdge (E, C2,C1, Face(), 0.); //:S4136: BRep_Tool::Tolerance(E)
1290 B.Range (E, Face(),cf,cl);
1291 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
1293 return Standard_True;
1296 //=======================================================================
1297 //function : FixShifted
1298 //purpose : fix parametric curves which may be shifted
1299 // to whole parametric range of closed surface as result of recomputing
1300 // from 3d representation.
1301 // This can be a curve on a seam or near it.
1302 // This function is to be called before FixDegenerated.
1303 // LIMITATION: this function cannot fix cases when, e.g., closed wire is
1304 // composed of two meridians of the sphere and one of them is seam.
1305 // NOTE: wire is supposed to be closed and sorted !
1306 //=======================================================================
1308 //:p4 abv 23.02.99: PRO9234 #15720: update UV points of edge
1309 static void UpdateEdgeUVPoints (TopoDS_Edge &E, const TopoDS_Face &F)
1311 Standard_Real first, last;
1312 BRep_Tool::Range ( E, F, first, last );
1313 BRep_Builder().Range ( E, F, first, last );
1317 //=======================================================================
1318 //function : FixShifted
1320 //=======================================================================
1322 Standard_Boolean ShapeFix_Wire::FixShifted()
1324 myLastFixStatus = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
1325 if ( ! IsReady() ) return Standard_False;
1327 Handle(ShapeAnalysis_Surface) surf = myAnalyzer->Surface();
1328 //#78 rln 12.03.99 S4135: checking spatial closure with Precision
1329 Standard_Boolean uclosed = surf->IsUClosed(Precision());
1330 Standard_Boolean vclosed = surf->IsVClosed(Precision()) || surf->Surface()->IsKind (STANDARD_TYPE(Geom_SphericalSurface));
1331 //#67 rln 01.03.99 S4135: ims010.igs entity D11900 (2D contour is 2*PI higher than V range [-pi/2,p/2])
1333 // PTV 26.06.2002 begin
1334 // CTS18546-2.igs entity 2222: base curve is periodic and 2dcurve is shifted
1335 Standard_Boolean IsVCrvClosed = Standard_False;
1336 Standard_Real VRange = 1.;
1337 if (surf->Surface()->IsKind (STANDARD_TYPE(Geom_SurfaceOfRevolution))) {
1338 Handle(Geom_SurfaceOfRevolution) aSurOfRev = Handle(Geom_SurfaceOfRevolution)::DownCast(surf->Surface());
1339 Handle(Geom_Curve) aBaseCrv = aSurOfRev->BasisCurve();
1340 while ( (aBaseCrv->IsKind(STANDARD_TYPE(Geom_OffsetCurve))) ||
1341 (aBaseCrv->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) ) {
1342 if (aBaseCrv->IsKind(STANDARD_TYPE(Geom_OffsetCurve)))
1343 aBaseCrv = Handle(Geom_OffsetCurve)::DownCast(aBaseCrv)->BasisCurve();
1344 if (aBaseCrv->IsKind(STANDARD_TYPE(Geom_TrimmedCurve)))
1345 aBaseCrv = Handle(Geom_TrimmedCurve)::DownCast(aBaseCrv)->BasisCurve();
1347 if (aBaseCrv->IsPeriodic()) {
1348 vclosed = Standard_True;
1349 VRange = aBaseCrv->Period();
1350 IsVCrvClosed = Standard_True;
1352 cout << "Warning: ShapeFix_Wire::FixShifted set vclosed True for Surface of Revolution" << endl;
1356 // PTV 26.06.2002 end
1357 if ( ! uclosed && ! vclosed ) return Standard_False;
1359 Standard_Real URange, /*VRange,*/ SUF, SUL, SVF, SVL;
1360 surf->Surface()->Bounds ( SUF, SUL, SVF, SVL );
1361 Standard_Real SUMid, SVMid;
1362 SUMid = 0.5*(SUF+SUL);
1363 SVMid = 0.5*(SVF+SVL);
1364 if (uclosed) URange = Abs ( SUL - SUF );
1365 else URange = RealLast();
1366 if (!IsVCrvClosed) {
1367 if (vclosed) VRange = Abs ( SVL - SVF );
1368 else VRange = RealLast();
1370 Standard_Real UTol = 0.2 * URange, VTol = 0.2 * VRange;
1372 Handle(ShapeExtend_WireData) sbwdOring = WireData();
1373 ShapeAnalysis_Edge sae;
1374 Handle(ShapeExtend_WireData) sbwd = new ShapeExtend_WireData;
1375 for ( Standard_Integer i=1; i <= sbwdOring->NbEdges(); i++ ) {
1376 TopoDS_Edge E1 = sbwdOring->Edge ( i );
1377 if ( BRep_Tool::Degenerated(E1) && !sae.HasPCurve(E1,Face()))
1383 ShapeBuild_Edge sbe;
1384 Standard_Integer nb = sbwd->NbEdges();
1385 Standard_Boolean end = (nb == 0), degstop = Standard_False;;
1386 Standard_Integer stop = nb;
1387 Standard_Integer degn2 = 0;
1389 //pdn 17.12.98 r0901_ec 38237 to shift wire at 0
1391 //GeomAdaptor_Surface& GAS = myAnalyzer->Surface()->Adaptor3d()->ChangeSurface(); //SK
1393 for ( Standard_Integer n2=1, n1=nb; ! end; n1 = n2++ ) {
1394 if ( n2 > nb ) n2 = 1;
1395 if ( n2 == stop ) end = Standard_True;
1397 TopoDS_Edge E1 = sbwd->Edge ( n1 );
1398 TopoDS_Edge E2 = sbwd->Edge ( n2 );
1400 if ( BRep_Tool::Degenerated(E1) || BRep_Tool::Degenerated(E2) )
1405 degstop = Standard_True;
1410 TopoDS_Vertex V = sae.FirstVertex ( E2 );
1414 gp_Pnt p = BRep_Tool::Pnt ( V );
1416 Standard_Real a1 = 0., b1 = 0., a2 = 0., b2 = 0.;
1417 Handle(Geom2d_Curve) c2d1, c2d2;
1419 //:abv 29.08.01: torCuts.sat: distinguish degeneration by U and by V;
1420 // only corresponding move is prohibited
1421 // Standard_Boolean isDeg = surf->IsDegenerated ( p, Max ( Precision(), BRep_Tool::Tolerance(V) ) );
1422 Standard_Integer isDeg = 0;
1423 gp_Pnt2d degP1, degP2;
1424 Standard_Real degT1, degT2;
1425 if ( surf->DegeneratedValues ( p, Max ( Precision(), BRep_Tool::Tolerance(V) ),
1426 degP1, degP2, degT1, degT2 ) )
1427 isDeg = ( Abs ( degP1.X() - degP2.X() ) > Abs ( degP1.Y() - degP2.Y() ) ? 1 : 2 );
1429 // abv 23 Feb 00: UKI60107-6 210: additional check for near-degenerated case
1430 //smh#15 PRO19800. Check if the surface is surface of revolution.
1431 if (surf->Surface()->IsKind (STANDARD_TYPE(Geom_SurfaceOfRevolution))) {
1432 if ( ! isDeg && ! vclosed ) {
1433 if ( c2d1.IsNull() && ! sae.PCurve ( E1, Face(), c2d1, a1, b1, Standard_True ) ) continue;
1434 gp_Pnt2d p1 ( SUF, c2d1->Value(b1).Y() );
1435 gp_Pnt2d p2 ( SUL, c2d1->Value(b1).Y() );
1436 if ( surf->IsDegenerated ( p1, p2, MaxTolerance(), 10 ) &&
1437 ! surf->IsDegenerated ( c2d1->Value(a1), c2d1->Value(b1), MaxTolerance(), 10 ) ) // abv 31.07.00: trj4_pm1-ec-214.stp #31274: still allow work if edge already exists
1440 if ( ! isDeg && ! uclosed ) {
1441 if ( c2d1.IsNull() && ! sae.PCurve ( E1, Face(), c2d1, a1, b1, Standard_True ) ) continue;
1442 gp_Pnt2d p1 ( c2d1->Value(b1).X(), SVF );
1443 gp_Pnt2d p2 ( c2d1->Value(b1).X(), SVL );
1444 if ( surf->IsDegenerated ( p1, p2, MaxTolerance(), 10 ) &&
1445 ! surf->IsDegenerated ( c2d1->Value(a1), c2d1->Value(b1), MaxTolerance(), 10 ) ) // abv 31.07.00: trj4_pm1-ec-214.stp #31274: still allow work if edge already exists
1455 degstop = Standard_True;
1465 if ( pdeg.SquareDistance(p) < Precision() * Precision() )
1468 //if ( stop < n2 ) { stop = n2; degstop = Standard_True; }
1472 Standard_Real ax1 = 0., bx1 = 0., ax2 = 0., bx2 = 0.;
1473 Handle(Geom2d_Curve) cx1, cx2;
1474 if ( ( c2d1.IsNull() && ! sae.PCurve ( E1, Face(), c2d1, a1, b1, Standard_True ) ) ||
1475 ( c2d2.IsNull() && ! sae.PCurve ( E2, Face(), c2d2, a2, b2, Standard_True ) ) ||
1476 ! sae.PCurve ( sbwd->Edge ( degn2 >1 ? degn2-1 : nb ), Face(), cx1, ax1, bx1, Standard_True ) ||
1477 ! sae.PCurve ( sbwd->Edge ( degn2 ), Face(), cx2, ax2, bx2, Standard_True ) )
1479 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL1 );
1482 gp_Pnt2d pd1 = cx1->Value ( bx1 );
1483 gp_Pnt2d pd2 = cx2->Value ( ax2 );
1484 gp_Pnt2d pn1 = c2d1->Value ( b1 );
1485 gp_Pnt2d pn2 = c2d2->Value ( a2 );
1486 gp_Vec2d x(0.,0.); // shift vector
1487 Standard_Real period;
1488 if ( uclosed ) { x.SetX ( 1. ); period = URange; }
1489 else { x.SetY ( 1. ); period = VRange; }
1490 Standard_Real rot1 = ( pn1.XY() - pd2.XY() ) ^ x.XY();
1491 Standard_Real rot2 = ( pd1.XY() - pn2.XY() ) ^ x.XY();
1492 Standard_Real scld = ( pd2.XY() - pd1.XY() ) * x.XY();
1493 Standard_Real scln = ( pn2.XY() - pn1.XY() ) * x.XY();
1494 if ( rot1 * rot2 < -::Precision::PConfusion() &&
1495 scld * scln < -::Precision::PConfusion() &&
1496 Abs ( scln ) > 0.1 * period && Abs ( scld ) > 0.1 * period &&
1497 rot1 * scld > ::Precision::PConfusion() &&
1498 rot2 * scln > ::Precision::PConfusion() )
1500 // abv 02 Mar 00: trying more sophisticated analysis (ie_exhaust-A.stp #37520)
1501 Standard_Real sign = ( rot2 >0 ? 1. : -1. );
1502 Standard_Real deep1 = Min ( sign * ( pn2.XY() * x.XY() ),
1503 Min ( sign * ( pd1.XY() * x.XY() ),
1504 Min ( sign * ( c2d2->Value(b2 ).XY() * x.XY() ),
1505 Min ( sign * ( cx1->Value(ax1).XY() * x.XY() ),
1506 Min ( sign * ( c2d2->Value(0.5*(a2 +b2 )).XY() * x.XY() ),
1507 sign * ( cx1->Value(0.5*(ax1+bx1)).XY() * x.XY() ) ) ) ) ) );
1508 Standard_Real deep2 = Max ( sign * ( pn1.XY() * x.XY() ),
1509 Max ( sign * ( pd2.XY() * x.XY() ),
1510 Max ( sign * ( c2d1->Value(a1 ).XY() * x.XY() ),
1511 Max ( sign * ( cx2->Value(bx2).XY() * x.XY() ),
1512 Max ( sign * ( c2d1->Value(0.5*(a1 +b1 )).XY() * x.XY() ),
1513 sign * ( cx2->Value(0.5*(ax2+bx2)).XY() * x.XY() ) ) ) ) ) );
1514 Standard_Real deep = deep2 - deep1; // estimated current size of wire by x
1515 // pdn 30 Oct 00: trying correct period [0,period] (trj5_k1-tc-203.stp #4698)
1516 Standard_Real dx = ShapeAnalysis::AdjustToPeriod ( deep, ::Precision::PConfusion(), period+::Precision::PConfusion());
1517 x *= ( scld >0 ? -dx : dx );
1518 //x *= ( Abs(scld-scln) > 1.5 * period ? 2. : 1. ) *
1519 // ( scld >0 ? -period : period );
1521 Shift.SetTranslation ( x );
1522 for ( Standard_Integer k=degn2; ; k++ )
1524 if ( k > nb ) k = 1;
1525 if ( k == n2 ) break;
1526 TopoDS_Edge edge = sbwd->Edge ( k );
1527 if ( ! sae.PCurve ( edge, Face(), cx1, ax1, bx1, Standard_True ) ) continue;
1528 //cx1->Transform ( Shift );
1529 // skl 15.05.2002 for OCC208 (if few edges have reference to one pcurve)
1530 Handle(Geom2d_Curve) cx1new = Handle(Geom2d_Curve)::DownCast(cx1->Transformed(Shift));
1531 sbe.ReplacePCurve(edge,cx1new,Face());
1532 UpdateEdgeUVPoints ( edge, Face() );
1534 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
1536 cout << "Info: ShapeFix_Wire::FixShifted(): bi - meridian case fixed" << endl;
1540 //degn2 = n2; pdeg = p; // ie_exhaust-A.stp #37520
1544 // pdn to fix half sphere
1545 TopoDS_Vertex VE = sae.LastVertex ( E2 );
1546 gp_Pnt pe = BRep_Tool::Pnt ( VE );
1547 //pdn is second vertex on singular point ?
1548 if ( surf->IsDegenerated ( pe, Max ( Precision(), BRep_Tool::Tolerance(V) ) ) ) {
1549 if ( ( c2d1.IsNull() && ! sae.PCurve ( E1, Face(), c2d1, a1, b1, Standard_True ) ) ||
1550 ( c2d2.IsNull() && ! sae.PCurve ( E2, Face(), c2d2, a2, b2, Standard_True ) ) ) {
1551 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL1 );
1554 gp_Pnt2d p2d1 = c2d1->Value ( b1 ), p2f = c2d2->Value ( a2 ), p2l = c2d2->Value ( b2 );
1555 Standard_Real pres2 = ::Precision::PConfusion();
1556 Standard_Real du = 0.,dv = 0.;
1557 //#79 rln 15.03.99 S4135: bmarkmdl.igs entity 633 (incorrectly oriented contour) check for gap
1558 if(uclosed&&(Abs(p2f.X()-p2l.X())<pres2)&&Abs(p2d1.X()-p2f.X())>GAS.UResolution(Precision())) {
1559 if((Abs(p2f.X()-SUF)<pres2)&&(p2f.Y()<p2l.Y()))
1561 if((Abs(p2f.X()-SUL)<pres2)&&(p2f.Y()>p2l.Y()))
1564 if(vclosed&&(Abs(p2f.Y()-p2l.Y())<pres2)&&Abs(p2d1.Y()-p2f.Y())>GAS.VResolution(Precision())) {
1565 if((Abs(p2f.Y()-SVF)<pres2)&&(p2f.X()>p2l.X()))
1567 if((Abs(p2f.Y()-SVL)<pres2)&&(p2f.X()<p2l.X()))
1570 if ( du ==0. && dv == 0. ) continue;
1571 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
1573 Shift.SetTranslation ( gp_Vec2d ( du, dv ) );
1574 c2d2->Transform ( Shift );
1575 UpdateEdgeUVPoints ( E2, Face() );//rln 15.03.99 syntax correction :E1
1578 //:abv 29.08.01: torCuts.sat: continue;
1581 if ( ( c2d1.IsNull() && ! sae.PCurve ( E1, Face(), c2d1, a1, b1, Standard_True ) ) ||
1582 ( c2d2.IsNull() && ! sae.PCurve ( E2, Face(), c2d2, a2, b2, Standard_True ) ) ) {
1583 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL1 );
1586 gp_Pnt2d p2d1 = c2d1->Value ( b1 );
1587 gp_Pnt2d p2d2 = c2d2->Value ( a2 );
1590 Standard_Real du=0., dv=0.;
1591 if ( uclosed && isDeg != 1 ) {
1592 Standard_Real dx = Abs ( p2d2.X() - p2d1.X() );
1593 if ( dx > URange - UTol )
1594 du = ShapeAnalysis::AdjustByPeriod ( p2d2.X(), p2d1.X(), URange );
1595 else if ( dx > UTol && stop == nb ) stop = n2; //:abv 29.08.01: torCuts2.stp
1597 if ( vclosed && isDeg != 2 ) {
1598 Standard_Real dy = Abs ( p2d2.Y() - p2d1.Y() );
1599 if ( dy > VRange - VTol )
1600 dv = ShapeAnalysis::AdjustByPeriod ( p2d2.Y(), p2d1.Y(), VRange );
1601 else if ( dy > VTol && stop == nb ) stop = n2;
1603 if ( du ==0. && dv == 0. ) continue;
1605 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
1607 Shift.SetTranslation ( gp_Vec2d ( du, dv ) );
1608 //c2d2->Transform ( Shift );
1609 // skl 15.05.2002 for OCC208 (if few edges have reference to one pcurve)
1610 Handle(Geom2d_Curve) c2d2new = Handle(Geom2d_Curve)::DownCast(c2d2->Transformed(Shift));
1611 sbe.ReplacePCurve(E2,c2d2new,Face());
1612 UpdateEdgeUVPoints ( E2, Face() );
1614 if ( box.IsVoid() ) return Standard_False; //#3 smh 01.04.99. S4163: Overflow, when box is void.
1616 Standard_Real umin, vmin, umax, vmax;
1617 box.Get ( umin, vmin, umax, vmax );
1618 if ( Abs ( umin + umax - SUF - SUL ) < URange &&
1619 Abs ( vmin + vmax - SVF - SVL ) < VRange &&
1620 ! LastFixStatus ( ShapeExtend_DONE ) ) return Standard_False;
1623 Standard_Integer n; // svv Jan11 2000 : porting on DEC
1624 for ( n=1; n <= nb; n++ ) {
1626 Handle(Geom2d_Curve) c2d;
1627 if ( ! sae.PCurve ( sbwd->Edge(n), Face(), c2d, a, b, Standard_True ) ) continue;
1628 box.Add ( c2d->Value ( a ) );
1629 box.Add ( c2d->Value ( 0.5 * ( a + b ) ) );
1631 box.Get ( umin, vmin, umax, vmax );
1633 Standard_Real du=0., dv=0.;
1636 Standard_Real umid = 0.5 * ( umin + umax );
1637 // du = ShapeAnalysis::AdjustToPeriod(umid, SUF, SUL);
1638 // PTV 26.06.2002 xloop torus-apple iges face mode
1639 du = ShapeAnalysis::AdjustByPeriod(umid, SUMid, URange);
1642 Standard_Real vmid = 0.5 * ( vmin + vmax );
1643 // dv = ShapeAnalysis::AdjustToPeriod(vmid, SVF, SVL);
1644 // PTV 26.06.2002 xloop torus-apple iges face mode
1645 dv = ShapeAnalysis::AdjustByPeriod(vmid, SVMid, VRange);
1648 if ( du ==0. && dv == 0. ) return Standard_True;
1650 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE2 );
1653 Shift.SetTranslation ( gp_Vec2d ( du, dv ) );
1655 for ( n=1; n <= sbwdOring->NbEdges(); n++ ) {
1657 Handle(Geom2d_Curve) c2d;
1658 TopoDS_Edge ed = sbwdOring->Edge(n);
1659 if ( ! sae.PCurve ( ed, Face(), c2d, a, b, Standard_True ) ) continue;
1660 // skl 15.05.2002 for OCC208 (if few edges have reference to one pcurve)
1661 Handle(Geom2d_Curve) c2d2 = Handle(Geom2d_Curve)::DownCast(c2d->Transformed(Shift));
1662 sbe.ReplacePCurve(ed,c2d2,Face());
1663 UpdateEdgeUVPoints ( ed, Face() );
1665 return Standard_True;
1668 //=======================================================================
1669 //function : FixDegenerated
1671 //=======================================================================
1673 Standard_Boolean ShapeFix_Wire::FixDegenerated (const Standard_Integer num)
1675 myLastFixStatus = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
1676 if ( ! IsReady() ) return Standard_False;
1679 gp_Pnt2d p2d1, p2d2;
1680 myAnalyzer->CheckDegenerated ( num, p2d1, p2d2 );
1681 if ( myAnalyzer->LastCheckStatus ( ShapeExtend_FAIL1 ) ) {
1682 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL1 );
1684 //: abv 29.08.01: torHalf2.sat: if edge was encoded as degenerated but
1685 // has no pcurve and no singularity is found at that point, remove it
1686 if ( myAnalyzer->LastCheckStatus ( ShapeExtend_FAIL2 ) ) {
1687 WireData()->Remove ( num );
1688 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE3 );
1689 return Standard_True;
1691 if ( ! myAnalyzer->LastCheckStatus ( ShapeExtend_DONE ) ) return Standard_False;
1693 // action: create degenerated edge and insert it (or replace)
1695 gp_Vec2d vect2d ( p2d1, p2d2 );
1696 gp_Dir2d dir2d ( vect2d );
1697 Handle(Geom2d_Line) line2d = new Geom2d_Line ( p2d1, dir2d );
1699 TopoDS_Edge degEdge;
1701 B.MakeEdge ( degEdge );
1702 B.Degenerated ( degEdge, Standard_True );
1703 B.UpdateEdge ( degEdge, line2d, Face(), ::Precision::Confusion() );
1704 B.Range ( degEdge, Face(), 0., vect2d.Magnitude() );
1706 Handle(ShapeExtend_WireData) sbwd = WireData();
1707 Standard_Integer n2 = ( num >0 ? num : sbwd->NbEdges() );
1708 Standard_Integer n1 = ( n2 >1 ? n2-1 : sbwd->NbEdges() );
1710 Standard_Boolean lack = myAnalyzer->LastCheckStatus ( ShapeExtend_DONE1 );
1711 Standard_Integer n3 = ( lack ? n2 : ( n2 < sbwd->NbEdges() ? n2+1 : 1 ) );
1713 ShapeAnalysis_Edge sae;
1714 TopoDS_Vertex V1 = sae.LastVertex ( sbwd->Edge ( n1 ) );
1715 TopoDS_Vertex V2 = sae.FirstVertex ( sbwd->Edge ( n3 ) );
1717 V1.Orientation(TopAbs_FORWARD);
1718 V2.Orientation(TopAbs_REVERSED);
1721 degEdge.Orientation(TopAbs_FORWARD);
1724 sbwd->Add ( degEdge, n2 );
1725 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
1728 sbwd->Set ( degEdge, n2 );
1729 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE2 );
1732 // commented to avoid extra messages
1733 // SendWarning ( degEdge, Message_Msg ( "FixWire.FixDegenerated.MSG0" ) );// Degenerated edge(s) detected
1735 return Standard_True;
1738 //=======================================================================
1739 //function : FixSelfIntersectingEdge
1740 //purpose : Tests edge for self-intersection and updates tolerance of vertex
1741 // if intersection is found
1742 // Returns True if tolerance was increased
1743 //=======================================================================
1745 // Create edge on basis of E with new pcurve and call FixSP
1746 // Return resulting tolerance and modified pcurve
1747 static Standard_Boolean TryNewPCurve (const TopoDS_Edge &E, const TopoDS_Face &face,
1748 Handle(Geom2d_Curve) &c2d,
1749 Standard_Real &first,
1750 Standard_Real &last,
1754 Handle(Geom_Curve) crv = BRep_Tool::Curve ( E, f, l );
1755 if ( crv.IsNull() ) return Standard_False;
1757 // make temp edge and compute tolerance
1758 BRepBuilderAPI_MakeEdge mkedge ( crv, f, l );
1760 ShapeBuild_Edge SBE; //skl 17.07.2001
1761 SBE.SetRange3d(mkedge,f,l); //skl 17.07.2001
1763 if ( ! mkedge.IsDone() ) return Standard_False;
1765 TopoDS_Edge edge = mkedge;
1767 B.UpdateEdge ( edge, c2d, face, 0. );
1768 B.Range ( edge, face, first, last );
1769 B.SameRange ( edge, Standard_False );
1770 // no call to BRepLib: B.SameParameter ( edge, Standard_False );
1772 Handle(ShapeFix_Edge) sfe = new ShapeFix_Edge;
1773 sfe->FixSameParameter ( edge );
1774 c2d = BRep_Tool::CurveOnSurface ( edge, face, first, last );
1775 tol = BRep_Tool::Tolerance ( edge );
1776 return Standard_True;
1779 //:k3 abv 24 Dec 98: BUC50070 #26682 and #30087:
1780 // Try to cut out the loop on the pcurve and make new pcurve by concatenating
1782 // If result is not SameParameter with prec, does nothing and returns False
1783 // Warning: resulting pcurve will be C0
1785 //=======================================================================
1786 //function : howMuchPCurves
1788 //=======================================================================
1789 static Standard_Integer howMuchPCurves (const TopoDS_Edge &E)
1791 Standard_Integer count = 0;
1792 BRep_ListIteratorOfListOfCurveRepresentation itcr
1793 ((*((Handle(BRep_TEdge)*)&E.TShape()))->ChangeCurves());
1795 while (itcr.More()) {
1796 const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
1797 if ( cr->IsCurveOnSurface() )
1806 //=======================================================================
1807 //function : RemoveLoop
1809 //=======================================================================
1810 static Standard_Boolean RemoveLoop (TopoDS_Edge &E, const TopoDS_Face &face,
1811 const IntRes2d_IntersectionPoint &IP,
1812 const Standard_Real tolfact,
1813 const Standard_Real prec,
1814 const Standard_Boolean RemoveLoop3d)
1816 Standard_Boolean loopRemoved3d;
1817 if ( BRep_Tool::IsClosed ( E, face ) ) return Standard_False;
1820 Handle(Geom_Curve) crv = BRep_Tool::Curve ( E, f, l );
1822 Standard_Real t1 = IP.ParamOnFirst();
1823 Standard_Real t2 = IP.ParamOnSecond();
1824 if ( t1 > t2 ) { Standard_Real t = t1; t1 = t2; t2 = t; }
1826 ShapeAnalysis_Edge sae;
1828 Handle(Geom2d_Curve) c2d;
1829 if ( ! sae.PCurve ( E, face, c2d, a, b, Standard_False ) )
1830 return Standard_False;
1833 cout << "Cut Loop: params (" << t1 << ", " << t2;
1835 GeomAdaptor_Curve GAC ( crv, f, l );
1836 Standard_Real dt = tolfact * GAC.Resolution(prec);
1837 t1 -= dt; //1e-3;//::Precision::PConfusion();
1838 t2 += dt; //1e-3;//::Precision::PConfusion();
1840 cout << ") -> (" << t1 << ", " << t2 << ")" << endl;
1843 if ( t1 <= a || t2 >= b ) { // should not be so, but to be sure ..
1844 return Standard_False;
1847 // direct construction causes error on osf system.
1854 Handle(Geom_Plane) aPlaneSurf = Handle(Geom_Plane)::DownCast( BRep_Tool::Surface(face) );
1855 Handle(Geom_Curve) pcurve3d = crv;
1856 if ( !aPlaneSurf.IsNull() ) {
1857 Pln = aPlaneSurf->Pln();
1860 pcurve3d = GeomAPI::To3d ( c2d, Pln );
1863 //Handle(Geom_Curve) pcurve3d = GeomAPI::To3d ( c2d, Pln );
1864 Handle(Geom_TrimmedCurve) trim = new Geom_TrimmedCurve (pcurve3d, a, t1);
1865 GeomConvert_CompCurveToBSplineCurve connect ( trim );
1867 // null-length segment patch instead of loop
1868 TColgp_Array1OfPnt Poles(1,2);
1869 TColStd_Array1OfReal Knots(1,2);
1870 TColStd_Array1OfInteger Mults(1,2);
1872 Poles.SetValue(1,trim->Value(t1));
1873 Knots.SetValue(1,t1);
1874 Mults.SetValue(1,2);
1876 trim = new Geom_TrimmedCurve (pcurve3d, t2, b);
1877 Poles.SetValue(2,trim->Value(t2));
1878 Knots.SetValue(2,t2);
1879 Mults.SetValue(2,2);
1881 Handle(Geom_BSplineCurve) patch = new Geom_BSplineCurve ( Poles, Knots, Mults, 1 );
1882 if ( ! connect.Add (patch, ::Precision::PConfusion(), Standard_True, Standard_False) )
1883 return Standard_False;
1886 if ( ! connect.Add (trim, ::Precision::PConfusion(), Standard_True, Standard_False) )
1887 return Standard_False;
1889 // keep created 3d curve
1890 Handle(Geom_Curve) aNew3dCrv = connect.BSplineCurve();
1893 Handle(Geom2d_Curve) bs = GeomAPI::To2d ( aNew3dCrv, Pln );
1894 if ( bs.IsNull() ) return Standard_False;
1896 // make temp edge and compute tolerance
1899 if(!RemoveLoop3d) { // old variant (not remove loop 3d)
1900 Standard_Real newtol=0;
1902 Standard_Integer nbC2d = howMuchPCurves( E );
1903 if ( nbC2d <= 1 && !aPlaneSurf.IsNull() )
1904 B.UpdateEdge ( E, bs, face, 0 );
1906 if ( ! TryNewPCurve ( E, face, bs, a, b, newtol ) ) return Standard_False;
1908 Standard_Real tol = BRep_Tool::Tolerance ( E );
1910 cout << "Cut Loop: tol orig " << tol << ", prec " << prec << ", new tol " << newtol << endl;
1912 if ( newtol > Max ( prec, tol ) ) return Standard_False;
1913 //:s2 bs = BRep_Tool::CurveOnSurface ( edge, face, a, b );
1914 if ( Abs ( a - f ) > ::Precision::PConfusion() || // smth strange, cancel
1915 Abs ( b - l ) > ::Precision::PConfusion() ) return Standard_False;
1917 if ( !aPlaneSurf.IsNull() ) {
1918 B.UpdateEdge ( E, aNew3dCrv, Max (newtol, tol) );
1920 if ( ! TryNewPCurve ( E, face, bs, a, b, newtol ) ) return Standard_False;
1922 B.UpdateEdge ( E, bs, face, newtol );
1923 B.UpdateVertex ( sae.FirstVertex ( E ), newtol );
1924 B.UpdateVertex ( sae.LastVertex ( E ), newtol );
1925 return Standard_True;
1930 Handle(Geom_Surface) S = BRep_Tool::Surface(face, L);
1931 Handle(Geom2dAdaptor_HCurve) AC = new Geom2dAdaptor_HCurve(c2d);
1932 Handle(GeomAdaptor_HSurface) AS = new GeomAdaptor_HSurface(S);
1934 Adaptor3d_CurveOnSurface ACS(AC,AS);
1935 gp_Pnt P1(ACS.Value(t1));
1936 gp_Pnt P2(ACS.Value(t2));
1937 gp_Pnt pcurPnt((P1.X()+P2.X())/2,(P1.Y()+P2.Y())/2,(P1.Z()+P2.Z())/2);
1939 ShapeAnalysis_TransferParametersProj SFTP(E,face);
1940 Handle(TColStd_HSequenceOfReal) Seq2d = new TColStd_HSequenceOfReal;
1943 Seq2d->Append((t1+t2)/2);
1944 Handle(TColStd_HSequenceOfReal) Seq3d = new TColStd_HSequenceOfReal;
1945 Seq3d->Append(SFTP.Perform(Seq2d,Standard_False));
1947 Standard_Real dist1 = pcurPnt.Distance(crv->Value(Seq3d->Value(1)));// correting Seq3d already project
1948 Standard_Real dist2 = pcurPnt.Distance(crv->Value(Seq3d->Value(2)));
1949 Standard_Real dist3 = pcurPnt.Distance(crv->Value(Seq3d->Value(3)));
1950 Standard_Real ftrim,ltrim;
1951 if(dist3>Max(dist1,dist2)) {
1952 loopRemoved3d = Standard_False;
1955 loopRemoved3d = Standard_True;
1958 Handle(Geom_Curve) bs1;
1960 if (!loopRemoved3d) {
1961 // create new 3d curve
1962 ftrim = Seq3d->Value(1);
1963 ltrim = Seq3d->Value(2);
1966 Handle(Geom_TrimmedCurve) trim1 = new Geom_TrimmedCurve (crv, f, ftrim);
1967 GeomConvert_CompCurveToBSplineCurve connect1 ( trim1 );
1968 TColgp_Array1OfPnt Poles1(1,2);
1969 TColStd_Array1OfReal Knots1(1,2);
1970 TColStd_Array1OfInteger Mults1(1,2);
1972 Poles1.SetValue(1,trim1->Value(ftrim));
1973 Knots1.SetValue(1,ftrim);
1974 Mults1.SetValue(1,2);
1976 trim1 = new Geom_TrimmedCurve (crv, ltrim, l);
1977 Poles1.SetValue(2,trim1->Value(ltrim));
1978 Knots1.SetValue(2,ltrim);
1979 Mults1.SetValue(2,2);
1981 // create b-spline curve
1982 Handle(Geom_BSplineCurve) patch1 = new Geom_BSplineCurve ( Poles1, Knots1, Mults1, 1 );
1984 if ( ! connect1.Add (patch1, ::Precision::PConfusion(), Standard_True, Standard_False) )
1985 return Standard_False;
1988 if ( ! connect1.Add (trim1, ::Precision::PConfusion(), Standard_True, Standard_False) )
1989 return Standard_False;
1991 bs1 = connect1.BSplineCurve();
1994 return Standard_False;
1996 // Standard_Real oldtol = BRep_Tool::Tolerance ( E );
1999 B.UpdateEdge ( E, bs1, L, 0. );
2000 B.UpdateEdge ( E, bs, face, 0. );
2001 B.Range ( E, face, f, l );
2002 B.SameRange ( E, Standard_False );
2004 Handle(ShapeFix_Edge) sfe = new ShapeFix_Edge;
2005 sfe->FixSameParameter ( E );
2007 return Standard_True;
2010 //=======================================================================
2011 //function : RemoveLoop
2013 //=======================================================================
2014 static Standard_Boolean RemoveLoop (TopoDS_Edge &E, const TopoDS_Face &face,
2015 const IntRes2d_IntersectionPoint &IP2d,
2020 cout<<"Info: ShapeFix_Wire::FixSelfIntersection : Try insert vertex"<<endl;
2023 if ( BRep_Tool::IsClosed ( E, face ) ) return Standard_False;
2026 Handle(Geom_Curve) crv = BRep_Tool::Curve ( E, f, l );
2028 Standard_Real t1 = IP2d.ParamOnFirst();
2029 Standard_Real t2 = IP2d.ParamOnSecond();
2033 Standard_Real t = t1; t1 = t2; t2 = t;
2036 ShapeAnalysis_Edge sae;
2038 // define vertexes Vfirst , Vlast, Vmid
2039 TopoDS_Vertex Vfirst,Vlast,Vmid;
2040 // initialize Vfirst and Vlast
2041 Vfirst = sae.FirstVertex(E);
2042 Vlast = sae.LastVertex(E);
2044 // find a 2d curve and parameters from edge E
2046 Handle (Geom2d_Curve) c2d;
2047 if ( ! sae.PCurve ( E, face, c2d, a, b, Standard_False ) )
2048 return Standard_False;
2050 // first segment for 2d curve
2051 Handle(Geom2d_TrimmedCurve) trim1;
2052 if( (t1-a)>Precision::PConfusion() )
2053 trim1 = new Geom2d_TrimmedCurve (c2d, a, t1);
2054 // second segment for 2d curve
2055 Handle(Geom2d_TrimmedCurve) trim2 = new Geom2d_TrimmedCurve (c2d, t2, b);
2057 // if ( trim1.IsNull() || trim2.IsNull() )
2059 return Standard_False;
2062 Handle (Geom_Surface) S = BRep_Tool::Surface(face, L);
2063 Handle (Geom2dAdaptor_HCurve) AC = new Geom2dAdaptor_HCurve(c2d);
2064 Handle (GeomAdaptor_HSurface) AS = new GeomAdaptor_HSurface(S);
2066 Adaptor3d_CurveOnSurface ACS(AC,AS);
2067 gp_Pnt P1(ACS.Value(t1));
2068 gp_Pnt P2(ACS.Value(t2));
2069 gp_Pnt pcurPnt((P1.X()+P2.X())/2,(P1.Y()+P2.Y())/2,(P1.Z()+P2.Z())/2);
2071 // transfer parameters from pcurve to 3d curve
2072 ShapeAnalysis_TransferParametersProj SFTP(E,face);
2073 Handle (TColStd_HSequenceOfReal) Seq2d = new TColStd_HSequenceOfReal;
2076 Seq2d->Append((t1+t2)/2);
2077 Handle (TColStd_HSequenceOfReal) Seq3d = new TColStd_HSequenceOfReal;
2078 Seq3d->Append(SFTP.Perform(Seq2d,Standard_False));
2080 Standard_Real dist1 = pcurPnt.Distance(crv->Value(Seq3d->Value(1)));// correting Seq3d already project
2081 Standard_Real dist2 = pcurPnt.Distance(crv->Value(Seq3d->Value(2)));
2082 Standard_Real dist3 = pcurPnt.Distance(crv->Value(Seq3d->Value(3)));
2083 Standard_Real ftrim,ltrim;
2084 if ( dist3 > Max(dist1, dist2)) { // is loop in 3d
2085 ftrim = Seq3d->Value(1);
2086 ltrim = Seq3d->Value(2);
2088 else { // not loop in 3d
2089 ftrim = Seq3d->Value(3);
2090 ltrim = Seq3d->Value(3);
2093 // ftrim = Seq3d->Value(1);
2094 // ltrim = Seq3d->Value(2);
2096 // trim for 3d curve 'crv' with parameters from 'f' to 'l'
2097 Handle(Geom_TrimmedCurve) trim3;
2099 trim3 = new Geom_TrimmedCurve (crv, f, ftrim);
2100 // second segment for 3d curve
2101 Handle(Geom_TrimmedCurve) trim4 = new Geom_TrimmedCurve (crv, ltrim, l);
2103 // if ( trim3.IsNull() || trim4.IsNull() )
2105 return Standard_False;
2107 // create a point for middle vertex
2108 gp_Pnt pnt1 = crv->Value(ftrim);
2109 gp_Pnt pnt2 = crv->Value(ltrim);
2110 gp_Pnt Pmid((pnt1.X()+pnt2.X())/2,(pnt1.Y()+pnt2.Y())/2,(pnt1.Z()+pnt2.Z())/2);
2114 // create new copies for E1 and E2
2116 E1=TopoDS::Edge(E.EmptyCopied());
2117 E2=TopoDS::Edge(E.EmptyCopied());
2119 // initialize middle vertex Vmid
2121 B.MakeVertex(Vmid, pnt2, 0.);
2123 B.MakeVertex(Vmid, Pmid, 0.);
2125 ShapeBuild_Edge sbe;
2127 // replace verteces for new edges E1 and E2
2128 if (E.Orientation()== TopAbs_FORWARD)
2131 E1=sbe.CopyReplaceVertices(E1,Vfirst,Vmid);
2132 E2=sbe.CopyReplaceVertices(E2,Vmid, Vlast);
2137 E1=sbe.CopyReplaceVertices(E1,Vmid, Vlast);
2138 E2=sbe.CopyReplaceVertices(E2,Vfirst, Vmid);
2141 // Update edges by 2d and 3d curves
2142 Handle(ShapeFix_Edge) mySfe = new ShapeFix_Edge;
2144 B.UpdateEdge(E1, trim1, face, 0.);
2145 B.UpdateEdge(E1, trim3, 0.);
2146 B.Range(E1, f, ftrim);
2147 B.SameRange(E1,Standard_False);
2148 // B.SameParameter(E1,Standard_False);
2149 mySfe->FixSameParameter(E1);
2150 mySfe->FixVertexTolerance(E1);
2152 B.UpdateEdge(E2, trim2, face, 0.);
2153 B.UpdateEdge(E2, trim4, 0.);
2154 B.Range ( E2,ltrim,l );
2155 B.SameRange(E2, Standard_False );
2156 // B.SameParameter(E2, Standard_False);
2157 mySfe->FixSameParameter(E2);
2158 mySfe->FixVertexTolerance(E2);
2160 return Standard_True;
2164 //=======================================================================
2165 //function : FixSelfIntersectingEdge
2167 //=======================================================================
2169 Standard_Boolean ShapeFix_Wire::FixSelfIntersectingEdge (const Standard_Integer num)
2171 myLastFixStatus = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
2172 if ( ! IsReady() ) return Standard_False;
2175 IntRes2d_SequenceOfIntersectionPoint points2d;
2176 TColgp_SequenceOfPnt points3d;
2177 Handle(ShapeAnalysis_Wire) theAdvAnalyzer = Handle(ShapeAnalysis_Wire)::DownCast(myAnalyzer);
2178 if (theAdvAnalyzer.IsNull()) return Standard_False;
2179 theAdvAnalyzer->CheckSelfIntersectingEdge ( num, points2d, points3d );
2180 if ( theAdvAnalyzer->LastCheckStatus ( ShapeExtend_FAIL ) ) {
2181 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL1 );
2183 if ( ! theAdvAnalyzer->LastCheckStatus ( ShapeExtend_DONE ) ) return Standard_False;
2185 // action: increase tolerance of vertex
2187 TopoDS_Edge E = WireData()->Edge ( num >0 ? num : NbEdges() );
2189 ShapeAnalysis_Edge sae;
2190 TopoDS_Vertex V1 = sae.FirstVertex ( E );
2191 TopoDS_Vertex V2 = sae.LastVertex ( E );
2192 Standard_Real tol1 = BRep_Tool::Tolerance ( V1 );
2193 Standard_Real tol2 = BRep_Tool::Tolerance ( V2 );
2194 gp_Pnt pnt1 = BRep_Tool::Pnt ( V1 );
2195 gp_Pnt pnt2 = BRep_Tool::Pnt ( V2 );
2197 // cycle is to verify fix in case of RemoveLoop
2198 Standard_Real tolfact = 0.1; // factor for shifting by parameter in RemoveLoop
2199 Standard_Real f2d = 0., l2d = 0.;
2200 Handle(Geom2d_Curve) c2d;
2201 Standard_Real newtol=0.; // = Precision();
2203 if (myRemoveLoopMode<1) {
2204 for ( Standard_Integer iter=0; iter < 30; iter++ ) {
2205 Standard_Boolean loopRemoved = Standard_False;;
2206 Standard_Real prevFirst = 0 , prevLast = 0;
2207 for ( Standard_Integer i=1; i<=points2d.Length(); i++ ) {
2208 gp_Pnt pint = points3d.Value(i);
2209 Standard_Real dist21 = pnt1.SquareDistance ( pint );
2210 Standard_Real dist22 = pnt2.SquareDistance ( pint );
2211 if ( dist21 < tol1 * tol1 || dist22 < tol2 * tol2 ) continue;
2212 newtol = 1.001 * Sqrt ( Min ( dist21, dist22 ) ); //:f8
2214 //:k3 abv 24 Dec 98: BUC50070 #26682 and #30087: try to remove loop
2217 sae.PCurve ( E, Face(), c2d, f2d, l2d, Standard_False );
2218 Standard_Real firstpar = points2d.Value(i).ParamOnFirst();
2219 Standard_Real lastpar = points2d.Value(i).ParamOnSecond();
2220 if(firstpar > prevFirst && lastpar < prevLast) continue;
2221 if ( RemoveLoop (E, Face(), points2d.Value(i), tolfact,
2222 Min( MaxTolerance(), Max(newtol,Precision()) ),
2223 myRemoveLoopMode==0 ) ) {
2224 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE4 );
2225 loopRemoved = Standard_True;
2226 prevFirst = firstpar;
2228 continue; // repeat of fix on that edge required (to be done by caller)
2231 if ( newtol < MaxTolerance() ) {
2232 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
2234 if ( dist21 < dist22 ) B.UpdateVertex ( V1, tol1 = newtol );
2235 else B.UpdateVertex ( V2, tol2 = newtol );
2237 else myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL2 );
2240 // after RemoveLoop, check that self-intersection disappeared
2241 if ( loopRemoved ) {
2242 IntRes2d_SequenceOfIntersectionPoint pnts2d;
2243 TColgp_SequenceOfPnt pnts3d;
2244 theAdvAnalyzer->CheckSelfIntersectingEdge ( num, pnts2d, pnts3d );
2245 if ( ! theAdvAnalyzer->LastCheckStatus ( ShapeExtend_DONE ) ) break;
2246 //points3d.Append(pnts3d);
2247 //points2d.Append(pnts2d);
2251 B.UpdateEdge ( E, c2d, Face(), 0. );
2252 B.Range ( E, Face(), f2d, l2d );
2253 //newtol+=Precision();
2261 //===============================================
2262 // RemoveLoopMode = 1 , insert vertex
2263 //===============================================
2264 if (myRemoveLoopMode == 1) {
2265 // after fixing will be nb+1 edges
2266 Standard_Boolean loopRemoved;
2267 // create a sequence of resulting edges
2268 Handle (TopTools_HSequenceOfShape) TTSS = new TopTools_HSequenceOfShape;
2271 loopRemoved = Standard_False;
2272 //:k3 abv 24 Dec 98: BUC50070 #26682 and #30087: try to remove loop
2275 sae.PCurve ( E, Face(), c2d, f2d, l2d, Standard_False );
2277 if ( RemoveLoop (E, Face(), points2d.Value(1),E1,E2) ) {
2278 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE4 );
2279 loopRemoved = Standard_True;
2282 newtol = Max(BRep_Tool::Tolerance(E1),BRep_Tool::Tolerance(E2));
2285 newtol = BRep_Tool::Tolerance(E2);
2291 if ( newtol > MaxTolerance() )
2292 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL2 );
2294 ShapeExtend_WireData sewd;
2295 for (Standard_Integer i=1 ; i <= TTSS->Length(); i++) {
2296 sewd.Add(TopoDS::Edge(TTSS->Value(i)));
2298 if (! Context().IsNull()) {
2299 Context()->Replace ( E, sewd.Wire() );
2303 WireData()->Remove(num >0 ? num : NbEdges());
2304 WireData()->Add(sewd.Wire(), num >0 ? num : NbEdges());
2307 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE8 );
2311 if ( LastFixStatus ( ShapeExtend_DONE ) && ! myShape.IsNull() ) {
2312 SendWarning ( E, Message_Msg ( "FixAdvWire.FixIntersection.MSG5" ) );// Edge was self-intersecting, corrected
2315 return LastFixStatus ( ShapeExtend_DONE );
2319 //=======================================================================
2320 //function : ComputeLocalDeviation
2321 //purpose : auxilary
2322 //=======================================================================
2323 static Standard_Real ComputeLocalDeviation (const TopoDS_Edge &edge,
2324 const gp_Pnt &pint,const gp_Pnt &pnt,
2325 Standard_Real f, Standard_Real l,
2326 const TopoDS_Face &face )
2328 ShapeAnalysis_Edge sae;
2329 Handle(Geom_Curve) c3d;
2331 if ( ! sae.Curve3d ( edge, c3d, a, b, Standard_False ) ) return RealLast();
2333 gp_Lin line ( pint, gp_Vec ( pint, pnt ) );
2335 Handle(Geom2d_Curve) Crv;
2336 Standard_Real fp,lp;
2337 if ( sae.PCurve(edge,face,Crv,fp,lp,Standard_False) ) {
2338 if(Crv->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve))) {
2339 Handle(Geom2d_TrimmedCurve) tc = Handle(Geom2d_TrimmedCurve)::DownCast(Crv);
2340 if(tc->BasisCurve()->IsKind(STANDARD_TYPE(Geom2d_Line))) {
2341 f = a + (f-fp)*(b-a)/(lp-fp);
2342 l = a + (l-fp)*(b-a)/(lp-fp);
2347 const Standard_Integer NSEG = 10;
2348 Standard_Real step = ( l - f ) / NSEG;
2349 Standard_Real dev = 0.;
2350 for ( Standard_Integer i=1; i < NSEG; i++ ) {
2351 gp_Pnt p = c3d->Value ( f + i * step );
2352 Standard_Real d = line.Distance ( p );
2353 if ( dev < d ) dev = d;
2358 //=======================================================================
2359 //function : FixIntersectingEdges
2361 //=======================================================================
2363 Standard_Boolean ShapeFix_Wire::FixIntersectingEdges (const Standard_Integer num)
2365 myLastFixStatus = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
2366 if ( ! IsReady() || NbEdges() <2 ) return Standard_False;
2369 IntRes2d_SequenceOfIntersectionPoint points2d;
2370 TColgp_SequenceOfPnt points3d;
2371 TColStd_SequenceOfReal errors;
2372 Handle(ShapeAnalysis_Wire) theAdvAnalyzer = Handle(ShapeAnalysis_Wire)::DownCast(myAnalyzer);
2373 if (theAdvAnalyzer.IsNull()) return Standard_False;
2374 theAdvAnalyzer->CheckIntersectingEdges ( num, points2d, points3d, errors );
2375 if ( theAdvAnalyzer->LastCheckStatus ( ShapeExtend_FAIL ) ) {
2376 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL1 );
2378 if ( ! theAdvAnalyzer->LastCheckStatus ( ShapeExtend_DONE ) ) return Standard_False;
2380 //rln 03/02/98: CSR#BUC50004 entity 56 (to avoid later inserting lacking edge)
2381 //:l0 Standard_Boolean isLacking = myAnalyzer->CheckLacking ( num );
2383 // action: increase tolerance of vertex
2385 Handle(ShapeExtend_WireData) sbwd = WireData();
2386 Standard_Integer n2 = ( num >0 ? num : sbwd->NbEdges() );
2387 Standard_Integer n1 = ( n2 >1 ? n2-1 : sbwd->NbEdges() );
2388 TopoDS_Edge E1 = sbwd->Edge(n1);
2389 TopoDS_Edge E2 = sbwd->Edge(n2);
2390 Standard_Boolean isForward1 = ( E1.Orientation() == TopAbs_FORWARD );
2391 Standard_Boolean isForward2 = ( E2.Orientation() == TopAbs_FORWARD );
2392 Standard_Real a1, b1, a2, b2;
2393 BRep_Tool::Range ( E1, Face(), a1, b1 );
2394 BRep_Tool::Range ( E2, Face(), a2, b2 );
2396 ShapeAnalysis_Edge sae;
2397 TopoDS_Vertex Vp = sae.FirstVertex ( E1 );
2398 TopoDS_Vertex V1 = sae.LastVertex ( E1 );
2399 TopoDS_Vertex V2 = sae.FirstVertex ( E2 );
2400 TopoDS_Vertex Vn = sae.LastVertex ( E2 );
2402 Standard_Real tol = BRep_Tool::Tolerance ( V1 );
2403 gp_Pnt pnt = BRep_Tool::Pnt ( V1 );
2405 Standard_Real prevRange1 = RealLast(), prevRange2 = RealLast();
2406 Standard_Boolean cutEdge1 = Standard_False, cutEdge2 = Standard_False;
2407 Standard_Boolean IsCutLine = Standard_False;
2411 Standard_Integer nb = points3d.Length();
2412 for ( Standard_Integer i=1; i <= nb; i++ ) {
2413 const IntRes2d_IntersectionPoint &IP = points2d.Value(i);
2414 Standard_Real param1 = ( num ==1 ? IP.ParamOnSecond() : IP.ParamOnFirst() );
2415 Standard_Real param2 = ( num ==1 ? IP.ParamOnFirst() : IP.ParamOnSecond() );
2417 Standard_Real newRange1 = Abs ( ( isForward1 ? a1 : b1 ) - param1 );
2418 Standard_Real newRange2 = Abs ( ( isForward2 ? b2 : a2 ) - param2 );
2419 if ( newRange1 > prevRange1 && newRange2 > prevRange2 ) continue;
2421 gp_Pnt pint = points3d.Value(i);
2422 Standard_Real rad = errors.Value(i);
2423 Standard_Real newtol = 1.0001 * ( pnt.Distance ( pint ) + rad );
2425 //GeomAdaptor_Surface& Ads = myAnalyzer->Surface()->Adaptor3d()->ChangeSurface();
2427 //:r8 abv 12 Apr 99: try increasing tolerance of edge
2429 Standard_Boolean locMayEdit = myTopoMode;
2430 // Always try to modify the tolerance firstly as a better solution
2431 if ( /*! myTopoMode &&*/ newtol > tol ) {
2432 Standard_Real te1 = rad + ComputeLocalDeviation (E1, pint, pnt,
2433 param1, ( isForward1 ? b1 : a1 ), Face() );
2434 Standard_Real te2 = rad + ComputeLocalDeviation (E2, pint, pnt,
2435 ( isForward2 ? a2 : b2 ), param2, Face() );
2436 Standard_Real maxte = Max ( te1, te2 );
2437 if ( maxte < MaxTolerance() && maxte < newtol ) {
2438 if ( BRep_Tool::Tolerance(E1) < te1 || BRep_Tool::Tolerance(E2) < te2 ) {
2440 cout << "Warning: ShapeFix_Wire::FixIE: edges tolerance increased: (" <<
2441 te1 << ", " << te2 << ") / " << newtol << endl;
2443 B.UpdateEdge ( E1, 1.000001 * te1 );
2444 B.UpdateVertex ( sae.FirstVertex ( E1 ), 1.000001 * te1 );
2445 B.UpdateVertex ( sae.LastVertex ( E1 ), 1.000001 * te1 );
2446 B.UpdateEdge ( E2, 1.000001 * te2 );
2447 B.UpdateVertex ( sae.FirstVertex ( E2 ), 1.000001 * te2 );
2448 B.UpdateVertex ( sae.LastVertex ( E2 ), 1.000001 * te2 );
2450 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE6 );
2451 locMayEdit = Standard_False;
2453 newtol = 1.000001 * maxte;
2457 if ( locMayEdit || newtol <= MaxTolerance() )
2459 prevRange1 = newRange1;
2460 prevRange2 = newRange2;
2463 newtol = 1.0001 * ( pnt.Distance ( pint ) + rad );
2464 //:j6 abv 7 Dec 98: ProSTEP TR10 r0601_id.stp #57676 & #58586: do not cut edges because of influence on adjacent faces
2465 ShapeFix_SplitTool aTool;
2467 if ( ! aTool.CutEdge ( E1, ( isForward1 ? a1 : b1 ), param1, Face(), IsCutLine ) ) {
2468 if ( V1.IsSame ( Vp ) )
2469 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE3 );
2470 else locMayEdit = Standard_False;
2472 else cutEdge1 = Standard_True; //:h4
2474 if ( ! aTool.CutEdge ( E2, ( isForward2 ? b2 : a2 ), param2, Face(), IsCutLine ) ) {
2475 if ( V2.IsSame ( Vn ) )
2476 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE4 );
2477 else locMayEdit = Standard_False;
2479 else cutEdge2 = Standard_True; //:h4
2483 newRange1 <= prevRange1 && newRange2 <= prevRange2 && //rln 09/01/98
2484 BRep_Tool::SameParameter ( E1 ) &&
2485 BRep_Tool::SameParameter ( E2 ) )
2487 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE2 );
2490 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
2498 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE2 );
2501 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
2506 { // else increase tolerance
2508 { //rln 07.04.99 CCI60005-brep.igs
2509 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
2517 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL2 );
2521 if ( ! LastFixStatus ( ShapeExtend_DONE ) ) return Standard_False;
2523 B.UpdateVertex ( V1, pnt, tol );
2524 B.UpdateVertex ( V2, pnt, tol );
2526 //:h4: make edges SP (after all cuts: t4mug.stp #3730+#6460)
2527 if ( cutEdge1 ) myFixEdge->FixSameParameter ( E1 );
2528 if ( cutEdge2 && !IsCutLine ) myFixEdge->FixSameParameter ( E2 );
2529 if ( cutEdge1 || cutEdge2 ) {
2530 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE7 );
2532 if ( ! myShape.IsNull() ) {
2533 SendWarning ( Message_Msg ( "FixAdvWire.FixIntersection.MSG10" ) );// Edges were intersecting, corrected
2535 return Standard_True;
2538 //=======================================================================
2539 //function : FixIntersectingEdges
2541 //=======================================================================
2542 //pdn 17.03.99 fixing non ajacent intersection by increasing tolerance of vertex
2544 Standard_Boolean ShapeFix_Wire::FixIntersectingEdges (const Standard_Integer num1,
2545 const Standard_Integer num2)
2547 myLastFixStatus = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
2548 if ( !IsReady() ) return Standard_False;
2549 IntRes2d_SequenceOfIntersectionPoint points2d;
2550 TColgp_SequenceOfPnt points3d;
2551 TColStd_SequenceOfReal errors;
2552 Handle(ShapeAnalysis_Wire) theAdvAnalyzer = Handle(ShapeAnalysis_Wire)::DownCast(myAnalyzer);
2553 if (theAdvAnalyzer.IsNull()) return Standard_False;
2554 theAdvAnalyzer->CheckIntersectingEdges ( num1, num2, points2d, points3d, errors);
2555 if ( theAdvAnalyzer->LastCheckStatus ( ShapeExtend_FAIL ) ) {
2556 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL1 );
2558 if ( ! theAdvAnalyzer->LastCheckStatus ( ShapeExtend_DONE ) ) return Standard_False;
2559 TColgp_Array1OfPnt vertexPoints(1,4);
2560 TColStd_Array1OfReal vertexTolers(1,4);
2561 TColStd_Array1OfReal newTolers(1,4);
2562 TopTools_Array1OfShape vertices(1,4);
2565 Handle(ShapeExtend_WireData) sbwd = WireData();
2566 Standard_Integer n2 = ( num1 >0 ? num1 : sbwd->NbEdges() );
2567 Standard_Integer n1 = ( num2 >1 ? num2 : sbwd->NbEdges() );
2568 if(n1==n2) return Standard_False;
2570 TopoDS_Edge edge1 = sbwd->Edge(n1);
2571 TopoDS_Edge edge2 = sbwd->Edge(n2);
2573 ShapeAnalysis_Edge sae;
2574 vertices(1) = sae.FirstVertex(edge1);
2575 vertices(2) = sae.LastVertex(edge1);
2576 vertices(3) = sae.FirstVertex(edge2);
2577 vertices(4) = sae.LastVertex(edge2);
2579 Standard_Integer i; // svv Jan11 2000 : porting on DEC
2580 for (i = 1; i <=4; i++) {
2581 vertexPoints(i) = BRep_Tool::Pnt(TopoDS::Vertex(vertices(i)));
2582 vertexTolers(i) = BRep_Tool::Tolerance(TopoDS::Vertex(vertices(i)));
2585 Standard_Real aNewTolEdge1 = 0.0, aNewTolEdge2 = 0.0;
2586 Standard_Integer nb = points3d.Length();
2587 for ( i=1; i <= nb; i++ ) {
2588 gp_Pnt pint = points3d.Value(i);
2590 // searching for the nearest vertexies to the intersection point
2591 Standard_Real aVtx1Param=0., aVtx2Param=0.;
2592 Standard_Integer aVC1, aVC2;
2593 Standard_Real aMinDist = RealLast();
2594 gp_Pnt aNearestVertex;
2595 Standard_Real aNecessaryVtxTole = 0.0;
2596 for(aVC1 = 1; aVC1 <= 2; aVC1++) {
2597 for(aVC2 = 3; aVC2 <= 4; aVC2++) {
2599 Standard_Real aVtxIPDist = pint.Distance(vertexPoints(aVC1));
2600 Standard_Real aVtxVtxDist = vertexPoints(aVC1).Distance(vertexPoints(aVC2));
2601 if(aMinDist > aVtxIPDist && aVtxIPDist > aVtxVtxDist) {
2602 aNecessaryVtxTole = aVtxVtxDist;
2603 aNearestVertex = vertexPoints(aVC1);
2604 aMinDist = aVtxIPDist;
2605 aVtx1Param = BRep_Tool::Parameter(TopoDS::Vertex(vertices(aVC1)),edge1);
2606 aVtx2Param = BRep_Tool::Parameter(TopoDS::Vertex(vertices(aVC2)),edge2);
2611 // calculation of necessary tolerances of edges
2612 const IntRes2d_IntersectionPoint &IP = points2d.Value(i);
2613 Standard_Real param1 = IP.ParamOnFirst();
2614 Standard_Real param2 = IP.ParamOnSecond();
2615 Handle(Geom_Curve) aCurve1, aCurve2;
2617 TopLoc_Location L1, L2;
2618 aCurve1 = BRep_Tool::Curve(edge1, L1, f, l);
2619 aCurve2 = BRep_Tool::Curve(edge2, L2, f, l);
2621 // if aMinDist lower than resolution than the intersection point lyes inside the vertex
2622 if(aMinDist < gp::Resolution())
2625 Standard_Real aMaxEdgeTol1 = 0.0, aMaxEdgeTol2 = 0.0;
2626 if(aMinDist < RealLast() && !aCurve1.IsNull() && !aCurve2.IsNull())
2628 gp_Lin aLig(aNearestVertex, gp_Vec(aNearestVertex, pint));
2629 Standard_Integer aPointsC;
2630 Standard_Real du1 = 0.05*(param1 - aVtx1Param);
2631 Standard_Real du2 = 0.05*(param2 - aVtx2Param);
2632 Standard_Real tole1=BRep_Tool::Tolerance(edge1);
2633 Standard_Real tole2=BRep_Tool::Tolerance(edge2);
2634 for(aPointsC = 2; aPointsC < 19; aPointsC++)
2636 Standard_Real u = aVtx1Param + aPointsC * du1;
2637 gp_Pnt P1 = aCurve1->Value(u);
2638 P1.Transform(L1.Transformation());
2639 Standard_Real d1 = aLig.Distance(P1) * 2.0000001;
2640 if(d1 > tole1 && d1 > aMaxEdgeTol1)
2643 u = aVtx2Param + aPointsC * du2;
2644 gp_Pnt P2 = aCurve2->Value(u);
2645 P2.Transform(L2.Transformation());
2646 Standard_Real d2 = aLig.Distance(P2) * 2.0000001;
2647 if(d2 > tole2 && d2 > aMaxEdgeTol2)
2650 if(aMaxEdgeTol1 == 0.0 && aMaxEdgeTol2 == 0.0) continue;
2651 // if the vertexies are far than tolerances so
2652 // we do not need to increase edge tolerance
2653 if(aNecessaryVtxTole > Max(aMaxEdgeTol1, tole1) ||
2654 aNecessaryVtxTole > Max(aMaxEdgeTol2, tole2))
2661 Standard_Real rad = errors.Value(i);
2662 Standard_Real finTol = RealLast();
2663 Standard_Integer rank=1;
2664 for(Standard_Integer j=1; j<=4; j++) {
2665 Standard_Real newtol = 1.0001 * ( pint.Distance (vertexPoints(j)) + rad );
2671 if(finTol <= MaxTolerance()) {
2672 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1);
2673 if(newTolers(rank) < finTol)
2675 if(Max(aMaxEdgeTol1, aMaxEdgeTol2) < finTol && (aMaxEdgeTol1 > 0 || aMaxEdgeTol2 > 0))
2677 aNewTolEdge1 = Max(aNewTolEdge1, aMaxEdgeTol1);
2678 aNewTolEdge2 = Max(aNewTolEdge2, aMaxEdgeTol2);
2682 newTolers(rank) = finTol;
2686 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL2 );
2691 // update of tolerances of edges
2692 if(aNewTolEdge1 > 0)
2694 for(i = 1; i <= 2; i++)
2695 if(aNewTolEdge1 > Max(vertexTolers(i), newTolers(i)))
2696 newTolers(i) = aNewTolEdge1;
2697 B.UpdateEdge(edge1, aNewTolEdge1);
2699 if(aNewTolEdge2 > 0)
2701 for(i = 3; i <= 4; i++)
2702 if(aNewTolEdge2 > Max(vertexTolers(i), newTolers(i)))
2703 newTolers(i) = aNewTolEdge2;
2704 B.UpdateEdge(edge2, aNewTolEdge2);
2707 // update of tolerances of vertexies
2708 for(i = 1; i <=4; i++)
2709 if(newTolers(i)>0) B.UpdateVertex(TopoDS::Vertex(vertices(i)),newTolers(i));
2711 if ( ! myShape.IsNull() ) {
2712 SendWarning ( Message_Msg ( "FixAdvWire.FixIntersection.MSG10" ) );// Edges were intersecting, corrected
2714 return Standard_True;
2717 //=======================================================================
2718 //function : FixLacking
2719 //purpose : Test if two adjucent edges are disconnected in 2d (while connected
2720 // in 3d), and in that case either increase tolerance of the vertex or
2721 // add a new edge (straight in 2d space), in order to close wire in 2d.
2722 // Returns True if edge was added or tolerance was increased.
2723 //NOTE : Is to be run after FixDegenerated
2724 //Algorithm: 1. Compute the 2d gap between edges and calculate a tolerance
2725 // which should have vertex in order to comprise the gap
2726 // (using GeomAdaptor_Surface); computed value is inctol
2727 // 2. If inctol < tol of vertex, return False (everything is OK)
2728 // 3. If inctol < Precision, just increase tolerance of vertex to inctol
2729 // 4. Else (if both edges are not degenerated) try to add new edge
2730 // with straight pcurve (in order to close the gap):
2731 // a) if flag MayEdit is False
2732 // 1. if inctol < MaxTolerance, increase tolerance of vertex to inctol
2733 // 2. else try to add degenerated edge (check that middle point of
2734 // that pcurveis inside the vertex)
2735 // b) if MayEdit is True
2736 // 1. try to replace big vertex with two new small vertices
2737 // connected by new edge. This is made if there is a 3d space
2738 // between ends of adjacent edges.
2739 // 2. if inctol < MaxTolerance, increase tolerance of vertex to inctol
2740 // 3. else add either degenerated or closed edge (if middle point
2741 // of a pcurve of a new edge is inside the vertex, then
2742 // degenerated edge is added, else new edge is closed).
2743 // 5. If new edge cannot be added, but inctol < MaxTolerance,
2744 // when increase tolerance of vertex to a value of inctol
2745 //Short list of some internal variables:
2746 // tol - tolerance of vertex
2747 // tol2d - tolerance in parametric space of the surface corresponding to 2*tol
2748 // dist2d - distance between ends of pcurves of edges (2d)
2749 // inctol - tolerance required for vertex to close 2d gap (=tol*dist2d/tol2d)
2750 // tol1, tol2 - tolerances of edges, tol0 = tol1 + tol2
2751 // p3d1, p3d2 - ends of 3d curves of edges
2752 //=======================================================================
2753 //:h2 abv 28 May 98: merged modifications by abv 22 Apr 98, gka 27 May 98
2754 // and pdn 25 May 98 concerning lacking closed or degenerated edges
2755 // Example files: r0501_pe #107813, UKI60107-6 250, UKI60107-3 1577.
2757 //:s2 abv 21 Apr 99: add functionality for bending pcurve
2758 static Standard_Boolean TryBendingPCurve (const TopoDS_Edge &E, const TopoDS_Face &face,
2759 const gp_Pnt2d p2d, const Standard_Boolean end,
2760 Handle(Geom2d_Curve) &c2d,
2761 Standard_Real &first, Standard_Real &last,
2764 ShapeAnalysis_Edge sae;
2765 if ( ! sae.PCurve ( E, face, c2d, first, last, Standard_False ) ) return Standard_False;
2770 Handle(Geom2d_BSplineCurve) bs;
2771 if ( c2d->IsKind(STANDARD_TYPE(Geom2d_BSplineCurve)) )
2772 bs = Handle(Geom2d_BSplineCurve)::DownCast(c2d->Copy());
2773 else // if ( c2d->IsKind(STANDARD_TYPE(Geom2d_Line)) )
2775 Handle(Geom2d_TrimmedCurve) trim = new Geom2d_TrimmedCurve ( c2d, first, last );
2776 bs = Geom2dConvert::CurveToBSplineCurve ( trim );
2778 if ( bs.IsNull() ) return Standard_False;
2780 Standard_Real par = ( end ? last : first );
2781 if ( fabs ( bs->FirstParameter() - par ) < ::Precision::PConfusion() &&
2782 bs->Multiplicity(1) > bs->Degree() ) bs->SetPole ( 1, p2d );
2783 else if ( fabs ( bs->LastParameter() - par ) < ::Precision::PConfusion() &&
2784 bs->Multiplicity(bs->NbKnots()) > bs->Degree() ) bs->SetPole ( bs->NbPoles(), p2d );
2786 bs->Segment ( first, last );
2787 if (fabs ( bs->FirstParameter() - par ) < ::Precision::PConfusion() &&
2788 bs->Multiplicity(1) > bs->Degree()) bs->SetPole ( 1, p2d );
2789 else if (fabs ( bs->LastParameter() - par ) < ::Precision::PConfusion() &&
2790 bs->Multiplicity(bs->NbKnots()) > bs->Degree()) bs->SetPole ( bs->NbPoles(), p2d );
2791 else return Standard_False;
2795 if ( ! TryNewPCurve ( E, face, c2d, first, last, tol ) ) return Standard_False;
2797 catch ( Standard_Failure ) {
2799 cout << "Warning: ShapeFix_Wire::FixLacking: Exception in Geom2d_BSplineCurve::Segment()" << endl;
2801 return Standard_False;
2805 return Standard_True;
2809 //=======================================================================
2810 //function : FixLacking
2812 //=======================================================================
2814 Standard_Boolean ShapeFix_Wire::FixLacking (const Standard_Integer num,
2815 const Standard_Boolean force)
2817 myLastFixStatus = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
2818 if ( ! IsReady() ) return Standard_False;
2821 // First phase: analysis whether the problem (gap) exists
2822 gp_Pnt2d p2d1, p2d2;
2823 Handle(ShapeAnalysis_Wire)::DownCast(myAnalyzer)->CheckLacking ( num, ( force ? Precision() : 0. ), p2d1, p2d2 );
2824 if ( myAnalyzer->LastCheckStatus ( ShapeExtend_FAIL ) ) {
2825 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL1 );
2827 if ( ! myAnalyzer->LastCheckStatus ( ShapeExtend_DONE ) ) return Standard_False;
2830 // Second phase: collection of data necessary for further analysis
2832 Handle(ShapeExtend_WireData) sbwd = WireData();
2833 Standard_Integer n2 = ( num >0 ? num : sbwd->NbEdges() );
2834 Standard_Integer n1 = ( n2 >1 ? n2-1 : sbwd->NbEdges() );
2835 TopoDS_Edge E1 = sbwd->Edge(n1);
2836 TopoDS_Edge E2 = sbwd->Edge(n2);
2838 ShapeAnalysis_Edge sae;
2839 TopoDS_Vertex V1 = sae.LastVertex ( E1 );
2840 TopoDS_Vertex V2 = sae.FirstVertex ( E2 );
2841 Standard_Real tol = Max ( BRep_Tool::Tolerance ( V1 ), BRep_Tool::Tolerance ( V2 ) );
2843 Standard_Real Prec = Precision();
2844 Standard_Real dist2d = myAnalyzer->MaxDistance2d();
2845 Standard_Real inctol = myAnalyzer->MaxDistance3d();
2847 TopoDS_Face face = myAnalyzer->Face();
2848 Handle(ShapeAnalysis_Surface) surf = myAnalyzer->Surface();
2851 Standard_Real tol1=::Precision::Confusion(), tol2=::Precision::Confusion(); //SK
2854 //:s2 abv 21 Apr 99: Speculation: try bending pcurves
2855 Standard_Real bendtol1 = 0., bendtol2 = 0.;
2856 Handle(Geom2d_Curve) bendc1, bendc2;
2857 Standard_Real bendf1 = 0., bendl1 = 0., bendf2 = 0., bendl2 = 0.;
2858 if ( myGeomMode && ! BRep_Tool::IsClosed(E1,face) && ! BRep_Tool::IsClosed(E2,face) ) {
2859 gp_Pnt2d p2d = 0.5 * ( p2d1.XY() + p2d2.XY() );
2860 Standard_Boolean ok1 = TryBendingPCurve (E1, face, p2d, E1.Orientation() == TopAbs_FORWARD,
2861 bendc1, bendf1, bendl1, bendtol1);
2862 Standard_Boolean ok2 = TryBendingPCurve (E2, face, p2d, E2.Orientation() == TopAbs_REVERSED,
2863 bendc2, bendf2, bendl2, bendtol2);
2864 if ( ok1 && ! ok2 ) {
2865 bendtol2 = BRep_Tool::Tolerance(E2);
2866 ok1 = TryBendingPCurve (E1, face, p2d2, E1.Orientation() == TopAbs_FORWARD,
2867 bendc1, bendf1, bendl1, bendtol1);
2869 else if ( ! ok1 && ok2 ) {
2870 bendtol1 = BRep_Tool::Tolerance(E1);
2871 ok2 = TryBendingPCurve (E2, face, p2d1, E2.Orientation() == TopAbs_FORWARD,
2872 bendc2, bendf2, bendl2, bendtol2);
2874 if ( ! ok1 && ! ok2 ) bendc1.Nullify();
2878 // Third phase: analyse how to fix the problem
2880 // selector of solutions
2881 Standard_Boolean doIncrease = Standard_False; // increase tolerance
2882 Standard_Boolean doAddLong = Standard_False; // add long 3d edge in replacement of a vertex
2883 Standard_Boolean doAddClosed = Standard_False; // add closed 3d edge
2884 Standard_Boolean doAddDegen = Standard_False; // add degenerated edge
2885 Standard_Boolean doBend = Standard_False; //:s2 bend pcurves
2887 // if bending is OK with existing tolerances of edges, take it
2888 if ( ! bendc1.IsNull() && ! bendc2.IsNull() &&
2889 ( ( bendtol1 < BRep_Tool::Tolerance(E1) &&
2890 bendtol2 < BRep_Tool::Tolerance(E2) ) ||
2891 ( inctol < Prec && bendtol1 < inctol && bendtol2 < inctol ) ) )
2892 doBend = Standard_True;
2894 // is it OK just to increase tolerance (to a value less than preci)?
2895 else if ( inctol < Prec ) doIncrease = Standard_True;
2897 // If increase is not OK or force, try to find other solutions (adding edge)
2898 else if ( ! BRep_Tool::Degenerated ( E2 ) && ! BRep_Tool::Degenerated ( E1 ) ) {
2900 // analyze the 3d space btw edges: is it enough to add long 3d edge?
2902 Handle(Geom_Curve) c3d;
2904 if ( ! sae.Curve3d ( E1, c3d, a, b, Standard_True ) ) { // cannot work
2905 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL1 );
2906 return Standard_False;
2908 p3d1 = c3d->Value ( b );
2909 Standard_Real dist2d3d1 = p3d1.Distance ( surf->Value ( p2d1 ) );
2910 if ( ! sae.Curve3d ( E2, c3d, a, b, Standard_True ) ) { // cannot work
2911 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL1 );
2912 return Standard_False;
2914 p3d2 = c3d->Value ( a );
2915 Standard_Real dist2d3d2 = p3d2.Distance ( surf->Value ( p2d2 ) );
2917 tol1 = Max ( BRep_Tool::Tolerance ( E1 ), dist2d3d1 );
2918 tol2 = Max ( BRep_Tool::Tolerance ( E2 ), dist2d3d2 );
2919 //:c5 Standard_Real tol0 = Max ( tol1 + tol2, thepreci );
2920 Standard_Real tol0 = tol1 + tol2; //:c5 abv 26 Feb 98: CTS17806 #44418
2921 Standard_Real dist3d2 = p3d1.SquareDistance ( p3d2 );
2923 // is it OK to add a long 3d edge?
2924 if ( ! myAnalyzer->LastCheckStatus ( ShapeExtend_DONE2 ) && //:81 abv 20 Jan 98: don`t add back-going edges (zigzags)
2925 dist3d2 > 1.25 * tol0 * tol0 &&
2926 ( force || dist3d2 > Prec * Prec || inctol > MaxTolerance() ) ) {
2927 doAddLong = Standard_True;
2931 //:h6 abv 25 Jun 98: BUC40132 6361: try to increase tol up to MaxTol if not add
2932 if ( ! doAddLong && inctol < MaxTolerance() &&
2933 ! myAnalyzer->Surface()->IsDegenerated ( p2d1, p2d2, 2.*tol, 10. ) ) { //:p7
2934 if ( ! bendc1.IsNull() && ! bendc2.IsNull() &&
2935 bendtol1 < inctol && bendtol2 < inctol ) doBend = Standard_True;
2936 else doIncrease = Standard_True;
2940 // else try to add either degenerated or closed edge
2941 if ( ! doAddLong ) {
2942 gp_Pnt pV = 0.5 * ( BRep_Tool::Pnt(V1).XYZ() + BRep_Tool::Pnt(V2).XYZ() );
2943 gp_Pnt pm = myAnalyzer->Surface()->Value ( 0.5 * ( p2d1.XY() + p2d2.XY() ) );
2945 Standard_Real dist = pV.Distance ( pm );
2946 if ( dist <= tol ) doAddDegen = Standard_True;
2947 else if ( myTopoMode ) doAddClosed = Standard_True;
2948 else if ( dist <= MaxTolerance() ) { //:r7 abv 12 Apr 99: t3d_opt.stp #14245 after S4136
2949 doAddDegen = Standard_True;
2950 doIncrease = Standard_True;
2956 else if ( !BRep_Tool::Degenerated(E2) && BRep_Tool::Degenerated(E1) ) {
2957 // create new degenerated edge and replace E1 to new edge
2959 else if ( BRep_Tool::Degenerated(E2) && !BRep_Tool::Degenerated(E1) ) {
2960 // create new degenerated edge and replace E2 to new edge
2964 // Third phase - do the fixes
2968 if ( doAddLong || doAddDegen || doAddClosed ) {
2970 // construct new vertices
2971 TopoDS_Vertex newV1, newV2;
2973 newV1 = BRepBuilderAPI_MakeVertex ( p3d1 );
2975 newV2 = BRepBuilderAPI_MakeVertex ( p3d2 );
2976 B.UpdateVertex ( newV1, 1.001 * tol1 );
2977 B.UpdateVertex ( newV2, 1.001 * tol2 );
2986 B.MakeEdge ( edge );
2987 if ( doAddDegen ) B.Degenerated ( edge, Standard_True ); // sln: do it before adding curve
2988 gp_Vec2d v12 ( p2d1, p2d2 );
2989 Handle(Geom2d_Line) theLine2d = new Geom2d_Line ( p2d1, gp_Dir2d ( v12 ) );
2990 B.UpdateEdge ( edge, theLine2d, face, ::Precision::Confusion() );
2991 B.Range ( edge, face, 0, dist2d );
2992 B.Add ( edge, newV1.Oriented ( TopAbs_FORWARD ) );
2993 B.Add ( edge, newV2.Oriented ( TopAbs_REVERSED ) );
2994 ShapeBuild_Edge sbe;
2995 if ( ! doAddDegen && ! sbe.BuildCurve3d ( edge ) ) {
2996 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL3 );
2997 return Standard_False;
3000 // if long edge is added, replace vertices of adjacent edges
3003 // replace 1st edge (n1==n2 - special case: wire consists of one edge)
3004 TopoDS_Edge edge1 = sbe.CopyReplaceVertices ( E1,
3005 ( n1 == n2 ? newV2 : TopoDS_Vertex() ), newV1 );
3006 sbwd->Set ( edge1, n1 );
3007 if ( ! Context().IsNull() ) {
3008 Context()->Replace ( E1, edge1 );
3009 // actually, this will occur only in context of single face
3010 // hence, recording to ReShape is rather for tracking modifications
3011 // than for keeping sharing
3012 Context()->Replace ( V1, newV1.Oriented ( V1.Orientation() ) );
3013 if ( ! V1.IsSame ( V2 ) ) {
3014 Context()->Replace ( V2, newV2.Oriented ( V2.Orientation() ) );
3019 TopoDS_Edge edge2 = sbe.CopyReplaceVertices ( E2, newV2, TopoDS_Vertex() );
3020 sbwd->Set ( edge2, n2 );
3021 if ( ! Context().IsNull() ) Context()->Replace ( E2, edge2 );
3023 if ( ! Context().IsNull() ) UpdateWire();
3028 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE3 );
3030 cout << "Warning: ShapeFix_Wire::FixLacking: degenerated edge added" << endl;
3033 else if ( ! doAddLong ) {
3034 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE4 );
3036 sbwd->Add ( edge, n2 );
3037 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE2 );
3040 // else try to increase tol up to MaxTol
3041 else if ( inctol > tol && inctol < MaxTolerance() ) {
3042 if ( ! bendc1.IsNull() && ! bendc2.IsNull() &&
3043 bendtol1 < inctol && bendtol2 < inctol ) doBend = Standard_True;
3044 else doIncrease = Standard_True;
3048 if ( doBend ) { //:s2 abv 21 Apr 99
3049 B.UpdateEdge ( E1, bendc1, face, bendtol1 );
3050 B.Range ( E1, face, bendf1, bendl1 );
3051 B.UpdateEdge ( E2, bendc2, face, bendtol2 );
3052 B.Range ( E2, face, bendf2, bendl2 );
3053 B.UpdateVertex ( sae.FirstVertex(E1), bendtol1 );
3054 B.UpdateVertex ( sae.LastVertex(E1), bendtol1 );
3055 B.UpdateVertex ( sae.FirstVertex(E2), bendtol2 );
3056 B.UpdateVertex ( sae.LastVertex(E2), bendtol2 );
3057 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE5 );
3058 //:s3 abv 22 Apr 99: PRO7187 #11534: self-intersection not detected unitil curve is bent (!)
3059 FixSelfIntersectingEdge ( n1 );
3060 FixSelfIntersectingEdge ( n2 );
3061 FixIntersectingEdges ( n2 ); //skl 24.04.2003 for OCC58
3063 cout << "Info: ShapeFix_Wire::FixLacking: Bending pcurves" << endl;
3065 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE5 );
3068 // increase vertex tolerance
3070 B.UpdateVertex ( V1, 1.001 * inctol );
3071 B.UpdateVertex ( V2, 1.001 * inctol );
3072 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
3075 if ( LastFixStatus ( ShapeExtend_DONE ) ) return Standard_True;
3077 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL2 );
3078 return Standard_False;
3081 //=======================================================================
3082 //function : FixNotchedEdges
3084 //=======================================================================
3086 Standard_Boolean ShapeFix_Wire::FixNotchedEdges()
3088 myLastFixStatus = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
3089 if ( ! IsReady() ) return Standard_False;
3091 Handle(ShapeAnalysis_Wire) theAdvAnalyzer = Handle(ShapeAnalysis_Wire)::DownCast(myAnalyzer);
3092 TopoDS_Face face = Face();
3093 if ( ! Context().IsNull() ) UpdateWire();
3094 Handle(ShapeExtend_WireData) sewd = WireData();
3096 for (Standard_Integer i = 1; i <= NbEdges() && NbEdges() > 2; i++) {
3097 Standard_Real param;
3098 Standard_Integer toRemove;
3099 if(theAdvAnalyzer->CheckNotchedEdges(i,toRemove,param,MinTolerance())){
3100 Standard_Integer n2 = (i > 0) ? i : NbEdges();
3101 Standard_Integer n1 = (n2 > 1) ? n2-1 : NbEdges();
3102 Standard_Boolean isRemoveFirst = (n1==toRemove);
3103 Standard_Integer toSplit = (n2==toRemove ? n1 : n2);
3104 TopoDS_Edge splitE = sewd->Edge ( toSplit );
3105 ShapeAnalysis_Edge sae;
3106 Handle(Geom2d_Curve) c2d;
3108 sae.PCurve ( splitE, face, c2d, a, b, Standard_True );
3109 Standard_Real ppar = (isRemoveFirst ? b : a);
3110 ShapeBuild_Edge sbe;
3111 TopAbs_Orientation orient = splitE.Orientation();
3112 if ( Abs(param - ppar) > ::Precision::PConfusion() ) {
3113 //pdn perform splitting of the edge and adding to wire
3115 //pdn check if it is necessary
3116 if( Abs((isRemoveFirst ? a : b)-param) < ::Precision::PConfusion() ) {
3120 Handle(ShapeAnalysis_TransferParametersProj) transferParameters =
3121 new ShapeAnalysis_TransferParametersProj;
3122 transferParameters->SetMaxTolerance(MaxTolerance());
3123 transferParameters->Init(splitE,face);
3124 Standard_Real first, last;
3135 B.MakeVertex(Vnew,Analyzer()->Surface()->Value(c2d->Value(param)),::Precision::Confusion());
3136 TopoDS_Edge wE = splitE;
3137 wE.Orientation ( TopAbs_FORWARD );
3138 TopoDS_Shape aTmpShape = Vnew.Oriented(TopAbs_REVERSED); //for porting
3139 TopoDS_Edge newE1 = sbe.CopyReplaceVertices ( wE, sae.FirstVertex(wE), TopoDS::Vertex(aTmpShape) );
3140 sbe.CopyPCurves ( newE1, wE );
3141 transferParameters->TransferRange(newE1,first,param,Standard_True);
3142 B.SameRange(newE1,Standard_False);
3143 B.SameParameter(newE1,Standard_False);
3144 aTmpShape = Vnew.Oriented(TopAbs_FORWARD);
3145 TopoDS_Edge newE2 = sbe.CopyReplaceVertices ( wE, TopoDS::Vertex(aTmpShape),sae.LastVertex(wE) );
3146 sbe.CopyPCurves ( newE2, wE );
3147 transferParameters->TransferRange(newE2,param,last,Standard_True);
3148 B.SameRange(newE2,Standard_False);
3149 B.SameParameter(newE2,Standard_False);
3151 if ( !Context().IsNull() ) {
3156 Context()->Replace ( wE, wire );
3159 newE1.Orientation(orient);
3160 newE2.Orientation(orient);
3161 if (orient==TopAbs_REVERSED){ TopoDS_Edge tmp = newE2; newE2 = newE1; newE1=tmp;}
3163 Standard_Boolean isRemoveLast = ((n1==NbEdges())&&(n2==1));
3164 sewd->Set ( newE1, toSplit);
3165 sewd->Add ( newE2, (toSplit==NbEdges() ? 0 : toSplit+1));
3167 FixDummySeam(isRemoveLast ? NbEdges() : toRemove);
3168 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE2 );
3173 // The seam edge is removed from the list. So, need to step back to avoid missing of edge processing
3178 if(!Context().IsNull()) //skl 07.03.2002 for OCC180
3180 myLastFixStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
3183 myStatusNotches = myLastFixStatus;
3184 return LastFixStatus ( ShapeExtend_DONE );
3187 //=======================================================================
3188 //function : FixDummySeam
3190 //=======================================================================
3192 static void CopyReversePcurves(const TopoDS_Edge& toedge,
3193 const TopoDS_Edge& fromedge,
3194 const Standard_Boolean reverse)
3196 TopLoc_Location fromLoc = fromedge.Location();
3197 TopLoc_Location toLoc = toedge.Location();
3198 for (BRep_ListIteratorOfListOfCurveRepresentation fromitcr
3199 ((*((Handle(BRep_TEdge)*)&fromedge.TShape()))->ChangeCurves()); fromitcr.More(); fromitcr.Next()) {
3200 Handle(BRep_GCurve) fromGC = Handle(BRep_GCurve)::DownCast(fromitcr.Value());
3201 if ( fromGC.IsNull() ) continue;
3202 if ( fromGC->IsCurveOnSurface() ) {
3203 Handle(Geom_Surface) surface = fromGC->Surface();
3204 TopLoc_Location L = fromGC->Location();
3205 Standard_Boolean found = Standard_False;
3206 BRep_ListOfCurveRepresentation& tolist = (*((Handle(BRep_TEdge)*)&toedge.TShape()))->ChangeCurves();
3207 Handle(BRep_GCurve) toGC;
3208 for (BRep_ListIteratorOfListOfCurveRepresentation toitcr (tolist); toitcr.More() && !found; toitcr.Next()) {
3209 toGC = Handle(BRep_GCurve)::DownCast(toitcr.Value());
3210 if ( toGC.IsNull() || !toGC->IsCurveOnSurface() ||
3211 surface != toGC->Surface() || L != toGC->Location() ) continue;
3212 found = Standard_True;
3216 Standard_Real fp = fromGC->First();
3217 Standard_Real lp = fromGC->Last();
3218 toGC = Handle(BRep_GCurve)::DownCast(fromGC->Copy());
3219 tolist.Append (toGC);
3220 Handle(Geom2d_Curve) pcurve = Handle(Geom2d_Curve)::DownCast( fromGC->PCurve()->Copy() );
3222 fp = pcurve->ReversedParameter(fp);
3223 lp = pcurve->ReversedParameter(lp);
3225 Standard_Real tmp = fp;
3229 //bug OCC209 invalid location of pcurve in the edge after copying
3230 TopLoc_Location newLoc = (fromLoc*L).Predivided(toLoc);
3231 toGC->SetRange(fp,lp);
3232 toGC->PCurve(pcurve);
3233 toGC->Location(newLoc);
3234 if ( fromGC->IsCurveOnClosedSurface() ) {
3235 pcurve = fromGC->PCurve2();
3236 toGC->PCurve2(Handle(Geom2d_Curve)::DownCast(pcurve->Copy()));
3243 //=======================================================================
3244 //function : HasNewPCurves
3246 //=======================================================================
3247 // Note: This function temporarily not used, because adress to it in
3248 // function FixDummySeam() (see below line 2472) not used too
3250 //static Standard_Boolean HasNewPCurves(const TopoDS_Edge& toedge,
3251 // const TopoDS_Edge& fromedge)
3254 // for (BRep_ListIteratorOfListOfCurveRepresentation fromitcr
3255 // ((*((Handle(BRep_TEdge)*)&fromedge.TShape()))->ChangeCurves()); fromitcr.More(); fromitcr.Next()) {
3256 // Handle(BRep_GCurve) fromGC = Handle(BRep_GCurve)::DownCast(fromitcr.Value());
3257 // if ( fromGC.IsNull() ) continue;
3258 // if ( fromGC->IsCurveOnSurface() ) {
3259 // Handle(Geom_Surface) surface = fromGC->Surface();
3260 // TopLoc_Location L = fromGC->Location();
3261 // Standard_Boolean found = Standard_False;
3262 // BRep_ListOfCurveRepresentation& tolist = (*((Handle(BRep_TEdge)*)&toedge.TShape()))->ChangeCurves();
3263 // Handle(BRep_GCurve) toGC;
3264 // for (BRep_ListIteratorOfListOfCurveRepresentation toitcr (tolist); toitcr.More() && !found; toitcr.Next()) {
3265 // toGC = Handle(BRep_GCurve)::DownCast(toitcr.Value());
3266 // if ( toGC.IsNull() || !toGC->IsCurveOnSurface() ||
3267 // surface != toGC->Surface() || L != toGC->Location() ) continue;
3268 // found = Standard_True;
3272 // return Standard_True;
3275 // return Standard_False;
3278 //=======================================================================
3279 //function : FixDummySeam
3281 //=======================================================================
3283 void ShapeFix_Wire::FixDummySeam(const Standard_Integer num)
3285 ShapeAnalysis_Edge sae;
3286 ShapeBuild_Edge sbe;
3287 ShapeBuild_Vertex sbv;
3288 Standard_Integer num1 = (num == NbEdges()) ? 1 : num+1;
3289 Handle(ShapeExtend_WireData) sewd = WireData();
3290 TopoDS_Edge E1 = sewd->Edge(num), E2 = sewd->Edge(num1);
3291 TopoDS_Vertex V1 = sae.FirstVertex(E1), V2 = sae.LastVertex(E2);
3292 TopoDS_Vertex Vm = sbv.CombineVertex ( V1, V2, 1.0001 );
3294 //pnd defining if new pcurves exists
3295 //pdn Temporary not removed
3296 // Standard_Boolean toRemove = !(HasNewPCurves(E1,E2)||HasNewPCurves(E2,E1));
3297 Standard_Boolean toRemove = Standard_False;
3299 //creating new edge with pcurves and new vertex
3300 TopoDS_Vertex Vs = sae.FirstVertex(E2);
3301 if ( Vs.IsSame ( V1 ) || Vs.IsSame ( V2 ) ) Vs = Vm;
3302 TopoDS_Edge newEdge = sbe.CopyReplaceVertices ( E2, Vs, Vm );
3303 CopyReversePcurves(newEdge,E1,E1.Orientation()==E2.Orientation());
3305 B.SameRange(newEdge,Standard_False);
3306 B.SameParameter(newEdge,Standard_False);
3308 if ( !Context().IsNull() ) {
3310 Context()->Remove ( E2 );
3311 Context()->Remove ( E1 );
3314 Context()->Replace ( E2, newEdge );
3315 Context()->Replace ( E1, newEdge.Reversed());
3317 Context()->Replace ( V1, Vm.Oriented(V1.Orientation()) );
3318 Context()->Replace ( V2, Vm.Oriented(V2.Orientation()) );
3321 Standard_Integer next = ( num1 == NbEdges()) ? 1 : num1+1;
3322 Standard_Integer prev = ( num > 1) ? num-1 : NbEdges();
3323 TopoDS_Edge prevE = sewd->Edge(prev), nextE = sewd->Edge(next);
3325 TopoDS_Edge tmpE1=sbe.CopyReplaceVertices( prevE, TopoDS_Vertex(), Vm);
3326 sewd->Set ( tmpE1,prev );
3327 if ( !Context().IsNull() ) Context()->Replace ( prevE, tmpE1);
3329 tmpE1 = sbe.CopyReplaceVertices ( nextE, Vm, TopoDS_Vertex());
3330 sewd->Set ( tmpE1,next );
3331 if ( !Context().IsNull() ) Context()->Replace ( nextE, tmpE1);
3333 //removing edges from wire
3334 Standard_Integer n1, n2;
3336 n1 = num; n2 = num1;
3338 n1 = num1; n2 = num;
3344 //=======================================================================
3345 //function : UpdateWire
3347 //=======================================================================
3349 void ShapeFix_Wire::UpdateWire ()
3351 Handle(ShapeExtend_WireData) sbwd = WireData();
3352 for ( Standard_Integer i=1; i <= sbwd->NbEdges(); i++ ) {
3353 TopoDS_Edge E = sbwd->Edge(i);
3354 TopoDS_Shape S = Context()->Apply ( E );
3355 if ( S == E ) continue;
3356 for ( TopExp_Explorer exp(S,TopAbs_EDGE); exp.More(); exp.Next() )
3357 sbwd->Add ( exp.Current(), i++ );
3358 sbwd->Remove ( i-- );
3362 //=======================================================================
3363 //function : FixTails
3365 //=======================================================================
3366 Standard_Boolean ShapeFix_Wire::FixTails()
3368 if (myMaxTailWidth < 0 || !IsReady())
3370 return Standard_False;
3373 myLastFixStatus = ShapeExtend::EncodeStatus(ShapeExtend_OK);
3374 if (!Context().IsNull())
3378 Handle(ShapeExtend_WireData) aSEWD = WireData();
3379 Standard_Integer aECount = NbEdges(), aENs[] = {aECount, 1};
3380 Standard_Boolean aCheckAngle = Standard_True;
3381 while (aECount >= 2 && aENs[1] <= aECount)
3383 const TopoDS_Edge aEs[] = {aSEWD->Edge(aENs[0]), aSEWD->Edge(aENs[1])};
3384 TopoDS_Edge aEParts[2][2];
3385 if (!myAnalyzer->CheckTail(aEs[0], aEs[1],
3386 aCheckAngle ? myMaxTailAngleSine : -1, myMaxTailWidth, MaxTolerance(),
3387 aEParts[0][0], aEParts[0][1], aEParts[1][0], aEParts[1][1]))
3389 aENs[0] = aENs[1]++;
3390 aCheckAngle = Standard_True;
3394 // Provide not less than 1 edge in the result wire.
3395 Standard_Integer aSplitCounts[] =
3396 {aEParts[0][1].IsNull() ? 0 : 1, aEParts[1][1].IsNull() ? 0 : 1};
3397 const Standard_Integer aRemoveCount =
3398 (aEParts[0][0].IsNull() ? 0 : 1) + (aEParts[1][0].IsNull() ? 0 : 1);
3399 if (aECount + aSplitCounts[0] + aSplitCounts[1] < 1 + aRemoveCount)
3401 aENs[0] = aENs[1]++;
3402 aCheckAngle = Standard_True;
3407 for (Standard_Integer aEI = 0; aEI < 2; ++aEI)
3409 if (aSplitCounts[aEI] == 0)
3414 // Replace the edge by the wire of its parts in the shape.
3415 const TopoDS_Edge aE = aEs[aEI];
3416 if (!Context().IsNull())
3419 BRep_Builder().MakeWire(aEWire);
3420 BRep_Builder().Add(aEWire, aEParts[aEI][0]);
3421 BRep_Builder().Add(aEWire, aEParts[aEI][1]);
3422 TopoDS_Edge aFE = TopoDS::Edge(aE.Oriented(TopAbs_FORWARD));
3423 Context()->Replace(aFE, aEWire);
3426 // Replace the edge by its parts in the edge wire.
3427 const TopAbs_Orientation aOrient = aE.Orientation();
3428 aEParts[aEI][0].Orientation(aOrient);
3429 aEParts[aEI][1].Orientation(aOrient);
3430 const Standard_Integer aFirstPI = (aOrient != TopAbs_REVERSED) ? 0 : 1;
3431 const Standard_Integer aAdd =
3432 (aEI == 0 || aENs[1] < aENs[0]) ? 0 : aSplitCounts[0];
3433 aSEWD->Set(aEParts[aEI][aFirstPI], aENs[aEI] + aAdd);
3434 aSEWD->Add(aEParts[aEI][1 - aFirstPI], aENs[aEI] + 1 + aAdd);
3438 if (aRemoveCount == 2)
3440 aCheckAngle = Standard_True;
3441 FixDummySeam(aENs[0] + aSplitCounts[0] +
3442 ((aENs[0] < aENs[1]) ? 0 : aSplitCounts[1]));
3443 if (!Context().IsNull())
3447 myLastFixStatus |= ShapeExtend::EncodeStatus(ShapeExtend_DONE);
3449 if (aSplitCounts[0] + aSplitCounts[1] == 2)
3451 aENs[0] = aENs[1]++;
3455 if (aSplitCounts[0] == aSplitCounts[1])
3468 aCheckAngle = Standard_False;
3473 if (aSplitCounts[0] != 0)
3475 aENs[0] = (aENs[0] <= aECount) ? aENs[0] : aECount;
3494 aCheckAngle = Standard_False;
3496 const Standard_Integer aRI = aEParts[0][0].IsNull() ? 1 : 0;
3497 if (aSplitCounts[aRI] != 0)
3514 aENs[0] = (aENs[1] > 1) ? aENs[0] : aECount;
3517 aSEWD->Remove(aENs[aRI] + ((aRI != 0 || aSplitCounts[0] == 0) ? 0 : 1));
3518 if (!Context().IsNull())
3520 Context()->Remove(aEs[aRI].Oriented(TopAbs_FORWARD));
3523 myLastFixStatus |= ShapeExtend::EncodeStatus(ShapeExtend_DONE);
3526 myStatusNotches = myLastFixStatus;
3527 return ShapeExtend::DecodeStatus(myLastFixStatus, ShapeExtend_DONE);