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