Salome HOME
0020907: Werror in GEOM: integrate patch from Erwan ADAM
[modules/geom.git] / src / GEOMImpl / GEOMImpl_FilletDriver.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <Standard_Stream.hxx>
24
25 #include <GEOMImpl_FilletDriver.hxx>
26 #include <GEOMImpl_IFillet.hxx>
27 #include <GEOMImpl_Types.hxx>
28 #include <GEOMImpl_ILocalOperations.hxx>
29 #include <GEOM_Function.hxx>
30
31 #include <BRepFilletAPI_MakeFillet.hxx>
32 #include <BRepCheck_Analyzer.hxx>
33 #include <BRep_Tool.hxx>
34
35 #include <TopoDS.hxx>
36 #include <TopoDS_Shape.hxx>
37 #include <TopoDS_Edge.hxx>
38 #include <TopAbs.hxx>
39 #include <TopExp_Explorer.hxx>
40
41 #include <ShapeFix_ShapeTolerance.hxx>
42 #include <ShapeFix_Shape.hxx>
43
44 #include <Precision.hxx>
45 #include <gp_Pnt.hxx>
46 #include <StdFail_NotDone.hxx>
47
48 //=======================================================================
49 //function : GetID
50 //purpose  :
51 //=======================================================================
52 const Standard_GUID& GEOMImpl_FilletDriver::GetID()
53 {
54   static Standard_GUID aFilletDriver("FF1BBB41-5D14-4df2-980B-3A668264EA16");
55   return aFilletDriver;
56 }
57
58
59 //=======================================================================
60 //function : GEOMImpl_FilletDriver
61 //purpose  :
62 //=======================================================================
63 GEOMImpl_FilletDriver::GEOMImpl_FilletDriver()
64 {
65 }
66
67 //=======================================================================
68 //function : Execute
69 //purpose  :
70 //=======================================================================
71 Standard_Integer GEOMImpl_FilletDriver::Execute(TFunction_Logbook& log) const
72 {
73   if (Label().IsNull()) return 0;
74   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
75
76   GEOMImpl_IFillet aCI (aFunction);
77   Standard_Integer aType = aFunction->GetType();
78
79   TopoDS_Shape aShape;
80
81   Handle(GEOM_Function) aRefShape = aCI.GetShape();
82   TopoDS_Shape aShapeBase = aRefShape->GetValue();
83   BRepFilletAPI_MakeFillet fill (aShapeBase);
84
85   if (aType == FILLET_SHAPE_ALL) {
86     TopExp_Explorer Exp (aShapeBase, TopAbs_EDGE);
87     for (; Exp.More(); Exp.Next()) {
88       TopoDS_Edge E = TopoDS::Edge(Exp.Current());
89       fill.Add(E);
90     }
91   } else if (aType == FILLET_SHAPE_EDGES || aType == FILLET_SHAPE_EDGES_2R) {
92     int aLen = aCI.GetLength();
93     int ind = 1;
94     for (; ind <= aLen; ind++) {
95       TopoDS_Shape aShapeEdge;
96       if (GEOMImpl_ILocalOperations::GetSubShape
97           (aShapeBase, aCI.GetEdge(ind), aShapeEdge)) {
98           fill.Add(TopoDS::Edge(aShapeEdge));
99       }
100     }
101   } else if (aType == FILLET_SHAPE_FACES || aType == FILLET_SHAPE_FACES_2R) {
102     int aLen = aCI.GetLength();
103     int ind = 1;
104     for (; ind <= aLen; ind++) {
105       TopoDS_Shape aShapeFace;
106       if (GEOMImpl_ILocalOperations::GetSubShape
107           (aShapeBase, aCI.GetFace(ind), aShapeFace)) {
108         TopExp_Explorer Exp (aShapeFace, TopAbs_EDGE);
109         for (; Exp.More(); Exp.Next()) {
110           fill.Add(TopoDS::Edge(Exp.Current()));
111         }
112       }
113     }
114   } else {
115   }
116   if (aType == FILLET_SHAPE_FACES || aType == FILLET_SHAPE_EDGES || aType == FILLET_SHAPE_ALL)
117     for (int i = 1; i <= fill.NbContours(); i++)
118       fill.SetRadius(aCI.GetR(), i, 1);
119     else if (aType == FILLET_SHAPE_FACES_2R || aType == FILLET_SHAPE_EDGES_2R)
120       for (int i = 1; i <= fill.NbContours(); i++)
121         {
122         fill.SetRadius(aCI.GetR1(), aCI.GetR2(), i, 1);
123         }
124
125   fill.Build();
126   if (!fill.IsDone()) {
127     StdFail_NotDone::Raise("Fillet can't be computed on the given shape with the given radius");
128   }
129   aShape = fill.Shape();
130
131   if (aShape.IsNull()) return 0;
132
133   // Check shape validity
134   BRepCheck_Analyzer ana (aShape, false);
135   if (!ana.IsValid()) {
136     // 08.07.2008 added by skl during fixing bug 19761 from Mantis
137     ShapeFix_ShapeTolerance aSFT;
138     aSFT.LimitTolerance(aShape, Precision::Confusion(),
139                         Precision::Confusion(), TopAbs_SHAPE);
140     Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
141     aSfs->Perform();
142     aShape = aSfs->Shape();
143     ana.Init(aShape);
144     if (!ana.IsValid())
145       StdFail_NotDone::Raise("Fillet algorithm have produced an invalid shape result");
146   }
147
148   aFunction->SetValue(aShape);
149
150   log.SetTouched(Label());
151
152   return 1;
153 }
154
155
156 //=======================================================================
157 //function :  GEOMImpl_FilletDriver_Type_
158 //purpose  :
159 //=======================================================================
160 Standard_EXPORT Handle_Standard_Type& GEOMImpl_FilletDriver_Type_()
161 {
162
163   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
164   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
165   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
166   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
167   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
168   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
169
170
171   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
172   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_FilletDriver",
173                                                          sizeof(GEOMImpl_FilletDriver),
174                                                          1,
175                                                          (Standard_Address)_Ancestors,
176                                                          (Standard_Address)NULL);
177
178   return _aType;
179 }
180
181 //=======================================================================
182 //function : DownCast
183 //purpose  :
184 //=======================================================================
185 const Handle(GEOMImpl_FilletDriver) Handle(GEOMImpl_FilletDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
186 {
187   Handle(GEOMImpl_FilletDriver) _anOtherObject;
188
189   if (!AnObject.IsNull()) {
190      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_FilletDriver))) {
191        _anOtherObject = Handle(GEOMImpl_FilletDriver)((Handle(GEOMImpl_FilletDriver)&)AnObject);
192      }
193   }
194
195   return _anOtherObject ;
196 }