Salome HOME
Changes for 0020673 - Implementation of "Auto-correct edges orientation".
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ChamferDriver.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_ChamferDriver.hxx>
25 #include <GEOMImpl_IChamfer.hxx>
26 #include <GEOMImpl_Types.hxx>
27 #include <GEOMImpl_ILocalOperations.hxx>
28 #include <GEOMImpl_Block6Explorer.hxx>
29
30 #include <GEOM_Function.hxx>
31
32 #include <BRepLib.hxx>
33 #include <BRep_Tool.hxx>
34 #include <BRepTools.hxx>
35 #include <BRepFilletAPI_MakeChamfer.hxx>
36
37 #include <ShapeFix_Shape.hxx>
38 #include <ShapeFix_ShapeTolerance.hxx>
39
40 #include <TopAbs.hxx>
41 #include <TopoDS.hxx>
42 #include <TopoDS_Edge.hxx>
43 #include <TopoDS_Face.hxx>
44 #include <TopoDS_Shape.hxx>
45 #include <TopoDS_Iterator.hxx>
46 #include <TopExp.hxx>
47 #include <TopExp_Explorer.hxx>
48 #include <TopTools_MapOfShape.hxx>
49 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
50
51 #include <Precision.hxx>
52 #include <gp_Pnt.hxx>
53 #include <StdFail_NotDone.hxx>
54
55 //=======================================================================
56 //function : GetID
57 //purpose  :
58 //=======================================================================
59 const Standard_GUID& GEOMImpl_ChamferDriver::GetID()
60 {
61   static Standard_GUID aChamferDriver("FF1BBB42-5D14-4df2-980B-3A668264EA16");
62   return aChamferDriver;
63 }
64
65
66 //=======================================================================
67 //function : GEOMImpl_ChamferDriver
68 //purpose  :
69 //=======================================================================
70 GEOMImpl_ChamferDriver::GEOMImpl_ChamferDriver()
71 {
72 }
73
74 //=======================================================================
75 //function : isGoodForChamfer
76 //purpose  :
77 //=======================================================================
78 static Standard_Boolean isGoodForChamfer (const TopoDS_Shape& theShape)
79 {
80   if (theShape.ShapeType() == TopAbs_SHELL ||
81       theShape.ShapeType() == TopAbs_SOLID ||
82       theShape.ShapeType() == TopAbs_COMPSOLID) {
83     return Standard_True;
84   }
85
86   if (theShape.ShapeType() == TopAbs_COMPOUND) {
87     TopTools_MapOfShape mapShape;
88     TopoDS_Iterator It (theShape, Standard_False, Standard_False);
89     for (; It.More(); It.Next()) {
90       if (mapShape.Add(It.Value())) {
91         if (!isGoodForChamfer(It.Value())) {
92           return Standard_False;
93         }
94       }
95     }
96     return Standard_True;
97   }
98
99   return Standard_False;
100 }
101
102 //=======================================================================
103 //function : Execute
104 //purpose  :
105 //=======================================================================
106 Standard_Integer GEOMImpl_ChamferDriver::Execute(TFunction_Logbook& log) const
107 {
108   if (Label().IsNull()) return 0;
109   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
110
111   GEOMImpl_IChamfer aCI (aFunction);
112   Standard_Integer aType = aFunction->GetType();
113
114   TopoDS_Shape aShape;
115
116   Handle(GEOM_Function) aRefShape = aCI.GetShape();
117   TopoDS_Shape aShapeBase = aRefShape->GetValue();
118
119   // Check the shape type. It have to be shell
120   // or solid, or compsolid, or compound of these shapes.
121   if (!isGoodForChamfer(aShapeBase)) {
122     StdFail_NotDone::Raise
123       ("Wrong shape. Must be shell or solid, or compsolid or compound of these shapes");
124   }
125
126   BRepFilletAPI_MakeChamfer fill (aShapeBase);
127
128   if (aType == CHAMFER_SHAPE_ALL) {
129     // symmetric chamfer on all edges
130     double aD = aCI.GetD();
131     TopTools_IndexedDataMapOfShapeListOfShape M;
132     GEOMImpl_Block6Explorer::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, M);
133     for (int i = 1; i <= M.Extent(); i++) {
134       TopoDS_Edge E = TopoDS::Edge(M.FindKey(i));
135       TopoDS_Face F = TopoDS::Face(M.FindFromIndex(i).First());
136       if (!BRepTools::IsReallyClosed(E, F) &&
137           !BRep_Tool::Degenerated(E) &&
138           M.FindFromIndex(i).Extent() == 2)
139         fill.Add(aD, E, F);
140     }
141   }
142   else if (aType == CHAMFER_SHAPE_EDGE || aType == CHAMFER_SHAPE_EDGE_AD) {
143     // chamfer on edges, common to two faces, with D1 on the first face
144
145     TopoDS_Shape aFace1, aFace2;
146     if (GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetFace1(), aFace1) &&
147         GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetFace2(), aFace2))
148     {
149       TopoDS_Face F = TopoDS::Face(aFace1);
150
151       // fill map of edges of the second face
152       TopTools_MapOfShape aMap;
153       TopExp_Explorer Exp2 (aFace2, TopAbs_EDGE);
154       for (; Exp2.More(); Exp2.Next()) {
155         aMap.Add(Exp2.Current());
156       }
157
158       // find edges of the first face, common with the second face
159       TopExp_Explorer Exp (aFace1, TopAbs_EDGE);
160       for (; Exp.More(); Exp.Next()) {
161         if (aMap.Contains(Exp.Current())) {
162           TopoDS_Edge E = TopoDS::Edge(Exp.Current());
163           if (!BRepTools::IsReallyClosed(E, F) && !BRep_Tool::Degenerated(E))
164           {
165             if ( aType == CHAMFER_SHAPE_EDGE )
166             {
167               double aD1 = aCI.GetD1();
168               double aD2 = aCI.GetD2();
169               fill.Add(aD1, aD2, E, F);
170             }
171             else
172             {
173               double aD = aCI.GetD();
174               double anAngle = aCI.GetAngle();
175               if ( (anAngle > 0) && (anAngle < (Standard_PI/2)) )
176                 fill.AddDA(aD, anAngle, E, F);
177             }
178           }
179         }
180       }
181     }
182   }
183   else if (aType == CHAMFER_SHAPE_FACES || aType == CHAMFER_SHAPE_FACES_AD) {
184     // chamfer on all edges of the selected faces, with D1 on the selected face
185     // (on first selected face, if the edge belongs to two selected faces)
186
187     int aLen = aCI.GetLength();
188     int ind = 1;
189     TopTools_MapOfShape aMap;
190     TopTools_IndexedDataMapOfShapeListOfShape M;
191     GEOMImpl_Block6Explorer::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, M);
192     for (; ind <= aLen; ind++)
193     {
194       TopoDS_Shape aShapeFace;
195       if (GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetFace(ind), aShapeFace))
196       {
197         TopoDS_Face F = TopoDS::Face(aShapeFace);
198         TopExp_Explorer Exp (F, TopAbs_EDGE);
199         for (; Exp.More(); Exp.Next()) {
200           if (!aMap.Contains(Exp.Current()))
201           {
202             TopoDS_Edge E = TopoDS::Edge(Exp.Current());
203             if (!BRepTools::IsReallyClosed(E, F) &&
204                 !BRep_Tool::Degenerated(E) &&
205                 M.FindFromKey(E).Extent() == 2)
206             {
207               if (aType == CHAMFER_SHAPE_FACES)
208               {
209                 double aD1 = aCI.GetD1();
210                 double aD2 = aCI.GetD2();
211                 fill.Add(aD1, aD2, E, F);
212               }
213               else
214               {
215                 double aD = aCI.GetD();
216                 double anAngle = aCI.GetAngle();
217                 if ( (anAngle > 0) && (anAngle < (Standard_PI/2)) )
218                   fill.AddDA(aD, anAngle, E, F);
219               }
220             }
221           }
222         }
223       }
224     }
225   }
226   else if (aType == CHAMFER_SHAPE_EDGES || aType == CHAMFER_SHAPE_EDGES_AD)
227   {
228     // chamfer on selected edges with lenght param D1 & D2.
229
230     int aLen = aCI.GetLength();
231     int ind = 1;
232     TopTools_MapOfShape aMap;
233     TopTools_IndexedDataMapOfShapeListOfShape M;
234     GEOMImpl_Block6Explorer::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, M);
235     for (; ind <= aLen; ind++)
236     {
237       TopoDS_Shape aShapeEdge;
238       if (GEOMImpl_ILocalOperations::GetSubShape(aShapeBase, aCI.GetEdge(ind), aShapeEdge))
239       {
240         TopoDS_Edge E = TopoDS::Edge(aShapeEdge);
241         const TopTools_ListOfShape& aFacesList = M.FindFromKey(E);
242         TopoDS_Face F = TopoDS::Face( aFacesList.First() );
243         if (aType == CHAMFER_SHAPE_EDGES)
244         {
245           double aD1 = aCI.GetD1();
246           double aD2 = aCI.GetD2();
247           fill.Add(aD1, aD2, E, F);
248         }
249         else
250         {
251           double aD = aCI.GetD();
252           double anAngle = aCI.GetAngle();
253           if ( (anAngle > 0) && (anAngle < (Standard_PI/2)) )
254             fill.AddDA(aD, anAngle, E, F);
255         }
256       }
257     }
258   }
259   else {
260   }
261
262   fill.Build();
263   if (!fill.IsDone()) {
264     StdFail_NotDone::Raise("Chamfer can not be computed on the given shape with the given parameters");
265   }
266   aShape = fill.Shape();
267
268   if (aShape.IsNull()) return 0;
269
270   // reduce tolerances
271   ShapeFix_ShapeTolerance aSFT;
272   aSFT.LimitTolerance(aShape, Precision::Confusion(),
273                       Precision::Confusion(), TopAbs_SHAPE);
274   Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
275   aSfs->Perform();
276   aShape = aSfs->Shape();
277
278   // fix SameParameter flag
279   BRepLib::SameParameter(aShape, 1.E-5, Standard_True);
280
281   aFunction->SetValue(aShape);
282
283   log.SetTouched(Label());
284
285   return 1;
286 }
287
288
289 //=======================================================================
290 //function :  GEOMImpl_ChamferDriver_Type_
291 //purpose  :
292 //=======================================================================
293 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ChamferDriver_Type_()
294 {
295
296   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
297   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
298   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
299   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
300   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
301   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
302
303
304   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
305   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ChamferDriver",
306                                                          sizeof(GEOMImpl_ChamferDriver),
307                                                          1,
308                                                          (Standard_Address)_Ancestors,
309                                                          (Standard_Address)NULL);
310
311   return _aType;
312 }
313
314 //=======================================================================
315 //function : DownCast
316 //purpose  :
317 //=======================================================================
318 const Handle(GEOMImpl_ChamferDriver) Handle(GEOMImpl_ChamferDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
319 {
320   Handle(GEOMImpl_ChamferDriver) _anOtherObject;
321
322   if (!AnObject.IsNull()) {
323      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ChamferDriver))) {
324        _anOtherObject = Handle(GEOMImpl_ChamferDriver)((Handle(GEOMImpl_ChamferDriver)&)AnObject);
325      }
326   }
327
328   return _anOtherObject;
329 }