Salome HOME
129c9b223fce77be32ccf131dc1e605e128e59f7
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Chamfer.cpp
1 // Copyright (C) 2017-2023  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 #include "GeomAlgoAPI_DFLoader.h"
22
23 #include <BRep_Tool.hxx>
24 #include <BRepFilletAPI_MakeChamfer.hxx>
25 #include <BRepTools.hxx>
26 #include <TopExp.hxx>
27 #include <TopExp_Explorer.hxx>
28 #include <TopoDS.hxx>
29 #include <TopoDS_Face.hxx>
30
31 //=================================================================================================
32 GeomAlgoAPI_Chamfer::GeomAlgoAPI_Chamfer(const GeomShapePtr& theBaseSolid,
33                                          const ListOfShape&  theChamferShapes,
34                                          const std::map<GeomShapePtr, GeomShapePtr> theMapEdgeFace,
35                                          const bool performDistances,
36                                          const double theVal1,
37                                          const double theVal2)
38 {
39   build(theBaseSolid, theChamferShapes, theMapEdgeFace, performDistances, theVal1, theVal2);
40 }
41
42 //=================================================================================================
43 void GeomAlgoAPI_Chamfer::build(const GeomShapePtr& theBaseSolid,
44                                 const ListOfShape&  theChamferShapes,
45                                 const std::map<GeomShapePtr, GeomShapePtr> theMapEdgeFace,
46                                 const bool performDistances,
47                                 const double theVal1,
48                                 const double theVal2)
49 {
50   TopoDS_Shape aShapeBase = theBaseSolid->impl<TopoDS_Shape>();
51   TopTools_IndexedDataMapOfShapeListOfShape M;
52   TopExp::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, M);
53
54   // create chamfer builder
55   BRepFilletAPI_MakeChamfer* aChamferBuilder =
56       new BRepFilletAPI_MakeChamfer(aShapeBase);
57   setImpl(aChamferBuilder);
58   setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
59
60   for (ListOfShape::const_iterator anIt = theChamferShapes.begin();
61      anIt != theChamferShapes.end(); ++anIt) {
62     if ((*anIt)->isEdge()) {
63       TopoDS_Edge E = (*anIt)->impl<TopoDS_Edge>();
64       if (theMapEdgeFace.find(*anIt) != theMapEdgeFace.end()) {
65         //TopoDS_Face F = (theMapEdgeFace[*anIt])->impl<TopoDS_Face>();
66         TopoDS_Face F = (theMapEdgeFace.at(*anIt))->impl<TopoDS_Face>();
67         if (!BRepTools::IsReallyClosed(E,F) && !BRep_Tool::Degenerated(E) &&
68               M.FindFromKey(E).Extent() == 2) {
69           if (performDistances) {
70               aChamferBuilder->Add(theVal1, theVal2, E, F);
71             } else {
72               aChamferBuilder->AddDA(theVal1, theVal2 * M_PI / 180., E, F);
73             }
74           }
75       } else {
76         const TopTools_ListOfShape& aFacesList = M.FindFromKey(E);
77         TopoDS_Face F = TopoDS::Face(aFacesList.First());
78         if (performDistances) {
79           aChamferBuilder->Add(theVal1, theVal2, E, F);
80         } else {
81           aChamferBuilder->AddDA(theVal1, theVal2 * M_PI / 180., E, F);
82         }
83       }
84     }
85   }
86
87   // build and get result
88   aChamferBuilder->Build();
89   if (!aChamferBuilder->IsDone())
90     return;
91   TopoDS_Shape aResult = GeomAlgoAPI_DFLoader::refineResult(aChamferBuilder->Shape());
92
93   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
94   aShape->setImpl(new TopoDS_Shape(aResult));
95   setShape(aShape);
96   setDone(true);
97 }