1 // Created on: 1995-03-24
2 // Created by: Jing Cheng MEI
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 // dcl CCI60011 : Correction of degeneratedSection
18 // Improvement of SameParameter Edge to treat case of failure in BRepLib::SameParameter
19 // dcl Thu Aug 20 09:24:49 1998
20 // Suppression of little faces.
21 // dcl Fri Aug 7 15:27:46 1998
22 // Refection of function SameParameter Edge.
23 // Merge on the edge which has the less of poles.
24 // Suppression of the Connected Edge function.
25 // dcl Tue Jun 9 14:21:53 1998
26 // Do not merge edge if they belong the same face
27 // Tolerance management in VerticesAssembling
28 // Tolerance management in Cutting
29 // dcl Thu May 14 15:51:46 1998
30 // optimization of cutting
31 // dcl Thu May 7 15:51:46 1998
32 // Add of cutting option
33 // Add of SameParameter call
37 //-- dpf December 10 1997 Processing of pcurve collections
39 //rln 02.02.99 BUC60449 Making compilable on NT in DEB mode
40 //rln 02.02.99 BUC60449 Protection against exception on NT
45 #include <Bnd_Box.hxx>
46 #include <Bnd_Box2d.hxx>
47 #include <Bnd_HArray1OfBox.hxx>
48 #include <BndLib_Add2dCurve.hxx>
49 #include <BndLib_Add3dCurve.hxx>
50 #include <BRep_Builder.hxx>
51 #include <BRep_ListOfPointRepresentation.hxx>
52 #include <BRep_PointOnCurve.hxx>
53 #include <BRep_Tool.hxx>
54 #include <BRep_TVertex.hxx>
55 #include <BRepBuilderAPI_BndBoxTreeSelector.hxx>
56 #include <BRepBuilderAPI_CellFilter.hxx>
57 #include <BRepBuilderAPI_Sewing.hxx>
58 #include <BRepBuilderAPI_VertexInspector.hxx>
59 #include <BRepLib.hxx>
60 #include <BRepTools.hxx>
61 #include <BRepTools_Quilt.hxx>
62 #include <BRepTools_ReShape.hxx>
63 #include <BSplCLib.hxx>
64 #include <Extrema_ExtPC.hxx>
65 #include <GCPnts_AbscissaPoint.hxx>
66 #include <GCPnts_UniformAbscissa.hxx>
67 #include <GCPnts_UniformDeflection.hxx>
68 #include <Geom2d_BezierCurve.hxx>
69 #include <Geom2d_BSplineCurve.hxx>
70 #include <Geom2d_Curve.hxx>
71 #include <Geom2d_Line.hxx>
72 #include <Geom2d_TrimmedCurve.hxx>
73 #include <Geom2dAdaptor_Curve.hxx>
74 #include <Geom2dConvert.hxx>
75 #include <Geom_BezierCurve.hxx>
76 #include <Geom_BSplineCurve.hxx>
77 #include <Geom_Curve.hxx>
78 #include <Geom_Line.hxx>
79 #include <Geom_OffsetSurface.hxx>
80 #include <Geom_RectangularTrimmedSurface.hxx>
81 #include <Geom_Surface.hxx>
82 #include <GeomAdaptor_Curve.hxx>
83 #include <GeomAdaptor_Surface.hxx>
84 #include <GeomLib.hxx>
87 #include <Message_ProgressIndicator.hxx>
88 #include <Message_ProgressSentry.hxx>
89 #include <NCollection_UBTreeFiller.hxx>
90 #include <Precision.hxx>
91 #include <Standard_ErrorHandler.hxx>
92 #include <Standard_Failure.hxx>
93 #include <Standard_NoSuchObject.hxx>
94 #include <Standard_OutOfRange.hxx>
95 #include <Standard_Type.hxx>
96 #include <TColgp_Array1OfVec.hxx>
97 #include <TColgp_SequenceOfPnt.hxx>
98 #include <TColStd_Array1OfInteger.hxx>
99 #include <TColStd_Array1OfReal.hxx>
100 #include <TColStd_Array2OfReal.hxx>
101 #include <TColStd_DataMapIteratorOfDataMapOfIntegerListOfInteger.hxx>
102 #include <TColStd_DataMapOfIntegerListOfInteger.hxx>
103 #include <TColStd_IndexedMapOfInteger.hxx>
104 #include <TColStd_ListIteratorOfListOfInteger.hxx>
105 #include <TColStd_ListOfInteger.hxx>
106 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
107 #include <TColStd_MapOfInteger.hxx>
108 #include <TColStd_SequenceOfReal.hxx>
109 #include <TopAbs.hxx>
110 #include <TopExp.hxx>
111 #include <TopExp_Explorer.hxx>
112 #include <TopLoc_Location.hxx>
113 #include <TopoDS.hxx>
114 #include <TopoDS_Compound.hxx>
115 #include <TopoDS_Edge.hxx>
116 #include <TopoDS_Face.hxx>
117 #include <TopoDS_Iterator.hxx>
118 #include <TopoDS_Shape.hxx>
119 #include <TopoDS_Shell.hxx>
120 #include <TopoDS_Vertex.hxx>
121 #include <TopoDS_Wire.hxx>
122 #include <TopTools_Array1OfShape.hxx>
123 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
124 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
125 #include <TopTools_DataMapOfShapeInteger.hxx>
126 #include <TopTools_DataMapOfShapeListOfShape.hxx>
127 #include <TopTools_ListIteratorOfListOfShape.hxx>
128 #include <TopTools_ListOfShape.hxx>
129 #include <TopTools_MapIteratorOfMapOfShape.hxx>
130 #include <TopTools_MapOfShape.hxx>
131 #include <TopTools_SequenceOfShape.hxx>
133 //#include <LocalAnalysis_SurfaceContinuity.hxx>
134 //=======================================================================
135 //function : SameRange
137 //=======================================================================
138 Handle(Geom2d_Curve) BRepBuilderAPI_Sewing::SameRange(const Handle(Geom2d_Curve)& CurvePtr,
139 const Standard_Real FirstOnCurve,
140 const Standard_Real LastOnCurve,
141 const Standard_Real RequestedFirst,
142 const Standard_Real RequestedLast) const
144 Handle(Geom2d_Curve) NewCurvePtr;
147 GeomLib::SameRange(Precision::PConfusion(),CurvePtr,FirstOnCurve,LastOnCurve,
148 RequestedFirst,RequestedLast,NewCurvePtr);
150 catch (Standard_Failure) {
152 cout << "Exception in BRepBuilderAPI_Sewing::SameRange: ";
153 Standard_Failure::Caught()->Print(cout); cout << endl;
159 //=======================================================================
160 //function : WhichFace
161 //purpose : Give the face whose edge is the border
162 //=======================================================================
164 TopoDS_Face BRepBuilderAPI_Sewing::WhichFace(const TopoDS_Edge& theEdg, const Standard_Integer index) const
166 TopoDS_Shape bound = theEdg;
167 if (mySectionBound.IsBound(bound)) bound = mySectionBound(bound);
168 if (myBoundFaces.Contains(bound)) {
169 Standard_Integer i = 1;
170 TopTools_ListIteratorOfListOfShape itf(myBoundFaces.FindFromKey(bound));
171 for (; itf.More(); itf.Next(), i++)
172 if (i == index) return TopoDS::Face(itf.Value());
174 return TopoDS_Face();
177 //=======================================================================
178 //function : IsClosedShape
180 //=======================================================================
182 static Standard_Boolean IsClosedShape(const TopoDS_Shape& theshape,
183 const TopoDS_Shape& v1, const TopoDS_Shape& v2)
185 Standard_Real TotLength = 0.0;
186 TopExp_Explorer aexp;
187 for (aexp.Init(theshape,TopAbs_EDGE); aexp.More(); aexp.Next()) {
188 TopoDS_Edge aedge = TopoDS::Edge(aexp.Current());
189 if (aedge.IsNull()) continue;
190 TopoDS_Vertex ve1,ve2;
191 TopExp::Vertices(aedge,ve1,ve2);
192 if (!ve1.IsSame(v1) && !ve1.IsSame(v2)) continue;
193 if (BRep_Tool::Degenerated(aedge)) continue;
194 Standard_Real first,last;
195 Handle(Geom_Curve) c3d = BRep_Tool::Curve(TopoDS::Edge(aedge), first, last);
197 GeomAdaptor_Curve cAdapt(c3d);
198 Standard_Real length = GCPnts_AbscissaPoint::Length(cAdapt, first, last);
200 if (ve2.IsSame(v1) || ve2.IsSame(v2)) break;
203 if (TotLength > 0.0) {
204 gp_Pnt p1 = BRep_Tool::Pnt(TopoDS::Vertex(v1));
205 gp_Pnt p2 = BRep_Tool::Pnt(TopoDS::Vertex(v2));
206 return (p1.Distance(p2) < TotLength/(1.2 * M_PI));
208 return Standard_False;
211 //=======================================================================
212 //function : IsClosedByIsos
214 //=======================================================================
215 static Standard_Boolean IsClosedByIsos(const Handle(Geom_Surface)& thesurf,
216 const Handle(Geom2d_Curve)& acrv2d,
217 const Standard_Real f2d,
218 const Standard_Real l2d,
219 const Standard_Boolean isUIsos)
221 Standard_Boolean isClosed = Standard_False;
223 gp_Pnt2d psurf1 = (acrv2d->IsPeriodic() ?
224 acrv2d->Value(f2d) : acrv2d->Value(Max(f2d,acrv2d->FirstParameter())));
225 gp_Pnt2d psurf2 = (acrv2d->IsPeriodic() ?
226 acrv2d->Value(l2d) : acrv2d->Value(Min(l2d,acrv2d->LastParameter())));
227 Handle(Geom_Curve) aCrv1;
228 Handle(Geom_Curve) aCrv2;
230 aCrv1 = thesurf->UIso(psurf1.X());
231 aCrv2 = thesurf->UIso(psurf2.X());
234 aCrv1 = thesurf->VIso(psurf1.Y());
235 aCrv2 = thesurf->VIso(psurf2.Y());
237 gp_Pnt p11,p1m,p12,p21,p2m,p22;
238 Standard_Real af1 = aCrv1->FirstParameter();
239 Standard_Real al1 = aCrv1->LastParameter();
240 Standard_Real af2 = aCrv2->FirstParameter();
241 Standard_Real al2 = aCrv2->LastParameter();
243 aCrv1->D0((af1+al1)*0.5,p1m);
246 aCrv2->D0((af2+al2)*0.5,p2m);
248 isClosed = (((p11.XYZ() - p12.XYZ()).Modulus() <
249 (p11.XYZ() - p1m.XYZ()).Modulus() - Precision::Confusion()) &&
250 ((p21.XYZ() - p22.XYZ()).Modulus() <
251 (p21.XYZ() - p2m.XYZ()).Modulus() - Precision::Confusion())) ;
254 //=======================================================================
255 //function : IsUClosedSurface
257 //=======================================================================
259 Standard_Boolean BRepBuilderAPI_Sewing::IsUClosedSurface(const Handle(Geom_Surface)& surf,
260 const TopoDS_Shape& theEdge,
261 const TopLoc_Location& theloc) const
263 Handle(Geom_Surface) tmpsurf = surf;
264 if(tmpsurf->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface)))
265 tmpsurf = Handle(Geom_RectangularTrimmedSurface)::DownCast(surf)->BasisSurface();
266 else if(tmpsurf->IsKind(STANDARD_TYPE(Geom_OffsetSurface)))
267 tmpsurf = Handle(Geom_OffsetSurface)::DownCast(surf)->BasisSurface();
269 Standard_Boolean isClosed = tmpsurf->IsUClosed();
271 Standard_Real f2d, l2d;
272 Handle(Geom2d_Curve) acrv2d = BRep_Tool::CurveOnSurface(TopoDS::Edge(theEdge), surf,theloc, f2d, l2d);
274 isClosed = IsClosedByIsos(tmpsurf,acrv2d,f2d, l2d,Standard_False );
279 return IsUClosedSurface(tmpsurf,theEdge,theloc);
280 //return surf->IsUClosed();
283 //=======================================================================
284 //function : IsVClosedSurface
286 //=======================================================================
288 Standard_Boolean BRepBuilderAPI_Sewing::IsVClosedSurface(const Handle(Geom_Surface)& surf,
289 const TopoDS_Shape& theEdge,
290 const TopLoc_Location& theloc) const
292 Handle(Geom_Surface) tmpsurf = surf;
293 if(tmpsurf->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface)))
294 tmpsurf = Handle(Geom_RectangularTrimmedSurface)::DownCast(surf)->BasisSurface();
295 else if(tmpsurf->IsKind(STANDARD_TYPE(Geom_OffsetSurface)))
296 tmpsurf = Handle(Geom_OffsetSurface)::DownCast(surf)->BasisSurface();
298 Standard_Boolean isClosed = tmpsurf->IsVClosed();
300 Standard_Real f2d, l2d;
301 Handle(Geom2d_Curve) acrv2d = BRep_Tool::CurveOnSurface(TopoDS::Edge(theEdge), surf,theloc, f2d, l2d);
303 isClosed = IsClosedByIsos(tmpsurf,acrv2d,f2d, l2d,Standard_True );
307 return IsVClosedSurface(tmpsurf,theEdge,theloc);
308 //return surf->IsVClosed();
311 //=======================================================================
312 //function : SameParameter
313 //purpose : internal use
314 //=======================================================================
316 void BRepBuilderAPI_Sewing::SameParameter(const TopoDS_Edge& edge) const
320 BRepLib::SameParameter(edge);
322 catch (Standard_Failure) {
324 cout << "Exception in BRepBuilderAPI_Sewing::SameParameter: ";
325 Standard_Failure::Caught()->Print(cout); cout << endl;
330 //=======================================================================
331 //function : SameParameterEdge
332 //purpose : internal use
333 // Merge the Sequence Of Section on one edge.
334 // This function keep the curve3d,curve2d,range and parametrization
335 // from the first section, and report and made sameparameter the
336 // pcurves of the other function.
337 // This function works when the are not more than two Pcurves
339 //=======================================================================
341 TopoDS_Edge BRepBuilderAPI_Sewing::SameParameterEdge(const TopoDS_Shape& edge,
342 const TopTools_SequenceOfShape& seqEdges,
343 const TColStd_SequenceOfInteger& seqForward,
344 TopTools_MapOfShape& mapMerged,
345 const Handle(BRepTools_ReShape)& locReShape)
347 // Retrieve reference section
348 TopoDS_Shape aTmpShape = myReShape->Apply(edge); //for porting
349 TopoDS_Edge Edge1 = TopoDS::Edge(aTmpShape);
350 aTmpShape = locReShape->Apply(Edge1);
351 if (locReShape != myReShape) Edge1 = TopoDS::Edge(aTmpShape);
352 Standard_Boolean isDone = Standard_False;
354 // Create data structures for temporary merged edges
355 TopTools_ListOfShape listFaces1;
356 TopTools_MapOfShape MergedFaces;
360 // Fill MergedFaces with faces of Edge1
361 TopoDS_Shape bnd1 = edge;
362 if (mySectionBound.IsBound(bnd1)) bnd1 = mySectionBound(bnd1);
363 if (myBoundFaces.Contains(bnd1)) {
364 TopTools_ListIteratorOfListOfShape itf(myBoundFaces.FindFromKey(bnd1));
365 for (; itf.More(); itf.Next())
366 if (MergedFaces.Add(itf.Value()))
367 listFaces1.Append(itf.Value());
372 // Create presentation edge
373 TopoDS_Vertex V1, V2;
374 TopExp::Vertices(Edge1,V1,V2);
375 if (myVertexNode.Contains(V1)) V1 = TopoDS::Vertex(myVertexNode.FindFromKey(V1));
376 if (myVertexNode.Contains(V2)) V2 = TopoDS::Vertex(myVertexNode.FindFromKey(V2));
378 TopoDS_Edge NewEdge = Edge1;
382 BRep_Builder aBuilder;
383 TopoDS_Shape anEdge = NewEdge.Oriented(TopAbs_FORWARD);
384 aBuilder.Add(anEdge,V1.Oriented(TopAbs_FORWARD));
385 aBuilder.Add(anEdge,V2.Oriented(TopAbs_REVERSED));
390 Standard_Boolean isForward = Standard_True;
392 // Merge candidate sections
393 for (Standard_Integer i = 1; i <= seqEdges.Length(); i++) {
395 // Retrieve candidate section
396 TopoDS_Shape oedge2 = seqEdges(i);
400 aTmpShape = myReShape->Apply(oedge2); //for porting
401 TopoDS_Edge Edge2 = TopoDS::Edge(aTmpShape);
402 aTmpShape = locReShape->Apply(Edge2);
403 if (locReShape != myReShape) Edge2 = TopoDS::Edge(aTmpShape);
405 // Calculate relative orientation
406 Standard_Integer Orientation = seqForward(i);
407 if (!isForward) Orientation = (Orientation? 0 : 1);
409 // Retrieve faces information for the second edge
410 TopoDS_Shape bnd2 = oedge2;
411 if (mySectionBound.IsBound(bnd2)) bnd2 = mySectionBound(bnd2);
412 if (!myBoundFaces.Contains(bnd2)) continue; // Skip floating edge
413 const TopTools_ListOfShape& listFaces2 = myBoundFaces.FindFromKey(bnd2);
415 Standard_Integer whichSec = 1; // Indicates on which edge the pCurve has been reported
416 TopoDS_Edge NewEdge = SameParameterEdge(Edge1,Edge2,listFaces1,listFaces2,Orientation,whichSec);
417 if (NewEdge.IsNull()) continue;
419 // Record faces information for the temporary merged edge
420 TopTools_ListIteratorOfListOfShape itf(listFaces2);
421 for (; itf.More(); itf.Next())
422 if (MergedFaces.Add(itf.Value()))
423 listFaces1.Append(itf.Value());
425 // Record merged section orientation
426 if (!Orientation && whichSec != 1)
427 isForward = isForward? Standard_False : Standard_True;
431 // Append actually merged edge
432 mapMerged.Add(oedge2);
433 isDone = Standard_True;
435 if (!myNonmanifold) break;
439 // Change result orientation
440 Edge1.Orientation(isForward? TopAbs_FORWARD : TopAbs_REVERSED);
442 else Edge1.Nullify();
447 //=======================================================================
448 //function : SameParameterEdge
449 //purpose : internal use
450 //=======================================================================
451 static Standard_Boolean findNMVertices(const TopoDS_Edge& theEdge,
452 TopTools_SequenceOfShape& theSeqNMVert,
453 TColStd_SequenceOfReal& theSeqPars)
455 TopoDS_Iterator aItV(theEdge,Standard_False);
456 for( ; aItV.More(); aItV.Next()) {
457 if(aItV.Value().Orientation() == TopAbs_INTERNAL ||
458 aItV.Value().Orientation() == TopAbs_EXTERNAL)
459 theSeqNMVert.Append(aItV.Value());
461 Standard_Integer nbV = theSeqNMVert.Length();
463 return Standard_False;
464 Standard_Real first, last;
465 Handle(Geom_Curve) c3d = BRep_Tool::Curve(theEdge,first, last);
466 GeomAdaptor_Curve GAC(c3d);
467 Extrema_ExtPC locProj;
468 locProj.Initialize(GAC, first, last);
469 gp_Pnt pfirst = GAC.Value(first), plast = GAC.Value(last);
472 for (Standard_Integer i = 1; i <= nbV; i++) {
473 TopoDS_Vertex aV = TopoDS::Vertex(theSeqNMVert.Value(i));
474 gp_Pnt pt = BRep_Tool::Pnt(aV);
476 Standard_Real distF2 = pfirst.SquareDistance(pt);
477 Standard_Real distL2 = plast.SquareDistance(pt);
478 Standard_Real apar = (distF2 > distL2 ? last : first);
479 // Project current point on curve
481 if (locProj.IsDone() && locProj.NbExt() > 0) {
482 Standard_Real dist2Min = Min(distF2,distL2);
483 Standard_Integer ind, indMin = 0;
484 for (ind = 1; ind <= locProj.NbExt(); ind++) {
485 Standard_Real dProj2 = locProj.SquareDistance(ind);
486 if (dProj2 < dist2Min) {
487 indMin = ind; dist2Min = dProj2;
491 apar = locProj.Point(indMin).Parameter();
493 theSeqPars.Append(apar);
497 return Standard_True;
500 static void ComputeToleranceVertex(TopoDS_Vertex theV1, TopoDS_Vertex theV2,
501 TopoDS_Vertex& theNewV)
503 Standard_Integer m, n;
504 Standard_Real aR[2], dR, aD, aEps;
509 aEps = RealEpsilon();
512 for (m = 0; m < 2; ++m) {
513 aP[m] = BRep_Tool::Pnt(aV[m]);
514 aR[m] = BRep_Tool::Tolerance(aV[m]);
524 dR = aR[m] - aR[n]; // dR >= 0.
525 gp_Vec aVD(aP[m], aP[n]);
526 aD = aVD.Magnitude();
528 if (aD <= dR || aD < aEps) {
529 aBB.MakeVertex (theNewV, aP[m], aR[m]);
536 aRr = 0.5 * (aR[m] + aR[n] + aD);
537 aXYZr = 0.5 * (aP[m].XYZ() + aP[n].XYZ() - aVD.XYZ() * (dR/aD));
540 aBB.MakeVertex (theNewV, aPr, aRr);
545 static void ComputeToleranceVertex(TopoDS_Vertex theV1, TopoDS_Vertex theV2,
546 TopoDS_Vertex theV3, TopoDS_Vertex& theNewV)
548 Standard_Real aDi, aDmax;
553 gp_XYZ aXYZ(0.,0.,0.);
557 for (Standard_Integer i = 0; i < 3; ++i) {
558 aP[i] = BRep_Tool::Pnt(aV[i]);
559 aR[i] = BRep_Tool::Tolerance(aV[i]);
560 aXYZ = aXYZ + aP[i].XYZ();
564 aCenter.SetXYZ(aXYZ);
567 for ( Standard_Integer i = 0; i < 3; ++i) {
568 aDi = aCenter.Distance(aP[i]);
575 aBB.MakeVertex (theNewV, aCenter, aDmax);
579 TopoDS_Edge BRepBuilderAPI_Sewing::SameParameterEdge(const TopoDS_Edge& edgeFirst,
580 const TopoDS_Edge& edgeLast,
581 const TopTools_ListOfShape& listFacesFirst,
582 const TopTools_ListOfShape& listFacesLast,
583 const Standard_Boolean secForward,
584 Standard_Integer& whichSec,
585 const Standard_Boolean firstCall)
587 // Do not process floating edges
588 if (!listFacesFirst.Extent() || !listFacesLast.Extent()) return TopoDS_Edge();
591 TopoDS_Edge edge1, edge2;
593 // Take the longest edge as first
595 Handle(Geom_Curve) c3d1 = BRep_Tool::Curve(TopoDS::Edge(edgeFirst), f, l);
596 GeomAdaptor_Curve cAdapt1(c3d1);
597 Standard_Real len1 = GCPnts_AbscissaPoint::Length(cAdapt1, f, l);
598 Handle(Geom_Curve) c3d2 = BRep_Tool::Curve(TopoDS::Edge(edgeLast), f, l);
599 GeomAdaptor_Curve cAdapt2(c3d2);
600 Standard_Real len2 = GCPnts_AbscissaPoint::Length(cAdapt2, f, l);
625 Standard_Real first, last;
626 BRep_Tool::Range(edge1, first, last);
627 BRep_Builder aBuilder;
629 //To keep NM vertices on edge
630 TopTools_SequenceOfShape aSeqNMVert;
631 TColStd_SequenceOfReal aSeqNMPars;
632 findNMVertices(edge1,aSeqNMVert,aSeqNMPars);
633 findNMVertices(edge2,aSeqNMVert,aSeqNMPars);
637 aBuilder.MakeEdge(edge);
638 edge.Orientation( edge1.Orientation());
641 // Retrieve edge curve
642 TopLoc_Location loc3d;
643 Standard_Real first3d, last3d;
644 Handle(Geom_Curve) c3d = BRep_Tool::Curve(edge1, loc3d, first3d, last3d);
645 if (!loc3d.IsIdentity()) {
646 c3d = Handle(Geom_Curve)::DownCast(c3d->Copy());
647 c3d->Transform(loc3d.Transformation());
649 aBuilder.UpdateEdge(edge,c3d,BRep_Tool::Tolerance(edge1));
650 aBuilder.Range(edge, first, last);
651 aBuilder.SameRange(edge, Standard_False); //Standard_True
652 aBuilder.SameParameter(edge, Standard_False);
653 // Create and add new vertices
655 TopoDS_Vertex V1New, V2New;
657 // Retrieve original vertices from edges
658 TopoDS_Vertex V11,V12,V21,V22;
659 TopExp::Vertices(edge1,V11,V12);
660 TopExp::Vertices(edge2,V21,V22);
662 //check that edges merged valid way (for edges having length less than specified
664 // Check if edges are closed
665 Standard_Boolean isClosed1 = V11.IsSame(V12);
666 Standard_Boolean isClosed2 = V21.IsSame(V22);
667 if(!isClosed1 && !isClosed2)
671 if( V11.IsSame(V22) || V12.IsSame(V21) )
672 return TopoDS_Edge();
676 if( V11.IsSame(V21) || V12.IsSame(V22) )
677 return TopoDS_Edge();
681 //szv: do not reshape here!!!
682 //V11 = TopoDS::Vertex(myReShape->Apply(V11));
683 //V12 = TopoDS::Vertex(myReShape->Apply(V12));
684 //V21 = TopoDS::Vertex(myReShape->Apply(V21));
685 //V22 = TopoDS::Vertex(myReShape->Apply(V22));
687 //Standard_Boolean isRev = Standard_False;
688 if (isClosed1 || isClosed2) {
689 // at least one of the edges is closed
690 if (isClosed1 && isClosed2) {
691 // both edges are closed
692 ComputeToleranceVertex(V11, V21, V1New);
694 else if (isClosed1) {
695 // only first edge is closed
696 ComputeToleranceVertex(V22, V21, V11, V1New);
699 // only second edge is closed
700 ComputeToleranceVertex(V11, V12, V21, V1New);
705 // both edges are open
706 Standard_Boolean isOldFirst = ( secForward ? V11.IsSame(V21) : V11.IsSame(V22) );
707 Standard_Boolean isOldLast = ( secForward ? V12.IsSame(V22) : V12.IsSame(V21)) ;
709 //case if vertices already sewed
712 ComputeToleranceVertex(V11, V21, V1New);
716 ComputeToleranceVertex(V12, V22, V2New);
722 ComputeToleranceVertex(V11, V22, V1New);
726 ComputeToleranceVertex(V12, V21, V2New);
735 // Add the vertices in the good sense
736 TopoDS_Shape anEdge = edge.Oriented(TopAbs_FORWARD);
737 TopoDS_Shape aLocalEdge = V1New.Oriented(TopAbs_FORWARD); //(listNode.First()).Oriented(TopAbs_FORWARD);
738 aBuilder.Add(anEdge,aLocalEdge);
739 aLocalEdge = V2New.Oriented(TopAbs_REVERSED); //(listNode.Last()).Oriented(TopAbs_REVERSED);
740 aBuilder.Add(anEdge,aLocalEdge);
742 Standard_Integer k =1;
743 for( ; k <= aSeqNMVert.Length(); k++)
744 aBuilder.Add(anEdge,aSeqNMVert.Value(k));
748 // Retrieve second PCurves
749 TopLoc_Location loc2;
750 Handle(Geom_Surface) surf2;
752 //Handle(Geom2d_Curve) c2d2, c2d21;
753 // Standard_Real firstOld, lastOld;
755 TopTools_ListIteratorOfListOfShape itf2;
756 if (whichSec == 1) itf2.Initialize(listFacesLast);
757 else itf2.Initialize(listFacesFirst);
758 Standard_Boolean isResEdge = Standard_False;
760 for (; itf2.More(); itf2.Next()) {
761 Handle(Geom2d_Curve) c2d2, c2d21;
762 Standard_Real firstOld, lastOld;
763 fac2 = TopoDS::Face(itf2.Value());
765 surf2 = BRep_Tool::Surface(fac2, loc2);
766 Standard_Boolean isSeam2 = ((IsUClosedSurface(surf2,edge2,loc2) || IsVClosedSurface(surf2,edge2,loc2)) &&
767 BRep_Tool::IsClosed(TopoDS::Edge(edge2),fac2));
769 if (!myNonmanifold) return TopoDS_Edge();
770 TopoDS_Shape aTmpShape = edge2.Reversed(); //for porting
771 c2d21 = BRep_Tool::CurveOnSurface(TopoDS::Edge(aTmpShape), fac2, firstOld, lastOld);
773 c2d2 = BRep_Tool::CurveOnSurface(edge2, fac2, firstOld, lastOld);
774 if (c2d2.IsNull() && c2d21.IsNull()) continue;
776 if (!c2d21.IsNull()) {
777 c2d21 = Handle(Geom2d_Curve)::DownCast(c2d21->Copy());
779 if (c2d21->IsKind(STANDARD_TYPE(Geom2d_Line)))
780 c2d21 = new Geom2d_TrimmedCurve(c2d21, firstOld, lastOld);
781 Standard_Real first2d = firstOld; //c2dTmp->FirstParameter(); BUG USA60321
782 Standard_Real last2d = lastOld; //c2dTmp->LastParameter();
783 firstOld = c2d21->ReversedParameter(last2d);
784 lastOld = c2d21->ReversedParameter(first2d);
787 c2d21 = SameRange(c2d21,firstOld,lastOld,first,last);
790 // Make second PCurve sameRange with the 3d curve
791 c2d2 = Handle(Geom2d_Curve)::DownCast(c2d2->Copy());
794 if (c2d2->IsKind(STANDARD_TYPE(Geom2d_Line)))
795 c2d2 = new Geom2d_TrimmedCurve(c2d2, firstOld, lastOld);
796 Standard_Real first2d = firstOld;
797 Standard_Real last2d = lastOld;
798 firstOld = c2d2->ReversedParameter(last2d);
799 lastOld = c2d2->ReversedParameter(first2d);
803 c2d2 = SameRange(c2d2,firstOld,lastOld,first,last);
804 if (c2d2.IsNull()) continue;
807 Standard_Boolean isSeam = Standard_False;
808 TopAbs_Orientation Ori = TopAbs_FORWARD;
809 //Handle(Geom2d_Curve) c2d1, c2d11;
811 TopTools_ListIteratorOfListOfShape itf1;
812 if (whichSec == 1) itf1.Initialize(listFacesFirst);
813 else itf1.Initialize(listFacesLast);
814 for (; itf1.More() && !isSeam; itf1.Next()) {
815 Handle(Geom2d_Curve) c2d1, c2d11;
816 const TopoDS_Face& fac1 = TopoDS::Face(itf1.Value());
818 TopLoc_Location loc1;
819 Handle(Geom_Surface) surf1 = BRep_Tool::Surface(fac1, loc1);
821 Standard_Real first2d, last2d;
822 Standard_Boolean isSeam1 = ((IsUClosedSurface(surf1,edge1,loc1) || IsVClosedSurface(surf1,edge1,loc1)) &&
823 BRep_Tool::IsClosed(TopoDS::Edge(edge1),fac1));
824 c2d1 = BRep_Tool::CurveOnSurface(edge1, fac1, first2d, last2d);
825 Ori = edge1.Orientation();
826 if (fac1.Orientation() == TopAbs_REVERSED)
827 Ori = TopAbs::Reverse(Ori);
830 if (!myNonmanifold) return TopoDS_Edge();
831 TopoDS_Shape aTmpShape = edge1.Reversed(); //for porting
832 c2d11 = BRep_Tool::CurveOnSurface(TopoDS::Edge(aTmpShape), fac1, first2d, last2d);
833 //if(fac1.Orientation() == TopAbs_REVERSED) //
834 if(Ori == TopAbs_FORWARD)
835 aBuilder.UpdateEdge(edge,c2d1,c2d11,fac1,0);
837 aBuilder.UpdateEdge(edge,c2d11,c2d1,fac1,0);
839 else aBuilder.UpdateEdge(edge,c2d1,fac1,0);
841 if (c2d1.IsNull() && c2d11.IsNull()) continue;
843 if (surf2 == surf1) {
844 // Merge sections which are on the same face
845 if (!loc2.IsDifferent(loc1)) {
846 Standard_Boolean uclosed = IsUClosedSurface(surf2,edge2,loc2);
847 Standard_Boolean vclosed = IsVClosedSurface(surf2,edge2,loc2);
848 if (uclosed || vclosed) {
849 Standard_Real pf = c2d1->FirstParameter();
850 // Standard_Real pl = c2d1->LastParameter();
851 gp_Pnt2d p1n = c2d1->Value(Max(first,pf));
852 // gp_Pnt2d p2n = c2d1->Value(Min(pl,last));
853 gp_Pnt2d p21n = c2d2->Value(Max(first,c2d2->FirstParameter()));
854 gp_Pnt2d p22n = c2d2->Value(Min(last,c2d2->LastParameter()));
855 Standard_Real aDist = Min(p1n.Distance(p21n), p1n.Distance(p22n));
856 Standard_Real U1, U2, V1, V2;
857 surf2->Bounds(U1, U2, V1, V2);
858 isSeam = ((uclosed && aDist > 0.75*(fabs(U2-U1))) ||
859 (vclosed && aDist > 0.75*(fabs(V2-V1))));
860 if( !isSeam && BRep_Tool::IsClosed(TopoDS::Edge(edge),fac1)) continue;
865 isResEdge = Standard_True;
867 if (Ori == TopAbs_FORWARD)
868 aBuilder.UpdateEdge(edge, c2d1, c2d2, surf2, loc2, Precision::Confusion());
870 aBuilder.UpdateEdge(edge, c2d2, c2d1, surf2, loc2, Precision::Confusion());
873 TopAbs_Orientation InitOri = edge2.Orientation();
874 TopAbs_Orientation SecOri = edge.Orientation();
875 if (fac2.Orientation() == TopAbs_REVERSED) {
877 InitOri = TopAbs::Reverse(InitOri);
878 SecOri = TopAbs::Reverse(SecOri);
881 InitOri = TopAbs::Reverse(InitOri);
883 if (InitOri == TopAbs_FORWARD)
884 aBuilder.UpdateEdge(edge, c2d2,c2d21, surf2, loc2, Precision::Confusion());
886 aBuilder.UpdateEdge(edge, c2d21,c2d2, surf2, loc2, Precision::Confusion());
889 aBuilder.UpdateEdge(edge, c2d2, surf2, loc2, Precision::Confusion());
894 Standard_Real tolReached = Precision::Infinite();
895 Standard_Boolean isSamePar = Standard_False;
902 if( BRep_Tool::SameParameter(edge))
904 isSamePar = Standard_True;
905 tolReached = BRep_Tool::Tolerance(edge);
909 catch(Standard_Failure)
911 isSamePar = Standard_False;
915 if (firstCall && ( !isResEdge || !isSamePar || tolReached > myTolerance)) {
916 Standard_Integer whichSecn = whichSec;
917 // Try to merge on the second section
918 Standard_Boolean second_ok = Standard_False;
919 TopoDS_Edge s_edge = SameParameterEdge(edgeFirst,edgeLast,listFacesFirst,listFacesLast,
920 secForward,whichSecn,Standard_False);
921 if( !s_edge.IsNull())
923 Standard_Real tolReached_2 = BRep_Tool::Tolerance(s_edge);
924 second_ok = ( BRep_Tool::SameParameter(s_edge) && tolReached_2 < tolReached );
928 whichSec = whichSecn;
929 tolReached = tolReached_2;
933 if (!second_ok && !edge.IsNull()) {
935 GeomAdaptor_Curve c3dAdapt(c3d);
937 // Discretize edge curve
938 Standard_Integer i, j, nbp = 23;
939 Standard_Real deltaT = (last3d - first3d) / (nbp -1);
940 TColgp_Array1OfPnt c3dpnt(1,nbp);
941 for (i = 1; i <= nbp; i++)
942 c3dpnt(i) = c3dAdapt.Value(first3d + (i-1)*deltaT);
944 Standard_Real dist = 0., maxTol = -1.0;
945 Standard_Boolean more = Standard_True;
947 for (j = 1; more; j++) {
948 Handle(Geom2d_Curve) c2d2;
949 BRep_Tool::CurveOnSurface(edge, c2d2, surf2, loc2, first, last, j);
951 more = !c2d2.IsNull();
953 Handle(Geom_Surface) aS = surf2;
954 if(!loc2.IsIdentity())
955 aS = Handle(Geom_Surface)::DownCast(surf2->Transformed ( loc2 ));
957 Standard_Real dist2 = 0.;
958 deltaT = (last - first) / (nbp - 1);
959 for (i = 1; i <= nbp; i++) {
960 gp_Pnt2d aP2d = c2d2->Value(first + (i -1)*deltaT);
961 gp_Pnt aP2(0.,0.,0.);
962 aS->D0(aP2d.X(),aP2d.Y(), aP2);
963 gp_Pnt aP1 = c3dpnt(i);
964 dist = aP2.SquareDistance(aP1);
968 maxTol = Max(sqrt(dist2), Precision::Confusion());
971 if(maxTol >= 0. && maxTol < tolReached)
972 aBuilder.UpdateEdge(edge, maxTol);
973 aBuilder.SameParameter(edge,Standard_True);
977 BRepLib::EncodeRegularity(edge,0.01);
978 Standard_Real tolEdge1 = BRep_Tool::Tolerance(edge);
979 if (tolEdge1 > MaxTolerance()) edge.Nullify();
983 //=======================================================================
984 // function : EvaluateAngulars
985 // purpose : internal use
986 //=======================================================================
988 void BRepBuilderAPI_Sewing::EvaluateAngulars(TopTools_SequenceOfShape& sequenceSec,
989 TColStd_Array1OfBoolean& secForward,
990 TColStd_Array1OfReal& tabAng,
991 const Standard_Integer indRef) const
995 Standard_Integer i, j, npt = 4, lengSec = sequenceSec.Length();
1000 Standard_Real first, last;
1001 Handle(Geom_Curve) c3d;
1002 Handle(Geom2d_Curve) c2d;
1003 Handle(Geom_Surface) surf;
1004 TColgp_Array1OfVec normRef(1,npt);
1006 for (i = indRef; i <= lengSec; i++) {
1008 edge = TopoDS::Edge(sequenceSec(i));
1010 TopoDS_Shape bnd = edge;
1011 if (mySectionBound.IsBound(bnd)) bnd = mySectionBound(bnd);
1012 if (myBoundFaces.Contains(bnd)) {
1013 face = TopoDS::Face(myBoundFaces.FindFromKey(bnd).First());
1014 surf = BRep_Tool::Surface(face,loc);
1015 if (!loc.IsIdentity()) {
1016 surf = Handle(Geom_Surface)::DownCast(surf->Copy());
1017 surf->Transform(loc.Transformation());
1019 c2d = BRep_Tool::CurveOnSurface(edge, face, first, last);
1021 else if (i == indRef) return;
1024 c3d = BRep_Tool::Curve(edge, loc, first, last);
1025 if (!loc.IsIdentity()) {
1026 c3d = Handle(Geom_Curve)::DownCast(c3d->Copy());
1027 c3d->Transform(loc.Transformation());
1030 GeomAdaptor_Curve adapt(c3d);
1031 GCPnts_UniformAbscissa uniAbs(adapt, npt, first, last);
1033 Standard_Real cumulateAngular = 0.0;
1034 Standard_Integer nbComputedAngle = 0;
1036 for (j = 1; j <= npt; j++) {
1038 c2d->D0(uniAbs.Parameter((secForward(i) || i == indRef)? j : npt-j+1),P);
1041 surf->D1(P.X(), P.Y(), unused, w1, w2);
1042 gp_Vec n = w1^w2; // Compute the normal vector
1043 if (i == indRef) normRef(j) = n;
1044 else if ((n.Magnitude()>gp::Resolution()) && (normRef(j).Magnitude()>gp::Resolution())) {
1046 Standard_Real angular = n.Angle(normRef(j));
1047 if (angular > M_PI/2.) angular = M_PI - angular;
1048 cumulateAngular += angular;
1052 if (nbComputedAngle)
1053 tabAng(i) = cumulateAngular/((Standard_Real)nbComputedAngle);
1057 //=======================================================================
1058 // function : EvaluateDistances
1059 // purpose : internal use
1060 // Evaluate distance beetween edges with indice indRef and the following edges in the list
1061 // Remarks (lengSec - indRef) must be >= 1
1062 //=======================================================================
1063 void BRepBuilderAPI_Sewing::EvaluateDistances(TopTools_SequenceOfShape& sequenceSec,
1064 TColStd_Array1OfBoolean& secForward,
1065 TColStd_Array1OfReal& tabDst,
1066 TColStd_Array1OfReal& arrLen,
1067 TColStd_Array1OfReal& tabMinDist,
1068 const Standard_Integer indRef) const
1070 secForward.Init(Standard_True);
1073 tabMinDist.Init(Precision::Infinite());
1074 const Standard_Integer npt = 8; // Number of points for curve discretization
1075 TColgp_Array1OfPnt ptsRef(1, npt), ptsSec(1, npt);
1077 Standard_Integer i, j, lengSec = sequenceSec.Length();
1078 TColgp_SequenceOfPnt seqSec;
1080 Handle(Geom_Curve) c3dRef;
1081 Standard_Real firstRef=0., lastRef=0.;
1083 for (i = indRef; i <= lengSec; i++) {
1085 // reading of the edge (attention for the first one: reference)
1086 const TopoDS_Edge& sec = TopoDS::Edge(sequenceSec(i));
1088 TopLoc_Location loc;
1089 Standard_Real first, last;
1090 Handle(Geom_Curve) c3d = BRep_Tool::Curve(sec, loc, first, last);
1091 if (c3d.IsNull()) continue;
1092 if (!loc.IsIdentity()) {
1093 c3d = Handle(Geom_Curve)::DownCast(c3d->Copy());
1094 c3d->Transform(loc.Transformation());
1098 c3dRef = c3d; firstRef = first; lastRef = last;
1101 Standard_Real dist = Precision::Infinite(), distFor = -1.0, distRev = -1.0;
1102 Standard_Real aMinDist = Precision::Infinite();
1104 Standard_Real T, deltaT = (last - first) / (npt - 1);
1105 Standard_Real aLenSec2 = 0.;
1107 Standard_Integer nbFound = 0;
1108 for (j = 1; j <= npt; j++) {
1110 // Uniform parameter on curve
1111 if (j == 1) T = first;
1112 else if (j == npt) T = last;
1113 else T = first + (j - 1) * deltaT;
1115 // Take point on curve
1116 gp_Pnt pt = c3d->Value(T);
1121 aLenSec2 += pt.SquareDistance(ptsRef(j-1));
1125 //protection to avoid merging with small sections
1127 aLenSec2 += pt.SquareDistance(ptsSec(j-1));
1128 // To evaluate mutual orientation and distance
1129 dist = pt.Distance(ptsRef(j));
1132 if (distFor < dist) distFor = dist;
1133 dist = pt.Distance(ptsRef(npt-j+1));
1137 if (distRev < dist) distRev = dist;
1139 // Check that point lays between vertices of reference curve
1140 const gp_Pnt &p11 = ptsRef(1);
1141 const gp_Pnt &p12 = ptsRef(npt);
1142 const gp_Vec aVec1(pt,p11);
1143 const gp_Vec aVec2(pt,p12);
1144 const gp_Vec aVecRef(p11,p12);
1145 if((aVecRef * aVec1) * (aVecRef * aVec2) < 0.)
1150 Standard_Real aLenSec = sqrt(aLenSec2);
1152 //if(aLenSec < myMinTolerance )
1154 arrLen.SetValue(i,aLenSec);
1155 // Record mutual orientation
1156 Standard_Boolean isForward = (distFor < distRev); //szv debug: <=
1157 secForward(i) = isForward;
1159 dist = (isForward? distFor : distRev);
1160 if(i == indRef || (dist < myTolerance && nbFound >= npt * 0.5) )
1163 tabMinDist(i) = aMinDist;
1167 nbFound = 0, aMinDist = Precision::Infinite(), dist = -1;
1168 TColgp_Array1OfPnt arrProj(1, npt);
1169 TColStd_Array1OfReal arrDist(1, npt), arrPara(1, npt);
1170 if( arrLen(indRef) >= arrLen(i))
1171 ProjectPointsOnCurve(ptsSec,c3dRef,firstRef,lastRef,arrDist,arrPara,arrProj,Standard_False);
1173 ProjectPointsOnCurve(ptsRef,c3d,first,last,arrDist,arrPara,arrProj,Standard_False);
1174 for( j = 1; j <= npt; j++ )
1178 if(dist < arrDist(j))
1180 if( aMinDist > arrDist(j))
1181 aMinDist = arrDist(j);
1187 tabMinDist(i) = aMinDist;
1193 // Project distant points
1194 Standard_Integer nbFailed = seqSec.Length();
1195 if (!nbFailed) return;
1197 TColgp_Array1OfPnt arrPnt(1, nbFailed), arrProj(1, nbFailed);
1198 for (i = 1; i <= nbFailed; i++) arrPnt(i) = seqSec(i); seqSec.Clear();
1199 TColStd_Array1OfReal arrDist(1, nbFailed), arrPara(1, nbFailed);
1201 ProjectPointsOnCurve(arrPnt,c3dRef,firstRef,lastRef,arrDist,arrPara,arrProj,Standard_False);
1203 // Process distant sections
1204 Standard_Integer idx1 = 1;
1205 for (i = indRef + 1; i <= lengSec; i++) {
1207 // Skip section if already evaluated
1208 if (tabDst(i) >= 0.0) continue;
1210 Standard_Real dist, distMax = -1.0, aMinDist = Precision::Infinite();
1212 Standard_Integer idx2 = (idx1 - 1)*npt;
1214 for (j = 1; j <= npt; j++) {
1216 dist = arrDist(idx2 + j);
1217 // If point is not projected - stop evaluation
1218 if (dist < 0.0) { distMax = -1.0; break; }
1219 if (distMax < dist) distMax = dist;
1220 if(aMinDist > dist) aMinDist = dist;
1223 // If section is close - record distance
1224 if (distMax >= 0.0) {
1225 if (secForward(i)) {
1226 dist = arrPnt(idx2+1).Distance(ptsRef(1));
1227 if (distMax < dist) distMax = dist;
1228 if(aMinDist > dist) aMinDist = dist;
1229 dist = arrPnt(idx2+npt).Distance(ptsRef(npt));
1230 if (distMax < dist) distMax = dist;
1231 if(aMinDist > dist) aMinDist = dist;
1234 dist = arrPnt(idx2+1).Distance(ptsRef(npt));
1235 if (distMax < dist) distMax = dist;
1236 if(aMinDist > dist) aMinDist = dist;
1237 dist = arrPnt(idx2+npt).Distance(ptsRef(1));
1238 if (distMax < dist) distMax = dist;
1239 if(aMinDist > dist) aMinDist = dist;
1242 if (distMax < myTolerance)
1244 tabDst(i) = distMax;
1245 tabMinDist(i) = aMinDist;
1249 idx1++; // To the next distant curve
1253 //=======================================================================
1254 //function : IsMergedClosed
1255 //purpose : internal use
1256 //=======================================================================
1258 Standard_Boolean BRepBuilderAPI_Sewing::IsMergedClosed(const TopoDS_Edge& Edge1,
1259 const TopoDS_Edge& Edge2,
1260 const TopoDS_Face& face) const
1262 // Check for closed surface
1263 TopLoc_Location loc;
1264 Handle(Geom_Surface) surf = BRep_Tool::Surface(face,loc);
1265 Standard_Boolean isUClosed = IsUClosedSurface(surf,Edge1,loc);
1266 Standard_Boolean isVClosed = IsVClosedSurface(surf,Edge1,loc);
1267 if (!isUClosed && !isVClosed) return Standard_False;
1268 // Check condition on closed surface
1270 Standard_Real first1,last1,first2,last2;
1271 Handle(Geom_Curve) C3d1 = BRep_Tool::Curve(Edge1,first1,last1);
1272 Handle(Geom_Curve) C3d2 = BRep_Tool::Curve(Edge2,first2,last2);
1273 if (C3d1.IsNull() || C3d2.IsNull()) return Standard_False;
1275 Standard_Real first2d1,last2d1,first2d2,last2d2;
1276 Handle(Geom2d_Curve) C2d1 = BRep_Tool::CurveOnSurface(Edge1,face,first2d1,last2d1);
1277 Handle(Geom2d_Curve) C2d2 = BRep_Tool::CurveOnSurface(Edge2,face,first2d2,last2d2);
1278 if (C2d1.IsNull() || C2d2.IsNull()) return Standard_False;
1280 gp_Pnt p1 = C3d1->Value(0.5*(first1 + last1));
1281 gp_Pnt p2 = C3d1->Value(0.5*(first2 + last2));
1282 Standard_Real dist = p1.Distance(p2);
1283 gp_Pnt2d p12d = C2d1->Value(0.5*(first2d1 + last2d1));
1284 gp_Pnt2d p22d = C2d1->Value(0.5*(first2d2 + last2d2));
1285 Standard_Real dist2d = p12d.Distance(p22d);
1286 GeomAdaptor_Surface Ads(BRep_Tool::Surface(face));
1287 Standard_Real distSurf = Max(Ads.UResolution(dist), Ads.VResolution(dist));
1288 return (dist2d*0.2 >= distSurf);
1290 Standard_Integer isULongC1, isULongC2, isVLongC1, isVLongC2;
1291 Standard_Real SUmin, SUmax, SVmin, SVmax;
1292 Standard_Real C1Umin, C1Vmin, C1Umax, C1Vmax;
1293 Standard_Real C2Umin, C2Vmin, C2Umax, C2Vmax;
1294 { //szv: Use brackets to destroy local variables
1296 Geom2dAdaptor_Curve aC2d1(C2d1), aC2d2(C2d2);
1297 BndLib_Add2dCurve::Add(aC2d1,first2d1,last2d1,Precision::PConfusion(),B1);
1298 BndLib_Add2dCurve::Add(aC2d2,first2d2,last2d2,Precision::PConfusion(),B2);
1299 B1.Get(C1Umin,C1Vmin,C1Umax,C1Vmax);
1300 B2.Get(C2Umin,C2Vmin,C2Umax,C2Vmax);
1301 Standard_Real du, dv;
1302 du = (C1Umax - C1Umin); dv = (C1Vmax - C1Vmin);
1303 isULongC1 = (dv <= du); isVLongC1 = (du <= dv);
1304 du = (C2Umax - C2Umin); dv = (C2Vmax - C2Vmin);
1305 isULongC2 = (dv <= du); isVLongC2 = (du <= dv);
1306 surf->Bounds(SUmin,SUmax,SVmin,SVmax);
1308 if (isUClosed && isVLongC1 && isVLongC2) {
1309 // Do not merge if not overlapped by V
1310 Standard_Real dist = Max((C2Vmin - C1Vmax),(C1Vmin - C2Vmax));
1312 Standard_Real distInner = Max((C2Umin - C1Umax),(C1Umin - C2Umax));
1313 Standard_Real distOuter = (SUmax - SUmin) - Max((C2Umax - C1Umin),(C1Umax - C2Umin));
1314 if (distOuter <= distInner) return Standard_True;
1317 if (isVClosed && isULongC1 && isULongC2) {
1318 // Do not merge if not overlapped by U
1319 Standard_Real dist = Max((C2Umin - C1Umax),(C1Umin - C2Umax));
1321 Standard_Real distInner = Max((C2Vmin - C1Vmax),(C1Vmin - C2Vmax));
1322 Standard_Real distOuter = (SVmax - SVmin) - Max((C2Vmax - C1Vmin),(C1Vmax - C2Vmin));
1323 if (distOuter <= distInner) return Standard_True;
1326 return Standard_False;
1329 //=======================================================================
1330 //function : AnalysisNearestEdges
1332 //=======================================================================
1334 void BRepBuilderAPI_Sewing::AnalysisNearestEdges(const TopTools_SequenceOfShape& sequenceSec,
1335 TColStd_SequenceOfInteger& seqIndCandidate,
1336 TColStd_SequenceOfInteger& seqOrientations,
1337 const Standard_Boolean evalDist)
1340 Standard_Integer workIndex = seqIndCandidate.First();
1341 TopoDS_Shape workedge = sequenceSec.Value(workIndex);
1342 TopoDS_Shape bnd = workedge;
1343 TopTools_ListOfShape workfaces;
1344 if (mySectionBound.IsBound(bnd)) bnd = mySectionBound(bnd);
1345 if (myBoundFaces.Contains(bnd))
1346 workfaces = myBoundFaces.FindFromKey(bnd);
1347 if(workfaces.IsEmpty()) return;
1348 TopTools_MapOfShape mapFaces;
1349 TopTools_ListIteratorOfListOfShape lIt;
1350 for (lIt.Initialize(workfaces); lIt.More(); lIt.Next())
1351 mapFaces.Add(lIt.Value());
1352 TColStd_SequenceOfInteger seqNotCandidate;
1353 TColStd_SequenceOfInteger seqNewForward;
1354 // Separates edges belonging the same face as work edge
1355 // for exception of edges belonging closed faces
1357 seqNotCandidate.Append(workIndex);
1358 for(Standard_Integer i = 1; i<= seqIndCandidate.Length(); ) {
1359 Standard_Integer index = seqIndCandidate.Value(i);
1360 Standard_Boolean isRemove = Standard_False;
1361 if(index == workIndex) {
1362 seqIndCandidate.Remove(i);
1363 seqOrientations.Remove(i);
1364 isRemove = Standard_True;
1367 TopoDS_Shape bnd2 = sequenceSec.Value(index);
1368 if (mySectionBound.IsBound(bnd2)) bnd2 = mySectionBound(bnd2);
1370 if(myBoundFaces.Contains(bnd2)) {
1371 const TopTools_ListOfShape& listfaces = myBoundFaces.FindFromKey(bnd2);
1372 Standard_Boolean isMerged = Standard_True;
1373 for (lIt.Initialize(listfaces); lIt.More() && isMerged; lIt.Next()) {
1374 if(mapFaces.Contains(lIt.Value())) {
1375 TopLoc_Location loc;
1376 Handle(Geom_Surface) surf = BRep_Tool::Surface(TopoDS::Face(lIt.Value()),loc);
1377 isMerged = ((IsUClosedSurface(surf,bnd2,loc) || IsVClosedSurface(surf,bnd2,loc)) &&
1378 IsMergedClosed(TopoDS::Edge(sequenceSec.Value(index)),TopoDS::Edge(workedge),TopoDS::Face(lIt.Value())));
1382 seqNotCandidate.Append(index);
1383 seqIndCandidate.Remove(i);
1384 seqOrientations.Remove(i);
1385 isRemove = Standard_True;
1389 seqIndCandidate.Remove(i);
1390 seqOrientations.Remove(i);
1391 isRemove = Standard_True;
1396 if(seqIndCandidate.Length() == 0 || seqNotCandidate.Length() == 1) return;
1397 if(!evalDist) return;
1398 TColStd_Array2OfReal TotTabDist(1,seqNotCandidate.Length(),1,seqIndCandidate.Length());
1399 TColStd_MapOfInteger MapIndex;
1400 TColStd_SequenceOfInteger seqForward;
1402 // Definition and removing edges wich are not candidate for work edge
1403 // ( they have other nearest edges belonging to the work face)
1404 for(Standard_Integer k = 1; k<= seqNotCandidate.Length(); k++) {
1405 Standard_Integer index1 = seqNotCandidate.Value(k);
1406 TopoDS_Shape edge = sequenceSec.Value(index1);
1407 TopTools_SequenceOfShape tmpSeq;
1408 tmpSeq.Append(edge);
1409 for(Standard_Integer kk = 1; kk <= seqIndCandidate.Length();kk++)
1410 tmpSeq.Append(sequenceSec.Value(seqIndCandidate.Value(kk)));
1412 Standard_Integer lengSec = tmpSeq.Length();
1413 TColStd_Array1OfBoolean tabForward(1,lengSec);
1414 TColStd_Array1OfReal tabDist(1,lengSec);
1415 TColStd_Array1OfReal arrLen(1,lengSec);
1416 TColStd_Array1OfReal tabMinDist(1,lengSec);
1417 for (Standard_Integer i1 = 1 ; i1 <= lengSec; i1++)
1420 EvaluateDistances(tmpSeq,tabForward, tabDist,arrLen,tabMinDist,1 );
1422 for(Standard_Integer n = 1; n < lengSec; n++) {
1423 if(tabDist(n+1) == -1 || tabDist(n+1) > myTolerance) {
1427 TotTabDist(k,n) = tabDist(n+1 );
1428 seqForward.Append(tabForward(n+1) ? 1:0);
1432 for(Standard_Integer n = 1; n < lengSec; n++) {
1433 if(tabDist(n) == -1 || tabDist(n) > myTolerance) continue;
1434 if(tabDist(n+1) < TotTabDist(1,n)) {
1442 Standard_Integer i2 = seqIndCandidate.Length();
1443 for( ; i2 >=1 ; i2--)
1445 if(MapIndex.Contains(i2))
1447 seqIndCandidate.Remove(i2);
1448 seqOrientations.Remove(i2);
1452 //for(TColStd_MapIteratorOfMapOfInteger IMap(MapIndex); IMap.More(); IMap.Next()) {
1453 // seqIndCandidate.Remove(IMap.Key());
1454 // seqOrientations.Remove(IMap.Key());
1458 //=======================================================================
1459 //function : FindCandidates
1460 //purpose : internal use
1461 //=======================================================================
1463 Standard_Boolean BRepBuilderAPI_Sewing::FindCandidates(TopTools_SequenceOfShape& seqSections,
1464 TColStd_IndexedMapOfInteger& mapReference,
1465 TColStd_SequenceOfInteger& seqCandidates,
1466 TColStd_SequenceOfInteger& seqOrientations)
1468 Standard_Integer i, nbSections = seqSections.Length();
1470 return Standard_False;
1471 // Retrieve last reference index
1472 Standard_Integer indReference = mapReference(mapReference.Extent());
1473 Standard_Integer nbCandidates = 0;
1474 TopTools_MapOfShape Faces1;
1475 //if (nbSections > 1) {
1477 TopoDS_Edge Edge1 = TopoDS::Edge(seqSections(indReference));
1479 // Retrieve faces for reference section
1481 { //szv: Use brackets to destroy local variables
1482 TopoDS_Shape bnd = Edge1;
1483 if (mySectionBound.IsBound(bnd)) bnd = mySectionBound(bnd);
1484 if (myBoundFaces.Contains(bnd)) {
1485 TopTools_ListIteratorOfListOfShape itf1(myBoundFaces.FindFromKey(bnd));
1486 for (; itf1.More(); itf1.Next()) Faces1.Add(itf1.Value());
1490 // Check merging conditions for candidates and remove unsatisfactory
1491 TopTools_SequenceOfShape seqSectionsNew;
1492 TColStd_SequenceOfInteger seqCandidatesNew;
1493 for (i = 1; i <= nbSections; i++) {
1494 if (i == indReference) {
1495 seqSectionsNew.Prepend(Edge1);
1496 seqCandidatesNew.Prepend(i);
1499 const TopoDS_Edge& Edge2 = TopoDS::Edge(seqSections(i));
1500 seqSectionsNew.Append(Edge2);
1501 seqCandidatesNew.Append(i);
1505 Standard_Integer nbSectionsNew = seqSectionsNew.Length();
1506 if (nbSectionsNew > 1) {
1508 // Evaluate distances between reference and other sections
1509 TColStd_Array1OfBoolean arrForward(1,nbSectionsNew);
1510 TColStd_Array1OfReal arrDistance(1,nbSectionsNew);
1511 TColStd_Array1OfReal arrLen(1,nbSectionsNew);
1512 TColStd_Array1OfReal arrMinDist(1,nbSectionsNew);
1513 EvaluateDistances(seqSectionsNew,arrForward,arrDistance,arrLen,arrMinDist,1);
1515 // Fill sequence of candidate indices sorted by distance
1516 for (i = 2; i <= nbSectionsNew; i++) {
1517 Standard_Real aMaxDist = arrDistance(i);
1518 if (aMaxDist >= 0.0 && aMaxDist <= myTolerance && arrLen(i) > myMinTolerance) {
1520 // Reference section is connected to section #i
1521 Standard_Boolean isInserted = Standard_False;
1522 Standard_Integer j, ori = (arrForward(i)? 1 : 0);
1523 for (j = 1; (j <= seqCandidates.Length()) && !isInserted; j++) {
1524 Standard_Real aDelta = arrDistance(i) - arrDistance(seqCandidates.Value(j));
1526 if( aDelta < Precision::Confusion()) {
1528 if(fabs(aDelta) > RealSmall() ||
1529 arrMinDist(i) < arrMinDist(seqCandidates.Value(j)))
1531 seqCandidates.InsertBefore(j,i);
1532 seqOrientations.InsertBefore(j,ori);
1533 isInserted = Standard_True;
1538 seqCandidates.Append(i);
1539 seqOrientations.Append(ori);
1544 nbCandidates = seqCandidates.Length();
1546 return Standard_False; // Section has no candidates to merge
1548 // Replace candidate indices
1550 for (i = 1; i <= nbCandidates; i++)
1551 seqCandidates(i) = seqCandidatesNew(seqCandidates(i));
1555 if (!nbCandidates) return Standard_False; // Section has no candidates to merge
1557 if (myNonmanifold && nbCandidates >1) {
1558 TColStd_SequenceOfInteger seqNewCandidates;
1559 TColStd_SequenceOfInteger seqOrientationsNew;
1560 seqCandidates.Prepend(1);
1561 seqOrientations.Prepend(1);
1562 for(Standard_Integer k = 1; k <= seqSections.Length() && seqCandidates.Length() > 1 ; k++) {
1563 AnalysisNearestEdges(seqSections,seqCandidates,seqOrientations,(k==1));
1564 if(k == 1 && !seqCandidates.Length()) return Standard_False;
1565 if(seqCandidates.Length()) {
1566 seqNewCandidates.Append(seqCandidates.First());
1567 seqOrientationsNew.Append(seqOrientations.First());
1570 seqCandidates.Prepend(seqNewCandidates);
1571 seqOrientations.Prepend(seqOrientationsNew);
1572 return Standard_True;
1576 // For manifold case leave only one candidate from equidistant candidates
1577 /*Standard_Integer minIndex = seqCandidateIndex.First();
1578 Standard_Real minDistance = arrDistance(minIndex);
1580 // Find equidistant candidates
1581 TColStd_SequenceOfInteger seqEqDistantIndex; seqEqDistantIndex.Append(1);
1582 for (i = 2; i <= nbCandidates; i++) {
1583 Standard_Integer index = seqCandidateIndex(i);
1584 if (Abs(minDistance - arrDistance(index)) <= Precision::Confusion())
1585 seqEqDistantIndex.Append(index);
1588 Standard_Integer eqLen = seqEqDistantIndex.Length();
1591 // Fill map of faces which equidistant sections belong to
1592 TopTools_MapOfShape mapFace;
1593 for (i = 1; i <= eqLen; i++) {
1594 Standard_Integer index = seqEqDistantIndex.Value(i);
1595 if (isCandidate(index)) {
1596 mapFace.Add(arrFace(index));
1600 // Non Manifold case
1601 // Edges are merged by pair among a face continuity C1 criterion
1602 if (mapFace.Extent() == eqLen) {
1608 Standard_Integer indMin = -1;// To check if the edge can be merged.
1609 // Computation of distances between the edges.
1610 TopTools_SequenceOfShape seqSh;
1611 Standard_Integer nbInd = EqDistSeq.Length();
1612 TColStd_Array1OfBoolean tmptabForward(1,nbInd);
1613 seqSh.Append(sequenceSec.Value(1));
1614 for (j = 2; j <= EqDistSeq.Length(); j++) {
1615 Standard_Integer index = EqDistSeq.Value(j);
1616 tmptabForward(j) = tabForward(index);
1617 seqSh.Append(sequenceSec.Value(index));
1620 EvaluateAngulars(seqSh, tmptabForward, tabDist,1);
1622 for(j=2; j <= seqSh.Length(); j++) {
1623 if (tabDist(j) > -1.) { // if edge(j) is connected to edge(i)
1624 if (min > tabDist(j)) {
1631 // Construct minDist, tabMinInd , tabMinForward(i) = tabForward(j);
1633 seqSh.Remove(indMin);
1634 for(j =2; j <= tmpSeq.Length(); ) {
1635 TopoDS_Shape sh = tmpSeq.Value(j);
1636 Standard_Boolean isRem = Standard_False;
1637 for(Standard_Integer k = 1; k<= seqSh.Length();k++) {
1638 if(seqSh.Value(k) == sh) {
1639 isRem = Standard_True;
1645 tabMinForward.Remove(j); // = -1;
1653 // Find the best approved candidate
1654 while (nbCandidates) {
1655 // Retrieve first candidate
1656 Standard_Integer indCandidate = seqCandidates.First();
1657 // Candidate is approved if it is in the map
1658 if (mapReference.Contains(indCandidate)) break;
1659 // Find candidates for candidate #indCandidate
1660 mapReference.Add(indCandidate); // Push candidate in the map
1661 TColStd_SequenceOfInteger seqCandidates1, seqOrientations1;
1662 Standard_Boolean isFound =
1663 FindCandidates(seqSections,mapReference,seqCandidates1,seqOrientations1);
1664 mapReference.RemoveLast(); // Pop candidate from the map
1665 if (isFound) isFound = (seqCandidates1.Length() > 0);
1667 Standard_Integer indCandidate1 = seqCandidates1.First();
1668 // If indReference is the best candidate for indCandidate
1669 // then indCandidate is the best candidate for indReference
1670 if (indCandidate1 == indReference) break;
1671 // If some other reference in the map is the best candidate for indCandidate
1672 // then assume that reference is the best candidate for indReference
1673 if (mapReference.Contains(indCandidate1)) {
1674 seqCandidates.Prepend(indCandidate1);
1678 isFound = Standard_False;
1681 // Remove candidate #1
1682 seqCandidates.Remove(1);
1683 seqOrientations.Remove(1);
1689 if(nbCandidates > 0)
1691 Standard_Integer anInd = seqCandidates.Value(1);
1692 TopoDS_Edge Edge2 = TopoDS::Edge(seqSections(anInd));
1693 TopoDS_Shape bnd = Edge2;
1694 if (mySectionBound.IsBound(bnd))
1695 bnd = mySectionBound(bnd);
1697 if (myBoundFaces.Contains(bnd)) {
1698 Standard_Boolean isOK = Standard_True;
1699 TopTools_ListIteratorOfListOfShape itf2(myBoundFaces.FindFromKey(bnd));
1700 for (; itf2.More() && isOK; itf2.Next()) {
1701 const TopoDS_Face& Face2 = TopoDS::Face(itf2.Value());
1702 // Check whether condition is satisfied
1703 isOK = !Faces1.Contains(Face2);
1704 if (!isOK) isOK = IsMergedClosed(Edge1,Edge2,Face2);
1707 return Standard_False;
1710 return (nbCandidates > 0);
1713 //=======================================================================
1714 //function : Constructor
1716 //=======================================================================
1718 BRepBuilderAPI_Sewing::BRepBuilderAPI_Sewing(const Standard_Real tolerance,
1719 const Standard_Boolean optionSewing,
1720 const Standard_Boolean optionAnalysis,
1721 const Standard_Boolean optionCutting,
1722 const Standard_Boolean optionNonmanifold)
1724 myReShape = new BRepTools_ReShape;
1725 Init(tolerance, optionSewing, optionAnalysis, optionCutting, optionNonmanifold);
1728 //=======================================================================
1730 //purpose : Initialise Talerance, and options sewing, faceAnalysis and cutting
1731 //=======================================================================
1733 void BRepBuilderAPI_Sewing::Init(const Standard_Real tolerance,
1734 const Standard_Boolean optionSewing,
1735 const Standard_Boolean optionAnalysis,
1736 const Standard_Boolean optionCutting,
1737 const Standard_Boolean optionNonmanifold)
1739 // Set tolerance and Perform options
1740 myTolerance = Max (tolerance, Precision::Confusion());
1741 mySewing = optionSewing;
1742 myAnalysis = optionAnalysis;
1743 myCutting = optionCutting;
1744 myNonmanifold = optionNonmanifold;
1745 // Set min and max tolerances
1746 myMinTolerance = myTolerance * 1e-4; //szv: proposal
1747 if (myMinTolerance < Precision::Confusion()) myMinTolerance = Precision::Confusion();
1748 myMaxTolerance = Precision::Infinite();
1750 myFaceMode = Standard_True;
1751 myFloatingEdgesMode = Standard_False;
1752 //myCuttingFloatingEdgesMode = Standard_False; //gka
1753 mySameParameterMode = Standard_True;
1754 myLocalToleranceMode = Standard_False;
1755 mySewedShape.Nullify();
1757 Load(TopoDS_Shape());
1760 //=======================================================================
1762 //purpose : Loads the context shape
1763 //=======================================================================
1765 void BRepBuilderAPI_Sewing::Load(const TopoDS_Shape& theShape)
1768 if (theShape.IsNull()) myShape.Nullify();
1769 else myShape = myReShape->Apply(theShape);
1770 mySewedShape.Nullify();
1771 // Nullify flags and counters
1772 myNbShapes = myNbEdges = myNbVertices = 0;
1774 myOldShapes.Clear();
1775 //myOldFaces.Clear();
1776 myDegenerated.Clear();
1777 myFreeEdges.Clear();
1778 myMultipleEdges.Clear();
1779 myContigousEdges.Clear();
1780 myContigSecBound.Clear();
1781 myBoundFaces.Clear();
1782 myBoundSections.Clear();
1783 myVertexNode.Clear();
1784 myVertexNodeFree.Clear();
1785 myNodeSections.Clear();
1786 myCuttingNode.Clear();
1787 mySectionBound.Clear();
1788 myLittleFace.Clear();
1791 //=======================================================================
1794 //=======================================================================
1796 void BRepBuilderAPI_Sewing::Add(const TopoDS_Shape& aShape)
1798 if (aShape.IsNull()) return;
1799 TopoDS_Shape oShape = myReShape->Apply(aShape);
1800 myOldShapes.Add(aShape,oShape);
1801 myNbShapes = myOldShapes.Extent();
1804 //=======================================================================
1805 //function : Perform
1807 //=======================================================================
1810 #include <OSD_Timer.hxx>
1813 void BRepBuilderAPI_Sewing::Perform(const Handle(Message_ProgressIndicator)& thePI)
1815 const Standard_Integer aNumberOfStages = myAnalysis + myCutting + mySewing + 2;
1816 Message_ProgressSentry aPS (thePI, "Sewing", 0, aNumberOfStages, 1);
1818 Standard_Real t_total = 0., t_analysis = 0., t_assembling = 0., t_cutting = 0., t_merging = 0.;
1819 OSD_Chronometer chr_total, chr_local;
1828 cout << "Begin face analysis..." << endl;
1832 FaceAnalysis (thePI);
1838 chr_local.Show(t_analysis);
1839 cout << "Face analysis finished after " << t_analysis << " s" << endl;
1843 if (myNbShapes || !myShape.IsNull())
1846 FindFreeBoundaries();
1848 if (myBoundFaces.Extent())
1852 cout << "Begin vertices assembling..." << endl;
1856 VerticesAssembling (thePI);
1862 chr_local.Show(t_assembling);
1863 cout << "Vertices assembling finished after " << t_assembling << " s" << endl;
1868 cout << "Begin cutting..." << endl;
1878 chr_local.Show(t_cutting);
1879 cout << "Cutting finished after " << t_cutting << " s" << endl;
1883 cout << "Begin merging..." << endl;
1887 Merging (Standard_True, thePI);
1893 chr_local.Show(t_merging);
1894 cout << "Merging finished after " << t_merging << " s" << endl;
1899 aPS.Next( 1, Handle(TCollection_HAsciiString)());
1901 aPS.Next( 1, Handle(TCollection_HAsciiString)());
1902 aPS.Next( 1, Handle(TCollection_HAsciiString)());
1911 cout << "Creating sewed shape..." << endl;
1913 // examine the multiple edges if any and process sameparameter for edges if necessary
1914 EdgeProcessing (thePI);
1920 mySewedShape.Nullify();
1923 if (mySameParameterMode && myFaceMode)
1924 SameParameterShape();
1927 mySewedShape.Nullify();
1931 cout << "Sewed shape created" << endl;
1935 // create edge informations for output
1936 CreateOutputInformations();
1939 mySewedShape.Nullify();
1945 chr_total.Show(t_total);
1946 cout << "Sewing finished!" << endl;
1947 cout << " analysis time : " << t_analysis << " s" << endl;
1948 cout << " assembling time : " << t_assembling << " s" << endl;
1949 cout << " cutting time : " << t_cutting << " s" << endl;
1950 cout << " merging time : " << t_merging << " s" << endl;
1951 cout << "Total time : " << t_total << " s" << endl;
1955 //=======================================================================
1956 //function : SewedShape
1957 //purpose : give the sewed shape
1958 // if a null shape, reasons:
1959 // -- no useable input shapes : all input shapes are degenerated
1960 // -- has multiple edges
1961 //=======================================================================
1963 const TopoDS_Shape& BRepBuilderAPI_Sewing::SewedShape() const
1965 return mySewedShape;
1968 //=======================================================================
1969 //function : NbFreeEdges
1971 //=======================================================================
1973 Standard_Integer BRepBuilderAPI_Sewing::NbFreeEdges() const
1975 return myFreeEdges.Extent();
1978 //=======================================================================
1979 //function : FreeEdge
1981 //=======================================================================
1983 const TopoDS_Edge& BRepBuilderAPI_Sewing::FreeEdge(const Standard_Integer index) const
1985 Standard_OutOfRange_Raise_if(index < 0 || index > NbFreeEdges(), "BRepBuilderAPI_Sewing::FreeEdge");
1986 return TopoDS::Edge(myFreeEdges(index));
1989 //=======================================================================
1990 //function : NbMultipleEdges
1992 //=======================================================================
1994 Standard_Integer BRepBuilderAPI_Sewing::NbMultipleEdges() const
1996 return myMultipleEdges.Extent();
1999 //=======================================================================
2000 //function : MultipleEdge
2002 //=======================================================================
2004 const TopoDS_Edge& BRepBuilderAPI_Sewing::MultipleEdge(const Standard_Integer index) const
2006 Standard_OutOfRange_Raise_if(index < 0 || index > NbMultipleEdges(), "BRepBuilderAPI_Sewing::MultipleEdge");
2007 return TopoDS::Edge(myMultipleEdges(index));
2010 //=======================================================================
2011 //function : NbContigousEdges
2013 //=======================================================================
2015 Standard_Integer BRepBuilderAPI_Sewing::NbContigousEdges() const
2017 return myContigousEdges.Extent();
2020 //=======================================================================
2021 //function : ContigousEdge
2023 //=======================================================================
2025 const TopoDS_Edge& BRepBuilderAPI_Sewing::ContigousEdge(const Standard_Integer index) const
2027 Standard_OutOfRange_Raise_if(index < 0 || index > NbContigousEdges(), "BRepBuilderAPI_Sewing::ContigousEdge");
2028 return TopoDS::Edge(myContigousEdges.FindKey(index));
2031 //=======================================================================
2032 //function : ContigousEdgeCouple
2034 //=======================================================================
2036 const TopTools_ListOfShape& BRepBuilderAPI_Sewing::ContigousEdgeCouple(const Standard_Integer index) const
2038 Standard_OutOfRange_Raise_if(index < 0 || index > NbContigousEdges(), "BRepBuilderAPI_Sewing::ContigousEdgeCouple");
2039 return myContigousEdges(index);
2042 //=======================================================================
2043 //function : IsSectionBound
2045 //=======================================================================
2047 Standard_Boolean BRepBuilderAPI_Sewing::IsSectionBound(const TopoDS_Edge& section) const
2049 if(myContigSecBound.IsBound(section)) {
2050 return Standard_True;
2053 return Standard_False;
2057 //=======================================================================
2058 //function : SectionToBoundary
2060 //=======================================================================
2062 const TopoDS_Edge& BRepBuilderAPI_Sewing::SectionToBoundary(const TopoDS_Edge& section) const
2064 Standard_NoSuchObject_Raise_if(!IsSectionBound(section), "BRepBuilderAPI_Sewing::SectionToBoundary");
2065 return TopoDS::Edge(myContigSecBound(section));
2067 //=======================================================================
2068 //function : NbDeletedFaces
2070 //=======================================================================
2071 Standard_Integer BRepBuilderAPI_Sewing::NbDeletedFaces() const
2073 return myLittleFace.Extent();
2076 //=======================================================================
2077 //function : DeletedFace
2079 //=======================================================================
2080 const TopoDS_Face& BRepBuilderAPI_Sewing::DeletedFace(const Standard_Integer index) const
2082 Standard_OutOfRange_Raise_if(index < 0 || index > NbDeletedFaces(), "BRepBuilderAPI_Sewing::DeletedFace");
2083 return TopoDS::Face(myLittleFace(index));
2086 //=======================================================================
2087 //function : NbDegeneratedShapes
2089 //=======================================================================
2091 Standard_Integer BRepBuilderAPI_Sewing::NbDegeneratedShapes() const
2093 return myDegenerated.Extent();
2096 //=======================================================================
2097 //function : DegeneratedShape
2099 //=======================================================================
2101 const TopoDS_Shape& BRepBuilderAPI_Sewing::DegeneratedShape(const Standard_Integer index) const
2103 Standard_OutOfRange_Raise_if(index < 0 || index > NbDegeneratedShapes(), "BRepBuilderAPI_Sewing::DegereratedShape");
2104 return myDegenerated(index);
2107 //=======================================================================
2108 //function : IsDegenerated
2110 //=======================================================================
2112 Standard_Boolean BRepBuilderAPI_Sewing::IsDegenerated(const TopoDS_Shape& aShape) const
2114 TopoDS_Shape NewShape = myReShape->Apply(aShape);
2116 if (aShape.ShapeType() == TopAbs_FACE)
2117 return NewShape.IsNull();
2118 if (NewShape.IsNull()) return Standard_False;
2120 if (NewShape.ShapeType() == TopAbs_EDGE)
2121 return BRep_Tool::Degenerated(TopoDS::Edge(NewShape));
2123 if (NewShape.ShapeType() == TopAbs_WIRE) {
2124 Standard_Boolean isDegenerated = Standard_True;
2125 for (TopoDS_Iterator aIt(NewShape); aIt.More() && isDegenerated; aIt.Next())
2126 isDegenerated = BRep_Tool::Degenerated(TopoDS::Edge(aIt.Value()));
2127 return isDegenerated;
2129 return Standard_False;
2132 //=======================================================================
2133 //function : IsModified
2135 //=======================================================================
2137 Standard_Boolean BRepBuilderAPI_Sewing::IsModified(const TopoDS_Shape& aShape) const
2139 TopoDS_Shape NewShape = aShape;
2140 if (myOldShapes.Contains(aShape))
2141 NewShape = myOldShapes.FindFromKey(aShape);
2142 if(!NewShape.IsSame(aShape)) return Standard_True;
2143 return Standard_False;
2146 //=======================================================================
2147 //function : Modified
2149 //=======================================================================
2151 const TopoDS_Shape& BRepBuilderAPI_Sewing::Modified(const TopoDS_Shape& aShape) const
2153 if (myOldShapes.Contains(aShape)) return myOldShapes.FindFromKey(aShape);
2154 //if (myOldFaces.Contains(aShape)) return myOldFaces.FindFromKey(aShape);
2158 //=======================================================================
2159 //function : IsModifiedSubShape
2161 //=======================================================================
2163 Standard_Boolean BRepBuilderAPI_Sewing::IsModifiedSubShape(const TopoDS_Shape& aShape) const
2165 TopoDS_Shape NewShape = myReShape->Apply(aShape);
2166 if(!NewShape.IsSame(aShape)) return Standard_True;
2167 return Standard_False;
2170 //=======================================================================
2171 //function : ModifiedSubShape
2173 //=======================================================================
2175 TopoDS_Shape BRepBuilderAPI_Sewing::ModifiedSubShape(const TopoDS_Shape& aShape) const
2177 return myReShape->Apply(aShape);
2180 //=======================================================================
2183 //=======================================================================
2185 void BRepBuilderAPI_Sewing::Dump() const
2187 Standard_Integer i, NbBounds = myBoundFaces.Extent(), NbSections = 0;
2188 TopTools_MapOfShape mapVertices, mapEdges;
2189 for (i = 1; i <= NbBounds; i++) {
2190 TopoDS_Shape bound = myBoundFaces.FindKey(i);
2191 if (myBoundSections.IsBound(bound)) NbSections += myBoundSections(bound).Extent();
2193 TopExp_Explorer aExp(myReShape->Apply(bound),TopAbs_EDGE);
2194 for (; aExp.More(); aExp.Next()) {
2195 TopoDS_Edge E = TopoDS::Edge(aExp.Current());
2197 TopoDS_Vertex V1, V2;
2198 TopExp::Vertices(E,V1,V2);
2199 mapVertices.Add(V1);
2200 mapVertices.Add(V2);
2203 cout << " " << endl;
2204 cout << " Informations " << endl;
2205 cout << " ===========================================================" << endl;
2206 cout << " " << endl;
2207 cout << " Number of input shapes : " << myOldShapes.Extent() << endl;
2208 cout << " Number of actual shapes : " << myNbShapes << endl;
2209 cout << " Number of Bounds : " << NbBounds << endl;
2210 cout << " Number of Sections : " << NbSections << endl;
2211 cout << " Number of Edges : " << mapEdges.Extent() << endl;
2212 cout << " Number of Vertices : " << myNbVertices << endl;
2213 cout << " Number of Nodes : " << mapVertices.Extent() << endl;
2214 cout << " Number of Free Edges : " << myFreeEdges.Extent() << endl;
2215 cout << " Number of Contigous Edges : " << myContigousEdges.Extent() << endl;
2216 cout << " Number of Multiple Edges : " << myMultipleEdges.Extent() << endl;
2217 cout << " Number of Degenerated Edges : " << myDegenerated.Extent() << endl;
2218 cout << " ===========================================================" << endl;
2219 cout << " " << endl;
2222 //=======================================================================
2223 //function : FaceAnalysis
2231 //=======================================================================
2233 void BRepBuilderAPI_Sewing::FaceAnalysis(const Handle(Message_ProgressIndicator)& thePI)
2235 if (!myShape.IsNull() && myOldShapes.IsEmpty()) {
2241 TopTools_MapOfShape SmallEdges;
2242 TopTools_DataMapOfShapeListOfShape GluedVertices;
2243 Standard_Integer i = 1;
2244 Message_ProgressSentry aPS (thePI, "Shape analysis", 0, myOldShapes.Extent(), 1);
2245 for (i = 1; i <= myOldShapes.Extent() && aPS.More(); i++, aPS.Next()) {
2246 for (TopExp_Explorer fexp(myOldShapes(i),TopAbs_FACE); fexp.More(); fexp.Next()) {
2248 // Retrieve current face
2249 TopoDS_Shape aTmpShape = fexp.Current(); //for porting
2250 TopoDS_Face face = TopoDS::Face(aTmpShape);
2251 Standard_Integer nbEdges = 0, nbSmall = 0;
2253 // Build replacing face
2254 aTmpShape = face.EmptyCopied().Oriented(TopAbs_FORWARD); //for porting
2255 TopoDS_Face nface = TopoDS::Face(aTmpShape);
2256 Standard_Boolean isFaceChanged = Standard_False;
2258 TopoDS_Iterator witer(face.Oriented(TopAbs_FORWARD));
2259 for (; witer.More(); witer.Next()) {
2261 // Retrieve current wire
2262 aTmpShape = witer.Value(); //for porting
2263 if( aTmpShape.ShapeType() != TopAbs_WIRE) continue;
2264 TopoDS_Wire wire = TopoDS::Wire(aTmpShape);
2266 // Build replacing wire
2267 aTmpShape = wire.EmptyCopied().Oriented(TopAbs_FORWARD);
2268 TopoDS_Wire nwire = TopoDS::Wire(aTmpShape);
2269 Standard_Boolean isWireChanged = Standard_False;
2271 TopoDS_Iterator eiter(wire.Oriented(TopAbs_FORWARD));
2272 for (; eiter.More(); eiter.Next()) {
2274 // Retrieve current edge
2275 aTmpShape = eiter.Value(); //for porting
2276 TopoDS_Edge edge = TopoDS::Edge(aTmpShape);
2279 // Process degenerated edge
2280 if (BRep_Tool::Degenerated(edge)) {
2281 B.Add(nwire,edge); // Old edge kept
2282 myDegenerated.Add(edge);
2287 Standard_Boolean isSmall = SmallEdges.Contains(edge);
2290 // Check for small edge
2291 Standard_Real first, last;
2292 Handle(Geom_Curve) c3d = BRep_Tool::Curve(edge,first,last);
2295 cout << "Warning: Possibly small edge can be sewed: No 3D curve" << endl;
2299 // Evaluate curve compactness
2300 const Standard_Integer npt = 5;
2301 gp_Pnt cp((c3d->Value(first).XYZ()+c3d->Value(last).XYZ())*0.5);
2302 Standard_Real dist, maxdist = 0.0;
2303 Standard_Real delta = (last - first)/(npt - 1);
2304 for (Standard_Integer idx = 0; idx < npt; idx++) {
2305 dist = cp.Distance(c3d->Value(first + idx*delta));
2306 if (maxdist < dist) maxdist = dist;
2308 isSmall = (2.*maxdist <= MinTolerance());
2310 GeomAdaptor_Curve cAdapt(c3d);
2311 Standard_Real length = GCPnts_AbscissaPoint::Length(cAdapt,first,last);
2312 isSmall = (length <= MinTolerance());
2314 catch (Standard_Failure) {
2316 cout << "Warning: Possibly small edge can be sewed: ";
2317 Standard_Failure::Caught()->Print(cout); cout << endl;
2324 // Store small edge in the map
2325 SmallEdges.Add(edge);
2327 TopoDS_Vertex v1, v2;
2328 TopExp::Vertices(edge,v1,v2);
2329 TopoDS_Shape nv1 = myReShape->Apply(v1), nv2 = myReShape->Apply(v2);
2331 // Store glued vertices
2332 if (!nv1.IsSame(v1)) {
2333 TopTools_ListOfShape& vlist1 = GluedVertices(nv1);
2334 // First vertex was already glued
2335 if (!nv2.IsSame(v2)) {
2336 // Merge lists of glued vertices
2337 if (!nv1.IsSame(nv2)) {
2338 TopTools_ListIteratorOfListOfShape liter(GluedVertices(nv2));
2339 for (; liter.More(); liter.Next()) {
2340 TopoDS_Shape v = liter.Value();
2341 myReShape->Replace(v,nv1.Oriented(v.Orientation()));
2344 GluedVertices.UnBind(nv2);
2348 // Add second vertex to the existing list
2350 myReShape->Replace(v2,nv1.Oriented(v2.Orientation()));
2353 else if (!nv2.IsSame(v2)) {
2354 // Add first vertex to the existing list
2355 GluedVertices(nv2).Append(v1);
2356 myReShape->Replace(v1,nv2.Oriented(v1.Orientation()));
2358 else if (!v1.IsSame(v2)) {
2359 // Record new glued vertices
2362 TopTools_ListOfShape vlist;
2365 GluedVertices.Bind(nv,vlist);
2366 myReShape->Replace(v1,nv.Oriented(v1.Orientation()));
2367 myReShape->Replace(v2,nv.Oriented(v2.Orientation()));
2372 // Replace small edge
2375 cout << "Warning: Small edge made degenerated by FaceAnalysis" << endl;
2378 // Create new degenerated edge
2379 aTmpShape = edge.Oriented(TopAbs_FORWARD);
2380 TopoDS_Edge fedge = TopoDS::Edge(aTmpShape);
2381 Standard_Real pfirst, plast;
2382 Handle(Geom2d_Curve) c2d = BRep_Tool::CurveOnSurface(fedge,face,pfirst,plast);
2383 if (!c2d.IsNull()) {
2386 B.UpdateEdge(nedge,c2d,face,Precision::Confusion());
2387 B.Range(nedge,pfirst,plast);
2388 B.Degenerated(nedge,Standard_True);
2389 TopoDS_Vertex v1, v2;
2390 TopExp::Vertices(fedge,v1,v2);
2391 B.Add(nedge,myReShape->Apply(v1).Oriented(v1.Orientation()));
2392 B.Add(nedge,myReShape->Apply(v2).Oriented(v2.Orientation()));
2393 B.Add(nwire,nedge.Oriented(edge.Orientation()));
2394 myDegenerated.Add(nedge);
2396 isWireChanged = Standard_True;
2398 else B.Add(nwire,edge); // Old edge kept
2401 // Record wire in the new face
2402 if (isWireChanged) {
2403 B.Add(nface,nwire.Oriented(wire.Orientation()));
2404 isFaceChanged = Standard_True;
2406 else B.Add(nface,wire);
2409 // Remove small face
2410 if (nbSmall == nbEdges) {
2412 cout << "Warning: Small face removed by FaceAnalysis" << endl;
2414 myLittleFace.Add(face);
2415 myReShape->Remove(face);
2417 else if (isFaceChanged) {
2419 myReShape->Replace(face,nface.Oriented(face.Orientation()));
2424 // Update glued vertices
2425 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape miter(GluedVertices);
2426 for (; miter.More(); miter.Next()) {
2427 TopoDS_Vertex vnew = TopoDS::Vertex(miter.Key());
2428 gp_XYZ coord(0.,0.,0.);
2429 Standard_Integer nbPoints = 0;
2430 const TopTools_ListOfShape& vlist = miter.Value();
2431 TopTools_ListIteratorOfListOfShape liter1(vlist);
2432 for (; liter1.More(); liter1.Next()) {
2433 coord += BRep_Tool::Pnt(TopoDS::Vertex(liter1.Value())).XYZ();
2437 gp_Pnt vp(coord / nbPoints);
2438 Standard_Real tol = 0.0, mtol = 0.0;
2439 TopTools_ListIteratorOfListOfShape liter2(vlist);
2440 for (; liter2.More(); liter2.Next()) {
2441 Standard_Real vtol = BRep_Tool::Tolerance(TopoDS::Vertex(liter2.Value()));
2442 if (mtol < vtol) mtol = vtol;
2443 vtol = vp.Distance(BRep_Tool::Pnt(TopoDS::Vertex(liter2.Value())));
2444 if (tol < vtol) tol = vtol;
2446 B.UpdateVertex(vnew,vp,tol+mtol);
2450 // Update input shapes
2451 for (i = 1; i <= myOldShapes.Extent(); i++)
2452 myOldShapes(i) = myReShape->Apply(myOldShapes(i));
2455 //=======================================================================
2456 //function : FindFreeBoundaries
2457 //purpose : Constructs :
2458 // myBoundFaces (bound = list of faces) - REFERENCE
2459 // myVertexNode (vertex = node)
2460 // myVertexNodeFree (floating vertex = node)
2462 //=======================================================================
2464 void BRepBuilderAPI_Sewing::FindFreeBoundaries()
2466 // Take into account the context shape if needed
2467 TopTools_IndexedMapOfShape NewShapes;
2468 if (!myShape.IsNull()) {
2469 if (myOldShapes.IsEmpty()) {
2474 TopoDS_Shape newShape = myReShape->Apply(myShape);
2475 if (!newShape.IsNull()) NewShapes.Add(newShape);
2478 // Create map Edge -> Faces
2479 TopTools_IndexedDataMapOfShapeListOfShape EdgeFaces;
2480 Standard_Integer i, nbShapes = myOldShapes.Extent();
2481 for (i = 1; i <= nbShapes; i++) {
2482 // Retrieve new shape
2483 TopoDS_Shape shape = myOldShapes(i);
2484 if (shape.IsNull()) continue;
2485 NewShapes.Add(shape);
2486 // Explore shape to find all boundaries
2487 for (TopExp_Explorer eExp(shape,TopAbs_EDGE); eExp.More(); eExp.Next()) {
2488 TopoDS_Shape edge = eExp.Current();
2489 if (!EdgeFaces.Contains(edge)) {
2490 TopTools_ListOfShape listFaces;
2491 EdgeFaces.Add(edge,listFaces);
2495 // Fill map Edge -> Faces
2496 nbShapes = NewShapes.Extent();
2497 TopTools_MapOfShape mapFaces;
2498 for (i = 1; i <= nbShapes; i++) {
2499 // Explore shape to find all faces
2500 TopExp_Explorer fExp(NewShapes.FindKey(i),TopAbs_FACE);
2501 for (; fExp.More(); fExp.Next()) {
2502 TopoDS_Shape face = fExp.Current();
2503 if(mapFaces.Contains(face)) continue;
2506 // Explore face to find all boundaries
2507 for (TopoDS_Iterator aIw(face); aIw.More(); aIw.Next()) {
2508 if(aIw.Value().ShapeType() != TopAbs_WIRE) continue;
2509 for (TopoDS_Iterator aIIe(aIw.Value()); aIIe.More(); aIIe.Next()) {
2511 TopoDS_Shape edge = aIIe.Value();
2513 if (EdgeFaces.Contains(edge)) {
2514 EdgeFaces.ChangeFromKey(edge).Append(face);
2515 //TopTools_ListOfShape& listFaces = EdgeFaces.ChangeFromKey(edge);
2516 //Standard_Boolean isContained = Standard_False;
2517 //TopTools_ListIteratorOfListOfShape itf(listFaces);
2518 //for (; itf.More() && !isContained; itf.Next())
2519 // isContained = face.IsSame(itf.Value());
2520 //if (!isContained) listFaces.Append(face);
2526 // Find free boundaries
2527 nbShapes = EdgeFaces.Extent();
2528 for (i = 1; i <= nbShapes; i++) {
2529 TopTools_ListOfShape& listFaces = EdgeFaces(i);
2530 Standard_Integer nbFaces = listFaces.Extent();
2531 TopoDS_Shape edge = EdgeFaces.FindKey(i);
2532 if(edge.Orientation() == TopAbs_INTERNAL)
2534 Standard_Boolean isSeam = Standard_False;
2536 TopoDS_Face face = TopoDS::Face(listFaces.First());
2537 isSeam = BRep_Tool::IsClosed(TopoDS::Edge(edge),face);
2539 ///Handle(Geom_Surface) surf = BRep_Tool::Surface(face);
2540 //isSeam = (IsUClosedSurface(surf) || IsVClosedSurface(surf));
2543 TopoDS_Shape anewEdge = edge.EmptyCopied();
2544 TopoDS_Iterator aItV(edge);
2545 for( ; aItV.More() ; aItV.Next())
2546 aB.Add(anewEdge,aItV.Value());
2550 Standard_Real first2d,last2d;
2551 Handle(Geom2d_Curve) c2dold =
2552 BRep_Tool::CurveOnSurface(TopoDS::Edge(edge),TopoDS::Face(listFaces.First()),first2d,last2d);
2554 Handle(Geom2d_Curve) c2d;
2556 B.UpdateEdge(TopoDS::Edge(anewEdge),c2d,c2d,TopoDS::Face(listFaces.First()),0);
2557 B.UpdateEdge(TopoDS::Edge(anewEdge),c2dold,TopoDS::Face(listFaces.First()),0);
2559 Standard_Real aFirst, aLast;
2560 BRep_Tool::Range(TopoDS::Edge(edge),aFirst, aLast);
2561 aB.Range(TopoDS::Edge(anewEdge),aFirst, aLast);
2562 aB.Range(TopoDS::Edge(anewEdge),TopoDS::Face(listFaces.First()),first2d,last2d);
2563 myReShape->Replace(edge,anewEdge);
2567 isSeam = Standard_False;
2570 Standard_Boolean isBoundFloat = (myFloatingEdgesMode && !nbFaces);
2571 Standard_Boolean isBound = (myFaceMode && ((myNonmanifold && nbFaces) || (nbFaces == 1 && !isSeam)));
2572 if (isBound || isBoundFloat) {
2573 // Ignore degenerated edge
2574 if (BRep_Tool::Degenerated(TopoDS::Edge(edge))) continue;
2575 // Ignore edge with internal vertices
2576 // Standard_Integer nbVtx = 0;
2577 // for (TopExp_Explorer vExp(edge,TopAbs_VERTEX); vExp.More(); vExp.Next()) nbVtx++;
2578 // if (nbVtx != 2) continue;
2579 // Add to BoundFaces
2580 TopTools_ListOfShape listFacesCopy;
2581 listFacesCopy.Append(listFaces);
2582 myBoundFaces.Add(edge,listFacesCopy);
2583 // Process edge vertices
2584 TopoDS_Vertex vFirst, vLast;
2585 TopExp::Vertices(TopoDS::Edge(edge), vFirst, vLast);
2586 if(vFirst.IsNull() || vLast.IsNull()) continue;
2587 if(vFirst.Orientation() == TopAbs_INTERNAL || vLast.Orientation() == TopAbs_INTERNAL)
2590 // Add to VertexNode
2591 if (!myVertexNode.Contains(vFirst))
2592 myVertexNode.Add(vFirst,vFirst);
2593 if (!myVertexNode.Contains(vLast))
2594 myVertexNode.Add(vLast,vLast);
2597 // Add to VertexNodeFree
2598 if (!myVertexNodeFree.Contains(vFirst))
2599 myVertexNodeFree.Add(vFirst,vFirst);
2600 if (!myVertexNodeFree.Contains(vLast))
2601 myVertexNodeFree.Add(vLast,vLast);
2607 //=======================================================================
2608 //function : VerticesAssembling
2609 //purpose : Modifies :
2610 // myVertexNode (nodes glued)
2611 // myVertexNodeFree (nodes glued)
2612 // myNodeSections (lists of sections merged for glued nodes)
2614 //=======================================================================
2616 static Standard_Boolean CreateNewNodes(const TopTools_IndexedDataMapOfShapeShape& NodeNearestNode,
2617 const TopTools_IndexedDataMapOfShapeListOfShape& NodeVertices,
2618 TopTools_IndexedDataMapOfShapeShape& aVertexNode,
2619 TopTools_DataMapOfShapeListOfShape& aNodeEdges)
2621 Standard_Integer i, nbNearest = NodeNearestNode.Extent();
2625 TopTools_DataMapOfShapeShape OldNodeNewNode;
2626 TopTools_DataMapOfShapeListOfShape NewNodeOldNodes;
2627 for (i = 1; i <= nbNearest; i++) {
2628 // Retrieve a pair of nodes to merge
2629 TopoDS_Shape oldnode1 = NodeNearestNode.FindKey(i);
2630 TopoDS_Shape oldnode2 = NodeNearestNode(i);
2631 // Second node should also be in the map
2632 if (!NodeNearestNode.Contains(oldnode2)) continue;
2633 // Get new node for old node #1
2634 if (OldNodeNewNode.IsBound(oldnode1)) {
2635 TopoDS_Shape newnode1 = OldNodeNewNode(oldnode1);
2636 if (OldNodeNewNode.IsBound(oldnode2)) {
2637 TopoDS_Shape newnode2 = OldNodeNewNode(oldnode2);
2638 if (!newnode1.IsSame(newnode2)) {
2639 // Change data for new node #2
2640 TopTools_ListOfShape& lnode1 = NewNodeOldNodes(newnode1);
2641 TopTools_ListIteratorOfListOfShape itn(NewNodeOldNodes(newnode2));
2642 for (; itn.More(); itn.Next()) {
2643 TopoDS_Shape node2 = itn.Value();
2644 lnode1.Append(node2);
2645 OldNodeNewNode(node2) = newnode1;
2647 NewNodeOldNodes.UnBind(newnode2);
2651 // Old node #2 is not bound - add to old node #1
2652 OldNodeNewNode.Bind(oldnode2,newnode1);
2653 NewNodeOldNodes(newnode1).Append(oldnode2);
2657 if (OldNodeNewNode.IsBound(oldnode2)) {
2658 // Old node #1 is not bound - add to old node #2
2659 TopoDS_Shape newnode2 = OldNodeNewNode(oldnode2);
2660 OldNodeNewNode.Bind(oldnode1,newnode2);
2661 NewNodeOldNodes(newnode2).Append(oldnode1);
2664 // Nodes are not bound - create new node
2665 TopoDS_Vertex newnode;
2666 B.MakeVertex(newnode);
2667 OldNodeNewNode.Bind(oldnode1,newnode);
2668 OldNodeNewNode.Bind(oldnode2,newnode);
2669 TopTools_ListOfShape lnodes;
2670 lnodes.Append(oldnode1);
2671 lnodes.Append(oldnode2);
2672 NewNodeOldNodes.Bind(newnode,lnodes);
2677 // Stop if no new nodes created
2678 if (!NewNodeOldNodes.Extent()) return Standard_False;
2680 TopTools_DataMapIteratorOfDataMapOfShapeListOfShape iter1(NewNodeOldNodes);
2681 for (; iter1.More(); iter1.Next()) {
2682 const TopoDS_Vertex& newnode = TopoDS::Vertex(iter1.Key());
2683 // Calculate new node center point
2684 gp_XYZ theCoordinates(0.,0.,0.);
2685 TopTools_ListOfShape lvert; // Accumulate node vertices
2686 TopTools_MapOfShape medge;
2687 TopTools_ListOfShape ledge; // Accumulate node edges
2688 // Iterate on old nodes
2689 TopTools_ListIteratorOfListOfShape itn(iter1.Value());
2690 for (; itn.More(); itn.Next()) {
2691 const TopoDS_Shape& oldnode = itn.Value();
2692 // Iterate on node vertices
2693 TopTools_ListIteratorOfListOfShape itv(NodeVertices.FindFromKey(oldnode));
2694 for (; itv.More(); itv.Next()) {
2695 TopoDS_Vertex vertex = TopoDS::Vertex(itv.Value());
2696 // Change node for vertex
2697 aVertexNode.ChangeFromKey(vertex) = newnode;
2698 // Accumulate coordinates
2699 theCoordinates += BRep_Tool::Pnt(vertex).XYZ();
2700 lvert.Append(vertex);
2702 // Iterate on node edges
2703 const TopTools_ListOfShape& edges = aNodeEdges(oldnode);
2704 TopTools_ListIteratorOfListOfShape ite(edges);
2705 for (; ite.More(); ite.Next()) {
2706 TopoDS_Shape edge = ite.Value();
2707 if (!medge.Contains(edge)) { medge.Add(edge); ledge.Append(edge); }
2709 // Unbind old node edges
2710 aNodeEdges.UnBind(oldnode);
2712 // Bind new node edges
2713 aNodeEdges.Bind(newnode,ledge);
2714 gp_Pnt center(theCoordinates / lvert.Extent());
2715 // Calculate new node tolerance
2716 Standard_Real toler = 0.0;
2717 TopTools_ListIteratorOfListOfShape itv(lvert);
2718 for (; itv.More(); itv.Next()) {
2719 const TopoDS_Vertex& vertex = TopoDS::Vertex(itv.Value());
2720 Standard_Real t = center.Distance(BRep_Tool::Pnt(vertex)) + BRep_Tool::Tolerance(vertex);
2721 if (toler < t) toler = t;
2723 // Update new node parameters
2724 B.UpdateVertex(newnode,center,toler);
2727 return Standard_True;
2730 static Standard_Integer IsMergedVertices(const TopoDS_Shape& face1,
2731 const TopoDS_Shape& e1, const TopoDS_Shape& e2,
2732 const TopoDS_Shape& vtx1, const TopoDS_Shape& vtx2)
2734 //Case of floating edges
2736 return (!IsClosedShape(e1,vtx1,vtx2));
2738 // Find wires containing given edges
2739 TopoDS_Shape wire1, wire2;
2740 TopExp_Explorer itw(face1,TopAbs_WIRE);
2741 for (; itw.More() && (wire1.IsNull() || wire2.IsNull()); itw.Next()) {
2742 TopoDS_Iterator ite(itw.Current(),Standard_False);
2743 for (; ite.More() && (wire1.IsNull() || wire2.IsNull()); ite.Next()) {
2744 if (wire1.IsNull() && e1.IsSame(ite.Value())) wire1 = itw.Current();
2745 if (wire2.IsNull() && e2.IsSame(ite.Value())) wire2 = itw.Current();
2748 Standard_Integer Status = 0;
2749 if (!wire1.IsNull() && !wire2.IsNull()) {
2750 if (wire1.IsSame(wire2)) {
2751 for (TopoDS_Iterator aIte(wire1,Standard_False); aIte.More(); aIte.Next()) {
2752 TopoDS_Vertex ve1,ve2;
2753 TopExp::Vertices(TopoDS::Edge(aIte.Value()),ve1,ve2);
2754 if ((ve1.IsSame(vtx1) && ve2.IsSame(vtx2)) ||
2755 (ve2.IsSame(vtx1) && ve1.IsSame(vtx2)))
2756 return (IsClosedShape(aIte.Value(),vtx1,vtx2)? 0 : 1);
2758 if (IsClosedShape(wire1,vtx1,vtx2)) {
2759 TopoDS_Vertex V1, V2;
2760 TopExp::Vertices(TopoDS::Wire(wire1),V1,V2);
2761 Standard_Boolean isEndVertex = ((V1.IsSame(vtx1) && V2.IsSame(vtx2)) ||
2762 (V2.IsSame(vtx1) && V1.IsSame(vtx2)));
2763 if (!isEndVertex) Status = 1;
2772 static Standard_Boolean GlueVertices(TopTools_IndexedDataMapOfShapeShape& aVertexNode,
2773 TopTools_DataMapOfShapeListOfShape& aNodeEdges,
2774 const TopTools_IndexedDataMapOfShapeListOfShape& aBoundFaces,
2775 const Standard_Real Tolerance,
2776 const Handle(Message_ProgressIndicator)& theProgress)
2778 Standard_Integer i, nbVertices = aVertexNode.Extent();
2779 // Create map of node -> vertices
2780 TopTools_IndexedDataMapOfShapeListOfShape NodeVertices;
2781 BRepBuilderAPI_CellFilter aFilter (BRepBuilderAPI_VertexInspector::Dimension, Tolerance);
2782 BRepBuilderAPI_VertexInspector anInspector (Tolerance);
2783 for (i = 1; i <= nbVertices; i++) {
2784 TopoDS_Shape vertex = aVertexNode.FindKey(i);
2785 TopoDS_Vertex node = TopoDS::Vertex(aVertexNode(i));
2786 if (NodeVertices.Contains(node)) {
2787 NodeVertices.ChangeFromKey(node).Append(vertex);
2790 TopTools_ListOfShape vlist;
2791 vlist.Append(vertex);
2792 NodeVertices.Add(node,vlist);
2793 gp_Pnt aPnt = BRep_Tool::Pnt (TopoDS::Vertex (node));
2794 aFilter.Add (NodeVertices.FindIndex (node), aPnt.XYZ());
2795 anInspector.Add (aPnt.XYZ());
2798 Standard_Integer nbNodes = NodeVertices.Extent();
2800 cout << "Glueing " << nbNodes << " nodes..." << endl;
2802 // Merge nearest nodes
2803 TopTools_IndexedDataMapOfShapeShape NodeNearestNode;
2804 Message_ProgressSentry aPS (theProgress, "Glueing nodes", 0, nbNodes, 1, Standard_True);
2805 for (i = 1; i <= nbNodes && aPS.More(); i++, aPS.Next()) {
2806 TopoDS_Vertex node1 = TopoDS::Vertex(NodeVertices.FindKey(i));
2808 gp_Pnt pt1 = BRep_Tool::Pnt (node1);
2809 anInspector.SetCurrent (pt1.XYZ());
2810 gp_XYZ aPntMin = anInspector.Shift (pt1.XYZ(), -Tolerance);
2811 gp_XYZ aPntMax = anInspector.Shift (pt1.XYZ(), Tolerance);
2812 aFilter.Inspect (aPntMin, aPntMax, anInspector);
2813 if (anInspector.ResInd().IsEmpty()) continue;
2814 // Retrieve list of edges for the first node
2815 const TopTools_ListOfShape& ledges1 = aNodeEdges(node1);
2816 // Explore list of near nodes and fill the sequence of glued nodes
2817 TopTools_SequenceOfShape SeqNodes;
2818 TopTools_ListOfShape listNodesSameEdge;
2819 //gp_Pnt pt1 = BRep_Tool::Pnt(node1);
2820 TColStd_ListIteratorOfListOfInteger iter1(anInspector.ResInd());
2821 for (; iter1.More(); iter1.Next()) {
2822 TopoDS_Vertex node2 = TopoDS::Vertex(NodeVertices.FindKey(iter1.Value()));
2823 if (node1 == node2) continue;
2824 // Retrieve list of edges for the second node
2825 const TopTools_ListOfShape& ledges2 = aNodeEdges(node2);
2826 // Check merging condition for the pair of nodes
2827 Standard_Integer Status = 0, isSameEdge = Standard_False;
2828 // Explore edges of the first node
2829 TopTools_ListIteratorOfListOfShape Ie1(ledges1);
2830 for (; Ie1.More() && !Status && !isSameEdge; Ie1.Next()) {
2831 const TopoDS_Shape& e1 = Ie1.Value();
2832 // Obtain real vertex from edge
2833 TopoDS_Shape v1 = node1;
2834 { //szv: Use brackets to destroy local variables
2835 TopoDS_Vertex ov1, ov2;
2836 TopExp::Vertices(TopoDS::Edge(e1),ov1,ov2);
2837 if (aVertexNode.Contains(ov1)) {
2838 if (node1.IsSame(aVertexNode.FindFromKey(ov1))) v1 = ov1;
2840 if (aVertexNode.Contains(ov2)) {
2841 if (node1.IsSame(aVertexNode.FindFromKey(ov2))) v1 = ov2;
2844 // Create map of faces for e1
2845 TopTools_MapOfShape Faces1;
2846 const TopTools_ListOfShape& lfac1 = aBoundFaces.FindFromKey(e1);
2847 if (lfac1.Extent()) {
2848 TopTools_ListIteratorOfListOfShape itf(lfac1);
2849 for (; itf.More(); itf.Next())
2850 if (!itf.Value().IsNull())
2851 Faces1.Add(itf.Value());
2853 // Explore edges of the second node
2854 TopTools_ListIteratorOfListOfShape Ie2(ledges2);
2855 for (; Ie2.More() && !Status && !isSameEdge; Ie2.Next()) {
2856 const TopoDS_Shape& e2 = Ie2.Value();
2857 // Obtain real vertex from edge
2858 TopoDS_Shape v2 = node2;
2859 { //szv: Use brackets to destroy local variables
2860 TopoDS_Vertex ov1, ov2;
2861 TopExp::Vertices(TopoDS::Edge(e2),ov1,ov2);
2862 if (aVertexNode.Contains(ov1)) {
2863 if (node2.IsSame(aVertexNode.FindFromKey(ov1))) v2 = ov1;
2865 if (aVertexNode.Contains(ov2)) {
2866 if (node2.IsSame(aVertexNode.FindFromKey(ov2))) v2 = ov2;
2869 // Explore faces for e2
2870 const TopTools_ListOfShape& lfac2 = aBoundFaces.FindFromKey(e2);
2871 if (lfac2.Extent()) {
2872 TopTools_ListIteratorOfListOfShape itf(lfac2);
2873 for (; itf.More() && !Status && !isSameEdge; itf.Next()) {
2874 // Check merging conditions for the same face
2875 if (Faces1.Contains(itf.Value())) {
2876 Standard_Integer stat = IsMergedVertices(itf.Value(),e1,e2,v1,v2);
2877 if (stat == 1) isSameEdge = Standard_True;
2882 else if (Faces1.IsEmpty() && e1 == e2) {
2883 Standard_Integer stat = IsMergedVertices(TopoDS_Face(),e1,e1,v1,v2);
2884 if (stat == 1) isSameEdge = Standard_True;
2890 if (Status) continue;
2891 if (isSameEdge) listNodesSameEdge.Append(node2);
2892 // Append near node to the sequence
2893 gp_Pnt pt2 = BRep_Tool::Pnt(node2);
2894 Standard_Real dist = pt1.Distance(pt2);
2895 if (dist < Tolerance) {
2896 Standard_Boolean isIns = Standard_False;
2897 for (Standard_Integer kk = 1; kk <= SeqNodes.Length() && !isIns; kk++) {
2898 gp_Pnt pt = BRep_Tool::Pnt(TopoDS::Vertex(SeqNodes.Value(kk)));
2899 if (dist < pt1.Distance(pt)) {
2900 SeqNodes.InsertBefore(kk,node2);
2901 isIns = Standard_True;
2904 if (!isIns) SeqNodes.Append(node2);
2907 if (SeqNodes.Length()) {
2908 // Remove nodes near to some other from the same edge
2909 if (listNodesSameEdge.Extent()) {
2910 TopTools_ListIteratorOfListOfShape lInt(listNodesSameEdge);
2911 for (; lInt.More(); lInt.Next()) {
2912 const TopoDS_Vertex& n2 = TopoDS::Vertex(lInt.Value());
2913 gp_Pnt p2 = BRep_Tool::Pnt(n2);
2914 for (Standard_Integer k = 1; k <= SeqNodes.Length(); ) {
2915 const TopoDS_Vertex& n1 = TopoDS::Vertex(SeqNodes.Value(k));
2917 gp_Pnt p1 = BRep_Tool::Pnt(n1);
2918 if (p2.Distance(p1) >= pt1.Distance(p1)) { k++; continue; }
2924 // Bind nearest node if at least one exists
2925 if (SeqNodes.Length())
2926 NodeNearestNode.Add(node1,SeqNodes.First());
2928 anInspector.ClearResList();
2931 // Create new nodes for chained nearest nodes
2932 if (NodeNearestNode.IsEmpty()) return Standard_False;
2934 return CreateNewNodes(NodeNearestNode,NodeVertices,aVertexNode,aNodeEdges);
2937 void BRepBuilderAPI_Sewing::VerticesAssembling(const Handle(Message_ProgressIndicator)& thePI)
2939 Standard_Integer nbVert = myVertexNode.Extent();
2940 Standard_Integer nbVertFree = myVertexNodeFree.Extent();
2941 Message_ProgressSentry aPS (thePI, "Vertices assembling", 0, 2, 1);
2942 if (nbVert || nbVertFree) {
2943 // Fill map node -> sections
2945 for (i = 1; i <= myBoundFaces.Extent(); i++) {
2946 TopoDS_Shape bound = myBoundFaces.FindKey(i);
2947 for (TopoDS_Iterator itv(bound,Standard_False); itv.More(); itv.Next()) {
2948 TopoDS_Shape node = itv.Value();
2949 if (myNodeSections.IsBound(node))
2950 myNodeSections(node).Append(bound);
2952 TopTools_ListOfShape lbnd;
2954 myNodeSections.Bind(node,lbnd);
2961 cout << "Assemble " << nbVert << " vertices on faces..." << endl;
2963 while (GlueVertices(myVertexNode,myNodeSections,myBoundFaces,myTolerance, thePI));
2970 cout << "Assemble " << nbVertFree << " vertices on floating edges..." << endl;
2972 while (GlueVertices(myVertexNodeFree,myNodeSections,myBoundFaces,myTolerance, thePI));
2977 //=======================================================================
2978 //function : replaceNMVertices
2979 //purpose : internal use (static)
2980 //=======================================================================
2981 static void replaceNMVertices(const TopoDS_Edge& theEdge,
2982 const TopoDS_Vertex& theV1,
2983 const TopoDS_Vertex& theV2,
2984 const Handle(BRepTools_ReShape)& theReShape)
2986 //To keep NM vertices on edge
2987 TopTools_SequenceOfShape aSeqNMVert;
2988 TColStd_SequenceOfReal aSeqNMPars;
2989 Standard_Boolean hasNMVert = findNMVertices(theEdge,aSeqNMVert,aSeqNMPars);
2992 Standard_Real first, last;
2993 BRep_Tool::Range(theEdge, first, last);
2994 TopLoc_Location aLoc;
2995 Handle(Geom_Curve) c3d = BRep_Tool::Curve(theEdge,aLoc,first, last);
2998 TopTools_SequenceOfShape aEdVert;
2999 TColStd_SequenceOfReal aEdParams;
3000 Standard_Integer i =1, nb = aSeqNMPars.Length();
3002 for( ; i <= nb;i++) {
3003 Standard_Real apar = aSeqNMPars.Value(i);
3004 if(fabs(apar - first) <= Precision::PConfusion()) {
3005 theReShape->Replace(aSeqNMVert.Value(i),theV1);
3008 if(fabs(apar - last) <= Precision::PConfusion()) {
3009 theReShape->Replace(aSeqNMVert.Value(i),theV2);
3012 TopoDS_Shape aV = aSeqNMVert.Value(i);
3013 Standard_Integer j =1;
3014 for( ; j <= aEdParams.Length();j++) {
3015 Standard_Real apar2 = aEdParams.Value(j);
3016 if(fabs(apar - apar2) <= Precision::PConfusion()) {
3017 theReShape->Replace(aV,aEdVert.Value(j));
3020 else if(apar < apar2) {
3021 TopoDS_Shape anewV = aV.EmptyCopied();
3022 aEdVert.InsertBefore(j,anewV);
3023 aEdParams.InsertBefore(j,apar);
3024 BRep_ListOfPointRepresentation& alistrep =
3025 (*((Handle(BRep_TVertex)*)&anewV.TShape()))->ChangePoints();
3026 Handle(BRep_PointOnCurve) aPRep = new BRep_PointOnCurve(apar,c3d,aLoc);
3027 alistrep.Append(aPRep);
3028 theReShape->Replace(aV,anewV);
3032 if (j > aEdParams.Length()) {
3033 TopoDS_Shape anewV = aV.EmptyCopied();
3034 aEdVert.Append(anewV);
3035 aEdParams.Append(apar);
3036 BRep_ListOfPointRepresentation& alistrep =
3037 (*((Handle(BRep_TVertex)*) &anewV.TShape()))->ChangePoints();
3038 Handle(BRep_PointOnCurve) aPRep = new BRep_PointOnCurve(apar,c3d,aLoc);
3039 alistrep.Append(aPRep);
3040 theReShape->Replace(aV,anewV);
3044 Standard_Integer newnb = aEdParams.Length();
3047 TopoDS_Shape anewEdge = theEdge.EmptyCopied();
3048 TopAbs_Orientation anOri = theEdge.Orientation();
3049 anewEdge.Orientation(TopAbs_FORWARD);
3051 aB.Add(anewEdge,theV1);
3052 aB.Add(anewEdge,theV2);
3054 for( i =1; i <= aEdVert.Length();i++)
3055 aB.Add(anewEdge,aEdVert.Value(i));
3056 anewEdge.Orientation(anOri);
3057 theReShape->Replace(theEdge,anewEdge);
3062 //=======================================================================
3063 //function : ReplaceEdge
3064 //purpose : internal use (static)
3065 //=======================================================================
3067 static void ReplaceEdge(const TopoDS_Shape& oldEdge,
3068 const TopoDS_Shape& theNewShape,
3069 const Handle(BRepTools_ReShape)& aReShape)
3071 TopoDS_Shape oldShape = aReShape->Apply(oldEdge);
3072 TopoDS_Shape newShape = aReShape->Apply(theNewShape);
3073 if (oldShape.IsSame(newShape)|| aReShape->IsRecorded(newShape)) return;
3076 aReShape->Replace(oldShape,newShape);
3077 TopoDS_Vertex V1old,V2old,V1new,V2new;
3078 TopExp::Vertices(TopoDS::Edge(oldShape),V1old,V2old);
3079 TopAbs_Orientation Orold = oldShape.Orientation();
3080 TopAbs_Orientation Ornew = Orold;
3081 if (newShape.ShapeType() == TopAbs_EDGE) {
3082 TopoDS_Edge aEn = TopoDS::Edge(newShape);
3083 TopExp::Vertices(aEn,V1new,V2new);
3084 Ornew = aEn.Orientation();
3085 replaceNMVertices(aEn,V1new,V2new,aReShape);
3087 else if (newShape.ShapeType() == TopAbs_WIRE) {
3088 for (TopExp_Explorer aex(newShape,TopAbs_EDGE); aex.More(); aex.Next()) {
3089 TopoDS_Edge ed = TopoDS::Edge(aex.Current());
3090 Ornew = ed.Orientation();
3091 TopoDS_Vertex aV1,aV2;
3092 TopExp::Vertices(ed,aV1,aV2);
3093 replaceNMVertices(ed,aV1,aV2,aReShape);
3100 V1new.Orientation(V1old.Orientation());
3101 V2new.Orientation(V2old.Orientation());
3102 if (V1old.IsSame(V2old) && !V1old.IsSame(V1new)&& !aReShape->IsRecorded(V1new)) {
3103 aReShape->Replace(V1old,V1new);
3106 if (Orold == Ornew) {
3107 V1new.Orientation(V1old.Orientation());
3108 V2new.Orientation(V2old.Orientation());
3109 if (!V1old.IsSame(V1new) && !V1old.IsSame(V2new)&& !aReShape->IsRecorded(V1new))
3110 aReShape->Replace(V1old,V1new);
3111 if (!V2old.IsSame(V2new) && !V2old.IsSame(V1new)&& !aReShape->IsRecorded(V2new))
3112 aReShape->Replace(V2old,V2new);
3115 V1new.Orientation(V2old.Orientation());
3116 V2new.Orientation(V1old.Orientation());
3117 if (!V1old.IsSame(V2new) && !V1old.IsSame(V1new)&& !aReShape->IsRecorded(V2new))
3118 aReShape->Replace(V1old,V2new);
3119 if (!V2old.IsSame(V2new) && !V2old.IsSame(V1new)&& !aReShape->IsRecorded(V1new))
3120 aReShape->Replace(V2old,V1new);
3124 //=======================================================================
3125 //function : Merging
3126 //purpose : Modifies :
3129 //=======================================================================
3131 void BRepBuilderAPI_Sewing::Merging(const Standard_Boolean /* firstTime */,
3132 const Handle(Message_ProgressIndicator)& thePI)
3135 // TopTools_MapOfShape MergedEdges;
3136 Message_ProgressSentry aPS (thePI, "Merging bounds", 0, myBoundFaces.Extent(), 1);
3137 for (Standard_Integer i = 1; i <= myBoundFaces.Extent() && aPS.More(); i++, aPS.Next()) {
3139 TopoDS_Shape bound = myBoundFaces.FindKey(i);
3141 // If bound was already merged - continue
3142 if (myMergedEdges.Contains(bound)) continue;
3144 if (!myBoundFaces(i).Extent()) {
3145 // Merge free edge - only vertices
3146 TopoDS_Vertex no1, no2;
3147 TopExp::Vertices(TopoDS::Edge(bound),no1,no2);
3148 TopoDS_Shape nno1 = no1, nno2 = no2;
3149 if (myVertexNodeFree.Contains(no1))
3150 nno1 = myVertexNodeFree.FindFromKey(no1);
3151 if (myVertexNodeFree.Contains(no2))
3152 nno2 = myVertexNodeFree.FindFromKey(no2);
3153 if (!no1.IsSame(nno1)) {
3154 nno1.Orientation(no1.Orientation());
3155 myReShape->Replace(no1,nno1);
3157 if (!no2.IsSame(nno2)) {
3158 nno2.Orientation(no2.Orientation());
3159 myReShape->Replace(no2,nno2);
3161 myMergedEdges.Add(bound);
3165 // Check for previous splitting, build replacing wire
3166 TopoDS_Wire BoundWire;
3167 Standard_Boolean isPrevSplit = Standard_False;
3168 Standard_Boolean hasCuttingSections = myBoundSections.IsBound(bound);
3169 if (hasCuttingSections) {
3170 B.MakeWire(BoundWire);
3171 BoundWire.Orientation(bound.Orientation());
3172 // Iterate on cutting sections
3173 TopTools_ListIteratorOfListOfShape its(myBoundSections(bound));
3174 for (; its.More(); its.Next()) {
3175 TopoDS_Shape section = its.Value();
3176 B.Add(BoundWire,section);
3177 if (myMergedEdges.Contains(section)) isPrevSplit = Standard_True;
3182 TopTools_DataMapOfShapeShape MergedWithBound;
3184 // Obtain sequence of edges merged with bound
3185 TopTools_SequenceOfShape seqMergedWithBound;
3186 TColStd_SequenceOfInteger seqMergedWithBoundOri;
3187 if (MergedNearestEdges(bound,seqMergedWithBound,seqMergedWithBoundOri)) {
3188 // Store bound in the map
3189 MergedWithBound.Bind(bound,bound);
3190 // Iterate on edges merged with bound
3191 Standard_Integer ii = 1;
3192 while (ii <= seqMergedWithBound.Length()) {
3193 TopoDS_Shape iedge = seqMergedWithBound.Value(ii);
3194 // Remove edge if recorded as merged
3195 Standard_Boolean isRejected = (myMergedEdges.Contains(iedge) ||
3196 MergedWithBound.IsBound(iedge));
3198 if (myBoundSections.IsBound(iedge)) {
3199 // Edge is splitted - check sections
3200 TopTools_ListIteratorOfListOfShape lit(myBoundSections(iedge));
3201 for (; lit.More() && !isRejected; lit.Next()) {
3202 const TopoDS_Shape& sec = lit.Value();
3203 // Remove edge (bound) if at least one of its sections already merged
3204 isRejected = (myMergedEdges.Contains(sec) || MergedWithBound.IsBound(sec));
3208 if (mySectionBound.IsBound(iedge)) {
3209 // Edge is a section - check bound
3210 const TopoDS_Shape& bnd = mySectionBound(iedge);
3211 // Remove edge (section) if its bound already merged
3212 isRejected = (myMergedEdges.Contains(bnd) || MergedWithBound.IsBound(bnd));
3216 // To the next merged edge
3218 // Remove rejected edge
3219 seqMergedWithBound.Remove(ii);
3220 seqMergedWithBoundOri.Remove(ii);
3223 // Process accepted edge
3224 MergedWithBound.Bind(iedge,iedge);
3228 Standard_Integer nbMerged = seqMergedWithBound.Length();
3230 // Create same parameter edge
3231 TopTools_MapOfShape ActuallyMerged;
3232 TopoDS_Edge MergedEdge = SameParameterEdge(bound,seqMergedWithBound,
3233 seqMergedWithBoundOri,
3234 ActuallyMerged,myReShape);
3235 Standard_Boolean isForward = Standard_False;
3236 if (!MergedEdge.IsNull()) isForward = (MergedEdge.Orientation() == TopAbs_FORWARD);
3237 // Process actually merged edges
3238 Standard_Integer nbActuallyMerged = 0;
3239 for (ii = 1; ii <= nbMerged; ii++) {
3240 TopoDS_Shape iedge = seqMergedWithBound(ii);
3241 if (ActuallyMerged.Contains(iedge)) {
3243 // Record merged edge in the map
3244 TopAbs_Orientation orient = iedge.Orientation();
3245 if (!isForward) orient = TopAbs::Reverse(orient);
3246 if (!seqMergedWithBoundOri(ii)) orient = TopAbs::Reverse(orient);
3247 MergedWithBound.ChangeFind(iedge) = MergedEdge.Oriented(orient);
3249 else MergedWithBound.UnBind(iedge);
3251 if (nbActuallyMerged) {
3252 // Record merged bound in the map
3253 TopAbs_Orientation orient = bound.Orientation();
3254 if (!isForward) orient = TopAbs::Reverse(orient);
3255 MergedWithBound.ChangeFind(bound) = MergedEdge.Oriented(orient);
3257 nbMerged = nbActuallyMerged;
3259 // Remove bound from the map if not finally merged
3260 if (!nbMerged) MergedWithBound.UnBind(bound);
3263 Standard_Boolean isMerged = MergedWithBound.Extent();
3265 // Merge with cutting sections
3266 Handle(BRepTools_ReShape) SectionsReShape = new BRepTools_ReShape;
3267 TopTools_DataMapOfShapeShape MergedWithSections;
3268 if (hasCuttingSections) {
3269 // Iterate on cutting sections
3270 TopTools_ListIteratorOfListOfShape its(myBoundSections(bound));
3271 for (; its.More(); its.Next()) {
3272 // Retrieve cutting section
3273 TopoDS_Shape section = its.Value();
3274 // Skip section if already merged
3275 if (myMergedEdges.Contains(section)) continue;
3276 // Merge cutting section
3277 TopTools_SequenceOfShape seqMergedWithSection;
3278 TColStd_SequenceOfInteger seqMergedWithSectionOri;
3279 if (MergedNearestEdges(section,seqMergedWithSection,seqMergedWithSectionOri)) {
3280 // Store section in the map
3281 MergedWithSections.Bind(section,section);
3282 // Iterate on edges merged with section
3283 Standard_Integer ii = 1;
3284 while (ii <= seqMergedWithSection.Length()) {
3285 TopoDS_Shape iedge = seqMergedWithSection.Value(ii);
3286 // Remove edge if recorded as merged
3287 Standard_Boolean isRejected = (myMergedEdges.Contains(iedge) || MergedWithSections.IsBound(iedge));
3289 if (myBoundSections.IsBound(iedge)) {
3290 // Edge is splitted - check sections
3291 TopTools_ListIteratorOfListOfShape lit(myBoundSections(iedge));
3292 for (; lit.More() && !isRejected; lit.Next()) {
3293 const TopoDS_Shape& sec = lit.Value();
3294 // Remove edge (bound) if at least one of its sections already merged
3295 isRejected = (myMergedEdges.Contains(sec) || MergedWithSections.IsBound(sec));
3299 if (mySectionBound.IsBound(iedge)) {
3300 // Edge is a section - check bound
3301 const TopoDS_Shape& bnd = mySectionBound(iedge);
3302 // Remove edge (section) if its bound already merged
3303 isRejected = (myMergedEdges.Contains(bnd) || MergedWithSections.IsBound(bnd));
3307 // To the next merged edge
3309 // Remove rejected edge
3310 seqMergedWithSection.Remove(ii);
3311 seqMergedWithSectionOri.Remove(ii);
3314 // Process accepted edge
3315 MergedWithSections.Bind(iedge,iedge);
3319 Standard_Integer nbMerged = seqMergedWithSection.Length();
3321 // Create same parameter edge
3322 TopTools_MapOfShape ActuallyMerged;
3323 TopoDS_Edge MergedEdge = SameParameterEdge(section,seqMergedWithSection,
3324 seqMergedWithSectionOri,
3325 ActuallyMerged,SectionsReShape);
3326 Standard_Boolean isForward = Standard_False;
3327 if (!MergedEdge.IsNull()) isForward = (MergedEdge.Orientation() == TopAbs_FORWARD);
3328 // Process actually merged edges
3329 Standard_Integer nbActuallyMerged = 0;
3330 for (ii = 1; ii <= nbMerged; ii++) {
3331 TopoDS_Shape iedge = seqMergedWithSection(ii);
3332 if (ActuallyMerged.Contains(iedge)) {
3334 // Record merged edge in the map
3335 TopAbs_Orientation orient = iedge.Orientation();
3336 if (!isForward) orient = TopAbs::Reverse(orient);
3337 if (!seqMergedWithSectionOri(ii)) orient = TopAbs::Reverse(orient);
3338 TopoDS_Shape oedge = MergedEdge.Oriented(orient);
3339 MergedWithSections.ChangeFind(iedge) = oedge;
3340 ReplaceEdge(myReShape->Apply(iedge),oedge,SectionsReShape);
3342 else MergedWithSections.UnBind(iedge);
3344 if (nbActuallyMerged) {
3345 // Record merged section in the map
3346 TopAbs_Orientation orient = section.Orientation();
3347 if (!isForward) orient = TopAbs::Reverse(orient);
3348 TopoDS_Shape oedge = MergedEdge.Oriented(orient);
3349 MergedWithSections.ChangeFind(section) = oedge;
3350 ReplaceEdge(myReShape->Apply(section),oedge,SectionsReShape);
3352 nbMerged = nbActuallyMerged;
3354 // Remove section from the map if not finally merged
3355 if (!nbMerged) MergedWithSections.UnBind(section);
3357 else if (isMerged) {
3358 // Reject merging of sections
3359 MergedWithSections.Clear();
3364 Standard_Boolean isMergedSplit = MergedWithSections.Extent();
3366 if (!isMerged && !isMergedSplit) {
3367 // Nothing was merged in this iteration
3369 // Replace previously splitted bound
3370 myReShape->Replace(myReShape->Apply(bound),myReShape->Apply(BoundWire));
3372 // else if (hasCuttingSections) {
3373 // myBoundSections.UnBind(bound); //szv: are you sure ???
3378 // Set splitting flag
3379 Standard_Boolean isSplitted = ((!isMerged && isMergedSplit) || isPrevSplit);
3381 // Choose between bound and sections merging
3382 if (isMerged && isMergedSplit && !isPrevSplit) {
3383 // Fill map of merged cutting sections
3384 TopTools_MapOfShape MapSplitEdges;
3385 TopTools_DataMapIteratorOfDataMapOfShapeShape itm;
3386 for (itm.Initialize(MergedWithSections); itm.More(); itm.Next()) {
3387 TopoDS_Shape edge = itm.Key();
3388 MapSplitEdges.Add(edge);
3390 // Iterate on edges merged with bound
3391 for (itm.Initialize(MergedWithBound); itm.More(); itm.Next()) {
3392 // Retrieve edge merged with bound
3393 TopoDS_Shape edge = itm.Key();
3394 // Remove edge from the map
3395 if (MapSplitEdges.Contains(edge)) MapSplitEdges.Remove(edge);
3396 if (myBoundSections.IsBound(edge)) {
3397 // Edge has cutting sections
3398 TopTools_ListIteratorOfListOfShape its(myBoundSections(edge));
3399 for (; its.More(); its.Next()) {
3400 TopoDS_Shape sec = its.Value();
3401 // Remove section from the map
3402 if (MapSplitEdges.Contains(sec)) MapSplitEdges.Remove(sec);
3406 // Calculate section merging tolerance
3407 Standard_Real MinSplitTol = RealLast();
3408 TopTools_MapIteratorOfMapOfShape im(MapSplitEdges);
3409 for (; im.More(); im.Next()) {
3410 TopoDS_Edge edge = TopoDS::Edge(MergedWithSections(im.Key()));
3411 MinSplitTol = Min(MinSplitTol,BRep_Tool::Tolerance(edge));
3413 // Calculate bound merging tolerance
3414 TopoDS_Edge BoundEdge = TopoDS::Edge(MergedWithBound(bound));
3415 Standard_Real BoundEdgeTol = BRep_Tool::Tolerance(BoundEdge);
3416 isSplitted = ((MinSplitTol < BoundEdgeTol+MinTolerance()) || myNonmanifold);
3417 isSplitted = (!MapSplitEdges.IsEmpty() && isSplitted);
3421 // Merging of cutting sections
3422 //myMergedEdges.Add(bound);
3423 myReShape->Replace(myReShape->Apply(bound),myReShape->Apply(BoundWire));
3424 TopTools_DataMapIteratorOfDataMapOfShapeShape itm(MergedWithSections);
3425 for (; itm.More(); itm.Next()) {
3426 TopoDS_Shape oldedge = itm.Key();
3427 TopoDS_Shape newedge = SectionsReShape->Apply(itm.Value());
3428 ReplaceEdge(myReShape->Apply(oldedge),newedge,myReShape);
3429 myMergedEdges.Add(oldedge);
3430 if (myBoundSections.IsBound(oldedge)) myBoundSections.UnBind(oldedge);
3435 // Merging of initial bound
3436 TopTools_DataMapIteratorOfDataMapOfShapeShape itm(MergedWithBound);
3437 //myMergedEdges.Add(bound);
3438 for (; itm.More(); itm.Next()) {
3439 TopoDS_Shape oldedge = itm.Key();
3440 TopoDS_Shape newedge = itm.Value();
3441 ReplaceEdge(myReShape->Apply(oldedge),newedge,myReShape);
3442 myMergedEdges.Add(oldedge);
3443 if (myBoundSections.IsBound(oldedge)) myBoundSections.UnBind(oldedge);
3445 if (myBoundSections.IsBound(bound)) myBoundSections.UnBind(bound);
3446 if(!myMergedEdges.Contains(bound))
3447 myMergedEdges.Add(bound);
3451 myNbVertices = myVertexNode.Extent() + myVertexNodeFree.Extent();
3452 myNodeSections.Clear();
3453 myVertexNode.Clear();
3454 myVertexNodeFree.Clear();
3455 myCuttingNode.Clear();
3458 //=======================================================================
3459 //function : MergedNearestEdges
3461 //=======================================================================
3463 Standard_Boolean BRepBuilderAPI_Sewing::MergedNearestEdges(const TopoDS_Shape& edge,
3464 TopTools_SequenceOfShape& SeqMergedEdge,
3465 TColStd_SequenceOfInteger& SeqMergedOri)
3467 // Retrieve edge nodes
3468 TopoDS_Vertex no1, no2;
3469 TopExp::Vertices(TopoDS::Edge(edge),no1,no2);
3470 TopoDS_Shape nno1 = no1, nno2 = no2;
3471 Standard_Boolean isNode1 = myVertexNode.Contains(no1);
3472 Standard_Boolean isNode2 = myVertexNode.Contains(no2);
3473 if (isNode1) nno1 = myVertexNode.FindFromKey(no1);
3474 if (isNode2) nno2 = myVertexNode.FindFromKey(no2);
3476 // Fill map of nodes connected to the node #1
3477 TopTools_MapOfShape mapVert1;
3479 if (myCuttingNode.IsBound(nno1)) {
3480 TopTools_ListIteratorOfListOfShape ilv(myCuttingNode(nno1));
3481 for (; ilv.More(); ilv.Next()) {
3482 TopoDS_Shape v1 = ilv.Value();
3484 if (!isNode1 && myCuttingNode.IsBound(v1)) {
3485 TopTools_ListIteratorOfListOfShape ilvn(myCuttingNode(v1));
3486 for (; ilvn.More(); ilvn.Next()) {
3487 TopoDS_Shape vn = ilvn.Value();
3494 // Fill map of nodes connected to the node #2
3495 TopTools_MapOfShape mapVert2;
3497 if (myCuttingNode.IsBound(nno2)) {
3498 TopTools_ListIteratorOfListOfShape ilv(myCuttingNode(nno2));
3499 for (; ilv.More(); ilv.Next()) {
3500 TopoDS_Shape v1 = ilv.Value();
3502 if (!isNode2 && myCuttingNode.IsBound(v1)) {
3503 TopTools_ListIteratorOfListOfShape ilvn(myCuttingNode(v1));
3504 for (; ilvn.More(); ilvn.Next()) {
3505 TopoDS_Shape vn = ilvn.Value();
3512 // Find all possible contigous edges
3513 TopTools_SequenceOfShape seqEdges;
3514 seqEdges.Append(edge);
3515 TopTools_MapOfShape mapEdges;
3517 for (TopTools_MapIteratorOfMapOfShape imv1(mapVert1); imv1.More(); imv1.Next()) {
3518 TopoDS_Shape node1 = imv1.Key();
3519 if (!myNodeSections.IsBound(node1)) continue;
3520 TopTools_ListIteratorOfListOfShape ilsec(myNodeSections(node1));
3521 for (; ilsec.More(); ilsec.Next()) {
3522 TopoDS_Shape sec = ilsec.Value();
3523 if (sec.IsSame(edge)) continue;
3524 // Retrieve section nodes
3525 TopoDS_Vertex vs1, vs2;
3526 TopExp::Vertices(TopoDS::Edge(sec),vs1,vs2);
3527 TopoDS_Shape vs1n = vs1, vs2n = vs2;
3528 if (myVertexNode.Contains(vs1)) vs1n = myVertexNode.FindFromKey(vs1);
3529 if (myVertexNode.Contains(vs2)) vs2n = myVertexNode.FindFromKey(vs2);
3530 if ((mapVert1.Contains(vs1n) && mapVert2.Contains(vs2n)) ||
3531 (mapVert1.Contains(vs2n) && mapVert2.Contains(vs1n)))
3532 if (mapEdges.Add(sec)) {
3533 // Check for rejected cutting
3534 Standard_Boolean isRejected = myMergedEdges.Contains(sec);
3535 if(!isRejected && myBoundSections.IsBound(sec))
3537 TopTools_ListIteratorOfListOfShape its(myBoundSections(sec));
3538 for (; its.More() && !isRejected; its.Next()) {
3539 TopoDS_Shape section = its.Value();
3541 if (myMergedEdges.Contains(section))
3542 isRejected = Standard_True;
3545 if( !isRejected && mySectionBound.IsBound(sec)) {
3546 const TopoDS_Shape& bnd = mySectionBound(sec);
3547 isRejected = (!myBoundSections.IsBound(bnd) ||
3548 myMergedEdges.Contains(bnd));
3551 if (!isRejected) seqEdges.Append(sec);
3558 Standard_Boolean success = Standard_False;
3560 Standard_Integer nbSection = seqEdges.Length();
3561 if (nbSection > 1) {
3562 // Find the longest edge CCI60011
3563 Standard_Integer i, indRef = 1;
3564 if (myNonmanifold) {
3565 Standard_Real lenRef = 0.;
3566 for (i = 1; i <= nbSection; i++) {
3568 Handle(Geom_Curve) c3d = BRep_Tool::Curve(TopoDS::Edge(seqEdges(i)), f, l);
3569 GeomAdaptor_Curve cAdapt(c3d);
3570 Standard_Real len = GCPnts_AbscissaPoint::Length(cAdapt, f, l);
3571 if (len > lenRef) { indRef = i; lenRef = len; }
3574 TopoDS_Shape longEdge = seqEdges(indRef);
3575 seqEdges(indRef) = seqEdges(1);
3576 seqEdges(1) = longEdge;
3580 // Find merging candidates
3581 TColStd_SequenceOfInteger seqForward;
3582 TColStd_SequenceOfInteger seqCandidates;
3583 TColStd_IndexedMapOfInteger mapReference;
3584 mapReference.Add(indRef); // Add index of reference section
3585 if (FindCandidates(seqEdges,mapReference,seqCandidates,seqForward)) {
3586 Standard_Integer nbCandidates = seqCandidates.Length();
3587 // Record candidate sections
3588 for (i = 1; i <= nbCandidates; i++) {
3589 // Retrieve merged edge
3590 TopoDS_Shape iedge = seqEdges(seqCandidates(i));
3591 Standard_Integer ori = (seqForward(i))? 1 : 0;
3592 SeqMergedEdge.Append(iedge);
3593 SeqMergedOri.Append(ori);
3594 if (!myNonmanifold) break;
3596 success = nbCandidates;
3603 //=======================================================================
3604 //function : Cutting
3605 //purpose : Modifies :
3609 //=======================================================================
3611 void BRepBuilderAPI_Sewing::Cutting(const Handle(Message_ProgressIndicator)& thePI)
3613 Standard_Integer i, nbVertices = myVertexNode.Extent();
3614 if (!nbVertices) return;
3615 // Create a box tree with vertices
3616 Standard_Real eps = myTolerance*0.5;
3617 BRepBuilderAPI_BndBoxTree aTree;
3618 NCollection_UBTreeFiller <Standard_Integer, Bnd_Box> aTreeFiller (aTree);
3619 BRepBuilderAPI_BndBoxTreeSelector aSelector;
3620 for (i = 1; i <= nbVertices; i++) {
3621 gp_Pnt pt = BRep_Tool::Pnt(TopoDS::Vertex(myVertexNode.FindKey(i)));
3625 aTreeFiller.Add (i, aBox);
3629 Handle(Geom_Curve) c3d;
3630 TopLoc_Location loc;
3631 Standard_Real first, last;
3632 // Iterate on all boundaries
3633 Standard_Integer nbBounds = myBoundFaces.Extent();
3634 Message_ProgressSentry aPS (thePI, "Cutting bounds", 0, nbBounds, 1);
3635 for (i = 1; i <= nbBounds && aPS.More(); i++, aPS.Next()) {
3636 const TopoDS_Edge& bound = TopoDS::Edge(myBoundFaces.FindKey(i));
3637 // Do not cut floating edges
3638 if (!myBoundFaces(i).Extent()) continue;
3639 // Obtain bound curve
3640 c3d = BRep_Tool::Curve(bound, loc, first, last);
3641 if (c3d.IsNull()) continue;
3642 if (!loc.IsIdentity()) {
3643 c3d = Handle(Geom_Curve)::DownCast(c3d->Copy());
3644 c3d->Transform(loc.Transformation());
3646 // Create cutting sections
3647 TopTools_ListOfShape listSections;
3648 { //szv: Use brackets to destroy local variables
3649 // Obtain candidate vertices
3650 TopoDS_Vertex V1, V2;
3651 TopTools_IndexedMapOfShape CandidateVertices;
3652 { //szv: Use brackets to destroy local variables
3653 // Create bounding box around curve
3655 GeomAdaptor_Curve adptC(c3d,first,last);
3656 BndLib_Add3dCurve::Add(adptC,myTolerance,aGlobalBox);
3657 // Sort vertices to find candidates
3658 aSelector.SetCurrent (aGlobalBox);
3659 aTree.Select (aSelector);
3660 // Skip bound if no node is in the boundind box
3661 if (!aSelector.ResInd().Extent()) continue;
3662 // Retrieve bound nodes
3663 TopExp::Vertices(bound,V1,V2);
3664 const TopoDS_Shape& Node1 = myVertexNode.FindFromKey(V1);
3665 const TopoDS_Shape& Node2 = myVertexNode.FindFromKey(V2);
3666 // Fill map of candidate vertices
3667 TColStd_ListIteratorOfListOfInteger itl(aSelector.ResInd());
3668 for (; itl.More(); itl.Next()) {
3669 const Standard_Integer index = itl.Value();
3670 const TopoDS_Shape& Node = myVertexNode.FindFromIndex(index);
3671 if (!Node.IsSame(Node1) && !Node.IsSame(Node2)) {
3672 TopoDS_Shape vertex = myVertexNode.FindKey(index);
3673 CandidateVertices.Add(vertex);
3676 aSelector.ClearResList();
3678 Standard_Integer nbCandidates = CandidateVertices.Extent();
3679 if (!nbCandidates) continue;
3680 // Project vertices on curve
3681 TColStd_Array1OfReal arrPara(1,nbCandidates), arrDist(1,nbCandidates);
3682 TColgp_Array1OfPnt arrPnt(1,nbCandidates), arrProj(1,nbCandidates);
3683 for (Standard_Integer j = 1; j <= nbCandidates; j++)
3684 arrPnt(j) = BRep_Tool::Pnt(TopoDS::Vertex(CandidateVertices(j)));
3685 ProjectPointsOnCurve(arrPnt,c3d,first,last,arrDist,arrPara,arrProj,Standard_True);
3686 // Create cutting nodes
3687 TopTools_SequenceOfShape seqNode;
3688 TColStd_SequenceOfReal seqPara;
3689 CreateCuttingNodes(CandidateVertices,bound,
3690 V1,V2,arrDist,arrPara,arrProj,seqNode,seqPara);
3691 if (!seqPara.Length()) continue;
3692 // Create cutting sections
3693 CreateSections(bound, seqNode, seqPara, listSections);
3695 if (listSections.Extent() > 1) {
3696 // modification of maps:
3698 TopTools_ListIteratorOfListOfShape its(listSections);
3699 for (; its.More(); its.Next()) {
3700 TopoDS_Shape section = its.Value();
3701 // Iterate on section vertices
3702 for (TopoDS_Iterator itv(section); itv.More(); itv.Next()) {
3703 TopoDS_Shape vertex = itv.Value();
3704 // Convert vertex to node
3705 if (myVertexNode.Contains(vertex))
3706 vertex = TopoDS::Vertex(myVertexNode.FindFromKey(vertex));
3707 // Update node sections
3708 if (myNodeSections.IsBound(vertex))
3709 myNodeSections.ChangeFind(vertex).Append(section);
3711 TopTools_ListOfShape lsec;
3712 lsec.Append(section);
3713 myNodeSections.Bind(vertex,lsec);
3716 // Store bound for section
3717 mySectionBound.Bind(section,bound);
3719 // Store splitted bound
3720 myBoundSections.Bind(bound,listSections);
3724 cout << "From " << nbBounds << " bounds " << myBoundSections.Extent()
3725 << " were cut into " << mySectionBound.Extent() << " sections" << endl;
3729 //=======================================================================
3730 //function : GetSeqEdges
3732 //=======================================================================
3734 static void GetSeqEdges(const TopoDS_Shape& edge,
3735 TopTools_SequenceOfShape& seqEdges,
3736 TopTools_DataMapOfShapeListOfShape& VertEdge)
3738 Standard_Integer numV = 0;
3739 for (TopoDS_Iterator Iv(edge,Standard_False); Iv.More(); Iv.Next()) {
3740 TopoDS_Vertex V1 = TopoDS::Vertex(Iv.Value());
3742 if (VertEdge.IsBound(V1)) {
3743 const TopTools_ListOfShape& listEdges = VertEdge.Find(V1);
3744 for (TopTools_ListIteratorOfListOfShape lIt(listEdges); lIt.More(); lIt.Next()) {
3745 TopoDS_Shape edge1 = lIt.Value();
3746 if (edge1.IsSame(edge)) continue;
3747 Standard_Boolean isContained = Standard_False;
3748 Standard_Integer i, index = 1;
3749 for (i = 1; i <= seqEdges.Length() && !isContained; i++) {
3750 isContained = seqEdges.Value(i).IsSame(edge1);
3751 if (!isContained && seqEdges.Value(i).IsSame(edge)) index = i;
3754 if (numV == 1) seqEdges.InsertBefore(index,edge1);
3755 else seqEdges.InsertAfter(index,edge1);
3756 GetSeqEdges(edge1,seqEdges,VertEdge);
3763 //=======================================================================
3764 //function : GetFreeWires
3766 //=======================================================================
3768 void BRepBuilderAPI_Sewing::GetFreeWires(TopTools_MapOfShape& MapFreeEdges, TopTools_SequenceOfShape& seqWires)
3770 TopTools_DataMapOfShapeListOfShape VertEdge;
3771 TopTools_MapIteratorOfMapOfShape itMap(MapFreeEdges);
3772 TopTools_SequenceOfShape seqFreeEdges;
3773 for (; itMap.More(); itMap.Next()) {
3774 TopoDS_Shape edge = itMap.Key();
3775 seqFreeEdges.Append(edge);
3776 for (TopoDS_Iterator Iv(edge,Standard_False); Iv.More(); Iv.Next()) {
3777 TopoDS_Vertex V1 = TopoDS::Vertex(Iv.Value());
3778 if (VertEdge.IsBound(V1))
3779 VertEdge.ChangeFind(V1).Append(edge);
3781 TopTools_ListOfShape ls;
3783 VertEdge.Bind(V1,ls);
3788 Standard_Integer i, j;
3789 for (i = 1; i <= seqFreeEdges.Length(); i++) {
3790 TopTools_SequenceOfShape seqEdges;
3791 TopoDS_Shape edge = seqFreeEdges.Value(i);
3792 if (!MapFreeEdges.Contains(edge)) continue;
3793 seqEdges.Append(edge);
3794 GetSeqEdges(edge,seqEdges,VertEdge);
3797 for (j = 1; j <= seqEdges.Length(); j++) {
3798 B.Add(wire,seqEdges.Value(j));
3799 MapFreeEdges.Remove(seqEdges.Value(j));
3801 seqWires.Append(wire);
3802 if (MapFreeEdges.IsEmpty()) break;
3806 //=======================================================================
3807 //function : IsDegeneratedWire
3808 //purpose : internal use
3809 //=======================================================================
3811 static Standard_Boolean IsDegeneratedWire(const TopoDS_Shape& wire)
3813 if (wire.ShapeType() != TopAbs_WIRE) return Standard_False;
3814 // Get maximal vertices tolerance
3815 TopoDS_Vertex V1,V2;
3816 //TopExp::Vertices(TopoDS::Wire(wire),V1,V2);
3817 //Standard_Real tol = Max(BRep_Tool::Tolerance(V1),BRep_Tool::Tolerance(V2));
3818 Standard_Real wireLength = 0.0;
3819 TopLoc_Location loc;
3820 Standard_Real first, last;
3821 Standard_Integer nume = 0;
3822 Standard_Integer isSmall = 0;
3823 for (TopoDS_Iterator aIt(wire,Standard_False); aIt.More(); aIt.Next()) {
3825 TopoDS_Shape edge = aIt.Value();
3826 TopoDS_Vertex Ve1,Ve2;
3827 TopExp::Vertices(TopoDS::Edge(edge),Ve1,Ve2);
3835 else if(Ve1.IsSame(V2))
3839 else if(Ve2.IsSame(V2))
3842 Handle(Geom_Curve) c3d = BRep_Tool::Curve(TopoDS::Edge(aIt.Value()),loc,first,last);
3843 if (!c3d.IsNull()) {
3844 c3d = Handle(Geom_Curve)::DownCast(c3d->Copy());
3845 if (!loc.IsIdentity()) {
3846 //c3d = Handle(Geom_Curve)::DownCast(c3d->Copy());
3847 c3d->Transform(loc.Transformation());
3849 gp_Pnt pfirst = c3d->Value(first);
3850 gp_Pnt plast = c3d->Value(last);
3851 gp_Pnt pmid = c3d->Value((first +last)*0.5);
3852 Standard_Real length =0;
3853 if(pfirst.Distance(plast) > pfirst.Distance(pmid)) {
3854 length = pfirst.Distance(plast);
3857 GeomAdaptor_Curve cAdapt(c3d);
3858 length = GCPnts_AbscissaPoint::Length(cAdapt, first, last);
3860 Standard_Real tole = BRep_Tool::Tolerance(Ve1)+BRep_Tool::Tolerance(Ve2);
3861 if(length <= tole) isSmall++;
3862 wireLength += length;
3865 if(isSmall == nume) return Standard_True;
3866 Standard_Real tol = BRep_Tool::Tolerance(V1)+BRep_Tool::Tolerance(V2);//Max(BRep_Tool::Tolerance(V1),BRep_Tool::Tolerance(V2));
3867 if (wireLength > tol) return Standard_False;
3868 return Standard_True;
3871 //=======================================================================
3872 //function : DegeneratedSection
3873 //purpose : internal use
3874 // create a new degenerated edge if the section is degenerated
3875 //=======================================================================
3877 static TopoDS_Edge DegeneratedSection(const TopoDS_Shape& section, const TopoDS_Shape& face)
3879 // Return if section is already degenerated
3880 if (BRep_Tool::Degenerated(TopoDS::Edge(section))) return TopoDS::Edge(section);
3882 // Retrieve edge curve
3883 TopLoc_Location loc;
3884 Standard_Real first, last;
3885 Handle(Geom_Curve) c3d = BRep_Tool::Curve(TopoDS::Edge(section), loc, first, last);
3886 if (c3d.IsNull()) { //gka
3888 TopoDS_Edge edge1 = TopoDS::Edge(section);
3889 aB.Degenerated(edge1, Standard_True);
3892 if (!loc.IsIdentity()) {
3893 c3d = Handle(Geom_Curve)::DownCast(c3d->Copy());
3894 c3d->Transform(loc.Transformation());
3897 // Test if the new edge is degenerated
3898 TopoDS_Vertex v1,v2;
3899 TopExp::Vertices(TopoDS::Edge(section),v1,v2);
3900 //Standard_Real tol = Max(BRep_Tool::Tolerance(v1),BRep_Tool::Tolerance(v2));
3901 //tol = Max(tolerance,tol);
3904 p1 = BRep_Tool::Pnt(v1);
3905 p3 = BRep_Tool::Pnt(v2);
3906 c3d->D0(0.5*(first + last),p2);
3908 //Standard_Boolean isDegenerated = Standard_False;
3909 //if (p1.Distance(p3) < tol) {
3910 //GeomAdaptor_Curve cAdapt(c3d);
3911 //Standard_Real length = GCPnts_AbscissaPoint::Length(cAdapt, first, last);
3912 //isDegenerated = Standard_True; //(length < tol);
3916 //if (!isDegenerated) return edge;
3919 BRep_Builder aBuilder;
3920 edge = TopoDS::Edge(section);
3922 if (v1.IsSame(v2)) {
3923 TopoDS_Shape anEdge = edge.Oriented(TopAbs_FORWARD);
3924 aBuilder.Add(anEdge, v1.Oriented(TopAbs_FORWARD));
3925 aBuilder.Add(anEdge, v2.Oriented(TopAbs_REVERSED));
3928 TopoDS_Vertex newVertex;
3929 if (p1.Distance(p3) < BRep_Tool::Tolerance(v1))
3931 else if (p1.Distance(p3) < BRep_Tool::Tolerance(v2))
3934 Standard_Real d1 = BRep_Tool::Tolerance(v1) + p2.Distance(p1);
3935 Standard_Real d2 = BRep_Tool::Tolerance(v2) + p2.Distance(p3);
3936 Standard_Real newTolerance = Max(d1,d2);
3937 aBuilder.MakeVertex(newVertex, p2, newTolerance);
3939 TopoDS_Shape anEdge = edge.Oriented(TopAbs_FORWARD);
3940 aBuilder.Add(anEdge, newVertex.Oriented(TopAbs_FORWARD));
3941 aBuilder.Add(anEdge, newVertex.Oriented(TopAbs_REVERSED));
3944 BRep_Tool::Range(TopoDS::Edge(section), first, last);
3945 TopoDS_Shape anEdge = edge.Oriented(TopAbs_FORWARD);
3946 aBuilder.Range(TopoDS::Edge(anEdge), first, last);
3947 aBuilder.Degenerated(edge, Standard_True);
3948 Handle(Geom_Curve) aC3dNew;
3949 if (!face.IsNull()) {
3950 Standard_Real af,al;
3951 Handle(Geom2d_Curve) aC2dt = BRep_Tool::CurveOnSurface(TopoDS::Edge(section),TopoDS::Face(face),af,al);
3952 aBuilder.UpdateEdge(edge,aC3dNew,0);
3953 Handle(Geom2d_Curve) aC2dn = BRep_Tool::CurveOnSurface(edge,TopoDS::Face(face),af,al);
3955 aBuilder.UpdateEdge(edge,aC2dt,TopoDS::Face(face),0);
3961 //=======================================================================
3962 //function : EdgeProcessing
3963 //purpose : modifies :
3965 // myHasMultipleEdge
3967 // . if multiple edge
3968 // - length < 100.*myTolerance -> several free edge
3969 // . if no multiple edge
3970 // - make the contigous edges sameparameter
3971 //=======================================================================
3973 void BRepBuilderAPI_Sewing::EdgeProcessing(const Handle(Message_ProgressIndicator)& thePI)
3975 // constructs sectionEdge
3976 TopTools_MapOfShape MapFreeEdges;
3977 TopTools_DataMapOfShapeShape EdgeFace;
3978 Message_ProgressSentry aPS (thePI, "Edge processing", 0, myBoundFaces.Extent(), 1);
3979 for (Standard_Integer i = 1; i <= myBoundFaces.Extent() && aPS.More(); i++, aPS.Next()) {
3980 const TopoDS_Shape& bound = myBoundFaces.FindKey(i);
3981 const TopTools_ListOfShape& listFaces = myBoundFaces(i);
3982 if (listFaces.Extent() == 1) {
3983 if (myBoundSections.IsBound(bound)) {
3984 TopTools_ListIteratorOfListOfShape liter(myBoundSections(bound));
3985 for (; liter.More(); liter.Next()) {
3986 if (!myMergedEdges.Contains(liter.Value())) { //myReShape->IsRecorded(liter.Value())) {
3987 TopoDS_Shape edge = myReShape->Apply(liter.Value());
3988 if (!MapFreeEdges.Contains(edge)) {
3989 TopoDS_Shape face = listFaces.First();
3990 EdgeFace.Bind(edge,face);
3991 MapFreeEdges.Add(edge);
3997 if (!myMergedEdges.Contains(bound)) {
3998 TopoDS_Shape edge = myReShape->Apply(bound);
3999 if (!MapFreeEdges.Contains(edge)) {
4000 TopoDS_Shape face = listFaces.First();
4001 EdgeFace.Bind(edge,face);
4002 MapFreeEdges.Add(edge);
4009 if (!MapFreeEdges.IsEmpty()) {
4010 TopTools_SequenceOfShape seqWires;
4011 GetFreeWires(MapFreeEdges,seqWires);
4012 for (Standard_Integer j = 1; j <= seqWires.Length(); j++) {
4013 TopoDS_Wire wire = TopoDS::Wire(seqWires.Value(j));
4014 if (!IsDegeneratedWire(wire)) continue;
4015 for (TopoDS_Iterator Ie(wire,Standard_False); Ie.More(); Ie.Next()) {
4016 TopoDS_Shape aTmpShape = myReShape->Apply(Ie.Value()); //for porting
4017 TopoDS_Edge edge = TopoDS::Edge(aTmpShape);
4019 if (EdgeFace.IsBound(edge))
4020 face = EdgeFace.Find(edge);
4021 TopoDS_Shape degedge = DegeneratedSection(edge,face);
4022 if (degedge.IsNull()) continue;
4023 if (!degedge.IsSame(edge))
4024 ReplaceEdge(edge,degedge,myReShape);
4025 if (BRep_Tool::Degenerated(TopoDS::Edge(degedge)))
4026 myDegenerated.Add(degedge);
4030 myMergedEdges.Clear();
4033 //=======================================================================
4034 //function : CreateSewedShape
4036 //=======================================================================
4038 void BRepBuilderAPI_Sewing::CreateSewedShape()
4040 // ---------------------
4041 // create the new shapes
4042 // ---------------------
4043 BRepTools_Quilt aQuilt;
4044 Standard_Boolean isLocal = !myShape.IsNull();
4047 TopoDS_Shape ns = myReShape->Apply(myShape);
4051 for (i = 1; i <= myOldShapes.Extent(); i++) {
4052 TopoDS_Shape sh = myOldShapes(i);
4054 sh = myReShape->Apply(sh);
4055 myOldShapes(i) = sh;
4056 if (!isLocal) aQuilt.Add(sh);
4059 TopoDS_Shape aNewShape = aQuilt.Shells();
4060 Standard_Integer numsh = 0;
4062 TopTools_IndexedMapOfShape OldShells;
4065 TopoDS_Compound aComp;
4066 aB.MakeCompound(aComp);
4067 for (TopoDS_Iterator aExpSh(aNewShape,Standard_False); aExpSh.More(); aExpSh.Next()) {
4068 TopoDS_Shape sh = aExpSh.Value();
4069 Standard_Boolean hasEdges = Standard_False;
4070 if (sh.ShapeType() == TopAbs_SHELL) {
4072 hasEdges = !OldShells.Contains(sh);
4075 Standard_Integer numf = 0;
4076 for (TopExp_Explorer aExpF(sh,TopAbs_FACE); aExpF.More() && (numf < 2); aExpF.Next()) {
4077 face = aExpF.Current();
4080 if (numf == 1) aB.Add(aComp,face);
4081 else if (numf > 1) aB.Add(aComp,sh);
4085 else if (sh.ShapeType() == TopAbs_FACE) {
4086 if (myNonmanifold) {
4091 hasEdges = Standard_True;
4093 else { aB.Add(aComp,sh); numsh++; }
4095 else { aB.Add(aComp,sh); numsh++; }
4096 if (hasEdges) OldShells.Add(sh);
4098 // Process collected shells
4099 if (myNonmanifold) {
4100 Standard_Integer nbOldShells = OldShells.Extent();
4101 if (nbOldShells == 1) {
4102 // Single shell - check for single face
4103 TopoDS_Shape sh = OldShells.FindKey(1);
4105 Standard_Integer numf = 0;
4106 for (TopExp_Explorer aExpF(sh,TopAbs_FACE); aExpF.More() && (numf < 2); aExpF.Next()) {
4107 face = aExpF.Current();
4110 if (numf == 1) aB.Add(aComp,face);
4111 else if (numf > 1) aB.Add(aComp,sh);
4114 else if (nbOldShells) {
4115 // Several shells should be merged
4116 TColStd_MapOfInteger IndexMerged;
4117 while (IndexMerged.Extent() < nbOldShells) {
4118 TopoDS_Shell NewShell;
4119 TopTools_MapOfShape NewEdges;
4120 for (i = 1; i <= nbOldShells; i++) {
4121 if (IndexMerged.Contains(i)) continue;
4122 TopoDS_Shell shell = TopoDS::Shell(OldShells.FindKey(i));
4123 if (NewShell.IsNull()) {
4125 aB.MakeShell(NewShell);
4126 TopoDS_Iterator aItSS(shell) ;
4127 for( ; aItSS.More(); aItSS.Next())
4128 aB.Add(NewShell,aItSS.Value())
4130 // Fill map of edges
4131 for (TopExp_Explorer eexp(shell,TopAbs_EDGE); eexp.More(); eexp.Next()) {
4132 TopoDS_Shape edge = eexp.Current();
4138 Standard_Boolean hasSharedEdge = Standard_False;
4139 TopExp_Explorer eexp(shell,TopAbs_EDGE);
4140 for (; eexp.More() && !hasSharedEdge; eexp.Next())
4141 hasSharedEdge = NewEdges.Contains(eexp.Current());
4142 if (hasSharedEdge) {
4143 // Add edges to the map
4144 for (TopExp_Explorer eexp1(shell,TopAbs_EDGE); eexp1.More(); eexp1.Next()) {
4145 TopoDS_Shape edge = eexp1.Current();
4148 // Add faces to the shell
4149 for (TopExp_Explorer fexp(shell,TopAbs_FACE); fexp.More(); fexp.Next()) {
4150 TopoDS_Shape face = fexp.Current();
4151 aB.Add(NewShell,face);
4157 // Process new shell
4159 Standard_Integer numf = 0;
4160 TopExp_Explorer aExpF(NewShell,TopAbs_FACE);
4161 for (; aExpF.More() && (numf < 2); aExpF.Next()) {
4162 face = aExpF.Current();
4165 if (numf == 1) aB.Add(aComp,face);
4166 else if (numf > 1) aB.Add(aComp,NewShell);
4172 // Extract single component
4173 TopoDS_Iterator aIt(aComp,Standard_False);
4174 mySewedShape = aIt.Value();
4177 mySewedShape = aComp;
4180 //=======================================================================
4181 //function : CreateOutputInformations
4182 //purpose : constructs :
4186 // myNbContigousEdges
4187 // myNbMultipleEdges
4189 //=======================================================================
4191 void BRepBuilderAPI_Sewing::CreateOutputInformations()
4193 // Construct edgeSections
4195 //TopTools_DataMapOfShapeListOfShape edgeSections;
4196 TopTools_IndexedDataMapOfShapeListOfShape edgeSections; //use index map for regulating free edges
4197 for (i = 1; i <= myBoundFaces.Extent(); i++) {
4198 const TopoDS_Shape& bound = myBoundFaces.FindKey(i);
4199 TopTools_ListOfShape lsect;
4200 if (myBoundSections.IsBound(bound)) lsect = myBoundSections(bound);
4201 TopExp_Explorer aExp(myReShape->Apply(bound),TopAbs_EDGE);
4202 for (; aExp.More(); aExp.Next()) {
4203 TopoDS_Shape sec = bound, edge = aExp.Current();
4204 TopTools_ListIteratorOfListOfShape aI(lsect);
4205 for (; aI.More(); aI.Next()) {
4206 const TopoDS_Shape& section = aI.Value();
4207 if (edge.IsSame(myReShape->Apply(section))) { sec = section; break; }
4209 if (edgeSections.Contains(edge))
4210 edgeSections.ChangeFromKey(edge).Append(sec);
4212 TopTools_ListOfShape listSec;
4213 listSec.Append(sec);
4214 edgeSections.Add(edge,listSec);
4219 // Fill maps of Free, Contigous and Multiple edges
4220 //TopTools_DataMapIteratorOfDataMapOfShapeListOfShape iter2(edgeSections);
4221 for (i = 1; i <= edgeSections.Extent(); i++) {
4222 const TopoDS_Shape& edge = edgeSections.FindKey(i);
4223 const TopTools_ListOfShape& listSection = edgeSections(i);
4224 if (listSection.Extent() == 1) {
4225 if (BRep_Tool::Degenerated(TopoDS::Edge(edge)))
4226 myDegenerated.Add(edge);
4228 myFreeEdges.Add(edge);
4230 else if (listSection.Extent() == 2) {
4231 myContigousEdges.Add(edge,listSection);
4234 myMultipleEdges.Add(edge);
4238 // constructs myContigSectBound
4239 TopTools_DataMapOfShapeListOfShape aEdgeMap; //gka
4240 for (i = 1; i <= myBoundFaces.Extent(); i++) {
4241 TopoDS_Shape bound = myBoundFaces.FindKey(i);
4242 if (myBoundSections.IsBound(bound)) {
4243 TopTools_ListIteratorOfListOfShape iter(myBoundSections(bound));
4244 for (; iter.More(); iter.Next()) {
4245 TopoDS_Shape section = iter.Value();
4246 if(!myMergedEdges.Contains(section)) continue;
4247 //if (!myReShape->IsRecorded(section)) continue; // section is free
4248 TopoDS_Shape nedge = myReShape->Apply(section);
4249 if (nedge.IsNull()) continue; //szv debug
4250 if (!bound.IsSame(section))
4251 if (myContigousEdges.Contains(nedge))
4252 myContigSecBound.Bind(section, bound);
4258 //=======================================================================
4259 //function : ProjectPointsOnCurve
4260 //purpose : internal use
4261 //=======================================================================
4263 void BRepBuilderAPI_Sewing::ProjectPointsOnCurve(const TColgp_Array1OfPnt& arrPnt,
4264 const Handle(Geom_Curve)& c3d,
4265 const Standard_Real first,
4266 const Standard_Real last,
4267 TColStd_Array1OfReal& arrDist,
4268 TColStd_Array1OfReal& arrPara,
4269 TColgp_Array1OfPnt& arrProj,
4270 const Standard_Boolean isConsiderEnds) const
4274 GeomAdaptor_Curve GAC(c3d);
4275 Extrema_ExtPC locProj;
4276 locProj.Initialize(GAC, first, last);
4277 gp_Pnt pfirst = GAC.Value(first), plast = GAC.Value(last);
4278 Standard_Integer find = 1;//(isConsiderEnds ? 1 : 2);
4279 Standard_Integer lind = arrPnt.Length();//(isConsiderEnds ? arrPnt.Length() : arrPnt.Length() -1);
4281 for (Standard_Integer i1 = find; i1 <= lind ; i1++) {
4282 gp_Pnt pt = arrPnt(i1);
4283 Standard_Real worktol = myTolerance;
4284 Standard_Real distF2 = pfirst.SquareDistance(pt);
4285 Standard_Real distL2 = plast.SquareDistance(pt);
4286 Standard_Boolean isProjected = Standard_False;
4289 // Project current point on curve
4290 locProj.Perform(pt);
4291 if (locProj.IsDone() && locProj.NbExt() > 0) {
4292 Standard_Real dist2Min = (isConsiderEnds || i1 == find || i1 == lind ? Min(distF2,distL2) : Precision::Infinite());
4293 Standard_Integer ind, indMin = 0;
4294 for (ind = 1; ind <= locProj.NbExt(); ind++) {
4295 Standard_Real dProj2 = locProj.SquareDistance(ind);
4296 if (dProj2 < dist2Min) { indMin = ind; dist2Min = dProj2; }
4299 isProjected = Standard_True;
4300 Extrema_POnCurv pOnC = locProj.Point(indMin);
4301 Standard_Real paramProj = pOnC.Parameter();
4302 gp_Pnt ptProj = GAC.Value(paramProj);
4303 Standard_Real distProj2 = ptProj.SquareDistance(pt);
4304 if (!locProj.IsMin(indMin)) {
4305 if (Min(distF2,distL2) < dist2Min) {
4306 if (distF2 < distL2) {
4318 if (distProj2 < worktol * worktol || !isConsiderEnds) {
4319 arrDist(i1) = sqrt (distProj2);
4320 arrPara(i1) = paramProj;
4321 arrProj(i1) = ptProj;
4326 catch (Standard_Failure) {
4327 worktol = MinTolerance();
4329 cout << "Exception in BRepBuilderAPI_Sewing::ProjectPointsOnCurve: ";
4330 Standard_Failure::Caught()->Print(cout); cout << endl;
4333 if (!isProjected && isConsiderEnds) {
4334 if (Min(distF2,distL2) < worktol * worktol) {
4335 if (distF2 < distL2) {
4336 arrDist(i1) = sqrt (distF2);
4337 arrPara(i1) = first;
4338 arrProj(i1) = pfirst;
4341 arrDist(i1) = sqrt (distL2);
4343 arrProj(i1) = plast;
4350 //=======================================================================
4351 //function : CreateCuttingNodes
4352 //purpose : internal use
4353 //=======================================================================
4355 void BRepBuilderAPI_Sewing::CreateCuttingNodes(const TopTools_IndexedMapOfShape& MapVert,
4356 const TopoDS_Shape& bound,
4357 const TopoDS_Shape& vfirst,
4358 const TopoDS_Shape& vlast,
4359 const TColStd_Array1OfReal& arrDist,
4360 const TColStd_Array1OfReal& arrPara,
4361 const TColgp_Array1OfPnt& arrPnt,
4362 TopTools_SequenceOfShape& seqVert,
4363 TColStd_SequenceOfReal& seqPara)
4365 Standard_Integer i, j, nbProj = MapVert.Extent();
4367 // Reorder projections by distance
4368 TColStd_SequenceOfInteger seqOrderedIndex;
4369 { //szv: Use brackets to destroy local variables
4370 TColStd_SequenceOfReal seqOrderedDistance;
4371 for (i = 1; i <= nbProj; i++) {
4372 Standard_Real distProj = arrDist(i);
4373 if (distProj < 0.0) continue; // Skip vertex if not projected
4374 Standard_Boolean isInserted = Standard_False;
4375 for (j = 1; j <= seqOrderedIndex.Length() && !isInserted; j++) {
4376 isInserted = (distProj < seqOrderedDistance(j));
4378 seqOrderedIndex.InsertBefore(j,i);
4379 seqOrderedDistance.InsertBefore(j,distProj);
4383 seqOrderedIndex.Append(i);
4384 seqOrderedDistance.Append(distProj);
4388 nbProj = seqOrderedIndex.Length();
4389 if (!nbProj) return;
4391 BRep_Builder aBuilder;
4393 // Insert two initial vertices (to be removed later)
4394 TColStd_SequenceOfReal seqDist;
4395 TColgp_SequenceOfPnt seqPnt;
4396 { //szv: Use brackets to destroy local variables
4397 // Retrieve bound curve
4398 TopLoc_Location loc;
4399 Standard_Real first,last;
4400 Handle(Geom_Curve) c3d = BRep_Tool::Curve(TopoDS::Edge(bound), loc, first, last);
4401 if (!loc.IsIdentity()) {
4402 c3d = Handle(Geom_Curve)::DownCast(c3d->Copy());
4403 c3d->Transform(loc.Transformation());
4405 GeomAdaptor_Curve GAC(c3d);
4406 seqVert.Prepend(vfirst); seqVert.Append(vlast);
4407 seqPara.Prepend(first); seqPara.Append(last);
4408 seqDist.Prepend(-1.0); seqDist.Append(-1.0);
4409 seqPnt.Prepend(GAC.Value(first)); seqPnt.Append(GAC.Value(last));
4412 TopTools_DataMapOfShapeShape NodeCuttingVertex;
4413 for (i = 1; i <= nbProj; i++) {
4415 const Standard_Integer index = seqOrderedIndex(i);
4416 Standard_Real disProj = arrDist(index);
4417 gp_Pnt pntProj = arrPnt(index);
4419 // Skip node if already bound to cutting vertex
4420 TopoDS_Shape node = myVertexNode.FindFromKey(MapVert(index));
4421 if (NodeCuttingVertex.IsBound(node)) continue;
4423 // Find the closest vertex
4424 Standard_Integer indexMin = 1;
4425 Standard_Real dist, distMin = pntProj.Distance(seqPnt(1));
4426 for (j = 2; j <= seqPnt.Length(); j++) {
4427 dist = pntProj.Distance(seqPnt(j));
4428 if (dist < distMin) { distMin = dist; indexMin = j; }
4431 // Check if current point is close to one of the existent
4432 if (distMin <= Max(disProj*0.1,MinTolerance())) {
4433 // Check distance if close
4434 Standard_Real jdist = seqDist.Value(indexMin);
4436 // Bind new cutting node (end vertex only)
4437 seqDist.SetValue(indexMin,disProj);
4438 TopoDS_Shape cvertex = seqVert.Value(indexMin);
4439 NodeCuttingVertex.Bind(node,cvertex);
4442 // Bind secondary cutting nodes
4443 NodeCuttingVertex.Bind(node,TopoDS_Vertex());
4447 // Build new cutting vertex
4448 TopoDS_Vertex cvertex;
4449 aBuilder.MakeVertex(cvertex, pntProj, Precision::Confusion());
4450 // Bind new cutting vertex
4451 NodeCuttingVertex.Bind(node,cvertex);
4452 // Insert cutting vertex in the sequences
4453 Standard_Real parProj = arrPara(index);
4454 for (j = 2; j <= seqPara.Length(); j++) {
4455 if (parProj <= seqPara.Value(j)) {
4456 seqVert.InsertBefore(j,cvertex);
4457 seqPara.InsertBefore(j,parProj);
4458 seqDist.InsertBefore(j,disProj);
4459 seqPnt.InsertBefore (j,pntProj);
4466 // filling map for cutting nodes
4467 TopTools_DataMapIteratorOfDataMapOfShapeShape mit(NodeCuttingVertex);
4468 for (; mit.More(); mit.Next()) {
4469 TopoDS_Shape cnode = mit.Value();
4470 // Skip secondary nodes
4471 if (cnode.IsNull()) continue;
4472 // Obtain vertex node
4473 TopoDS_Shape node = mit.Key();
4474 if (myVertexNode.Contains(cnode)) {
4475 // This is an end vertex
4476 cnode = myVertexNode.FindFromKey(cnode);
4479 // Create link: cutting vertex -> node
4480 TopTools_ListOfShape ls;
4482 myCuttingNode.Bind(cnode,ls);
4484 // Create link: node -> cutting vertex
4485 if (myCuttingNode.IsBound(node)) {
4486 myCuttingNode.ChangeFind(node).Append(cnode);
4489 TopTools_ListOfShape ls;
4491 myCuttingNode.Bind(node,ls);
4495 // Remove two initial vertices
4496 seqVert.Remove(1); seqVert.Remove(seqVert.Length());
4497 seqPara.Remove(1); seqPara.Remove(seqPara.Length());
4500 //=======================================================================
4501 //function : CreateSections
4502 //purpose : internal use
4503 //=======================================================================
4505 void BRepBuilderAPI_Sewing::CreateSections(const TopoDS_Shape& section,
4506 const TopTools_SequenceOfShape& seqNode,
4507 const TColStd_SequenceOfReal& seqPara,
4508 TopTools_ListOfShape& listEdge)
4510 const TopoDS_Edge& sec = TopoDS::Edge(section);
4511 // TopAbs_Orientation aInitOr = sec.Orientation();
4514 //To keep NM vertices on edge
4515 TopTools_SequenceOfShape aSeqNMVert;
4516 TColStd_SequenceOfReal aSeqNMPars;
4517 findNMVertices(sec,aSeqNMVert,aSeqNMPars);
4519 BRep_Builder aBuilder;
4521 Standard_Real first, last;
4522 BRep_Tool::Range(sec, first, last);
4524 // Create cutting sections
4525 Standard_Real par1, par2;
4526 TopoDS_Shape V1, V2;
4527 Standard_Integer i, len = seqPara.Length() + 1;
4528 for (i = 1; i <= len; i++) {
4530 TopoDS_Edge edge = sec;
4536 V1 = TopExp::FirstVertex(sec);
4539 else if (i == len) {
4540 par1 = seqPara(i-1);
4543 V2 = TopExp::LastVertex(sec);
4546 par1 = seqPara(i-1);
4552 TopoDS_Shape aTmpShape = edge.Oriented(TopAbs_FORWARD);
4553 TopoDS_Edge aTmpEdge = TopoDS::Edge (aTmpShape); // for porting
4554 aTmpShape = V1.Oriented(TopAbs_FORWARD);
4555 aBuilder.Add(aTmpEdge, aTmpShape);
4556 aTmpShape = V2.Oriented(TopAbs_REVERSED);
4557 aBuilder.Add(aTmpEdge, aTmpShape);
4558 aBuilder.Range(aTmpEdge, par1, par2);
4559 // if(aInitOr == TopAbs_REVERSED)
4560 // listEdge.Prepend(edge);
4563 Standard_Integer k =1;
4564 for( ; k <= aSeqNMPars.Length() ; k++) {
4565 Standard_Real apar = aSeqNMPars.Value(k);
4566 if(apar >= par1 && apar <= par2) {
4567 aBuilder.Add(aTmpEdge,aSeqNMVert.Value(k));
4568 aSeqNMVert.Remove(k);
4569 aSeqNMPars.Remove(k);
4573 listEdge.Append(edge);
4576 const TopTools_ListOfShape& listFaces = myBoundFaces.FindFromKey(sec);
4577 if (!listFaces.Extent()) return;
4579 Standard_Real tolEdge = BRep_Tool::Tolerance(sec);
4581 // Add cutting pcurves
4582 TopTools_ListIteratorOfListOfShape itf(listFaces);
4583 for (; itf.More(); itf.Next()) {
4585 const TopoDS_Face& fac = TopoDS::Face(itf.Value());
4587 // Retrieve curve on surface
4588 Standard_Real first2d=0., last2d=0.,first2d1=0,last2d1=0.;
4589 Handle(Geom2d_Curve) c2d = BRep_Tool::CurveOnSurface(sec, fac, first2d, last2d);
4590 if (c2d.IsNull()) continue;
4591 Handle(Geom2d_Curve) c2d1;
4592 Standard_Boolean isSeam = BRep_Tool::IsClosed(sec,fac);
4594 //gka - Convert to BSpline was commented because
4595 //it is not necessary to create BSpline instead of Lines or cIrcles.
4596 //Besides after conversion circles to BSpline
4597 //it is necessary to recompute parameters of cutting because paramerization of created
4598 //BSpline curve differs from parametrization of circle.
4600 // Convert pcurve to BSpline
4601 /*Handle(Geom2d_BSplineCurve) c2dBSP,c2dBSP1;
4602 if (c2d->IsKind(STANDARD_TYPE(Geom2d_BSplineCurve))) {
4603 c2dBSP = Handle(Geom2d_BSplineCurve)::DownCast(c2d);
4606 if (first > (c2d->FirstParameter() + Precision::PConfusion()) ||
4607 last < (c2d->LastParameter() - Precision::PConfusion())) {
4608 Handle(Geom2d_TrimmedCurve) TC = new Geom2d_TrimmedCurve(c2d, first, last);
4609 c2dBSP = Geom2dConvert::CurveToBSplineCurve(TC);
4611 else c2dBSP = Geom2dConvert::CurveToBSplineCurve(c2d);
4613 if (c2dBSP.IsNull()) continue;*/
4614 //gka fix for bug OCC12203 21.04.06 addition second curve for seam edges
4618 TopoDS_Edge secRev = TopoDS::Edge(sec.Reversed());
4620 c2d1 = BRep_Tool::CurveOnSurface(secRev, fac, first2d1, last2d1);
4621 if (c2d1.IsNull()) continue;
4623 /*if (c2d1->IsKind(STANDARD_TYPE(Geom2d_BSplineCurve))) {
4624 c2dBSP1 = Handle(Geom2d_BSplineCurve)::DownCast(c2d1);
4627 if (first > (c2d1->FirstParameter() + Precision::PConfusion()) ||
4628 last < (c2d1->LastParameter() - Precision::PConfusion())) {
4629 Handle(Geom2d_TrimmedCurve) TC = new Geom2d_TrimmedCurve(c2d1, first, last);
4630 //c2dBSP1 = Geom2dConvert::CurveToBSplineCurve(TC);
4632 //else c2dBSP1 = Geom2dConvert::CurveToBSplineCurve(c2d);
4637 /*first2d = c2dBSP->FirstParameter();
4638 last2d = c2dBSP->LastParameter();
4640 if(!c2dBSP1.IsNull()) {
4641 first2d1 = c2dBSP1->FirstParameter();
4642 last2d1 = c2dBSP1->LastParameter();
4645 // Update cutting sections
4646 Handle(Geom2d_Curve) c2dNew,c2d1New;
4647 TopTools_ListIteratorOfListOfShape ite(listEdge);
4648 for (; ite.More(); ite.Next()) {
4650 // Retrieve cutting section
4651 const TopoDS_Edge& edge = TopoDS::Edge(ite.Value());
4652 BRep_Tool::Range(edge, par1, par2);
4654 // Cut BSpline pcurve
4656 c2dNew = Handle(Geom2d_Curve)::DownCast(c2d->Copy());
4657 //c2dNew = Handle(Geom2d_Curve)::DownCast(c2dBSP->Copy());
4658 //Handle(Geom2d_BSplineCurve)::DownCast(c2dNew)->Segment(Max(first2d,par1),Min(par2,last2d));
4659 if(!c2d1.IsNull()) { //if(!c2dBSP1.IsNull()) {
4660 c2d1New = Handle(Geom2d_Curve)::DownCast(c2d1->Copy());
4661 //c2d1New = Handle(Geom2d_Curve)::DownCast(c2dBSP1->Copy());
4662 //Handle(Geom2d_BSplineCurve)::DownCast(c2d1New)->Segment(Max(first2d1,par1),Min(par2,last2d1));
4665 /*catch (Standard_Failure) {
4667 cout << "Exception in CreateSections: segment [" << par1 << "," << par2 << "]: ";
4668 Standard_Failure::Caught()->Print(cout); cout << endl;
4670 Handle(Geom2d_TrimmedCurve) c2dT = new Geom2d_TrimmedCurve(c2dNew,Max(first2d,par1),Min(par2,last2d));
4675 if(!isSeam && c2d1New.IsNull())
4676 aBuilder.UpdateEdge(edge, c2dNew, fac, tolEdge);
4678 TopAbs_Orientation Ori = edge.Orientation();
4679 if(fac.Orientation() == TopAbs_REVERSED)
4680 Ori = TopAbs::Reverse(Ori);
4682 if(Ori == TopAbs_FORWARD)
4683 aBuilder.UpdateEdge(edge, c2dNew,c2d1New ,fac, tolEdge);
4685 aBuilder.UpdateEdge(edge, c2d1New,c2dNew ,fac, tolEdge);
4691 //=======================================================================
4692 //function : SameParameterShape
4694 //=======================================================================
4696 void BRepBuilderAPI_Sewing::SameParameterShape()
4698 if (!mySameParameterMode) return;
4699 TopExp_Explorer exp(mySewedShape, TopAbs_EDGE);
4700 // Le flag sameparameter est a false pour chaque edge cousue
4701 for (; exp.More(); exp.Next()) {
4702 const TopoDS_Edge& sec = TopoDS::Edge(exp.Current());
4705 BRepLib::SameParameter(sec, BRep_Tool::Tolerance(sec));
4707 catch (Standard_Failure) {
4709 cout << "Fail: BRepBuilderAPI_Sewing::SameParameterShape exception in BRepLib::SameParameter" << endl;
4716 //=======================================================================
4717 //function : Inspect
4718 //purpose : Used for selection and storage of coinciding points
4719 //=======================================================================
4721 NCollection_CellFilter_Action BRepBuilderAPI_VertexInspector::Inspect (const Standard_Integer theTarget)
4723 /*gp_Pnt aPnt = gp_Pnt (myPoints.Value (theTarget - 1));
4724 if (aPnt.SquareDistance (gp_Pnt (myCurrent)) <= myTol)
4725 myResInd.Append (theTarget);*/
4727 const gp_XYZ& aPnt = myPoints.Value (theTarget - 1);
4728 Standard_Real aDx, aDy, aDz;
4729 aDx = myCurrent.X() - aPnt.X();
4730 aDy = myCurrent.Y() - aPnt.Y();
4731 aDz = myCurrent.Z() - aPnt.Z();
4733 if ((aDx*aDx <= myTol) && (aDy*aDy <= myTol) && (aDz*aDz <= myTol))
4734 myResInd.Append (theTarget);
4735 return CellFilter_Keep;
4738 //=======================================================================
4739 //function : Context
4741 //=======================================================================
4742 const Handle(BRepTools_ReShape)& BRepBuilderAPI_Sewing::GetContext() const
4747 //=======================================================================
4748 //function : SetContext
4750 //=======================================================================
4751 void BRepBuilderAPI_Sewing::SetContext(const Handle(BRepTools_ReShape)& theContext)
4753 myReShape = theContext;