1 // Copyright (C) 2007-2023 CEA, EDF, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File: BlockFix_UnionFaces.cxx
24 // Created: Tue Dec 7 17:15:42 2004
25 // Author: Pavel DURANDIN
27 #include <BlockFix_UnionFaces.hxx>
29 #include <Basics_OCCTVersion.hxx>
31 #include <ShapeAnalysis_WireOrder.hxx>
32 #include <ShapeAnalysis_Edge.hxx>
34 #include <ShapeBuild_Edge.hxx>
35 #include <ShapeBuild_ReShape.hxx>
37 #include <ShapeExtend_WireData.hxx>
38 #include <ShapeExtend_CompositeSurface.hxx>
40 #include <ShapeFix_Face.hxx>
41 #include <ShapeFix_ComposeShell.hxx>
42 #include <ShapeFix_SequenceOfWireSegment.hxx>
43 #include <ShapeFix_WireSegment.hxx>
44 #include <ShapeFix_Wire.hxx>
45 #include <ShapeFix_Edge.hxx>
47 #include <IntPatch_ImpImpIntersection.hxx>
49 #include <BRep_Tool.hxx>
50 #include <BRep_Builder.hxx>
51 #include <BRepTools.hxx>
52 #include <BRepTopAdaptor_TopolTool.hxx>
55 #include <TopExp_Explorer.hxx>
57 #include <TopTools_SequenceOfShape.hxx>
58 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
59 #include <TopTools_ListOfShape.hxx>
60 #include <TopTools_ListIteratorOfListOfShape.hxx>
61 #include <TopTools_MapOfShape.hxx>
62 #include <TopTools_MapIteratorOfMapOfShape.hxx>
65 #include <TopoDS_Edge.hxx>
66 #include <TopoDS_Wire.hxx>
67 #include <TopoDS_Face.hxx>
68 #include <TopoDS_Solid.hxx>
69 #include <TopoDS_Vertex.hxx>
70 #include <TopoDS_Shell.hxx>
71 #include <TopoDS_Iterator.hxx>
72 #include <TopoDS_Shape.hxx>
74 #include <TColGeom_HArray2OfSurface.hxx>
76 #if OCC_VERSION_LARGE < 0x07070000
77 #include <GeomAdaptor_HSurface.hxx>
79 #include <GeomAdaptor_Surface.hxx>
82 #include <GeomLib_IsPlanarSurface.hxx>
83 #include <Geom_Surface.hxx>
84 #include <Geom_Plane.hxx>
85 #include <Geom_OffsetSurface.hxx>
86 #include <Geom_SphericalSurface.hxx>
87 #include <Geom_CylindricalSurface.hxx>
88 #include <Geom_SurfaceOfRevolution.hxx>
89 #include <Geom_SurfaceOfLinearExtrusion.hxx>
90 #include <Geom_RectangularTrimmedSurface.hxx>
91 #include <BRepAdaptor_Surface.hxx>
92 #if OCC_VERSION_LARGE < 0x07070000
93 #include <BRepAdaptor_HSurface.hxx>
95 #include <LocalAnalysis_SurfaceContinuity.hxx>
96 #include <GeomConvert_ApproxSurface.hxx>
97 #include <Bnd_Box.hxx>
98 #include <BRepBndLib.hxx>
100 #include <Geom_TrimmedCurve.hxx>
101 #include <Geom_Curve.hxx>
102 #include <Geom_Line.hxx>
103 #include <Geom_Circle.hxx>
105 #include <Geom2d_Line.hxx>
108 #include <gp_Pnt2d.hxx>
110 #include <Standard_Failure.hxx>
111 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
113 //=======================================================================
114 //function : BlockFix_UnionFaces
116 //=======================================================================
117 BlockFix_UnionFaces::BlockFix_UnionFaces()
118 : myTolerance(Precision::Confusion()),
123 //=======================================================================
124 //function : GetTolerance
126 //=======================================================================
127 Standard_Real& BlockFix_UnionFaces::GetTolerance()
132 //=======================================================================
133 //function : GetOptimumNbFaces
135 //=======================================================================
136 Standard_Integer& BlockFix_UnionFaces::GetOptimumNbFaces()
138 return myOptimumNbFaces;
141 //=======================================================================
142 //function : AddOrdinaryEdges
143 //purpose : auxiliary
144 // adds edges from the shape to the sequence
145 // seams and equal edges are dropped
146 // Returns true if one of original edges dropped
147 //=======================================================================
148 static Standard_Boolean AddOrdinaryEdges(TopTools_SequenceOfShape& edges,
149 const TopoDS_Shape aShape,
150 Standard_Integer& anIndex)
153 TopTools_MapOfShape aNewEdges;
154 TopExp_Explorer exp(aShape,TopAbs_EDGE);
155 //add edges without seams
156 for(; exp.More(); exp.Next()) {
157 TopoDS_Shape edge = exp.Current();
158 if(aNewEdges.Contains(edge))
159 aNewEdges.Remove(edge);
164 Standard_Boolean isDropped = Standard_False;
165 //merge edges and drop seams
166 for(Standard_Integer i = 1; i <= edges.Length(); i++) {
167 TopoDS_Shape current = edges(i);
168 if(aNewEdges.Contains(current)) {
170 aNewEdges.Remove(current);
175 isDropped = Standard_True;
181 //add edges to the sequence
182 for(exp.ReInit(); exp.More(); exp.Next()) {
183 const TopoDS_Shape &anEdge = exp.Current();
185 if (aNewEdges.Contains(anEdge)) {
186 edges.Append(anEdge);
193 //=======================================================================
194 //function : ClearRts
195 //purpose : auxiliary
196 //=======================================================================
197 static Handle(Geom_Surface) ClearRts(const Handle(Geom_Surface)& aSurface)
199 if(aSurface->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
200 Handle(Geom_RectangularTrimmedSurface) rts =
201 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
202 return rts->BasisSurface();
207 //=======================================================================
208 //function : IsFacesOfSameSolids
209 //purpose : auxiliary
210 //=======================================================================
211 static Standard_Boolean IsFacesOfSameSolids
212 (const TopoDS_Face &theFace1,
213 const TopoDS_Face &theFace2,
214 const TopTools_IndexedDataMapOfShapeListOfShape &theMapFaceSolids)
216 Standard_Boolean isSame = Standard_False;
218 if (theMapFaceSolids.Contains(theFace1) &&
219 theMapFaceSolids.Contains(theFace2)) {
220 const TopTools_ListOfShape& aList1 = theMapFaceSolids.FindFromKey(theFace1);
221 const TopTools_ListOfShape& aList2 = theMapFaceSolids.FindFromKey(theFace2);
223 if (aList1.Extent() == aList2.Extent()) {
224 TopTools_ListIteratorOfListOfShape anIter1(aList1);
226 isSame = Standard_True;
228 for (; anIter1.More(); anIter1.Next()) {
229 const TopoDS_Shape &aSolid1 = anIter1.Value();
230 TopTools_ListIteratorOfListOfShape anIter2(aList2);
232 for (; anIter2.More(); anIter2.Next()) {
233 if (aSolid1.IsSame(anIter2.Value())) {
234 // Same solid is detected. Break the loop
239 if (!anIter2.More()) {
240 // No same solid is detected. Break the loop.
241 isSame = Standard_False;
251 //=======================================================================
252 //function : DefineMaxTolerance
253 //purpose : calculates maximum possible tolerance on edges of shape
254 //=======================================================================
255 static Standard_Real DefineMaxTolerance(const TopoDS_Shape& theShape)
257 Standard_Real aTol = Precision::Confusion();
259 Standard_Real MinSize = RealLast();
260 TopExp_Explorer Explo(theShape, TopAbs_EDGE);
261 for (; Explo.More(); Explo.Next())
263 const TopoDS_Edge& anEdge = TopoDS::Edge(Explo.Current());
265 BRepBndLib::Add(anEdge, aBox);
266 Standard_Real Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
267 aBox.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
268 Standard_Real MaxSize = Max(Xmax - Xmin, Max(Ymax - Ymin, Zmax - Zmin));
269 if (MaxSize < MinSize)
273 if (!Precision::IsInfinite(MinSize))
274 aTol = 0.1 * MinSize;
279 //=======================================================================
280 //function : IsTangentFaces
281 //purpose : decides: is edge on closed surface tangent or not
282 //=======================================================================
283 static Standard_Boolean IsTangentFaces(const TopoDS_Edge& theEdge,
284 const TopoDS_Face& theFace)
286 Standard_Real TolC0 = Max(0.001, 1.5*BRep_Tool::Tolerance(theEdge));
288 Standard_Real aFirst;
291 // Obtaining of pcurves of edge on two faces.
292 const Handle(Geom2d_Curve) aC2d1 = BRep_Tool::CurveOnSurface
293 (theEdge, theFace, aFirst, aLast);
294 TopoDS_Edge ReversedEdge = theEdge;
295 ReversedEdge.Reverse();
296 const Handle(Geom2d_Curve) aC2d2 = BRep_Tool::CurveOnSurface
297 (ReversedEdge, theFace, aFirst, aLast);
298 if (aC2d1.IsNull() || aC2d2.IsNull())
299 return Standard_False;
301 // Obtaining of two surfaces from adjacent faces.
302 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(theFace);
305 return Standard_False;
307 // Computation of the number of samples on the edge.
308 BRepAdaptor_Surface aBAS(theFace);
309 #if OCC_VERSION_LARGE < 0x07070000
310 Handle(BRepAdaptor_HSurface) aBAHS = new BRepAdaptor_HSurface(aBAS);
312 Handle(BRepAdaptor_Surface) aBAHS = new BRepAdaptor_Surface(aBAS);
314 Handle(BRepTopAdaptor_TopolTool) aTool = new BRepTopAdaptor_TopolTool(aBAHS);
315 Standard_Integer aNbSamples = aTool->NbSamples();
316 const Standard_Integer aNbSamplesMax = 23;
317 aNbSamples = Min(aNbSamplesMax, aNbSamples);
318 const Standard_Real aTolAngle = M_PI/18;
321 // Computation of the continuity.
323 Standard_Real aDelta = (aLast - aFirst)/(aNbSamples - 1);
324 Standard_Integer i, nbNotDone = 0;
326 for (i = 1, aPar = aFirst; i <= aNbSamples; i++, aPar += aDelta) {
327 if (i == aNbSamples) aPar = aLast;
329 LocalAnalysis_SurfaceContinuity aCont(aC2d1, aC2d2, aPar,
330 aSurf, aSurf, GeomAbs_G1,
331 0.001, TolC0, aTolAngle, 0.1, 0.1);
339 return Standard_False;
342 if (nbNotDone == aNbSamples)
343 return Standard_False;
345 return Standard_True;
348 //=======================================================================
349 //function : HasSeamEdge
350 //purpose : Detects if a face contains a seam edge
351 //=======================================================================
352 static Standard_Boolean HasSeamEdge(const TopoDS_Face& theFace)
354 TopExp_Explorer Explo(theFace, TopAbs_EDGE);
355 for (; Explo.More(); Explo.Next())
357 const TopoDS_Edge& anEdge = TopoDS::Edge(Explo.Current());
358 if (BRepTools::IsReallyClosed(anEdge, theFace))
359 return Standard_True;
362 return Standard_False;
366 //=======================================================================
367 //function : IsEdgeValidToMerge
368 //purpose : Edge is valid if it is not seam or if it is a seam and the face
369 // has another seam edge.
370 //=======================================================================
371 static Standard_Boolean IsEdgeValidToMerge(const TopoDS_Edge& theEdge,
372 const TopoDS_Face& theFace,
373 const Handle(Geom_Surface)& theSurface,
374 Standard_Boolean& theIsEdgeOnSeam,
375 Standard_Boolean& theToMakeUPeriodic,
376 Standard_Boolean& theToMakeVPeriodic)
378 Standard_Boolean isValid = Standard_True;
380 theIsEdgeOnSeam |= BRep_Tool::IsClosed(theEdge, theFace);
382 if (BRepTools::IsReallyClosed(theEdge, theFace)) {
383 // Mantis issue 0023451, now code corresponds to the comment to this method
384 isValid = Standard_False;
386 // This is a seam edge. Check if there are another seam edges on the face.
387 TopExp_Explorer anExp(theFace, TopAbs_EDGE);
389 for (; anExp.More(); anExp.Next()) {
390 const TopoDS_Shape &aShEdge = anExp.Current();
393 if (theEdge.IsSame(aShEdge)) {
397 // Check if this edge is a seam.
398 TopoDS_Edge anEdge = TopoDS::Edge(aShEdge);
400 if (BRep_Tool::IsClosed(anEdge, theFace)) {
401 // Mantis issue 0023451, now code corresponds to the comment to this method
402 //isValid = Standard_False;
403 isValid = Standard_True;
408 else if (theIsEdgeOnSeam)
410 Standard_Real fpar, lpar;
411 Handle(Geom2d_Curve) aPCurve = BRep_Tool::CurveOnSurface(theEdge, theFace, fpar, lpar);
412 gp_Pnt2d P2d1 = aPCurve->Value(fpar);
413 gp_Pnt2d P2d2 = aPCurve->Value(lpar);
414 if (!theSurface->IsUPeriodic() &&
415 theSurface->IsUClosed() &&
416 Abs(P2d1.X() - P2d2.X()) < Abs(P2d1.Y() - P2d2.Y()))
418 if (IsTangentFaces(theEdge, theFace))
419 theToMakeUPeriodic = Standard_True;
421 isValid = Standard_False;
423 if (!theSurface->IsVPeriodic() &&
424 theSurface->IsVClosed() &&
425 Abs(P2d1.Y() - P2d2.Y()) < Abs(P2d1.X() - P2d2.X()))
427 if (IsTangentFaces(theEdge, theFace))
428 theToMakeVPeriodic = Standard_True;
430 isValid = Standard_False;
437 //=======================================================================
440 //=======================================================================
441 TopoDS_Shape BlockFix_UnionFaces::Perform(const TopoDS_Shape& Shape)
443 // Fill Map of faces as keys and list of solids or shells as items.
444 TopTools_IndexedDataMapOfShapeListOfShape aMapFaceSoOrSh;
446 TopAbs_ShapeEnum aType = Shape.ShapeType();
448 if (aType != TopAbs_SHELL) {
449 aType = TopAbs_SOLID;
452 TopExp::MapShapesAndAncestors
453 (Shape, TopAbs_FACE, aType, aMapFaceSoOrSh);
455 // processing each solid
456 Handle(ShapeBuild_ReShape) aContext = new ShapeBuild_ReShape;
457 TopTools_MapOfShape aProcessed;
458 TopExp_Explorer exps;
459 for (exps.Init(Shape, aType); exps.More(); exps.Next()) {
460 TopoDS_Shape aSoOrSh = exps.Current();
462 // creating map of edge faces
463 TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
464 TopExp::MapShapesAndAncestors(aSoOrSh, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
466 Standard_Integer NbModif = 0;
467 Standard_Boolean hasFailed = Standard_False;
468 Standard_Real tol = Min(Max(Precision::Confusion(), myTolerance/10.), 0.1);
473 TopTools_MapOfShape mapF;
474 for (exp.Init(aSoOrSh, TopAbs_FACE); exp.More(); exp.Next()) {
475 if (mapF.Add(exp.Current()))
479 bool doUnion = ((myOptimumNbFaces == 0) ||
480 ((myOptimumNbFaces > 0) && (nbf > myOptimumNbFaces)));
482 // processing each face
484 for (exp.Init(aSoOrSh, TopAbs_FACE); exp.More() && doUnion; exp.Next()) {
485 TopoDS_Face aFace = TopoDS::Face(exp.Current().Oriented(TopAbs_FORWARD));
487 if (aProcessed.Contains(aFace)) {
491 Standard_Integer dummy;
492 TopTools_SequenceOfShape edges;
493 AddOrdinaryEdges(edges,aFace,dummy);
495 TopTools_SequenceOfShape faces;
498 //surface and location to construct result
499 TopLoc_Location aBaseLocation;
500 Handle(Geom_Surface) aBaseSurface = BRep_Tool::Surface(aFace,aBaseLocation);
501 aBaseSurface = ClearRts(aBaseSurface);
502 aBaseSurface = Handle(Geom_Surface)::DownCast(aBaseSurface->Copy());
503 Standard_Boolean ToMakeUPeriodic = Standard_False, ToMakeVPeriodic = Standard_False;
505 // find adjacent faces to union
507 for (i = 1; i <= edges.Length(); i++) {
508 TopoDS_Edge edge = TopoDS::Edge(edges(i));
509 Standard_Boolean IsEdgeOnSeam = Standard_False;
510 if (BRep_Tool::Degenerated(edge) ||
511 !IsEdgeValidToMerge(edge, aFace, aBaseSurface,
512 IsEdgeOnSeam, ToMakeUPeriodic, ToMakeVPeriodic))
515 const TopTools_ListOfShape& aList = aMapEdgeFaces.FindFromKey(edge);
516 TopTools_ListIteratorOfListOfShape anIter(aList);
517 for (; anIter.More(); anIter.Next()) {
518 TopoDS_Face anCheckedFace = TopoDS::Face(anIter.Value().Oriented(TopAbs_FORWARD));
519 if (anCheckedFace.IsSame(aFace))
522 if (aProcessed.Contains(anCheckedFace))
525 if (!IsEdgeValidToMerge(edge, anCheckedFace, aBaseSurface,
526 IsEdgeOnSeam, ToMakeUPeriodic, ToMakeVPeriodic)) {
531 // Check if faces belong to same solids.
532 if (!IsFacesOfSameSolids(aFace, anCheckedFace, aMapFaceSoOrSh)) {
536 if (IsSameDomain(aFace,anCheckedFace)) {
538 if (aList.Extent() != 2) {
539 // non mainfold case is not processed
543 //Prevent creating a face with parametric range more than period
545 (HasSeamEdge(aFace) || HasSeamEdge(anCheckedFace)))
549 TopoDS_Face aMockUpFace;
551 B.MakeFace(aMockUpFace,aBaseSurface,aBaseLocation,0.);
552 MovePCurves(aMockUpFace,anCheckedFace);
554 if (AddOrdinaryEdges(edges,aMockUpFace,dummy)) {
555 // sequence edges is modified
559 faces.Append(anCheckedFace);
560 aProcessed.Add(anCheckedFace);
566 // all faces collected in the sequence. Perform union of faces
567 if (faces.Length() > 1) {
571 if (ToMakeUPeriodic || ToMakeVPeriodic)
573 Handle(Geom_BSplineSurface) aBSplineSurface = Handle(Geom_BSplineSurface)::DownCast(aBaseSurface);
574 if (aBSplineSurface.IsNull())
576 Standard_Real aTol = 1.e-4;
577 GeomAbs_Shape aUCont = GeomAbs_C1, aVCont = GeomAbs_C1;
578 Standard_Integer degU = 14, degV = 14;
579 Standard_Integer nmax = 16;
580 Standard_Integer aPrec = 1;
581 GeomConvert_ApproxSurface Approximator(aBaseSurface,aTol,aUCont,aVCont,degU,degV,nmax,aPrec);
582 aBSplineSurface = Approximator.Surface();
586 aBSplineSurface->SetUPeriodic();
588 aBSplineSurface->SetVPeriodic();
590 aBaseSurface = aBSplineSurface;
592 B.MakeFace(aResult,aBaseSurface,aBaseLocation,0);
593 Standard_Integer nbWires = 0;
596 while (edges.Length()>0) {
598 Standard_Boolean isEdge3d = Standard_False;
600 TopTools_MapOfShape aVertices;
604 TopoDS_Edge anEdge = TopoDS::Edge(edges(1));
607 isEdge3d |= !BRep_Tool::Degenerated(anEdge);
610 TopExp::Vertices(anEdge,V1,V2);
614 Standard_Boolean isNewFound = Standard_False;
616 isNewFound = Standard_False;
617 for(Standard_Integer j = 1; j <= edges.Length(); j++) {
618 anEdge = TopoDS::Edge(edges(j));
619 TopExp::Vertices(anEdge,V1,V2);
620 if(aVertices.Contains(V1) || aVertices.Contains(V2)) {
621 isEdge3d |= !BRep_Tool::Degenerated(anEdge);
627 isNewFound = Standard_True;
630 } while (isNewFound);
632 // sorting any type of edges
633 aWire = TopoDS::Wire(aContext->Apply(aWire));
635 Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire(aWire,aResult,Precision::Confusion());
637 Standard_Boolean isDegRemoved = Standard_False;
638 if(!sfw->StatusReorder ( ShapeExtend_FAIL )) {
639 // clear degenerated edges if at least one with 3d curve exist
641 Handle(ShapeExtend_WireData) sewd = sfw->WireData();
642 for(Standard_Integer j = 1; j<=sewd->NbEdges();j++) {
643 TopoDS_Edge E = sewd->Edge(j);
644 if(BRep_Tool::Degenerated(E)) {
646 isDegRemoved = Standard_True;
653 sfw->FixDegenerated();
655 TopoDS_Wire aWireFixed = sfw->Wire();
656 aContext->Replace(aWire,aWireFixed);
657 // add resulting wire
659 B.Add(aResult,aWireFixed);
663 Handle(ShapeExtend_WireData) sbwd = sfw->WireData();
664 Standard_Integer nbEdges = sbwd->NbEdges();
665 // sort degenerated edges and create one edge instead of several ones
666 ShapeAnalysis_WireOrder sawo(Standard_False, 0);
667 ShapeAnalysis_Edge sae;
668 Standard_Integer aLastEdge = nbEdges;
669 for (Standard_Integer j = 1; j <= nbEdges; j++) {
671 //smh protection on NULL pcurve
672 Handle(Geom2d_Curve) c2d;
673 if (!sae.PCurve(sbwd->Edge(j),aResult,c2d,f,l)) {
677 sawo.Add(c2d->Value(f).XY(),c2d->Value(l).XY());
682 // constructing one degenerative edge
683 gp_XY aStart, anEnd, tmp;
684 Standard_Integer nbFirst = sawo.Ordered(1);
685 TopoDS_Edge anOrigE = TopoDS::Edge(sbwd->Edge(nbFirst).Oriented(TopAbs_FORWARD));
687 TopoDS_Vertex aDummyV;
688 TopoDS_Edge E = sbe.CopyReplaceVertices(anOrigE,aDummyV,aDummyV);
689 sawo.XY(nbFirst,aStart,tmp);
690 sawo.XY(sawo.Ordered(aLastEdge),tmp,anEnd);
692 gp_XY aVec = anEnd-aStart;
693 Handle(Geom2d_Line) aLine = new Geom2d_Line(aStart,gp_Dir2d(anEnd-aStart));
695 B.UpdateEdge(E,aLine,aResult,0.);
696 B.Range(E,aResult,0.,aVec.Modulus());
697 Handle(Geom_Curve) C3d;
698 B.UpdateEdge(E,C3d,0.);
699 B.Degenerated(E,Standard_True);
708 // perform substitution of face
709 aContext->Replace(aContext->Apply(aFace),aResult);
711 ShapeFix_Face sff (aResult);
712 //Initializing by tolerances
713 sff.SetPrecision(myTolerance);
714 sff.SetMinTolerance(tol);
715 Standard_Real MaxTol = DefineMaxTolerance(aResult);
716 sff.SetMaxTolerance(MaxTol);
718 sff.FixOrientationMode() = 0;
719 //sff.FixWireMode() = 0;
720 sff.SetContext(aContext);
721 // Applying the fixes
723 if(sff.Status(ShapeExtend_FAIL))
724 hasFailed = Standard_True;
726 // breaking down to several faces
727 TopoDS_Shape theResult = aContext->Apply(aResult);
728 for (TopExp_Explorer aFaceExp (theResult,TopAbs_FACE); aFaceExp.More(); aFaceExp.Next()) {
729 TopoDS_Face aCurrent = TopoDS::Face(aFaceExp.Current().Oriented(TopAbs_FORWARD));
730 Handle(TColGeom_HArray2OfSurface) grid = new TColGeom_HArray2OfSurface ( 1, 1, 1, 1 );
731 grid->SetValue ( 1, 1, aBaseSurface );
732 Handle(ShapeExtend_CompositeSurface) G = new ShapeExtend_CompositeSurface ( grid );
733 ShapeFix_ComposeShell CompShell;
734 CompShell.Init ( G, aBaseLocation, aCurrent, ::Precision::Confusion() );//myPrecision
735 CompShell.SetContext( aContext );
737 TopTools_SequenceOfShape parts;
738 ShapeFix_SequenceOfWireSegment wires;
739 for(TopExp_Explorer W_Exp(aCurrent,TopAbs_WIRE);W_Exp.More();W_Exp.Next()) {
740 Handle(ShapeExtend_WireData) sbwd =
741 new ShapeExtend_WireData ( TopoDS::Wire(W_Exp.Current() ));
742 ShapeFix_WireSegment seg ( sbwd, TopAbs_REVERSED );
746 CompShell.DispatchWires ( parts,wires );
747 for (Standard_Integer j=1; j <= parts.Length(); j++ ) {
748 ShapeFix_Face aFixOrient(TopoDS::Face(parts(j)));
749 aFixOrient.SetContext(aContext);
750 aFixOrient.FixOrientation();
753 TopoDS_Shape CompRes;
754 if ( faces.Length() !=1 ) {
757 for ( i=1; i <= parts.Length(); i++ )
758 B.Add ( S, parts(i) );
761 else CompRes = parts(1);
763 aContext->Replace(aCurrent,CompRes);
766 // remove the remaining faces
767 for(i = 2; i <= faces.Length(); i++)
768 aContext->Remove(faces(i));
770 } // end processing each face
772 //TopoDS_Shape aResult = Shape;
773 if (NbModif > 0 && !hasFailed) {
774 TopoDS_Shape aResult = aContext->Apply(aSoOrSh);
777 for (exp.Init(aResult,TopAbs_EDGE); exp.More(); exp.Next()) {
778 TopoDS_Edge E = TopoDS::Edge(exp.Current());
779 sfe.FixVertexTolerance (E);
780 // ptv add fix same parameter
781 sfe.FixSameParameter(E, myTolerance);
785 for (exp.Init(aSoOrSh, TopAbs_FACE); exp.More(); exp.Next()) {
786 TopoDS_Face aFace = TopoDS::Face(exp.Current().Oriented(TopAbs_FORWARD));
787 Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire;
788 sfw->SetContext(aContext);
789 sfw->SetPrecision(myTolerance);
790 sfw->SetMinTolerance(myTolerance);
791 sfw->SetMaxTolerance(Max(1.,myTolerance*1000.));
793 for (TopoDS_Iterator iter (aFace,Standard_False); iter.More(); iter.Next()) {
794 TopoDS_Shape aFaceCont = iter.Value();
795 if (!aFaceCont.IsNull() && aFaceCont.ShapeType() == TopAbs_WIRE) {
796 TopoDS_Wire wire = TopoDS::Wire(iter.Value());
803 } // end processing each solid
805 const TopoDS_Shape aResShape = aContext->Apply(Shape);
810 //=======================================================================
811 //function : IsSameDomain
813 //=======================================================================
814 bool getCylinder (Handle(Geom_Surface)& theInSurface, gp_Cylinder& theOutCylinder)
816 bool isCylinder = false;
818 if (theInSurface->IsKind(STANDARD_TYPE(Geom_CylindricalSurface))) {
819 Handle(Geom_CylindricalSurface) aGC = Handle(Geom_CylindricalSurface)::DownCast(theInSurface);
821 theOutCylinder = aGC->Cylinder();
824 else if (theInSurface->IsKind(STANDARD_TYPE(Geom_SurfaceOfRevolution))) {
825 Handle(Geom_SurfaceOfRevolution) aRS =
826 Handle(Geom_SurfaceOfRevolution)::DownCast(theInSurface);
827 Handle(Geom_Curve) aBasis = aRS->BasisCurve();
829 while (aBasis->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
830 Handle(Geom_TrimmedCurve) aTc =
831 Handle(Geom_TrimmedCurve)::DownCast(aBasis);
832 aBasis = aTc->BasisCurve();
835 if (aBasis->IsKind(STANDARD_TYPE(Geom_Line))) {
836 Handle(Geom_Line) aBasisLine = Handle(Geom_Line)::DownCast(aBasis);
837 gp_Dir aDir = aRS->Direction();
838 gp_Dir aBasisDir = aBasisLine->Position().Direction();
839 if (aBasisDir.IsParallel(aDir, Precision::Confusion())) {
840 // basis line is parallel to the revolution axis: it is a cylinder
841 gp_Pnt aLoc = aRS->Location();
842 Standard_Real aR = aBasisLine->Lin().Distance(aLoc);
843 gp_Ax3 aCylAx (aLoc, aDir);
845 theOutCylinder = gp_Cylinder(aCylAx, aR);
850 else if (theInSurface->IsKind(STANDARD_TYPE(Geom_SurfaceOfLinearExtrusion))) {
851 Handle(Geom_SurfaceOfLinearExtrusion) aLES =
852 Handle(Geom_SurfaceOfLinearExtrusion)::DownCast(theInSurface);
853 Handle(Geom_Curve) aBasis = aLES->BasisCurve();
855 while (aBasis->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
856 Handle(Geom_TrimmedCurve) aTc =
857 Handle(Geom_TrimmedCurve)::DownCast(aBasis);
858 aBasis = aTc->BasisCurve();
861 if (aBasis->IsKind(STANDARD_TYPE(Geom_Circle))) {
862 Handle(Geom_Circle) aBasisCircle = Handle(Geom_Circle)::DownCast(aBasis);
863 gp_Dir aDir = aLES->Direction();
864 gp_Dir aBasisDir = aBasisCircle->Position().Direction();
865 if (aBasisDir.IsParallel(aDir, Precision::Confusion())) {
866 // basis circle is normal to the extrusion axis: it is a cylinder
867 gp_Pnt aLoc = aBasisCircle->Location();
868 Standard_Real aR = aBasisCircle->Radius();
869 gp_Ax3 aCylAx (aLoc, aDir);
871 theOutCylinder = gp_Cylinder(aCylAx, aR);
882 Standard_Boolean BlockFix_UnionFaces::IsSameDomain(const TopoDS_Face& aFace,
883 const TopoDS_Face& aCheckedFace) const
885 //checking the same handles
886 TopLoc_Location L1, L2;
887 Handle(Geom_Surface) S1, S2;
889 S1 = BRep_Tool::Surface(aFace,L1);
890 S2 = BRep_Tool::Surface(aCheckedFace,L2);
892 if (S1 == S2 && L1 == L2)
895 // planar and cylindrical cases (IMP 20052)
896 Standard_Real aPrec = Precision::Confusion();
898 S1 = BRep_Tool::Surface(aFace);
899 S2 = BRep_Tool::Surface(aCheckedFace);
904 //Handle(Geom_OffsetSurface) aGOFS1, aGOFS2;
905 //aGOFS1 = Handle(Geom_OffsetSurface)::DownCast(S1);
906 //aGOFS2 = Handle(Geom_OffsetSurface)::DownCast(S2);
907 //if (!aGOFS1.IsNull()) S1 = aGOFS1->BasisSurface();
908 //if (!aGOFS2.IsNull()) S2 = aGOFS2->BasisSurface();
910 // case of two elementary surfaces: use OCCT tool
911 // elementary surfaces: ConicalSurface, CylindricalSurface,
912 // Plane, SphericalSurface and ToroidalSurface
913 if (S1->IsKind(STANDARD_TYPE(Geom_ElementarySurface)) &&
914 S2->IsKind(STANDARD_TYPE(Geom_ElementarySurface)))
916 #if OCC_VERSION_LARGE < 0x07070000
917 Handle(GeomAdaptor_HSurface) aGA1 = new GeomAdaptor_HSurface(S1);
918 Handle(GeomAdaptor_HSurface) aGA2 = new GeomAdaptor_HSurface(S2);
920 Handle(GeomAdaptor_Surface) aGA1 = new GeomAdaptor_Surface(S1);
921 Handle(GeomAdaptor_Surface) aGA2 = new GeomAdaptor_Surface(S2);
924 Handle(BRepTopAdaptor_TopolTool) aTT1 = new BRepTopAdaptor_TopolTool();
925 Handle(BRepTopAdaptor_TopolTool) aTT2 = new BRepTopAdaptor_TopolTool();
930 IntPatch_ImpImpIntersection anIIInt (aGA1, aTT1, aGA2, aTT2, aPrec, aPrec);
932 if (!anIIInt.IsDone() || anIIInt.IsEmpty())
935 return anIIInt.TangentFaces();
937 catch (Standard_Failure&) {
942 // case of two planar surfaces:
943 // all kinds of surfaces checked, including b-spline and bezier
944 GeomLib_IsPlanarSurface aPlanarityChecker1 (S1, aPrec);
945 if (aPlanarityChecker1.IsPlanar()) {
946 GeomLib_IsPlanarSurface aPlanarityChecker2 (S2, aPrec);
947 if (aPlanarityChecker2.IsPlanar()) {
948 gp_Pln aPln1 = aPlanarityChecker1.Plan();
949 gp_Pln aPln2 = aPlanarityChecker2.Plan();
951 if (aPln1.Position().Direction().IsParallel(aPln2.Position().Direction(), aPrec) &&
952 aPln1.Distance(aPln2) < aPrec) {
958 // case of two cylindrical surfaces, at least one of which is a swept surface
959 // swept surfaces: SurfaceOfLinearExtrusion, SurfaceOfRevolution
960 if ((S1->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) ||
961 S1->IsKind(STANDARD_TYPE(Geom_SweptSurface))) &&
962 (S2->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) ||
963 S2->IsKind(STANDARD_TYPE(Geom_SweptSurface))))
965 gp_Cylinder aCyl1, aCyl2;
966 if (getCylinder(S1, aCyl1) && getCylinder(S2, aCyl2)) {
967 if (fabs(aCyl1.Radius() - aCyl2.Radius()) < aPrec) {
968 gp_Dir aDir1 = aCyl1.Position().Direction();
969 gp_Dir aDir2 = aCyl2.Position().Direction();
970 if (aDir1.IsParallel(aDir2, aPrec)) {
971 gp_Pnt aLoc1 = aCyl1.Location();
972 gp_Pnt aLoc2 = aCyl2.Location();
973 gp_Vec aVec12 (aLoc1, aLoc2);
974 if (aVec12.SquareMagnitude() < aPrec*aPrec ||
975 aVec12.IsParallel(aDir1, aPrec)) {
986 //=======================================================================
987 //function : MovePCurves
989 //=======================================================================
990 void BlockFix_UnionFaces::MovePCurves(TopoDS_Face& aTarget,
991 const TopoDS_Face& aSource) const
994 for(TopExp_Explorer wexp(aSource,TopAbs_WIRE);wexp.More();wexp.Next()) {
995 Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire(TopoDS::Wire(wexp.Current()),
996 aTarget, Precision::Confusion());
998 Standard_Boolean isReoredFailed = sfw->StatusReorder ( ShapeExtend_FAIL );
999 sfw->FixEdgeCurves();
1004 sfw->FixDegenerated();
1006 // remove degenerated edges from not degenerated points
1007 ShapeAnalysis_Edge sae;
1008 Handle(ShapeExtend_WireData) sewd = sfw->WireData();
1009 for(Standard_Integer i = 1; i<=sewd->NbEdges();i++) {
1010 TopoDS_Edge E = sewd->Edge(i);
1011 if(BRep_Tool::Degenerated(E)&&!sae.HasPCurve(E,aTarget)) {
1017 TopoDS_Wire ResWire = sfw->Wire();
1018 B.Add(aTarget,ResWire);