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_CompositeBoolean.h"
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_ResultCompSolid.h>
25 #include <ModelAPI_Tools.h>
27 #include <GeomAlgoAPI_Boolean.h>
28 #include <GeomAlgoAPI_MakeShapeList.h>
29 #include <GeomAlgoAPI_PaveFiller.h>
30 #include <GeomAlgoAPI_ShapeTools.h>
32 #include <GeomAPI_ShapeExplorer.h>
36 //=================================================================================================
37 void FeaturesPlugin_CompositeBoolean::initBooleanAttributes()
39 myFeature->data()->addAttribute(OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
42 //=================================================================================================
43 void FeaturesPlugin_CompositeBoolean::executeCompositeBoolean()
46 ListOfShape aGenBaseShapes;
47 ListOfMakeShape aGenMakeShapes;
48 if(!makeGeneration(aGenBaseShapes, aGenMakeShapes)) {
54 for(ListOfMakeShape::const_iterator
55 anIt = aGenMakeShapes.cbegin(); anIt != aGenMakeShapes.cend(); ++anIt) {
56 aTools.push_back((*anIt)->shape());
60 ListOfShape aBooleanObjects;
61 ListOfMakeShape aBooleanMakeShapes;
62 if(!makeBoolean(aTools, aBooleanObjects, aBooleanMakeShapes)) {
66 if(myOperationType == BOOL_FUSE) {
67 aTools.splice(aTools.begin(), aBooleanObjects);
68 aBooleanObjects.splice(aBooleanObjects.begin(), aTools, aTools.begin());
73 ListOfShape::const_iterator aBoolObjIt = aBooleanObjects.cbegin();
74 ListOfMakeShape::const_iterator aBoolMSIt = aBooleanMakeShapes.cbegin();
75 for(; aBoolObjIt != aBooleanObjects.cend() && aBoolMSIt != aBooleanMakeShapes.cend();
76 ++aBoolObjIt, ++aBoolMSIt) {
80 ResultBodyPtr aResultBody = myFeature->document()->createBody(myFeature->data(), aResultIndex);
81 aResultBody->storeModified(*aBoolObjIt, (*aBoolMSIt)->shape(), aTag);
85 // Store generation history.
86 ListOfShape::const_iterator aGenBaseIt = aGenBaseShapes.cbegin();
87 ListOfMakeShape::const_iterator aGenMSIt = aGenMakeShapes.cbegin();
88 for(; aGenBaseIt != aGenBaseShapes.cend() && aGenMSIt != aGenMakeShapes.cend();
89 ++aGenBaseIt, ++aGenMSIt) {
90 storeGenerationHistory(aResultBody, *aGenBaseIt, *aGenMSIt, aTag);
94 storeModificationHistory(aResultBody, *aBoolObjIt, aTools, *aBoolMSIt, aModTag);
96 myFeature->setResult(aResultBody, aResultIndex++);
99 myFeature->removeResults(aResultIndex);
102 //=================================================================================================
103 bool FeaturesPlugin_CompositeBoolean::makeBoolean(const ListOfShape& theTools,
104 ListOfShape& theObjects,
105 ListOfMakeShape& theMakeShapes)
108 ListOfShape anObjects, anEdgesAndFaces, aCompSolids;
109 std::map<GeomShapePtr, ListOfShape> aCompSolidsObjects;
110 AttributeSelectionListPtr anObjectsSelList = myFeature->selectionList(OBJECTS_ID());
111 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
112 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
113 GeomShapePtr anObject = anObjectAttr->value();
114 if(!anObject.get()) {
115 myFeature->setError("Error: Could not get object.");
118 ResultPtr aContext = anObjectAttr->context();
119 ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aContext);
120 if(aResCompSolidPtr.get()) {
121 GeomShapePtr aContextShape = aResCompSolidPtr->shape();
122 std::map<GeomShapePtr, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
123 for(; anIt != aCompSolidsObjects.end(); anIt++) {
124 if(anIt->first->isEqual(aContextShape)) {
125 aCompSolidsObjects[anIt->first].push_back(anObject);
129 if(anIt == aCompSolidsObjects.end()) {
130 aCompSolidsObjects[aContextShape].push_back(anObject);
131 aCompSolids.push_back(aContextShape);
134 if(anObject->shapeType() == GeomAPI_Shape::EDGE ||
135 anObject->shapeType() == GeomAPI_Shape::FACE) {
136 anEdgesAndFaces.push_back(anObject);
138 anObjects.push_back(anObject);
143 switch(myOperationType) {
145 if((anObjects.empty() && aCompSolidsObjects.empty()) || theTools.empty()) {
146 myFeature->setError("Error: Not enough objects for boolean operation.");
150 // For solids cut each object with all tools.
151 for(ListOfShape::const_iterator
152 anIt = anObjects.cbegin(); anIt != anObjects.cend(); ++anIt) {
153 GeomShapePtr anObject = *anIt;
154 ListOfShape aListWithObject;
155 aListWithObject.push_back(anObject);
156 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aListWithObject,
158 GeomAlgoAPI_Boolean::BOOL_CUT));
160 // Checking that the algorithm worked properly.
161 if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
162 myFeature->setError("Error: Boolean algorithm failed.");
166 if(GeomAlgoAPI_ShapeTools::volume(aBoolAlgo->shape()) > 1.e-27) {
167 theObjects.push_back(anObject);
168 theMakeShapes.push_back(aBoolAlgo);
172 // Compsolids handling
173 for(std::map<GeomShapePtr, ListOfShape>::const_iterator anIt = aCompSolidsObjects.cbegin();
174 anIt != aCompSolidsObjects.cend(); ++anIt) {
175 GeomShapePtr aCompSolid = anIt->first;
176 const ListOfShape& aUsedShapes = anIt->second;
178 // Collecting solids from compsolids which will not be modified in boolean operation.
179 ListOfShape aShapesToAdd;
180 for(GeomAPI_ShapeExplorer
181 anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
182 GeomShapePtr aSolidInCompSolid = anExp.current();
183 ListOfShape::const_iterator aUsedShapesIt = aUsedShapes.cbegin();
184 for(; aUsedShapesIt != aUsedShapes.cend(); ++aUsedShapesIt) {
185 if(aSolidInCompSolid->isEqual(*aUsedShapesIt)) {
189 if(aUsedShapesIt == aUsedShapes.end()) {
190 aShapesToAdd.push_back(aSolidInCompSolid);
194 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aUsedShapes,
196 GeomAlgoAPI_Boolean::BOOL_CUT));
198 // Checking that the algorithm worked properly.
199 if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
200 myFeature->setError("Error: Boolean algorithm failed.");
204 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
205 aMakeShapeList->appendAlgo(aBoolAlgo);
207 // Add result to not used solids from compsolid.
208 aShapesToAdd.push_back(aBoolAlgo->shape());
209 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
210 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
211 if(!aFillerAlgo->isDone() || aFillerAlgo->shape()->isNull() || !aFillerAlgo->isValid()) {
212 myFeature->setError("Error: PaveFiller algorithm failed.");
216 aMakeShapeList->appendAlgo(aFillerAlgo);
218 if(GeomAlgoAPI_ShapeTools::volume(aFillerAlgo->shape()) > 1.e-27) {
219 theObjects.push_back(aCompSolid);
220 theMakeShapes.push_back(aMakeShapeList);
227 theObjects.insert(theObjects.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
228 theObjects.insert(theObjects.end(), anObjects.begin(), anObjects.end());
229 theObjects.insert(theObjects.end(), aCompSolids.begin(), aCompSolids.end());
231 // Filter edges and faces in tools.
233 for(ListOfShape::const_iterator anIt = theTools.cbegin(); anIt != theTools.cend(); ++anIt) {
234 if((*anIt)->shapeType() == GeomAPI_Shape::EDGE ||
235 (*anIt)->shapeType() == GeomAPI_Shape::FACE) {
236 anEdgesAndFaces.push_back(*anIt);
238 aTools.push_back(*anIt);
242 if((anObjects.size() + aTools.size() +
243 aCompSolidsObjects.size() + anEdgesAndFaces.size()) < 2) {
244 myFeature->setError("Error: Not enough objects for boolean operation.");
248 // Collecting all solids which will be fused.
249 ListOfShape aSolidsToFuse;
250 aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
251 aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
253 // Collecting solids from compsolids which will not be
254 // modified in boolean operation and will be added to result.
255 ListOfShape aShapesToAdd;
256 for(std::map<GeomShapePtr, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
257 anIt != aCompSolidsObjects.end(); anIt++) {
258 GeomShapePtr aCompSolid = anIt->first;
259 ListOfShape& aUsedShapes = anIt->second;
260 aSolidsToFuse.insert(aSolidsToFuse.end(), aUsedShapes.begin(), aUsedShapes.end());
262 // Collect solids from compsolid which will not be modified in boolean operation.
263 for(GeomAPI_ShapeExplorer
264 anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
265 GeomShapePtr aSolidInCompSolid = anExp.current();
266 ListOfShape::iterator anIt = aUsedShapes.begin();
267 for(; anIt != aUsedShapes.end(); anIt++) {
268 if(aSolidInCompSolid->isEqual(*anIt)) {
272 if(anIt == aUsedShapes.end()) {
273 aShapesToAdd.push_back(aSolidInCompSolid);
278 // Cut edges and faces(if we have any) with solids.
279 ListOfShape aCutTools;
280 aCutTools.insert(aCutTools.end(), anObjects.begin(), anObjects.end());
281 aCutTools.insert(aCutTools.end(), aCompSolids.begin(), aCompSolids.end());
282 aCutTools.insert(aCutTools.end(), aTools.begin(), aTools.end());
284 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
285 if(!anEdgesAndFaces.empty() && !aCutTools.empty()) {
286 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces,
288 GeomAlgoAPI_Boolean::BOOL_CUT));
289 if(aCutAlgo->isDone() && !aCutAlgo->shape()->isNull() && aCutAlgo->isValid()) {
290 anEdgesAndFaces.clear();
291 anEdgesAndFaces.push_back(aCutAlgo->shape());
292 aMakeShapeList->appendAlgo(aCutAlgo);
296 // If we have compsolids then cut with not used solids all others.
297 if(!aShapesToAdd.empty()) {
298 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(aSolidsToFuse,
300 GeomAlgoAPI_Boolean::BOOL_CUT));
301 if(aCutAlgo->isDone() && GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-27) {
302 aSolidsToFuse.clear();
303 aSolidsToFuse.push_back(aCutAlgo->shape());
304 aMakeShapeList->appendAlgo(aCutAlgo);
308 // Fuse all objects and all tools.
309 GeomShapePtr aFusedShape;
310 if(aSolidsToFuse.size() == 1) {
311 aFusedShape = aSolidsToFuse.front();
312 } else if(aSolidsToFuse.size() > 1){
314 anObjects.push_back(aSolidsToFuse.front());
315 aSolidsToFuse.pop_front();
316 aTools = aSolidsToFuse;
318 std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
320 GeomAlgoAPI_Boolean::BOOL_FUSE));
322 // Checking that the algorithm worked properly.
323 if(!aFuseAlgo->isDone() || aFuseAlgo->shape()->isNull() || !aFuseAlgo->isValid()) {
324 myFeature->setError("Error: Boolean algorithm failed.");
328 aFusedShape = aFuseAlgo->shape();
329 aMakeShapeList->appendAlgo(aFuseAlgo);
332 // Combine result with not used solids from compsolid and edges and faces (if we have any).
333 aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
334 if(!aShapesToAdd.empty()) {
335 if(aFusedShape.get()) {
336 aShapesToAdd.push_back(aFusedShape);
339 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
340 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
341 if(!aFillerAlgo->isDone() || aFillerAlgo->shape()->isNull() || !aFillerAlgo->isValid()) {
342 myFeature->setError("Error: PaveFiller algorithm failed.");
346 aMakeShapeList->appendAlgo(aFillerAlgo);
349 theMakeShapes.push_back(aMakeShapeList);
357 //=================================================================================================
358 void FeaturesPlugin_CompositeBoolean::storeModificationHistory(ResultBodyPtr theResultBody,
359 const GeomShapePtr theObject,
360 const ListOfShape& theTools,
361 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
364 int aModTag = theTag;
365 int anEdgesAndFacesTag = ++aModTag;
366 int aDelTag = ++anEdgesAndFacesTag;
369 const std::string aModName = "Modfied";
371 ListOfShape aTools = theTools;
372 aTools.push_back(theObject);
374 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMap = theMakeShape->mapOfSubShapes();
378 for(ListOfShape::const_iterator anIt = aTools.begin(); anIt != aTools.end(); anIt++) {
379 if((*anIt)->shapeType() == GeomAPI_Shape::EDGE) {
380 aTag = anEdgesAndFacesTag;
381 aName = aModName + "_Edge";
383 else if((*anIt)->shapeType() == GeomAPI_Shape::FACE) {
384 aTag = anEdgesAndFacesTag;
385 aName = aModName + "_Face";
390 theResultBody->loadAndOrientModifiedShapes(theMakeShape.get(), *anIt,
391 (*anIt)->shapeType() == GeomAPI_Shape::EDGE ?
392 GeomAPI_Shape::EDGE : GeomAPI_Shape::FACE, aTag, aName, *aMap.get(), false, false, true);
393 theResultBody->loadDeletedShapes(theMakeShape.get(), *anIt, GeomAPI_Shape::FACE, aDelTag);