1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "FeaturesPlugin_BooleanSmash.h"
23 #include "FeaturesPlugin_Tools.h"
25 #include <ModelAPI_ResultBody.h>
26 #include <ModelAPI_AttributeSelectionList.h>
27 #include <ModelAPI_Tools.h>
29 #include <GeomAlgoAPI_Boolean.h>
30 #include <GeomAlgoAPI_CompoundBuilder.h>
31 #include <GeomAlgoAPI_MakeShapeList.h>
32 #include <GeomAlgoAPI_PaveFiller.h>
33 #include <GeomAlgoAPI_ShapeTools.h>
34 #include <GeomAlgoAPI_Tools.h>
36 #include <GeomAPI_ShapeExplorer.h>
37 #include <GeomAPI_ShapeIterator.h>
39 //==================================================================================================
40 FeaturesPlugin_BooleanSmash::FeaturesPlugin_BooleanSmash()
41 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_SMASH)
45 //==================================================================================================
46 void FeaturesPlugin_BooleanSmash::initAttributes()
48 data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
49 data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
52 //==================================================================================================
53 void FeaturesPlugin_BooleanSmash::execute()
56 ListOfShape anObjects, aTools;
57 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
60 AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECT_LIST_ID());
61 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
62 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
63 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
67 ResultPtr aContext = anObjectAttr->context();
68 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
69 if (aResCompSolidPtr.get())
71 std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
73 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
74 anIt = aCompSolidsObjects.begin();
75 for (; anIt != aCompSolidsObjects.end(); anIt++) {
76 if (anIt->first->isEqual(aContextShape)) {
77 aCompSolidsObjects[anIt->first].push_back(anObject);
81 if (anIt == aCompSolidsObjects.end()) {
82 aCompSolidsObjects[aContextShape].push_back(anObject);
86 anObjects.push_back(anObject);
91 AttributeSelectionListPtr aToolsSelList = selectionList(TOOL_LIST_ID());
92 for(int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
93 AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
94 GeomShapePtr aTool = aToolAttr->value();
98 aTools.push_back(aTool);
101 int aResultIndex = 0;
103 if((anObjects.empty() && aCompSolidsObjects.empty())
105 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
106 setError(aFeatureError);
110 // List of original shapes for naming.
111 ListOfShape anOriginalShapes;
112 anOriginalShapes.insert(anOriginalShapes.end(), anObjects.begin(), anObjects.end());
113 anOriginalShapes.insert(anOriginalShapes.end(), aTools.begin(), aTools.end());
115 // Collecting all shapes which will be smashed.
116 ListOfShape aShapesToSmash;
117 aShapesToSmash.insert(aShapesToSmash.end(), anObjects.begin(), anObjects.end());
119 // Collecting solids from compsolids which will not be modified in
120 // boolean operation and will be added to result.
121 ListOfShape aShapesToAdd;
122 for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
123 anIt = aCompSolidsObjects.begin();
124 anIt != aCompSolidsObjects.end();
127 std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
128 ListOfShape& aUsedInOperationSolids = anIt->second;
129 anOriginalShapes.push_back(aCompSolid);
130 aShapesToSmash.insert(aShapesToSmash.end(),
131 aUsedInOperationSolids.begin(),
132 aUsedInOperationSolids.end());
134 // Collect solids from compsolid which will not be modified in boolean operation.
135 for (GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID);
139 std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
140 ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
141 for (; anIt != aUsedInOperationSolids.end(); anIt++) {
142 if (aSolidInCompSolid->isEqual(*anIt)) {
146 if (anIt == aUsedInOperationSolids.end()) {
147 aShapesToAdd.push_back(aSolidInCompSolid);
152 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
153 if (!aShapesToAdd.empty()) {
154 // Cut objects with not used solids.
155 std::shared_ptr<GeomAlgoAPI_Boolean> anObjectsCutAlgo(
156 new GeomAlgoAPI_Boolean(aShapesToSmash,
158 GeomAlgoAPI_Boolean::BOOL_CUT));
160 if (GeomAlgoAPI_ShapeTools::volume(anObjectsCutAlgo->shape()) > 1.e-27) {
161 aShapesToSmash.clear();
162 aShapesToSmash.push_back(anObjectsCutAlgo->shape());
163 aMakeShapeList->appendAlgo(anObjectsCutAlgo);
166 // Cut tools with not used solids.
167 std::shared_ptr<GeomAlgoAPI_Boolean> aToolsCutAlgo(
168 new GeomAlgoAPI_Boolean(aTools,
170 GeomAlgoAPI_Boolean::BOOL_CUT));
172 if (GeomAlgoAPI_ShapeTools::volume(aToolsCutAlgo->shape()) > 1.e-27) {
174 aTools.push_back(aToolsCutAlgo->shape());
175 aMakeShapeList->appendAlgo(aToolsCutAlgo);
179 // Cut objects with tools.
180 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(
181 new GeomAlgoAPI_Boolean(aShapesToSmash,
183 GeomAlgoAPI_Boolean::BOOL_CUT));
185 // Checking that the algorithm worked properly.
186 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
191 aMakeShapeList->appendAlgo(aBoolAlgo);
193 // Put all (cut result, tools and not used solids) to PaveFiller.
194 GeomShapePtr aShape = aBoolAlgo->shape();
195 GeomAPI_ShapeIterator anIt(aShape);
196 if (anIt.more() || aShape->shapeType() == GeomAPI_Shape::VERTEX) {
197 aShapesToAdd.push_back(aShape);
199 aShapesToAdd.insert(aShapesToAdd.end(), aTools.begin(), aTools.end());
201 if (aShapesToAdd.size() == 1) {
202 aShape = aShapesToAdd.front();
205 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
206 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
207 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
212 aShape = aFillerAlgo->shape();
213 aMakeShapeList->appendAlgo(aFillerAlgo);
216 std::shared_ptr<GeomAPI_Shape> aFrontShape = anOriginalShapes.front();
217 anOriginalShapes.pop_front();
218 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
220 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
226 setResult(aResultBody, aResultIndex);
229 FeaturesPlugin_Tools::loadDeletedShapes(aResultBody,
235 // remove the rest results if there were produced in the previous pass
236 removeResults(aResultIndex);