1 // Copyright (C) 2020 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>
21 #include <FeaturesPlugin_Tools.h>
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_Tools.h>
26 #include <GeomAPI_ShapeExplorer.h>
28 #include <GeomAlgoAPI_CompoundBuilder.h>
29 #include <GeomAlgoAPI_Defeaturing.h>
30 #include <GeomAlgoAPI_MakeShapeList.h>
31 #include <GeomAlgoAPI_Tools.h>
33 #include <unordered_map>
36 FeaturesPlugin_Defeaturing::FeaturesPlugin_Defeaturing()
40 void FeaturesPlugin_Defeaturing::initAttributes()
42 data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
46 void FeaturesPlugin_Defeaturing::execute()
48 typedef std::unordered_map<GeomShapePtr, ListOfShape,
49 GeomAPI_Shape::Hash, GeomAPI_Shape::Equal> SolidFaces;
50 SolidFaces aBodiesAndFacesToRemove;
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();
60 ResultPtr aContext = anObjectAttr->context();
64 ResultBodyPtr aCtxOwner = ModelAPI_Tools::bodyOwner(aContext, true);
65 GeomShapePtr aParent = aCtxOwner ? aCtxOwner->shape() : aContext->shape();
66 aBodiesAndFacesToRemove[aParent].push_back(anObject);
69 // Perform Defeaturing algorithm
70 GeomAlgoAPI_MakeShapeList aMakeShapeList;
71 std::shared_ptr<GeomAlgoAPI_Defeaturing> anAlgo;
75 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo> aResultBaseAlgoList;
76 ListOfShape anOriginalShapesList, aResultShapesList;
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)) {
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());
92 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
93 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShapes, ListOfShape(),
94 anAlgo, aResult, "Defeaturing");
96 setResult(aResultBody, aResultIndex);
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());
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);
114 removeResults(aResultIndex);