Salome HOME
054cb19b5efd6e6a5e24bcd70c1334636be2b366
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Defeaturing.cpp
1 // Copyright (C) 2020-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_Defeaturing.h>
21 #include <GeomAlgoAPI_DFLoader.h>
22
23 #include <BRepAlgoAPI_Defeaturing.hxx>
24
25 GeomAlgoAPI_Defeaturing::GeomAlgoAPI_Defeaturing(const GeomShapePtr& theBaseSolid,
26                                                  const ListOfShape&  theFacesToRemove)
27 {
28   build(theBaseSolid, theFacesToRemove);
29 }
30
31 void GeomAlgoAPI_Defeaturing::build(const GeomShapePtr& theBaseSolid,
32                                     const ListOfShape&  theFacesToRemove)
33 {
34   if (!theBaseSolid || theFacesToRemove.empty())
35     return;
36
37   BRepAlgoAPI_Defeaturing* aDefeaturing = new BRepAlgoAPI_Defeaturing;
38   aDefeaturing->SetShape(theBaseSolid->impl<TopoDS_Shape>());
39   aDefeaturing->SetRunParallel(Standard_True);
40
41   // collect faces to remove
42   TopTools_ListOfShape aFaces;
43   for (ListOfShape::const_iterator anIt = theFacesToRemove.begin();
44        anIt != theFacesToRemove.end(); ++anIt)
45     aDefeaturing->AddFaceToRemove((*anIt)->impl<TopoDS_Shape>());
46
47   setImpl(aDefeaturing);
48   setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
49
50   // build and get result
51   aDefeaturing->Build();
52   if (!aDefeaturing->IsDone() || aDefeaturing->HasErrors() || aDefeaturing->HasWarnings()) {
53     std::ostringstream errors;
54     aDefeaturing->DumpErrors(errors);
55     aDefeaturing->DumpWarnings(errors);
56     myError = errors.str();
57     // the error string may end by '\n', remove it for correct translation
58     size_t aNbToRemove = 0;
59     for (std::string::reverse_iterator it = myError.rbegin();
60          it != myError.rend() && *it == '\n'; ++it)
61       ++aNbToRemove;
62     if (aNbToRemove > 0)
63       myError = myError.substr(0, myError.size() - aNbToRemove);
64     return;
65   }
66
67   TopoDS_Shape aResult = GeomAlgoAPI_DFLoader::refineResult(aDefeaturing->Shape());
68
69   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
70   aShape->setImpl(new TopoDS_Shape(aResult));
71   setShape(aShape);
72   setDone(true);
73 }