Salome HOME
13b905a1b600dd9ea1e9249f9dc2825c4a1da889
[modules/geom.git] / src / GEOMImpl / GEOMImpl_FilletDriver.cxx
1 // Copyright (C) 2007-2023  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(Handle(TFunction_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   log->SetTouched(Label());
151
152   return 1;
153 }
154
155 //================================================================================
156 /*!
157  * \brief Returns a name of creation operation and names and values of creation parameters
158  */
159 //================================================================================
160
161 bool GEOMImpl_FilletDriver::
162 GetCreationInformation(std::string&             theOperationName,
163                        std::vector<GEOM_Param>& theParams)
164 {
165   if (Label().IsNull()) return 0;
166   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
167
168   GEOMImpl_IFillet aCI( function );
169   Standard_Integer aType = function->GetType();
170
171   theOperationName = "FILLET";
172
173   switch ( aType ) {
174   case FILLET_SHAPE_ALL:
175     AddParam( theParams, "Main Object", aCI.GetShape() );
176     AddParam( theParams, "Selected edges", "all" );
177     break;
178   case FILLET_SHAPE_EDGES:
179   case FILLET_SHAPE_EDGES_2R:
180     AddParam( theParams, "Main Object", aCI.GetShape() );
181     AddParam( theParams, "Selected edges" );
182     if ( aCI.GetLength() > 1 )
183       theParams[1] << aCI.GetLength() << " edges: ";
184     for (int i = 1; i <= aCI.GetLength(); ++i )
185       theParams[1] << aCI.GetEdge( i ) << " ";
186     if ( aType == FILLET_SHAPE_EDGES ) {
187       AddParam( theParams, "Radius", aCI.GetR() );
188     }
189     else {
190       AddParam( theParams, "R1", aCI.GetR1() );
191       AddParam( theParams, "R2", aCI.GetR2() );
192     }
193     break;
194   case FILLET_SHAPE_FACES:
195   case FILLET_SHAPE_FACES_2R:
196     AddParam( theParams, "Main Object", aCI.GetShape() );
197     AddParam( theParams, "Selected faces" );
198     if ( aCI.GetLength() > 1 )
199       theParams[1] << aCI.GetLength() << " faces: ";
200     for (int i = 1; i <= aCI.GetLength(); ++i )
201       theParams[1] << aCI.GetFace( i ) << " ";
202     if ( aType == FILLET_SHAPE_FACES ) {
203       AddParam( theParams, "Radius", aCI.GetR() );
204     }
205     else {
206       AddParam( theParams, "R1", aCI.GetR1() );
207       AddParam( theParams, "R2", aCI.GetR2() );
208     }
209     break;
210   default:
211     return false;
212   }
213   
214   return true;
215 }
216
217 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_FilletDriver,GEOM_BaseDriver)