Salome HOME
Add Chamfer feature
[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> aMapEdgeFace,
34                                          const bool performDistances,
35                                          const double aVal1,
36                                          const double aVal2)
37 {
38   build(theBaseSolid, theChamferShapes, aMapEdgeFace, performDistances, aVal1, aVal2);
39 }
40
41 //=================================================================================================
42 void GeomAlgoAPI_Chamfer::build(const GeomShapePtr& theBaseSolid,
43                                 const ListOfShape&  theChamferShapes,
44                                 const std::map<GeomShapePtr, GeomShapePtr> aMapEdgeFace,
45                                 const bool performDistances,
46                                 const double aVal1,
47                                 const double aVal2)
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 (aMapEdgeFace.find(*anIt) != aMapEdgeFace.end()) {
64         TopoDS_Face F = (aMapEdgeFace[*anIt])->impl<TopoDS_Face>();
65         if (!BRepTools::IsReallyClosed(E,F) && !BRep_Tool::Degenerated(E) && 
66               M.FindFromKey(E).Extent() == 2) {
67           if (performDistances) {
68               aChamferBuilder->Add(aVal1, aVal2, E, F);
69             } else {
70               aChamferBuilder->AddDA(aVal1, aVal2 * M_PI / 180., E, F);
71             }
72           }
73       } else {
74         const TopTools_ListOfShape& aFacesList = M.FindFromKey(E);
75         TopoDS_Face F = TopoDS::Face(aFacesList.First());
76         if (performDistances) {
77           aChamferBuilder->Add(aVal1, aVal2, E, F);
78         } else {
79           aChamferBuilder->AddDA(aVal1, aVal2 * M_PI / 180., E, F);
80         }
81       }
82     }
83   }
84
85   // build and get result
86   aChamferBuilder->Build();
87   if (!aChamferBuilder->IsDone())
88     return;
89   const TopoDS_Shape& aResult = aChamferBuilder->Shape();
90
91   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
92   aShape->setImpl(new TopoDS_Shape(aResult));
93   setShape(aShape);
94   setDone(true);
95 }