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);
82 if((*aBoolObjIt)->isEqual((*aBoolMSIt)->shape())) {
83 aResultBody->store((*aBoolMSIt)->shape(), false);
87 aResultBody->storeModified(*aBoolObjIt, (*aBoolMSIt)->shape(), aTag);
91 // Store generation history.
92 ListOfShape::const_iterator aGenBaseIt = aGenBaseShapes.cbegin();
93 ListOfMakeShape::const_iterator aGenMSIt = aGenMakeShapes.cbegin();
94 for(; aGenBaseIt != aGenBaseShapes.cend() && aGenMSIt != aGenMakeShapes.cend();
95 ++aGenBaseIt, ++aGenMSIt) {
96 storeGenerationHistory(aResultBody, *aGenBaseIt, *aGenMSIt, aTag);
100 storeModificationHistory(aResultBody, *aBoolObjIt, aTools, *aBoolMSIt, aModTag);
103 myFeature->setResult(aResultBody, aResultIndex++);
106 myFeature->removeResults(aResultIndex);
109 //=================================================================================================
110 bool FeaturesPlugin_CompositeBoolean::makeBoolean(const ListOfShape& theTools,
111 ListOfShape& theObjects,
112 ListOfMakeShape& theMakeShapes)
115 ListOfShape anObjects, anEdgesAndFaces, aCompSolids;
116 std::map<GeomShapePtr, ListOfShape> aCompSolidsObjects;
117 AttributeSelectionListPtr anObjectsSelList = myFeature->selectionList(OBJECTS_ID());
118 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
119 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
120 GeomShapePtr anObject = anObjectAttr->value();
121 if(!anObject.get()) {
122 myFeature->setError("Error: Could not get object.");
125 ResultPtr aContext = anObjectAttr->context();
126 ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aContext);
127 if(aResCompSolidPtr.get()) {
128 GeomShapePtr aContextShape = aResCompSolidPtr->shape();
129 std::map<GeomShapePtr, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
130 for(; anIt != aCompSolidsObjects.end(); anIt++) {
131 if(anIt->first->isEqual(aContextShape)) {
132 aCompSolidsObjects[anIt->first].push_back(anObject);
136 if(anIt == aCompSolidsObjects.end()) {
137 aCompSolidsObjects[aContextShape].push_back(anObject);
138 aCompSolids.push_back(aContextShape);
141 if(anObject->shapeType() == GeomAPI_Shape::EDGE ||
142 anObject->shapeType() == GeomAPI_Shape::FACE) {
143 anEdgesAndFaces.push_back(anObject);
145 anObjects.push_back(anObject);
150 switch(myOperationType) {
152 if((anObjects.empty() && aCompSolidsObjects.empty()) || theTools.empty()) {
153 myFeature->setError("Error: Not enough objects for boolean operation.");
157 // For solids cut each object with all tools.
158 for(ListOfShape::const_iterator
159 anIt = anObjects.cbegin(); anIt != anObjects.cend(); ++anIt) {
160 GeomShapePtr anObject = *anIt;
161 ListOfShape aListWithObject;
162 aListWithObject.push_back(anObject);
163 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aListWithObject,
165 GeomAlgoAPI_Boolean::BOOL_CUT));
167 // Checking that the algorithm worked properly.
168 if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
169 myFeature->setError("Error: Boolean algorithm failed.");
173 if(GeomAlgoAPI_ShapeTools::volume(aBoolAlgo->shape()) > 1.e-27) {
174 theObjects.push_back(anObject);
175 theMakeShapes.push_back(aBoolAlgo);
179 // Compsolids handling
180 for(std::map<GeomShapePtr, ListOfShape>::const_iterator anIt = aCompSolidsObjects.cbegin();
181 anIt != aCompSolidsObjects.cend(); ++anIt) {
182 GeomShapePtr aCompSolid = anIt->first;
183 const ListOfShape& aUsedShapes = anIt->second;
185 // Collecting solids from compsolids which will not be modified in boolean operation.
186 ListOfShape aShapesToAdd;
187 for(GeomAPI_ShapeExplorer
188 anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
189 GeomShapePtr aSolidInCompSolid = anExp.current();
190 ListOfShape::const_iterator aUsedShapesIt = aUsedShapes.cbegin();
191 for(; aUsedShapesIt != aUsedShapes.cend(); ++aUsedShapesIt) {
192 if(aSolidInCompSolid->isEqual(*aUsedShapesIt)) {
196 if(aUsedShapesIt == aUsedShapes.end()) {
197 aShapesToAdd.push_back(aSolidInCompSolid);
201 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aUsedShapes,
203 GeomAlgoAPI_Boolean::BOOL_CUT));
205 // Checking that the algorithm worked properly.
206 if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
207 myFeature->setError("Error: Boolean algorithm failed.");
211 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
212 aMakeShapeList->appendAlgo(aBoolAlgo);
214 // Add result to not used solids from compsolid.
215 aShapesToAdd.push_back(aBoolAlgo->shape());
216 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
217 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
218 if(!aFillerAlgo->isDone() || aFillerAlgo->shape()->isNull() || !aFillerAlgo->isValid()) {
219 myFeature->setError("Error: PaveFiller algorithm failed.");
223 aMakeShapeList->appendAlgo(aFillerAlgo);
225 if(GeomAlgoAPI_ShapeTools::volume(aFillerAlgo->shape()) > 1.e-27) {
226 theObjects.push_back(aCompSolid);
227 theMakeShapes.push_back(aMakeShapeList);
234 theObjects.insert(theObjects.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
235 theObjects.insert(theObjects.end(), anObjects.begin(), anObjects.end());
236 theObjects.insert(theObjects.end(), aCompSolids.begin(), aCompSolids.end());
238 // Filter edges and faces in tools.
240 for(ListOfShape::const_iterator anIt = theTools.cbegin(); anIt != theTools.cend(); ++anIt) {
241 if((*anIt)->shapeType() == GeomAPI_Shape::EDGE ||
242 (*anIt)->shapeType() == GeomAPI_Shape::FACE) {
243 anEdgesAndFaces.push_back(*anIt);
245 aTools.push_back(*anIt);
249 if((anObjects.size() + aTools.size() +
250 aCompSolidsObjects.size() + anEdgesAndFaces.size()) < 2) {
251 myFeature->setError("Error: Not enough objects for boolean operation.");
255 // Collecting all solids which will be fused.
256 ListOfShape aSolidsToFuse;
257 aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
258 aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
260 // Collecting solids from compsolids which will not be
261 // modified in boolean operation and will be added to result.
262 ListOfShape aShapesToAdd;
263 for(std::map<GeomShapePtr, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
264 anIt != aCompSolidsObjects.end(); anIt++) {
265 GeomShapePtr aCompSolid = anIt->first;
266 ListOfShape& aUsedShapes = anIt->second;
267 aSolidsToFuse.insert(aSolidsToFuse.end(), aUsedShapes.begin(), aUsedShapes.end());
269 // Collect solids from compsolid which will not be modified in boolean operation.
270 for(GeomAPI_ShapeExplorer
271 anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
272 GeomShapePtr aSolidInCompSolid = anExp.current();
273 ListOfShape::iterator anIt = aUsedShapes.begin();
274 for(; anIt != aUsedShapes.end(); anIt++) {
275 if(aSolidInCompSolid->isEqual(*anIt)) {
279 if(anIt == aUsedShapes.end()) {
280 aShapesToAdd.push_back(aSolidInCompSolid);
285 // Cut edges and faces(if we have any) with solids.
286 ListOfShape aCutTools;
287 aCutTools.insert(aCutTools.end(), anObjects.begin(), anObjects.end());
288 aCutTools.insert(aCutTools.end(), aCompSolids.begin(), aCompSolids.end());
289 aCutTools.insert(aCutTools.end(), aTools.begin(), aTools.end());
291 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
292 if(!anEdgesAndFaces.empty() && !aCutTools.empty()) {
293 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces,
295 GeomAlgoAPI_Boolean::BOOL_CUT));
296 if(aCutAlgo->isDone() && !aCutAlgo->shape()->isNull() && aCutAlgo->isValid()) {
297 anEdgesAndFaces.clear();
298 anEdgesAndFaces.push_back(aCutAlgo->shape());
299 aMakeShapeList->appendAlgo(aCutAlgo);
303 // If we have compsolids then cut with not used solids all others.
304 if(!aShapesToAdd.empty()) {
305 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(aSolidsToFuse,
307 GeomAlgoAPI_Boolean::BOOL_CUT));
308 if(aCutAlgo->isDone() && GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-27) {
309 aSolidsToFuse.clear();
310 aSolidsToFuse.push_back(aCutAlgo->shape());
311 aMakeShapeList->appendAlgo(aCutAlgo);
315 // Fuse all objects and all tools.
316 GeomShapePtr aFusedShape;
317 if(aSolidsToFuse.size() == 1) {
318 aFusedShape = aSolidsToFuse.front();
319 } else if(aSolidsToFuse.size() > 1){
321 anObjects.push_back(aSolidsToFuse.front());
322 aSolidsToFuse.pop_front();
323 aTools = aSolidsToFuse;
325 std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
327 GeomAlgoAPI_Boolean::BOOL_FUSE));
329 // Checking that the algorithm worked properly.
330 if(!aFuseAlgo->isDone() || aFuseAlgo->shape()->isNull() || !aFuseAlgo->isValid()) {
331 myFeature->setError("Error: Boolean algorithm failed.");
335 aFusedShape = aFuseAlgo->shape();
336 aMakeShapeList->appendAlgo(aFuseAlgo);
339 // Combine result with not used solids from compsolid and edges and faces (if we have any).
340 aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
341 if(!aShapesToAdd.empty()) {
342 if(aFusedShape.get()) {
343 aShapesToAdd.push_back(aFusedShape);
346 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
347 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
348 if(!aFillerAlgo->isDone() || aFillerAlgo->shape()->isNull() || !aFillerAlgo->isValid()) {
349 myFeature->setError("Error: PaveFiller algorithm failed.");
353 aMakeShapeList->appendAlgo(aFillerAlgo);
356 theMakeShapes.push_back(aMakeShapeList);
364 //=================================================================================================
365 void FeaturesPlugin_CompositeBoolean::storeModificationHistory(ResultBodyPtr theResultBody,
366 const GeomShapePtr theObject,
367 const ListOfShape& theTools,
368 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
371 int aModTag = theTag;
372 int anEdgesAndFacesTag = ++aModTag;
373 int aDelTag = ++anEdgesAndFacesTag;
376 const std::string aModName = "Modfied";
378 ListOfShape aTools = theTools;
379 aTools.push_back(theObject);
381 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMap = theMakeShape->mapOfSubShapes();
385 for(ListOfShape::const_iterator anIt = aTools.begin(); anIt != aTools.end(); anIt++) {
386 if((*anIt)->shapeType() == GeomAPI_Shape::EDGE) {
387 aTag = anEdgesAndFacesTag;
388 aName = aModName + "_Edge";
390 else if((*anIt)->shapeType() == GeomAPI_Shape::FACE) {
391 aTag = anEdgesAndFacesTag;
392 aName = aModName + "_Face";
397 theResultBody->loadAndOrientModifiedShapes(theMakeShape.get(), *anIt,
398 (*anIt)->shapeType() == GeomAPI_Shape::EDGE ?
399 GeomAPI_Shape::EDGE : GeomAPI_Shape::FACE, aTag, aName, *aMap.get(), false, false, true);
400 theResultBody->loadDeletedShapes(theMakeShape.get(), *anIt, GeomAPI_Shape::FACE, aDelTag);