Salome HOME
0023505: Sigsegv with fuse on cylinder and cone
[modules/geom.git] / src / GEOMImpl / GEOMImpl_FilletDriver.cxx
1 // Copyright (C) 2007-2016  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 <GEOMImpl_FilletDriver.hxx>
24 #include <GEOMImpl_IFillet.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOMImpl_ILocalOperations.hxx>
27 #include <GEOMUtils.hxx>
28 #include <GEOM_Function.hxx>
29
30 #include <BRepFilletAPI_MakeFillet.hxx>
31 #include <TopoDS.hxx>
32 #include <TopoDS_Shape.hxx>
33 #include <TopoDS_Edge.hxx>
34 #include <TopAbs.hxx>
35 #include <TopExp_Explorer.hxx>
36 #include <StdFail_NotDone.hxx>
37
38 // Debug PipeTShape function: uncomment the macro below to correct tolerance
39 // of resulting face after fillet creation
40 // VSR 30/12/2014: macro disabled
41 //#define FIX_FACE_TOLERANCE
42 // Debug PipeTShape function: uncomment the macro below to correct tolerance
43 // of resulting curves after fillet creation
44 // VSR 30/12/2014: macro disabled
45 //#define FIX_CURVES_TOLERANCES
46
47 //=======================================================================
48 //function : GetID
49 //purpose  :
50 //=======================================================================
51 const Standard_GUID& GEOMImpl_FilletDriver::GetID()
52 {
53   static Standard_GUID aFilletDriver("FF1BBB41-5D14-4df2-980B-3A668264EA16");
54   return aFilletDriver;
55 }
56
57
58 //=======================================================================
59 //function : GEOMImpl_FilletDriver
60 //purpose  :
61 //=======================================================================
62 GEOMImpl_FilletDriver::GEOMImpl_FilletDriver()
63 {
64 }
65
66 //=======================================================================
67 //function : Execute
68 //purpose  :
69 //=======================================================================
70 Standard_Integer GEOMImpl_FilletDriver::Execute(LOGBOOK& log) const
71 {
72   if (Label().IsNull()) return 0;
73   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
74
75   GEOMImpl_IFillet aCI (aFunction);
76   Standard_Integer aType = aFunction->GetType();
77
78   TopoDS_Shape aShape;
79
80   Handle(GEOM_Function) aRefShape = aCI.GetShape();
81   TopoDS_Shape aShapeBase = aRefShape->GetValue();
82   BRepFilletAPI_MakeFillet fill (aShapeBase);
83
84   if (aType == FILLET_SHAPE_ALL) {
85     TopExp_Explorer Exp (aShapeBase, TopAbs_EDGE);
86     for (; Exp.More(); Exp.Next()) {
87       TopoDS_Edge E = TopoDS::Edge(Exp.Current());
88       fill.Add(E);
89     }
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   }
102   else if (aType == FILLET_SHAPE_FACES || aType == FILLET_SHAPE_FACES_2R) {
103     int aLen = aCI.GetLength();
104     int ind = 1;
105     for (; ind <= aLen; ind++) {
106       TopoDS_Shape aShapeFace;
107       if (GEOMImpl_ILocalOperations::GetSubShape
108           (aShapeBase, aCI.GetFace(ind), aShapeFace)) {
109         TopExp_Explorer Exp (aShapeFace, TopAbs_EDGE);
110         for (; Exp.More(); Exp.Next()) {
111           fill.Add(TopoDS::Edge(Exp.Current()));
112         }
113       }
114     }
115   }
116   else {
117   }
118
119   if (aType == FILLET_SHAPE_FACES || aType == FILLET_SHAPE_EDGES || aType == FILLET_SHAPE_ALL) {
120     for (int i = 1; i <= fill.NbContours(); i++)
121       fill.SetRadius(aCI.GetR(), i, 1);
122   }
123   else if (aType == FILLET_SHAPE_FACES_2R || aType == FILLET_SHAPE_EDGES_2R) {
124     for (int i = 1; i <= fill.NbContours(); i++) {
125       fill.SetRadius(aCI.GetR1(), aCI.GetR2(), i, 1);
126     }
127   }
128
129   fill.Build();
130   if (!fill.IsDone()) {
131     StdFail_NotDone::Raise("Fillet can't be computed on the given shape with the given radius");
132   }
133   aShape = GEOMUtils::ReduceCompound( fill.Shape() );
134
135   if (aShape.IsNull()) return 0;
136
137 #if defined(FIX_CURVES_TOLERANCES)
138   bool isOk = GEOMUtils::FixShapeCurves(aShape);
139 #elif defined(FIX_FACE_TOLERANCE)
140   bool isOk = GEOMUtils::FixShapeTolerance(aShape, TopAbs_FACE);
141 #else
142   // 08.07.2008 added by skl during fixing bug 19761 from Mantis
143   bool isOk = GEOMUtils::CheckShape(aShape) || GEOMUtils::FixShapeTolerance(aShape);
144 #endif
145   if ( !isOk )
146     StdFail_NotDone::Raise("Fillet algorithm have produced an invalid shape result");
147
148   aFunction->SetValue(aShape);
149
150 #if OCC_VERSION_MAJOR < 7
151   log.SetTouched(Label());
152 #else
153   log->SetTouched(Label());
154 #endif
155
156   return 1;
157 }
158
159 //================================================================================
160 /*!
161  * \brief Returns a name of creation operation and names and values of creation parameters
162  */
163 //================================================================================
164
165 bool GEOMImpl_FilletDriver::
166 GetCreationInformation(std::string&             theOperationName,
167                        std::vector<GEOM_Param>& theParams)
168 {
169   if (Label().IsNull()) return 0;
170   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
171
172   GEOMImpl_IFillet aCI( function );
173   Standard_Integer aType = function->GetType();
174
175   theOperationName = "FILLET";
176
177   switch ( aType ) {
178   case FILLET_SHAPE_ALL:
179     AddParam( theParams, "Main Object", aCI.GetShape() );
180     AddParam( theParams, "Selected edges", "all" );
181     break;
182   case FILLET_SHAPE_EDGES:
183   case FILLET_SHAPE_EDGES_2R:
184     AddParam( theParams, "Main Object", aCI.GetShape() );
185     AddParam( theParams, "Selected edges" );
186     if ( aCI.GetLength() > 1 )
187       theParams[1] << aCI.GetLength() << " edges: ";
188     for (int i = 1; i <= aCI.GetLength(); ++i )
189       theParams[1] << aCI.GetEdge( i ) << " ";
190     if ( aType == FILLET_SHAPE_EDGES ) {
191       AddParam( theParams, "Radius", aCI.GetR() );
192     }
193     else {
194       AddParam( theParams, "R1", aCI.GetR1() );
195       AddParam( theParams, "R2", aCI.GetR2() );
196     }
197     break;
198   case FILLET_SHAPE_FACES:
199   case FILLET_SHAPE_FACES_2R:
200     AddParam( theParams, "Main Object", aCI.GetShape() );
201     AddParam( theParams, "Selected faces" );
202     if ( aCI.GetLength() > 1 )
203       theParams[1] << aCI.GetLength() << " faces: ";
204     for (int i = 1; i <= aCI.GetLength(); ++i )
205       theParams[1] << aCI.GetFace( i ) << " ";
206     if ( aType == FILLET_SHAPE_FACES ) {
207       AddParam( theParams, "Radius", aCI.GetR() );
208     }
209     else {
210       AddParam( theParams, "R1", aCI.GetR1() );
211       AddParam( theParams, "R2", aCI.GetR2() );
212     }
213     break;
214   default:
215     return false;
216   }
217   
218   return true;
219 }
220
221 OCCT_IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_FilletDriver,GEOM_BaseDriver);