1 // Copyright (C) 2017-20xx CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "GeomAlgoAPI_Filling.h"
23 #include <BRep_Tool.hxx>
24 #include <BRepBuilderAPI_MakeFace.hxx>
25 #include <Geom_BSplineCurve.hxx>
26 #include <Geom_BSplineSurface.hxx>
27 #include <Geom_TrimmedCurve.hxx>
28 #include <GeomAPI_PointsToBSplineSurface.hxx>
29 #include <GeomFill_AppSurf.hxx>
30 #include <GeomFill_Line.hxx>
31 #include <GeomFill_SectionGenerator.hxx>
32 #include <Precision.hxx>
33 #include <ShapeFix_Face.hxx>
34 #include <TopoDS_Edge.hxx>
35 #include <TopoDS_Face.hxx>
37 static void edgesToCurves(const std::list<GeomEdgePtr>& theEdges,
38 std::list<Handle(Geom_Curve)>& theCurves)
40 for (std::list<GeomEdgePtr>::const_iterator anIt = theEdges.begin();
41 anIt != theEdges.end(); ++anIt) {
42 const TopoDS_Edge& anEdge = (*anIt)->impl<TopoDS_Edge>();
43 if (BRep_Tool::Degenerated(anEdge))
47 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
49 aCurve = new Geom_TrimmedCurve(aCurve, aFirst, aLast);
50 if (anEdge.Orientation() == TopAbs_REVERSED)
53 theCurves.push_back(aCurve);
58 GeomAlgoAPI_Filling::GeomAlgoAPI_Filling(const int theMinDegree,
59 const int theMaxDegree,
61 const double theTol2D,
62 const double theTol3D)
63 : myMinDegree(theMinDegree),
64 myMaxDegree(theMaxDegree),
71 void GeomAlgoAPI_Filling::add(const GeomEdgePtr theEdge)
73 myConstraints.push_back(theEdge);
76 void GeomAlgoAPI_Filling::build(bool isApproximate)
78 if (myConstraints.size() <= 1) // not enough edges
82 buildByControlPoints();
87 void GeomAlgoAPI_Filling::buildByEdges()
89 GeomFill_SectionGenerator aSection;
91 // obtain section curves
92 std::list<Handle(Geom_Curve)> aCurves;
93 edgesToCurves(myConstraints, aCurves);
94 for (std::list<Handle(Geom_Curve)>::iterator anIt = aCurves.begin();
95 anIt != aCurves.end(); ++anIt)
96 aSection.AddCurve(*anIt);
98 // a 'tolerance' is used to compare 2 knots
99 aSection.Perform(Precision::PConfusion());
100 Handle(GeomFill_Line) aLine = new GeomFill_Line((int)aCurves.size());
102 // perform filling by sections
103 GeomFill_AppSurf anAppSurf(myMinDegree, myMaxDegree, myTol3D, myTol2D, myNbIter);
104 anAppSurf.Perform(aLine, aSection);
105 if (!anAppSurf.IsDone())
108 // build calculated surface
109 Standard_Integer UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots;
110 anAppSurf.SurfShape(UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots);
111 Handle(Geom_BSplineSurface) GBS = new Geom_BSplineSurface(
112 anAppSurf.SurfPoles(), anAppSurf.SurfWeights(), anAppSurf.SurfUKnots(), anAppSurf.SurfVKnots(),
113 anAppSurf.SurfUMults(), anAppSurf.SurfVMults(), anAppSurf.UDegree(), anAppSurf.VDegree());
119 TopoDS_Face aFace = BRepBuilderAPI_MakeFace(GBS, Precision::Confusion());
120 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
121 aShape->setImpl(new TopoDS_Shape(aFace));
126 static Handle(Geom_Curve) removeTrim(const Handle(Geom_Curve)& theCurve)
128 Handle(Geom_Curve) aCurve = theCurve;
129 Handle(Geom_TrimmedCurve) aTC = Handle(Geom_TrimmedCurve)::DownCast(aCurve);
130 while (!aTC.IsNull()) {
131 aCurve = aTC->BasisCurve();
132 aTC = Handle(Geom_TrimmedCurve)::DownCast(aCurve);
137 void GeomAlgoAPI_Filling::buildByControlPoints()
139 // obtain section curves
140 std::list<Handle(Geom_Curve)> aCurves;
141 edgesToCurves(myConstraints, aCurves);
143 // compute maximal number of poles in B-spline curves
145 std::list<Handle(Geom_Curve)>::iterator anIt = aCurves.begin();
146 for (; anIt != aCurves.end(); ++anIt) {
147 Handle(Geom_BSplineCurve) aBC = Handle(Geom_BSplineCurve)::DownCast(removeTrim(*anIt));
149 aMaxPoles = Max(aMaxPoles, aBC->NbPoles());
152 // prepare array of points for creation bspline surface
153 // size of this array: by U parameter - number of curves,
154 // by V parameter - determ using MaxNbPoles but it's
155 // value must be between 21(min) and 101(max)
156 int aNbSections = (int) aCurves.size();
157 int aNbPntInSection = Max(21, 2 * aMaxPoles - 1);
158 TColgp_Array2OfPnt aPoints(1, aNbSections, 1, aNbPntInSection);
159 anIt = aCurves.begin();
160 for (int i = 1; anIt != aCurves.end(); ++i, ++anIt) {
161 Handle(Geom_Curve) aC = *anIt;
162 double fp = aC->FirstParameter();
163 double lp = aC->LastParameter();
164 double dp = (lp - fp) / (aNbPntInSection - 1);
167 for (int j = 0; j < aNbPntInSection; j++) {
168 aC->D0(fp + dp * j, aPnt);
169 aPoints.SetValue(i, j+1, aPnt);
173 // convert a grid of points to B-spline surface
174 GeomAPI_PointsToBSplineSurface aPTB(aPoints, myMinDegree, myMaxDegree, GeomAbs_C2, myTol3D);
175 Handle(Geom_BSplineSurface) aBS = aPTB.Surface();
179 // fix face orientation
180 TopoDS_Face aFace = BRepBuilderAPI_MakeFace(aBS, Precision::Confusion());
181 Handle(ShapeFix_Face) aFix = new ShapeFix_Face(aFace);
183 aFix->FixOrientation();
184 aFace = aFix->Face();
187 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
188 aShape->setImpl(new TopoDS_Shape(aFace));