1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, 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 #include <Standard_Stream.hxx>
25 #include <GEOMImpl_FillingDriver.hxx>
26 #include <GEOMImpl_IFilling.hxx>
27 #include <GEOMImpl_Types.hxx>
29 #include <GEOM_Function.hxx>
31 #include <Basics_OCCTVersion.hxx>
33 #include <ShapeFix_Face.hxx>
35 #include <BRep_Tool.hxx>
36 #include <BRepAlgo.hxx>
37 #include <BRep_Builder.hxx>
38 #include <BRepBuilderAPI_MakeFace.hxx>
39 #include <BRepBuilderAPI_Copy.hxx>
42 #include <TopExp_Explorer.hxx>
44 #include <TopoDS_Compound.hxx>
45 #include <TopoDS_Edge.hxx>
46 #include <TopoDS_Shape.hxx>
47 #include <TopoDS_Vertex.hxx>
49 #include <Geom_Curve.hxx>
50 #include <Geom_Surface.hxx>
51 #include <Geom_TrimmedCurve.hxx>
52 #include <Geom_Line.hxx>
53 #include <Geom_Circle.hxx>
54 #include <Geom_Ellipse.hxx>
55 #include <Geom_BezierCurve.hxx>
56 #include <Geom_BSplineCurve.hxx>
57 #include <Geom_BSplineSurface.hxx>
58 #include <GeomFill_Line.hxx>
59 #include <GeomFill_AppSurf.hxx>
60 #include <GeomFill_SectionGenerator.hxx>
61 #include <GeomAPI_PointsToBSplineSurface.hxx>
62 #include <GeomAPI_PointsToBSpline.hxx>
64 #include <TColGeom_SequenceOfCurve.hxx>
66 #include <TColgp_SequenceOfPnt.hxx>
67 #include <TColgp_Array1OfPnt.hxx>
69 #include <Precision.hxx>
71 #include <Standard_ConstructionError.hxx>
73 //=======================================================================
76 //=======================================================================
77 const Standard_GUID& GEOMImpl_FillingDriver::GetID()
79 static Standard_GUID aFillingDriver ("FF1BBB62-5D14-4df2-980B-3A668264EA16");
80 return aFillingDriver;
83 //=======================================================================
84 //function : GEOMImpl_FillingDriver
86 //=======================================================================
88 GEOMImpl_FillingDriver::GEOMImpl_FillingDriver()
92 //=======================================================================
95 //=======================================================================
96 Standard_Integer GEOMImpl_FillingDriver::Execute(TFunction_Logbook& log) const
98 if (Label().IsNull()) return 0;
99 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
100 if (aFunction.IsNull()) return 0;
101 if (aFunction->GetType() != BASIC_FILLING) return 0;
103 GEOMImpl_IFilling IF (aFunction);
104 Handle(GEOM_Function) aShapeFunction = IF.GetShape();
105 if (aShapeFunction.IsNull()) return 0;
108 BRepBuilderAPI_Copy Copy (aShapeFunction->GetValue());
110 aShape = Copy.Shape();
112 if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND) return 0;
114 Standard_Integer mindeg = IF.GetMinDeg();
115 Standard_Integer maxdeg = IF.GetMaxDeg();
116 Standard_Real tol3d = IF.GetTol3D();
117 Standard_Boolean isApprox = IF.GetApprox();
119 if (mindeg > maxdeg) {
120 Standard_RangeError::Raise("Minimal degree can not be more than maximal degree");
123 /* we verify the contents of the shape */
125 TopoDS_Shape Scurrent;
126 Standard_Real First, Last;
127 Handle(Geom_Curve) C;
129 TopoDS_Compound aComp;
131 B.MakeCompound(aComp);
133 // 1. Convert argument wires, if any, into BSpline edges
134 TopoDS_Iterator It (aShape);
135 for (; It.More(); It.Next()) {
136 Scurrent = It.Value();
137 if (Scurrent.ShapeType() != TopAbs_EDGE) {
139 if (Scurrent.ShapeType() == TopAbs_WIRE)
141 const TopoDS_Wire& CurWire = TopoDS::Wire(Scurrent);
142 NewEdge = BRepAlgo::ConcatenateWireC0(CurWire);
144 if (NewEdge.IsNull()) {
145 Standard_ConstructionError::Raise("The argument compound must contain only edges");
149 B.Add(aComp,Scurrent);
153 // 2. The surface construction
155 // make filling as in old version of SALOME (before 4.1.1)
157 Standard_Real tol2d = IF.GetTol2D();
158 Standard_Integer nbiter = IF.GetNbIter();
159 Standard_Integer aMethod = IF.GetMethod();
161 GeomFill_SectionGenerator Section;
162 Standard_Integer i = 0;
163 Handle(Geom_Curve) aLastC;
165 for (Ex.Init(aShape, TopAbs_EDGE); Ex.More(); Ex.Next()) {
166 Scurrent = Ex.Current();
167 if (Scurrent.IsNull() || Scurrent.ShapeType() != TopAbs_EDGE) return 0;
168 if (BRep_Tool::Degenerated(TopoDS::Edge(Scurrent))) continue;
169 C = BRep_Tool::Curve(TopoDS::Edge(Scurrent), First, Last);
170 //if (Scurrent.Orientation() == TopAbs_REVERSED)
171 // // Mantis isuue 0020659: consider the orientation of the edges
172 // C = new Geom_TrimmedCurve(C, Last, First);
174 // C = new Geom_TrimmedCurve(C, First, Last);
175 C = new Geom_TrimmedCurve(C, First, Last);
180 if (aMethod == 1 && Scurrent.Orientation() == TopAbs_REVERSED) {
183 else if (aMethod == 2) {
189 double d1 = PL1.Distance(P1) + PL2.Distance(P2);
190 double d2 = PL1.Distance(P2) + PL2.Distance(P1);
207 /* a 'tolerance' is used to compare 2 knots : see GeomFill_Generator.cdl */
208 Section.Perform(Precision::Confusion());
209 Handle(GeomFill_Line) Line = new GeomFill_Line(i);
211 GeomFill_AppSurf App (mindeg, maxdeg, tol3d, tol2d, nbiter); /* user parameters */
212 App.Perform(Line, Section);
214 if (!App.IsDone()) return 0;
215 Standard_Integer UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots;
216 App.SurfShape(UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots);
217 Handle(Geom_BSplineSurface) GBS = new Geom_BSplineSurface
218 (App.SurfPoles(), App.SurfWeights(), App.SurfUKnots(), App.SurfVKnots(),
219 App.SurfUMults(), App.SurfVMults(), App.UDegree(), App.VDegree());
221 if (GBS.IsNull()) return 0;
222 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
223 aShape = BRepBuilderAPI_MakeFace(GBS, Precision::Confusion());
225 aShape = BRepBuilderAPI_MakeFace(GBS);
229 // implemented by skl 20.03.2008 for bug 16568
230 // make approximation - try to create bspline surface
231 // using GeomAPI_PointsToBSplineSurface
233 TColGeom_SequenceOfCurve aSeq;
236 // add curves from edges to sequence and find maximal
237 // number of poles if some of them are bsplines
238 for (Ex.Init(aShape, TopAbs_EDGE); Ex.More(); Ex.Next()) {
239 Scurrent = Ex.Current();
240 if (Scurrent.IsNull() || Scurrent.ShapeType() != TopAbs_EDGE) return 0;
241 if (BRep_Tool::Degenerated(TopoDS::Edge(Scurrent))) continue;
242 C = BRep_Tool::Curve(TopoDS::Edge(Scurrent), First, Last);
243 Handle(Geom_TrimmedCurve) TC = Handle(Geom_TrimmedCurve)::DownCast(C);
245 Handle(Geom_BSplineCurve) BC = Handle(Geom_BSplineCurve)::DownCast(C);
247 MaxNbPoles = Max(MaxNbPoles,BC->NbPoles());
251 Handle(Geom_BSplineCurve) BC = Handle(Geom_BSplineCurve)::DownCast(TC->BasisCurve());
253 Handle(Geom_TrimmedCurve) TC1 = Handle(Geom_TrimmedCurve)::DownCast(TC->BasisCurve());
255 BC = Handle(Geom_BSplineCurve)::DownCast(TC1->BasisCurve());
259 MaxNbPoles = Max(MaxNbPoles,BC->NbPoles());
264 // prepare array of points for creation bspline surface
265 // size of this array: by U parameter - number of curves,
266 // by V parameter - determ using MaxNbPoles but it's
267 // value must be between 21(min) and 101(max)
268 int nbc = aSeq.Length();
269 int nbp = Max(21, 2*MaxNbPoles-1);
271 // commented for Mantis issue 0021541
272 //if (nbp > 101) nbp = 101;
274 TColgp_Array2OfPnt Points (1, nbc, 1, nbp);
276 for (; ic <= nbc; ic++) {
277 Handle(Geom_Curve) C = aSeq.Value(ic);
278 double fp = C->FirstParameter();
279 double lp = C->LastParameter();
280 double dp = (lp-fp)/(nbp-1);
283 for (; j < nbp; j++) {
285 Points.SetValue(ic, j+1, P);
288 GeomAPI_PointsToBSplineSurface PTB (Points, mindeg, maxdeg, GeomAbs_C2, tol3d);
289 Handle(Geom_BSplineSurface) BS = PTB.Surface();
290 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
291 BRepBuilderAPI_MakeFace BB (BS, Precision::Confusion());
293 BRepBuilderAPI_MakeFace BB (BS);
295 TopoDS_Face NewF = BB.Face();
296 Handle(ShapeFix_Face) sff = new ShapeFix_Face (NewF);
298 sff->FixOrientation();
299 aShape = sff->Face();
302 /* We test the validity of resulting shape */
303 if (!BRepAlgo::IsValid((aShape))) {
304 Standard_ConstructionError::Raise("Algorithm has produced an invalid shape result");
308 aFunction->SetValue(aShape);
310 log.SetTouched(Label());
314 //================================================================================
316 * \brief Returns a name of creation operation and names and values of creation parameters
318 //================================================================================
320 bool GEOMImpl_FillingDriver::
321 GetCreationInformation(std::string& theOperationName,
322 std::vector<GEOM_Param>& theParams)
324 if (Label().IsNull()) return 0;
325 Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
327 GEOMImpl_IFilling aCI( function );
328 Standard_Integer aType = function->GetType();
330 theOperationName = "FILLING";
335 AddParam( theParams, "Input compound", aCI.GetShape() );
336 AddParam( theParams, "Method", aCI.GetMethod() );
337 const char* method[3] =
338 { "Standard", "Use edges orientation", "Correct edges orientation" };
339 if ( 0 <= aCI.GetMethod() && aCI.GetMethod() < 3 )
340 theParams[1] << " = " << method[ aCI.GetMethod() ];
341 AddParam( theParams, "Min deg", aCI.GetMinDeg() );
342 AddParam( theParams, "Max deg", aCI.GetMaxDeg() );
343 AddParam( theParams, "Nb. Iter", aCI.GetNbIter() );
344 AddParam( theParams, "Tol. 2D", aCI.GetTol2D() );
345 AddParam( theParams, "Tol. 3D", aCI.GetTol3D() );
346 AddParam( theParams, "Approximation", aCI.GetApprox() );
356 IMPLEMENT_STANDARD_HANDLE (GEOMImpl_FillingDriver,GEOM_BaseDriver);
357 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_FillingDriver,GEOM_BaseDriver);