1 // Copyright (C) 2014-2019 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_BooleanFuse.h"
22 #include "FeaturesPlugin_Tools.h"
24 #include <ModelAPI_ResultBody.h>
25 #include <ModelAPI_AttributeBoolean.h>
26 #include <ModelAPI_AttributeSelectionList.h>
27 #include <ModelAPI_AttributeString.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Tools.h>
30 #include <ModelAPI_Validator.h>
32 #include <GeomAlgoAPI_Boolean.h>
33 #include <GeomAlgoAPI_MakeShapeList.h>
34 #include <GeomAlgoAPI_PaveFiller.h>
35 #include <GeomAlgoAPI_ShapeTools.h>
36 #include <GeomAlgoAPI_Tools.h>
37 #include <GeomAlgoAPI_UnifySameDomain.h>
38 #include <GeomAPI_ShapeExplorer.h>
40 //==================================================================================================
41 FeaturesPlugin_BooleanFuse::FeaturesPlugin_BooleanFuse()
42 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_FUSE)
46 //==================================================================================================
47 void FeaturesPlugin_BooleanFuse::initAttributes()
49 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
51 data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
52 data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
54 data()->addAttribute(REMOVE_INTERSECTION_EDGES_ID(), ModelAPI_AttributeBoolean::typeId());
56 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), OBJECT_LIST_ID());
57 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID());
60 //==================================================================================================
61 void FeaturesPlugin_BooleanFuse::execute()
64 ListOfShape anObjects, aTools, anEdgesAndFaces;
65 std::map<GeomShapePtr, ListOfShape> aCompSolidsObjects;
67 bool isSimpleCreation = false;
69 AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
70 if (aCreationMethodAttr.get()
71 && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE())
73 isSimpleCreation = true;
77 AttributeSelectionListPtr anObjectsSelList =
78 selectionList(FeaturesPlugin_Boolean::OBJECT_LIST_ID());
79 for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
80 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
81 GeomShapePtr anObject = anObjectAttr->value();
82 if (!anObject.get()) {
85 ResultPtr aContext = anObjectAttr->context();
86 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
88 && aResCompSolidPtr.get()
89 && aResCompSolidPtr->shape()->shapeType() == GeomAPI_Shape::COMPSOLID)
91 GeomShapePtr aContextShape = aResCompSolidPtr->shape();
92 std::map<GeomShapePtr, ListOfShape>::iterator
93 anIt = aCompSolidsObjects.begin();
94 for (; anIt != aCompSolidsObjects.end(); anIt++) {
95 if (anIt->first->isEqual(aContextShape)) {
96 aCompSolidsObjects[anIt->first].push_back(anObject);
100 if (anIt == aCompSolidsObjects.end()) {
101 aCompSolidsObjects[aContextShape].push_back(anObject);
104 if (anObject->shapeType() == GeomAPI_Shape::EDGE
105 || anObject->shapeType() == GeomAPI_Shape::FACE) {
106 anEdgesAndFaces.push_back(anObject);
108 anObjects.push_back(anObject);
114 if (!isSimpleCreation) {
115 AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Boolean::TOOL_LIST_ID());
116 for (int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
117 AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
118 GeomShapePtr aTool = aToolAttr->value();
119 if (aTool->shapeType() == GeomAPI_Shape::EDGE
120 || aTool->shapeType() == GeomAPI_Shape::FACE)
122 anEdgesAndFaces.push_back(aTool);
124 aTools.push_back(aTool);
129 if ((anObjects.size() + aTools.size() +
130 aCompSolidsObjects.size() + anEdgesAndFaces.size()) < 2) {
131 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
132 setError(aFeatureError);
136 // Collecting all solids which will be fused.
137 ListOfShape aSolidsToFuse;
138 aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
139 aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
141 // Collecting solids from compsolids which will not be modified
142 // in boolean operation and will be added to result.
143 ListOfShape aShapesToAdd;
144 for (std::map<GeomShapePtr, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
145 anIt != aCompSolidsObjects.end();
148 GeomShapePtr aCompSolid = anIt->first;
149 ListOfShape& aUsedInOperationSolids = anIt->second;
150 aSolidsToFuse.insert(aSolidsToFuse.end(), aUsedInOperationSolids.begin(),
151 aUsedInOperationSolids.end());
153 // Collect solids from compsolid which will not be modified in boolean operation.
154 for (GeomAPI_ShapeExplorer
155 anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
156 GeomShapePtr aSolidInCompSolid = anExp.current();
157 ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
158 for (; anIt != aUsedInOperationSolids.end(); anIt++) {
159 if (aSolidInCompSolid->isEqual(*anIt)) {
163 if (anIt == aUsedInOperationSolids.end()) {
164 aShapesToAdd.push_back(aSolidInCompSolid);
169 ListOfShape anOriginalShapes = aSolidsToFuse;
170 anOriginalShapes.insert(anOriginalShapes.end(), aShapesToAdd.begin(), aShapesToAdd.end());
172 // Cut edges and faces(if we have any) with solids.
173 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
174 GeomShapePtr aCuttedEdgesAndFaces;
175 if (!anEdgesAndFaces.empty()) {
176 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces,
177 anOriginalShapes, GeomAlgoAPI_Boolean::BOOL_CUT));
178 if (aCutAlgo->isDone()) {
179 aCuttedEdgesAndFaces = aCutAlgo->shape();
180 aMakeShapeList->appendAlgo(aCutAlgo);
183 anOriginalShapes.insert(anOriginalShapes.end(), anEdgesAndFaces.begin(),
184 anEdgesAndFaces.end());
186 // If we have compsolids then cut with not used solids all others.
187 if (!aShapesToAdd.empty()) {
188 aSolidsToFuse.clear();
189 for (ListOfShape::iterator
190 anIt = anOriginalShapes.begin(); anIt != anOriginalShapes.end(); anIt++) {
191 ListOfShape aOneObjectList;
192 aOneObjectList.push_back(*anIt);
193 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
194 new GeomAlgoAPI_Boolean(aOneObjectList, aShapesToAdd, GeomAlgoAPI_Boolean::BOOL_CUT));
196 if (GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-27) {
197 aSolidsToFuse.push_back(aCutAlgo->shape());
198 aMakeShapeList->appendAlgo(aCutAlgo);
203 if (!aSolidsToFuse.empty()) {
205 anObjects.push_back(aSolidsToFuse.back());
206 aSolidsToFuse.pop_back();
207 aTools = aSolidsToFuse;
210 // Fuse all objects and all tools.
212 if (anObjects.size() == 1 && aTools.empty()) {
213 aShape = anObjects.front();
214 } else if (anObjects.empty() && aTools.size() == 1) {
215 aShape = aTools.front();
216 } else if ((anObjects.size() + aTools.size()) > 1) {
217 std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
219 GeomAlgoAPI_Boolean::BOOL_FUSE));
221 // Checking that the algorithm worked properly.
222 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFuseAlgo, getKind(), anError)) {
227 aShape = aFuseAlgo->shape();
228 aMakeShapeList->appendAlgo(aFuseAlgo);
231 // Combine result with not used solids from compsolid and edges and faces (if we have any).
232 if (aCuttedEdgesAndFaces.get() && !aCuttedEdgesAndFaces->isNull()) {
233 aShapesToAdd.push_back(aCuttedEdgesAndFaces);
235 aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
237 if (!aShapesToAdd.empty()) {
239 aShapesToAdd.push_back(aShape);
241 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
242 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
243 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
248 aShape = aFillerAlgo->shape();
249 aMakeShapeList->appendAlgo(aFillerAlgo);
252 bool isRemoveEdges = false;
253 AttributeBooleanPtr removeEdgesAttr = boolean(REMOVE_INTERSECTION_EDGES_ID());
254 if (removeEdgesAttr.get()) {
255 isRemoveEdges = removeEdgesAttr->value();
259 std::shared_ptr<GeomAlgoAPI_UnifySameDomain> aUnifyAlgo(
260 new GeomAlgoAPI_UnifySameDomain(aShape));
262 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aUnifyAlgo, getKind(), anError)) {
267 aShape = aUnifyAlgo->shape();
268 aMakeShapeList->appendAlgo(aUnifyAlgo);
271 int aResultIndex = 0;
273 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
275 ListOfShape anEmptyTools;
276 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
281 setResult(aResultBody, aResultIndex);
284 FeaturesPlugin_Tools::loadDeletedShapes(aResultBody,
290 // remove the rest results if there were produced in the previous pass
291 removeResults(aResultIndex);