Salome HOME
43e206ef1bdb021868787b2ee9b2f95572e70356
[modules/geom.git] / src / GEOMImpl / GEOMImpl_FillingDriver.cxx
1
2 #include <Standard_Stream.hxx>
3
4 #include <GEOMImpl_FillingDriver.hxx>
5 #include <GEOM_Function.hxx>
6 #include <GEOMImpl_IFilling.hxx>
7 #include <GEOMImpl_Types.hxx>
8
9 #include <BRep_Tool.hxx>
10 #include <BRepAlgo.hxx>
11 #include <BRepBuilderAPI_MakeFace.hxx>
12
13 #include <TopAbs.hxx>
14 #include <TopoDS.hxx>
15 #include <TopoDS_Shape.hxx>
16 #include <TopExp_Explorer.hxx>
17
18 #include <Geom_Curve.hxx>
19 #include <Geom_Surface.hxx>
20 #include <Geom_TrimmedCurve.hxx>
21 #include <Geom_BSplineSurface.hxx>
22 #include <GeomFill_Line.hxx>
23 #include <GeomFill_AppSurf.hxx>
24 #include <GeomFill_SectionGenerator.hxx>
25
26 #include <Precision.hxx>
27 #include <Standard_ConstructionError.hxx>
28
29 //=======================================================================
30 //function : GetID
31 //purpose  :
32 //=======================================================================
33 const Standard_GUID& GEOMImpl_FillingDriver::GetID()
34 {
35   static Standard_GUID aFillingDriver ("FF1BBB62-5D14-4df2-980B-3A668264EA16");
36   return aFillingDriver;
37 }
38
39
40 //=======================================================================
41 //function : GEOMImpl_FillingDriver
42 //purpose  :
43 //=======================================================================
44
45 GEOMImpl_FillingDriver::GEOMImpl_FillingDriver()
46 {
47 }
48
49 //=======================================================================
50 //function : Execute
51 //purpose  :
52 //=======================================================================
53 Standard_Integer GEOMImpl_FillingDriver::Execute(TFunction_Logbook& log) const
54 {
55   if (Label().IsNull()) return 0;
56   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
57   if (aFunction.IsNull()) return 0;
58
59   if (aFunction->GetType() != BASIC_FILLING) return 0;
60
61   GEOMImpl_IFilling IF (aFunction);
62   Handle(GEOM_Function) aShapeFunction = IF.GetShape();
63   if (aShapeFunction.IsNull()) return 0;
64   TopoDS_Shape aShape = aShapeFunction->GetValue();
65   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND) return 0;
66
67   Standard_Integer mindeg = IF.GetMinDeg();
68   Standard_Integer maxdeg = IF.GetMaxDeg();
69   Standard_Real tol3d = IF.GetTol2D();
70   Standard_Real tol2d = IF.GetTol3D();
71   Standard_Integer nbiter = IF.GetNbIter();
72
73   if (mindeg > maxdeg) {
74     Standard_RangeError::Raise("Minimal degree can not be more than maximal degree");
75   }
76
77   /* we verify the contents of the shape */
78   TopExp_Explorer Ex;
79   TopoDS_Shape Scurrent;
80   Standard_Real First, Last;
81   Handle(Geom_Curve) C;
82   GeomFill_SectionGenerator Section;
83
84   Standard_Integer i = 0;
85   for (Ex.Init(aShape, TopAbs_EDGE); Ex.More(); Ex.Next()) {
86     Scurrent = Ex.Current() ;
87     if (Scurrent.IsNull() || Scurrent.ShapeType() != TopAbs_EDGE) return 0;
88     C = BRep_Tool::Curve(TopoDS::Edge(Scurrent), First, Last);
89     C = new Geom_TrimmedCurve(C, First, Last);
90     Section.AddCurve(C);
91     i++;
92   }
93
94   /* a 'tolerance' is used to compare 2 knots : see GeomFill_Generator.cdl */
95   Section.Perform(Precision::Confusion());
96   Handle(GeomFill_Line) Line = new GeomFill_Line(i);
97
98   GeomFill_AppSurf App (mindeg, maxdeg, tol3d, tol2d, nbiter); /* user parameters */
99   App.Perform(Line, Section);
100
101   if (!App.IsDone()) return 0;
102   Standard_Integer UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots;
103   App.SurfShape(UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots);
104   Handle(Geom_BSplineSurface) GBS = new Geom_BSplineSurface
105     (App.SurfPoles(), App.SurfWeights(), App.SurfUKnots(), App.SurfVKnots(),
106      App.SurfUMults(), App.SurfVMults(), App.UDegree(), App.VDegree());
107
108   if (GBS.IsNull()) return 0;
109   aShape = BRepBuilderAPI_MakeFace(GBS);
110
111   /* We test the validity of resulting shape */
112   if (!BRepAlgo::IsValid((aShape))) {
113     Standard_ConstructionError::Raise("Algorithm have produced an invalid shape result");
114     return 0;
115   }
116
117   aFunction->SetValue(aShape);
118
119   log.SetTouched(Label());
120   return 1;
121 }
122
123
124 //=======================================================================
125 //function :  GEOMImpl_FillingDriver_Type_
126 //purpose  :
127 //=======================================================================
128 Standard_EXPORT Handle_Standard_Type& GEOMImpl_FillingDriver_Type_()
129 {
130
131   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
132   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
133   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
134   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
135   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
136   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
137
138
139   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
140   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_FillingDriver",
141                                                          sizeof(GEOMImpl_FillingDriver),
142                                                          1,
143                                                          (Standard_Address)_Ancestors,
144                                                          (Standard_Address)NULL);
145
146   return _aType;
147 }
148
149 //=======================================================================
150 //function : DownCast
151 //purpose  :
152 //=======================================================================
153
154 const Handle(GEOMImpl_FillingDriver) Handle(GEOMImpl_FillingDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
155 {
156   Handle(GEOMImpl_FillingDriver) _anOtherObject;
157
158   if (!AnObject.IsNull()) {
159      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_FillingDriver))) {
160        _anOtherObject = Handle(GEOMImpl_FillingDriver)((Handle(GEOMImpl_FillingDriver)&)AnObject);
161      }
162   }
163
164   return _anOtherObject ;
165 }
166
167