Salome HOME
Fixing the coding rules issues.
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Chamfer.cpp
1 // Copyright (C) 2017-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "GeomAlgoAPI_Chamfer.h"
21
22 #include <BRep_Tool.hxx>
23 #include <BRepFilletAPI_MakeChamfer.hxx>
24 #include <BRepTools.hxx>
25 #include <TopExp.hxx>
26 #include <TopExp_Explorer.hxx>
27 #include <TopoDS.hxx>
28 #include <TopoDS_Face.hxx>
29
30 //=================================================================================================
31 GeomAlgoAPI_Chamfer::GeomAlgoAPI_Chamfer(const GeomShapePtr& theBaseSolid,
32                                          const ListOfShape&  theChamferShapes,
33                                          const std::map<GeomShapePtr, GeomShapePtr> theMapEdgeFace,
34                                          const bool performDistances,
35                                          const double theVal1,
36                                          const double theVal2)
37 {
38   build(theBaseSolid, theChamferShapes, theMapEdgeFace, performDistances, theVal1, theVal2);
39 }
40
41 //=================================================================================================
42 void GeomAlgoAPI_Chamfer::build(const GeomShapePtr& theBaseSolid,
43                                 const ListOfShape&  theChamferShapes,
44                                 const std::map<GeomShapePtr, GeomShapePtr> theMapEdgeFace,
45                                 const bool performDistances,
46                                 const double theVal1,
47                                 const double theVal2)
48 {
49   TopoDS_Shape aShapeBase = theBaseSolid->impl<TopoDS_Shape>();
50   TopTools_IndexedDataMapOfShapeListOfShape M;
51   TopExp::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, M);
52
53   // create chamfer builder
54   BRepFilletAPI_MakeChamfer* aChamferBuilder =
55       new BRepFilletAPI_MakeChamfer(aShapeBase);
56   setImpl(aChamferBuilder);
57   setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
58
59   for (ListOfShape::const_iterator anIt = theChamferShapes.begin();
60      anIt != theChamferShapes.end(); ++anIt) {
61     if ((*anIt)->isEdge()) {
62       TopoDS_Edge E = (*anIt)->impl<TopoDS_Edge>();
63       if (theMapEdgeFace.find(*anIt) != theMapEdgeFace.end()) {
64         //TopoDS_Face F = (theMapEdgeFace[*anIt])->impl<TopoDS_Face>();
65         TopoDS_Face F = (theMapEdgeFace.at(*anIt))->impl<TopoDS_Face>();
66         if (!BRepTools::IsReallyClosed(E,F) && !BRep_Tool::Degenerated(E) &&
67               M.FindFromKey(E).Extent() == 2) {
68           if (performDistances) {
69               aChamferBuilder->Add(theVal1, theVal2, E, F);
70             } else {
71               aChamferBuilder->AddDA(theVal1, theVal2 * M_PI / 180., E, F);
72             }
73           }
74       } else {
75         const TopTools_ListOfShape& aFacesList = M.FindFromKey(E);
76         TopoDS_Face F = TopoDS::Face(aFacesList.First());
77         if (performDistances) {
78           aChamferBuilder->Add(theVal1, theVal2, E, F);
79         } else {
80           aChamferBuilder->AddDA(theVal1, theVal2 * M_PI / 180., E, F);
81         }
82       }
83     }
84   }
85
86   // build and get result
87   aChamferBuilder->Build();
88   if (!aChamferBuilder->IsDone())
89     return;
90   const TopoDS_Shape& aResult = aChamferBuilder->Shape();
91
92   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
93   aShape->setImpl(new TopoDS_Shape(aResult));
94   setShape(aShape);
95   setDone(true);
96 }