Salome HOME
- Patch for recent Debian distrib:
[modules/geom.git] / src / GEOMImpl / GEOMImpl_FilletDriver.cxx
1 //  Copyright (C) 2007-2008  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 #include <Standard_Stream.hxx>
23
24 #include <GEOMImpl_FilletDriver.hxx>
25 #include <GEOMImpl_IFillet.hxx>
26 #include <GEOMImpl_Types.hxx>
27 #include <GEOMImpl_ILocalOperations.hxx>
28 #include <GEOM_Function.hxx>
29
30 #include <BRepFilletAPI_MakeFillet.hxx>
31 #include <BRepCheck_Analyzer.hxx>
32 #include <BRep_Tool.hxx>
33
34 #include <TopoDS.hxx>
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS_Edge.hxx>
37 #include <TopAbs.hxx>
38 #include <TopExp_Explorer.hxx>
39
40 #include <ShapeFix_ShapeTolerance.hxx>
41 #include <ShapeFix_Shape.hxx>
42
43 #include <Precision.hxx>
44 #include <gp_Pnt.hxx>
45 #include <StdFail_NotDone.hxx>
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(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   } else if (aType == FILLET_SHAPE_EDGES || aType == FILLET_SHAPE_EDGES_2R) {
91     int aLen = aCI.GetLength();
92     int ind = 1;
93     for (; ind <= aLen; ind++) {
94       TopoDS_Shape aShapeEdge;
95       if (GEOMImpl_ILocalOperations::GetSubShape
96           (aShapeBase, aCI.GetEdge(ind), aShapeEdge)) {
97           fill.Add(TopoDS::Edge(aShapeEdge));
98       }
99     }
100   } else if (aType == FILLET_SHAPE_FACES || aType == FILLET_SHAPE_FACES_2R) {
101     int aLen = aCI.GetLength();
102     int ind = 1;
103     for (; ind <= aLen; ind++) {
104       TopoDS_Shape aShapeFace;
105       if (GEOMImpl_ILocalOperations::GetSubShape
106           (aShapeBase, aCI.GetFace(ind), aShapeFace)) {
107         TopExp_Explorer Exp (aShapeFace, TopAbs_EDGE);
108         for (; Exp.More(); Exp.Next()) {
109           fill.Add(TopoDS::Edge(Exp.Current()));
110         }
111       }
112     }
113   } else {
114   }
115   if (aType == FILLET_SHAPE_FACES || aType == FILLET_SHAPE_EDGES || aType == FILLET_SHAPE_ALL)
116     for (int i = 1; i <= fill.NbContours(); i++)
117       fill.SetRadius(aCI.GetR(), i, 1);
118     else if (aType == FILLET_SHAPE_FACES_2R || aType == FILLET_SHAPE_EDGES_2R)
119       for (int i = 1; i <= fill.NbContours(); i++)
120         {
121         fill.SetRadius(aCI.GetR1(), aCI.GetR2(), i, 1);
122         }
123
124   fill.Build();
125   if (!fill.IsDone()) {
126     StdFail_NotDone::Raise("Fillet can't be computed on the given shape with the given radius");
127   }
128   aShape = fill.Shape();
129
130   if (aShape.IsNull()) return 0;
131
132   // Check shape validity
133   BRepCheck_Analyzer ana (aShape, false);
134   if (!ana.IsValid()) {
135     // 08.07.2008 added by skl during fixing bug 19761 from Mantis
136     ShapeFix_ShapeTolerance aSFT;
137     aSFT.LimitTolerance(aShape, Precision::Confusion(),
138                         Precision::Confusion(), TopAbs_SHAPE);
139     Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
140     aSfs->Perform();
141     aShape = aSfs->Shape();
142     ana.Init(aShape);
143     if (!ana.IsValid())
144       StdFail_NotDone::Raise("Fillet algorithm have produced an invalid shape result");
145   }
146
147   aFunction->SetValue(aShape);
148
149   log.SetTouched(Label());
150
151   return 1;
152 }
153
154
155 //=======================================================================
156 //function :  GEOMImpl_FilletDriver_Type_
157 //purpose  :
158 //=======================================================================
159 Standard_EXPORT Handle_Standard_Type& GEOMImpl_FilletDriver_Type_()
160 {
161
162   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
163   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
164   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
165   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
166   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
167   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
168
169
170   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
171   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_FilletDriver",
172                                                          sizeof(GEOMImpl_FilletDriver),
173                                                          1,
174                                                          (Standard_Address)_Ancestors,
175                                                          (Standard_Address)NULL);
176
177   return _aType;
178 }
179
180 //=======================================================================
181 //function : DownCast
182 //purpose  :
183 //=======================================================================
184 const Handle(GEOMImpl_FilletDriver) Handle(GEOMImpl_FilletDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
185 {
186   Handle(GEOMImpl_FilletDriver) _anOtherObject;
187
188   if (!AnObject.IsNull()) {
189      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_FilletDriver))) {
190        _anOtherObject = Handle(GEOMImpl_FilletDriver)((Handle(GEOMImpl_FilletDriver)&)AnObject);
191      }
192   }
193
194   return _anOtherObject ;
195 }