Salome HOME
f8b6756a0b8839353751e1bf0a241a58cbf0a750
[modules/geom.git] / src / GEOMImpl / GEOMImpl_FilletDriver.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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 // VSR 08/12/2014: debug PipeTShape function
49 // Uncomment the macro below to correct tolerance of resulting face after creating fillet
50 #define FIX_FACE_TOLERANCE
51
52 namespace
53 {
54   bool FixShape( TopoDS_Shape& shape,
55                  TopAbs_ShapeEnum type = TopAbs_SHAPE,
56                  Standard_Real tolerance = Precision::Confusion() )
57   {
58     ShapeFix_ShapeTolerance aSFT;
59     aSFT.LimitTolerance( shape, tolerance, tolerance, type );
60     Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape( shape );
61     aSfs->Perform();
62     shape = aSfs->Shape();
63     BRepCheck_Analyzer ana( shape, false );
64     return ana.IsValid();
65   }
66 }
67
68 //=======================================================================
69 //function : GetID
70 //purpose  :
71 //=======================================================================
72 const Standard_GUID& GEOMImpl_FilletDriver::GetID()
73 {
74   static Standard_GUID aFilletDriver("FF1BBB41-5D14-4df2-980B-3A668264EA16");
75   return aFilletDriver;
76 }
77
78
79 //=======================================================================
80 //function : GEOMImpl_FilletDriver
81 //purpose  :
82 //=======================================================================
83 GEOMImpl_FilletDriver::GEOMImpl_FilletDriver()
84 {
85 }
86
87 //=======================================================================
88 //function : Execute
89 //purpose  :
90 //=======================================================================
91 Standard_Integer GEOMImpl_FilletDriver::Execute(TFunction_Logbook& log) const
92 {
93   if (Label().IsNull()) return 0;
94   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
95
96   GEOMImpl_IFillet aCI (aFunction);
97   Standard_Integer aType = aFunction->GetType();
98
99   TopoDS_Shape aShape;
100
101   Handle(GEOM_Function) aRefShape = aCI.GetShape();
102   TopoDS_Shape aShapeBase = aRefShape->GetValue();
103   BRepFilletAPI_MakeFillet fill (aShapeBase);
104
105   if (aType == FILLET_SHAPE_ALL) {
106     TopExp_Explorer Exp (aShapeBase, TopAbs_EDGE);
107     for (; Exp.More(); Exp.Next()) {
108       TopoDS_Edge E = TopoDS::Edge(Exp.Current());
109       fill.Add(E);
110     }
111   }
112   else if (aType == FILLET_SHAPE_EDGES || aType == FILLET_SHAPE_EDGES_2R) {
113     int aLen = aCI.GetLength();
114     int ind = 1;
115     for (; ind <= aLen; ind++) {
116       TopoDS_Shape aShapeEdge;
117       if (GEOMImpl_ILocalOperations::GetSubShape
118           (aShapeBase, aCI.GetEdge(ind), aShapeEdge)) {
119           fill.Add(TopoDS::Edge(aShapeEdge));
120       }
121     }
122   }
123   else if (aType == FILLET_SHAPE_FACES || aType == FILLET_SHAPE_FACES_2R) {
124     int aLen = aCI.GetLength();
125     int ind = 1;
126     for (; ind <= aLen; ind++) {
127       TopoDS_Shape aShapeFace;
128       if (GEOMImpl_ILocalOperations::GetSubShape
129           (aShapeBase, aCI.GetFace(ind), aShapeFace)) {
130         TopExp_Explorer Exp (aShapeFace, TopAbs_EDGE);
131         for (; Exp.More(); Exp.Next()) {
132           fill.Add(TopoDS::Edge(Exp.Current()));
133         }
134       }
135     }
136   }
137   else {
138   }
139
140   if (aType == FILLET_SHAPE_FACES || aType == FILLET_SHAPE_EDGES || aType == FILLET_SHAPE_ALL) {
141     for (int i = 1; i <= fill.NbContours(); i++)
142       fill.SetRadius(aCI.GetR(), i, 1);
143   }
144   else if (aType == FILLET_SHAPE_FACES_2R || aType == FILLET_SHAPE_EDGES_2R) {
145     for (int i = 1; i <= fill.NbContours(); i++) {
146       fill.SetRadius(aCI.GetR1(), aCI.GetR2(), i, 1);
147     }
148   }
149
150   fill.Build();
151   if (!fill.IsDone()) {
152     StdFail_NotDone::Raise("Fillet can't be computed on the given shape with the given radius");
153   }
154   aShape = fill.Shape();
155
156   if (aShape.IsNull()) return 0;
157
158 #ifdef FIX_FACE_TOLERANCE
159   bool isOk = FixShape(aShape, TopAbs_FACE);
160 #else
161   // Check shape validity
162   BRepCheck_Analyzer ana(aShape, false);
163   // 08.07.2008 added by skl during fixing bug 19761 from Mantis
164   bool isOk = ana.IsValid() || FixShape(aShape);
165 #endif
166   if ( !isOk )
167     StdFail_NotDone::Raise("Fillet algorithm have produced an invalid shape result");
168
169   aFunction->SetValue(aShape);
170
171   log.SetTouched(Label());
172
173   return 1;
174 }
175
176 //================================================================================
177 /*!
178  * \brief Returns a name of creation operation and names and values of creation parameters
179  */
180 //================================================================================
181
182 bool GEOMImpl_FilletDriver::
183 GetCreationInformation(std::string&             theOperationName,
184                        std::vector<GEOM_Param>& theParams)
185 {
186   if (Label().IsNull()) return 0;
187   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
188
189   GEOMImpl_IFillet aCI( function );
190   Standard_Integer aType = function->GetType();
191
192   theOperationName = "FILLET";
193
194   switch ( aType ) {
195   case FILLET_SHAPE_ALL:
196     AddParam( theParams, "Main Object", aCI.GetShape() );
197     AddParam( theParams, "Selected edges", "all" );
198     break;
199   case FILLET_SHAPE_EDGES:
200   case FILLET_SHAPE_EDGES_2R:
201     AddParam( theParams, "Main Object", aCI.GetShape() );
202     AddParam( theParams, "Selected edges" );
203     if ( aCI.GetLength() > 1 )
204       theParams[1] << aCI.GetLength() << " edges: ";
205     for (int i = 1; i <= aCI.GetLength(); ++i )
206       theParams[1] << aCI.GetEdge( i ) << " ";
207     if ( aType == FILLET_SHAPE_EDGES ) {
208       AddParam( theParams, "Radius", aCI.GetR() );
209     }
210     else {
211       AddParam( theParams, "R1", aCI.GetR1() );
212       AddParam( theParams, "R2", aCI.GetR2() );
213     }
214     break;
215   case FILLET_SHAPE_FACES:
216   case FILLET_SHAPE_FACES_2R:
217     AddParam( theParams, "Main Object", aCI.GetShape() );
218     AddParam( theParams, "Selected faces" );
219     if ( aCI.GetLength() > 1 )
220       theParams[1] << aCI.GetLength() << " faces: ";
221     for (int i = 1; i <= aCI.GetLength(); ++i )
222       theParams[1] << aCI.GetFace( i ) << " ";
223     if ( aType == FILLET_SHAPE_FACES ) {
224       AddParam( theParams, "Radius", aCI.GetR() );
225     }
226     else {
227       AddParam( theParams, "R1", aCI.GetR1() );
228       AddParam( theParams, "R2", aCI.GetR2() );
229     }
230     break;
231   default:
232     return false;
233   }
234   
235   return true;
236 }
237
238 IMPLEMENT_STANDARD_HANDLE (GEOMImpl_FilletDriver,GEOM_BaseDriver);
239 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_FilletDriver,GEOM_BaseDriver);