1 // Created on: 1996-04-23
2 // Created by: Jacques GOUSSARD
3 // Copyright (c) 1996-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.
18 #include <Bnd_Box.hxx>
19 #include <BRep_Builder.hxx>
20 #include <BRep_Tool.hxx>
21 #include <BRepAdaptor_Curve.hxx>
22 #include <BRepBndLib.hxx>
23 #include <BRepBuilderAPI.hxx>
24 #include <BRepFeat.hxx>
25 #include <BRepLib_MakeFace.hxx>
26 #include <BRepLProp.hxx>
27 #include <BRepTools.hxx>
28 #include <BRepTopAdaptor_FClass2d.hxx>
30 #include <Extrema_ExtPC.hxx>
31 #include <GCPnts_QuasiUniformDeflection.hxx>
32 #include <Geom2d_TrimmedCurve.hxx>
33 #include <Geom2dAdaptor_Curve.hxx>
34 #include <Geom_ConicalSurface.hxx>
35 #include <Geom_Curve.hxx>
36 #include <Geom_CylindricalSurface.hxx>
37 #include <Geom_Plane.hxx>
38 #include <Geom_RectangularTrimmedSurface.hxx>
39 #include <Geom_Surface.hxx>
40 #include <Geom_TrimmedCurve.hxx>
41 #include <GeomAdaptor_Curve.hxx>
42 #include <GeomProjLib.hxx>
44 #include <gp_Vec2d.hxx>
46 #include <LocOpe_BuildShape.hxx>
47 #include <LocOpe_CSIntersector.hxx>
48 #include <LocOpe_PntFace.hxx>
49 #include <Precision.hxx>
50 #include <TColGeom_SequenceOfCurve.hxx>
51 #include <TopExp_Explorer.hxx>
53 #include <TopoDS_Edge.hxx>
54 #include <TopoDS_Face.hxx>
55 #include <TopoDS_Shape.hxx>
56 #include <TopoDS_Shell.hxx>
57 #include <TopoDS_Solid.hxx>
58 #include <TopoDS_Vertex.hxx>
59 #include <TopTools_MapOfShape.hxx>
61 //#include <BRepAlgo_Cut.hxx>
62 #define NECHANTBARYC 11
64 //=======================================================================
65 //function : SampleEdges
67 //=======================================================================
69 void BRepFeat::SampleEdges(const TopoDS_Shape& theShape,
70 TColgp_SequenceOfPnt& theSeq)
72 LocOpe::SampleEdges(theShape,theSeq);
77 //=======================================================================
78 //function : Barycenter
79 //purpose : Calcul du barycentre des edges d'un shape
80 //=======================================================================
82 void BRepFeat::Barycenter(const TopoDS_Shape& S,
85 TopTools_MapOfShape theMap;
86 TopExp_Explorer exp(S,TopAbs_EDGE);
89 Standard_Real f,l,prm;
91 Standard_Integer i, nbp= 0;
93 for (; exp.More(); exp.Next()) {
94 const TopoDS_Edge& edg = TopoDS::Edge(exp.Current());
95 if (!theMap.Add(edg)) {
98 if (!BRep_Tool::Degenerated(edg)) {
99 C = BRep_Tool::Curve(edg,Loc,f,l);
100 C = Handle(Geom_Curve)::DownCast(C->Transformed(Loc.Transformation()));
101 for (i=1;i<NECHANTBARYC; i++) {
102 prm = ((NECHANTBARYC-i)*f + i*l)/NECHANTBARYC;
103 Bar += C->Value(prm).XYZ();
109 for (exp.Init(S,TopAbs_VERTEX); exp.More(); exp.Next()) {
110 if (theMap.Add(exp.Current())) {
111 Bar += (BRep_Tool::Pnt(TopoDS::Vertex(exp.Current()))).XYZ();
116 Bar.Divide((Standard_Real)nbp);
121 //=======================================================================
122 //function : ParametricBarycenter
123 //purpose : Calcul du barycentre "parametrique" shape sur une courbe
124 //=======================================================================
126 Standard_Real BRepFeat::ParametricBarycenter(const TopoDS_Shape& S,
127 const Handle(Geom_Curve)& CC)
129 TopTools_MapOfShape theMap;
130 TopExp_Explorer exp(S,TopAbs_EDGE);
132 Handle(Geom_Curve) C;
133 Standard_Real f,l,prm;
134 Standard_Integer i, nbp= 0;
135 GeomAdaptor_Curve TheCurve(CC);
137 extpc.Initialize(TheCurve,CC->FirstParameter(),CC->LastParameter());
138 Standard_Real parbar = 0;
140 for (; exp.More(); exp.Next()) {
141 const TopoDS_Edge& edg = TopoDS::Edge(exp.Current());
142 if (!theMap.Add(edg)) {
145 if (!BRep_Tool::Degenerated(edg)) {
146 C = BRep_Tool::Curve(edg,Loc,f,l);
147 C = Handle(Geom_Curve)::DownCast(C->Transformed(Loc.Transformation()));
148 for (i=1;i<NECHANTBARYC; i++) {
149 prm = ((NECHANTBARYC-i)*f + i*l)/NECHANTBARYC;
150 gp_Pnt pone = C->Value(prm);
151 // On projette sur CC
153 if (extpc.IsDone() && extpc.NbExt() >= 1) {
154 Standard_Real Dist2Min = extpc.SquareDistance(1);
155 Standard_Integer kmin = 1;
156 for (Standard_Integer k=2; k<=extpc.NbExt(); k++) {
157 Standard_Real Dist2 = extpc.SquareDistance(k);
158 if (Dist2 < Dist2Min) {
164 Standard_Real prmp = extpc.Point(kmin).Parameter();
171 for (exp.Init(S,TopAbs_VERTEX); exp.More(); exp.Next()) {
172 if (theMap.Add(exp.Current())) {
173 gp_Pnt pone = BRep_Tool::Pnt(TopoDS::Vertex(exp.Current()));
174 // On projette sur CC
176 if (extpc.IsDone() && extpc.NbExt() >= 1) {
177 Standard_Real Dist2Min = extpc.SquareDistance(1);
178 for (Standard_Integer k=2; k<=extpc.NbExt(); k++) {
179 Standard_Real Dist2 = extpc.SquareDistance(k);
180 if (Dist2 < Dist2Min) {
189 parbar /=((Standard_Real)nbp);
194 //=======================================================================
195 //function : ParametricBarycenter
196 //purpose : Calcul du barycentre "parametrique" shape sur une courbe
197 //=======================================================================
199 void BRepFeat::ParametricMinMax(const TopoDS_Shape& S,
200 const Handle(Geom_Curve)& CC,
201 Standard_Real& prmin,
202 Standard_Real& prmax,
203 Standard_Real& prbmin,
204 Standard_Real& prbmax,
205 Standard_Boolean& flag,
206 const Standard_Boolean theOri)
208 LocOpe_CSIntersector ASI(S);
209 TColGeom_SequenceOfCurve scur;
212 if(ASI.IsDone() && ASI.NbPoints(1) >=1) {
214 prmin = Min(ASI.Point(1,1).Parameter(),
215 ASI.Point(1, ASI.NbPoints(1)).Parameter());
216 prmax = Max(ASI.Point(1,1).Parameter(),
217 ASI.Point(1, ASI.NbPoints(1)).Parameter());
220 TopAbs_Orientation Ori = ASI.Point(1,1).Orientation();
221 if (Ori == TopAbs_FORWARD) {
222 prmin = ASI.Point(1,1).Parameter();
223 prmax = ASI.Point(1, ASI.NbPoints(1)).Parameter();
226 prmax = ASI.Point(1,1).Parameter();
227 prmin = ASI.Point(1, ASI.NbPoints(1)).Parameter();
230 flag = Standard_True;
235 flag = Standard_False;
238 TopTools_MapOfShape theMap;
239 TopExp_Explorer exp(S,TopAbs_EDGE);
241 Handle(Geom_Curve) C;
242 Standard_Real f,l,prm;
243 // Standard_Integer i, nbp= 0;
245 GeomAdaptor_Curve TheCurve(CC);
247 extpc.Initialize(TheCurve,CC->FirstParameter(),CC->LastParameter());
249 prbmax = RealFirst();
250 for (; exp.More(); exp.Next()) {
251 const TopoDS_Edge& edg = TopoDS::Edge(exp.Current());
252 if (!theMap.Add(edg)) {
255 if (!BRep_Tool::Degenerated(edg)) {
256 C = BRep_Tool::Curve(edg,Loc,f,l);
257 C = Handle(Geom_Curve)::DownCast(C->Transformed(Loc.Transformation()));
258 for (i=1;i<NECHANTBARYC; i++) {
259 prm = ((NECHANTBARYC-i)*f + i*l)/NECHANTBARYC;
260 gp_Pnt pone = C->Value(prm);
261 // On projette sur CC
263 if (extpc.IsDone() && extpc.NbExt() >= 1) {
264 Standard_Real Dist2Min = extpc.SquareDistance(1);
265 Standard_Integer kmin = 1;
266 for (Standard_Integer k=2; k<=extpc.NbExt(); k++) {
267 Standard_Real Dist2 = extpc.SquareDistance(k);
268 if (Dist2 < Dist2Min) {
273 Standard_Real prmp = extpc.Point(kmin).Parameter();
274 if (prmp <= prbmin) {
277 if (prmp >= prbmax) {
285 for (exp.Init(S,TopAbs_VERTEX); exp.More(); exp.Next()) {
286 if (theMap.Add(exp.Current())) {
287 gp_Pnt pone = BRep_Tool::Pnt(TopoDS::Vertex(exp.Current()));
288 // On projette sur CC
290 if (extpc.IsDone() && extpc.NbExt() >= 1) {
291 Standard_Real Dist2Min = extpc.SquareDistance(1);
292 Standard_Integer kmin = 1;
293 for (Standard_Integer k=2; k<=extpc.NbExt(); k++) {
294 Standard_Real Dist2 = extpc.SquareDistance(k);
295 if (Dist2 < Dist2Min) {
300 Standard_Real prmp = extpc.Point(kmin).Parameter();
301 if (prmp <= prbmin) {
304 if (prmp >= prbmax) {
315 //=======================================================================
318 //=======================================================================
320 static Standard_Boolean IsIn (BRepTopAdaptor_FClass2d& FC,
321 Geom2dAdaptor_Curve AC)
323 Standard_Real Def = 100*Precision::Confusion();
324 GCPnts_QuasiUniformDeflection QU(AC,Def);
326 for (Standard_Integer i = 1; i <= QU.NbPoints(); i++) {
327 gp_Pnt2d P = AC.Value(QU.Parameter(i));
328 if (FC.Perform(P, Standard_False) == TopAbs_OUT) {
329 return Standard_False;
332 return Standard_True;
336 //=======================================================================
337 //function : PutInBoundsU
338 //purpose : Recadre la courbe 2d dans les bounds de la face
339 //=======================================================================
344 static void PutInBoundsU (Standard_Real umin,
347 Standard_Real period,
350 Handle(Geom2d_Curve)& C2d)
352 gp_Pnt2d Pf = C2d->Value(f);
353 gp_Pnt2d Pl = C2d->Value(l);
354 gp_Pnt2d Pm = C2d->Value(0.34*f + 0.66*l);
355 Standard_Real minC = Min(Pf.X(),Pl.X()); minC = Min(minC,Pm.X());
356 Standard_Real maxC = Max(Pf.X(),Pl.X()); maxC = Max(maxC,Pm.X());
357 Standard_Real du = 0.;
358 if (minC< umin - eps) {
359 du = (int((umin - minC)/period) + 1)*period;
361 if (minC > umax + eps) {
362 du = -(int((minC - umax)/period) + 1)*period;
367 minC += du; maxC += du;
369 // Ajuste au mieux la courbe dans le domaine.
370 if (maxC > umax +100*eps) {
371 Standard_Real d1 = maxC - umax;
372 Standard_Real d2 = umin - minC + period;
373 if (d2 < d1) du =-period;
382 //=======================================================================
383 //function : PutInBoundsU
384 //purpose : Recadre la courbe 2d dans les bounds de la face
385 //=======================================================================
390 static void PutInBoundsV (Standard_Real vmin,
393 Standard_Real period,
396 Handle(Geom2d_Curve)& C2d)
398 gp_Pnt2d Pf = C2d->Value(f);
399 gp_Pnt2d Pl = C2d->Value(l);
400 gp_Pnt2d Pm = C2d->Value(0.34*f + 0.66*l);
401 Standard_Real minC = Min(Pf.Y(),Pl.Y()); minC = Min(minC,Pm.Y());
402 Standard_Real maxC = Max(Pf.Y(),Pl.Y()); maxC = Max(maxC,Pm.Y());
403 Standard_Real dv = 0.;
404 if (minC< vmin - eps) {
405 dv = (int((vmin - minC)/period) + 1)*period;
407 if (minC > vmax + eps) {
408 dv = -(int((minC - vmax)/period) + 1)*period;
413 minC += dv; maxC += dv;
415 // Ajuste au mieux la courbe dans le domaine.
416 if (maxC > vmax +100*eps) {
417 Standard_Real d1 = maxC - vmax;
418 Standard_Real d2 = vmin - minC + period;
419 if (d2 < d1) dv =-period;
428 //=======================================================================
429 //function : IsInside
431 //=======================================================================
434 Standard_Boolean BRepFeat::IsInside(const TopoDS_Face& F1,
435 const TopoDS_Face& F2)
438 exp.Init(F1, TopAbs_EDGE);
440 Standard_Real umin,umax,vmin,vmax, uperiod=0, vperiod=0;
441 Standard_Integer flagu = 0, flagv = 0;
442 TopLoc_Location L; // Recup S avec la location pour eviter la copie.
443 Handle (Geom_Surface) S = BRep_Tool::Surface(F2);
444 // Standard_Real periodu, periodv;
445 BRepTools::UVBounds(F2,umin,umax,vmin,vmax);
447 if (S->IsUPeriodic()) {
449 uperiod = S->UPeriod();
452 if (S->IsVPeriodic()) {
454 vperiod = S->VPeriod();
456 TopoDS_Shape aLocalShape = F2.Oriented(TopAbs_FORWARD);
457 BRepTopAdaptor_FClass2d FC (TopoDS::Face(aLocalShape),Precision::Confusion());
458 // BRepTopAdaptor_FClass2d FC (TopoDS::Face(F2.Oriented(TopAbs_FORWARD)),
459 // Precision::Confusion());
460 for(; exp.More(); exp.Next()) {
462 Handle(Geom_Curve) C0 = BRep_Tool::Curve(TopoDS::Edge(exp.Current()),f1,l1);
463 Handle(Geom2d_Curve) C = GeomProjLib::Curve2d(C0,f1,l1,S);
464 TopoDS_Edge E = TopoDS::Edge(exp.Current());
465 if(flagu == 1 || flagv == 1) {
466 Standard_Real eps = BRep_Tool::Tolerance(E);
467 BRep_Tool::Range(E,f1,l1);
468 if(flagu == 1) PutInBoundsU(umin, umax, eps, uperiod, f1, l1, C);
469 if(flagv == 1) PutInBoundsV(vmin, vmax, eps, vperiod, f1, l1, C);
471 Geom2dAdaptor_Curve AC(C,f1,l1);
473 return Standard_False;
476 return Standard_True;
481 //=======================================================================
482 //function : FaceUntil
484 //=======================================================================
487 void BRepFeat::FaceUntil(const TopoDS_Shape& Sbase,
491 BRepBndLib::Add(Sbase,B);
492 Standard_Real x[2], y[2], z[2];
493 B.Get(x[0],y[0],z[0],x[1],y[1],z[1]);
494 Standard_Real diam = 10.*Sqrt(B.SquareExtent());
496 Handle(Geom_Surface) s = BRep_Tool::Surface(FUntil);
497 Handle(Standard_Type) styp = s->DynamicType();
498 if (styp == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
499 s = Handle(Geom_RectangularTrimmedSurface)::DownCast(s)->BasisSurface();
500 styp = s->DynamicType();
502 Handle(Geom_RectangularTrimmedSurface) str;
503 if (styp == STANDARD_TYPE(Geom_Plane)) {
504 gp_Pln aPln = Handle(Geom_Plane)::DownCast(s)->Pln();
505 Standard_Real u, v, umin = RealLast(), umax = -umin,
506 vmin = RealLast(), vmax = -vmin;
507 for(Standard_Integer i = 0 ; i < 2; i++)
509 for(Standard_Integer j = 0; j < 2; j++)
511 for(Standard_Integer k = 0; k < 2; k++)
513 gp_Pnt aP(x[i], y[j], z[k]);
514 ElSLib::Parameters(aPln, aP, u, v);
530 str = new Geom_RectangularTrimmedSurface
531 (s, umin, umax, vmin, vmax, Standard_True, Standard_True);
533 else if (styp == STANDARD_TYPE(Geom_CylindricalSurface)) {
534 gp_Cylinder aCyl = Handle(Geom_CylindricalSurface)::DownCast(s)->Cylinder();
535 Standard_Real u, v, vmin = RealLast(), vmax = -vmin;
536 for(Standard_Integer i = 0 ; i < 2; i++)
538 for(Standard_Integer j = 0; j < 2; j++)
540 for(Standard_Integer k = 0; k < 2; k++)
542 gp_Pnt aP(x[i], y[j], z[k]);
543 ElSLib::Parameters(aCyl, aP, u, v);
553 str = new Geom_RectangularTrimmedSurface
554 (s, vmin, vmax, Standard_False, Standard_True);
556 else if (styp == STANDARD_TYPE(Geom_ConicalSurface)) {
557 gp_Cone aCon = Handle(Geom_ConicalSurface)::DownCast(s)->Cone();
558 Standard_Real u, v, vmin = RealLast(), vmax = -vmin;
559 for(Standard_Integer i = 0 ; i < 2; i++)
561 for(Standard_Integer j = 0; j < 2; j++)
563 for(Standard_Integer k = 0; k < 2; k++)
565 gp_Pnt aP(x[i], y[j], z[k]);
566 ElSLib::Parameters(aCon, aP, u, v);
576 str = new Geom_RectangularTrimmedSurface
577 (s, vmin, vmax, Standard_False, Standard_True);
584 FUntil = BRepLib_MakeFace(str, Precision::Confusion());
589 //=======================================================================
592 //=======================================================================
594 TopoDS_Solid BRepFeat::Tool(const TopoDS_Shape& SRef,
595 const TopoDS_Face& Fac,
596 const TopAbs_Orientation Orf)
598 TopTools_ListOfShape lfaces;
599 // for (TopExp_Explorer exp(SRef,TopAbs_FACE); exp.More(); exp.Next()) {
600 TopExp_Explorer exp(SRef,TopAbs_FACE) ;
601 for ( ; exp.More(); exp.Next()) {
602 if (exp.Current().ShapeType() == TopAbs_FACE) {
603 lfaces.Append(exp.Current());
607 LocOpe_BuildShape bs(lfaces);
608 const TopoDS_Shape& Res = bs.Shape();
610 if (Res.ShapeType() == TopAbs_SHELL) {
612 Sh = TopoDS::Shell(Res);
614 else if (Res.ShapeType() == TopAbs_SOLID) {
615 exp.Init(Res,TopAbs_SHELL);
616 Sh = TopoDS::Shell(exp.Current());
629 Sh.Orientation(TopAbs_FORWARD);
631 TopAbs_Orientation orient = TopAbs_FORWARD;
633 for (exp.Init(Sh,TopAbs_FACE); exp.More(); exp.Next()) {
634 if (exp.Current().IsSame(Fac)) {
635 orient = exp.Current().Orientation();
640 Standard_Boolean reverse = Standard_False;
641 if ((orient == Fac.Orientation() && Orf == TopAbs_REVERSED) ||
642 (orient != Fac.Orientation() && Orf == TopAbs_FORWARD)) {
643 reverse = Standard_True;
658 //=======================================================================
660 //purpose : Print the error Description of a StatusError on a stream.
661 //=======================================================================
663 Standard_OStream& BRepFeat::Print(const BRepFeat_StatusError se,
670 case BRepFeat_BadDirect :
671 s << "Directions must be opposite";
673 case BRepFeat_BadIntersect :
674 s << "Intersection failure";
676 case BRepFeat_EmptyBaryCurve :
677 s << "Empty BaryCurve";
679 case BRepFeat_EmptyCutResult :
680 s << "Failure in Cut : Empty resulting shape";
682 case BRepFeat_FalseSide :
683 s << "Verify plane and wire orientation";
685 case BRepFeat_IncDirection :
686 s << "Incoherent Direction for shapes From and Until";
688 case BRepFeat_IncSlidFace :
689 s << "Sliding face not in Base shape";
691 case BRepFeat_IncParameter :
692 s << "Incoherent Parameter : shape Until before shape From";
694 case BRepFeat_IncTypes :
695 s << "Invalid option for faces From and Until : 1 Support and 1 not";
697 case BRepFeat_IntervalOverlap :
698 s << "Shapes From and Until overlap";
700 case BRepFeat_InvFirstShape :
701 s << "Invalid First shape : more than 1 face";
703 case BRepFeat_InvOption :
704 s << "Invalid option";
706 case BRepFeat_InvShape :
707 s << "Invalid shape";
709 case BRepFeat_LocOpeNotDone :
710 s << "Local Operation not done";
712 case BRepFeat_LocOpeInvNotDone :
713 s << "Local Operation : intersection line conflict";
715 case BRepFeat_NoExtFace :
716 s << "No Extreme faces";
718 case BRepFeat_NoFaceProf :
719 s << "No Face Profile";
721 case BRepFeat_NoGluer :
722 s << "Gluer Failure";
724 case BRepFeat_NoIntersectF :
725 s << "No intersection between Feature and shape From";
727 case BRepFeat_NoIntersectU :
728 s << "No intersection between Feature and shape Until";
730 case BRepFeat_NoParts :
731 s << "No parts of tool kept";
733 case BRepFeat_NoProjPt :
734 s << "No projection points";
736 case BRepFeat_NotInitialized :
737 s << "Fields not initialized";
739 case BRepFeat_NotYetImplemented :
740 s << "Not yet implemented";
742 case BRepFeat_NullRealTool :
743 s << "Real Tool : Null DPrism";
745 case BRepFeat_NullToolF :
746 s << "Null Tool : Invalid type for shape Form";
748 case BRepFeat_NullToolU :
749 s << "Null Tool : Invalid type for shape Until";