1 // Copyright (C) 2020-2022 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <FeaturesPlugin_Defeaturing.h>
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_Tools.h>
25 #include <GeomAPI_ShapeExplorer.h>
27 #include <GeomAlgoAPI_CompoundBuilder.h>
28 #include <GeomAlgoAPI_Defeaturing.h>
29 #include <GeomAlgoAPI_MakeShapeList.h>
30 #include <GeomAlgoAPI_Tools.h>
32 #include <unordered_map>
35 FeaturesPlugin_Defeaturing::FeaturesPlugin_Defeaturing()
39 void FeaturesPlugin_Defeaturing::initAttributes()
41 data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
45 void FeaturesPlugin_Defeaturing::execute()
47 typedef std::unordered_map<GeomShapePtr, ListOfShape,
48 GeomAPI_Shape::Hash, GeomAPI_Shape::Equal> SolidFaces;
49 SolidFaces aBodiesAndFacesToRemove;
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();
59 ResultPtr aContext = anObjectAttr->context();
63 ResultBodyPtr aCtxOwner = ModelAPI_Tools::bodyOwner(aContext, true);
64 GeomShapePtr aParent = aCtxOwner ? aCtxOwner->shape() : aContext->shape();
65 aBodiesAndFacesToRemove[aParent].push_back(anObject);
68 // Perform Defeaturing algorithm
69 GeomAlgoAPI_MakeShapeList aMakeShapeList;
70 std::shared_ptr<GeomAlgoAPI_Defeaturing> anAlgo;
74 std::vector<ModelAPI_Tools::ResultBaseAlgo> aResultBaseAlgoList;
75 ListOfShape anOriginalShapesList, aResultShapesList;
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)) {
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());
91 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
92 ModelAPI_Tools::loadModifiedShapes(aResultBody, aBaseShapes, ListOfShape(),
93 anAlgo, aResult, "Defeaturing");
95 setResult(aResultBody, aResultIndex);
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());
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);
113 removeResults(aResultIndex);