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