Salome HOME
Merge branch 'occ/shaper2smesh'
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Defeaturing.cpp
1 // Copyright (C) 2020  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 <FeaturesPlugin_Defeaturing.h>
21 #include <FeaturesPlugin_Tools.h>
22
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_Tools.h>
25
26 #include <GeomAPI_ShapeExplorer.h>
27
28 #include <GeomAlgoAPI_CompoundBuilder.h>
29 #include <GeomAlgoAPI_Defeaturing.h>
30 #include <GeomAlgoAPI_MakeShapeList.h>
31 #include <GeomAlgoAPI_Tools.h>
32
33 #include <unordered_map>
34
35
36 FeaturesPlugin_Defeaturing::FeaturesPlugin_Defeaturing()
37 {
38 }
39
40 void FeaturesPlugin_Defeaturing::initAttributes()
41 {
42   data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
43 }
44
45
46 void FeaturesPlugin_Defeaturing::execute()
47 {
48   typedef std::unordered_map<GeomShapePtr, ListOfShape,
49                              GeomAPI_Shape::Hash, GeomAPI_Shape::Equal> SolidFaces;
50   SolidFaces aBodiesAndFacesToRemove;
51
52   // getting objects and sort them according to parent solids
53   AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECT_LIST_ID());
54   for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); ++anObjectsIndex) {
55     AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
56     GeomShapePtr anObject = anObjectAttr->value();
57     if (!anObject)
58       return;
59
60     ResultPtr aContext = anObjectAttr->context();
61     if (!aContext.get())
62       return;
63
64     ResultBodyPtr aCtxOwner = ModelAPI_Tools::bodyOwner(aContext, true);
65     GeomShapePtr aParent = aCtxOwner ? aCtxOwner->shape() : aContext->shape();
66     aBodiesAndFacesToRemove[aParent].push_back(anObject);
67   }
68
69   // Perform Defeaturing algorithm
70   GeomAlgoAPI_MakeShapeList aMakeShapeList;
71   std::shared_ptr<GeomAlgoAPI_Defeaturing> anAlgo;
72   int aResultIndex = 0;
73   std::string anError;
74
75   std::vector<FeaturesPlugin_Tools::ResultBaseAlgo> aResultBaseAlgoList;
76   ListOfShape anOriginalShapesList, aResultShapesList;
77
78   for (SolidFaces::iterator anIt = aBodiesAndFacesToRemove.begin();
79        anIt != aBodiesAndFacesToRemove.end(); ++anIt) {
80     GeomShapePtr aParent = anIt->first;
81     anAlgo.reset(new GeomAlgoAPI_Defeaturing(aParent, anIt->second));
82     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(anAlgo, getKind(), anError)) {
83       setError(anError);
84       return;
85     }
86
87     GeomShapePtr aResult = anAlgo->shape();
88     ListOfShape aBaseShapes;
89     for (GeomAPI_ShapeExplorer anExp(aParent, GeomAPI_Shape::SOLID); anExp.more(); anExp.next())
90       aBaseShapes.push_back(anExp.current());
91
92     std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
93     FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShapes, ListOfShape(),
94                                              anAlgo, aResult, "Defeaturing");
95
96     setResult(aResultBody, aResultIndex);
97     aResultIndex++;
98
99     FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
100     aRBA.resultBody = aResultBody;
101     aRBA.baseShape = aParent;
102     aRBA.makeShape = anAlgo;
103     aResultBaseAlgoList.push_back(aRBA);
104     aResultShapesList.push_back(aResult);
105     anOriginalShapesList.insert(anOriginalShapesList.end(), aBaseShapes.begin(), aBaseShapes.end());
106   }
107
108   // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
109   // result shape has been deleted, but in another it was modified or stayed.
110   GeomShapePtr aResultShapesCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
111   FeaturesPlugin_Tools::loadDeletedShapes(aResultBaseAlgoList,
112       anOriginalShapesList, aResultShapesCompound);
113
114   removeResults(aResultIndex);
115 }