1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: FeaturesPlugin_CompositeBoolean.cpp
4 // Created: 11 June 2015
5 // Author: Dmitry Bobylev
7 #include "FeaturesPlugin_CompositeBoolean.h"
9 #include <ModelAPI_AttributeSelectionList.h>
10 #include <ModelAPI_Tools.h>
12 #include <GeomAlgoAPI_Boolean.h>
13 #include <GeomAlgoAPI_MakeShapeList.h>
14 #include <GeomAlgoAPI_PaveFiller.h>
15 #include <GeomAlgoAPI_ShapeTools.h>
17 #include <GeomAPI_ShapeExplorer.h>
21 //=================================================================================================
22 void FeaturesPlugin_CompositeBoolean::initBooleanAttributes()
24 myFeature->data()->addAttribute(OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
27 //=================================================================================================
28 void FeaturesPlugin_CompositeBoolean::executeCompositeBoolean()
31 ListOfShape aGenBaseShapes;
32 ListOfMakeShape aGenMakeShapes;
33 if(!makeGeneration(aGenBaseShapes, aGenMakeShapes)) {
39 for(ListOfMakeShape::const_iterator
40 anIt = aGenMakeShapes.cbegin(); anIt != aGenMakeShapes.cend(); ++anIt) {
41 aTools.push_back((*anIt)->shape());
45 ListOfShape aBooleanObjects;
46 ListOfMakeShape aBooleanMakeShapes;
47 if(!makeBoolean(aTools, aBooleanObjects, aBooleanMakeShapes)) {
51 if(myOperationType == BOOL_FUSE) {
52 aTools.splice(aTools.begin(), aBooleanObjects);
53 aBooleanObjects.splice(aBooleanObjects.begin(), aTools, aTools.begin());
58 ListOfShape::const_iterator aBoolObjIt = aBooleanObjects.cbegin();
59 ListOfMakeShape::const_iterator aBoolMSIt = aBooleanMakeShapes.cbegin();
60 for(; aBoolObjIt != aBooleanObjects.cend() && aBoolMSIt != aBooleanMakeShapes.cend();
61 ++aBoolObjIt, ++aBoolMSIt) {
65 ResultBodyPtr aResultBody = myFeature->document()->createBody(myFeature->data(), aResultIndex);
66 aResultBody->storeModified(*aBoolObjIt, (*aBoolMSIt)->shape(), aTag);
70 // Store generation history.
71 ListOfShape::const_iterator aGenBaseIt = aGenBaseShapes.cbegin();
72 ListOfMakeShape::const_iterator aGenMSIt = aGenMakeShapes.cbegin();
73 for(; aGenBaseIt != aGenBaseShapes.cend() && aGenMSIt != aGenMakeShapes.cend();
74 ++aGenBaseIt, ++aGenMSIt) {
75 storeGenerationHistory(aResultBody, *aGenBaseIt, *aGenMSIt, aTag);
79 storeModificationHistory(aResultBody, *aBoolObjIt, aTools, *aBoolMSIt, aModTag);
81 myFeature->setResult(aResultBody, aResultIndex++);
84 myFeature->removeResults(aResultIndex);
87 //=================================================================================================
88 bool FeaturesPlugin_CompositeBoolean::makeBoolean(const ListOfShape& theTools,
89 ListOfShape& theObjects,
90 ListOfMakeShape& theMakeShapes)
93 ListOfShape anObjects, anEdgesAndFaces, aCompSolids;
94 std::map<GeomShapePtr, ListOfShape> aCompSolidsObjects;
95 AttributeSelectionListPtr anObjectsSelList = myFeature->selectionList(OBJECTS_ID());
96 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
97 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
98 GeomShapePtr anObject = anObjectAttr->value();
100 myFeature->setError("Error: Could not get object.");
103 ResultPtr aContext = anObjectAttr->context();
104 ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aContext);
105 if(aResCompSolidPtr.get()) {
106 GeomShapePtr aContextShape = aResCompSolidPtr->shape();
107 std::map<GeomShapePtr, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
108 for(; anIt != aCompSolidsObjects.end(); anIt++) {
109 if(anIt->first->isEqual(aContextShape)) {
110 aCompSolidsObjects[anIt->first].push_back(anObject);
114 if(anIt == aCompSolidsObjects.end()) {
115 aCompSolidsObjects[aContextShape].push_back(anObject);
116 aCompSolids.push_back(aContextShape);
119 if(anObject->shapeType() == GeomAPI_Shape::EDGE ||
120 anObject->shapeType() == GeomAPI_Shape::FACE) {
121 anEdgesAndFaces.push_back(anObject);
123 anObjects.push_back(anObject);
128 switch(myOperationType) {
130 if((anObjects.empty() && aCompSolidsObjects.empty()) || theTools.empty()) {
131 myFeature->setError("Error: Not enough objects for boolean operation.");
135 // For solids cut each object with all tools.
136 for(ListOfShape::const_iterator
137 anIt = anObjects.cbegin(); anIt != anObjects.cend(); ++anIt) {
138 GeomShapePtr anObject = *anIt;
139 ListOfShape aListWithObject;
140 aListWithObject.push_back(anObject);
141 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aListWithObject,
143 GeomAlgoAPI_Boolean::BOOL_CUT));
145 // Checking that the algorithm worked properly.
146 if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
147 myFeature->setError("Error: Boolean algorithm failed.");
151 if(GeomAlgoAPI_ShapeTools::volume(aBoolAlgo->shape()) > 1.e-27) {
152 theObjects.push_back(anObject);
153 theMakeShapes.push_back(aBoolAlgo);
157 // Compsolids handling
158 for(std::map<GeomShapePtr, ListOfShape>::const_iterator anIt = aCompSolidsObjects.cbegin();
159 anIt != aCompSolidsObjects.cend(); ++anIt) {
160 GeomShapePtr aCompSolid = anIt->first;
161 const ListOfShape& aUsedShapes = anIt->second;
163 // Collecting solids from compsolids which will not be modified in boolean operation.
164 ListOfShape aShapesToAdd;
165 for(GeomAPI_ShapeExplorer
166 anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
167 GeomShapePtr aSolidInCompSolid = anExp.current();
168 ListOfShape::const_iterator aUsedShapesIt = aUsedShapes.cbegin();
169 for(; aUsedShapesIt != aUsedShapes.cend(); ++aUsedShapesIt) {
170 if(aSolidInCompSolid->isEqual(*aUsedShapesIt)) {
174 if(aUsedShapesIt == aUsedShapes.end()) {
175 aShapesToAdd.push_back(aSolidInCompSolid);
179 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aUsedShapes,
181 GeomAlgoAPI_Boolean::BOOL_CUT));
183 // Checking that the algorithm worked properly.
184 if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
185 myFeature->setError("Error: Boolean algorithm failed.");
189 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
190 aMakeShapeList->appendAlgo(aBoolAlgo);
192 // Add result to not used solids from compsolid.
193 aShapesToAdd.push_back(aBoolAlgo->shape());
194 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
195 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
196 if(!aFillerAlgo->isDone() || aFillerAlgo->shape()->isNull() || !aFillerAlgo->isValid()) {
197 myFeature->setError("Error: PaveFiller algorithm failed.");
201 aMakeShapeList->appendAlgo(aFillerAlgo);
203 if(GeomAlgoAPI_ShapeTools::volume(aFillerAlgo->shape()) > 1.e-27) {
204 theObjects.push_back(aCompSolid);
205 theMakeShapes.push_back(aMakeShapeList);
212 theObjects.insert(theObjects.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
213 theObjects.insert(theObjects.end(), anObjects.begin(), anObjects.end());
214 theObjects.insert(theObjects.end(), aCompSolids.begin(), aCompSolids.end());
216 // Filter edges and faces in tools.
218 for(ListOfShape::const_iterator anIt = theTools.cbegin(); anIt != theTools.cend(); ++anIt) {
219 if((*anIt)->shapeType() == GeomAPI_Shape::EDGE ||
220 (*anIt)->shapeType() == GeomAPI_Shape::FACE) {
221 anEdgesAndFaces.push_back(*anIt);
223 aTools.push_back(*anIt);
227 if((anObjects.size() + aTools.size() +
228 aCompSolidsObjects.size() + anEdgesAndFaces.size()) < 2) {
229 myFeature->setError("Error: Not enough objects for boolean operation.");
233 // Collecting all solids which will be fused.
234 ListOfShape aSolidsToFuse;
235 aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
236 aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
238 // Collecting solids from compsolids which will not be
239 // modified in boolean operation and will be added to result.
240 ListOfShape aShapesToAdd;
241 for(std::map<GeomShapePtr, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
242 anIt != aCompSolidsObjects.end(); anIt++) {
243 GeomShapePtr aCompSolid = anIt->first;
244 ListOfShape& aUsedShapes = anIt->second;
245 aSolidsToFuse.insert(aSolidsToFuse.end(), aUsedShapes.begin(), aUsedShapes.end());
247 // Collect solids from compsolid which will not be modified in boolean operation.
248 for(GeomAPI_ShapeExplorer
249 anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
250 GeomShapePtr aSolidInCompSolid = anExp.current();
251 ListOfShape::iterator anIt = aUsedShapes.begin();
252 for(; anIt != aUsedShapes.end(); anIt++) {
253 if(aSolidInCompSolid->isEqual(*anIt)) {
257 if(anIt == aUsedShapes.end()) {
258 aShapesToAdd.push_back(aSolidInCompSolid);
263 // Cut edges and faces(if we have any) with solids.
264 ListOfShape aCutTools;
265 aCutTools.insert(aCutTools.end(), anObjects.begin(), anObjects.end());
266 aCutTools.insert(aCutTools.end(), aCompSolids.begin(), aCompSolids.end());
267 aCutTools.insert(aCutTools.end(), aTools.begin(), aTools.end());
269 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
270 if(!anEdgesAndFaces.empty() && !aCutTools.empty()) {
271 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces,
273 GeomAlgoAPI_Boolean::BOOL_CUT));
274 if(aCutAlgo->isDone() && !aCutAlgo->shape()->isNull() && aCutAlgo->isValid()) {
275 anEdgesAndFaces.clear();
276 anEdgesAndFaces.push_back(aCutAlgo->shape());
277 aMakeShapeList->appendAlgo(aCutAlgo);
281 // If we have compsolids then cut with not used solids all others.
282 if(!aShapesToAdd.empty()) {
283 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(aSolidsToFuse,
285 GeomAlgoAPI_Boolean::BOOL_CUT));
286 if(aCutAlgo->isDone() && GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-27) {
287 aSolidsToFuse.clear();
288 aSolidsToFuse.push_back(aCutAlgo->shape());
289 aMakeShapeList->appendAlgo(aCutAlgo);
293 // Fuse all objects and all tools.
294 GeomShapePtr aFusedShape;
295 if(aSolidsToFuse.size() == 1) {
296 aFusedShape = aSolidsToFuse.front();
297 } else if(aSolidsToFuse.size() > 1){
299 anObjects.push_back(aSolidsToFuse.front());
300 aSolidsToFuse.pop_front();
301 aTools = aSolidsToFuse;
303 std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
305 GeomAlgoAPI_Boolean::BOOL_FUSE));
307 // Checking that the algorithm worked properly.
308 if(!aFuseAlgo->isDone() || aFuseAlgo->shape()->isNull() || !aFuseAlgo->isValid()) {
309 myFeature->setError("Error: Boolean algorithm failed.");
313 aFusedShape = aFuseAlgo->shape();
314 aMakeShapeList->appendAlgo(aFuseAlgo);
317 // Combine result with not used solids from compsolid and edges and faces (if we have any).
318 aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
319 if(!aShapesToAdd.empty()) {
320 if(aFusedShape.get()) {
321 aShapesToAdd.push_back(aFusedShape);
324 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
325 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
326 if(!aFillerAlgo->isDone() || aFillerAlgo->shape()->isNull() || !aFillerAlgo->isValid()) {
327 myFeature->setError("Error: PaveFiller algorithm failed.");
331 aMakeShapeList->appendAlgo(aFillerAlgo);
334 theMakeShapes.push_back(aMakeShapeList);
342 //=================================================================================================
343 void FeaturesPlugin_CompositeBoolean::storeModificationHistory(ResultBodyPtr theResultBody,
344 const GeomShapePtr theObject,
345 const ListOfShape& theTools,
346 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
349 int aModTag = theTag;
350 int anEdgesAndFacesTag = ++aModTag;
351 int aDelTag = ++anEdgesAndFacesTag;
354 const std::string aModName = "Modfied";
356 ListOfShape aTools = theTools;
357 aTools.push_back(theObject);
359 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMap = theMakeShape->mapOfSubShapes();
363 for(ListOfShape::const_iterator anIt = aTools.begin(); anIt != aTools.end(); anIt++) {
364 if((*anIt)->shapeType() == GeomAPI_Shape::EDGE) {
365 aTag = anEdgesAndFacesTag;
366 aName = aModName + "_Edge";
368 else if((*anIt)->shapeType() == GeomAPI_Shape::FACE) {
369 aTag = anEdgesAndFacesTag;
370 aName = aModName + "_Face";
375 theResultBody->loadAndOrientModifiedShapes(theMakeShape.get(), *anIt,
376 (*anIt)->shapeType() == GeomAPI_Shape::EDGE ?
377 GeomAPI_Shape::EDGE : GeomAPI_Shape::FACE, aTag, aName, *aMap.get());
378 theResultBody->loadDeletedShapes(theMakeShape.get(), *anIt, GeomAPI_Shape::FACE, aDelTag);